Sergei Shtylyov | 128296f | 2014-01-03 15:52:22 +0300 | [diff] [blame] | 1 | /* SuperH Ethernet device driver |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 2 | * |
Hisashi Nakamura | 966d6db | 2014-11-13 15:54:05 +0900 | [diff] [blame] | 3 | * Copyright (C) 2014 Renesas Electronics Corporation |
Nobuhiro Iwamatsu | f0e81fe | 2012-03-25 18:59:51 +0000 | [diff] [blame] | 4 | * Copyright (C) 2006-2012 Nobuhiro Iwamatsu |
Sergei Shtylyov | b356e97 | 2014-02-18 03:12:43 +0300 | [diff] [blame] | 5 | * Copyright (C) 2008-2014 Renesas Solutions Corp. |
Sergei Shtylyov | b2b14d2 | 2016-02-10 01:38:28 +0300 | [diff] [blame] | 6 | * Copyright (C) 2013-2016 Cogent Embedded, Inc. |
Ben Dooks | 702eca0 | 2014-03-12 17:47:40 +0000 | [diff] [blame] | 7 | * Copyright (C) 2014 Codethink Limited |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 8 | * |
| 9 | * This program is free software; you can redistribute it and/or modify it |
| 10 | * under the terms and conditions of the GNU General Public License, |
| 11 | * version 2, as published by the Free Software Foundation. |
| 12 | * |
| 13 | * This program is distributed in the hope it will be useful, but WITHOUT |
| 14 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 15 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
| 16 | * more details. |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 17 | * |
| 18 | * The full GNU General Public License is included in this distribution in |
| 19 | * the file called "COPYING". |
| 20 | */ |
| 21 | |
Yoshihiro Shimoda | 0654011 | 2011-09-29 17:16:57 +0000 | [diff] [blame] | 22 | #include <linux/module.h> |
| 23 | #include <linux/kernel.h> |
| 24 | #include <linux/spinlock.h> |
David S. Miller | 823dcd2 | 2011-08-20 10:39:12 -0700 | [diff] [blame] | 25 | #include <linux/interrupt.h> |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 26 | #include <linux/dma-mapping.h> |
| 27 | #include <linux/etherdevice.h> |
| 28 | #include <linux/delay.h> |
| 29 | #include <linux/platform_device.h> |
| 30 | #include <linux/mdio-bitbang.h> |
| 31 | #include <linux/netdevice.h> |
Sergei Shtylyov | b356e97 | 2014-02-18 03:12:43 +0300 | [diff] [blame] | 32 | #include <linux/of.h> |
| 33 | #include <linux/of_device.h> |
| 34 | #include <linux/of_irq.h> |
| 35 | #include <linux/of_net.h> |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 36 | #include <linux/phy.h> |
| 37 | #include <linux/cache.h> |
| 38 | #include <linux/io.h> |
Magnus Damm | bcd5149 | 2009-10-09 00:20:04 +0000 | [diff] [blame] | 39 | #include <linux/pm_runtime.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 40 | #include <linux/slab.h> |
Nobuhiro Iwamatsu | dc19e4e | 2011-02-15 21:17:32 +0000 | [diff] [blame] | 41 | #include <linux/ethtool.h> |
Yoshihiro Shimoda | fdb37a7 | 2012-02-06 23:55:15 +0000 | [diff] [blame] | 42 | #include <linux/if_vlan.h> |
Nobuhiro Iwamatsu | f0e81fe | 2012-03-25 18:59:51 +0000 | [diff] [blame] | 43 | #include <linux/clk.h> |
Yoshihiro Shimoda | d4fa0e3 | 2011-09-27 21:49:12 +0000 | [diff] [blame] | 44 | #include <linux/sh_eth.h> |
Ben Dooks | 702eca0 | 2014-03-12 17:47:40 +0000 | [diff] [blame] | 45 | #include <linux/of_mdio.h> |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 46 | |
| 47 | #include "sh_eth.h" |
| 48 | |
Nobuhiro Iwamatsu | dc19e4e | 2011-02-15 21:17:32 +0000 | [diff] [blame] | 49 | #define SH_ETH_DEF_MSG_ENABLE \ |
| 50 | (NETIF_MSG_LINK | \ |
| 51 | NETIF_MSG_TIMER | \ |
| 52 | NETIF_MSG_RX_ERR| \ |
| 53 | NETIF_MSG_TX_ERR) |
| 54 | |
Sergei Shtylyov | 2274d37 | 2015-12-13 01:44:50 +0300 | [diff] [blame] | 55 | #define SH_ETH_OFFSET_INVALID ((u16)~0) |
| 56 | |
Ben Hutchings | 3365711 | 2015-02-26 20:34:14 +0000 | [diff] [blame] | 57 | #define SH_ETH_OFFSET_DEFAULTS \ |
| 58 | [0 ... SH_ETH_MAX_REGISTER_OFFSET - 1] = SH_ETH_OFFSET_INVALID |
| 59 | |
Sergei Shtylyov | c0013f6 | 2013-03-28 11:48:26 +0000 | [diff] [blame] | 60 | static const u16 sh_eth_offset_gigabit[SH_ETH_MAX_REGISTER_OFFSET] = { |
Ben Hutchings | 3365711 | 2015-02-26 20:34:14 +0000 | [diff] [blame] | 61 | SH_ETH_OFFSET_DEFAULTS, |
| 62 | |
Sergei Shtylyov | c0013f6 | 2013-03-28 11:48:26 +0000 | [diff] [blame] | 63 | [EDSR] = 0x0000, |
| 64 | [EDMR] = 0x0400, |
| 65 | [EDTRR] = 0x0408, |
| 66 | [EDRRR] = 0x0410, |
| 67 | [EESR] = 0x0428, |
| 68 | [EESIPR] = 0x0430, |
| 69 | [TDLAR] = 0x0010, |
| 70 | [TDFAR] = 0x0014, |
| 71 | [TDFXR] = 0x0018, |
| 72 | [TDFFR] = 0x001c, |
| 73 | [RDLAR] = 0x0030, |
| 74 | [RDFAR] = 0x0034, |
| 75 | [RDFXR] = 0x0038, |
| 76 | [RDFFR] = 0x003c, |
| 77 | [TRSCER] = 0x0438, |
| 78 | [RMFCR] = 0x0440, |
| 79 | [TFTR] = 0x0448, |
| 80 | [FDR] = 0x0450, |
| 81 | [RMCR] = 0x0458, |
| 82 | [RPADIR] = 0x0460, |
| 83 | [FCFTR] = 0x0468, |
| 84 | [CSMR] = 0x04E4, |
| 85 | |
| 86 | [ECMR] = 0x0500, |
| 87 | [ECSR] = 0x0510, |
| 88 | [ECSIPR] = 0x0518, |
| 89 | [PIR] = 0x0520, |
| 90 | [PSR] = 0x0528, |
| 91 | [PIPR] = 0x052c, |
| 92 | [RFLR] = 0x0508, |
| 93 | [APR] = 0x0554, |
| 94 | [MPR] = 0x0558, |
| 95 | [PFTCR] = 0x055c, |
| 96 | [PFRCR] = 0x0560, |
| 97 | [TPAUSER] = 0x0564, |
| 98 | [GECMR] = 0x05b0, |
| 99 | [BCULR] = 0x05b4, |
| 100 | [MAHR] = 0x05c0, |
| 101 | [MALR] = 0x05c8, |
| 102 | [TROCR] = 0x0700, |
| 103 | [CDCR] = 0x0708, |
| 104 | [LCCR] = 0x0710, |
| 105 | [CEFCR] = 0x0740, |
| 106 | [FRECR] = 0x0748, |
| 107 | [TSFRCR] = 0x0750, |
| 108 | [TLFRCR] = 0x0758, |
| 109 | [RFCR] = 0x0760, |
| 110 | [CERCR] = 0x0768, |
| 111 | [CEECR] = 0x0770, |
| 112 | [MAFCR] = 0x0778, |
| 113 | [RMII_MII] = 0x0790, |
| 114 | |
| 115 | [ARSTR] = 0x0000, |
| 116 | [TSU_CTRST] = 0x0004, |
| 117 | [TSU_FWEN0] = 0x0010, |
| 118 | [TSU_FWEN1] = 0x0014, |
| 119 | [TSU_FCM] = 0x0018, |
| 120 | [TSU_BSYSL0] = 0x0020, |
| 121 | [TSU_BSYSL1] = 0x0024, |
| 122 | [TSU_PRISL0] = 0x0028, |
| 123 | [TSU_PRISL1] = 0x002c, |
| 124 | [TSU_FWSL0] = 0x0030, |
| 125 | [TSU_FWSL1] = 0x0034, |
| 126 | [TSU_FWSLC] = 0x0038, |
| 127 | [TSU_QTAG0] = 0x0040, |
| 128 | [TSU_QTAG1] = 0x0044, |
| 129 | [TSU_FWSR] = 0x0050, |
| 130 | [TSU_FWINMK] = 0x0054, |
| 131 | [TSU_ADQT0] = 0x0048, |
| 132 | [TSU_ADQT1] = 0x004c, |
| 133 | [TSU_VTAG0] = 0x0058, |
| 134 | [TSU_VTAG1] = 0x005c, |
| 135 | [TSU_ADSBSY] = 0x0060, |
| 136 | [TSU_TEN] = 0x0064, |
| 137 | [TSU_POST1] = 0x0070, |
| 138 | [TSU_POST2] = 0x0074, |
| 139 | [TSU_POST3] = 0x0078, |
| 140 | [TSU_POST4] = 0x007c, |
| 141 | [TSU_ADRH0] = 0x0100, |
Sergei Shtylyov | c0013f6 | 2013-03-28 11:48:26 +0000 | [diff] [blame] | 142 | |
| 143 | [TXNLCR0] = 0x0080, |
| 144 | [TXALCR0] = 0x0084, |
| 145 | [RXNLCR0] = 0x0088, |
| 146 | [RXALCR0] = 0x008c, |
| 147 | [FWNLCR0] = 0x0090, |
| 148 | [FWALCR0] = 0x0094, |
| 149 | [TXNLCR1] = 0x00a0, |
| 150 | [TXALCR1] = 0x00a0, |
| 151 | [RXNLCR1] = 0x00a8, |
| 152 | [RXALCR1] = 0x00ac, |
| 153 | [FWNLCR1] = 0x00b0, |
| 154 | [FWALCR1] = 0x00b4, |
| 155 | }; |
| 156 | |
Simon Horman | db89347 | 2014-01-17 09:22:28 +0900 | [diff] [blame] | 157 | static const u16 sh_eth_offset_fast_rz[SH_ETH_MAX_REGISTER_OFFSET] = { |
Ben Hutchings | 3365711 | 2015-02-26 20:34:14 +0000 | [diff] [blame] | 158 | SH_ETH_OFFSET_DEFAULTS, |
| 159 | |
Simon Horman | db89347 | 2014-01-17 09:22:28 +0900 | [diff] [blame] | 160 | [EDSR] = 0x0000, |
| 161 | [EDMR] = 0x0400, |
| 162 | [EDTRR] = 0x0408, |
| 163 | [EDRRR] = 0x0410, |
| 164 | [EESR] = 0x0428, |
| 165 | [EESIPR] = 0x0430, |
| 166 | [TDLAR] = 0x0010, |
| 167 | [TDFAR] = 0x0014, |
| 168 | [TDFXR] = 0x0018, |
| 169 | [TDFFR] = 0x001c, |
| 170 | [RDLAR] = 0x0030, |
| 171 | [RDFAR] = 0x0034, |
| 172 | [RDFXR] = 0x0038, |
| 173 | [RDFFR] = 0x003c, |
| 174 | [TRSCER] = 0x0438, |
| 175 | [RMFCR] = 0x0440, |
| 176 | [TFTR] = 0x0448, |
| 177 | [FDR] = 0x0450, |
| 178 | [RMCR] = 0x0458, |
| 179 | [RPADIR] = 0x0460, |
| 180 | [FCFTR] = 0x0468, |
| 181 | [CSMR] = 0x04E4, |
| 182 | |
| 183 | [ECMR] = 0x0500, |
| 184 | [RFLR] = 0x0508, |
| 185 | [ECSR] = 0x0510, |
| 186 | [ECSIPR] = 0x0518, |
| 187 | [PIR] = 0x0520, |
| 188 | [APR] = 0x0554, |
| 189 | [MPR] = 0x0558, |
| 190 | [PFTCR] = 0x055c, |
| 191 | [PFRCR] = 0x0560, |
| 192 | [TPAUSER] = 0x0564, |
| 193 | [MAHR] = 0x05c0, |
| 194 | [MALR] = 0x05c8, |
| 195 | [CEFCR] = 0x0740, |
| 196 | [FRECR] = 0x0748, |
| 197 | [TSFRCR] = 0x0750, |
| 198 | [TLFRCR] = 0x0758, |
| 199 | [RFCR] = 0x0760, |
| 200 | [MAFCR] = 0x0778, |
| 201 | |
| 202 | [ARSTR] = 0x0000, |
| 203 | [TSU_CTRST] = 0x0004, |
| 204 | [TSU_VTAG0] = 0x0058, |
| 205 | [TSU_ADSBSY] = 0x0060, |
| 206 | [TSU_TEN] = 0x0064, |
| 207 | [TSU_ADRH0] = 0x0100, |
Simon Horman | db89347 | 2014-01-17 09:22:28 +0900 | [diff] [blame] | 208 | |
| 209 | [TXNLCR0] = 0x0080, |
| 210 | [TXALCR0] = 0x0084, |
| 211 | [RXNLCR0] = 0x0088, |
| 212 | [RXALCR0] = 0x008C, |
| 213 | }; |
| 214 | |
Sergei Shtylyov | a3f109b | 2013-03-28 11:51:31 +0000 | [diff] [blame] | 215 | static const u16 sh_eth_offset_fast_rcar[SH_ETH_MAX_REGISTER_OFFSET] = { |
Ben Hutchings | 3365711 | 2015-02-26 20:34:14 +0000 | [diff] [blame] | 216 | SH_ETH_OFFSET_DEFAULTS, |
| 217 | |
Sergei Shtylyov | a3f109b | 2013-03-28 11:51:31 +0000 | [diff] [blame] | 218 | [ECMR] = 0x0300, |
| 219 | [RFLR] = 0x0308, |
| 220 | [ECSR] = 0x0310, |
| 221 | [ECSIPR] = 0x0318, |
| 222 | [PIR] = 0x0320, |
| 223 | [PSR] = 0x0328, |
| 224 | [RDMLR] = 0x0340, |
| 225 | [IPGR] = 0x0350, |
| 226 | [APR] = 0x0354, |
| 227 | [MPR] = 0x0358, |
| 228 | [RFCF] = 0x0360, |
| 229 | [TPAUSER] = 0x0364, |
| 230 | [TPAUSECR] = 0x0368, |
| 231 | [MAHR] = 0x03c0, |
| 232 | [MALR] = 0x03c8, |
| 233 | [TROCR] = 0x03d0, |
| 234 | [CDCR] = 0x03d4, |
| 235 | [LCCR] = 0x03d8, |
| 236 | [CNDCR] = 0x03dc, |
| 237 | [CEFCR] = 0x03e4, |
| 238 | [FRECR] = 0x03e8, |
| 239 | [TSFRCR] = 0x03ec, |
| 240 | [TLFRCR] = 0x03f0, |
| 241 | [RFCR] = 0x03f4, |
| 242 | [MAFCR] = 0x03f8, |
| 243 | |
| 244 | [EDMR] = 0x0200, |
| 245 | [EDTRR] = 0x0208, |
| 246 | [EDRRR] = 0x0210, |
| 247 | [TDLAR] = 0x0218, |
| 248 | [RDLAR] = 0x0220, |
| 249 | [EESR] = 0x0228, |
| 250 | [EESIPR] = 0x0230, |
| 251 | [TRSCER] = 0x0238, |
| 252 | [RMFCR] = 0x0240, |
| 253 | [TFTR] = 0x0248, |
| 254 | [FDR] = 0x0250, |
| 255 | [RMCR] = 0x0258, |
| 256 | [TFUCR] = 0x0264, |
| 257 | [RFOCR] = 0x0268, |
Simon Horman | 55754f1 | 2013-07-23 10:18:04 +0900 | [diff] [blame] | 258 | [RMIIMODE] = 0x026c, |
Sergei Shtylyov | a3f109b | 2013-03-28 11:51:31 +0000 | [diff] [blame] | 259 | [FCFTR] = 0x0270, |
| 260 | [TRIMD] = 0x027c, |
| 261 | }; |
| 262 | |
Sergei Shtylyov | c0013f6 | 2013-03-28 11:48:26 +0000 | [diff] [blame] | 263 | static const u16 sh_eth_offset_fast_sh4[SH_ETH_MAX_REGISTER_OFFSET] = { |
Ben Hutchings | 3365711 | 2015-02-26 20:34:14 +0000 | [diff] [blame] | 264 | SH_ETH_OFFSET_DEFAULTS, |
| 265 | |
Sergei Shtylyov | c0013f6 | 2013-03-28 11:48:26 +0000 | [diff] [blame] | 266 | [ECMR] = 0x0100, |
| 267 | [RFLR] = 0x0108, |
| 268 | [ECSR] = 0x0110, |
| 269 | [ECSIPR] = 0x0118, |
| 270 | [PIR] = 0x0120, |
| 271 | [PSR] = 0x0128, |
| 272 | [RDMLR] = 0x0140, |
| 273 | [IPGR] = 0x0150, |
| 274 | [APR] = 0x0154, |
| 275 | [MPR] = 0x0158, |
| 276 | [TPAUSER] = 0x0164, |
| 277 | [RFCF] = 0x0160, |
| 278 | [TPAUSECR] = 0x0168, |
| 279 | [BCFRR] = 0x016c, |
| 280 | [MAHR] = 0x01c0, |
| 281 | [MALR] = 0x01c8, |
| 282 | [TROCR] = 0x01d0, |
| 283 | [CDCR] = 0x01d4, |
| 284 | [LCCR] = 0x01d8, |
| 285 | [CNDCR] = 0x01dc, |
| 286 | [CEFCR] = 0x01e4, |
| 287 | [FRECR] = 0x01e8, |
| 288 | [TSFRCR] = 0x01ec, |
| 289 | [TLFRCR] = 0x01f0, |
| 290 | [RFCR] = 0x01f4, |
| 291 | [MAFCR] = 0x01f8, |
| 292 | [RTRATE] = 0x01fc, |
| 293 | |
| 294 | [EDMR] = 0x0000, |
| 295 | [EDTRR] = 0x0008, |
| 296 | [EDRRR] = 0x0010, |
| 297 | [TDLAR] = 0x0018, |
| 298 | [RDLAR] = 0x0020, |
| 299 | [EESR] = 0x0028, |
| 300 | [EESIPR] = 0x0030, |
| 301 | [TRSCER] = 0x0038, |
| 302 | [RMFCR] = 0x0040, |
| 303 | [TFTR] = 0x0048, |
| 304 | [FDR] = 0x0050, |
| 305 | [RMCR] = 0x0058, |
| 306 | [TFUCR] = 0x0064, |
| 307 | [RFOCR] = 0x0068, |
| 308 | [FCFTR] = 0x0070, |
| 309 | [RPADIR] = 0x0078, |
| 310 | [TRIMD] = 0x007c, |
| 311 | [RBWAR] = 0x00c8, |
| 312 | [RDFAR] = 0x00cc, |
| 313 | [TBRAR] = 0x00d4, |
| 314 | [TDFAR] = 0x00d8, |
| 315 | }; |
| 316 | |
| 317 | static const u16 sh_eth_offset_fast_sh3_sh2[SH_ETH_MAX_REGISTER_OFFSET] = { |
Ben Hutchings | 3365711 | 2015-02-26 20:34:14 +0000 | [diff] [blame] | 318 | SH_ETH_OFFSET_DEFAULTS, |
| 319 | |
Sergei Shtylyov | d8b0426 | 2014-06-03 23:42:26 +0400 | [diff] [blame] | 320 | [EDMR] = 0x0000, |
| 321 | [EDTRR] = 0x0004, |
| 322 | [EDRRR] = 0x0008, |
| 323 | [TDLAR] = 0x000c, |
| 324 | [RDLAR] = 0x0010, |
| 325 | [EESR] = 0x0014, |
| 326 | [EESIPR] = 0x0018, |
| 327 | [TRSCER] = 0x001c, |
| 328 | [RMFCR] = 0x0020, |
| 329 | [TFTR] = 0x0024, |
| 330 | [FDR] = 0x0028, |
| 331 | [RMCR] = 0x002c, |
| 332 | [EDOCR] = 0x0030, |
| 333 | [FCFTR] = 0x0034, |
| 334 | [RPADIR] = 0x0038, |
| 335 | [TRIMD] = 0x003c, |
| 336 | [RBWAR] = 0x0040, |
| 337 | [RDFAR] = 0x0044, |
| 338 | [TBRAR] = 0x004c, |
| 339 | [TDFAR] = 0x0050, |
| 340 | |
Sergei Shtylyov | c0013f6 | 2013-03-28 11:48:26 +0000 | [diff] [blame] | 341 | [ECMR] = 0x0160, |
| 342 | [ECSR] = 0x0164, |
| 343 | [ECSIPR] = 0x0168, |
| 344 | [PIR] = 0x016c, |
| 345 | [MAHR] = 0x0170, |
| 346 | [MALR] = 0x0174, |
| 347 | [RFLR] = 0x0178, |
| 348 | [PSR] = 0x017c, |
| 349 | [TROCR] = 0x0180, |
| 350 | [CDCR] = 0x0184, |
| 351 | [LCCR] = 0x0188, |
| 352 | [CNDCR] = 0x018c, |
| 353 | [CEFCR] = 0x0194, |
| 354 | [FRECR] = 0x0198, |
| 355 | [TSFRCR] = 0x019c, |
| 356 | [TLFRCR] = 0x01a0, |
| 357 | [RFCR] = 0x01a4, |
| 358 | [MAFCR] = 0x01a8, |
| 359 | [IPGR] = 0x01b4, |
| 360 | [APR] = 0x01b8, |
| 361 | [MPR] = 0x01bc, |
| 362 | [TPAUSER] = 0x01c4, |
| 363 | [BCFR] = 0x01cc, |
| 364 | |
| 365 | [ARSTR] = 0x0000, |
| 366 | [TSU_CTRST] = 0x0004, |
| 367 | [TSU_FWEN0] = 0x0010, |
| 368 | [TSU_FWEN1] = 0x0014, |
| 369 | [TSU_FCM] = 0x0018, |
| 370 | [TSU_BSYSL0] = 0x0020, |
| 371 | [TSU_BSYSL1] = 0x0024, |
| 372 | [TSU_PRISL0] = 0x0028, |
| 373 | [TSU_PRISL1] = 0x002c, |
| 374 | [TSU_FWSL0] = 0x0030, |
| 375 | [TSU_FWSL1] = 0x0034, |
| 376 | [TSU_FWSLC] = 0x0038, |
| 377 | [TSU_QTAGM0] = 0x0040, |
| 378 | [TSU_QTAGM1] = 0x0044, |
| 379 | [TSU_ADQT0] = 0x0048, |
| 380 | [TSU_ADQT1] = 0x004c, |
| 381 | [TSU_FWSR] = 0x0050, |
| 382 | [TSU_FWINMK] = 0x0054, |
| 383 | [TSU_ADSBSY] = 0x0060, |
| 384 | [TSU_TEN] = 0x0064, |
| 385 | [TSU_POST1] = 0x0070, |
| 386 | [TSU_POST2] = 0x0074, |
| 387 | [TSU_POST3] = 0x0078, |
| 388 | [TSU_POST4] = 0x007c, |
| 389 | |
| 390 | [TXNLCR0] = 0x0080, |
| 391 | [TXALCR0] = 0x0084, |
| 392 | [RXNLCR0] = 0x0088, |
| 393 | [RXALCR0] = 0x008c, |
| 394 | [FWNLCR0] = 0x0090, |
| 395 | [FWALCR0] = 0x0094, |
| 396 | [TXNLCR1] = 0x00a0, |
| 397 | [TXALCR1] = 0x00a0, |
| 398 | [RXNLCR1] = 0x00a8, |
| 399 | [RXALCR1] = 0x00ac, |
| 400 | [FWNLCR1] = 0x00b0, |
| 401 | [FWALCR1] = 0x00b4, |
| 402 | |
| 403 | [TSU_ADRH0] = 0x0100, |
Sergei Shtylyov | c0013f6 | 2013-03-28 11:48:26 +0000 | [diff] [blame] | 404 | }; |
| 405 | |
Ben Hutchings | 740c7f3 | 2015-01-27 00:49:32 +0000 | [diff] [blame] | 406 | static void sh_eth_rcv_snd_disable(struct net_device *ndev); |
| 407 | static struct net_device_stats *sh_eth_get_stats(struct net_device *ndev); |
| 408 | |
Sergei Shtylyov | 2274d37 | 2015-12-13 01:44:50 +0300 | [diff] [blame] | 409 | static void sh_eth_write(struct net_device *ndev, u32 data, int enum_index) |
| 410 | { |
| 411 | struct sh_eth_private *mdp = netdev_priv(ndev); |
| 412 | u16 offset = mdp->reg_offset[enum_index]; |
| 413 | |
| 414 | if (WARN_ON(offset == SH_ETH_OFFSET_INVALID)) |
| 415 | return; |
| 416 | |
| 417 | iowrite32(data, mdp->addr + offset); |
| 418 | } |
| 419 | |
| 420 | static u32 sh_eth_read(struct net_device *ndev, int enum_index) |
| 421 | { |
| 422 | struct sh_eth_private *mdp = netdev_priv(ndev); |
| 423 | u16 offset = mdp->reg_offset[enum_index]; |
| 424 | |
| 425 | if (WARN_ON(offset == SH_ETH_OFFSET_INVALID)) |
| 426 | return ~0U; |
| 427 | |
| 428 | return ioread32(mdp->addr + offset); |
| 429 | } |
| 430 | |
Sergei Shtylyov | b2b14d2 | 2016-02-10 01:38:28 +0300 | [diff] [blame] | 431 | static void sh_eth_modify(struct net_device *ndev, int enum_index, u32 clear, |
| 432 | u32 set) |
| 433 | { |
| 434 | sh_eth_write(ndev, (sh_eth_read(ndev, enum_index) & ~clear) | set, |
| 435 | enum_index); |
| 436 | } |
| 437 | |
Simon Horman | 504c8ca | 2014-01-17 09:22:27 +0900 | [diff] [blame] | 438 | static bool sh_eth_is_gether(struct sh_eth_private *mdp) |
Nobuhiro Iwamatsu | dabdde9 | 2013-06-06 09:51:39 +0000 | [diff] [blame] | 439 | { |
Simon Horman | 504c8ca | 2014-01-17 09:22:27 +0900 | [diff] [blame] | 440 | return mdp->reg_offset == sh_eth_offset_gigabit; |
Nobuhiro Iwamatsu | dabdde9 | 2013-06-06 09:51:39 +0000 | [diff] [blame] | 441 | } |
| 442 | |
Simon Horman | db89347 | 2014-01-17 09:22:28 +0900 | [diff] [blame] | 443 | static bool sh_eth_is_rz_fast_ether(struct sh_eth_private *mdp) |
| 444 | { |
| 445 | return mdp->reg_offset == sh_eth_offset_fast_rz; |
| 446 | } |
| 447 | |
Sergei Shtylyov | 8e99440 | 2013-06-12 03:07:29 +0400 | [diff] [blame] | 448 | static void sh_eth_select_mii(struct net_device *ndev) |
Nobuhiro Iwamatsu | 5e7a76b | 2012-06-25 17:34:14 +0000 | [diff] [blame] | 449 | { |
Nobuhiro Iwamatsu | 5e7a76b | 2012-06-25 17:34:14 +0000 | [diff] [blame] | 450 | struct sh_eth_private *mdp = netdev_priv(ndev); |
Sergei Shtylyov | 4fa8c3c | 2016-03-13 01:29:45 +0300 | [diff] [blame] | 451 | u32 value; |
Nobuhiro Iwamatsu | 5e7a76b | 2012-06-25 17:34:14 +0000 | [diff] [blame] | 452 | |
| 453 | switch (mdp->phy_interface) { |
| 454 | case PHY_INTERFACE_MODE_GMII: |
| 455 | value = 0x2; |
| 456 | break; |
| 457 | case PHY_INTERFACE_MODE_MII: |
| 458 | value = 0x1; |
| 459 | break; |
| 460 | case PHY_INTERFACE_MODE_RMII: |
| 461 | value = 0x0; |
| 462 | break; |
| 463 | default: |
Sergei Shtylyov | f75f14e | 2014-03-15 03:27:54 +0300 | [diff] [blame] | 464 | netdev_warn(ndev, |
| 465 | "PHY interface mode was not setup. Set to MII.\n"); |
Nobuhiro Iwamatsu | 5e7a76b | 2012-06-25 17:34:14 +0000 | [diff] [blame] | 466 | value = 0x1; |
| 467 | break; |
| 468 | } |
| 469 | |
| 470 | sh_eth_write(ndev, value, RMII_MII); |
| 471 | } |
Nobuhiro Iwamatsu | 5e7a76b | 2012-06-25 17:34:14 +0000 | [diff] [blame] | 472 | |
Sergei Shtylyov | 8e99440 | 2013-06-12 03:07:29 +0400 | [diff] [blame] | 473 | static void sh_eth_set_duplex(struct net_device *ndev) |
Yoshihiro Shimoda | 65ac885 | 2009-05-24 23:54:30 +0000 | [diff] [blame] | 474 | { |
| 475 | struct sh_eth_private *mdp = netdev_priv(ndev); |
Yoshihiro Shimoda | 65ac885 | 2009-05-24 23:54:30 +0000 | [diff] [blame] | 476 | |
Sergei Shtylyov | b2b14d2 | 2016-02-10 01:38:28 +0300 | [diff] [blame] | 477 | sh_eth_modify(ndev, ECMR, ECMR_DM, mdp->duplex ? ECMR_DM : 0); |
Yoshihiro Shimoda | 65ac885 | 2009-05-24 23:54:30 +0000 | [diff] [blame] | 478 | } |
| 479 | |
Geert Uytterhoeven | 99f84be | 2015-11-24 15:40:57 +0100 | [diff] [blame] | 480 | static void sh_eth_chip_reset(struct net_device *ndev) |
| 481 | { |
| 482 | struct sh_eth_private *mdp = netdev_priv(ndev); |
| 483 | |
| 484 | /* reset device */ |
| 485 | sh_eth_tsu_write(mdp, ARSTR_ARSTR, ARSTR); |
| 486 | mdelay(1); |
| 487 | } |
| 488 | |
Geert Uytterhoeven | a0f48be | 2015-11-24 15:40:59 +0100 | [diff] [blame] | 489 | static void sh_eth_set_rate_gether(struct net_device *ndev) |
| 490 | { |
| 491 | struct sh_eth_private *mdp = netdev_priv(ndev); |
| 492 | |
| 493 | switch (mdp->speed) { |
| 494 | case 10: /* 10BASE */ |
| 495 | sh_eth_write(ndev, GECMR_10, GECMR); |
| 496 | break; |
| 497 | case 100:/* 100BASE */ |
| 498 | sh_eth_write(ndev, GECMR_100, GECMR); |
| 499 | break; |
| 500 | case 1000: /* 1000BASE */ |
| 501 | sh_eth_write(ndev, GECMR_1000, GECMR); |
| 502 | break; |
Geert Uytterhoeven | a0f48be | 2015-11-24 15:40:59 +0100 | [diff] [blame] | 503 | } |
| 504 | } |
| 505 | |
Geert Uytterhoeven | 99f84be | 2015-11-24 15:40:57 +0100 | [diff] [blame] | 506 | #ifdef CONFIG_OF |
| 507 | /* R7S72100 */ |
| 508 | static struct sh_eth_cpu_data r7s72100_data = { |
| 509 | .chip_reset = sh_eth_chip_reset, |
| 510 | .set_duplex = sh_eth_set_duplex, |
| 511 | |
| 512 | .register_type = SH_ETH_REG_FAST_RZ, |
| 513 | |
| 514 | .ecsr_value = ECSR_ICD, |
| 515 | .ecsipr_value = ECSIPR_ICDIP, |
| 516 | .eesipr_value = 0xff7f009f, |
| 517 | |
| 518 | .tx_check = EESR_TC1 | EESR_FTC, |
| 519 | .eesr_err_check = EESR_TWB1 | EESR_TWB | EESR_TABT | EESR_RABT | |
| 520 | EESR_RFE | EESR_RDE | EESR_RFRMER | EESR_TFE | |
| 521 | EESR_TDE | EESR_ECI, |
| 522 | .fdr_value = 0x0000070f, |
| 523 | |
| 524 | .no_psr = 1, |
| 525 | .apr = 1, |
| 526 | .mpr = 1, |
| 527 | .tpauser = 1, |
| 528 | .hw_swap = 1, |
| 529 | .rpadir = 1, |
| 530 | .rpadir_value = 2 << 16, |
| 531 | .no_trimd = 1, |
| 532 | .no_ade = 1, |
| 533 | .hw_crc = 1, |
| 534 | .tsu = 1, |
| 535 | .shift_rd0 = 1, |
| 536 | }; |
Geert Uytterhoeven | a0f48be | 2015-11-24 15:40:59 +0100 | [diff] [blame] | 537 | |
| 538 | static void sh_eth_chip_reset_r8a7740(struct net_device *ndev) |
| 539 | { |
| 540 | struct sh_eth_private *mdp = netdev_priv(ndev); |
| 541 | |
| 542 | /* reset device */ |
| 543 | sh_eth_tsu_write(mdp, ARSTR_ARSTR, ARSTR); |
| 544 | mdelay(1); |
| 545 | |
| 546 | sh_eth_select_mii(ndev); |
| 547 | } |
| 548 | |
| 549 | /* R8A7740 */ |
| 550 | static struct sh_eth_cpu_data r8a7740_data = { |
| 551 | .chip_reset = sh_eth_chip_reset_r8a7740, |
| 552 | .set_duplex = sh_eth_set_duplex, |
| 553 | .set_rate = sh_eth_set_rate_gether, |
| 554 | |
| 555 | .register_type = SH_ETH_REG_GIGABIT, |
| 556 | |
| 557 | .ecsr_value = ECSR_ICD | ECSR_MPD, |
| 558 | .ecsipr_value = ECSIPR_LCHNGIP | ECSIPR_ICDIP | ECSIPR_MPDIP, |
| 559 | .eesipr_value = DMAC_M_RFRMER | DMAC_M_ECI | 0x003fffff, |
| 560 | |
| 561 | .tx_check = EESR_TC1 | EESR_FTC, |
| 562 | .eesr_err_check = EESR_TWB1 | EESR_TWB | EESR_TABT | EESR_RABT | |
| 563 | EESR_RFE | EESR_RDE | EESR_RFRMER | EESR_TFE | |
| 564 | EESR_TDE | EESR_ECI, |
| 565 | .fdr_value = 0x0000070f, |
| 566 | |
| 567 | .apr = 1, |
| 568 | .mpr = 1, |
| 569 | .tpauser = 1, |
| 570 | .bculr = 1, |
| 571 | .hw_swap = 1, |
| 572 | .rpadir = 1, |
| 573 | .rpadir_value = 2 << 16, |
| 574 | .no_trimd = 1, |
| 575 | .no_ade = 1, |
| 576 | .tsu = 1, |
| 577 | .select_mii = 1, |
| 578 | .shift_rd0 = 1, |
| 579 | }; |
Geert Uytterhoeven | 99f84be | 2015-11-24 15:40:57 +0100 | [diff] [blame] | 580 | |
Nobuhiro Iwamatsu | 04b0ed2 | 2013-06-06 09:45:25 +0000 | [diff] [blame] | 581 | /* There is CPU dependent code */ |
Sergei Shtylyov | 589ebde | 2013-06-07 14:05:59 +0000 | [diff] [blame] | 582 | static void sh_eth_set_rate_r8a777x(struct net_device *ndev) |
Yoshihiro Shimoda | 65ac885 | 2009-05-24 23:54:30 +0000 | [diff] [blame] | 583 | { |
| 584 | struct sh_eth_private *mdp = netdev_priv(ndev); |
Yoshihiro Shimoda | 65ac885 | 2009-05-24 23:54:30 +0000 | [diff] [blame] | 585 | |
| 586 | switch (mdp->speed) { |
| 587 | case 10: /* 10BASE */ |
Sergei Shtylyov | b2b14d2 | 2016-02-10 01:38:28 +0300 | [diff] [blame] | 588 | sh_eth_modify(ndev, ECMR, ECMR_ELB, 0); |
Yoshihiro Shimoda | 65ac885 | 2009-05-24 23:54:30 +0000 | [diff] [blame] | 589 | break; |
| 590 | case 100:/* 100BASE */ |
Sergei Shtylyov | b2b14d2 | 2016-02-10 01:38:28 +0300 | [diff] [blame] | 591 | sh_eth_modify(ndev, ECMR, ECMR_ELB, ECMR_ELB); |
Sergei Shtylyov | a3f109b | 2013-03-28 11:51:31 +0000 | [diff] [blame] | 592 | break; |
Sergei Shtylyov | a3f109b | 2013-03-28 11:51:31 +0000 | [diff] [blame] | 593 | } |
| 594 | } |
| 595 | |
Sergei Shtylyov | 674853b | 2013-04-27 10:44:24 +0000 | [diff] [blame] | 596 | /* R8A7778/9 */ |
Sergei Shtylyov | 589ebde | 2013-06-07 14:05:59 +0000 | [diff] [blame] | 597 | static struct sh_eth_cpu_data r8a777x_data = { |
Sergei Shtylyov | a3f109b | 2013-03-28 11:51:31 +0000 | [diff] [blame] | 598 | .set_duplex = sh_eth_set_duplex, |
Sergei Shtylyov | 589ebde | 2013-06-07 14:05:59 +0000 | [diff] [blame] | 599 | .set_rate = sh_eth_set_rate_r8a777x, |
Sergei Shtylyov | a3f109b | 2013-03-28 11:51:31 +0000 | [diff] [blame] | 600 | |
Sergei Shtylyov | a3153d8 | 2013-08-18 03:11:28 +0400 | [diff] [blame] | 601 | .register_type = SH_ETH_REG_FAST_RCAR, |
| 602 | |
Sergei Shtylyov | a3f109b | 2013-03-28 11:51:31 +0000 | [diff] [blame] | 603 | .ecsr_value = ECSR_PSRTO | ECSR_LCHNG | ECSR_ICD, |
| 604 | .ecsipr_value = ECSIPR_PSRTOIP | ECSIPR_LCHNGIP | ECSIPR_ICDIP, |
| 605 | .eesipr_value = 0x01ff009f, |
| 606 | |
| 607 | .tx_check = EESR_FTC | EESR_CND | EESR_DLC | EESR_CD | EESR_RTO, |
Sergei Shtylyov | ca8c358 | 2013-06-21 01:12:21 +0400 | [diff] [blame] | 608 | .eesr_err_check = EESR_TWB | EESR_TABT | EESR_RABT | EESR_RFE | |
| 609 | EESR_RDE | EESR_RFRMER | EESR_TFE | EESR_TDE | |
| 610 | EESR_ECI, |
Nobuhiro Iwamatsu | d407bc0 | 2015-01-07 14:40:15 +0900 | [diff] [blame] | 611 | .fdr_value = 0x00000f0f, |
Sergei Shtylyov | a3f109b | 2013-03-28 11:51:31 +0000 | [diff] [blame] | 612 | |
| 613 | .apr = 1, |
| 614 | .mpr = 1, |
| 615 | .tpauser = 1, |
| 616 | .hw_swap = 1, |
| 617 | }; |
Sergei Shtylyov | a3f109b | 2013-03-28 11:51:31 +0000 | [diff] [blame] | 618 | |
Sergei Shtylyov | 94a12b1 | 2013-12-08 02:59:18 +0300 | [diff] [blame] | 619 | /* R8A7790/1 */ |
| 620 | static struct sh_eth_cpu_data r8a779x_data = { |
Simon Horman | e18dbf7 | 2013-07-23 10:18:05 +0900 | [diff] [blame] | 621 | .set_duplex = sh_eth_set_duplex, |
| 622 | .set_rate = sh_eth_set_rate_r8a777x, |
| 623 | |
Sergei Shtylyov | a3153d8 | 2013-08-18 03:11:28 +0400 | [diff] [blame] | 624 | .register_type = SH_ETH_REG_FAST_RCAR, |
| 625 | |
Simon Horman | e18dbf7 | 2013-07-23 10:18:05 +0900 | [diff] [blame] | 626 | .ecsr_value = ECSR_PSRTO | ECSR_LCHNG | ECSR_ICD, |
| 627 | .ecsipr_value = ECSIPR_PSRTOIP | ECSIPR_LCHNGIP | ECSIPR_ICDIP, |
| 628 | .eesipr_value = 0x01ff009f, |
| 629 | |
| 630 | .tx_check = EESR_FTC | EESR_CND | EESR_DLC | EESR_CD | EESR_RTO, |
Laurent Pinchart | ba361cb | 2013-07-31 16:42:11 +0900 | [diff] [blame] | 631 | .eesr_err_check = EESR_TWB | EESR_TABT | EESR_RABT | EESR_RFE | |
| 632 | EESR_RDE | EESR_RFRMER | EESR_TFE | EESR_TDE | |
| 633 | EESR_ECI, |
Nobuhiro Iwamatsu | d407bc0 | 2015-01-07 14:40:15 +0900 | [diff] [blame] | 634 | .fdr_value = 0x00000f0f, |
Simon Horman | e18dbf7 | 2013-07-23 10:18:05 +0900 | [diff] [blame] | 635 | |
Geert Uytterhoeven | 01fbd3f | 2015-01-15 11:52:19 +0100 | [diff] [blame] | 636 | .trscer_err_mask = DESC_I_RINT8, |
| 637 | |
Simon Horman | e18dbf7 | 2013-07-23 10:18:05 +0900 | [diff] [blame] | 638 | .apr = 1, |
| 639 | .mpr = 1, |
| 640 | .tpauser = 1, |
| 641 | .hw_swap = 1, |
| 642 | .rmiimode = 1, |
| 643 | }; |
Geert Uytterhoeven | c74a224 | 2015-11-24 15:40:58 +0100 | [diff] [blame] | 644 | #endif /* CONFIG_OF */ |
Simon Horman | e18dbf7 | 2013-07-23 10:18:05 +0900 | [diff] [blame] | 645 | |
Sergei Shtylyov | 9c3beaa | 2013-06-07 14:03:37 +0000 | [diff] [blame] | 646 | static void sh_eth_set_rate_sh7724(struct net_device *ndev) |
Sergei Shtylyov | a3f109b | 2013-03-28 11:51:31 +0000 | [diff] [blame] | 647 | { |
| 648 | struct sh_eth_private *mdp = netdev_priv(ndev); |
| 649 | |
| 650 | switch (mdp->speed) { |
| 651 | case 10: /* 10BASE */ |
Sergei Shtylyov | b2b14d2 | 2016-02-10 01:38:28 +0300 | [diff] [blame] | 652 | sh_eth_modify(ndev, ECMR, ECMR_RTM, 0); |
Sergei Shtylyov | a3f109b | 2013-03-28 11:51:31 +0000 | [diff] [blame] | 653 | break; |
| 654 | case 100:/* 100BASE */ |
Sergei Shtylyov | b2b14d2 | 2016-02-10 01:38:28 +0300 | [diff] [blame] | 655 | sh_eth_modify(ndev, ECMR, ECMR_RTM, ECMR_RTM); |
Yoshihiro Shimoda | 65ac885 | 2009-05-24 23:54:30 +0000 | [diff] [blame] | 656 | break; |
Yoshihiro Shimoda | 65ac885 | 2009-05-24 23:54:30 +0000 | [diff] [blame] | 657 | } |
| 658 | } |
| 659 | |
| 660 | /* SH7724 */ |
Sergei Shtylyov | 9c3beaa | 2013-06-07 14:03:37 +0000 | [diff] [blame] | 661 | static struct sh_eth_cpu_data sh7724_data = { |
Yoshihiro Shimoda | 65ac885 | 2009-05-24 23:54:30 +0000 | [diff] [blame] | 662 | .set_duplex = sh_eth_set_duplex, |
Sergei Shtylyov | 9c3beaa | 2013-06-07 14:03:37 +0000 | [diff] [blame] | 663 | .set_rate = sh_eth_set_rate_sh7724, |
Yoshihiro Shimoda | 65ac885 | 2009-05-24 23:54:30 +0000 | [diff] [blame] | 664 | |
Sergei Shtylyov | a3153d8 | 2013-08-18 03:11:28 +0400 | [diff] [blame] | 665 | .register_type = SH_ETH_REG_FAST_SH4, |
| 666 | |
Yoshihiro Shimoda | 65ac885 | 2009-05-24 23:54:30 +0000 | [diff] [blame] | 667 | .ecsr_value = ECSR_PSRTO | ECSR_LCHNG | ECSR_ICD, |
| 668 | .ecsipr_value = ECSIPR_PSRTOIP | ECSIPR_LCHNGIP | ECSIPR_ICDIP, |
Sergei Shtylyov | a80c3de | 2013-06-20 02:24:54 +0400 | [diff] [blame] | 669 | .eesipr_value = 0x01ff009f, |
Yoshihiro Shimoda | 65ac885 | 2009-05-24 23:54:30 +0000 | [diff] [blame] | 670 | |
| 671 | .tx_check = EESR_FTC | EESR_CND | EESR_DLC | EESR_CD | EESR_RTO, |
Sergei Shtylyov | ca8c358 | 2013-06-21 01:12:21 +0400 | [diff] [blame] | 672 | .eesr_err_check = EESR_TWB | EESR_TABT | EESR_RABT | EESR_RFE | |
| 673 | EESR_RDE | EESR_RFRMER | EESR_TFE | EESR_TDE | |
| 674 | EESR_ECI, |
Yoshihiro Shimoda | 65ac885 | 2009-05-24 23:54:30 +0000 | [diff] [blame] | 675 | |
| 676 | .apr = 1, |
| 677 | .mpr = 1, |
| 678 | .tpauser = 1, |
| 679 | .hw_swap = 1, |
Magnus Damm | 503914c | 2009-12-15 21:16:55 -0800 | [diff] [blame] | 680 | .rpadir = 1, |
| 681 | .rpadir_value = 0x00020000, /* NET_IP_ALIGN assumed to be 2 */ |
Yoshihiro Shimoda | 65ac885 | 2009-05-24 23:54:30 +0000 | [diff] [blame] | 682 | }; |
Nobuhiro Iwamatsu | 5cee1d3 | 2012-06-25 17:35:12 +0000 | [diff] [blame] | 683 | |
Sergei Shtylyov | 24549e2 | 2013-06-07 13:59:21 +0000 | [diff] [blame] | 684 | static void sh_eth_set_rate_sh7757(struct net_device *ndev) |
Yoshihiro Shimoda | f29a3d0 | 2010-07-05 18:32:50 +0000 | [diff] [blame] | 685 | { |
| 686 | struct sh_eth_private *mdp = netdev_priv(ndev); |
Yoshihiro Shimoda | f29a3d0 | 2010-07-05 18:32:50 +0000 | [diff] [blame] | 687 | |
| 688 | switch (mdp->speed) { |
| 689 | case 10: /* 10BASE */ |
Yoshihiro Shimoda | 4a55530 | 2011-03-07 21:59:26 +0000 | [diff] [blame] | 690 | sh_eth_write(ndev, 0, RTRATE); |
Yoshihiro Shimoda | f29a3d0 | 2010-07-05 18:32:50 +0000 | [diff] [blame] | 691 | break; |
| 692 | case 100:/* 100BASE */ |
Yoshihiro Shimoda | 4a55530 | 2011-03-07 21:59:26 +0000 | [diff] [blame] | 693 | sh_eth_write(ndev, 1, RTRATE); |
Yoshihiro Shimoda | f29a3d0 | 2010-07-05 18:32:50 +0000 | [diff] [blame] | 694 | break; |
Yoshihiro Shimoda | f29a3d0 | 2010-07-05 18:32:50 +0000 | [diff] [blame] | 695 | } |
| 696 | } |
| 697 | |
| 698 | /* SH7757 */ |
Sergei Shtylyov | 24549e2 | 2013-06-07 13:59:21 +0000 | [diff] [blame] | 699 | static struct sh_eth_cpu_data sh7757_data = { |
| 700 | .set_duplex = sh_eth_set_duplex, |
| 701 | .set_rate = sh_eth_set_rate_sh7757, |
Yoshihiro Shimoda | f29a3d0 | 2010-07-05 18:32:50 +0000 | [diff] [blame] | 702 | |
Sergei Shtylyov | a3153d8 | 2013-08-18 03:11:28 +0400 | [diff] [blame] | 703 | .register_type = SH_ETH_REG_FAST_SH4, |
| 704 | |
Yoshihiro Shimoda | f29a3d0 | 2010-07-05 18:32:50 +0000 | [diff] [blame] | 705 | .eesipr_value = DMAC_M_RFRMER | DMAC_M_ECI | 0x003fffff, |
Yoshihiro Shimoda | f29a3d0 | 2010-07-05 18:32:50 +0000 | [diff] [blame] | 706 | |
| 707 | .tx_check = EESR_FTC | EESR_CND | EESR_DLC | EESR_CD | EESR_RTO, |
Sergei Shtylyov | ca8c358 | 2013-06-21 01:12:21 +0400 | [diff] [blame] | 708 | .eesr_err_check = EESR_TWB | EESR_TABT | EESR_RABT | EESR_RFE | |
| 709 | EESR_RDE | EESR_RFRMER | EESR_TFE | EESR_TDE | |
| 710 | EESR_ECI, |
Yoshihiro Shimoda | f29a3d0 | 2010-07-05 18:32:50 +0000 | [diff] [blame] | 711 | |
Nobuhiro Iwamatsu | 5b3dfd1 | 2013-06-06 09:49:30 +0000 | [diff] [blame] | 712 | .irq_flags = IRQF_SHARED, |
Yoshihiro Shimoda | f29a3d0 | 2010-07-05 18:32:50 +0000 | [diff] [blame] | 713 | .apr = 1, |
| 714 | .mpr = 1, |
| 715 | .tpauser = 1, |
| 716 | .hw_swap = 1, |
| 717 | .no_ade = 1, |
Yoshihiro Shimoda | 2e98e79 | 2011-07-05 20:33:57 +0000 | [diff] [blame] | 718 | .rpadir = 1, |
| 719 | .rpadir_value = 2 << 16, |
Ben Hutchings | 6b4b4fe | 2015-02-26 20:34:35 +0000 | [diff] [blame] | 720 | .rtrate = 1, |
Yoshihiro Shimoda | f29a3d0 | 2010-07-05 18:32:50 +0000 | [diff] [blame] | 721 | }; |
Yoshihiro Shimoda | 65ac885 | 2009-05-24 23:54:30 +0000 | [diff] [blame] | 722 | |
David S. Miller | e403d29 | 2013-06-07 23:40:41 -0700 | [diff] [blame] | 723 | #define SH_GIGA_ETH_BASE 0xfee00000UL |
Yoshihiro Shimoda | 8fcd496 | 2011-03-07 21:59:49 +0000 | [diff] [blame] | 724 | #define GIGA_MALR(port) (SH_GIGA_ETH_BASE + 0x800 * (port) + 0x05c8) |
| 725 | #define GIGA_MAHR(port) (SH_GIGA_ETH_BASE + 0x800 * (port) + 0x05c0) |
| 726 | static void sh_eth_chip_reset_giga(struct net_device *ndev) |
| 727 | { |
| 728 | int i; |
Geert Uytterhoeven | 0799c2d | 2015-01-15 11:54:28 +0100 | [diff] [blame] | 729 | u32 mahr[2], malr[2]; |
Yoshihiro Shimoda | 8fcd496 | 2011-03-07 21:59:49 +0000 | [diff] [blame] | 730 | |
| 731 | /* save MAHR and MALR */ |
| 732 | for (i = 0; i < 2; i++) { |
Yoshihiro Shimoda | ae70644 | 2011-09-27 21:48:58 +0000 | [diff] [blame] | 733 | malr[i] = ioread32((void *)GIGA_MALR(i)); |
| 734 | mahr[i] = ioread32((void *)GIGA_MAHR(i)); |
Yoshihiro Shimoda | 8fcd496 | 2011-03-07 21:59:49 +0000 | [diff] [blame] | 735 | } |
| 736 | |
| 737 | /* reset device */ |
Yoshihiro Shimoda | ae70644 | 2011-09-27 21:48:58 +0000 | [diff] [blame] | 738 | iowrite32(ARSTR_ARSTR, (void *)(SH_GIGA_ETH_BASE + 0x1800)); |
Yoshihiro Shimoda | 8fcd496 | 2011-03-07 21:59:49 +0000 | [diff] [blame] | 739 | mdelay(1); |
| 740 | |
| 741 | /* restore MAHR and MALR */ |
| 742 | for (i = 0; i < 2; i++) { |
Yoshihiro Shimoda | ae70644 | 2011-09-27 21:48:58 +0000 | [diff] [blame] | 743 | iowrite32(malr[i], (void *)GIGA_MALR(i)); |
| 744 | iowrite32(mahr[i], (void *)GIGA_MAHR(i)); |
Yoshihiro Shimoda | 8fcd496 | 2011-03-07 21:59:49 +0000 | [diff] [blame] | 745 | } |
| 746 | } |
| 747 | |
Yoshihiro Shimoda | 8fcd496 | 2011-03-07 21:59:49 +0000 | [diff] [blame] | 748 | static void sh_eth_set_rate_giga(struct net_device *ndev) |
| 749 | { |
| 750 | struct sh_eth_private *mdp = netdev_priv(ndev); |
| 751 | |
| 752 | switch (mdp->speed) { |
| 753 | case 10: /* 10BASE */ |
| 754 | sh_eth_write(ndev, 0x00000000, GECMR); |
| 755 | break; |
| 756 | case 100:/* 100BASE */ |
| 757 | sh_eth_write(ndev, 0x00000010, GECMR); |
| 758 | break; |
| 759 | case 1000: /* 1000BASE */ |
| 760 | sh_eth_write(ndev, 0x00000020, GECMR); |
| 761 | break; |
Yoshihiro Shimoda | 8fcd496 | 2011-03-07 21:59:49 +0000 | [diff] [blame] | 762 | } |
| 763 | } |
| 764 | |
| 765 | /* SH7757(GETHERC) */ |
Sergei Shtylyov | 24549e2 | 2013-06-07 13:59:21 +0000 | [diff] [blame] | 766 | static struct sh_eth_cpu_data sh7757_data_giga = { |
Yoshihiro Shimoda | 8fcd496 | 2011-03-07 21:59:49 +0000 | [diff] [blame] | 767 | .chip_reset = sh_eth_chip_reset_giga, |
Nobuhiro Iwamatsu | 04b0ed2 | 2013-06-06 09:45:25 +0000 | [diff] [blame] | 768 | .set_duplex = sh_eth_set_duplex, |
Yoshihiro Shimoda | 8fcd496 | 2011-03-07 21:59:49 +0000 | [diff] [blame] | 769 | .set_rate = sh_eth_set_rate_giga, |
| 770 | |
Sergei Shtylyov | a3153d8 | 2013-08-18 03:11:28 +0400 | [diff] [blame] | 771 | .register_type = SH_ETH_REG_GIGABIT, |
| 772 | |
Yoshihiro Shimoda | 8fcd496 | 2011-03-07 21:59:49 +0000 | [diff] [blame] | 773 | .ecsr_value = ECSR_ICD | ECSR_MPD, |
| 774 | .ecsipr_value = ECSIPR_LCHNGIP | ECSIPR_ICDIP | ECSIPR_MPDIP, |
| 775 | .eesipr_value = DMAC_M_RFRMER | DMAC_M_ECI | 0x003fffff, |
| 776 | |
| 777 | .tx_check = EESR_TC1 | EESR_FTC, |
Sergei Shtylyov | ca8c358 | 2013-06-21 01:12:21 +0400 | [diff] [blame] | 778 | .eesr_err_check = EESR_TWB1 | EESR_TWB | EESR_TABT | EESR_RABT | |
| 779 | EESR_RFE | EESR_RDE | EESR_RFRMER | EESR_TFE | |
| 780 | EESR_TDE | EESR_ECI, |
Yoshihiro Shimoda | 8fcd496 | 2011-03-07 21:59:49 +0000 | [diff] [blame] | 781 | .fdr_value = 0x0000072f, |
Yoshihiro Shimoda | 8fcd496 | 2011-03-07 21:59:49 +0000 | [diff] [blame] | 782 | |
Nobuhiro Iwamatsu | 5b3dfd1 | 2013-06-06 09:49:30 +0000 | [diff] [blame] | 783 | .irq_flags = IRQF_SHARED, |
Yoshihiro Shimoda | 8fcd496 | 2011-03-07 21:59:49 +0000 | [diff] [blame] | 784 | .apr = 1, |
| 785 | .mpr = 1, |
| 786 | .tpauser = 1, |
| 787 | .bculr = 1, |
| 788 | .hw_swap = 1, |
| 789 | .rpadir = 1, |
| 790 | .rpadir_value = 2 << 16, |
| 791 | .no_trimd = 1, |
| 792 | .no_ade = 1, |
Yoshihiro Shimoda | 3acbc97 | 2012-02-15 17:54:51 +0000 | [diff] [blame] | 793 | .tsu = 1, |
Yoshihiro Shimoda | 8fcd496 | 2011-03-07 21:59:49 +0000 | [diff] [blame] | 794 | }; |
| 795 | |
Sergei Shtylyov | f5d1276 | 2013-06-07 13:58:18 +0000 | [diff] [blame] | 796 | /* SH7734 */ |
| 797 | static struct sh_eth_cpu_data sh7734_data = { |
Yoshihiro Shimoda | 380af9e | 2009-05-24 23:54:21 +0000 | [diff] [blame] | 798 | .chip_reset = sh_eth_chip_reset, |
| 799 | .set_duplex = sh_eth_set_duplex, |
Sergei Shtylyov | f5d1276 | 2013-06-07 13:58:18 +0000 | [diff] [blame] | 800 | .set_rate = sh_eth_set_rate_gether, |
Yoshihiro Shimoda | 380af9e | 2009-05-24 23:54:21 +0000 | [diff] [blame] | 801 | |
Sergei Shtylyov | a3153d8 | 2013-08-18 03:11:28 +0400 | [diff] [blame] | 802 | .register_type = SH_ETH_REG_GIGABIT, |
| 803 | |
Yoshihiro Shimoda | 380af9e | 2009-05-24 23:54:21 +0000 | [diff] [blame] | 804 | .ecsr_value = ECSR_ICD | ECSR_MPD, |
| 805 | .ecsipr_value = ECSIPR_LCHNGIP | ECSIPR_ICDIP | ECSIPR_MPDIP, |
| 806 | .eesipr_value = DMAC_M_RFRMER | DMAC_M_ECI | 0x003fffff, |
| 807 | |
| 808 | .tx_check = EESR_TC1 | EESR_FTC, |
Sergei Shtylyov | ca8c358 | 2013-06-21 01:12:21 +0400 | [diff] [blame] | 809 | .eesr_err_check = EESR_TWB1 | EESR_TWB | EESR_TABT | EESR_RABT | |
| 810 | EESR_RFE | EESR_RDE | EESR_RFRMER | EESR_TFE | |
| 811 | EESR_TDE | EESR_ECI, |
Yoshihiro Shimoda | 380af9e | 2009-05-24 23:54:21 +0000 | [diff] [blame] | 812 | |
| 813 | .apr = 1, |
| 814 | .mpr = 1, |
| 815 | .tpauser = 1, |
| 816 | .bculr = 1, |
| 817 | .hw_swap = 1, |
Yoshihiro Shimoda | 380af9e | 2009-05-24 23:54:21 +0000 | [diff] [blame] | 818 | .no_trimd = 1, |
| 819 | .no_ade = 1, |
Yoshihiro Shimoda | 4986b99 | 2011-03-07 21:59:34 +0000 | [diff] [blame] | 820 | .tsu = 1, |
Sergei Shtylyov | f5d1276 | 2013-06-07 13:58:18 +0000 | [diff] [blame] | 821 | .hw_crc = 1, |
| 822 | .select_mii = 1, |
Yoshihiro Shimoda | 380af9e | 2009-05-24 23:54:21 +0000 | [diff] [blame] | 823 | }; |
Sergei Shtylyov | f5d1276 | 2013-06-07 13:58:18 +0000 | [diff] [blame] | 824 | |
| 825 | /* SH7763 */ |
| 826 | static struct sh_eth_cpu_data sh7763_data = { |
| 827 | .chip_reset = sh_eth_chip_reset, |
| 828 | .set_duplex = sh_eth_set_duplex, |
| 829 | .set_rate = sh_eth_set_rate_gether, |
| 830 | |
Sergei Shtylyov | a3153d8 | 2013-08-18 03:11:28 +0400 | [diff] [blame] | 831 | .register_type = SH_ETH_REG_GIGABIT, |
| 832 | |
Sergei Shtylyov | f5d1276 | 2013-06-07 13:58:18 +0000 | [diff] [blame] | 833 | .ecsr_value = ECSR_ICD | ECSR_MPD, |
| 834 | .ecsipr_value = ECSIPR_LCHNGIP | ECSIPR_ICDIP | ECSIPR_MPDIP, |
| 835 | .eesipr_value = DMAC_M_RFRMER | DMAC_M_ECI | 0x003fffff, |
| 836 | |
| 837 | .tx_check = EESR_TC1 | EESR_FTC, |
Sergei Shtylyov | 128296f | 2014-01-03 15:52:22 +0300 | [diff] [blame] | 838 | .eesr_err_check = EESR_TWB1 | EESR_TWB | EESR_TABT | EESR_RABT | |
| 839 | EESR_RDE | EESR_RFRMER | EESR_TFE | EESR_TDE | |
Sergei Shtylyov | f5d1276 | 2013-06-07 13:58:18 +0000 | [diff] [blame] | 840 | EESR_ECI, |
Sergei Shtylyov | f5d1276 | 2013-06-07 13:58:18 +0000 | [diff] [blame] | 841 | |
| 842 | .apr = 1, |
| 843 | .mpr = 1, |
| 844 | .tpauser = 1, |
| 845 | .bculr = 1, |
| 846 | .hw_swap = 1, |
| 847 | .no_trimd = 1, |
| 848 | .no_ade = 1, |
| 849 | .tsu = 1, |
| 850 | .irq_flags = IRQF_SHARED, |
| 851 | }; |
Yoshihiro Shimoda | 380af9e | 2009-05-24 23:54:21 +0000 | [diff] [blame] | 852 | |
Sergei Shtylyov | c18a79a | 2013-06-07 13:56:05 +0000 | [diff] [blame] | 853 | static struct sh_eth_cpu_data sh7619_data = { |
Sergei Shtylyov | a3153d8 | 2013-08-18 03:11:28 +0400 | [diff] [blame] | 854 | .register_type = SH_ETH_REG_FAST_SH3_SH2, |
| 855 | |
Yoshihiro Shimoda | 380af9e | 2009-05-24 23:54:21 +0000 | [diff] [blame] | 856 | .eesipr_value = DMAC_M_RFRMER | DMAC_M_ECI | 0x003fffff, |
| 857 | |
| 858 | .apr = 1, |
| 859 | .mpr = 1, |
| 860 | .tpauser = 1, |
| 861 | .hw_swap = 1, |
| 862 | }; |
Sergei Shtylyov | 7bbe150 | 2013-06-07 13:55:08 +0000 | [diff] [blame] | 863 | |
| 864 | static struct sh_eth_cpu_data sh771x_data = { |
Sergei Shtylyov | a3153d8 | 2013-08-18 03:11:28 +0400 | [diff] [blame] | 865 | .register_type = SH_ETH_REG_FAST_SH3_SH2, |
| 866 | |
Yoshihiro Shimoda | 380af9e | 2009-05-24 23:54:21 +0000 | [diff] [blame] | 867 | .eesipr_value = DMAC_M_RFRMER | DMAC_M_ECI | 0x003fffff, |
Yoshihiro Shimoda | 4986b99 | 2011-03-07 21:59:34 +0000 | [diff] [blame] | 868 | .tsu = 1, |
Yoshihiro Shimoda | 380af9e | 2009-05-24 23:54:21 +0000 | [diff] [blame] | 869 | }; |
Yoshihiro Shimoda | 380af9e | 2009-05-24 23:54:21 +0000 | [diff] [blame] | 870 | |
| 871 | static void sh_eth_set_default_cpu_data(struct sh_eth_cpu_data *cd) |
| 872 | { |
| 873 | if (!cd->ecsr_value) |
| 874 | cd->ecsr_value = DEFAULT_ECSR_INIT; |
| 875 | |
| 876 | if (!cd->ecsipr_value) |
| 877 | cd->ecsipr_value = DEFAULT_ECSIPR_INIT; |
| 878 | |
| 879 | if (!cd->fcftr_value) |
Sergei Shtylyov | 128296f | 2014-01-03 15:52:22 +0300 | [diff] [blame] | 880 | cd->fcftr_value = DEFAULT_FIFO_F_D_RFF | |
Yoshihiro Shimoda | 380af9e | 2009-05-24 23:54:21 +0000 | [diff] [blame] | 881 | DEFAULT_FIFO_F_D_RFD; |
| 882 | |
| 883 | if (!cd->fdr_value) |
| 884 | cd->fdr_value = DEFAULT_FDR_INIT; |
| 885 | |
Yoshihiro Shimoda | 380af9e | 2009-05-24 23:54:21 +0000 | [diff] [blame] | 886 | if (!cd->tx_check) |
| 887 | cd->tx_check = DEFAULT_TX_CHECK; |
| 888 | |
| 889 | if (!cd->eesr_err_check) |
| 890 | cd->eesr_err_check = DEFAULT_EESR_ERR_CHECK; |
Nobuhiro Iwamatsu | b284fbe | 2015-01-08 15:25:07 +0900 | [diff] [blame] | 891 | |
| 892 | if (!cd->trscer_err_mask) |
| 893 | cd->trscer_err_mask = DEFAULT_TRSCER_ERR_MASK; |
Yoshihiro Shimoda | 380af9e | 2009-05-24 23:54:21 +0000 | [diff] [blame] | 894 | } |
| 895 | |
Nobuhiro Iwamatsu | 5cee1d3 | 2012-06-25 17:35:12 +0000 | [diff] [blame] | 896 | static int sh_eth_check_reset(struct net_device *ndev) |
| 897 | { |
| 898 | int ret = 0; |
| 899 | int cnt = 100; |
| 900 | |
| 901 | while (cnt > 0) { |
Sergei Shtylyov | 97717ed | 2016-04-24 23:45:23 +0300 | [diff] [blame^] | 902 | if (!(sh_eth_read(ndev, EDMR) & EDMR_SRST_GETHER)) |
Nobuhiro Iwamatsu | 5cee1d3 | 2012-06-25 17:35:12 +0000 | [diff] [blame] | 903 | break; |
| 904 | mdelay(1); |
| 905 | cnt--; |
| 906 | } |
Sergei Shtylyov | 9f8c426 | 2013-06-05 23:54:01 +0400 | [diff] [blame] | 907 | if (cnt <= 0) { |
Sergei Shtylyov | f75f14e | 2014-03-15 03:27:54 +0300 | [diff] [blame] | 908 | netdev_err(ndev, "Device reset failed\n"); |
Nobuhiro Iwamatsu | 5cee1d3 | 2012-06-25 17:35:12 +0000 | [diff] [blame] | 909 | ret = -ETIMEDOUT; |
| 910 | } |
| 911 | return ret; |
Yoshihiro Shimoda | 380af9e | 2009-05-24 23:54:21 +0000 | [diff] [blame] | 912 | } |
Nobuhiro Iwamatsu | dabdde9 | 2013-06-06 09:51:39 +0000 | [diff] [blame] | 913 | |
| 914 | static int sh_eth_reset(struct net_device *ndev) |
| 915 | { |
| 916 | struct sh_eth_private *mdp = netdev_priv(ndev); |
| 917 | int ret = 0; |
| 918 | |
Simon Horman | db89347 | 2014-01-17 09:22:28 +0900 | [diff] [blame] | 919 | if (sh_eth_is_gether(mdp) || sh_eth_is_rz_fast_ether(mdp)) { |
Nobuhiro Iwamatsu | dabdde9 | 2013-06-06 09:51:39 +0000 | [diff] [blame] | 920 | sh_eth_write(ndev, EDSR_ENALL, EDSR); |
Sergei Shtylyov | b2b14d2 | 2016-02-10 01:38:28 +0300 | [diff] [blame] | 921 | sh_eth_modify(ndev, EDMR, EDMR_SRST_GETHER, EDMR_SRST_GETHER); |
Nobuhiro Iwamatsu | dabdde9 | 2013-06-06 09:51:39 +0000 | [diff] [blame] | 922 | |
| 923 | ret = sh_eth_check_reset(ndev); |
| 924 | if (ret) |
Laurent Pinchart | f738a13 | 2014-03-20 15:00:35 +0100 | [diff] [blame] | 925 | return ret; |
Nobuhiro Iwamatsu | dabdde9 | 2013-06-06 09:51:39 +0000 | [diff] [blame] | 926 | |
| 927 | /* Table Init */ |
| 928 | sh_eth_write(ndev, 0x0, TDLAR); |
| 929 | sh_eth_write(ndev, 0x0, TDFAR); |
| 930 | sh_eth_write(ndev, 0x0, TDFXR); |
| 931 | sh_eth_write(ndev, 0x0, TDFFR); |
| 932 | sh_eth_write(ndev, 0x0, RDLAR); |
| 933 | sh_eth_write(ndev, 0x0, RDFAR); |
| 934 | sh_eth_write(ndev, 0x0, RDFXR); |
| 935 | sh_eth_write(ndev, 0x0, RDFFR); |
| 936 | |
| 937 | /* Reset HW CRC register */ |
| 938 | if (mdp->cd->hw_crc) |
| 939 | sh_eth_write(ndev, 0x0, CSMR); |
| 940 | |
| 941 | /* Select MII mode */ |
| 942 | if (mdp->cd->select_mii) |
| 943 | sh_eth_select_mii(ndev); |
| 944 | } else { |
Sergei Shtylyov | b2b14d2 | 2016-02-10 01:38:28 +0300 | [diff] [blame] | 945 | sh_eth_modify(ndev, EDMR, EDMR_SRST_ETHER, EDMR_SRST_ETHER); |
Nobuhiro Iwamatsu | dabdde9 | 2013-06-06 09:51:39 +0000 | [diff] [blame] | 946 | mdelay(3); |
Sergei Shtylyov | b2b14d2 | 2016-02-10 01:38:28 +0300 | [diff] [blame] | 947 | sh_eth_modify(ndev, EDMR, EDMR_SRST_ETHER, 0); |
Nobuhiro Iwamatsu | dabdde9 | 2013-06-06 09:51:39 +0000 | [diff] [blame] | 948 | } |
| 949 | |
Nobuhiro Iwamatsu | dabdde9 | 2013-06-06 09:51:39 +0000 | [diff] [blame] | 950 | return ret; |
| 951 | } |
Yoshihiro Shimoda | 380af9e | 2009-05-24 23:54:21 +0000 | [diff] [blame] | 952 | |
Yoshihiro Shimoda | 380af9e | 2009-05-24 23:54:21 +0000 | [diff] [blame] | 953 | static void sh_eth_set_receive_align(struct sk_buff *skb) |
| 954 | { |
Mitsuhiro Kimura | 4d6a949 | 2014-11-27 20:34:00 +0900 | [diff] [blame] | 955 | uintptr_t reserve = (uintptr_t)skb->data & (SH_ETH_RX_ALIGN - 1); |
Yoshihiro Shimoda | 380af9e | 2009-05-24 23:54:21 +0000 | [diff] [blame] | 956 | |
Yoshihiro Shimoda | 380af9e | 2009-05-24 23:54:21 +0000 | [diff] [blame] | 957 | if (reserve) |
Mitsuhiro Kimura | 4d6a949 | 2014-11-27 20:34:00 +0900 | [diff] [blame] | 958 | skb_reserve(skb, SH_ETH_RX_ALIGN - reserve); |
Yoshihiro Shimoda | 380af9e | 2009-05-24 23:54:21 +0000 | [diff] [blame] | 959 | } |
Yoshihiro Shimoda | 380af9e | 2009-05-24 23:54:21 +0000 | [diff] [blame] | 960 | |
Sergei Shtylyov | 128296f | 2014-01-03 15:52:22 +0300 | [diff] [blame] | 961 | /* Program the hardware MAC address from dev->dev_addr. */ |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 962 | static void update_mac_address(struct net_device *ndev) |
| 963 | { |
Yoshihiro Shimoda | 4a55530 | 2011-03-07 21:59:26 +0000 | [diff] [blame] | 964 | sh_eth_write(ndev, |
Sergei Shtylyov | 128296f | 2014-01-03 15:52:22 +0300 | [diff] [blame] | 965 | (ndev->dev_addr[0] << 24) | (ndev->dev_addr[1] << 16) | |
| 966 | (ndev->dev_addr[2] << 8) | (ndev->dev_addr[3]), MAHR); |
Yoshihiro Shimoda | 4a55530 | 2011-03-07 21:59:26 +0000 | [diff] [blame] | 967 | sh_eth_write(ndev, |
Sergei Shtylyov | 128296f | 2014-01-03 15:52:22 +0300 | [diff] [blame] | 968 | (ndev->dev_addr[4] << 8) | (ndev->dev_addr[5]), MALR); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 969 | } |
| 970 | |
Sergei Shtylyov | 128296f | 2014-01-03 15:52:22 +0300 | [diff] [blame] | 971 | /* Get MAC address from SuperH MAC address register |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 972 | * |
| 973 | * SuperH's Ethernet device doesn't have 'ROM' to MAC address. |
| 974 | * This driver get MAC address that use by bootloader(U-boot or sh-ipl+g). |
| 975 | * When you want use this device, you must set MAC address in bootloader. |
| 976 | * |
| 977 | */ |
Magnus Damm | 748031f | 2009-10-09 00:17:14 +0000 | [diff] [blame] | 978 | static void read_mac_address(struct net_device *ndev, unsigned char *mac) |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 979 | { |
Magnus Damm | 748031f | 2009-10-09 00:17:14 +0000 | [diff] [blame] | 980 | if (mac[0] || mac[1] || mac[2] || mac[3] || mac[4] || mac[5]) { |
Joe Perches | d458cdf | 2013-10-01 19:04:40 -0700 | [diff] [blame] | 981 | memcpy(ndev->dev_addr, mac, ETH_ALEN); |
Magnus Damm | 748031f | 2009-10-09 00:17:14 +0000 | [diff] [blame] | 982 | } else { |
Sergei Shtylyov | 37742f0 | 2015-12-05 00:58:57 +0300 | [diff] [blame] | 983 | u32 mahr = sh_eth_read(ndev, MAHR); |
| 984 | u32 malr = sh_eth_read(ndev, MALR); |
| 985 | |
| 986 | ndev->dev_addr[0] = (mahr >> 24) & 0xFF; |
| 987 | ndev->dev_addr[1] = (mahr >> 16) & 0xFF; |
| 988 | ndev->dev_addr[2] = (mahr >> 8) & 0xFF; |
| 989 | ndev->dev_addr[3] = (mahr >> 0) & 0xFF; |
| 990 | ndev->dev_addr[4] = (malr >> 8) & 0xFF; |
| 991 | ndev->dev_addr[5] = (malr >> 0) & 0xFF; |
Magnus Damm | 748031f | 2009-10-09 00:17:14 +0000 | [diff] [blame] | 992 | } |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 993 | } |
| 994 | |
Geert Uytterhoeven | 0799c2d | 2015-01-15 11:54:28 +0100 | [diff] [blame] | 995 | static u32 sh_eth_get_edtrr_trns(struct sh_eth_private *mdp) |
Yoshihiro Shimoda | c5ed536 | 2011-03-07 21:59:38 +0000 | [diff] [blame] | 996 | { |
Simon Horman | db89347 | 2014-01-17 09:22:28 +0900 | [diff] [blame] | 997 | if (sh_eth_is_gether(mdp) || sh_eth_is_rz_fast_ether(mdp)) |
Yoshihiro Shimoda | c5ed536 | 2011-03-07 21:59:38 +0000 | [diff] [blame] | 998 | return EDTRR_TRNS_GETHER; |
| 999 | else |
| 1000 | return EDTRR_TRNS_ETHER; |
| 1001 | } |
| 1002 | |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1003 | struct bb_info { |
Yoshihiro Shimoda | ae70644 | 2011-09-27 21:48:58 +0000 | [diff] [blame] | 1004 | void (*set_gate)(void *addr); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1005 | struct mdiobb_ctrl ctrl; |
Yoshihiro Shimoda | ae70644 | 2011-09-27 21:48:58 +0000 | [diff] [blame] | 1006 | void *addr; |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1007 | }; |
| 1008 | |
Sergei Shtylyov | 39b4b06 | 2015-12-08 00:40:57 +0300 | [diff] [blame] | 1009 | static void sh_mdio_ctrl(struct mdiobb_ctrl *ctrl, u32 mask, int set) |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1010 | { |
| 1011 | struct bb_info *bitbang = container_of(ctrl, struct bb_info, ctrl); |
Sergei Shtylyov | 78fa3c5 | 2015-12-08 00:41:43 +0300 | [diff] [blame] | 1012 | u32 pir; |
Yoshihiro Shimoda | b3017e6 | 2011-03-07 21:59:55 +0000 | [diff] [blame] | 1013 | |
| 1014 | if (bitbang->set_gate) |
| 1015 | bitbang->set_gate(bitbang->addr); |
| 1016 | |
Sergei Shtylyov | 78fa3c5 | 2015-12-08 00:41:43 +0300 | [diff] [blame] | 1017 | pir = ioread32(bitbang->addr); |
Sergei Shtylyov | 39b4b06 | 2015-12-08 00:40:57 +0300 | [diff] [blame] | 1018 | if (set) |
Sergei Shtylyov | 78fa3c5 | 2015-12-08 00:41:43 +0300 | [diff] [blame] | 1019 | pir |= mask; |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1020 | else |
Sergei Shtylyov | 78fa3c5 | 2015-12-08 00:41:43 +0300 | [diff] [blame] | 1021 | pir &= ~mask; |
| 1022 | iowrite32(pir, bitbang->addr); |
Sergei Shtylyov | 39b4b06 | 2015-12-08 00:40:57 +0300 | [diff] [blame] | 1023 | } |
| 1024 | |
| 1025 | /* Data I/O pin control */ |
| 1026 | static void sh_mmd_ctrl(struct mdiobb_ctrl *ctrl, int bit) |
| 1027 | { |
| 1028 | sh_mdio_ctrl(ctrl, PIR_MMD, bit); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1029 | } |
| 1030 | |
| 1031 | /* Set bit data*/ |
| 1032 | static void sh_set_mdio(struct mdiobb_ctrl *ctrl, int bit) |
| 1033 | { |
Sergei Shtylyov | 39b4b06 | 2015-12-08 00:40:57 +0300 | [diff] [blame] | 1034 | sh_mdio_ctrl(ctrl, PIR_MDO, bit); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1035 | } |
| 1036 | |
| 1037 | /* Get bit data*/ |
| 1038 | static int sh_get_mdio(struct mdiobb_ctrl *ctrl) |
| 1039 | { |
| 1040 | struct bb_info *bitbang = container_of(ctrl, struct bb_info, ctrl); |
Yoshihiro Shimoda | b3017e6 | 2011-03-07 21:59:55 +0000 | [diff] [blame] | 1041 | |
| 1042 | if (bitbang->set_gate) |
| 1043 | bitbang->set_gate(bitbang->addr); |
| 1044 | |
Sergei Shtylyov | 78fa3c5 | 2015-12-08 00:41:43 +0300 | [diff] [blame] | 1045 | return (ioread32(bitbang->addr) & PIR_MDI) != 0; |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1046 | } |
| 1047 | |
| 1048 | /* MDC pin control */ |
| 1049 | static void sh_mdc_ctrl(struct mdiobb_ctrl *ctrl, int bit) |
| 1050 | { |
Sergei Shtylyov | 39b4b06 | 2015-12-08 00:40:57 +0300 | [diff] [blame] | 1051 | sh_mdio_ctrl(ctrl, PIR_MDC, bit); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1052 | } |
| 1053 | |
| 1054 | /* mdio bus control struct */ |
| 1055 | static struct mdiobb_ops bb_ops = { |
| 1056 | .owner = THIS_MODULE, |
| 1057 | .set_mdc = sh_mdc_ctrl, |
| 1058 | .set_mdio_dir = sh_mmd_ctrl, |
| 1059 | .set_mdio_data = sh_set_mdio, |
| 1060 | .get_mdio_data = sh_get_mdio, |
| 1061 | }; |
| 1062 | |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1063 | /* free skb and descriptor buffer */ |
| 1064 | static void sh_eth_ring_free(struct net_device *ndev) |
| 1065 | { |
| 1066 | struct sh_eth_private *mdp = netdev_priv(ndev); |
Sergei Shtylyov | 8e03a5e | 2015-11-04 00:55:13 +0300 | [diff] [blame] | 1067 | int ringsize, i; |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1068 | |
| 1069 | /* Free Rx skb ringbuffer */ |
| 1070 | if (mdp->rx_skbuff) { |
Sergei Shtylyov | 179d80a | 2014-06-28 04:10:00 +0400 | [diff] [blame] | 1071 | for (i = 0; i < mdp->num_rx_ring; i++) |
| 1072 | dev_kfree_skb(mdp->rx_skbuff[i]); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1073 | } |
| 1074 | kfree(mdp->rx_skbuff); |
Yoshihiro Shimoda | 91c7755 | 2012-06-26 20:00:01 +0000 | [diff] [blame] | 1075 | mdp->rx_skbuff = NULL; |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1076 | |
| 1077 | /* Free Tx skb ringbuffer */ |
| 1078 | if (mdp->tx_skbuff) { |
Sergei Shtylyov | 179d80a | 2014-06-28 04:10:00 +0400 | [diff] [blame] | 1079 | for (i = 0; i < mdp->num_tx_ring; i++) |
| 1080 | dev_kfree_skb(mdp->tx_skbuff[i]); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1081 | } |
| 1082 | kfree(mdp->tx_skbuff); |
Yoshihiro Shimoda | 91c7755 | 2012-06-26 20:00:01 +0000 | [diff] [blame] | 1083 | mdp->tx_skbuff = NULL; |
Sergei Shtylyov | 8e03a5e | 2015-11-04 00:55:13 +0300 | [diff] [blame] | 1084 | |
| 1085 | if (mdp->rx_ring) { |
| 1086 | ringsize = sizeof(struct sh_eth_rxdesc) * mdp->num_rx_ring; |
| 1087 | dma_free_coherent(NULL, ringsize, mdp->rx_ring, |
| 1088 | mdp->rx_desc_dma); |
| 1089 | mdp->rx_ring = NULL; |
| 1090 | } |
| 1091 | |
| 1092 | if (mdp->tx_ring) { |
| 1093 | ringsize = sizeof(struct sh_eth_txdesc) * mdp->num_tx_ring; |
| 1094 | dma_free_coherent(NULL, ringsize, mdp->tx_ring, |
| 1095 | mdp->tx_desc_dma); |
| 1096 | mdp->tx_ring = NULL; |
| 1097 | } |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1098 | } |
| 1099 | |
| 1100 | /* format skb and descriptor buffer */ |
| 1101 | static void sh_eth_ring_format(struct net_device *ndev) |
| 1102 | { |
| 1103 | struct sh_eth_private *mdp = netdev_priv(ndev); |
| 1104 | int i; |
| 1105 | struct sk_buff *skb; |
| 1106 | struct sh_eth_rxdesc *rxdesc = NULL; |
| 1107 | struct sh_eth_txdesc *txdesc = NULL; |
Yoshihiro Shimoda | 525b807 | 2012-06-26 20:00:03 +0000 | [diff] [blame] | 1108 | int rx_ringsize = sizeof(*rxdesc) * mdp->num_rx_ring; |
| 1109 | int tx_ringsize = sizeof(*txdesc) * mdp->num_tx_ring; |
Sergei Shtylyov | cb36859 | 2015-10-24 00:46:40 +0300 | [diff] [blame] | 1110 | int skbuff_size = mdp->rx_buf_sz + SH_ETH_RX_ALIGN + 32 - 1; |
Ben Hutchings | 52b9fa3 | 2015-01-27 00:50:24 +0000 | [diff] [blame] | 1111 | dma_addr_t dma_addr; |
Sergei Shtylyov | 5cbf20c | 2015-12-20 01:48:04 +0300 | [diff] [blame] | 1112 | u32 buf_len; |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1113 | |
Sergei Shtylyov | 128296f | 2014-01-03 15:52:22 +0300 | [diff] [blame] | 1114 | mdp->cur_rx = 0; |
| 1115 | mdp->cur_tx = 0; |
| 1116 | mdp->dirty_rx = 0; |
| 1117 | mdp->dirty_tx = 0; |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1118 | |
| 1119 | memset(mdp->rx_ring, 0, rx_ringsize); |
| 1120 | |
| 1121 | /* build Rx ring buffer */ |
Yoshihiro Shimoda | 525b807 | 2012-06-26 20:00:03 +0000 | [diff] [blame] | 1122 | for (i = 0; i < mdp->num_rx_ring; i++) { |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1123 | /* skb */ |
| 1124 | mdp->rx_skbuff[i] = NULL; |
Mitsuhiro Kimura | 4d6a949 | 2014-11-27 20:34:00 +0900 | [diff] [blame] | 1125 | skb = netdev_alloc_skb(ndev, skbuff_size); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1126 | if (skb == NULL) |
| 1127 | break; |
Yoshihiro Shimoda | 380af9e | 2009-05-24 23:54:21 +0000 | [diff] [blame] | 1128 | sh_eth_set_receive_align(skb); |
| 1129 | |
Sergei Shtylyov | ab85791 | 2015-10-24 00:46:03 +0300 | [diff] [blame] | 1130 | /* The size of the buffer is a multiple of 32 bytes. */ |
Sergei Shtylyov | 5cbf20c | 2015-12-20 01:48:04 +0300 | [diff] [blame] | 1131 | buf_len = ALIGN(mdp->rx_buf_sz, 32); |
Sergei Shtylyov | 5cbf20c | 2015-12-20 01:48:04 +0300 | [diff] [blame] | 1132 | dma_addr = dma_map_single(&ndev->dev, skb->data, buf_len, |
Ben Hutchings | 52b9fa3 | 2015-01-27 00:50:24 +0000 | [diff] [blame] | 1133 | DMA_FROM_DEVICE); |
| 1134 | if (dma_mapping_error(&ndev->dev, dma_addr)) { |
| 1135 | kfree_skb(skb); |
| 1136 | break; |
| 1137 | } |
| 1138 | mdp->rx_skbuff[i] = skb; |
Sergei Shtylyov | d0ba913 | 2016-03-08 01:37:09 +0300 | [diff] [blame] | 1139 | |
| 1140 | /* RX descriptor */ |
| 1141 | rxdesc = &mdp->rx_ring[i]; |
| 1142 | rxdesc->len = cpu_to_le32(buf_len << 16); |
Sergei Shtylyov | 7cf7247 | 2015-12-28 02:10:47 +0300 | [diff] [blame] | 1143 | rxdesc->addr = cpu_to_le32(dma_addr); |
| 1144 | rxdesc->status = cpu_to_le32(RD_RACT | RD_RFP); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1145 | |
Nobuhiro Iwamatsu | b0ca2a2 | 2008-06-30 11:08:17 +0900 | [diff] [blame] | 1146 | /* Rx descriptor address set */ |
| 1147 | if (i == 0) { |
Yoshihiro Shimoda | 4a55530 | 2011-03-07 21:59:26 +0000 | [diff] [blame] | 1148 | sh_eth_write(ndev, mdp->rx_desc_dma, RDLAR); |
Simon Horman | db89347 | 2014-01-17 09:22:28 +0900 | [diff] [blame] | 1149 | if (sh_eth_is_gether(mdp) || |
| 1150 | sh_eth_is_rz_fast_ether(mdp)) |
Yoshihiro Shimoda | c5ed536 | 2011-03-07 21:59:38 +0000 | [diff] [blame] | 1151 | sh_eth_write(ndev, mdp->rx_desc_dma, RDFAR); |
Nobuhiro Iwamatsu | b0ca2a2 | 2008-06-30 11:08:17 +0900 | [diff] [blame] | 1152 | } |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1153 | } |
| 1154 | |
Yoshihiro Shimoda | 525b807 | 2012-06-26 20:00:03 +0000 | [diff] [blame] | 1155 | mdp->dirty_rx = (u32) (i - mdp->num_rx_ring); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1156 | |
| 1157 | /* Mark the last entry as wrapping the ring. */ |
Sergei Shtylyov | c1b7fca | 2016-03-08 01:36:28 +0300 | [diff] [blame] | 1158 | if (rxdesc) |
| 1159 | rxdesc->status |= cpu_to_le32(RD_RDLE); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1160 | |
| 1161 | memset(mdp->tx_ring, 0, tx_ringsize); |
| 1162 | |
| 1163 | /* build Tx ring buffer */ |
Yoshihiro Shimoda | 525b807 | 2012-06-26 20:00:03 +0000 | [diff] [blame] | 1164 | for (i = 0; i < mdp->num_tx_ring; i++) { |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1165 | mdp->tx_skbuff[i] = NULL; |
| 1166 | txdesc = &mdp->tx_ring[i]; |
Sergei Shtylyov | 7cf7247 | 2015-12-28 02:10:47 +0300 | [diff] [blame] | 1167 | txdesc->status = cpu_to_le32(TD_TFP); |
| 1168 | txdesc->len = cpu_to_le32(0); |
Nobuhiro Iwamatsu | b0ca2a2 | 2008-06-30 11:08:17 +0900 | [diff] [blame] | 1169 | if (i == 0) { |
Yoshinori Sato | 71557a3 | 2008-08-06 19:49:00 -0400 | [diff] [blame] | 1170 | /* Tx descriptor address set */ |
Yoshihiro Shimoda | 4a55530 | 2011-03-07 21:59:26 +0000 | [diff] [blame] | 1171 | sh_eth_write(ndev, mdp->tx_desc_dma, TDLAR); |
Simon Horman | db89347 | 2014-01-17 09:22:28 +0900 | [diff] [blame] | 1172 | if (sh_eth_is_gether(mdp) || |
| 1173 | sh_eth_is_rz_fast_ether(mdp)) |
Yoshihiro Shimoda | c5ed536 | 2011-03-07 21:59:38 +0000 | [diff] [blame] | 1174 | sh_eth_write(ndev, mdp->tx_desc_dma, TDFAR); |
Nobuhiro Iwamatsu | b0ca2a2 | 2008-06-30 11:08:17 +0900 | [diff] [blame] | 1175 | } |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1176 | } |
| 1177 | |
Sergei Shtylyov | 7cf7247 | 2015-12-28 02:10:47 +0300 | [diff] [blame] | 1178 | txdesc->status |= cpu_to_le32(TD_TDLE); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1179 | } |
| 1180 | |
| 1181 | /* Get skb and descriptor buffer */ |
| 1182 | static int sh_eth_ring_init(struct net_device *ndev) |
| 1183 | { |
| 1184 | struct sh_eth_private *mdp = netdev_priv(ndev); |
Sergei Shtylyov | 91d8068 | 2015-11-04 00:17:08 +0300 | [diff] [blame] | 1185 | int rx_ringsize, tx_ringsize; |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1186 | |
Sergei Shtylyov | 128296f | 2014-01-03 15:52:22 +0300 | [diff] [blame] | 1187 | /* +26 gets the maximum ethernet encapsulation, +7 & ~7 because the |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1188 | * card needs room to do 8 byte alignment, +2 so we can reserve |
| 1189 | * the first 2 bytes, and +16 gets room for the status word from the |
| 1190 | * card. |
| 1191 | */ |
| 1192 | mdp->rx_buf_sz = (ndev->mtu <= 1492 ? PKT_BUF_SZ : |
| 1193 | (((ndev->mtu + 26 + 7) & ~7) + 2 + 16)); |
Magnus Damm | 503914c | 2009-12-15 21:16:55 -0800 | [diff] [blame] | 1194 | if (mdp->cd->rpadir) |
| 1195 | mdp->rx_buf_sz += NET_IP_ALIGN; |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1196 | |
| 1197 | /* Allocate RX and TX skb rings */ |
Sergei Shtylyov | 2c94e85 | 2015-10-31 02:05:56 +0300 | [diff] [blame] | 1198 | mdp->rx_skbuff = kcalloc(mdp->num_rx_ring, sizeof(*mdp->rx_skbuff), |
| 1199 | GFP_KERNEL); |
Sergei Shtylyov | 91d8068 | 2015-11-04 00:17:08 +0300 | [diff] [blame] | 1200 | if (!mdp->rx_skbuff) |
| 1201 | return -ENOMEM; |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1202 | |
Sergei Shtylyov | 2c94e85 | 2015-10-31 02:05:56 +0300 | [diff] [blame] | 1203 | mdp->tx_skbuff = kcalloc(mdp->num_tx_ring, sizeof(*mdp->tx_skbuff), |
| 1204 | GFP_KERNEL); |
Sergei Shtylyov | 91d8068 | 2015-11-04 00:17:08 +0300 | [diff] [blame] | 1205 | if (!mdp->tx_skbuff) |
Sergei Shtylyov | 8e03a5e | 2015-11-04 00:55:13 +0300 | [diff] [blame] | 1206 | goto ring_free; |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1207 | |
| 1208 | /* Allocate all Rx descriptors. */ |
Yoshihiro Shimoda | 525b807 | 2012-06-26 20:00:03 +0000 | [diff] [blame] | 1209 | rx_ringsize = sizeof(struct sh_eth_rxdesc) * mdp->num_rx_ring; |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1210 | mdp->rx_ring = dma_alloc_coherent(NULL, rx_ringsize, &mdp->rx_desc_dma, |
Joe Perches | d0320f7 | 2013-03-14 13:07:21 +0000 | [diff] [blame] | 1211 | GFP_KERNEL); |
Sergei Shtylyov | 91d8068 | 2015-11-04 00:17:08 +0300 | [diff] [blame] | 1212 | if (!mdp->rx_ring) |
Sergei Shtylyov | 8e03a5e | 2015-11-04 00:55:13 +0300 | [diff] [blame] | 1213 | goto ring_free; |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1214 | |
| 1215 | mdp->dirty_rx = 0; |
| 1216 | |
| 1217 | /* Allocate all Tx descriptors. */ |
Yoshihiro Shimoda | 525b807 | 2012-06-26 20:00:03 +0000 | [diff] [blame] | 1218 | tx_ringsize = sizeof(struct sh_eth_txdesc) * mdp->num_tx_ring; |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1219 | mdp->tx_ring = dma_alloc_coherent(NULL, tx_ringsize, &mdp->tx_desc_dma, |
Joe Perches | d0320f7 | 2013-03-14 13:07:21 +0000 | [diff] [blame] | 1220 | GFP_KERNEL); |
Sergei Shtylyov | 91d8068 | 2015-11-04 00:17:08 +0300 | [diff] [blame] | 1221 | if (!mdp->tx_ring) |
Sergei Shtylyov | 8e03a5e | 2015-11-04 00:55:13 +0300 | [diff] [blame] | 1222 | goto ring_free; |
Sergei Shtylyov | 91d8068 | 2015-11-04 00:17:08 +0300 | [diff] [blame] | 1223 | return 0; |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1224 | |
Sergei Shtylyov | 8e03a5e | 2015-11-04 00:55:13 +0300 | [diff] [blame] | 1225 | ring_free: |
| 1226 | /* Free Rx and Tx skb ring buffer and DMA buffer */ |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1227 | sh_eth_ring_free(ndev); |
| 1228 | |
Sergei Shtylyov | 91d8068 | 2015-11-04 00:17:08 +0300 | [diff] [blame] | 1229 | return -ENOMEM; |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1230 | } |
| 1231 | |
Sergei Shtylyov | f796721 | 2016-04-24 19:11:07 +0300 | [diff] [blame] | 1232 | static int sh_eth_dev_init(struct net_device *ndev) |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1233 | { |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1234 | struct sh_eth_private *mdp = netdev_priv(ndev); |
Sergei Shtylyov | 4fa8c3c | 2016-03-13 01:29:45 +0300 | [diff] [blame] | 1235 | int ret; |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1236 | |
| 1237 | /* Soft Reset */ |
Nobuhiro Iwamatsu | 5cee1d3 | 2012-06-25 17:35:12 +0000 | [diff] [blame] | 1238 | ret = sh_eth_reset(ndev); |
| 1239 | if (ret) |
Laurent Pinchart | f738a13 | 2014-03-20 15:00:35 +0100 | [diff] [blame] | 1240 | return ret; |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1241 | |
Simon Horman | 55754f1 | 2013-07-23 10:18:04 +0900 | [diff] [blame] | 1242 | if (mdp->cd->rmiimode) |
| 1243 | sh_eth_write(ndev, 0x1, RMIIMODE); |
| 1244 | |
Nobuhiro Iwamatsu | b0ca2a2 | 2008-06-30 11:08:17 +0900 | [diff] [blame] | 1245 | /* Descriptor format */ |
| 1246 | sh_eth_ring_format(ndev); |
Yoshihiro Shimoda | 380af9e | 2009-05-24 23:54:21 +0000 | [diff] [blame] | 1247 | if (mdp->cd->rpadir) |
Yoshihiro Shimoda | 4a55530 | 2011-03-07 21:59:26 +0000 | [diff] [blame] | 1248 | sh_eth_write(ndev, mdp->cd->rpadir_value, RPADIR); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1249 | |
| 1250 | /* all sh_eth int mask */ |
Yoshihiro Shimoda | 4a55530 | 2011-03-07 21:59:26 +0000 | [diff] [blame] | 1251 | sh_eth_write(ndev, 0, EESIPR); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1252 | |
Yoshihiro Shimoda | 10b9194 | 2012-03-29 19:32:08 +0000 | [diff] [blame] | 1253 | #if defined(__LITTLE_ENDIAN) |
Yoshihiro Shimoda | 380af9e | 2009-05-24 23:54:21 +0000 | [diff] [blame] | 1254 | if (mdp->cd->hw_swap) |
Yoshihiro Shimoda | 4a55530 | 2011-03-07 21:59:26 +0000 | [diff] [blame] | 1255 | sh_eth_write(ndev, EDMR_EL, EDMR); |
Yoshihiro Shimoda | 380af9e | 2009-05-24 23:54:21 +0000 | [diff] [blame] | 1256 | else |
Nobuhiro Iwamatsu | b0ca2a2 | 2008-06-30 11:08:17 +0900 | [diff] [blame] | 1257 | #endif |
Yoshihiro Shimoda | 4a55530 | 2011-03-07 21:59:26 +0000 | [diff] [blame] | 1258 | sh_eth_write(ndev, 0, EDMR); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1259 | |
Nobuhiro Iwamatsu | b0ca2a2 | 2008-06-30 11:08:17 +0900 | [diff] [blame] | 1260 | /* FIFO size set */ |
Yoshihiro Shimoda | 4a55530 | 2011-03-07 21:59:26 +0000 | [diff] [blame] | 1261 | sh_eth_write(ndev, mdp->cd->fdr_value, FDR); |
| 1262 | sh_eth_write(ndev, 0, TFTR); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1263 | |
Ben Dooks | 530aa2d | 2014-06-03 12:21:13 +0100 | [diff] [blame] | 1264 | /* Frame recv control (enable multiple-packets per rx irq) */ |
| 1265 | sh_eth_write(ndev, RMCR_RNC, RMCR); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1266 | |
Nobuhiro Iwamatsu | b284fbe | 2015-01-08 15:25:07 +0900 | [diff] [blame] | 1267 | sh_eth_write(ndev, mdp->cd->trscer_err_mask, TRSCER); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1268 | |
Yoshihiro Shimoda | 380af9e | 2009-05-24 23:54:21 +0000 | [diff] [blame] | 1269 | if (mdp->cd->bculr) |
Yoshihiro Shimoda | 4a55530 | 2011-03-07 21:59:26 +0000 | [diff] [blame] | 1270 | sh_eth_write(ndev, 0x800, BCULR); /* Burst sycle set */ |
Nobuhiro Iwamatsu | b0ca2a2 | 2008-06-30 11:08:17 +0900 | [diff] [blame] | 1271 | |
Yoshihiro Shimoda | 4a55530 | 2011-03-07 21:59:26 +0000 | [diff] [blame] | 1272 | sh_eth_write(ndev, mdp->cd->fcftr_value, FCFTR); |
Nobuhiro Iwamatsu | b0ca2a2 | 2008-06-30 11:08:17 +0900 | [diff] [blame] | 1273 | |
Yoshihiro Shimoda | 380af9e | 2009-05-24 23:54:21 +0000 | [diff] [blame] | 1274 | if (!mdp->cd->no_trimd) |
Yoshihiro Shimoda | 4a55530 | 2011-03-07 21:59:26 +0000 | [diff] [blame] | 1275 | sh_eth_write(ndev, 0, TRIMD); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1276 | |
Nobuhiro Iwamatsu | b0ca2a2 | 2008-06-30 11:08:17 +0900 | [diff] [blame] | 1277 | /* Recv frame limit set register */ |
Yoshihiro Shimoda | fdb37a7 | 2012-02-06 23:55:15 +0000 | [diff] [blame] | 1278 | sh_eth_write(ndev, ndev->mtu + ETH_HLEN + VLAN_HLEN + ETH_FCS_LEN, |
| 1279 | RFLR); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1280 | |
Sergei Shtylyov | b2b14d2 | 2016-02-10 01:38:28 +0300 | [diff] [blame] | 1281 | sh_eth_modify(ndev, EESR, 0, 0); |
Sergei Shtylyov | f796721 | 2016-04-24 19:11:07 +0300 | [diff] [blame] | 1282 | mdp->irq_enabled = true; |
| 1283 | sh_eth_write(ndev, mdp->cd->eesipr_value, EESIPR); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1284 | |
| 1285 | /* PAUSE Prohibition */ |
Sergei Shtylyov | bffa731 | 2016-01-11 00:28:14 +0300 | [diff] [blame] | 1286 | sh_eth_write(ndev, ECMR_ZPF | (mdp->duplex ? ECMR_DM : 0) | |
| 1287 | ECMR_TE | ECMR_RE, ECMR); |
Nobuhiro Iwamatsu | b0ca2a2 | 2008-06-30 11:08:17 +0900 | [diff] [blame] | 1288 | |
Yoshihiro Shimoda | 380af9e | 2009-05-24 23:54:21 +0000 | [diff] [blame] | 1289 | if (mdp->cd->set_rate) |
| 1290 | mdp->cd->set_rate(ndev); |
| 1291 | |
Nobuhiro Iwamatsu | b0ca2a2 | 2008-06-30 11:08:17 +0900 | [diff] [blame] | 1292 | /* E-MAC Status Register clear */ |
Yoshihiro Shimoda | 4a55530 | 2011-03-07 21:59:26 +0000 | [diff] [blame] | 1293 | sh_eth_write(ndev, mdp->cd->ecsr_value, ECSR); |
Nobuhiro Iwamatsu | b0ca2a2 | 2008-06-30 11:08:17 +0900 | [diff] [blame] | 1294 | |
| 1295 | /* E-MAC Interrupt Enable register */ |
Sergei Shtylyov | f796721 | 2016-04-24 19:11:07 +0300 | [diff] [blame] | 1296 | sh_eth_write(ndev, mdp->cd->ecsipr_value, ECSIPR); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1297 | |
| 1298 | /* Set MAC address */ |
| 1299 | update_mac_address(ndev); |
| 1300 | |
| 1301 | /* mask reset */ |
Yoshihiro Shimoda | 380af9e | 2009-05-24 23:54:21 +0000 | [diff] [blame] | 1302 | if (mdp->cd->apr) |
Yoshihiro Shimoda | 4a55530 | 2011-03-07 21:59:26 +0000 | [diff] [blame] | 1303 | sh_eth_write(ndev, APR_AP, APR); |
Yoshihiro Shimoda | 380af9e | 2009-05-24 23:54:21 +0000 | [diff] [blame] | 1304 | if (mdp->cd->mpr) |
Yoshihiro Shimoda | 4a55530 | 2011-03-07 21:59:26 +0000 | [diff] [blame] | 1305 | sh_eth_write(ndev, MPR_MP, MPR); |
Yoshihiro Shimoda | 380af9e | 2009-05-24 23:54:21 +0000 | [diff] [blame] | 1306 | if (mdp->cd->tpauser) |
Yoshihiro Shimoda | 4a55530 | 2011-03-07 21:59:26 +0000 | [diff] [blame] | 1307 | sh_eth_write(ndev, TPAUSER_UNLIMITED, TPAUSER); |
Nobuhiro Iwamatsu | b0ca2a2 | 2008-06-30 11:08:17 +0900 | [diff] [blame] | 1308 | |
Sergei Shtylyov | f796721 | 2016-04-24 19:11:07 +0300 | [diff] [blame] | 1309 | /* Setting the Rx mode will start the Rx process. */ |
| 1310 | sh_eth_write(ndev, EDRRR_R, EDRRR); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1311 | |
| 1312 | return ret; |
| 1313 | } |
| 1314 | |
Ben Hutchings | 740c7f3 | 2015-01-27 00:49:32 +0000 | [diff] [blame] | 1315 | static void sh_eth_dev_exit(struct net_device *ndev) |
| 1316 | { |
| 1317 | struct sh_eth_private *mdp = netdev_priv(ndev); |
| 1318 | int i; |
| 1319 | |
| 1320 | /* Deactivate all TX descriptors, so DMA should stop at next |
| 1321 | * packet boundary if it's currently running |
| 1322 | */ |
| 1323 | for (i = 0; i < mdp->num_tx_ring; i++) |
Sergei Shtylyov | 7cf7247 | 2015-12-28 02:10:47 +0300 | [diff] [blame] | 1324 | mdp->tx_ring[i].status &= ~cpu_to_le32(TD_TACT); |
Ben Hutchings | 740c7f3 | 2015-01-27 00:49:32 +0000 | [diff] [blame] | 1325 | |
| 1326 | /* Disable TX FIFO egress to MAC */ |
| 1327 | sh_eth_rcv_snd_disable(ndev); |
| 1328 | |
| 1329 | /* Stop RX DMA at next packet boundary */ |
| 1330 | sh_eth_write(ndev, 0, EDRRR); |
| 1331 | |
| 1332 | /* Aside from TX DMA, we can't tell when the hardware is |
| 1333 | * really stopped, so we need to reset to make sure. |
| 1334 | * Before doing that, wait for long enough to *probably* |
| 1335 | * finish transmitting the last packet and poll stats. |
| 1336 | */ |
| 1337 | msleep(2); /* max frame time at 10 Mbps < 1250 us */ |
| 1338 | sh_eth_get_stats(ndev); |
| 1339 | sh_eth_reset(ndev); |
Geert Uytterhoeven | a14c7d1 | 2015-02-27 17:16:26 +0100 | [diff] [blame] | 1340 | |
| 1341 | /* Set MAC address again */ |
| 1342 | update_mac_address(ndev); |
Ben Hutchings | 740c7f3 | 2015-01-27 00:49:32 +0000 | [diff] [blame] | 1343 | } |
| 1344 | |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1345 | /* free Tx skb function */ |
| 1346 | static int sh_eth_txfree(struct net_device *ndev) |
| 1347 | { |
| 1348 | struct sh_eth_private *mdp = netdev_priv(ndev); |
| 1349 | struct sh_eth_txdesc *txdesc; |
Sergei Shtylyov | 128296f | 2014-01-03 15:52:22 +0300 | [diff] [blame] | 1350 | int free_num = 0; |
Sergei Shtylyov | 4fa8c3c | 2016-03-13 01:29:45 +0300 | [diff] [blame] | 1351 | int entry; |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1352 | |
| 1353 | for (; mdp->cur_tx - mdp->dirty_tx > 0; mdp->dirty_tx++) { |
Yoshihiro Shimoda | 525b807 | 2012-06-26 20:00:03 +0000 | [diff] [blame] | 1354 | entry = mdp->dirty_tx % mdp->num_tx_ring; |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1355 | txdesc = &mdp->tx_ring[entry]; |
Sergei Shtylyov | 7cf7247 | 2015-12-28 02:10:47 +0300 | [diff] [blame] | 1356 | if (txdesc->status & cpu_to_le32(TD_TACT)) |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1357 | break; |
Ben Hutchings | 7d7355f | 2015-03-03 00:52:00 +0000 | [diff] [blame] | 1358 | /* TACT bit must be checked before all the following reads */ |
Sergei Shtylyov | f32bfb9 | 2015-11-03 22:36:04 +0300 | [diff] [blame] | 1359 | dma_rmb(); |
Ben Hutchings | e5fd13f | 2015-02-26 20:34:46 +0000 | [diff] [blame] | 1360 | netif_info(mdp, tx_done, ndev, |
| 1361 | "tx entry %d status 0x%08x\n", |
Sergei Shtylyov | 7cf7247 | 2015-12-28 02:10:47 +0300 | [diff] [blame] | 1362 | entry, le32_to_cpu(txdesc->status)); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1363 | /* Free the original skb. */ |
| 1364 | if (mdp->tx_skbuff[entry]) { |
Sergei Shtylyov | 7cf7247 | 2015-12-28 02:10:47 +0300 | [diff] [blame] | 1365 | dma_unmap_single(&ndev->dev, le32_to_cpu(txdesc->addr), |
| 1366 | le32_to_cpu(txdesc->len) >> 16, |
Sergei Shtylyov | 5cbf20c | 2015-12-20 01:48:04 +0300 | [diff] [blame] | 1367 | DMA_TO_DEVICE); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1368 | dev_kfree_skb_irq(mdp->tx_skbuff[entry]); |
| 1369 | mdp->tx_skbuff[entry] = NULL; |
Sergei Shtylyov | 128296f | 2014-01-03 15:52:22 +0300 | [diff] [blame] | 1370 | free_num++; |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1371 | } |
Sergei Shtylyov | 7cf7247 | 2015-12-28 02:10:47 +0300 | [diff] [blame] | 1372 | txdesc->status = cpu_to_le32(TD_TFP); |
Yoshihiro Shimoda | 525b807 | 2012-06-26 20:00:03 +0000 | [diff] [blame] | 1373 | if (entry >= mdp->num_tx_ring - 1) |
Sergei Shtylyov | 7cf7247 | 2015-12-28 02:10:47 +0300 | [diff] [blame] | 1374 | txdesc->status |= cpu_to_le32(TD_TDLE); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1375 | |
Eric Dumazet | bb7d92e | 2012-02-06 22:17:21 +0000 | [diff] [blame] | 1376 | ndev->stats.tx_packets++; |
Sergei Shtylyov | 7cf7247 | 2015-12-28 02:10:47 +0300 | [diff] [blame] | 1377 | ndev->stats.tx_bytes += le32_to_cpu(txdesc->len) >> 16; |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1378 | } |
Sergei Shtylyov | 128296f | 2014-01-03 15:52:22 +0300 | [diff] [blame] | 1379 | return free_num; |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1380 | } |
| 1381 | |
| 1382 | /* Packet receive function */ |
Sergei Shtylyov | 3719109 | 2013-06-19 23:30:23 +0400 | [diff] [blame] | 1383 | static int sh_eth_rx(struct net_device *ndev, u32 intr_status, int *quota) |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1384 | { |
| 1385 | struct sh_eth_private *mdp = netdev_priv(ndev); |
| 1386 | struct sh_eth_rxdesc *rxdesc; |
| 1387 | |
Yoshihiro Shimoda | 525b807 | 2012-06-26 20:00:03 +0000 | [diff] [blame] | 1388 | int entry = mdp->cur_rx % mdp->num_rx_ring; |
| 1389 | int boguscnt = (mdp->dirty_rx + mdp->num_rx_ring) - mdp->cur_rx; |
Mitsuhiro Kimura | 319cd52 | 2014-12-09 21:23:42 +0900 | [diff] [blame] | 1390 | int limit; |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1391 | struct sk_buff *skb; |
Yoshihiro Shimoda | 380af9e | 2009-05-24 23:54:21 +0000 | [diff] [blame] | 1392 | u32 desc_status; |
Sergei Shtylyov | cb36859 | 2015-10-24 00:46:40 +0300 | [diff] [blame] | 1393 | int skbuff_size = mdp->rx_buf_sz + SH_ETH_RX_ALIGN + 32 - 1; |
Ben Hutchings | 52b9fa3 | 2015-01-27 00:50:24 +0000 | [diff] [blame] | 1394 | dma_addr_t dma_addr; |
Sergei Shtylyov | 4fa8c3c | 2016-03-13 01:29:45 +0300 | [diff] [blame] | 1395 | u16 pkt_len; |
Sergei Shtylyov | 5cbf20c | 2015-12-20 01:48:04 +0300 | [diff] [blame] | 1396 | u32 buf_len; |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1397 | |
Mitsuhiro Kimura | 319cd52 | 2014-12-09 21:23:42 +0900 | [diff] [blame] | 1398 | boguscnt = min(boguscnt, *quota); |
| 1399 | limit = boguscnt; |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1400 | rxdesc = &mdp->rx_ring[entry]; |
Sergei Shtylyov | 7cf7247 | 2015-12-28 02:10:47 +0300 | [diff] [blame] | 1401 | while (!(rxdesc->status & cpu_to_le32(RD_RACT))) { |
Ben Hutchings | 7d7355f | 2015-03-03 00:52:00 +0000 | [diff] [blame] | 1402 | /* RACT bit must be checked before all the following reads */ |
Sergei Shtylyov | f32bfb9 | 2015-11-03 22:36:04 +0300 | [diff] [blame] | 1403 | dma_rmb(); |
Sergei Shtylyov | 7cf7247 | 2015-12-28 02:10:47 +0300 | [diff] [blame] | 1404 | desc_status = le32_to_cpu(rxdesc->status); |
| 1405 | pkt_len = le32_to_cpu(rxdesc->len) & RD_RFL; |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1406 | |
| 1407 | if (--boguscnt < 0) |
| 1408 | break; |
| 1409 | |
Ben Hutchings | e5fd13f | 2015-02-26 20:34:46 +0000 | [diff] [blame] | 1410 | netif_info(mdp, rx_status, ndev, |
| 1411 | "rx entry %d status 0x%08x len %d\n", |
| 1412 | entry, desc_status, pkt_len); |
| 1413 | |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1414 | if (!(desc_status & RDFEND)) |
Eric Dumazet | bb7d92e | 2012-02-06 22:17:21 +0000 | [diff] [blame] | 1415 | ndev->stats.rx_length_errors++; |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1416 | |
Sergei Shtylyov | 128296f | 2014-01-03 15:52:22 +0300 | [diff] [blame] | 1417 | /* In case of almost all GETHER/ETHERs, the Receive Frame State |
Yoshihiro Shimoda | dd01989 | 2013-06-13 10:15:45 +0900 | [diff] [blame] | 1418 | * (RFS) bits in the Receive Descriptor 0 are from bit 9 to |
Ben Hutchings | 9b4a636 | 2015-03-03 00:52:39 +0000 | [diff] [blame] | 1419 | * bit 0. However, in case of the R8A7740 and R7S72100 |
| 1420 | * the RFS bits are from bit 25 to bit 16. So, the |
Simon Horman | db89347 | 2014-01-17 09:22:28 +0900 | [diff] [blame] | 1421 | * driver needs right shifting by 16. |
Yoshihiro Shimoda | dd01989 | 2013-06-13 10:15:45 +0900 | [diff] [blame] | 1422 | */ |
Sergei Shtylyov | ac8025a | 2013-06-13 22:12:45 +0400 | [diff] [blame] | 1423 | if (mdp->cd->shift_rd0) |
| 1424 | desc_status >>= 16; |
Yoshihiro Shimoda | dd01989 | 2013-06-13 10:15:45 +0900 | [diff] [blame] | 1425 | |
Sergei Shtylyov | 248be83 | 2015-12-04 01:45:40 +0300 | [diff] [blame] | 1426 | skb = mdp->rx_skbuff[entry]; |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1427 | if (desc_status & (RD_RFS1 | RD_RFS2 | RD_RFS3 | RD_RFS4 | |
| 1428 | RD_RFS5 | RD_RFS6 | RD_RFS10)) { |
Eric Dumazet | bb7d92e | 2012-02-06 22:17:21 +0000 | [diff] [blame] | 1429 | ndev->stats.rx_errors++; |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1430 | if (desc_status & RD_RFS1) |
Eric Dumazet | bb7d92e | 2012-02-06 22:17:21 +0000 | [diff] [blame] | 1431 | ndev->stats.rx_crc_errors++; |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1432 | if (desc_status & RD_RFS2) |
Eric Dumazet | bb7d92e | 2012-02-06 22:17:21 +0000 | [diff] [blame] | 1433 | ndev->stats.rx_frame_errors++; |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1434 | if (desc_status & RD_RFS3) |
Eric Dumazet | bb7d92e | 2012-02-06 22:17:21 +0000 | [diff] [blame] | 1435 | ndev->stats.rx_length_errors++; |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1436 | if (desc_status & RD_RFS4) |
Eric Dumazet | bb7d92e | 2012-02-06 22:17:21 +0000 | [diff] [blame] | 1437 | ndev->stats.rx_length_errors++; |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1438 | if (desc_status & RD_RFS6) |
Eric Dumazet | bb7d92e | 2012-02-06 22:17:21 +0000 | [diff] [blame] | 1439 | ndev->stats.rx_missed_errors++; |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1440 | if (desc_status & RD_RFS10) |
Eric Dumazet | bb7d92e | 2012-02-06 22:17:21 +0000 | [diff] [blame] | 1441 | ndev->stats.rx_over_errors++; |
Sergei Shtylyov | 248be83 | 2015-12-04 01:45:40 +0300 | [diff] [blame] | 1442 | } else if (skb) { |
Sergei Shtylyov | 7cf7247 | 2015-12-28 02:10:47 +0300 | [diff] [blame] | 1443 | dma_addr = le32_to_cpu(rxdesc->addr); |
Yoshihiro Shimoda | 380af9e | 2009-05-24 23:54:21 +0000 | [diff] [blame] | 1444 | if (!mdp->cd->hw_swap) |
| 1445 | sh_eth_soft_swap( |
Sergei Shtylyov | 1299653 | 2015-12-13 23:05:07 +0300 | [diff] [blame] | 1446 | phys_to_virt(ALIGN(dma_addr, 4)), |
Yoshihiro Shimoda | 380af9e | 2009-05-24 23:54:21 +0000 | [diff] [blame] | 1447 | pkt_len + 2); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1448 | mdp->rx_skbuff[entry] = NULL; |
Magnus Damm | 503914c | 2009-12-15 21:16:55 -0800 | [diff] [blame] | 1449 | if (mdp->cd->rpadir) |
| 1450 | skb_reserve(skb, NET_IP_ALIGN); |
Sergei Shtylyov | 1299653 | 2015-12-13 23:05:07 +0300 | [diff] [blame] | 1451 | dma_unmap_single(&ndev->dev, dma_addr, |
Sergei Shtylyov | ab85791 | 2015-10-24 00:46:03 +0300 | [diff] [blame] | 1452 | ALIGN(mdp->rx_buf_sz, 32), |
Ben Hutchings | 52b9fa3 | 2015-01-27 00:50:24 +0000 | [diff] [blame] | 1453 | DMA_FROM_DEVICE); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1454 | skb_put(skb, pkt_len); |
| 1455 | skb->protocol = eth_type_trans(skb, ndev); |
Sergei Shtylyov | a8e9fd0 | 2013-09-03 03:03:10 +0400 | [diff] [blame] | 1456 | netif_receive_skb(skb); |
Eric Dumazet | bb7d92e | 2012-02-06 22:17:21 +0000 | [diff] [blame] | 1457 | ndev->stats.rx_packets++; |
| 1458 | ndev->stats.rx_bytes += pkt_len; |
Ben Hutchings | 25b77ad | 2015-02-26 20:33:30 +0000 | [diff] [blame] | 1459 | if (desc_status & RD_RFS8) |
| 1460 | ndev->stats.multicast++; |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1461 | } |
Yoshihiro Shimoda | 525b807 | 2012-06-26 20:00:03 +0000 | [diff] [blame] | 1462 | entry = (++mdp->cur_rx) % mdp->num_rx_ring; |
Yoshihiro Shimoda | 862df49 | 2009-05-24 23:53:40 +0000 | [diff] [blame] | 1463 | rxdesc = &mdp->rx_ring[entry]; |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1464 | } |
| 1465 | |
| 1466 | /* Refill the Rx ring buffers. */ |
| 1467 | for (; mdp->cur_rx - mdp->dirty_rx > 0; mdp->dirty_rx++) { |
Yoshihiro Shimoda | 525b807 | 2012-06-26 20:00:03 +0000 | [diff] [blame] | 1468 | entry = mdp->dirty_rx % mdp->num_rx_ring; |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1469 | rxdesc = &mdp->rx_ring[entry]; |
Sergei Shtylyov | ab85791 | 2015-10-24 00:46:03 +0300 | [diff] [blame] | 1470 | /* The size of the buffer is 32 byte boundary. */ |
Sergei Shtylyov | 5cbf20c | 2015-12-20 01:48:04 +0300 | [diff] [blame] | 1471 | buf_len = ALIGN(mdp->rx_buf_sz, 32); |
Sergei Shtylyov | 7cf7247 | 2015-12-28 02:10:47 +0300 | [diff] [blame] | 1472 | rxdesc->len = cpu_to_le32(buf_len << 16); |
Nobuhiro Iwamatsu | b0ca2a2 | 2008-06-30 11:08:17 +0900 | [diff] [blame] | 1473 | |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1474 | if (mdp->rx_skbuff[entry] == NULL) { |
Mitsuhiro Kimura | 4d6a949 | 2014-11-27 20:34:00 +0900 | [diff] [blame] | 1475 | skb = netdev_alloc_skb(ndev, skbuff_size); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1476 | if (skb == NULL) |
| 1477 | break; /* Better luck next round. */ |
Yoshihiro Shimoda | 380af9e | 2009-05-24 23:54:21 +0000 | [diff] [blame] | 1478 | sh_eth_set_receive_align(skb); |
Ben Hutchings | 52b9fa3 | 2015-01-27 00:50:24 +0000 | [diff] [blame] | 1479 | dma_addr = dma_map_single(&ndev->dev, skb->data, |
Sergei Shtylyov | 5cbf20c | 2015-12-20 01:48:04 +0300 | [diff] [blame] | 1480 | buf_len, DMA_FROM_DEVICE); |
Ben Hutchings | 52b9fa3 | 2015-01-27 00:50:24 +0000 | [diff] [blame] | 1481 | if (dma_mapping_error(&ndev->dev, dma_addr)) { |
| 1482 | kfree_skb(skb); |
| 1483 | break; |
| 1484 | } |
| 1485 | mdp->rx_skbuff[entry] = skb; |
Yoshihiro Shimoda | 380af9e | 2009-05-24 23:54:21 +0000 | [diff] [blame] | 1486 | |
Eric Dumazet | bc8acf2 | 2010-09-02 13:07:41 -0700 | [diff] [blame] | 1487 | skb_checksum_none_assert(skb); |
Sergei Shtylyov | 7cf7247 | 2015-12-28 02:10:47 +0300 | [diff] [blame] | 1488 | rxdesc->addr = cpu_to_le32(dma_addr); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1489 | } |
Sergei Shtylyov | f32bfb9 | 2015-11-03 22:36:04 +0300 | [diff] [blame] | 1490 | dma_wmb(); /* RACT bit must be set after all the above writes */ |
Yoshihiro Shimoda | 525b807 | 2012-06-26 20:00:03 +0000 | [diff] [blame] | 1491 | if (entry >= mdp->num_rx_ring - 1) |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1492 | rxdesc->status |= |
Sergei Shtylyov | 7cf7247 | 2015-12-28 02:10:47 +0300 | [diff] [blame] | 1493 | cpu_to_le32(RD_RACT | RD_RFP | RD_RDLE); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1494 | else |
Sergei Shtylyov | 7cf7247 | 2015-12-28 02:10:47 +0300 | [diff] [blame] | 1495 | rxdesc->status |= cpu_to_le32(RD_RACT | RD_RFP); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1496 | } |
| 1497 | |
| 1498 | /* Restart Rx engine if stopped. */ |
| 1499 | /* If we don't need to check status, don't. -KDU */ |
Yoshihiro Shimoda | 79fba9f | 2012-05-28 23:07:55 +0000 | [diff] [blame] | 1500 | if (!(sh_eth_read(ndev, EDRRR) & EDRRR_R)) { |
Yoshihiro Shimoda | a18e08b | 2012-06-20 15:26:34 +0000 | [diff] [blame] | 1501 | /* fix the values for the next receiving if RDE is set */ |
Ben Hutchings | 3365711 | 2015-02-26 20:34:14 +0000 | [diff] [blame] | 1502 | if (intr_status & EESR_RDE && |
| 1503 | mdp->reg_offset[RDFAR] != SH_ETH_OFFSET_INVALID) { |
Sergei Shtylyov | 128296f | 2014-01-03 15:52:22 +0300 | [diff] [blame] | 1504 | u32 count = (sh_eth_read(ndev, RDFAR) - |
| 1505 | sh_eth_read(ndev, RDLAR)) >> 4; |
| 1506 | |
| 1507 | mdp->cur_rx = count; |
| 1508 | mdp->dirty_rx = count; |
| 1509 | } |
Yoshihiro Shimoda | 4a55530 | 2011-03-07 21:59:26 +0000 | [diff] [blame] | 1510 | sh_eth_write(ndev, EDRRR_R, EDRRR); |
Yoshihiro Shimoda | 79fba9f | 2012-05-28 23:07:55 +0000 | [diff] [blame] | 1511 | } |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1512 | |
Mitsuhiro Kimura | 319cd52 | 2014-12-09 21:23:42 +0900 | [diff] [blame] | 1513 | *quota -= limit - boguscnt - 1; |
| 1514 | |
Yoshihiro Shimoda | 4f809ce | 2014-06-10 09:40:14 +0900 | [diff] [blame] | 1515 | return *quota <= 0; |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1516 | } |
| 1517 | |
Yoshihiro Shimoda | 4a55530 | 2011-03-07 21:59:26 +0000 | [diff] [blame] | 1518 | static void sh_eth_rcv_snd_disable(struct net_device *ndev) |
Nobuhiro Iwamatsu | dc19e4e | 2011-02-15 21:17:32 +0000 | [diff] [blame] | 1519 | { |
| 1520 | /* disable tx and rx */ |
Sergei Shtylyov | b2b14d2 | 2016-02-10 01:38:28 +0300 | [diff] [blame] | 1521 | sh_eth_modify(ndev, ECMR, ECMR_RE | ECMR_TE, 0); |
Nobuhiro Iwamatsu | dc19e4e | 2011-02-15 21:17:32 +0000 | [diff] [blame] | 1522 | } |
| 1523 | |
Yoshihiro Shimoda | 4a55530 | 2011-03-07 21:59:26 +0000 | [diff] [blame] | 1524 | static void sh_eth_rcv_snd_enable(struct net_device *ndev) |
Nobuhiro Iwamatsu | dc19e4e | 2011-02-15 21:17:32 +0000 | [diff] [blame] | 1525 | { |
| 1526 | /* enable tx and rx */ |
Sergei Shtylyov | b2b14d2 | 2016-02-10 01:38:28 +0300 | [diff] [blame] | 1527 | sh_eth_modify(ndev, ECMR, ECMR_RE | ECMR_TE, ECMR_RE | ECMR_TE); |
Nobuhiro Iwamatsu | dc19e4e | 2011-02-15 21:17:32 +0000 | [diff] [blame] | 1528 | } |
| 1529 | |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1530 | /* error control function */ |
Geert Uytterhoeven | 0799c2d | 2015-01-15 11:54:28 +0100 | [diff] [blame] | 1531 | static void sh_eth_error(struct net_device *ndev, u32 intr_status) |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1532 | { |
| 1533 | struct sh_eth_private *mdp = netdev_priv(ndev); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1534 | u32 felic_stat; |
Yoshihiro Shimoda | 380af9e | 2009-05-24 23:54:21 +0000 | [diff] [blame] | 1535 | u32 link_stat; |
| 1536 | u32 mask; |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1537 | |
| 1538 | if (intr_status & EESR_ECI) { |
Yoshihiro Shimoda | 4a55530 | 2011-03-07 21:59:26 +0000 | [diff] [blame] | 1539 | felic_stat = sh_eth_read(ndev, ECSR); |
| 1540 | sh_eth_write(ndev, felic_stat, ECSR); /* clear int */ |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1541 | if (felic_stat & ECSR_ICD) |
Eric Dumazet | bb7d92e | 2012-02-06 22:17:21 +0000 | [diff] [blame] | 1542 | ndev->stats.tx_carrier_errors++; |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1543 | if (felic_stat & ECSR_LCHNG) { |
| 1544 | /* Link Changed */ |
Yoshihiro Shimoda | 4923576 | 2009-08-27 23:25:03 +0000 | [diff] [blame] | 1545 | if (mdp->cd->no_psr || mdp->no_ether_link) { |
Sergei Shtylyov | 1e1b812 | 2013-03-31 09:50:07 +0000 | [diff] [blame] | 1546 | goto ignore_link; |
Yoshihiro Shimoda | 380af9e | 2009-05-24 23:54:21 +0000 | [diff] [blame] | 1547 | } else { |
Yoshihiro Shimoda | 4a55530 | 2011-03-07 21:59:26 +0000 | [diff] [blame] | 1548 | link_stat = (sh_eth_read(ndev, PSR)); |
Yoshihiro Shimoda | 4923576 | 2009-08-27 23:25:03 +0000 | [diff] [blame] | 1549 | if (mdp->ether_link_active_low) |
| 1550 | link_stat = ~link_stat; |
Yoshihiro Shimoda | 380af9e | 2009-05-24 23:54:21 +0000 | [diff] [blame] | 1551 | } |
Sergei Shtylyov | 128296f | 2014-01-03 15:52:22 +0300 | [diff] [blame] | 1552 | if (!(link_stat & PHY_ST_LINK)) { |
Yoshihiro Shimoda | 4a55530 | 2011-03-07 21:59:26 +0000 | [diff] [blame] | 1553 | sh_eth_rcv_snd_disable(ndev); |
Sergei Shtylyov | 128296f | 2014-01-03 15:52:22 +0300 | [diff] [blame] | 1554 | } else { |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1555 | /* Link Up */ |
Sergei Shtylyov | b2b14d2 | 2016-02-10 01:38:28 +0300 | [diff] [blame] | 1556 | sh_eth_modify(ndev, EESIPR, DMAC_M_ECI, 0); |
Sergei Shtylyov | 128296f | 2014-01-03 15:52:22 +0300 | [diff] [blame] | 1557 | /* clear int */ |
Sergei Shtylyov | b2b14d2 | 2016-02-10 01:38:28 +0300 | [diff] [blame] | 1558 | sh_eth_modify(ndev, ECSR, 0, 0); |
| 1559 | sh_eth_modify(ndev, EESIPR, DMAC_M_ECI, |
| 1560 | DMAC_M_ECI); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1561 | /* enable tx and rx */ |
Yoshihiro Shimoda | 4a55530 | 2011-03-07 21:59:26 +0000 | [diff] [blame] | 1562 | sh_eth_rcv_snd_enable(ndev); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1563 | } |
| 1564 | } |
| 1565 | } |
| 1566 | |
Sergei Shtylyov | 1e1b812 | 2013-03-31 09:50:07 +0000 | [diff] [blame] | 1567 | ignore_link: |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1568 | if (intr_status & EESR_TWB) { |
Sergei Shtylyov | 4eb313a | 2013-06-21 01:13:42 +0400 | [diff] [blame] | 1569 | /* Unused write back interrupt */ |
| 1570 | if (intr_status & EESR_TABT) { /* Transmit Abort int */ |
Eric Dumazet | bb7d92e | 2012-02-06 22:17:21 +0000 | [diff] [blame] | 1571 | ndev->stats.tx_aborted_errors++; |
Sergei Shtylyov | 8d5009f | 2014-03-15 03:30:59 +0300 | [diff] [blame] | 1572 | netif_err(mdp, tx_err, ndev, "Transmit Abort\n"); |
Sergei Shtylyov | 4eb313a | 2013-06-21 01:13:42 +0400 | [diff] [blame] | 1573 | } |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1574 | } |
| 1575 | |
| 1576 | if (intr_status & EESR_RABT) { |
| 1577 | /* Receive Abort int */ |
| 1578 | if (intr_status & EESR_RFRMER) { |
| 1579 | /* Receive Frame Overflow int */ |
Eric Dumazet | bb7d92e | 2012-02-06 22:17:21 +0000 | [diff] [blame] | 1580 | ndev->stats.rx_frame_errors++; |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1581 | } |
| 1582 | } |
Yoshihiro Shimoda | 380af9e | 2009-05-24 23:54:21 +0000 | [diff] [blame] | 1583 | |
Nobuhiro Iwamatsu | dc19e4e | 2011-02-15 21:17:32 +0000 | [diff] [blame] | 1584 | if (intr_status & EESR_TDE) { |
| 1585 | /* Transmit Descriptor Empty int */ |
Eric Dumazet | bb7d92e | 2012-02-06 22:17:21 +0000 | [diff] [blame] | 1586 | ndev->stats.tx_fifo_errors++; |
Sergei Shtylyov | 8d5009f | 2014-03-15 03:30:59 +0300 | [diff] [blame] | 1587 | netif_err(mdp, tx_err, ndev, "Transmit Descriptor Empty\n"); |
Nobuhiro Iwamatsu | dc19e4e | 2011-02-15 21:17:32 +0000 | [diff] [blame] | 1588 | } |
| 1589 | |
| 1590 | if (intr_status & EESR_TFE) { |
| 1591 | /* FIFO under flow */ |
Eric Dumazet | bb7d92e | 2012-02-06 22:17:21 +0000 | [diff] [blame] | 1592 | ndev->stats.tx_fifo_errors++; |
Sergei Shtylyov | 8d5009f | 2014-03-15 03:30:59 +0300 | [diff] [blame] | 1593 | netif_err(mdp, tx_err, ndev, "Transmit FIFO Under flow\n"); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1594 | } |
| 1595 | |
| 1596 | if (intr_status & EESR_RDE) { |
| 1597 | /* Receive Descriptor Empty int */ |
Eric Dumazet | bb7d92e | 2012-02-06 22:17:21 +0000 | [diff] [blame] | 1598 | ndev->stats.rx_over_errors++; |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1599 | } |
Nobuhiro Iwamatsu | dc19e4e | 2011-02-15 21:17:32 +0000 | [diff] [blame] | 1600 | |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1601 | if (intr_status & EESR_RFE) { |
| 1602 | /* Receive FIFO Overflow int */ |
Eric Dumazet | bb7d92e | 2012-02-06 22:17:21 +0000 | [diff] [blame] | 1603 | ndev->stats.rx_fifo_errors++; |
Nobuhiro Iwamatsu | dc19e4e | 2011-02-15 21:17:32 +0000 | [diff] [blame] | 1604 | } |
| 1605 | |
| 1606 | if (!mdp->cd->no_ade && (intr_status & EESR_ADE)) { |
| 1607 | /* Address Error */ |
Eric Dumazet | bb7d92e | 2012-02-06 22:17:21 +0000 | [diff] [blame] | 1608 | ndev->stats.tx_fifo_errors++; |
Sergei Shtylyov | 8d5009f | 2014-03-15 03:30:59 +0300 | [diff] [blame] | 1609 | netif_err(mdp, tx_err, ndev, "Address Error\n"); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1610 | } |
Yoshihiro Shimoda | 380af9e | 2009-05-24 23:54:21 +0000 | [diff] [blame] | 1611 | |
| 1612 | mask = EESR_TWB | EESR_TABT | EESR_ADE | EESR_TDE | EESR_TFE; |
| 1613 | if (mdp->cd->no_ade) |
| 1614 | mask &= ~EESR_ADE; |
| 1615 | if (intr_status & mask) { |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1616 | /* Tx error */ |
Yoshihiro Shimoda | 4a55530 | 2011-03-07 21:59:26 +0000 | [diff] [blame] | 1617 | u32 edtrr = sh_eth_read(ndev, EDTRR); |
Sergei Shtylyov | 090d560 | 2014-01-11 02:41:49 +0300 | [diff] [blame] | 1618 | |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1619 | /* dmesg */ |
Sergei Shtylyov | da24685 | 2014-03-15 03:29:14 +0300 | [diff] [blame] | 1620 | netdev_err(ndev, "TX error. status=%8.8x cur_tx=%8.8x dirty_tx=%8.8x state=%8.8x EDTRR=%8.8x.\n", |
| 1621 | intr_status, mdp->cur_tx, mdp->dirty_tx, |
| 1622 | (u32)ndev->state, edtrr); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1623 | /* dirty buffer free */ |
| 1624 | sh_eth_txfree(ndev); |
| 1625 | |
| 1626 | /* SH7712 BUG */ |
Yoshihiro Shimoda | c5ed536 | 2011-03-07 21:59:38 +0000 | [diff] [blame] | 1627 | if (edtrr ^ sh_eth_get_edtrr_trns(mdp)) { |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1628 | /* tx dma start */ |
Yoshihiro Shimoda | c5ed536 | 2011-03-07 21:59:38 +0000 | [diff] [blame] | 1629 | sh_eth_write(ndev, sh_eth_get_edtrr_trns(mdp), EDTRR); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1630 | } |
| 1631 | /* wakeup */ |
| 1632 | netif_wake_queue(ndev); |
| 1633 | } |
| 1634 | } |
| 1635 | |
| 1636 | static irqreturn_t sh_eth_interrupt(int irq, void *netdev) |
| 1637 | { |
| 1638 | struct net_device *ndev = netdev; |
| 1639 | struct sh_eth_private *mdp = netdev_priv(ndev); |
Yoshihiro Shimoda | 380af9e | 2009-05-24 23:54:21 +0000 | [diff] [blame] | 1640 | struct sh_eth_cpu_data *cd = mdp->cd; |
Nobuhiro Iwamatsu | 0e0fde3 | 2009-03-16 19:50:57 +0000 | [diff] [blame] | 1641 | irqreturn_t ret = IRQ_NONE; |
Geert Uytterhoeven | 0799c2d | 2015-01-15 11:54:28 +0100 | [diff] [blame] | 1642 | u32 intr_status, intr_enable; |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1643 | |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1644 | spin_lock(&mdp->lock); |
| 1645 | |
Sergei Shtylyov | 3893b27345 | 2013-03-31 09:54:20 +0000 | [diff] [blame] | 1646 | /* Get interrupt status */ |
Yoshihiro Shimoda | 4a55530 | 2011-03-07 21:59:26 +0000 | [diff] [blame] | 1647 | intr_status = sh_eth_read(ndev, EESR); |
Sergei Shtylyov | 3893b27345 | 2013-03-31 09:54:20 +0000 | [diff] [blame] | 1648 | /* Mask it with the interrupt mask, forcing ECI interrupt to be always |
| 1649 | * enabled since it's the one that comes thru regardless of the mask, |
| 1650 | * and we need to fully handle it in sh_eth_error() in order to quench |
| 1651 | * it as it doesn't get cleared by just writing 1 to the ECI bit... |
| 1652 | */ |
Sergei Shtylyov | 3719109 | 2013-06-19 23:30:23 +0400 | [diff] [blame] | 1653 | intr_enable = sh_eth_read(ndev, EESIPR); |
| 1654 | intr_status &= intr_enable | DMAC_M_ECI; |
| 1655 | if (intr_status & (EESR_RX_CHECK | cd->tx_check | cd->eesr_err_check)) |
Nobuhiro Iwamatsu | 0e0fde3 | 2009-03-16 19:50:57 +0000 | [diff] [blame] | 1656 | ret = IRQ_HANDLED; |
Sergei Shtylyov | 3719109 | 2013-06-19 23:30:23 +0400 | [diff] [blame] | 1657 | else |
Ben Hutchings | 283e38d | 2015-01-22 12:44:08 +0000 | [diff] [blame] | 1658 | goto out; |
| 1659 | |
| 1660 | if (!likely(mdp->irq_enabled)) { |
| 1661 | sh_eth_write(ndev, 0, EESIPR); |
| 1662 | goto out; |
| 1663 | } |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1664 | |
Sergei Shtylyov | 3719109 | 2013-06-19 23:30:23 +0400 | [diff] [blame] | 1665 | if (intr_status & EESR_RX_CHECK) { |
| 1666 | if (napi_schedule_prep(&mdp->napi)) { |
| 1667 | /* Mask Rx interrupts */ |
| 1668 | sh_eth_write(ndev, intr_enable & ~EESR_RX_CHECK, |
| 1669 | EESIPR); |
| 1670 | __napi_schedule(&mdp->napi); |
| 1671 | } else { |
Sergei Shtylyov | da24685 | 2014-03-15 03:29:14 +0300 | [diff] [blame] | 1672 | netdev_warn(ndev, |
Geert Uytterhoeven | 0799c2d | 2015-01-15 11:54:28 +0100 | [diff] [blame] | 1673 | "ignoring interrupt, status 0x%08x, mask 0x%08x.\n", |
Sergei Shtylyov | da24685 | 2014-03-15 03:29:14 +0300 | [diff] [blame] | 1674 | intr_status, intr_enable); |
Sergei Shtylyov | 3719109 | 2013-06-19 23:30:23 +0400 | [diff] [blame] | 1675 | } |
| 1676 | } |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1677 | |
Nobuhiro Iwamatsu | b0ca2a2 | 2008-06-30 11:08:17 +0900 | [diff] [blame] | 1678 | /* Tx Check */ |
Yoshihiro Shimoda | 380af9e | 2009-05-24 23:54:21 +0000 | [diff] [blame] | 1679 | if (intr_status & cd->tx_check) { |
Sergei Shtylyov | 3719109 | 2013-06-19 23:30:23 +0400 | [diff] [blame] | 1680 | /* Clear Tx interrupts */ |
| 1681 | sh_eth_write(ndev, intr_status & cd->tx_check, EESR); |
| 1682 | |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1683 | sh_eth_txfree(ndev); |
| 1684 | netif_wake_queue(ndev); |
| 1685 | } |
| 1686 | |
Sergei Shtylyov | 3719109 | 2013-06-19 23:30:23 +0400 | [diff] [blame] | 1687 | if (intr_status & cd->eesr_err_check) { |
| 1688 | /* Clear error interrupts */ |
| 1689 | sh_eth_write(ndev, intr_status & cd->eesr_err_check, EESR); |
| 1690 | |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1691 | sh_eth_error(ndev, intr_status); |
Sergei Shtylyov | 3719109 | 2013-06-19 23:30:23 +0400 | [diff] [blame] | 1692 | } |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1693 | |
Ben Hutchings | 283e38d | 2015-01-22 12:44:08 +0000 | [diff] [blame] | 1694 | out: |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1695 | spin_unlock(&mdp->lock); |
| 1696 | |
Nobuhiro Iwamatsu | 0e0fde3 | 2009-03-16 19:50:57 +0000 | [diff] [blame] | 1697 | return ret; |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1698 | } |
| 1699 | |
Sergei Shtylyov | 3719109 | 2013-06-19 23:30:23 +0400 | [diff] [blame] | 1700 | static int sh_eth_poll(struct napi_struct *napi, int budget) |
| 1701 | { |
| 1702 | struct sh_eth_private *mdp = container_of(napi, struct sh_eth_private, |
| 1703 | napi); |
| 1704 | struct net_device *ndev = napi->dev; |
| 1705 | int quota = budget; |
Geert Uytterhoeven | 0799c2d | 2015-01-15 11:54:28 +0100 | [diff] [blame] | 1706 | u32 intr_status; |
Sergei Shtylyov | 3719109 | 2013-06-19 23:30:23 +0400 | [diff] [blame] | 1707 | |
| 1708 | for (;;) { |
| 1709 | intr_status = sh_eth_read(ndev, EESR); |
| 1710 | if (!(intr_status & EESR_RX_CHECK)) |
| 1711 | break; |
| 1712 | /* Clear Rx interrupts */ |
| 1713 | sh_eth_write(ndev, intr_status & EESR_RX_CHECK, EESR); |
| 1714 | |
| 1715 | if (sh_eth_rx(ndev, intr_status, "a)) |
| 1716 | goto out; |
| 1717 | } |
| 1718 | |
| 1719 | napi_complete(napi); |
| 1720 | |
| 1721 | /* Reenable Rx interrupts */ |
Ben Hutchings | 283e38d | 2015-01-22 12:44:08 +0000 | [diff] [blame] | 1722 | if (mdp->irq_enabled) |
| 1723 | sh_eth_write(ndev, mdp->cd->eesipr_value, EESIPR); |
Sergei Shtylyov | 3719109 | 2013-06-19 23:30:23 +0400 | [diff] [blame] | 1724 | out: |
| 1725 | return budget - quota; |
| 1726 | } |
| 1727 | |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1728 | /* PHY state control function */ |
| 1729 | static void sh_eth_adjust_link(struct net_device *ndev) |
| 1730 | { |
| 1731 | struct sh_eth_private *mdp = netdev_priv(ndev); |
| 1732 | struct phy_device *phydev = mdp->phydev; |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1733 | int new_state = 0; |
| 1734 | |
Sergei Shtylyov | 3340d2a | 2013-03-31 10:11:04 +0000 | [diff] [blame] | 1735 | if (phydev->link) { |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1736 | if (phydev->duplex != mdp->duplex) { |
| 1737 | new_state = 1; |
| 1738 | mdp->duplex = phydev->duplex; |
Yoshihiro Shimoda | 380af9e | 2009-05-24 23:54:21 +0000 | [diff] [blame] | 1739 | if (mdp->cd->set_duplex) |
| 1740 | mdp->cd->set_duplex(ndev); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1741 | } |
| 1742 | |
| 1743 | if (phydev->speed != mdp->speed) { |
| 1744 | new_state = 1; |
| 1745 | mdp->speed = phydev->speed; |
Yoshihiro Shimoda | 380af9e | 2009-05-24 23:54:21 +0000 | [diff] [blame] | 1746 | if (mdp->cd->set_rate) |
| 1747 | mdp->cd->set_rate(ndev); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1748 | } |
Sergei Shtylyov | 3340d2a | 2013-03-31 10:11:04 +0000 | [diff] [blame] | 1749 | if (!mdp->link) { |
Sergei Shtylyov | b2b14d2 | 2016-02-10 01:38:28 +0300 | [diff] [blame] | 1750 | sh_eth_modify(ndev, ECMR, ECMR_TXF, 0); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1751 | new_state = 1; |
| 1752 | mdp->link = phydev->link; |
Sergei Shtylyov | 1e1b812 | 2013-03-31 09:50:07 +0000 | [diff] [blame] | 1753 | if (mdp->cd->no_psr || mdp->no_ether_link) |
| 1754 | sh_eth_rcv_snd_enable(ndev); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1755 | } |
| 1756 | } else if (mdp->link) { |
| 1757 | new_state = 1; |
Sergei Shtylyov | 3340d2a | 2013-03-31 10:11:04 +0000 | [diff] [blame] | 1758 | mdp->link = 0; |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1759 | mdp->speed = 0; |
| 1760 | mdp->duplex = -1; |
Sergei Shtylyov | 1e1b812 | 2013-03-31 09:50:07 +0000 | [diff] [blame] | 1761 | if (mdp->cd->no_psr || mdp->no_ether_link) |
| 1762 | sh_eth_rcv_snd_disable(ndev); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1763 | } |
| 1764 | |
Nobuhiro Iwamatsu | dc19e4e | 2011-02-15 21:17:32 +0000 | [diff] [blame] | 1765 | if (new_state && netif_msg_link(mdp)) |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1766 | phy_print_status(phydev); |
| 1767 | } |
| 1768 | |
| 1769 | /* PHY init function */ |
| 1770 | static int sh_eth_phy_init(struct net_device *ndev) |
| 1771 | { |
Ben Dooks | 702eca0 | 2014-03-12 17:47:40 +0000 | [diff] [blame] | 1772 | struct device_node *np = ndev->dev.parent->of_node; |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1773 | struct sh_eth_private *mdp = netdev_priv(ndev); |
Sergei Shtylyov | 4fa8c3c | 2016-03-13 01:29:45 +0300 | [diff] [blame] | 1774 | struct phy_device *phydev; |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1775 | |
Sergei Shtylyov | 3340d2a | 2013-03-31 10:11:04 +0000 | [diff] [blame] | 1776 | mdp->link = 0; |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1777 | mdp->speed = 0; |
| 1778 | mdp->duplex = -1; |
| 1779 | |
| 1780 | /* Try connect to PHY */ |
Ben Dooks | 702eca0 | 2014-03-12 17:47:40 +0000 | [diff] [blame] | 1781 | if (np) { |
| 1782 | struct device_node *pn; |
| 1783 | |
| 1784 | pn = of_parse_phandle(np, "phy-handle", 0); |
| 1785 | phydev = of_phy_connect(ndev, pn, |
| 1786 | sh_eth_adjust_link, 0, |
| 1787 | mdp->phy_interface); |
| 1788 | |
| 1789 | if (!phydev) |
| 1790 | phydev = ERR_PTR(-ENOENT); |
| 1791 | } else { |
| 1792 | char phy_id[MII_BUS_ID_SIZE + 3]; |
| 1793 | |
| 1794 | snprintf(phy_id, sizeof(phy_id), PHY_ID_FMT, |
| 1795 | mdp->mii_bus->id, mdp->phy_id); |
| 1796 | |
| 1797 | phydev = phy_connect(ndev, phy_id, sh_eth_adjust_link, |
| 1798 | mdp->phy_interface); |
| 1799 | } |
| 1800 | |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1801 | if (IS_ERR(phydev)) { |
Sergei Shtylyov | da24685 | 2014-03-15 03:29:14 +0300 | [diff] [blame] | 1802 | netdev_err(ndev, "failed to connect PHY\n"); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1803 | return PTR_ERR(phydev); |
| 1804 | } |
Yoshihiro Shimoda | 380af9e | 2009-05-24 23:54:21 +0000 | [diff] [blame] | 1805 | |
Andrew Lunn | 2220943 | 2016-01-06 20:11:13 +0100 | [diff] [blame] | 1806 | phy_attached_info(phydev); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1807 | |
| 1808 | mdp->phydev = phydev; |
| 1809 | |
| 1810 | return 0; |
| 1811 | } |
| 1812 | |
| 1813 | /* PHY control start function */ |
| 1814 | static int sh_eth_phy_start(struct net_device *ndev) |
| 1815 | { |
| 1816 | struct sh_eth_private *mdp = netdev_priv(ndev); |
| 1817 | int ret; |
| 1818 | |
| 1819 | ret = sh_eth_phy_init(ndev); |
| 1820 | if (ret) |
| 1821 | return ret; |
| 1822 | |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1823 | phy_start(mdp->phydev); |
| 1824 | |
| 1825 | return 0; |
| 1826 | } |
| 1827 | |
Nobuhiro Iwamatsu | dc19e4e | 2011-02-15 21:17:32 +0000 | [diff] [blame] | 1828 | static int sh_eth_get_settings(struct net_device *ndev, |
Sergei Shtylyov | 128296f | 2014-01-03 15:52:22 +0300 | [diff] [blame] | 1829 | struct ethtool_cmd *ecmd) |
Nobuhiro Iwamatsu | dc19e4e | 2011-02-15 21:17:32 +0000 | [diff] [blame] | 1830 | { |
| 1831 | struct sh_eth_private *mdp = netdev_priv(ndev); |
| 1832 | unsigned long flags; |
| 1833 | int ret; |
| 1834 | |
Ben Hutchings | 4f9dce23 | 2015-01-16 17:51:25 +0000 | [diff] [blame] | 1835 | if (!mdp->phydev) |
| 1836 | return -ENODEV; |
| 1837 | |
Nobuhiro Iwamatsu | dc19e4e | 2011-02-15 21:17:32 +0000 | [diff] [blame] | 1838 | spin_lock_irqsave(&mdp->lock, flags); |
| 1839 | ret = phy_ethtool_gset(mdp->phydev, ecmd); |
| 1840 | spin_unlock_irqrestore(&mdp->lock, flags); |
| 1841 | |
| 1842 | return ret; |
| 1843 | } |
| 1844 | |
| 1845 | static int sh_eth_set_settings(struct net_device *ndev, |
Sergei Shtylyov | 128296f | 2014-01-03 15:52:22 +0300 | [diff] [blame] | 1846 | struct ethtool_cmd *ecmd) |
Nobuhiro Iwamatsu | dc19e4e | 2011-02-15 21:17:32 +0000 | [diff] [blame] | 1847 | { |
| 1848 | struct sh_eth_private *mdp = netdev_priv(ndev); |
| 1849 | unsigned long flags; |
| 1850 | int ret; |
Nobuhiro Iwamatsu | dc19e4e | 2011-02-15 21:17:32 +0000 | [diff] [blame] | 1851 | |
Ben Hutchings | 4f9dce23 | 2015-01-16 17:51:25 +0000 | [diff] [blame] | 1852 | if (!mdp->phydev) |
| 1853 | return -ENODEV; |
| 1854 | |
Nobuhiro Iwamatsu | dc19e4e | 2011-02-15 21:17:32 +0000 | [diff] [blame] | 1855 | spin_lock_irqsave(&mdp->lock, flags); |
| 1856 | |
| 1857 | /* disable tx and rx */ |
Yoshihiro Shimoda | 4a55530 | 2011-03-07 21:59:26 +0000 | [diff] [blame] | 1858 | sh_eth_rcv_snd_disable(ndev); |
Nobuhiro Iwamatsu | dc19e4e | 2011-02-15 21:17:32 +0000 | [diff] [blame] | 1859 | |
| 1860 | ret = phy_ethtool_sset(mdp->phydev, ecmd); |
| 1861 | if (ret) |
| 1862 | goto error_exit; |
| 1863 | |
| 1864 | if (ecmd->duplex == DUPLEX_FULL) |
| 1865 | mdp->duplex = 1; |
| 1866 | else |
| 1867 | mdp->duplex = 0; |
| 1868 | |
| 1869 | if (mdp->cd->set_duplex) |
| 1870 | mdp->cd->set_duplex(ndev); |
| 1871 | |
| 1872 | error_exit: |
| 1873 | mdelay(1); |
| 1874 | |
| 1875 | /* enable tx and rx */ |
Yoshihiro Shimoda | 4a55530 | 2011-03-07 21:59:26 +0000 | [diff] [blame] | 1876 | sh_eth_rcv_snd_enable(ndev); |
Nobuhiro Iwamatsu | dc19e4e | 2011-02-15 21:17:32 +0000 | [diff] [blame] | 1877 | |
| 1878 | spin_unlock_irqrestore(&mdp->lock, flags); |
| 1879 | |
| 1880 | return ret; |
| 1881 | } |
| 1882 | |
Ben Hutchings | 6b4b4fe | 2015-02-26 20:34:35 +0000 | [diff] [blame] | 1883 | /* If it is ever necessary to increase SH_ETH_REG_DUMP_MAX_REGS, the |
| 1884 | * version must be bumped as well. Just adding registers up to that |
| 1885 | * limit is fine, as long as the existing register indices don't |
| 1886 | * change. |
| 1887 | */ |
| 1888 | #define SH_ETH_REG_DUMP_VERSION 1 |
| 1889 | #define SH_ETH_REG_DUMP_MAX_REGS 256 |
| 1890 | |
| 1891 | static size_t __sh_eth_get_regs(struct net_device *ndev, u32 *buf) |
| 1892 | { |
| 1893 | struct sh_eth_private *mdp = netdev_priv(ndev); |
| 1894 | struct sh_eth_cpu_data *cd = mdp->cd; |
| 1895 | u32 *valid_map; |
| 1896 | size_t len; |
| 1897 | |
| 1898 | BUILD_BUG_ON(SH_ETH_MAX_REGISTER_OFFSET > SH_ETH_REG_DUMP_MAX_REGS); |
| 1899 | |
| 1900 | /* Dump starts with a bitmap that tells ethtool which |
| 1901 | * registers are defined for this chip. |
| 1902 | */ |
| 1903 | len = DIV_ROUND_UP(SH_ETH_REG_DUMP_MAX_REGS, 32); |
| 1904 | if (buf) { |
| 1905 | valid_map = buf; |
| 1906 | buf += len; |
| 1907 | } else { |
| 1908 | valid_map = NULL; |
| 1909 | } |
| 1910 | |
| 1911 | /* Add a register to the dump, if it has a defined offset. |
| 1912 | * This automatically skips most undefined registers, but for |
| 1913 | * some it is also necessary to check a capability flag in |
| 1914 | * struct sh_eth_cpu_data. |
| 1915 | */ |
| 1916 | #define mark_reg_valid(reg) valid_map[reg / 32] |= 1U << (reg % 32) |
| 1917 | #define add_reg_from(reg, read_expr) do { \ |
| 1918 | if (mdp->reg_offset[reg] != SH_ETH_OFFSET_INVALID) { \ |
| 1919 | if (buf) { \ |
| 1920 | mark_reg_valid(reg); \ |
| 1921 | *buf++ = read_expr; \ |
| 1922 | } \ |
| 1923 | ++len; \ |
| 1924 | } \ |
| 1925 | } while (0) |
| 1926 | #define add_reg(reg) add_reg_from(reg, sh_eth_read(ndev, reg)) |
| 1927 | #define add_tsu_reg(reg) add_reg_from(reg, sh_eth_tsu_read(mdp, reg)) |
| 1928 | |
| 1929 | add_reg(EDSR); |
| 1930 | add_reg(EDMR); |
| 1931 | add_reg(EDTRR); |
| 1932 | add_reg(EDRRR); |
| 1933 | add_reg(EESR); |
| 1934 | add_reg(EESIPR); |
| 1935 | add_reg(TDLAR); |
| 1936 | add_reg(TDFAR); |
| 1937 | add_reg(TDFXR); |
| 1938 | add_reg(TDFFR); |
| 1939 | add_reg(RDLAR); |
| 1940 | add_reg(RDFAR); |
| 1941 | add_reg(RDFXR); |
| 1942 | add_reg(RDFFR); |
| 1943 | add_reg(TRSCER); |
| 1944 | add_reg(RMFCR); |
| 1945 | add_reg(TFTR); |
| 1946 | add_reg(FDR); |
| 1947 | add_reg(RMCR); |
| 1948 | add_reg(TFUCR); |
| 1949 | add_reg(RFOCR); |
| 1950 | if (cd->rmiimode) |
| 1951 | add_reg(RMIIMODE); |
| 1952 | add_reg(FCFTR); |
| 1953 | if (cd->rpadir) |
| 1954 | add_reg(RPADIR); |
| 1955 | if (!cd->no_trimd) |
| 1956 | add_reg(TRIMD); |
| 1957 | add_reg(ECMR); |
| 1958 | add_reg(ECSR); |
| 1959 | add_reg(ECSIPR); |
| 1960 | add_reg(PIR); |
| 1961 | if (!cd->no_psr) |
| 1962 | add_reg(PSR); |
| 1963 | add_reg(RDMLR); |
| 1964 | add_reg(RFLR); |
| 1965 | add_reg(IPGR); |
| 1966 | if (cd->apr) |
| 1967 | add_reg(APR); |
| 1968 | if (cd->mpr) |
| 1969 | add_reg(MPR); |
| 1970 | add_reg(RFCR); |
| 1971 | add_reg(RFCF); |
| 1972 | if (cd->tpauser) |
| 1973 | add_reg(TPAUSER); |
| 1974 | add_reg(TPAUSECR); |
| 1975 | add_reg(GECMR); |
| 1976 | if (cd->bculr) |
| 1977 | add_reg(BCULR); |
| 1978 | add_reg(MAHR); |
| 1979 | add_reg(MALR); |
| 1980 | add_reg(TROCR); |
| 1981 | add_reg(CDCR); |
| 1982 | add_reg(LCCR); |
| 1983 | add_reg(CNDCR); |
| 1984 | add_reg(CEFCR); |
| 1985 | add_reg(FRECR); |
| 1986 | add_reg(TSFRCR); |
| 1987 | add_reg(TLFRCR); |
| 1988 | add_reg(CERCR); |
| 1989 | add_reg(CEECR); |
| 1990 | add_reg(MAFCR); |
| 1991 | if (cd->rtrate) |
| 1992 | add_reg(RTRATE); |
| 1993 | if (cd->hw_crc) |
| 1994 | add_reg(CSMR); |
| 1995 | if (cd->select_mii) |
| 1996 | add_reg(RMII_MII); |
| 1997 | add_reg(ARSTR); |
| 1998 | if (cd->tsu) { |
| 1999 | add_tsu_reg(TSU_CTRST); |
| 2000 | add_tsu_reg(TSU_FWEN0); |
| 2001 | add_tsu_reg(TSU_FWEN1); |
| 2002 | add_tsu_reg(TSU_FCM); |
| 2003 | add_tsu_reg(TSU_BSYSL0); |
| 2004 | add_tsu_reg(TSU_BSYSL1); |
| 2005 | add_tsu_reg(TSU_PRISL0); |
| 2006 | add_tsu_reg(TSU_PRISL1); |
| 2007 | add_tsu_reg(TSU_FWSL0); |
| 2008 | add_tsu_reg(TSU_FWSL1); |
| 2009 | add_tsu_reg(TSU_FWSLC); |
| 2010 | add_tsu_reg(TSU_QTAG0); |
| 2011 | add_tsu_reg(TSU_QTAG1); |
| 2012 | add_tsu_reg(TSU_QTAGM0); |
| 2013 | add_tsu_reg(TSU_QTAGM1); |
| 2014 | add_tsu_reg(TSU_FWSR); |
| 2015 | add_tsu_reg(TSU_FWINMK); |
| 2016 | add_tsu_reg(TSU_ADQT0); |
| 2017 | add_tsu_reg(TSU_ADQT1); |
| 2018 | add_tsu_reg(TSU_VTAG0); |
| 2019 | add_tsu_reg(TSU_VTAG1); |
| 2020 | add_tsu_reg(TSU_ADSBSY); |
| 2021 | add_tsu_reg(TSU_TEN); |
| 2022 | add_tsu_reg(TSU_POST1); |
| 2023 | add_tsu_reg(TSU_POST2); |
| 2024 | add_tsu_reg(TSU_POST3); |
| 2025 | add_tsu_reg(TSU_POST4); |
| 2026 | if (mdp->reg_offset[TSU_ADRH0] != SH_ETH_OFFSET_INVALID) { |
| 2027 | /* This is the start of a table, not just a single |
| 2028 | * register. |
| 2029 | */ |
| 2030 | if (buf) { |
| 2031 | unsigned int i; |
| 2032 | |
| 2033 | mark_reg_valid(TSU_ADRH0); |
| 2034 | for (i = 0; i < SH_ETH_TSU_CAM_ENTRIES * 2; i++) |
| 2035 | *buf++ = ioread32( |
| 2036 | mdp->tsu_addr + |
| 2037 | mdp->reg_offset[TSU_ADRH0] + |
| 2038 | i * 4); |
| 2039 | } |
| 2040 | len += SH_ETH_TSU_CAM_ENTRIES * 2; |
| 2041 | } |
| 2042 | } |
| 2043 | |
| 2044 | #undef mark_reg_valid |
| 2045 | #undef add_reg_from |
| 2046 | #undef add_reg |
| 2047 | #undef add_tsu_reg |
| 2048 | |
| 2049 | return len * 4; |
| 2050 | } |
| 2051 | |
| 2052 | static int sh_eth_get_regs_len(struct net_device *ndev) |
| 2053 | { |
| 2054 | return __sh_eth_get_regs(ndev, NULL); |
| 2055 | } |
| 2056 | |
| 2057 | static void sh_eth_get_regs(struct net_device *ndev, struct ethtool_regs *regs, |
| 2058 | void *buf) |
| 2059 | { |
| 2060 | struct sh_eth_private *mdp = netdev_priv(ndev); |
| 2061 | |
| 2062 | regs->version = SH_ETH_REG_DUMP_VERSION; |
| 2063 | |
| 2064 | pm_runtime_get_sync(&mdp->pdev->dev); |
| 2065 | __sh_eth_get_regs(ndev, buf); |
| 2066 | pm_runtime_put_sync(&mdp->pdev->dev); |
| 2067 | } |
| 2068 | |
Nobuhiro Iwamatsu | dc19e4e | 2011-02-15 21:17:32 +0000 | [diff] [blame] | 2069 | static int sh_eth_nway_reset(struct net_device *ndev) |
| 2070 | { |
| 2071 | struct sh_eth_private *mdp = netdev_priv(ndev); |
| 2072 | unsigned long flags; |
| 2073 | int ret; |
| 2074 | |
Ben Hutchings | 4f9dce23 | 2015-01-16 17:51:25 +0000 | [diff] [blame] | 2075 | if (!mdp->phydev) |
| 2076 | return -ENODEV; |
| 2077 | |
Nobuhiro Iwamatsu | dc19e4e | 2011-02-15 21:17:32 +0000 | [diff] [blame] | 2078 | spin_lock_irqsave(&mdp->lock, flags); |
| 2079 | ret = phy_start_aneg(mdp->phydev); |
| 2080 | spin_unlock_irqrestore(&mdp->lock, flags); |
| 2081 | |
| 2082 | return ret; |
| 2083 | } |
| 2084 | |
| 2085 | static u32 sh_eth_get_msglevel(struct net_device *ndev) |
| 2086 | { |
| 2087 | struct sh_eth_private *mdp = netdev_priv(ndev); |
| 2088 | return mdp->msg_enable; |
| 2089 | } |
| 2090 | |
| 2091 | static void sh_eth_set_msglevel(struct net_device *ndev, u32 value) |
| 2092 | { |
| 2093 | struct sh_eth_private *mdp = netdev_priv(ndev); |
| 2094 | mdp->msg_enable = value; |
| 2095 | } |
| 2096 | |
| 2097 | static const char sh_eth_gstrings_stats[][ETH_GSTRING_LEN] = { |
| 2098 | "rx_current", "tx_current", |
| 2099 | "rx_dirty", "tx_dirty", |
| 2100 | }; |
| 2101 | #define SH_ETH_STATS_LEN ARRAY_SIZE(sh_eth_gstrings_stats) |
| 2102 | |
| 2103 | static int sh_eth_get_sset_count(struct net_device *netdev, int sset) |
| 2104 | { |
| 2105 | switch (sset) { |
| 2106 | case ETH_SS_STATS: |
| 2107 | return SH_ETH_STATS_LEN; |
| 2108 | default: |
| 2109 | return -EOPNOTSUPP; |
| 2110 | } |
| 2111 | } |
| 2112 | |
| 2113 | static void sh_eth_get_ethtool_stats(struct net_device *ndev, |
Sergei Shtylyov | 128296f | 2014-01-03 15:52:22 +0300 | [diff] [blame] | 2114 | struct ethtool_stats *stats, u64 *data) |
Nobuhiro Iwamatsu | dc19e4e | 2011-02-15 21:17:32 +0000 | [diff] [blame] | 2115 | { |
| 2116 | struct sh_eth_private *mdp = netdev_priv(ndev); |
| 2117 | int i = 0; |
| 2118 | |
| 2119 | /* device-specific stats */ |
| 2120 | data[i++] = mdp->cur_rx; |
| 2121 | data[i++] = mdp->cur_tx; |
| 2122 | data[i++] = mdp->dirty_rx; |
| 2123 | data[i++] = mdp->dirty_tx; |
| 2124 | } |
| 2125 | |
| 2126 | static void sh_eth_get_strings(struct net_device *ndev, u32 stringset, u8 *data) |
| 2127 | { |
| 2128 | switch (stringset) { |
| 2129 | case ETH_SS_STATS: |
| 2130 | memcpy(data, *sh_eth_gstrings_stats, |
Sergei Shtylyov | 128296f | 2014-01-03 15:52:22 +0300 | [diff] [blame] | 2131 | sizeof(sh_eth_gstrings_stats)); |
Nobuhiro Iwamatsu | dc19e4e | 2011-02-15 21:17:32 +0000 | [diff] [blame] | 2132 | break; |
| 2133 | } |
| 2134 | } |
| 2135 | |
Yoshihiro Shimoda | 525b807 | 2012-06-26 20:00:03 +0000 | [diff] [blame] | 2136 | static void sh_eth_get_ringparam(struct net_device *ndev, |
| 2137 | struct ethtool_ringparam *ring) |
| 2138 | { |
| 2139 | struct sh_eth_private *mdp = netdev_priv(ndev); |
| 2140 | |
| 2141 | ring->rx_max_pending = RX_RING_MAX; |
| 2142 | ring->tx_max_pending = TX_RING_MAX; |
| 2143 | ring->rx_pending = mdp->num_rx_ring; |
| 2144 | ring->tx_pending = mdp->num_tx_ring; |
| 2145 | } |
| 2146 | |
| 2147 | static int sh_eth_set_ringparam(struct net_device *ndev, |
| 2148 | struct ethtool_ringparam *ring) |
| 2149 | { |
| 2150 | struct sh_eth_private *mdp = netdev_priv(ndev); |
| 2151 | int ret; |
| 2152 | |
| 2153 | if (ring->tx_pending > TX_RING_MAX || |
| 2154 | ring->rx_pending > RX_RING_MAX || |
| 2155 | ring->tx_pending < TX_RING_MIN || |
| 2156 | ring->rx_pending < RX_RING_MIN) |
| 2157 | return -EINVAL; |
| 2158 | if (ring->rx_mini_pending || ring->rx_jumbo_pending) |
| 2159 | return -EINVAL; |
| 2160 | |
| 2161 | if (netif_running(ndev)) { |
Ben Hutchings | bd88891 | 2015-01-22 12:40:25 +0000 | [diff] [blame] | 2162 | netif_device_detach(ndev); |
Yoshihiro Shimoda | 525b807 | 2012-06-26 20:00:03 +0000 | [diff] [blame] | 2163 | netif_tx_disable(ndev); |
Yoshihiro Shimoda | 525b807 | 2012-06-26 20:00:03 +0000 | [diff] [blame] | 2164 | |
Ben Hutchings | 283e38d | 2015-01-22 12:44:08 +0000 | [diff] [blame] | 2165 | /* Serialise with the interrupt handler and NAPI, then |
| 2166 | * disable interrupts. We have to clear the |
| 2167 | * irq_enabled flag first to ensure that interrupts |
| 2168 | * won't be re-enabled. |
| 2169 | */ |
| 2170 | mdp->irq_enabled = false; |
| 2171 | synchronize_irq(ndev->irq); |
| 2172 | napi_synchronize(&mdp->napi); |
Yoshihiro Shimoda | 525b807 | 2012-06-26 20:00:03 +0000 | [diff] [blame] | 2173 | sh_eth_write(ndev, 0x0000, EESIPR); |
Ben Hutchings | 283e38d | 2015-01-22 12:44:08 +0000 | [diff] [blame] | 2174 | |
Ben Hutchings | 740c7f3 | 2015-01-27 00:49:32 +0000 | [diff] [blame] | 2175 | sh_eth_dev_exit(ndev); |
Yoshihiro Shimoda | 525b807 | 2012-06-26 20:00:03 +0000 | [diff] [blame] | 2176 | |
Sergei Shtylyov | 8e03a5e | 2015-11-04 00:55:13 +0300 | [diff] [blame] | 2177 | /* Free all the skbuffs in the Rx queue and the DMA buffers. */ |
Ben Hutchings | 084236d | 2015-01-22 12:41:34 +0000 | [diff] [blame] | 2178 | sh_eth_ring_free(ndev); |
Ben Hutchings | 084236d | 2015-01-22 12:41:34 +0000 | [diff] [blame] | 2179 | } |
Yoshihiro Shimoda | 525b807 | 2012-06-26 20:00:03 +0000 | [diff] [blame] | 2180 | |
| 2181 | /* Set new parameters */ |
| 2182 | mdp->num_rx_ring = ring->rx_pending; |
| 2183 | mdp->num_tx_ring = ring->tx_pending; |
| 2184 | |
Yoshihiro Shimoda | 525b807 | 2012-06-26 20:00:03 +0000 | [diff] [blame] | 2185 | if (netif_running(ndev)) { |
Ben Hutchings | 084236d | 2015-01-22 12:41:34 +0000 | [diff] [blame] | 2186 | ret = sh_eth_ring_init(ndev); |
| 2187 | if (ret < 0) { |
| 2188 | netdev_err(ndev, "%s: sh_eth_ring_init failed.\n", |
| 2189 | __func__); |
| 2190 | return ret; |
| 2191 | } |
Sergei Shtylyov | f796721 | 2016-04-24 19:11:07 +0300 | [diff] [blame] | 2192 | ret = sh_eth_dev_init(ndev); |
Ben Hutchings | 084236d | 2015-01-22 12:41:34 +0000 | [diff] [blame] | 2193 | if (ret < 0) { |
| 2194 | netdev_err(ndev, "%s: sh_eth_dev_init failed.\n", |
| 2195 | __func__); |
| 2196 | return ret; |
| 2197 | } |
| 2198 | |
Ben Hutchings | bd88891 | 2015-01-22 12:40:25 +0000 | [diff] [blame] | 2199 | netif_device_attach(ndev); |
Yoshihiro Shimoda | 525b807 | 2012-06-26 20:00:03 +0000 | [diff] [blame] | 2200 | } |
| 2201 | |
| 2202 | return 0; |
| 2203 | } |
| 2204 | |
stephen hemminger | 9b07be4 | 2012-01-04 12:59:49 +0000 | [diff] [blame] | 2205 | static const struct ethtool_ops sh_eth_ethtool_ops = { |
Nobuhiro Iwamatsu | dc19e4e | 2011-02-15 21:17:32 +0000 | [diff] [blame] | 2206 | .get_settings = sh_eth_get_settings, |
| 2207 | .set_settings = sh_eth_set_settings, |
Ben Hutchings | 6b4b4fe | 2015-02-26 20:34:35 +0000 | [diff] [blame] | 2208 | .get_regs_len = sh_eth_get_regs_len, |
| 2209 | .get_regs = sh_eth_get_regs, |
stephen hemminger | 9b07be4 | 2012-01-04 12:59:49 +0000 | [diff] [blame] | 2210 | .nway_reset = sh_eth_nway_reset, |
Nobuhiro Iwamatsu | dc19e4e | 2011-02-15 21:17:32 +0000 | [diff] [blame] | 2211 | .get_msglevel = sh_eth_get_msglevel, |
| 2212 | .set_msglevel = sh_eth_set_msglevel, |
stephen hemminger | 9b07be4 | 2012-01-04 12:59:49 +0000 | [diff] [blame] | 2213 | .get_link = ethtool_op_get_link, |
Nobuhiro Iwamatsu | dc19e4e | 2011-02-15 21:17:32 +0000 | [diff] [blame] | 2214 | .get_strings = sh_eth_get_strings, |
| 2215 | .get_ethtool_stats = sh_eth_get_ethtool_stats, |
| 2216 | .get_sset_count = sh_eth_get_sset_count, |
Yoshihiro Shimoda | 525b807 | 2012-06-26 20:00:03 +0000 | [diff] [blame] | 2217 | .get_ringparam = sh_eth_get_ringparam, |
| 2218 | .set_ringparam = sh_eth_set_ringparam, |
Nobuhiro Iwamatsu | dc19e4e | 2011-02-15 21:17:32 +0000 | [diff] [blame] | 2219 | }; |
| 2220 | |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 2221 | /* network device open function */ |
| 2222 | static int sh_eth_open(struct net_device *ndev) |
| 2223 | { |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 2224 | struct sh_eth_private *mdp = netdev_priv(ndev); |
Sergei Shtylyov | 4fa8c3c | 2016-03-13 01:29:45 +0300 | [diff] [blame] | 2225 | int ret; |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 2226 | |
Magnus Damm | bcd5149 | 2009-10-09 00:20:04 +0000 | [diff] [blame] | 2227 | pm_runtime_get_sync(&mdp->pdev->dev); |
| 2228 | |
Sergei Shtylyov | d2779e9 | 2013-09-04 02:41:27 +0400 | [diff] [blame] | 2229 | napi_enable(&mdp->napi); |
| 2230 | |
Joe Perches | a0607fd | 2009-11-18 23:29:17 -0800 | [diff] [blame] | 2231 | ret = request_irq(ndev->irq, sh_eth_interrupt, |
Nobuhiro Iwamatsu | 5b3dfd1 | 2013-06-06 09:49:30 +0000 | [diff] [blame] | 2232 | mdp->cd->irq_flags, ndev->name, ndev); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 2233 | if (ret) { |
Sergei Shtylyov | da24685 | 2014-03-15 03:29:14 +0300 | [diff] [blame] | 2234 | netdev_err(ndev, "Can not assign IRQ number\n"); |
Sergei Shtylyov | d2779e9 | 2013-09-04 02:41:27 +0400 | [diff] [blame] | 2235 | goto out_napi_off; |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 2236 | } |
| 2237 | |
| 2238 | /* Descriptor set */ |
| 2239 | ret = sh_eth_ring_init(ndev); |
| 2240 | if (ret) |
| 2241 | goto out_free_irq; |
| 2242 | |
| 2243 | /* device init */ |
Sergei Shtylyov | f796721 | 2016-04-24 19:11:07 +0300 | [diff] [blame] | 2244 | ret = sh_eth_dev_init(ndev); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 2245 | if (ret) |
| 2246 | goto out_free_irq; |
| 2247 | |
| 2248 | /* PHY control start*/ |
| 2249 | ret = sh_eth_phy_start(ndev); |
| 2250 | if (ret) |
| 2251 | goto out_free_irq; |
| 2252 | |
Sergei Shtylyov | ad846aa | 2016-03-14 01:09:53 +0300 | [diff] [blame] | 2253 | netif_start_queue(ndev); |
| 2254 | |
Mitsuhiro Kimura | 7fa2955 | 2014-11-28 10:04:15 +0900 | [diff] [blame] | 2255 | mdp->is_opened = 1; |
| 2256 | |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 2257 | return ret; |
| 2258 | |
| 2259 | out_free_irq: |
| 2260 | free_irq(ndev->irq, ndev); |
Sergei Shtylyov | d2779e9 | 2013-09-04 02:41:27 +0400 | [diff] [blame] | 2261 | out_napi_off: |
| 2262 | napi_disable(&mdp->napi); |
Magnus Damm | bcd5149 | 2009-10-09 00:20:04 +0000 | [diff] [blame] | 2263 | pm_runtime_put_sync(&mdp->pdev->dev); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 2264 | return ret; |
| 2265 | } |
| 2266 | |
| 2267 | /* Timeout function */ |
| 2268 | static void sh_eth_tx_timeout(struct net_device *ndev) |
| 2269 | { |
| 2270 | struct sh_eth_private *mdp = netdev_priv(ndev); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 2271 | struct sh_eth_rxdesc *rxdesc; |
| 2272 | int i; |
| 2273 | |
| 2274 | netif_stop_queue(ndev); |
| 2275 | |
Sergei Shtylyov | 8d5009f | 2014-03-15 03:30:59 +0300 | [diff] [blame] | 2276 | netif_err(mdp, timer, ndev, |
| 2277 | "transmit timed out, status %8.8x, resetting...\n", |
Geert Uytterhoeven | 0799c2d | 2015-01-15 11:54:28 +0100 | [diff] [blame] | 2278 | sh_eth_read(ndev, EESR)); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 2279 | |
| 2280 | /* tx_errors count up */ |
Eric Dumazet | bb7d92e | 2012-02-06 22:17:21 +0000 | [diff] [blame] | 2281 | ndev->stats.tx_errors++; |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 2282 | |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 2283 | /* Free all the skbuffs in the Rx queue. */ |
Yoshihiro Shimoda | 525b807 | 2012-06-26 20:00:03 +0000 | [diff] [blame] | 2284 | for (i = 0; i < mdp->num_rx_ring; i++) { |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 2285 | rxdesc = &mdp->rx_ring[i]; |
Sergei Shtylyov | 7cf7247 | 2015-12-28 02:10:47 +0300 | [diff] [blame] | 2286 | rxdesc->status = cpu_to_le32(0); |
| 2287 | rxdesc->addr = cpu_to_le32(0xBADF00D0); |
Sergei Shtylyov | 179d80a | 2014-06-28 04:10:00 +0400 | [diff] [blame] | 2288 | dev_kfree_skb(mdp->rx_skbuff[i]); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 2289 | mdp->rx_skbuff[i] = NULL; |
| 2290 | } |
Yoshihiro Shimoda | 525b807 | 2012-06-26 20:00:03 +0000 | [diff] [blame] | 2291 | for (i = 0; i < mdp->num_tx_ring; i++) { |
Sergei Shtylyov | 179d80a | 2014-06-28 04:10:00 +0400 | [diff] [blame] | 2292 | dev_kfree_skb(mdp->tx_skbuff[i]); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 2293 | mdp->tx_skbuff[i] = NULL; |
| 2294 | } |
| 2295 | |
| 2296 | /* device init */ |
Sergei Shtylyov | f796721 | 2016-04-24 19:11:07 +0300 | [diff] [blame] | 2297 | sh_eth_dev_init(ndev); |
Sergei Shtylyov | ad846aa | 2016-03-14 01:09:53 +0300 | [diff] [blame] | 2298 | |
| 2299 | netif_start_queue(ndev); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 2300 | } |
| 2301 | |
| 2302 | /* Packet transmit function */ |
| 2303 | static int sh_eth_start_xmit(struct sk_buff *skb, struct net_device *ndev) |
| 2304 | { |
| 2305 | struct sh_eth_private *mdp = netdev_priv(ndev); |
| 2306 | struct sh_eth_txdesc *txdesc; |
Sergei Shtylyov | 1299653 | 2015-12-13 23:05:07 +0300 | [diff] [blame] | 2307 | dma_addr_t dma_addr; |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 2308 | u32 entry; |
Nobuhiro Iwamatsu | fb5e2f9 | 2008-11-17 20:29:58 +0000 | [diff] [blame] | 2309 | unsigned long flags; |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 2310 | |
| 2311 | spin_lock_irqsave(&mdp->lock, flags); |
Yoshihiro Shimoda | 525b807 | 2012-06-26 20:00:03 +0000 | [diff] [blame] | 2312 | if ((mdp->cur_tx - mdp->dirty_tx) >= (mdp->num_tx_ring - 4)) { |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 2313 | if (!sh_eth_txfree(ndev)) { |
Sergei Shtylyov | 8d5009f | 2014-03-15 03:30:59 +0300 | [diff] [blame] | 2314 | netif_warn(mdp, tx_queued, ndev, "TxFD exhausted.\n"); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 2315 | netif_stop_queue(ndev); |
| 2316 | spin_unlock_irqrestore(&mdp->lock, flags); |
Patrick McHardy | 5b54814 | 2009-06-12 06:22:29 +0000 | [diff] [blame] | 2317 | return NETDEV_TX_BUSY; |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 2318 | } |
| 2319 | } |
| 2320 | spin_unlock_irqrestore(&mdp->lock, flags); |
| 2321 | |
Ben Hutchings | dacc73e | 2015-03-03 00:53:08 +0000 | [diff] [blame] | 2322 | if (skb_put_padto(skb, ETH_ZLEN)) |
Ben Hutchings | eebfb64 | 2015-01-22 12:40:13 +0000 | [diff] [blame] | 2323 | return NETDEV_TX_OK; |
| 2324 | |
Yoshihiro Shimoda | 525b807 | 2012-06-26 20:00:03 +0000 | [diff] [blame] | 2325 | entry = mdp->cur_tx % mdp->num_tx_ring; |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 2326 | mdp->tx_skbuff[entry] = skb; |
| 2327 | txdesc = &mdp->tx_ring[entry]; |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 2328 | /* soft swap. */ |
Yoshihiro Shimoda | 380af9e | 2009-05-24 23:54:21 +0000 | [diff] [blame] | 2329 | if (!mdp->cd->hw_swap) |
Sergei Shtylyov | 3e23099 | 2015-12-13 21:27:04 +0300 | [diff] [blame] | 2330 | sh_eth_soft_swap(PTR_ALIGN(skb->data, 4), skb->len + 2); |
Sergei Shtylyov | 1299653 | 2015-12-13 23:05:07 +0300 | [diff] [blame] | 2331 | dma_addr = dma_map_single(&ndev->dev, skb->data, skb->len, |
| 2332 | DMA_TO_DEVICE); |
| 2333 | if (dma_mapping_error(&ndev->dev, dma_addr)) { |
Ben Hutchings | aa3933b | 2015-01-27 00:49:47 +0000 | [diff] [blame] | 2334 | kfree_skb(skb); |
| 2335 | return NETDEV_TX_OK; |
| 2336 | } |
Sergei Shtylyov | 7cf7247 | 2015-12-28 02:10:47 +0300 | [diff] [blame] | 2337 | txdesc->addr = cpu_to_le32(dma_addr); |
| 2338 | txdesc->len = cpu_to_le32(skb->len << 16); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 2339 | |
Sergei Shtylyov | f32bfb9 | 2015-11-03 22:36:04 +0300 | [diff] [blame] | 2340 | dma_wmb(); /* TACT bit must be set after all the above writes */ |
Yoshihiro Shimoda | 525b807 | 2012-06-26 20:00:03 +0000 | [diff] [blame] | 2341 | if (entry >= mdp->num_tx_ring - 1) |
Sergei Shtylyov | 7cf7247 | 2015-12-28 02:10:47 +0300 | [diff] [blame] | 2342 | txdesc->status |= cpu_to_le32(TD_TACT | TD_TDLE); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 2343 | else |
Sergei Shtylyov | 7cf7247 | 2015-12-28 02:10:47 +0300 | [diff] [blame] | 2344 | txdesc->status |= cpu_to_le32(TD_TACT); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 2345 | |
| 2346 | mdp->cur_tx++; |
| 2347 | |
Yoshihiro Shimoda | c5ed536 | 2011-03-07 21:59:38 +0000 | [diff] [blame] | 2348 | if (!(sh_eth_read(ndev, EDTRR) & sh_eth_get_edtrr_trns(mdp))) |
| 2349 | sh_eth_write(ndev, sh_eth_get_edtrr_trns(mdp), EDTRR); |
Nobuhiro Iwamatsu | b0ca2a2 | 2008-06-30 11:08:17 +0900 | [diff] [blame] | 2350 | |
Patrick McHardy | 6ed1065 | 2009-06-23 06:03:08 +0000 | [diff] [blame] | 2351 | return NETDEV_TX_OK; |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 2352 | } |
| 2353 | |
Ben Hutchings | 4398f9c | 2015-02-26 20:35:05 +0000 | [diff] [blame] | 2354 | /* The statistics registers have write-clear behaviour, which means we |
| 2355 | * will lose any increment between the read and write. We mitigate |
| 2356 | * this by only clearing when we read a non-zero value, so we will |
| 2357 | * never falsely report a total of zero. |
| 2358 | */ |
| 2359 | static void |
| 2360 | sh_eth_update_stat(struct net_device *ndev, unsigned long *stat, int reg) |
| 2361 | { |
| 2362 | u32 delta = sh_eth_read(ndev, reg); |
| 2363 | |
| 2364 | if (delta) { |
| 2365 | *stat += delta; |
| 2366 | sh_eth_write(ndev, 0, reg); |
| 2367 | } |
| 2368 | } |
| 2369 | |
Mitsuhiro Kimura | 7fa2955 | 2014-11-28 10:04:15 +0900 | [diff] [blame] | 2370 | static struct net_device_stats *sh_eth_get_stats(struct net_device *ndev) |
| 2371 | { |
| 2372 | struct sh_eth_private *mdp = netdev_priv(ndev); |
| 2373 | |
| 2374 | if (sh_eth_is_rz_fast_ether(mdp)) |
| 2375 | return &ndev->stats; |
| 2376 | |
| 2377 | if (!mdp->is_opened) |
| 2378 | return &ndev->stats; |
| 2379 | |
Ben Hutchings | 4398f9c | 2015-02-26 20:35:05 +0000 | [diff] [blame] | 2380 | sh_eth_update_stat(ndev, &ndev->stats.tx_dropped, TROCR); |
| 2381 | sh_eth_update_stat(ndev, &ndev->stats.collisions, CDCR); |
| 2382 | sh_eth_update_stat(ndev, &ndev->stats.tx_carrier_errors, LCCR); |
Mitsuhiro Kimura | 7fa2955 | 2014-11-28 10:04:15 +0900 | [diff] [blame] | 2383 | |
| 2384 | if (sh_eth_is_gether(mdp)) { |
Ben Hutchings | 4398f9c | 2015-02-26 20:35:05 +0000 | [diff] [blame] | 2385 | sh_eth_update_stat(ndev, &ndev->stats.tx_carrier_errors, |
| 2386 | CERCR); |
| 2387 | sh_eth_update_stat(ndev, &ndev->stats.tx_carrier_errors, |
| 2388 | CEECR); |
Mitsuhiro Kimura | 7fa2955 | 2014-11-28 10:04:15 +0900 | [diff] [blame] | 2389 | } else { |
Ben Hutchings | 4398f9c | 2015-02-26 20:35:05 +0000 | [diff] [blame] | 2390 | sh_eth_update_stat(ndev, &ndev->stats.tx_carrier_errors, |
| 2391 | CNDCR); |
Mitsuhiro Kimura | 7fa2955 | 2014-11-28 10:04:15 +0900 | [diff] [blame] | 2392 | } |
| 2393 | |
| 2394 | return &ndev->stats; |
| 2395 | } |
| 2396 | |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 2397 | /* device close function */ |
| 2398 | static int sh_eth_close(struct net_device *ndev) |
| 2399 | { |
| 2400 | struct sh_eth_private *mdp = netdev_priv(ndev); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 2401 | |
| 2402 | netif_stop_queue(ndev); |
| 2403 | |
Ben Hutchings | 283e38d | 2015-01-22 12:44:08 +0000 | [diff] [blame] | 2404 | /* Serialise with the interrupt handler and NAPI, then disable |
| 2405 | * interrupts. We have to clear the irq_enabled flag first to |
| 2406 | * ensure that interrupts won't be re-enabled. |
| 2407 | */ |
| 2408 | mdp->irq_enabled = false; |
| 2409 | synchronize_irq(ndev->irq); |
| 2410 | napi_disable(&mdp->napi); |
Yoshihiro Shimoda | 4a55530 | 2011-03-07 21:59:26 +0000 | [diff] [blame] | 2411 | sh_eth_write(ndev, 0x0000, EESIPR); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 2412 | |
Ben Hutchings | 740c7f3 | 2015-01-27 00:49:32 +0000 | [diff] [blame] | 2413 | sh_eth_dev_exit(ndev); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 2414 | |
| 2415 | /* PHY Disconnect */ |
| 2416 | if (mdp->phydev) { |
| 2417 | phy_stop(mdp->phydev); |
| 2418 | phy_disconnect(mdp->phydev); |
Ben Hutchings | 4f9dce23 | 2015-01-16 17:51:25 +0000 | [diff] [blame] | 2419 | mdp->phydev = NULL; |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 2420 | } |
| 2421 | |
| 2422 | free_irq(ndev->irq, ndev); |
| 2423 | |
Sergei Shtylyov | 8e03a5e | 2015-11-04 00:55:13 +0300 | [diff] [blame] | 2424 | /* Free all the skbuffs in the Rx queue and the DMA buffer. */ |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 2425 | sh_eth_ring_free(ndev); |
| 2426 | |
Magnus Damm | bcd5149 | 2009-10-09 00:20:04 +0000 | [diff] [blame] | 2427 | pm_runtime_put_sync(&mdp->pdev->dev); |
| 2428 | |
Mitsuhiro Kimura | 7fa2955 | 2014-11-28 10:04:15 +0900 | [diff] [blame] | 2429 | mdp->is_opened = 0; |
| 2430 | |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 2431 | return 0; |
| 2432 | } |
| 2433 | |
Eric Dumazet | bb7d92e | 2012-02-06 22:17:21 +0000 | [diff] [blame] | 2434 | /* ioctl to device function */ |
Sergei Shtylyov | 128296f | 2014-01-03 15:52:22 +0300 | [diff] [blame] | 2435 | static int sh_eth_do_ioctl(struct net_device *ndev, struct ifreq *rq, int cmd) |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 2436 | { |
| 2437 | struct sh_eth_private *mdp = netdev_priv(ndev); |
| 2438 | struct phy_device *phydev = mdp->phydev; |
| 2439 | |
| 2440 | if (!netif_running(ndev)) |
| 2441 | return -EINVAL; |
| 2442 | |
| 2443 | if (!phydev) |
| 2444 | return -ENODEV; |
| 2445 | |
Richard Cochran | 28b0411 | 2010-07-17 08:48:55 +0000 | [diff] [blame] | 2446 | return phy_mii_ioctl(phydev, rq, cmd); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 2447 | } |
| 2448 | |
Yoshihiro Shimoda | 6743fe6 | 2012-02-15 17:55:03 +0000 | [diff] [blame] | 2449 | /* For TSU_POSTn. Please refer to the manual about this (strange) bitfields */ |
| 2450 | static void *sh_eth_tsu_get_post_reg_offset(struct sh_eth_private *mdp, |
| 2451 | int entry) |
| 2452 | { |
| 2453 | return sh_eth_tsu_get_offset(mdp, TSU_POST1) + (entry / 8 * 4); |
| 2454 | } |
| 2455 | |
| 2456 | static u32 sh_eth_tsu_get_post_mask(int entry) |
| 2457 | { |
| 2458 | return 0x0f << (28 - ((entry % 8) * 4)); |
| 2459 | } |
| 2460 | |
| 2461 | static u32 sh_eth_tsu_get_post_bit(struct sh_eth_private *mdp, int entry) |
| 2462 | { |
| 2463 | return (0x08 >> (mdp->port << 1)) << (28 - ((entry % 8) * 4)); |
| 2464 | } |
| 2465 | |
| 2466 | static void sh_eth_tsu_enable_cam_entry_post(struct net_device *ndev, |
| 2467 | int entry) |
| 2468 | { |
| 2469 | struct sh_eth_private *mdp = netdev_priv(ndev); |
| 2470 | u32 tmp; |
| 2471 | void *reg_offset; |
| 2472 | |
| 2473 | reg_offset = sh_eth_tsu_get_post_reg_offset(mdp, entry); |
| 2474 | tmp = ioread32(reg_offset); |
| 2475 | iowrite32(tmp | sh_eth_tsu_get_post_bit(mdp, entry), reg_offset); |
| 2476 | } |
| 2477 | |
| 2478 | static bool sh_eth_tsu_disable_cam_entry_post(struct net_device *ndev, |
| 2479 | int entry) |
| 2480 | { |
| 2481 | struct sh_eth_private *mdp = netdev_priv(ndev); |
| 2482 | u32 post_mask, ref_mask, tmp; |
| 2483 | void *reg_offset; |
| 2484 | |
| 2485 | reg_offset = sh_eth_tsu_get_post_reg_offset(mdp, entry); |
| 2486 | post_mask = sh_eth_tsu_get_post_mask(entry); |
| 2487 | ref_mask = sh_eth_tsu_get_post_bit(mdp, entry) & ~post_mask; |
| 2488 | |
| 2489 | tmp = ioread32(reg_offset); |
| 2490 | iowrite32(tmp & ~post_mask, reg_offset); |
| 2491 | |
| 2492 | /* If other port enables, the function returns "true" */ |
| 2493 | return tmp & ref_mask; |
| 2494 | } |
| 2495 | |
| 2496 | static int sh_eth_tsu_busy(struct net_device *ndev) |
| 2497 | { |
| 2498 | int timeout = SH_ETH_TSU_TIMEOUT_MS * 100; |
| 2499 | struct sh_eth_private *mdp = netdev_priv(ndev); |
| 2500 | |
| 2501 | while ((sh_eth_tsu_read(mdp, TSU_ADSBSY) & TSU_ADSBSY_0)) { |
| 2502 | udelay(10); |
| 2503 | timeout--; |
| 2504 | if (timeout <= 0) { |
Sergei Shtylyov | da24685 | 2014-03-15 03:29:14 +0300 | [diff] [blame] | 2505 | netdev_err(ndev, "%s: timeout\n", __func__); |
Yoshihiro Shimoda | 6743fe6 | 2012-02-15 17:55:03 +0000 | [diff] [blame] | 2506 | return -ETIMEDOUT; |
| 2507 | } |
| 2508 | } |
| 2509 | |
| 2510 | return 0; |
| 2511 | } |
| 2512 | |
| 2513 | static int sh_eth_tsu_write_entry(struct net_device *ndev, void *reg, |
| 2514 | const u8 *addr) |
| 2515 | { |
| 2516 | u32 val; |
| 2517 | |
| 2518 | val = addr[0] << 24 | addr[1] << 16 | addr[2] << 8 | addr[3]; |
| 2519 | iowrite32(val, reg); |
| 2520 | if (sh_eth_tsu_busy(ndev) < 0) |
| 2521 | return -EBUSY; |
| 2522 | |
| 2523 | val = addr[4] << 8 | addr[5]; |
| 2524 | iowrite32(val, reg + 4); |
| 2525 | if (sh_eth_tsu_busy(ndev) < 0) |
| 2526 | return -EBUSY; |
| 2527 | |
| 2528 | return 0; |
| 2529 | } |
| 2530 | |
| 2531 | static void sh_eth_tsu_read_entry(void *reg, u8 *addr) |
| 2532 | { |
| 2533 | u32 val; |
| 2534 | |
| 2535 | val = ioread32(reg); |
| 2536 | addr[0] = (val >> 24) & 0xff; |
| 2537 | addr[1] = (val >> 16) & 0xff; |
| 2538 | addr[2] = (val >> 8) & 0xff; |
| 2539 | addr[3] = val & 0xff; |
| 2540 | val = ioread32(reg + 4); |
| 2541 | addr[4] = (val >> 8) & 0xff; |
| 2542 | addr[5] = val & 0xff; |
| 2543 | } |
| 2544 | |
| 2545 | |
| 2546 | static int sh_eth_tsu_find_entry(struct net_device *ndev, const u8 *addr) |
| 2547 | { |
| 2548 | struct sh_eth_private *mdp = netdev_priv(ndev); |
| 2549 | void *reg_offset = sh_eth_tsu_get_offset(mdp, TSU_ADRH0); |
| 2550 | int i; |
| 2551 | u8 c_addr[ETH_ALEN]; |
| 2552 | |
| 2553 | for (i = 0; i < SH_ETH_TSU_CAM_ENTRIES; i++, reg_offset += 8) { |
| 2554 | sh_eth_tsu_read_entry(reg_offset, c_addr); |
dingtianhong | c4bde29 | 2013-12-30 15:41:17 +0800 | [diff] [blame] | 2555 | if (ether_addr_equal(addr, c_addr)) |
Yoshihiro Shimoda | 6743fe6 | 2012-02-15 17:55:03 +0000 | [diff] [blame] | 2556 | return i; |
| 2557 | } |
| 2558 | |
| 2559 | return -ENOENT; |
| 2560 | } |
| 2561 | |
| 2562 | static int sh_eth_tsu_find_empty(struct net_device *ndev) |
| 2563 | { |
| 2564 | u8 blank[ETH_ALEN]; |
| 2565 | int entry; |
| 2566 | |
| 2567 | memset(blank, 0, sizeof(blank)); |
| 2568 | entry = sh_eth_tsu_find_entry(ndev, blank); |
| 2569 | return (entry < 0) ? -ENOMEM : entry; |
| 2570 | } |
| 2571 | |
| 2572 | static int sh_eth_tsu_disable_cam_entry_table(struct net_device *ndev, |
| 2573 | int entry) |
| 2574 | { |
| 2575 | struct sh_eth_private *mdp = netdev_priv(ndev); |
| 2576 | void *reg_offset = sh_eth_tsu_get_offset(mdp, TSU_ADRH0); |
| 2577 | int ret; |
| 2578 | u8 blank[ETH_ALEN]; |
| 2579 | |
| 2580 | sh_eth_tsu_write(mdp, sh_eth_tsu_read(mdp, TSU_TEN) & |
| 2581 | ~(1 << (31 - entry)), TSU_TEN); |
| 2582 | |
| 2583 | memset(blank, 0, sizeof(blank)); |
| 2584 | ret = sh_eth_tsu_write_entry(ndev, reg_offset + entry * 8, blank); |
| 2585 | if (ret < 0) |
| 2586 | return ret; |
| 2587 | return 0; |
| 2588 | } |
| 2589 | |
| 2590 | static int sh_eth_tsu_add_entry(struct net_device *ndev, const u8 *addr) |
| 2591 | { |
| 2592 | struct sh_eth_private *mdp = netdev_priv(ndev); |
| 2593 | void *reg_offset = sh_eth_tsu_get_offset(mdp, TSU_ADRH0); |
| 2594 | int i, ret; |
| 2595 | |
| 2596 | if (!mdp->cd->tsu) |
| 2597 | return 0; |
| 2598 | |
| 2599 | i = sh_eth_tsu_find_entry(ndev, addr); |
| 2600 | if (i < 0) { |
| 2601 | /* No entry found, create one */ |
| 2602 | i = sh_eth_tsu_find_empty(ndev); |
| 2603 | if (i < 0) |
| 2604 | return -ENOMEM; |
| 2605 | ret = sh_eth_tsu_write_entry(ndev, reg_offset + i * 8, addr); |
| 2606 | if (ret < 0) |
| 2607 | return ret; |
| 2608 | |
| 2609 | /* Enable the entry */ |
| 2610 | sh_eth_tsu_write(mdp, sh_eth_tsu_read(mdp, TSU_TEN) | |
| 2611 | (1 << (31 - i)), TSU_TEN); |
| 2612 | } |
| 2613 | |
| 2614 | /* Entry found or created, enable POST */ |
| 2615 | sh_eth_tsu_enable_cam_entry_post(ndev, i); |
| 2616 | |
| 2617 | return 0; |
| 2618 | } |
| 2619 | |
| 2620 | static int sh_eth_tsu_del_entry(struct net_device *ndev, const u8 *addr) |
| 2621 | { |
| 2622 | struct sh_eth_private *mdp = netdev_priv(ndev); |
| 2623 | int i, ret; |
| 2624 | |
| 2625 | if (!mdp->cd->tsu) |
| 2626 | return 0; |
| 2627 | |
| 2628 | i = sh_eth_tsu_find_entry(ndev, addr); |
| 2629 | if (i) { |
| 2630 | /* Entry found */ |
| 2631 | if (sh_eth_tsu_disable_cam_entry_post(ndev, i)) |
| 2632 | goto done; |
| 2633 | |
| 2634 | /* Disable the entry if both ports was disabled */ |
| 2635 | ret = sh_eth_tsu_disable_cam_entry_table(ndev, i); |
| 2636 | if (ret < 0) |
| 2637 | return ret; |
| 2638 | } |
| 2639 | done: |
| 2640 | return 0; |
| 2641 | } |
| 2642 | |
| 2643 | static int sh_eth_tsu_purge_all(struct net_device *ndev) |
| 2644 | { |
| 2645 | struct sh_eth_private *mdp = netdev_priv(ndev); |
| 2646 | int i, ret; |
| 2647 | |
Ben Hutchings | b37feed | 2015-01-16 17:51:12 +0000 | [diff] [blame] | 2648 | if (!mdp->cd->tsu) |
Yoshihiro Shimoda | 6743fe6 | 2012-02-15 17:55:03 +0000 | [diff] [blame] | 2649 | return 0; |
| 2650 | |
| 2651 | for (i = 0; i < SH_ETH_TSU_CAM_ENTRIES; i++) { |
| 2652 | if (sh_eth_tsu_disable_cam_entry_post(ndev, i)) |
| 2653 | continue; |
| 2654 | |
| 2655 | /* Disable the entry if both ports was disabled */ |
| 2656 | ret = sh_eth_tsu_disable_cam_entry_table(ndev, i); |
| 2657 | if (ret < 0) |
| 2658 | return ret; |
| 2659 | } |
| 2660 | |
| 2661 | return 0; |
| 2662 | } |
| 2663 | |
| 2664 | static void sh_eth_tsu_purge_mcast(struct net_device *ndev) |
| 2665 | { |
| 2666 | struct sh_eth_private *mdp = netdev_priv(ndev); |
| 2667 | u8 addr[ETH_ALEN]; |
| 2668 | void *reg_offset = sh_eth_tsu_get_offset(mdp, TSU_ADRH0); |
| 2669 | int i; |
| 2670 | |
Ben Hutchings | b37feed | 2015-01-16 17:51:12 +0000 | [diff] [blame] | 2671 | if (!mdp->cd->tsu) |
Yoshihiro Shimoda | 6743fe6 | 2012-02-15 17:55:03 +0000 | [diff] [blame] | 2672 | return; |
| 2673 | |
| 2674 | for (i = 0; i < SH_ETH_TSU_CAM_ENTRIES; i++, reg_offset += 8) { |
| 2675 | sh_eth_tsu_read_entry(reg_offset, addr); |
| 2676 | if (is_multicast_ether_addr(addr)) |
| 2677 | sh_eth_tsu_del_entry(ndev, addr); |
| 2678 | } |
| 2679 | } |
| 2680 | |
Ben Hutchings | b37feed | 2015-01-16 17:51:12 +0000 | [diff] [blame] | 2681 | /* Update promiscuous flag and multicast filter */ |
| 2682 | static void sh_eth_set_rx_mode(struct net_device *ndev) |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 2683 | { |
Yoshihiro Shimoda | 6743fe6 | 2012-02-15 17:55:03 +0000 | [diff] [blame] | 2684 | struct sh_eth_private *mdp = netdev_priv(ndev); |
| 2685 | u32 ecmr_bits; |
| 2686 | int mcast_all = 0; |
| 2687 | unsigned long flags; |
| 2688 | |
| 2689 | spin_lock_irqsave(&mdp->lock, flags); |
Sergei Shtylyov | 128296f | 2014-01-03 15:52:22 +0300 | [diff] [blame] | 2690 | /* Initial condition is MCT = 1, PRM = 0. |
Yoshihiro Shimoda | 6743fe6 | 2012-02-15 17:55:03 +0000 | [diff] [blame] | 2691 | * Depending on ndev->flags, set PRM or clear MCT |
| 2692 | */ |
Ben Hutchings | b37feed | 2015-01-16 17:51:12 +0000 | [diff] [blame] | 2693 | ecmr_bits = sh_eth_read(ndev, ECMR) & ~ECMR_PRM; |
| 2694 | if (mdp->cd->tsu) |
| 2695 | ecmr_bits |= ECMR_MCT; |
Yoshihiro Shimoda | 6743fe6 | 2012-02-15 17:55:03 +0000 | [diff] [blame] | 2696 | |
| 2697 | if (!(ndev->flags & IFF_MULTICAST)) { |
| 2698 | sh_eth_tsu_purge_mcast(ndev); |
| 2699 | mcast_all = 1; |
| 2700 | } |
| 2701 | if (ndev->flags & IFF_ALLMULTI) { |
| 2702 | sh_eth_tsu_purge_mcast(ndev); |
| 2703 | ecmr_bits &= ~ECMR_MCT; |
| 2704 | mcast_all = 1; |
| 2705 | } |
| 2706 | |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 2707 | if (ndev->flags & IFF_PROMISC) { |
Yoshihiro Shimoda | 6743fe6 | 2012-02-15 17:55:03 +0000 | [diff] [blame] | 2708 | sh_eth_tsu_purge_all(ndev); |
| 2709 | ecmr_bits = (ecmr_bits & ~ECMR_MCT) | ECMR_PRM; |
| 2710 | } else if (mdp->cd->tsu) { |
| 2711 | struct netdev_hw_addr *ha; |
| 2712 | netdev_for_each_mc_addr(ha, ndev) { |
| 2713 | if (mcast_all && is_multicast_ether_addr(ha->addr)) |
| 2714 | continue; |
| 2715 | |
| 2716 | if (sh_eth_tsu_add_entry(ndev, ha->addr) < 0) { |
| 2717 | if (!mcast_all) { |
| 2718 | sh_eth_tsu_purge_mcast(ndev); |
| 2719 | ecmr_bits &= ~ECMR_MCT; |
| 2720 | mcast_all = 1; |
| 2721 | } |
| 2722 | } |
| 2723 | } |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 2724 | } |
Yoshihiro Shimoda | 6743fe6 | 2012-02-15 17:55:03 +0000 | [diff] [blame] | 2725 | |
| 2726 | /* update the ethernet mode */ |
| 2727 | sh_eth_write(ndev, ecmr_bits, ECMR); |
| 2728 | |
| 2729 | spin_unlock_irqrestore(&mdp->lock, flags); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 2730 | } |
Yoshihiro Shimoda | 71cc7c3 | 2012-02-15 17:55:06 +0000 | [diff] [blame] | 2731 | |
| 2732 | static int sh_eth_get_vtag_index(struct sh_eth_private *mdp) |
| 2733 | { |
| 2734 | if (!mdp->port) |
| 2735 | return TSU_VTAG0; |
| 2736 | else |
| 2737 | return TSU_VTAG1; |
| 2738 | } |
| 2739 | |
Patrick McHardy | 80d5c36 | 2013-04-19 02:04:28 +0000 | [diff] [blame] | 2740 | static int sh_eth_vlan_rx_add_vid(struct net_device *ndev, |
| 2741 | __be16 proto, u16 vid) |
Yoshihiro Shimoda | 71cc7c3 | 2012-02-15 17:55:06 +0000 | [diff] [blame] | 2742 | { |
| 2743 | struct sh_eth_private *mdp = netdev_priv(ndev); |
| 2744 | int vtag_reg_index = sh_eth_get_vtag_index(mdp); |
| 2745 | |
| 2746 | if (unlikely(!mdp->cd->tsu)) |
| 2747 | return -EPERM; |
| 2748 | |
| 2749 | /* No filtering if vid = 0 */ |
| 2750 | if (!vid) |
| 2751 | return 0; |
| 2752 | |
| 2753 | mdp->vlan_num_ids++; |
| 2754 | |
Sergei Shtylyov | 128296f | 2014-01-03 15:52:22 +0300 | [diff] [blame] | 2755 | /* The controller has one VLAN tag HW filter. So, if the filter is |
Yoshihiro Shimoda | 71cc7c3 | 2012-02-15 17:55:06 +0000 | [diff] [blame] | 2756 | * already enabled, the driver disables it and the filte |
| 2757 | */ |
| 2758 | if (mdp->vlan_num_ids > 1) { |
| 2759 | /* disable VLAN filter */ |
| 2760 | sh_eth_tsu_write(mdp, 0, vtag_reg_index); |
| 2761 | return 0; |
| 2762 | } |
| 2763 | |
| 2764 | sh_eth_tsu_write(mdp, TSU_VTAG_ENABLE | (vid & TSU_VTAG_VID_MASK), |
| 2765 | vtag_reg_index); |
| 2766 | |
| 2767 | return 0; |
| 2768 | } |
| 2769 | |
Patrick McHardy | 80d5c36 | 2013-04-19 02:04:28 +0000 | [diff] [blame] | 2770 | static int sh_eth_vlan_rx_kill_vid(struct net_device *ndev, |
| 2771 | __be16 proto, u16 vid) |
Yoshihiro Shimoda | 71cc7c3 | 2012-02-15 17:55:06 +0000 | [diff] [blame] | 2772 | { |
| 2773 | struct sh_eth_private *mdp = netdev_priv(ndev); |
| 2774 | int vtag_reg_index = sh_eth_get_vtag_index(mdp); |
| 2775 | |
| 2776 | if (unlikely(!mdp->cd->tsu)) |
| 2777 | return -EPERM; |
| 2778 | |
| 2779 | /* No filtering if vid = 0 */ |
| 2780 | if (!vid) |
| 2781 | return 0; |
| 2782 | |
| 2783 | mdp->vlan_num_ids--; |
| 2784 | sh_eth_tsu_write(mdp, 0, vtag_reg_index); |
| 2785 | |
| 2786 | return 0; |
| 2787 | } |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 2788 | |
| 2789 | /* SuperH's TSU register init function */ |
Yoshihiro Shimoda | 4a55530 | 2011-03-07 21:59:26 +0000 | [diff] [blame] | 2790 | static void sh_eth_tsu_init(struct sh_eth_private *mdp) |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 2791 | { |
Simon Horman | db89347 | 2014-01-17 09:22:28 +0900 | [diff] [blame] | 2792 | if (sh_eth_is_rz_fast_ether(mdp)) { |
| 2793 | sh_eth_tsu_write(mdp, 0, TSU_TEN); /* Disable all CAM entry */ |
| 2794 | return; |
| 2795 | } |
| 2796 | |
Yoshihiro Shimoda | 4a55530 | 2011-03-07 21:59:26 +0000 | [diff] [blame] | 2797 | sh_eth_tsu_write(mdp, 0, TSU_FWEN0); /* Disable forward(0->1) */ |
| 2798 | sh_eth_tsu_write(mdp, 0, TSU_FWEN1); /* Disable forward(1->0) */ |
| 2799 | sh_eth_tsu_write(mdp, 0, TSU_FCM); /* forward fifo 3k-3k */ |
| 2800 | sh_eth_tsu_write(mdp, 0xc, TSU_BSYSL0); |
| 2801 | sh_eth_tsu_write(mdp, 0xc, TSU_BSYSL1); |
| 2802 | sh_eth_tsu_write(mdp, 0, TSU_PRISL0); |
| 2803 | sh_eth_tsu_write(mdp, 0, TSU_PRISL1); |
| 2804 | sh_eth_tsu_write(mdp, 0, TSU_FWSL0); |
| 2805 | sh_eth_tsu_write(mdp, 0, TSU_FWSL1); |
| 2806 | sh_eth_tsu_write(mdp, TSU_FWSLC_POSTENU | TSU_FWSLC_POSTENL, TSU_FWSLC); |
Yoshihiro Shimoda | c5ed536 | 2011-03-07 21:59:38 +0000 | [diff] [blame] | 2807 | if (sh_eth_is_gether(mdp)) { |
| 2808 | sh_eth_tsu_write(mdp, 0, TSU_QTAG0); /* Disable QTAG(0->1) */ |
| 2809 | sh_eth_tsu_write(mdp, 0, TSU_QTAG1); /* Disable QTAG(1->0) */ |
| 2810 | } else { |
| 2811 | sh_eth_tsu_write(mdp, 0, TSU_QTAGM0); /* Disable QTAG(0->1) */ |
| 2812 | sh_eth_tsu_write(mdp, 0, TSU_QTAGM1); /* Disable QTAG(1->0) */ |
| 2813 | } |
Yoshihiro Shimoda | 4a55530 | 2011-03-07 21:59:26 +0000 | [diff] [blame] | 2814 | sh_eth_tsu_write(mdp, 0, TSU_FWSR); /* all interrupt status clear */ |
| 2815 | sh_eth_tsu_write(mdp, 0, TSU_FWINMK); /* Disable all interrupt */ |
| 2816 | sh_eth_tsu_write(mdp, 0, TSU_TEN); /* Disable all CAM entry */ |
| 2817 | sh_eth_tsu_write(mdp, 0, TSU_POST1); /* Disable CAM entry [ 0- 7] */ |
| 2818 | sh_eth_tsu_write(mdp, 0, TSU_POST2); /* Disable CAM entry [ 8-15] */ |
| 2819 | sh_eth_tsu_write(mdp, 0, TSU_POST3); /* Disable CAM entry [16-23] */ |
| 2820 | sh_eth_tsu_write(mdp, 0, TSU_POST4); /* Disable CAM entry [24-31] */ |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 2821 | } |
| 2822 | |
| 2823 | /* MDIO bus release function */ |
Laurent Pinchart | bd920ff | 2014-03-20 15:00:33 +0100 | [diff] [blame] | 2824 | static int sh_mdio_release(struct sh_eth_private *mdp) |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 2825 | { |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 2826 | /* unregister mdio bus */ |
Laurent Pinchart | bd920ff | 2014-03-20 15:00:33 +0100 | [diff] [blame] | 2827 | mdiobus_unregister(mdp->mii_bus); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 2828 | |
| 2829 | /* free bitbang info */ |
Laurent Pinchart | bd920ff | 2014-03-20 15:00:33 +0100 | [diff] [blame] | 2830 | free_mdio_bitbang(mdp->mii_bus); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 2831 | |
| 2832 | return 0; |
| 2833 | } |
| 2834 | |
| 2835 | /* MDIO bus init function */ |
Laurent Pinchart | bd920ff | 2014-03-20 15:00:33 +0100 | [diff] [blame] | 2836 | static int sh_mdio_init(struct sh_eth_private *mdp, |
Yoshihiro Shimoda | b3017e6 | 2011-03-07 21:59:55 +0000 | [diff] [blame] | 2837 | struct sh_eth_plat_data *pd) |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 2838 | { |
Andrew Lunn | e7f4dc3 | 2016-01-06 20:11:15 +0100 | [diff] [blame] | 2839 | int ret; |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 2840 | struct bb_info *bitbang; |
Laurent Pinchart | bd920ff | 2014-03-20 15:00:33 +0100 | [diff] [blame] | 2841 | struct platform_device *pdev = mdp->pdev; |
Laurent Pinchart | aa8d422 | 2014-03-20 15:00:31 +0100 | [diff] [blame] | 2842 | struct device *dev = &mdp->pdev->dev; |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 2843 | |
| 2844 | /* create bit control struct for PHY */ |
Laurent Pinchart | aa8d422 | 2014-03-20 15:00:31 +0100 | [diff] [blame] | 2845 | bitbang = devm_kzalloc(dev, sizeof(struct bb_info), GFP_KERNEL); |
Laurent Pinchart | f738a13 | 2014-03-20 15:00:35 +0100 | [diff] [blame] | 2846 | if (!bitbang) |
| 2847 | return -ENOMEM; |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 2848 | |
| 2849 | /* bitbang init */ |
Yoshihiro Shimoda | ae70644 | 2011-09-27 21:48:58 +0000 | [diff] [blame] | 2850 | bitbang->addr = mdp->addr + mdp->reg_offset[PIR]; |
Yoshihiro Shimoda | b3017e6 | 2011-03-07 21:59:55 +0000 | [diff] [blame] | 2851 | bitbang->set_gate = pd->set_mdio_gate; |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 2852 | bitbang->ctrl.ops = &bb_ops; |
| 2853 | |
Stefan Weil | c2e07b3 | 2010-08-03 19:44:52 +0200 | [diff] [blame] | 2854 | /* MII controller setting */ |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 2855 | mdp->mii_bus = alloc_mdio_bitbang(&bitbang->ctrl); |
Laurent Pinchart | f738a13 | 2014-03-20 15:00:35 +0100 | [diff] [blame] | 2856 | if (!mdp->mii_bus) |
| 2857 | return -ENOMEM; |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 2858 | |
| 2859 | /* Hook up MII support for ethtool */ |
| 2860 | mdp->mii_bus->name = "sh_mii"; |
Laurent Pinchart | a5bd6060 | 2014-03-20 15:00:32 +0100 | [diff] [blame] | 2861 | mdp->mii_bus->parent = dev; |
Florian Fainelli | 5278fb5 | 2012-01-09 23:59:17 +0000 | [diff] [blame] | 2862 | snprintf(mdp->mii_bus->id, MII_BUS_ID_SIZE, "%s-%x", |
Laurent Pinchart | bd920ff | 2014-03-20 15:00:33 +0100 | [diff] [blame] | 2863 | pdev->name, pdev->id); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 2864 | |
Laurent Pinchart | bd920ff | 2014-03-20 15:00:33 +0100 | [diff] [blame] | 2865 | /* register MDIO bus */ |
| 2866 | if (dev->of_node) { |
| 2867 | ret = of_mdiobus_register(mdp->mii_bus, dev->of_node); |
Ben Dooks | 702eca0 | 2014-03-12 17:47:40 +0000 | [diff] [blame] | 2868 | } else { |
Ben Dooks | 702eca0 | 2014-03-12 17:47:40 +0000 | [diff] [blame] | 2869 | if (pd->phy_irq > 0) |
| 2870 | mdp->mii_bus->irq[pd->phy] = pd->phy_irq; |
| 2871 | |
| 2872 | ret = mdiobus_register(mdp->mii_bus); |
| 2873 | } |
| 2874 | |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 2875 | if (ret) |
Sergei Shtylyov | d5e07e6 | 2013-03-21 10:41:11 +0000 | [diff] [blame] | 2876 | goto out_free_bus; |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 2877 | |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 2878 | return 0; |
| 2879 | |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 2880 | out_free_bus: |
Lennert Buytenhek | 298cf9b | 2008-10-08 16:29:57 -0700 | [diff] [blame] | 2881 | free_mdio_bitbang(mdp->mii_bus); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 2882 | return ret; |
| 2883 | } |
| 2884 | |
Yoshihiro Shimoda | 4a55530 | 2011-03-07 21:59:26 +0000 | [diff] [blame] | 2885 | static const u16 *sh_eth_get_register_offset(int register_type) |
| 2886 | { |
| 2887 | const u16 *reg_offset = NULL; |
| 2888 | |
| 2889 | switch (register_type) { |
| 2890 | case SH_ETH_REG_GIGABIT: |
| 2891 | reg_offset = sh_eth_offset_gigabit; |
| 2892 | break; |
Simon Horman | db89347 | 2014-01-17 09:22:28 +0900 | [diff] [blame] | 2893 | case SH_ETH_REG_FAST_RZ: |
| 2894 | reg_offset = sh_eth_offset_fast_rz; |
| 2895 | break; |
Sergei Shtylyov | a3f109b | 2013-03-28 11:51:31 +0000 | [diff] [blame] | 2896 | case SH_ETH_REG_FAST_RCAR: |
| 2897 | reg_offset = sh_eth_offset_fast_rcar; |
| 2898 | break; |
Yoshihiro Shimoda | 4a55530 | 2011-03-07 21:59:26 +0000 | [diff] [blame] | 2899 | case SH_ETH_REG_FAST_SH4: |
| 2900 | reg_offset = sh_eth_offset_fast_sh4; |
| 2901 | break; |
| 2902 | case SH_ETH_REG_FAST_SH3_SH2: |
| 2903 | reg_offset = sh_eth_offset_fast_sh3_sh2; |
| 2904 | break; |
Yoshihiro Shimoda | 4a55530 | 2011-03-07 21:59:26 +0000 | [diff] [blame] | 2905 | } |
| 2906 | |
| 2907 | return reg_offset; |
| 2908 | } |
| 2909 | |
Sergei Shtylyov | 8f728d7 | 2013-06-13 00:55:34 +0400 | [diff] [blame] | 2910 | static const struct net_device_ops sh_eth_netdev_ops = { |
Alexander Beregalov | ebf84ea | 2009-04-11 07:40:49 +0000 | [diff] [blame] | 2911 | .ndo_open = sh_eth_open, |
| 2912 | .ndo_stop = sh_eth_close, |
| 2913 | .ndo_start_xmit = sh_eth_start_xmit, |
| 2914 | .ndo_get_stats = sh_eth_get_stats, |
Ben Hutchings | b37feed | 2015-01-16 17:51:12 +0000 | [diff] [blame] | 2915 | .ndo_set_rx_mode = sh_eth_set_rx_mode, |
Alexander Beregalov | ebf84ea | 2009-04-11 07:40:49 +0000 | [diff] [blame] | 2916 | .ndo_tx_timeout = sh_eth_tx_timeout, |
| 2917 | .ndo_do_ioctl = sh_eth_do_ioctl, |
| 2918 | .ndo_validate_addr = eth_validate_addr, |
| 2919 | .ndo_set_mac_address = eth_mac_addr, |
| 2920 | .ndo_change_mtu = eth_change_mtu, |
| 2921 | }; |
| 2922 | |
Sergei Shtylyov | 8f728d7 | 2013-06-13 00:55:34 +0400 | [diff] [blame] | 2923 | static const struct net_device_ops sh_eth_netdev_ops_tsu = { |
| 2924 | .ndo_open = sh_eth_open, |
| 2925 | .ndo_stop = sh_eth_close, |
| 2926 | .ndo_start_xmit = sh_eth_start_xmit, |
| 2927 | .ndo_get_stats = sh_eth_get_stats, |
Ben Hutchings | b37feed | 2015-01-16 17:51:12 +0000 | [diff] [blame] | 2928 | .ndo_set_rx_mode = sh_eth_set_rx_mode, |
Sergei Shtylyov | 8f728d7 | 2013-06-13 00:55:34 +0400 | [diff] [blame] | 2929 | .ndo_vlan_rx_add_vid = sh_eth_vlan_rx_add_vid, |
| 2930 | .ndo_vlan_rx_kill_vid = sh_eth_vlan_rx_kill_vid, |
| 2931 | .ndo_tx_timeout = sh_eth_tx_timeout, |
| 2932 | .ndo_do_ioctl = sh_eth_do_ioctl, |
| 2933 | .ndo_validate_addr = eth_validate_addr, |
| 2934 | .ndo_set_mac_address = eth_mac_addr, |
| 2935 | .ndo_change_mtu = eth_change_mtu, |
| 2936 | }; |
| 2937 | |
Sergei Shtylyov | b356e97 | 2014-02-18 03:12:43 +0300 | [diff] [blame] | 2938 | #ifdef CONFIG_OF |
| 2939 | static struct sh_eth_plat_data *sh_eth_parse_dt(struct device *dev) |
| 2940 | { |
| 2941 | struct device_node *np = dev->of_node; |
| 2942 | struct sh_eth_plat_data *pdata; |
Sergei Shtylyov | b356e97 | 2014-02-18 03:12:43 +0300 | [diff] [blame] | 2943 | const char *mac_addr; |
| 2944 | |
| 2945 | pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL); |
| 2946 | if (!pdata) |
| 2947 | return NULL; |
| 2948 | |
| 2949 | pdata->phy_interface = of_get_phy_mode(np); |
| 2950 | |
Sergei Shtylyov | b356e97 | 2014-02-18 03:12:43 +0300 | [diff] [blame] | 2951 | mac_addr = of_get_mac_address(np); |
| 2952 | if (mac_addr) |
| 2953 | memcpy(pdata->mac_addr, mac_addr, ETH_ALEN); |
| 2954 | |
| 2955 | pdata->no_ether_link = |
| 2956 | of_property_read_bool(np, "renesas,no-ether-link"); |
| 2957 | pdata->ether_link_active_low = |
| 2958 | of_property_read_bool(np, "renesas,ether-link-active-low"); |
| 2959 | |
| 2960 | return pdata; |
| 2961 | } |
| 2962 | |
| 2963 | static const struct of_device_id sh_eth_match_table[] = { |
| 2964 | { .compatible = "renesas,gether-r8a7740", .data = &r8a7740_data }, |
| 2965 | { .compatible = "renesas,ether-r8a7778", .data = &r8a777x_data }, |
| 2966 | { .compatible = "renesas,ether-r8a7779", .data = &r8a777x_data }, |
| 2967 | { .compatible = "renesas,ether-r8a7790", .data = &r8a779x_data }, |
| 2968 | { .compatible = "renesas,ether-r8a7791", .data = &r8a779x_data }, |
Hisashi Nakamura | 9488e1e | 2014-11-13 15:59:07 +0900 | [diff] [blame] | 2969 | { .compatible = "renesas,ether-r8a7793", .data = &r8a779x_data }, |
Hisashi Nakamura | 0f76b9d | 2014-08-01 17:03:00 +0200 | [diff] [blame] | 2970 | { .compatible = "renesas,ether-r8a7794", .data = &r8a779x_data }, |
Sergei Shtylyov | b356e97 | 2014-02-18 03:12:43 +0300 | [diff] [blame] | 2971 | { .compatible = "renesas,ether-r7s72100", .data = &r7s72100_data }, |
| 2972 | { } |
| 2973 | }; |
| 2974 | MODULE_DEVICE_TABLE(of, sh_eth_match_table); |
| 2975 | #else |
| 2976 | static inline struct sh_eth_plat_data *sh_eth_parse_dt(struct device *dev) |
| 2977 | { |
| 2978 | return NULL; |
| 2979 | } |
| 2980 | #endif |
| 2981 | |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 2982 | static int sh_eth_drv_probe(struct platform_device *pdev) |
| 2983 | { |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 2984 | struct resource *res; |
Jingoo Han | 0b76b86 | 2013-08-30 14:00:11 +0900 | [diff] [blame] | 2985 | struct sh_eth_plat_data *pd = dev_get_platdata(&pdev->dev); |
Sergei Shtylyov | afe391a | 2013-06-07 13:54:02 +0000 | [diff] [blame] | 2986 | const struct platform_device_id *id = platform_get_device_id(pdev); |
Sergei Shtylyov | 4fa8c3c | 2016-03-13 01:29:45 +0300 | [diff] [blame] | 2987 | struct sh_eth_private *mdp; |
| 2988 | struct net_device *ndev; |
| 2989 | int ret, devno; |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 2990 | |
| 2991 | /* get base addr */ |
| 2992 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 2993 | |
| 2994 | ndev = alloc_etherdev(sizeof(struct sh_eth_private)); |
Laurent Pinchart | f738a13 | 2014-03-20 15:00:35 +0100 | [diff] [blame] | 2995 | if (!ndev) |
| 2996 | return -ENOMEM; |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 2997 | |
Ben Dooks | b5893a0 | 2014-03-21 12:09:14 +0100 | [diff] [blame] | 2998 | pm_runtime_enable(&pdev->dev); |
| 2999 | pm_runtime_get_sync(&pdev->dev); |
| 3000 | |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 3001 | devno = pdev->id; |
| 3002 | if (devno < 0) |
| 3003 | devno = 0; |
| 3004 | |
| 3005 | ndev->dma = -1; |
roel kluin | cc3c080 | 2008-09-10 19:22:44 +0200 | [diff] [blame] | 3006 | ret = platform_get_irq(pdev, 0); |
Sergei Shtylyov | 7a468ac | 2015-08-28 16:56:01 +0300 | [diff] [blame] | 3007 | if (ret < 0) |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 3008 | goto out_release; |
roel kluin | cc3c080 | 2008-09-10 19:22:44 +0200 | [diff] [blame] | 3009 | ndev->irq = ret; |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 3010 | |
| 3011 | SET_NETDEV_DEV(ndev, &pdev->dev); |
| 3012 | |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 3013 | mdp = netdev_priv(ndev); |
Yoshihiro Shimoda | 525b807 | 2012-06-26 20:00:03 +0000 | [diff] [blame] | 3014 | mdp->num_tx_ring = TX_RING_SIZE; |
| 3015 | mdp->num_rx_ring = RX_RING_SIZE; |
Sergei Shtylyov | d5e07e6 | 2013-03-21 10:41:11 +0000 | [diff] [blame] | 3016 | mdp->addr = devm_ioremap_resource(&pdev->dev, res); |
| 3017 | if (IS_ERR(mdp->addr)) { |
| 3018 | ret = PTR_ERR(mdp->addr); |
Yoshihiro Shimoda | ae70644 | 2011-09-27 21:48:58 +0000 | [diff] [blame] | 3019 | goto out_release; |
| 3020 | } |
| 3021 | |
Varka Bhadram | c960804 | 2014-10-24 07:42:09 +0530 | [diff] [blame] | 3022 | ndev->base_addr = res->start; |
| 3023 | |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 3024 | spin_lock_init(&mdp->lock); |
Magnus Damm | bcd5149 | 2009-10-09 00:20:04 +0000 | [diff] [blame] | 3025 | mdp->pdev = pdev; |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 3026 | |
Sergei Shtylyov | b356e97 | 2014-02-18 03:12:43 +0300 | [diff] [blame] | 3027 | if (pdev->dev.of_node) |
| 3028 | pd = sh_eth_parse_dt(&pdev->dev); |
Sergei Shtylyov | 3b4c5cb | 2013-10-30 23:30:19 +0300 | [diff] [blame] | 3029 | if (!pd) { |
| 3030 | dev_err(&pdev->dev, "no platform data\n"); |
| 3031 | ret = -EINVAL; |
| 3032 | goto out_release; |
| 3033 | } |
| 3034 | |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 3035 | /* get PHY ID */ |
Yoshinori Sato | 71557a3 | 2008-08-06 19:49:00 -0400 | [diff] [blame] | 3036 | mdp->phy_id = pd->phy; |
Yoshihiro Shimoda | e47c905 | 2011-03-07 21:59:45 +0000 | [diff] [blame] | 3037 | mdp->phy_interface = pd->phy_interface; |
Yoshihiro Shimoda | 4923576 | 2009-08-27 23:25:03 +0000 | [diff] [blame] | 3038 | mdp->no_ether_link = pd->no_ether_link; |
| 3039 | mdp->ether_link_active_low = pd->ether_link_active_low; |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 3040 | |
Yoshihiro Shimoda | 380af9e | 2009-05-24 23:54:21 +0000 | [diff] [blame] | 3041 | /* set cpu data */ |
Wolfram Sang | 42a67c9 | 2016-03-01 17:37:59 +0100 | [diff] [blame] | 3042 | if (id) |
Sergei Shtylyov | b356e97 | 2014-02-18 03:12:43 +0300 | [diff] [blame] | 3043 | mdp->cd = (struct sh_eth_cpu_data *)id->driver_data; |
Wolfram Sang | 42a67c9 | 2016-03-01 17:37:59 +0100 | [diff] [blame] | 3044 | else |
| 3045 | mdp->cd = (struct sh_eth_cpu_data *)of_device_get_match_data(&pdev->dev); |
Sergei Shtylyov | b356e97 | 2014-02-18 03:12:43 +0300 | [diff] [blame] | 3046 | |
Sergei Shtylyov | a3153d8 | 2013-08-18 03:11:28 +0400 | [diff] [blame] | 3047 | mdp->reg_offset = sh_eth_get_register_offset(mdp->cd->register_type); |
Sergei Shtylyov | 264be2f | 2014-03-15 03:11:24 +0300 | [diff] [blame] | 3048 | if (!mdp->reg_offset) { |
| 3049 | dev_err(&pdev->dev, "Unknown register type (%d)\n", |
| 3050 | mdp->cd->register_type); |
| 3051 | ret = -EINVAL; |
| 3052 | goto out_release; |
| 3053 | } |
Yoshihiro Shimoda | 380af9e | 2009-05-24 23:54:21 +0000 | [diff] [blame] | 3054 | sh_eth_set_default_cpu_data(mdp->cd); |
| 3055 | |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 3056 | /* set function */ |
Sergei Shtylyov | 8f728d7 | 2013-06-13 00:55:34 +0400 | [diff] [blame] | 3057 | if (mdp->cd->tsu) |
| 3058 | ndev->netdev_ops = &sh_eth_netdev_ops_tsu; |
| 3059 | else |
| 3060 | ndev->netdev_ops = &sh_eth_netdev_ops; |
Wilfried Klaebe | 7ad24ea | 2014-05-11 00:12:32 +0000 | [diff] [blame] | 3061 | ndev->ethtool_ops = &sh_eth_ethtool_ops; |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 3062 | ndev->watchdog_timeo = TX_TIMEOUT; |
| 3063 | |
Nobuhiro Iwamatsu | dc19e4e | 2011-02-15 21:17:32 +0000 | [diff] [blame] | 3064 | /* debug message level */ |
| 3065 | mdp->msg_enable = SH_ETH_DEF_MSG_ENABLE; |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 3066 | |
| 3067 | /* read and set MAC address */ |
Magnus Damm | 748031f | 2009-10-09 00:17:14 +0000 | [diff] [blame] | 3068 | read_mac_address(ndev, pd->mac_addr); |
Sergei Shtylyov | ff6e722 | 2013-04-29 09:49:42 +0000 | [diff] [blame] | 3069 | if (!is_valid_ether_addr(ndev->dev_addr)) { |
| 3070 | dev_warn(&pdev->dev, |
| 3071 | "no valid MAC address supplied, using a random one.\n"); |
| 3072 | eth_hw_addr_random(ndev); |
| 3073 | } |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 3074 | |
Yoshihiro Shimoda | 6ba8802 | 2012-02-15 17:55:01 +0000 | [diff] [blame] | 3075 | /* ioremap the TSU registers */ |
| 3076 | if (mdp->cd->tsu) { |
| 3077 | struct resource *rtsu; |
| 3078 | rtsu = platform_get_resource(pdev, IORESOURCE_MEM, 1); |
Sergei Shtylyov | d5e07e6 | 2013-03-21 10:41:11 +0000 | [diff] [blame] | 3079 | mdp->tsu_addr = devm_ioremap_resource(&pdev->dev, rtsu); |
| 3080 | if (IS_ERR(mdp->tsu_addr)) { |
| 3081 | ret = PTR_ERR(mdp->tsu_addr); |
Sergei Shtylyov | fc0c090 | 2013-03-19 13:41:32 +0000 | [diff] [blame] | 3082 | goto out_release; |
| 3083 | } |
Yoshihiro Shimoda | 6743fe6 | 2012-02-15 17:55:03 +0000 | [diff] [blame] | 3084 | mdp->port = devno % 2; |
Patrick McHardy | f646968 | 2013-04-19 02:04:27 +0000 | [diff] [blame] | 3085 | ndev->features = NETIF_F_HW_VLAN_CTAG_FILTER; |
Yoshihiro Shimoda | 6ba8802 | 2012-02-15 17:55:01 +0000 | [diff] [blame] | 3086 | } |
| 3087 | |
Yoshihiro Shimoda | 150647f | 2012-02-15 17:54:56 +0000 | [diff] [blame] | 3088 | /* initialize first or needed device */ |
| 3089 | if (!devno || pd->needs_init) { |
Yoshihiro Shimoda | 380af9e | 2009-05-24 23:54:21 +0000 | [diff] [blame] | 3090 | if (mdp->cd->chip_reset) |
| 3091 | mdp->cd->chip_reset(ndev); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 3092 | |
Yoshihiro Shimoda | 4986b99 | 2011-03-07 21:59:34 +0000 | [diff] [blame] | 3093 | if (mdp->cd->tsu) { |
| 3094 | /* TSU init (Init only)*/ |
| 3095 | sh_eth_tsu_init(mdp); |
| 3096 | } |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 3097 | } |
| 3098 | |
Hisashi Nakamura | 966d6db | 2014-11-13 15:54:05 +0900 | [diff] [blame] | 3099 | if (mdp->cd->rmiimode) |
| 3100 | sh_eth_write(ndev, 0x1, RMIIMODE); |
| 3101 | |
Laurent Pinchart | daacf03 | 2014-03-20 15:00:34 +0100 | [diff] [blame] | 3102 | /* MDIO bus init */ |
| 3103 | ret = sh_mdio_init(mdp, pd); |
| 3104 | if (ret) { |
| 3105 | dev_err(&ndev->dev, "failed to initialise MDIO\n"); |
| 3106 | goto out_release; |
| 3107 | } |
| 3108 | |
Sergei Shtylyov | 3719109 | 2013-06-19 23:30:23 +0400 | [diff] [blame] | 3109 | netif_napi_add(ndev, &mdp->napi, sh_eth_poll, 64); |
| 3110 | |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 3111 | /* network device register */ |
| 3112 | ret = register_netdev(ndev); |
| 3113 | if (ret) |
Sergei Shtylyov | 3719109 | 2013-06-19 23:30:23 +0400 | [diff] [blame] | 3114 | goto out_napi_del; |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 3115 | |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 3116 | /* print device information */ |
Sergei Shtylyov | f75f14e | 2014-03-15 03:27:54 +0300 | [diff] [blame] | 3117 | netdev_info(ndev, "Base address at 0x%x, %pM, IRQ %d.\n", |
| 3118 | (u32)ndev->base_addr, ndev->dev_addr, ndev->irq); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 3119 | |
Ben Dooks | b5893a0 | 2014-03-21 12:09:14 +0100 | [diff] [blame] | 3120 | pm_runtime_put(&pdev->dev); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 3121 | platform_set_drvdata(pdev, ndev); |
| 3122 | |
| 3123 | return ret; |
| 3124 | |
Sergei Shtylyov | 3719109 | 2013-06-19 23:30:23 +0400 | [diff] [blame] | 3125 | out_napi_del: |
| 3126 | netif_napi_del(&mdp->napi); |
Laurent Pinchart | daacf03 | 2014-03-20 15:00:34 +0100 | [diff] [blame] | 3127 | sh_mdio_release(mdp); |
Sergei Shtylyov | 3719109 | 2013-06-19 23:30:23 +0400 | [diff] [blame] | 3128 | |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 3129 | out_release: |
| 3130 | /* net_dev free */ |
| 3131 | if (ndev) |
| 3132 | free_netdev(ndev); |
| 3133 | |
Ben Dooks | b5893a0 | 2014-03-21 12:09:14 +0100 | [diff] [blame] | 3134 | pm_runtime_put(&pdev->dev); |
| 3135 | pm_runtime_disable(&pdev->dev); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 3136 | return ret; |
| 3137 | } |
| 3138 | |
| 3139 | static int sh_eth_drv_remove(struct platform_device *pdev) |
| 3140 | { |
| 3141 | struct net_device *ndev = platform_get_drvdata(pdev); |
Sergei Shtylyov | 3719109 | 2013-06-19 23:30:23 +0400 | [diff] [blame] | 3142 | struct sh_eth_private *mdp = netdev_priv(ndev); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 3143 | |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 3144 | unregister_netdev(ndev); |
Sergei Shtylyov | 3719109 | 2013-06-19 23:30:23 +0400 | [diff] [blame] | 3145 | netif_napi_del(&mdp->napi); |
Laurent Pinchart | daacf03 | 2014-03-20 15:00:34 +0100 | [diff] [blame] | 3146 | sh_mdio_release(mdp); |
Magnus Damm | bcd5149 | 2009-10-09 00:20:04 +0000 | [diff] [blame] | 3147 | pm_runtime_disable(&pdev->dev); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 3148 | free_netdev(ndev); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 3149 | |
| 3150 | return 0; |
| 3151 | } |
| 3152 | |
Nobuhiro Iwamatsu | 540ad1b | 2013-06-06 09:52:37 +0000 | [diff] [blame] | 3153 | #ifdef CONFIG_PM |
Mikhail Ulyanov | b71af04 | 2015-01-22 01:19:48 +0300 | [diff] [blame] | 3154 | #ifdef CONFIG_PM_SLEEP |
| 3155 | static int sh_eth_suspend(struct device *dev) |
| 3156 | { |
| 3157 | struct net_device *ndev = dev_get_drvdata(dev); |
| 3158 | int ret = 0; |
| 3159 | |
| 3160 | if (netif_running(ndev)) { |
| 3161 | netif_device_detach(ndev); |
| 3162 | ret = sh_eth_close(ndev); |
| 3163 | } |
| 3164 | |
| 3165 | return ret; |
| 3166 | } |
| 3167 | |
| 3168 | static int sh_eth_resume(struct device *dev) |
| 3169 | { |
| 3170 | struct net_device *ndev = dev_get_drvdata(dev); |
| 3171 | int ret = 0; |
| 3172 | |
| 3173 | if (netif_running(ndev)) { |
| 3174 | ret = sh_eth_open(ndev); |
| 3175 | if (ret < 0) |
| 3176 | return ret; |
| 3177 | netif_device_attach(ndev); |
| 3178 | } |
| 3179 | |
| 3180 | return ret; |
| 3181 | } |
| 3182 | #endif |
| 3183 | |
Magnus Damm | bcd5149 | 2009-10-09 00:20:04 +0000 | [diff] [blame] | 3184 | static int sh_eth_runtime_nop(struct device *dev) |
| 3185 | { |
Sergei Shtylyov | 128296f | 2014-01-03 15:52:22 +0300 | [diff] [blame] | 3186 | /* Runtime PM callback shared between ->runtime_suspend() |
Magnus Damm | bcd5149 | 2009-10-09 00:20:04 +0000 | [diff] [blame] | 3187 | * and ->runtime_resume(). Simply returns success. |
| 3188 | * |
| 3189 | * This driver re-initializes all registers after |
| 3190 | * pm_runtime_get_sync() anyway so there is no need |
| 3191 | * to save and restore registers here. |
| 3192 | */ |
| 3193 | return 0; |
| 3194 | } |
| 3195 | |
Nobuhiro Iwamatsu | 540ad1b | 2013-06-06 09:52:37 +0000 | [diff] [blame] | 3196 | static const struct dev_pm_ops sh_eth_dev_pm_ops = { |
Mikhail Ulyanov | b71af04 | 2015-01-22 01:19:48 +0300 | [diff] [blame] | 3197 | SET_SYSTEM_SLEEP_PM_OPS(sh_eth_suspend, sh_eth_resume) |
Mikhail Ulyanov | e7d7e89 | 2015-01-22 01:18:44 +0300 | [diff] [blame] | 3198 | SET_RUNTIME_PM_OPS(sh_eth_runtime_nop, sh_eth_runtime_nop, NULL) |
Magnus Damm | bcd5149 | 2009-10-09 00:20:04 +0000 | [diff] [blame] | 3199 | }; |
Nobuhiro Iwamatsu | 540ad1b | 2013-06-06 09:52:37 +0000 | [diff] [blame] | 3200 | #define SH_ETH_PM_OPS (&sh_eth_dev_pm_ops) |
| 3201 | #else |
| 3202 | #define SH_ETH_PM_OPS NULL |
| 3203 | #endif |
Magnus Damm | bcd5149 | 2009-10-09 00:20:04 +0000 | [diff] [blame] | 3204 | |
Sergei Shtylyov | afe391a | 2013-06-07 13:54:02 +0000 | [diff] [blame] | 3205 | static struct platform_device_id sh_eth_id_table[] = { |
Sergei Shtylyov | c18a79a | 2013-06-07 13:56:05 +0000 | [diff] [blame] | 3206 | { "sh7619-ether", (kernel_ulong_t)&sh7619_data }, |
Sergei Shtylyov | 7bbe150 | 2013-06-07 13:55:08 +0000 | [diff] [blame] | 3207 | { "sh771x-ether", (kernel_ulong_t)&sh771x_data }, |
Sergei Shtylyov | 9c3beaa | 2013-06-07 14:03:37 +0000 | [diff] [blame] | 3208 | { "sh7724-ether", (kernel_ulong_t)&sh7724_data }, |
Sergei Shtylyov | f5d1276 | 2013-06-07 13:58:18 +0000 | [diff] [blame] | 3209 | { "sh7734-gether", (kernel_ulong_t)&sh7734_data }, |
Sergei Shtylyov | 24549e2 | 2013-06-07 13:59:21 +0000 | [diff] [blame] | 3210 | { "sh7757-ether", (kernel_ulong_t)&sh7757_data }, |
| 3211 | { "sh7757-gether", (kernel_ulong_t)&sh7757_data_giga }, |
Sergei Shtylyov | f5d1276 | 2013-06-07 13:58:18 +0000 | [diff] [blame] | 3212 | { "sh7763-gether", (kernel_ulong_t)&sh7763_data }, |
Sergei Shtylyov | afe391a | 2013-06-07 13:54:02 +0000 | [diff] [blame] | 3213 | { } |
| 3214 | }; |
| 3215 | MODULE_DEVICE_TABLE(platform, sh_eth_id_table); |
| 3216 | |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 3217 | static struct platform_driver sh_eth_driver = { |
| 3218 | .probe = sh_eth_drv_probe, |
| 3219 | .remove = sh_eth_drv_remove, |
Sergei Shtylyov | afe391a | 2013-06-07 13:54:02 +0000 | [diff] [blame] | 3220 | .id_table = sh_eth_id_table, |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 3221 | .driver = { |
| 3222 | .name = CARDNAME, |
Nobuhiro Iwamatsu | 540ad1b | 2013-06-06 09:52:37 +0000 | [diff] [blame] | 3223 | .pm = SH_ETH_PM_OPS, |
Sergei Shtylyov | b356e97 | 2014-02-18 03:12:43 +0300 | [diff] [blame] | 3224 | .of_match_table = of_match_ptr(sh_eth_match_table), |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 3225 | }, |
| 3226 | }; |
| 3227 | |
Axel Lin | db62f68 | 2011-11-27 16:44:17 +0000 | [diff] [blame] | 3228 | module_platform_driver(sh_eth_driver); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 3229 | |
| 3230 | MODULE_AUTHOR("Nobuhiro Iwamatsu, Yoshihiro Shimoda"); |
| 3231 | MODULE_DESCRIPTION("Renesas SuperH Ethernet driver"); |
| 3232 | MODULE_LICENSE("GPL v2"); |