Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1 | /* |
| 2 | * SuperH Ethernet device driver |
| 3 | * |
Nobuhiro Iwamatsu | f0e81fe | 2012-03-25 18:59:51 +0000 | [diff] [blame] | 4 | * Copyright (C) 2006-2012 Nobuhiro Iwamatsu |
Sergei Shtylyov | a3f109b | 2013-03-28 11:51:31 +0000 | [diff] [blame] | 5 | * Copyright (C) 2008-2013 Renesas Solutions Corp. |
| 6 | * Copyright (C) 2013 Cogent Embedded, Inc. |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify it |
| 9 | * under the terms and conditions of the GNU General Public License, |
| 10 | * version 2, as published by the Free Software Foundation. |
| 11 | * |
| 12 | * This program is distributed in the hope it will be useful, but WITHOUT |
| 13 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 14 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
| 15 | * more details. |
| 16 | * You should have received a copy of the GNU General Public License along with |
| 17 | * this program; if not, write to the Free Software Foundation, Inc., |
| 18 | * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA. |
| 19 | * |
| 20 | * The full GNU General Public License is included in this distribution in |
| 21 | * the file called "COPYING". |
| 22 | */ |
| 23 | |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 24 | #include <linux/init.h> |
Yoshihiro Shimoda | 0654011 | 2011-09-29 17:16:57 +0000 | [diff] [blame] | 25 | #include <linux/module.h> |
| 26 | #include <linux/kernel.h> |
| 27 | #include <linux/spinlock.h> |
David S. Miller | 823dcd2 | 2011-08-20 10:39:12 -0700 | [diff] [blame] | 28 | #include <linux/interrupt.h> |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 29 | #include <linux/dma-mapping.h> |
| 30 | #include <linux/etherdevice.h> |
| 31 | #include <linux/delay.h> |
| 32 | #include <linux/platform_device.h> |
| 33 | #include <linux/mdio-bitbang.h> |
| 34 | #include <linux/netdevice.h> |
| 35 | #include <linux/phy.h> |
| 36 | #include <linux/cache.h> |
| 37 | #include <linux/io.h> |
Magnus Damm | bcd5149 | 2009-10-09 00:20:04 +0000 | [diff] [blame] | 38 | #include <linux/pm_runtime.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 39 | #include <linux/slab.h> |
Nobuhiro Iwamatsu | dc19e4e | 2011-02-15 21:17:32 +0000 | [diff] [blame] | 40 | #include <linux/ethtool.h> |
Yoshihiro Shimoda | fdb37a7 | 2012-02-06 23:55:15 +0000 | [diff] [blame] | 41 | #include <linux/if_vlan.h> |
Nobuhiro Iwamatsu | f0e81fe | 2012-03-25 18:59:51 +0000 | [diff] [blame] | 42 | #include <linux/clk.h> |
Yoshihiro Shimoda | d4fa0e3 | 2011-09-27 21:49:12 +0000 | [diff] [blame] | 43 | #include <linux/sh_eth.h> |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 44 | |
| 45 | #include "sh_eth.h" |
| 46 | |
Nobuhiro Iwamatsu | dc19e4e | 2011-02-15 21:17:32 +0000 | [diff] [blame] | 47 | #define SH_ETH_DEF_MSG_ENABLE \ |
| 48 | (NETIF_MSG_LINK | \ |
| 49 | NETIF_MSG_TIMER | \ |
| 50 | NETIF_MSG_RX_ERR| \ |
| 51 | NETIF_MSG_TX_ERR) |
| 52 | |
Sergei Shtylyov | c0013f6 | 2013-03-28 11:48:26 +0000 | [diff] [blame] | 53 | static const u16 sh_eth_offset_gigabit[SH_ETH_MAX_REGISTER_OFFSET] = { |
| 54 | [EDSR] = 0x0000, |
| 55 | [EDMR] = 0x0400, |
| 56 | [EDTRR] = 0x0408, |
| 57 | [EDRRR] = 0x0410, |
| 58 | [EESR] = 0x0428, |
| 59 | [EESIPR] = 0x0430, |
| 60 | [TDLAR] = 0x0010, |
| 61 | [TDFAR] = 0x0014, |
| 62 | [TDFXR] = 0x0018, |
| 63 | [TDFFR] = 0x001c, |
| 64 | [RDLAR] = 0x0030, |
| 65 | [RDFAR] = 0x0034, |
| 66 | [RDFXR] = 0x0038, |
| 67 | [RDFFR] = 0x003c, |
| 68 | [TRSCER] = 0x0438, |
| 69 | [RMFCR] = 0x0440, |
| 70 | [TFTR] = 0x0448, |
| 71 | [FDR] = 0x0450, |
| 72 | [RMCR] = 0x0458, |
| 73 | [RPADIR] = 0x0460, |
| 74 | [FCFTR] = 0x0468, |
| 75 | [CSMR] = 0x04E4, |
| 76 | |
| 77 | [ECMR] = 0x0500, |
| 78 | [ECSR] = 0x0510, |
| 79 | [ECSIPR] = 0x0518, |
| 80 | [PIR] = 0x0520, |
| 81 | [PSR] = 0x0528, |
| 82 | [PIPR] = 0x052c, |
| 83 | [RFLR] = 0x0508, |
| 84 | [APR] = 0x0554, |
| 85 | [MPR] = 0x0558, |
| 86 | [PFTCR] = 0x055c, |
| 87 | [PFRCR] = 0x0560, |
| 88 | [TPAUSER] = 0x0564, |
| 89 | [GECMR] = 0x05b0, |
| 90 | [BCULR] = 0x05b4, |
| 91 | [MAHR] = 0x05c0, |
| 92 | [MALR] = 0x05c8, |
| 93 | [TROCR] = 0x0700, |
| 94 | [CDCR] = 0x0708, |
| 95 | [LCCR] = 0x0710, |
| 96 | [CEFCR] = 0x0740, |
| 97 | [FRECR] = 0x0748, |
| 98 | [TSFRCR] = 0x0750, |
| 99 | [TLFRCR] = 0x0758, |
| 100 | [RFCR] = 0x0760, |
| 101 | [CERCR] = 0x0768, |
| 102 | [CEECR] = 0x0770, |
| 103 | [MAFCR] = 0x0778, |
| 104 | [RMII_MII] = 0x0790, |
| 105 | |
| 106 | [ARSTR] = 0x0000, |
| 107 | [TSU_CTRST] = 0x0004, |
| 108 | [TSU_FWEN0] = 0x0010, |
| 109 | [TSU_FWEN1] = 0x0014, |
| 110 | [TSU_FCM] = 0x0018, |
| 111 | [TSU_BSYSL0] = 0x0020, |
| 112 | [TSU_BSYSL1] = 0x0024, |
| 113 | [TSU_PRISL0] = 0x0028, |
| 114 | [TSU_PRISL1] = 0x002c, |
| 115 | [TSU_FWSL0] = 0x0030, |
| 116 | [TSU_FWSL1] = 0x0034, |
| 117 | [TSU_FWSLC] = 0x0038, |
| 118 | [TSU_QTAG0] = 0x0040, |
| 119 | [TSU_QTAG1] = 0x0044, |
| 120 | [TSU_FWSR] = 0x0050, |
| 121 | [TSU_FWINMK] = 0x0054, |
| 122 | [TSU_ADQT0] = 0x0048, |
| 123 | [TSU_ADQT1] = 0x004c, |
| 124 | [TSU_VTAG0] = 0x0058, |
| 125 | [TSU_VTAG1] = 0x005c, |
| 126 | [TSU_ADSBSY] = 0x0060, |
| 127 | [TSU_TEN] = 0x0064, |
| 128 | [TSU_POST1] = 0x0070, |
| 129 | [TSU_POST2] = 0x0074, |
| 130 | [TSU_POST3] = 0x0078, |
| 131 | [TSU_POST4] = 0x007c, |
| 132 | [TSU_ADRH0] = 0x0100, |
| 133 | [TSU_ADRL0] = 0x0104, |
| 134 | [TSU_ADRH31] = 0x01f8, |
| 135 | [TSU_ADRL31] = 0x01fc, |
| 136 | |
| 137 | [TXNLCR0] = 0x0080, |
| 138 | [TXALCR0] = 0x0084, |
| 139 | [RXNLCR0] = 0x0088, |
| 140 | [RXALCR0] = 0x008c, |
| 141 | [FWNLCR0] = 0x0090, |
| 142 | [FWALCR0] = 0x0094, |
| 143 | [TXNLCR1] = 0x00a0, |
| 144 | [TXALCR1] = 0x00a0, |
| 145 | [RXNLCR1] = 0x00a8, |
| 146 | [RXALCR1] = 0x00ac, |
| 147 | [FWNLCR1] = 0x00b0, |
| 148 | [FWALCR1] = 0x00b4, |
| 149 | }; |
| 150 | |
Sergei Shtylyov | a3f109b | 2013-03-28 11:51:31 +0000 | [diff] [blame] | 151 | static const u16 sh_eth_offset_fast_rcar[SH_ETH_MAX_REGISTER_OFFSET] = { |
| 152 | [ECMR] = 0x0300, |
| 153 | [RFLR] = 0x0308, |
| 154 | [ECSR] = 0x0310, |
| 155 | [ECSIPR] = 0x0318, |
| 156 | [PIR] = 0x0320, |
| 157 | [PSR] = 0x0328, |
| 158 | [RDMLR] = 0x0340, |
| 159 | [IPGR] = 0x0350, |
| 160 | [APR] = 0x0354, |
| 161 | [MPR] = 0x0358, |
| 162 | [RFCF] = 0x0360, |
| 163 | [TPAUSER] = 0x0364, |
| 164 | [TPAUSECR] = 0x0368, |
| 165 | [MAHR] = 0x03c0, |
| 166 | [MALR] = 0x03c8, |
| 167 | [TROCR] = 0x03d0, |
| 168 | [CDCR] = 0x03d4, |
| 169 | [LCCR] = 0x03d8, |
| 170 | [CNDCR] = 0x03dc, |
| 171 | [CEFCR] = 0x03e4, |
| 172 | [FRECR] = 0x03e8, |
| 173 | [TSFRCR] = 0x03ec, |
| 174 | [TLFRCR] = 0x03f0, |
| 175 | [RFCR] = 0x03f4, |
| 176 | [MAFCR] = 0x03f8, |
| 177 | |
| 178 | [EDMR] = 0x0200, |
| 179 | [EDTRR] = 0x0208, |
| 180 | [EDRRR] = 0x0210, |
| 181 | [TDLAR] = 0x0218, |
| 182 | [RDLAR] = 0x0220, |
| 183 | [EESR] = 0x0228, |
| 184 | [EESIPR] = 0x0230, |
| 185 | [TRSCER] = 0x0238, |
| 186 | [RMFCR] = 0x0240, |
| 187 | [TFTR] = 0x0248, |
| 188 | [FDR] = 0x0250, |
| 189 | [RMCR] = 0x0258, |
| 190 | [TFUCR] = 0x0264, |
| 191 | [RFOCR] = 0x0268, |
| 192 | [FCFTR] = 0x0270, |
| 193 | [TRIMD] = 0x027c, |
| 194 | }; |
| 195 | |
Sergei Shtylyov | c0013f6 | 2013-03-28 11:48:26 +0000 | [diff] [blame] | 196 | static const u16 sh_eth_offset_fast_sh4[SH_ETH_MAX_REGISTER_OFFSET] = { |
| 197 | [ECMR] = 0x0100, |
| 198 | [RFLR] = 0x0108, |
| 199 | [ECSR] = 0x0110, |
| 200 | [ECSIPR] = 0x0118, |
| 201 | [PIR] = 0x0120, |
| 202 | [PSR] = 0x0128, |
| 203 | [RDMLR] = 0x0140, |
| 204 | [IPGR] = 0x0150, |
| 205 | [APR] = 0x0154, |
| 206 | [MPR] = 0x0158, |
| 207 | [TPAUSER] = 0x0164, |
| 208 | [RFCF] = 0x0160, |
| 209 | [TPAUSECR] = 0x0168, |
| 210 | [BCFRR] = 0x016c, |
| 211 | [MAHR] = 0x01c0, |
| 212 | [MALR] = 0x01c8, |
| 213 | [TROCR] = 0x01d0, |
| 214 | [CDCR] = 0x01d4, |
| 215 | [LCCR] = 0x01d8, |
| 216 | [CNDCR] = 0x01dc, |
| 217 | [CEFCR] = 0x01e4, |
| 218 | [FRECR] = 0x01e8, |
| 219 | [TSFRCR] = 0x01ec, |
| 220 | [TLFRCR] = 0x01f0, |
| 221 | [RFCR] = 0x01f4, |
| 222 | [MAFCR] = 0x01f8, |
| 223 | [RTRATE] = 0x01fc, |
| 224 | |
| 225 | [EDMR] = 0x0000, |
| 226 | [EDTRR] = 0x0008, |
| 227 | [EDRRR] = 0x0010, |
| 228 | [TDLAR] = 0x0018, |
| 229 | [RDLAR] = 0x0020, |
| 230 | [EESR] = 0x0028, |
| 231 | [EESIPR] = 0x0030, |
| 232 | [TRSCER] = 0x0038, |
| 233 | [RMFCR] = 0x0040, |
| 234 | [TFTR] = 0x0048, |
| 235 | [FDR] = 0x0050, |
| 236 | [RMCR] = 0x0058, |
| 237 | [TFUCR] = 0x0064, |
| 238 | [RFOCR] = 0x0068, |
| 239 | [FCFTR] = 0x0070, |
| 240 | [RPADIR] = 0x0078, |
| 241 | [TRIMD] = 0x007c, |
| 242 | [RBWAR] = 0x00c8, |
| 243 | [RDFAR] = 0x00cc, |
| 244 | [TBRAR] = 0x00d4, |
| 245 | [TDFAR] = 0x00d8, |
| 246 | }; |
| 247 | |
| 248 | static const u16 sh_eth_offset_fast_sh3_sh2[SH_ETH_MAX_REGISTER_OFFSET] = { |
| 249 | [ECMR] = 0x0160, |
| 250 | [ECSR] = 0x0164, |
| 251 | [ECSIPR] = 0x0168, |
| 252 | [PIR] = 0x016c, |
| 253 | [MAHR] = 0x0170, |
| 254 | [MALR] = 0x0174, |
| 255 | [RFLR] = 0x0178, |
| 256 | [PSR] = 0x017c, |
| 257 | [TROCR] = 0x0180, |
| 258 | [CDCR] = 0x0184, |
| 259 | [LCCR] = 0x0188, |
| 260 | [CNDCR] = 0x018c, |
| 261 | [CEFCR] = 0x0194, |
| 262 | [FRECR] = 0x0198, |
| 263 | [TSFRCR] = 0x019c, |
| 264 | [TLFRCR] = 0x01a0, |
| 265 | [RFCR] = 0x01a4, |
| 266 | [MAFCR] = 0x01a8, |
| 267 | [IPGR] = 0x01b4, |
| 268 | [APR] = 0x01b8, |
| 269 | [MPR] = 0x01bc, |
| 270 | [TPAUSER] = 0x01c4, |
| 271 | [BCFR] = 0x01cc, |
| 272 | |
| 273 | [ARSTR] = 0x0000, |
| 274 | [TSU_CTRST] = 0x0004, |
| 275 | [TSU_FWEN0] = 0x0010, |
| 276 | [TSU_FWEN1] = 0x0014, |
| 277 | [TSU_FCM] = 0x0018, |
| 278 | [TSU_BSYSL0] = 0x0020, |
| 279 | [TSU_BSYSL1] = 0x0024, |
| 280 | [TSU_PRISL0] = 0x0028, |
| 281 | [TSU_PRISL1] = 0x002c, |
| 282 | [TSU_FWSL0] = 0x0030, |
| 283 | [TSU_FWSL1] = 0x0034, |
| 284 | [TSU_FWSLC] = 0x0038, |
| 285 | [TSU_QTAGM0] = 0x0040, |
| 286 | [TSU_QTAGM1] = 0x0044, |
| 287 | [TSU_ADQT0] = 0x0048, |
| 288 | [TSU_ADQT1] = 0x004c, |
| 289 | [TSU_FWSR] = 0x0050, |
| 290 | [TSU_FWINMK] = 0x0054, |
| 291 | [TSU_ADSBSY] = 0x0060, |
| 292 | [TSU_TEN] = 0x0064, |
| 293 | [TSU_POST1] = 0x0070, |
| 294 | [TSU_POST2] = 0x0074, |
| 295 | [TSU_POST3] = 0x0078, |
| 296 | [TSU_POST4] = 0x007c, |
| 297 | |
| 298 | [TXNLCR0] = 0x0080, |
| 299 | [TXALCR0] = 0x0084, |
| 300 | [RXNLCR0] = 0x0088, |
| 301 | [RXALCR0] = 0x008c, |
| 302 | [FWNLCR0] = 0x0090, |
| 303 | [FWALCR0] = 0x0094, |
| 304 | [TXNLCR1] = 0x00a0, |
| 305 | [TXALCR1] = 0x00a0, |
| 306 | [RXNLCR1] = 0x00a8, |
| 307 | [RXALCR1] = 0x00ac, |
| 308 | [FWNLCR1] = 0x00b0, |
| 309 | [FWALCR1] = 0x00b4, |
| 310 | |
| 311 | [TSU_ADRH0] = 0x0100, |
| 312 | [TSU_ADRL0] = 0x0104, |
| 313 | [TSU_ADRL31] = 0x01fc, |
| 314 | }; |
| 315 | |
Nobuhiro Iwamatsu | dabdde9 | 2013-06-06 09:51:39 +0000 | [diff] [blame] | 316 | static int sh_eth_is_gether(struct sh_eth_private *mdp) |
| 317 | { |
| 318 | if (mdp->reg_offset == sh_eth_offset_gigabit) |
| 319 | return 1; |
| 320 | else |
| 321 | return 0; |
| 322 | } |
| 323 | |
Nobuhiro Iwamatsu | b7feacf | 2013-06-06 09:50:30 +0000 | [diff] [blame] | 324 | static void __maybe_unused sh_eth_select_mii(struct net_device *ndev) |
Nobuhiro Iwamatsu | 5e7a76b | 2012-06-25 17:34:14 +0000 | [diff] [blame] | 325 | { |
| 326 | u32 value = 0x0; |
| 327 | struct sh_eth_private *mdp = netdev_priv(ndev); |
| 328 | |
| 329 | switch (mdp->phy_interface) { |
| 330 | case PHY_INTERFACE_MODE_GMII: |
| 331 | value = 0x2; |
| 332 | break; |
| 333 | case PHY_INTERFACE_MODE_MII: |
| 334 | value = 0x1; |
| 335 | break; |
| 336 | case PHY_INTERFACE_MODE_RMII: |
| 337 | value = 0x0; |
| 338 | break; |
| 339 | default: |
| 340 | pr_warn("PHY interface mode was not setup. Set to MII.\n"); |
| 341 | value = 0x1; |
| 342 | break; |
| 343 | } |
| 344 | |
| 345 | sh_eth_write(ndev, value, RMII_MII); |
| 346 | } |
Nobuhiro Iwamatsu | 5e7a76b | 2012-06-25 17:34:14 +0000 | [diff] [blame] | 347 | |
Nobuhiro Iwamatsu | 04b0ed2 | 2013-06-06 09:45:25 +0000 | [diff] [blame] | 348 | static void __maybe_unused sh_eth_set_duplex(struct net_device *ndev) |
Yoshihiro Shimoda | 65ac885 | 2009-05-24 23:54:30 +0000 | [diff] [blame] | 349 | { |
| 350 | struct sh_eth_private *mdp = netdev_priv(ndev); |
Yoshihiro Shimoda | 65ac885 | 2009-05-24 23:54:30 +0000 | [diff] [blame] | 351 | |
| 352 | if (mdp->duplex) /* Full */ |
Yoshihiro Shimoda | 4a55530 | 2011-03-07 21:59:26 +0000 | [diff] [blame] | 353 | sh_eth_write(ndev, sh_eth_read(ndev, ECMR) | ECMR_DM, ECMR); |
Yoshihiro Shimoda | 65ac885 | 2009-05-24 23:54:30 +0000 | [diff] [blame] | 354 | else /* Half */ |
Yoshihiro Shimoda | 4a55530 | 2011-03-07 21:59:26 +0000 | [diff] [blame] | 355 | sh_eth_write(ndev, sh_eth_read(ndev, ECMR) & ~ECMR_DM, ECMR); |
Yoshihiro Shimoda | 65ac885 | 2009-05-24 23:54:30 +0000 | [diff] [blame] | 356 | } |
| 357 | |
Nobuhiro Iwamatsu | 04b0ed2 | 2013-06-06 09:45:25 +0000 | [diff] [blame] | 358 | /* There is CPU dependent code */ |
| 359 | #if defined(CONFIG_ARCH_R8A7778) || defined(CONFIG_ARCH_R8A7779) |
Yoshihiro Shimoda | 65ac885 | 2009-05-24 23:54:30 +0000 | [diff] [blame] | 360 | static void sh_eth_set_rate(struct net_device *ndev) |
| 361 | { |
| 362 | struct sh_eth_private *mdp = netdev_priv(ndev); |
Yoshihiro Shimoda | 65ac885 | 2009-05-24 23:54:30 +0000 | [diff] [blame] | 363 | |
| 364 | switch (mdp->speed) { |
| 365 | case 10: /* 10BASE */ |
Sergei Shtylyov | a3f109b | 2013-03-28 11:51:31 +0000 | [diff] [blame] | 366 | sh_eth_write(ndev, sh_eth_read(ndev, ECMR) & ~ECMR_ELB, ECMR); |
Yoshihiro Shimoda | 65ac885 | 2009-05-24 23:54:30 +0000 | [diff] [blame] | 367 | break; |
| 368 | case 100:/* 100BASE */ |
Sergei Shtylyov | a3f109b | 2013-03-28 11:51:31 +0000 | [diff] [blame] | 369 | sh_eth_write(ndev, sh_eth_read(ndev, ECMR) | ECMR_ELB, ECMR); |
| 370 | break; |
| 371 | default: |
| 372 | break; |
| 373 | } |
| 374 | } |
| 375 | |
Sergei Shtylyov | 674853b | 2013-04-27 10:44:24 +0000 | [diff] [blame] | 376 | /* R8A7778/9 */ |
Sergei Shtylyov | a3f109b | 2013-03-28 11:51:31 +0000 | [diff] [blame] | 377 | static struct sh_eth_cpu_data sh_eth_my_cpu_data = { |
| 378 | .set_duplex = sh_eth_set_duplex, |
| 379 | .set_rate = sh_eth_set_rate, |
| 380 | |
| 381 | .ecsr_value = ECSR_PSRTO | ECSR_LCHNG | ECSR_ICD, |
| 382 | .ecsipr_value = ECSIPR_PSRTOIP | ECSIPR_LCHNGIP | ECSIPR_ICDIP, |
| 383 | .eesipr_value = 0x01ff009f, |
| 384 | |
| 385 | .tx_check = EESR_FTC | EESR_CND | EESR_DLC | EESR_CD | EESR_RTO, |
| 386 | .eesr_err_check = EESR_TWB | EESR_TABT | EESR_RABT | EESR_RDE | |
| 387 | EESR_RFRMER | EESR_TFE | EESR_TDE | EESR_ECI, |
| 388 | .tx_error_check = EESR_TWB | EESR_TABT | EESR_TDE | EESR_TFE, |
| 389 | |
| 390 | .apr = 1, |
| 391 | .mpr = 1, |
| 392 | .tpauser = 1, |
| 393 | .hw_swap = 1, |
| 394 | }; |
| 395 | #elif defined(CONFIG_CPU_SUBTYPE_SH7724) |
Sergei Shtylyov | a3f109b | 2013-03-28 11:51:31 +0000 | [diff] [blame] | 396 | |
| 397 | static void sh_eth_set_rate(struct net_device *ndev) |
| 398 | { |
| 399 | struct sh_eth_private *mdp = netdev_priv(ndev); |
| 400 | |
| 401 | switch (mdp->speed) { |
| 402 | case 10: /* 10BASE */ |
| 403 | sh_eth_write(ndev, sh_eth_read(ndev, ECMR) & ~ECMR_RTM, ECMR); |
| 404 | break; |
| 405 | case 100:/* 100BASE */ |
| 406 | sh_eth_write(ndev, sh_eth_read(ndev, ECMR) | ECMR_RTM, ECMR); |
Yoshihiro Shimoda | 65ac885 | 2009-05-24 23:54:30 +0000 | [diff] [blame] | 407 | break; |
| 408 | default: |
| 409 | break; |
| 410 | } |
| 411 | } |
| 412 | |
| 413 | /* SH7724 */ |
| 414 | static struct sh_eth_cpu_data sh_eth_my_cpu_data = { |
| 415 | .set_duplex = sh_eth_set_duplex, |
| 416 | .set_rate = sh_eth_set_rate, |
| 417 | |
| 418 | .ecsr_value = ECSR_PSRTO | ECSR_LCHNG | ECSR_ICD, |
| 419 | .ecsipr_value = ECSIPR_PSRTOIP | ECSIPR_LCHNGIP | ECSIPR_ICDIP, |
| 420 | .eesipr_value = DMAC_M_RFRMER | DMAC_M_ECI | 0x01ff009f, |
| 421 | |
| 422 | .tx_check = EESR_FTC | EESR_CND | EESR_DLC | EESR_CD | EESR_RTO, |
| 423 | .eesr_err_check = EESR_TWB | EESR_TABT | EESR_RABT | EESR_RDE | |
| 424 | EESR_RFRMER | EESR_TFE | EESR_TDE | EESR_ECI, |
| 425 | .tx_error_check = EESR_TWB | EESR_TABT | EESR_TDE | EESR_TFE, |
| 426 | |
| 427 | .apr = 1, |
| 428 | .mpr = 1, |
| 429 | .tpauser = 1, |
| 430 | .hw_swap = 1, |
Magnus Damm | 503914c | 2009-12-15 21:16:55 -0800 | [diff] [blame] | 431 | .rpadir = 1, |
| 432 | .rpadir_value = 0x00020000, /* NET_IP_ALIGN assumed to be 2 */ |
Yoshihiro Shimoda | 65ac885 | 2009-05-24 23:54:30 +0000 | [diff] [blame] | 433 | }; |
Yoshihiro Shimoda | f29a3d0 | 2010-07-05 18:32:50 +0000 | [diff] [blame] | 434 | #elif defined(CONFIG_CPU_SUBTYPE_SH7757) |
Yoshihiro Shimoda | 8fcd496 | 2011-03-07 21:59:49 +0000 | [diff] [blame] | 435 | #define SH_ETH_HAS_BOTH_MODULES 1 |
Nobuhiro Iwamatsu | 5cee1d3 | 2012-06-25 17:35:12 +0000 | [diff] [blame] | 436 | |
Yoshihiro Shimoda | f29a3d0 | 2010-07-05 18:32:50 +0000 | [diff] [blame] | 437 | static void sh_eth_set_rate(struct net_device *ndev) |
| 438 | { |
| 439 | struct sh_eth_private *mdp = netdev_priv(ndev); |
Yoshihiro Shimoda | f29a3d0 | 2010-07-05 18:32:50 +0000 | [diff] [blame] | 440 | |
| 441 | switch (mdp->speed) { |
| 442 | case 10: /* 10BASE */ |
Yoshihiro Shimoda | 4a55530 | 2011-03-07 21:59:26 +0000 | [diff] [blame] | 443 | sh_eth_write(ndev, 0, RTRATE); |
Yoshihiro Shimoda | f29a3d0 | 2010-07-05 18:32:50 +0000 | [diff] [blame] | 444 | break; |
| 445 | case 100:/* 100BASE */ |
Yoshihiro Shimoda | 4a55530 | 2011-03-07 21:59:26 +0000 | [diff] [blame] | 446 | sh_eth_write(ndev, 1, RTRATE); |
Yoshihiro Shimoda | f29a3d0 | 2010-07-05 18:32:50 +0000 | [diff] [blame] | 447 | break; |
| 448 | default: |
| 449 | break; |
| 450 | } |
| 451 | } |
| 452 | |
| 453 | /* SH7757 */ |
| 454 | static struct sh_eth_cpu_data sh_eth_my_cpu_data = { |
| 455 | .set_duplex = sh_eth_set_duplex, |
| 456 | .set_rate = sh_eth_set_rate, |
| 457 | |
| 458 | .eesipr_value = DMAC_M_RFRMER | DMAC_M_ECI | 0x003fffff, |
| 459 | .rmcr_value = 0x00000001, |
| 460 | |
| 461 | .tx_check = EESR_FTC | EESR_CND | EESR_DLC | EESR_CD | EESR_RTO, |
| 462 | .eesr_err_check = EESR_TWB | EESR_TABT | EESR_RABT | EESR_RDE | |
| 463 | EESR_RFRMER | EESR_TFE | EESR_TDE | EESR_ECI, |
| 464 | .tx_error_check = EESR_TWB | EESR_TABT | EESR_TDE | EESR_TFE, |
| 465 | |
Nobuhiro Iwamatsu | 5b3dfd1 | 2013-06-06 09:49:30 +0000 | [diff] [blame] | 466 | .irq_flags = IRQF_SHARED, |
Yoshihiro Shimoda | f29a3d0 | 2010-07-05 18:32:50 +0000 | [diff] [blame] | 467 | .apr = 1, |
| 468 | .mpr = 1, |
| 469 | .tpauser = 1, |
| 470 | .hw_swap = 1, |
| 471 | .no_ade = 1, |
Yoshihiro Shimoda | 2e98e79 | 2011-07-05 20:33:57 +0000 | [diff] [blame] | 472 | .rpadir = 1, |
| 473 | .rpadir_value = 2 << 16, |
Yoshihiro Shimoda | f29a3d0 | 2010-07-05 18:32:50 +0000 | [diff] [blame] | 474 | }; |
Yoshihiro Shimoda | 65ac885 | 2009-05-24 23:54:30 +0000 | [diff] [blame] | 475 | |
Yoshihiro Shimoda | 8fcd496 | 2011-03-07 21:59:49 +0000 | [diff] [blame] | 476 | #define SH_GIGA_ETH_BASE 0xfee00000 |
| 477 | #define GIGA_MALR(port) (SH_GIGA_ETH_BASE + 0x800 * (port) + 0x05c8) |
| 478 | #define GIGA_MAHR(port) (SH_GIGA_ETH_BASE + 0x800 * (port) + 0x05c0) |
| 479 | static void sh_eth_chip_reset_giga(struct net_device *ndev) |
| 480 | { |
| 481 | int i; |
| 482 | unsigned long mahr[2], malr[2]; |
| 483 | |
| 484 | /* save MAHR and MALR */ |
| 485 | for (i = 0; i < 2; i++) { |
Yoshihiro Shimoda | ae70644 | 2011-09-27 21:48:58 +0000 | [diff] [blame] | 486 | malr[i] = ioread32((void *)GIGA_MALR(i)); |
| 487 | mahr[i] = ioread32((void *)GIGA_MAHR(i)); |
Yoshihiro Shimoda | 8fcd496 | 2011-03-07 21:59:49 +0000 | [diff] [blame] | 488 | } |
| 489 | |
| 490 | /* reset device */ |
Yoshihiro Shimoda | ae70644 | 2011-09-27 21:48:58 +0000 | [diff] [blame] | 491 | iowrite32(ARSTR_ARSTR, (void *)(SH_GIGA_ETH_BASE + 0x1800)); |
Yoshihiro Shimoda | 8fcd496 | 2011-03-07 21:59:49 +0000 | [diff] [blame] | 492 | mdelay(1); |
| 493 | |
| 494 | /* restore MAHR and MALR */ |
| 495 | for (i = 0; i < 2; i++) { |
Yoshihiro Shimoda | ae70644 | 2011-09-27 21:48:58 +0000 | [diff] [blame] | 496 | iowrite32(malr[i], (void *)GIGA_MALR(i)); |
| 497 | iowrite32(mahr[i], (void *)GIGA_MAHR(i)); |
Yoshihiro Shimoda | 8fcd496 | 2011-03-07 21:59:49 +0000 | [diff] [blame] | 498 | } |
| 499 | } |
| 500 | |
Yoshihiro Shimoda | 8fcd496 | 2011-03-07 21:59:49 +0000 | [diff] [blame] | 501 | static void sh_eth_set_rate_giga(struct net_device *ndev) |
| 502 | { |
| 503 | struct sh_eth_private *mdp = netdev_priv(ndev); |
| 504 | |
| 505 | switch (mdp->speed) { |
| 506 | case 10: /* 10BASE */ |
| 507 | sh_eth_write(ndev, 0x00000000, GECMR); |
| 508 | break; |
| 509 | case 100:/* 100BASE */ |
| 510 | sh_eth_write(ndev, 0x00000010, GECMR); |
| 511 | break; |
| 512 | case 1000: /* 1000BASE */ |
| 513 | sh_eth_write(ndev, 0x00000020, GECMR); |
| 514 | break; |
| 515 | default: |
| 516 | break; |
| 517 | } |
| 518 | } |
| 519 | |
| 520 | /* SH7757(GETHERC) */ |
| 521 | static struct sh_eth_cpu_data sh_eth_my_cpu_data_giga = { |
| 522 | .chip_reset = sh_eth_chip_reset_giga, |
Nobuhiro Iwamatsu | 04b0ed2 | 2013-06-06 09:45:25 +0000 | [diff] [blame] | 523 | .set_duplex = sh_eth_set_duplex, |
Yoshihiro Shimoda | 8fcd496 | 2011-03-07 21:59:49 +0000 | [diff] [blame] | 524 | .set_rate = sh_eth_set_rate_giga, |
| 525 | |
| 526 | .ecsr_value = ECSR_ICD | ECSR_MPD, |
| 527 | .ecsipr_value = ECSIPR_LCHNGIP | ECSIPR_ICDIP | ECSIPR_MPDIP, |
| 528 | .eesipr_value = DMAC_M_RFRMER | DMAC_M_ECI | 0x003fffff, |
| 529 | |
| 530 | .tx_check = EESR_TC1 | EESR_FTC, |
| 531 | .eesr_err_check = EESR_TWB1 | EESR_TWB | EESR_TABT | EESR_RABT | \ |
| 532 | EESR_RDE | EESR_RFRMER | EESR_TFE | EESR_TDE | \ |
| 533 | EESR_ECI, |
| 534 | .tx_error_check = EESR_TWB1 | EESR_TWB | EESR_TABT | EESR_TDE | \ |
| 535 | EESR_TFE, |
| 536 | .fdr_value = 0x0000072f, |
| 537 | .rmcr_value = 0x00000001, |
| 538 | |
Nobuhiro Iwamatsu | 5b3dfd1 | 2013-06-06 09:49:30 +0000 | [diff] [blame] | 539 | .irq_flags = IRQF_SHARED, |
Yoshihiro Shimoda | 8fcd496 | 2011-03-07 21:59:49 +0000 | [diff] [blame] | 540 | .apr = 1, |
| 541 | .mpr = 1, |
| 542 | .tpauser = 1, |
| 543 | .bculr = 1, |
| 544 | .hw_swap = 1, |
| 545 | .rpadir = 1, |
| 546 | .rpadir_value = 2 << 16, |
| 547 | .no_trimd = 1, |
| 548 | .no_ade = 1, |
Yoshihiro Shimoda | 3acbc97 | 2012-02-15 17:54:51 +0000 | [diff] [blame] | 549 | .tsu = 1, |
Yoshihiro Shimoda | 8fcd496 | 2011-03-07 21:59:49 +0000 | [diff] [blame] | 550 | }; |
| 551 | |
| 552 | static struct sh_eth_cpu_data *sh_eth_get_cpu_data(struct sh_eth_private *mdp) |
| 553 | { |
| 554 | if (sh_eth_is_gether(mdp)) |
| 555 | return &sh_eth_my_cpu_data_giga; |
| 556 | else |
| 557 | return &sh_eth_my_cpu_data; |
| 558 | } |
| 559 | |
Nobuhiro Iwamatsu | f0e81fe | 2012-03-25 18:59:51 +0000 | [diff] [blame] | 560 | #elif defined(CONFIG_CPU_SUBTYPE_SH7734) || defined(CONFIG_CPU_SUBTYPE_SH7763) |
Nobuhiro Iwamatsu | 5e7a76b | 2012-06-25 17:34:14 +0000 | [diff] [blame] | 561 | |
Yoshihiro Shimoda | 380af9e | 2009-05-24 23:54:21 +0000 | [diff] [blame] | 562 | static void sh_eth_chip_reset(struct net_device *ndev) |
| 563 | { |
Yoshihiro Shimoda | 4986b99 | 2011-03-07 21:59:34 +0000 | [diff] [blame] | 564 | struct sh_eth_private *mdp = netdev_priv(ndev); |
| 565 | |
Yoshihiro Shimoda | 380af9e | 2009-05-24 23:54:21 +0000 | [diff] [blame] | 566 | /* reset device */ |
Yoshihiro Shimoda | 4986b99 | 2011-03-07 21:59:34 +0000 | [diff] [blame] | 567 | sh_eth_tsu_write(mdp, ARSTR_ARSTR, ARSTR); |
Yoshihiro Shimoda | 380af9e | 2009-05-24 23:54:21 +0000 | [diff] [blame] | 568 | mdelay(1); |
| 569 | } |
| 570 | |
Yoshihiro Shimoda | 380af9e | 2009-05-24 23:54:21 +0000 | [diff] [blame] | 571 | static void sh_eth_set_rate(struct net_device *ndev) |
| 572 | { |
| 573 | struct sh_eth_private *mdp = netdev_priv(ndev); |
Yoshihiro Shimoda | 380af9e | 2009-05-24 23:54:21 +0000 | [diff] [blame] | 574 | |
| 575 | switch (mdp->speed) { |
| 576 | case 10: /* 10BASE */ |
Yoshihiro Shimoda | 4a55530 | 2011-03-07 21:59:26 +0000 | [diff] [blame] | 577 | sh_eth_write(ndev, GECMR_10, GECMR); |
Yoshihiro Shimoda | 380af9e | 2009-05-24 23:54:21 +0000 | [diff] [blame] | 578 | break; |
| 579 | case 100:/* 100BASE */ |
Yoshihiro Shimoda | 4a55530 | 2011-03-07 21:59:26 +0000 | [diff] [blame] | 580 | sh_eth_write(ndev, GECMR_100, GECMR); |
Yoshihiro Shimoda | 380af9e | 2009-05-24 23:54:21 +0000 | [diff] [blame] | 581 | break; |
| 582 | case 1000: /* 1000BASE */ |
Yoshihiro Shimoda | 4a55530 | 2011-03-07 21:59:26 +0000 | [diff] [blame] | 583 | sh_eth_write(ndev, GECMR_1000, GECMR); |
Yoshihiro Shimoda | 380af9e | 2009-05-24 23:54:21 +0000 | [diff] [blame] | 584 | break; |
| 585 | default: |
| 586 | break; |
| 587 | } |
| 588 | } |
| 589 | |
| 590 | /* sh7763 */ |
| 591 | static struct sh_eth_cpu_data sh_eth_my_cpu_data = { |
| 592 | .chip_reset = sh_eth_chip_reset, |
| 593 | .set_duplex = sh_eth_set_duplex, |
| 594 | .set_rate = sh_eth_set_rate, |
| 595 | |
| 596 | .ecsr_value = ECSR_ICD | ECSR_MPD, |
| 597 | .ecsipr_value = ECSIPR_LCHNGIP | ECSIPR_ICDIP | ECSIPR_MPDIP, |
| 598 | .eesipr_value = DMAC_M_RFRMER | DMAC_M_ECI | 0x003fffff, |
| 599 | |
| 600 | .tx_check = EESR_TC1 | EESR_FTC, |
| 601 | .eesr_err_check = EESR_TWB1 | EESR_TWB | EESR_TABT | EESR_RABT | \ |
| 602 | EESR_RDE | EESR_RFRMER | EESR_TFE | EESR_TDE | \ |
| 603 | EESR_ECI, |
| 604 | .tx_error_check = EESR_TWB1 | EESR_TWB | EESR_TABT | EESR_TDE | \ |
| 605 | EESR_TFE, |
| 606 | |
| 607 | .apr = 1, |
| 608 | .mpr = 1, |
| 609 | .tpauser = 1, |
| 610 | .bculr = 1, |
| 611 | .hw_swap = 1, |
Yoshihiro Shimoda | 380af9e | 2009-05-24 23:54:21 +0000 | [diff] [blame] | 612 | .no_trimd = 1, |
| 613 | .no_ade = 1, |
Yoshihiro Shimoda | 4986b99 | 2011-03-07 21:59:34 +0000 | [diff] [blame] | 614 | .tsu = 1, |
Nobuhiro Iwamatsu | f0e81fe | 2012-03-25 18:59:51 +0000 | [diff] [blame] | 615 | #if defined(CONFIG_CPU_SUBTYPE_SH7734) |
| 616 | .hw_crc = 1, |
Nobuhiro Iwamatsu | 5e7a76b | 2012-06-25 17:34:14 +0000 | [diff] [blame] | 617 | .select_mii = 1, |
Nobuhiro Iwamatsu | 5b3dfd1 | 2013-06-06 09:49:30 +0000 | [diff] [blame] | 618 | #else |
| 619 | .irq_flags = IRQF_SHARED, |
Nobuhiro Iwamatsu | f0e81fe | 2012-03-25 18:59:51 +0000 | [diff] [blame] | 620 | #endif |
Yoshihiro Shimoda | 380af9e | 2009-05-24 23:54:21 +0000 | [diff] [blame] | 621 | }; |
Sergei Shtylyov | e5c9b4c | 2013-06-07 13:57:12 +0000 | [diff] [blame^] | 622 | #endif |
Yoshihiro Shimoda | 380af9e | 2009-05-24 23:54:21 +0000 | [diff] [blame] | 623 | |
Sergei Shtylyov | e5c9b4c | 2013-06-07 13:57:12 +0000 | [diff] [blame^] | 624 | static void sh_eth_chip_reset_r8a7740(struct net_device *ndev) |
Yoshihiro Shimoda | 73a0d90 | 2012-04-04 18:37:10 +0000 | [diff] [blame] | 625 | { |
| 626 | struct sh_eth_private *mdp = netdev_priv(ndev); |
Yoshihiro Shimoda | 73a0d90 | 2012-04-04 18:37:10 +0000 | [diff] [blame] | 627 | |
| 628 | /* reset device */ |
| 629 | sh_eth_tsu_write(mdp, ARSTR_ARSTR, ARSTR); |
| 630 | mdelay(1); |
| 631 | |
Nobuhiro Iwamatsu | 5e7a76b | 2012-06-25 17:34:14 +0000 | [diff] [blame] | 632 | sh_eth_select_mii(ndev); |
Yoshihiro Shimoda | 73a0d90 | 2012-04-04 18:37:10 +0000 | [diff] [blame] | 633 | } |
| 634 | |
Sergei Shtylyov | e5c9b4c | 2013-06-07 13:57:12 +0000 | [diff] [blame^] | 635 | static void sh_eth_set_rate_gether(struct net_device *ndev) |
Yoshihiro Shimoda | 73a0d90 | 2012-04-04 18:37:10 +0000 | [diff] [blame] | 636 | { |
| 637 | struct sh_eth_private *mdp = netdev_priv(ndev); |
| 638 | |
| 639 | switch (mdp->speed) { |
| 640 | case 10: /* 10BASE */ |
| 641 | sh_eth_write(ndev, GECMR_10, GECMR); |
| 642 | break; |
| 643 | case 100:/* 100BASE */ |
| 644 | sh_eth_write(ndev, GECMR_100, GECMR); |
| 645 | break; |
| 646 | case 1000: /* 1000BASE */ |
| 647 | sh_eth_write(ndev, GECMR_1000, GECMR); |
| 648 | break; |
| 649 | default: |
| 650 | break; |
| 651 | } |
| 652 | } |
| 653 | |
| 654 | /* R8A7740 */ |
Sergei Shtylyov | e5c9b4c | 2013-06-07 13:57:12 +0000 | [diff] [blame^] | 655 | static struct sh_eth_cpu_data r8a7740_data = { |
| 656 | .chip_reset = sh_eth_chip_reset_r8a7740, |
Yoshihiro Shimoda | 73a0d90 | 2012-04-04 18:37:10 +0000 | [diff] [blame] | 657 | .set_duplex = sh_eth_set_duplex, |
Sergei Shtylyov | e5c9b4c | 2013-06-07 13:57:12 +0000 | [diff] [blame^] | 658 | .set_rate = sh_eth_set_rate_gether, |
Yoshihiro Shimoda | 73a0d90 | 2012-04-04 18:37:10 +0000 | [diff] [blame] | 659 | |
| 660 | .ecsr_value = ECSR_ICD | ECSR_MPD, |
| 661 | .ecsipr_value = ECSIPR_LCHNGIP | ECSIPR_ICDIP | ECSIPR_MPDIP, |
| 662 | .eesipr_value = DMAC_M_RFRMER | DMAC_M_ECI | 0x003fffff, |
| 663 | |
| 664 | .tx_check = EESR_TC1 | EESR_FTC, |
| 665 | .eesr_err_check = EESR_TWB1 | EESR_TWB | EESR_TABT | EESR_RABT | \ |
| 666 | EESR_RDE | EESR_RFRMER | EESR_TFE | EESR_TDE | \ |
| 667 | EESR_ECI, |
| 668 | .tx_error_check = EESR_TWB1 | EESR_TWB | EESR_TABT | EESR_TDE | \ |
| 669 | EESR_TFE, |
| 670 | |
| 671 | .apr = 1, |
| 672 | .mpr = 1, |
| 673 | .tpauser = 1, |
| 674 | .bculr = 1, |
| 675 | .hw_swap = 1, |
| 676 | .no_trimd = 1, |
| 677 | .no_ade = 1, |
| 678 | .tsu = 1, |
Nobuhiro Iwamatsu | 5e7a76b | 2012-06-25 17:34:14 +0000 | [diff] [blame] | 679 | .select_mii = 1, |
Yoshihiro Shimoda | 73a0d90 | 2012-04-04 18:37:10 +0000 | [diff] [blame] | 680 | }; |
| 681 | |
Sergei Shtylyov | c18a79a | 2013-06-07 13:56:05 +0000 | [diff] [blame] | 682 | static struct sh_eth_cpu_data sh7619_data = { |
Yoshihiro Shimoda | 380af9e | 2009-05-24 23:54:21 +0000 | [diff] [blame] | 683 | .eesipr_value = DMAC_M_RFRMER | DMAC_M_ECI | 0x003fffff, |
| 684 | |
| 685 | .apr = 1, |
| 686 | .mpr = 1, |
| 687 | .tpauser = 1, |
| 688 | .hw_swap = 1, |
| 689 | }; |
Sergei Shtylyov | 7bbe150 | 2013-06-07 13:55:08 +0000 | [diff] [blame] | 690 | |
| 691 | static struct sh_eth_cpu_data sh771x_data = { |
Yoshihiro Shimoda | 380af9e | 2009-05-24 23:54:21 +0000 | [diff] [blame] | 692 | .eesipr_value = DMAC_M_RFRMER | DMAC_M_ECI | 0x003fffff, |
Yoshihiro Shimoda | 4986b99 | 2011-03-07 21:59:34 +0000 | [diff] [blame] | 693 | .tsu = 1, |
Yoshihiro Shimoda | 380af9e | 2009-05-24 23:54:21 +0000 | [diff] [blame] | 694 | }; |
Yoshihiro Shimoda | 380af9e | 2009-05-24 23:54:21 +0000 | [diff] [blame] | 695 | |
| 696 | static void sh_eth_set_default_cpu_data(struct sh_eth_cpu_data *cd) |
| 697 | { |
| 698 | if (!cd->ecsr_value) |
| 699 | cd->ecsr_value = DEFAULT_ECSR_INIT; |
| 700 | |
| 701 | if (!cd->ecsipr_value) |
| 702 | cd->ecsipr_value = DEFAULT_ECSIPR_INIT; |
| 703 | |
| 704 | if (!cd->fcftr_value) |
| 705 | cd->fcftr_value = DEFAULT_FIFO_F_D_RFF | \ |
| 706 | DEFAULT_FIFO_F_D_RFD; |
| 707 | |
| 708 | if (!cd->fdr_value) |
| 709 | cd->fdr_value = DEFAULT_FDR_INIT; |
| 710 | |
| 711 | if (!cd->rmcr_value) |
| 712 | cd->rmcr_value = DEFAULT_RMCR_VALUE; |
| 713 | |
| 714 | if (!cd->tx_check) |
| 715 | cd->tx_check = DEFAULT_TX_CHECK; |
| 716 | |
| 717 | if (!cd->eesr_err_check) |
| 718 | cd->eesr_err_check = DEFAULT_EESR_ERR_CHECK; |
| 719 | |
| 720 | if (!cd->tx_error_check) |
| 721 | cd->tx_error_check = DEFAULT_TX_ERROR_CHECK; |
| 722 | } |
| 723 | |
Nobuhiro Iwamatsu | 5cee1d3 | 2012-06-25 17:35:12 +0000 | [diff] [blame] | 724 | static int sh_eth_check_reset(struct net_device *ndev) |
| 725 | { |
| 726 | int ret = 0; |
| 727 | int cnt = 100; |
| 728 | |
| 729 | while (cnt > 0) { |
| 730 | if (!(sh_eth_read(ndev, EDMR) & 0x3)) |
| 731 | break; |
| 732 | mdelay(1); |
| 733 | cnt--; |
| 734 | } |
| 735 | if (cnt < 0) { |
Nobuhiro Iwamatsu | 14c3326 | 2013-03-20 22:46:55 +0000 | [diff] [blame] | 736 | pr_err("Device reset fail\n"); |
Nobuhiro Iwamatsu | 5cee1d3 | 2012-06-25 17:35:12 +0000 | [diff] [blame] | 737 | ret = -ETIMEDOUT; |
| 738 | } |
| 739 | return ret; |
Yoshihiro Shimoda | 380af9e | 2009-05-24 23:54:21 +0000 | [diff] [blame] | 740 | } |
Nobuhiro Iwamatsu | dabdde9 | 2013-06-06 09:51:39 +0000 | [diff] [blame] | 741 | |
| 742 | static int sh_eth_reset(struct net_device *ndev) |
| 743 | { |
| 744 | struct sh_eth_private *mdp = netdev_priv(ndev); |
| 745 | int ret = 0; |
| 746 | |
| 747 | if (sh_eth_is_gether(mdp)) { |
| 748 | sh_eth_write(ndev, EDSR_ENALL, EDSR); |
| 749 | sh_eth_write(ndev, sh_eth_read(ndev, EDMR) | EDMR_SRST_GETHER, |
| 750 | EDMR); |
| 751 | |
| 752 | ret = sh_eth_check_reset(ndev); |
| 753 | if (ret) |
| 754 | goto out; |
| 755 | |
| 756 | /* Table Init */ |
| 757 | sh_eth_write(ndev, 0x0, TDLAR); |
| 758 | sh_eth_write(ndev, 0x0, TDFAR); |
| 759 | sh_eth_write(ndev, 0x0, TDFXR); |
| 760 | sh_eth_write(ndev, 0x0, TDFFR); |
| 761 | sh_eth_write(ndev, 0x0, RDLAR); |
| 762 | sh_eth_write(ndev, 0x0, RDFAR); |
| 763 | sh_eth_write(ndev, 0x0, RDFXR); |
| 764 | sh_eth_write(ndev, 0x0, RDFFR); |
| 765 | |
| 766 | /* Reset HW CRC register */ |
| 767 | if (mdp->cd->hw_crc) |
| 768 | sh_eth_write(ndev, 0x0, CSMR); |
| 769 | |
| 770 | /* Select MII mode */ |
| 771 | if (mdp->cd->select_mii) |
| 772 | sh_eth_select_mii(ndev); |
| 773 | } else { |
| 774 | sh_eth_write(ndev, sh_eth_read(ndev, EDMR) | EDMR_SRST_ETHER, |
| 775 | EDMR); |
| 776 | mdelay(3); |
| 777 | sh_eth_write(ndev, sh_eth_read(ndev, EDMR) & ~EDMR_SRST_ETHER, |
| 778 | EDMR); |
| 779 | } |
| 780 | |
| 781 | out: |
| 782 | return ret; |
| 783 | } |
Yoshihiro Shimoda | 380af9e | 2009-05-24 23:54:21 +0000 | [diff] [blame] | 784 | |
Yoshihiro Shimoda | 73a0d90 | 2012-04-04 18:37:10 +0000 | [diff] [blame] | 785 | #if defined(CONFIG_CPU_SH4) || defined(CONFIG_ARCH_SHMOBILE) |
Yoshihiro Shimoda | 380af9e | 2009-05-24 23:54:21 +0000 | [diff] [blame] | 786 | static void sh_eth_set_receive_align(struct sk_buff *skb) |
| 787 | { |
| 788 | int reserve; |
| 789 | |
| 790 | reserve = SH4_SKB_RX_ALIGN - ((u32)skb->data & (SH4_SKB_RX_ALIGN - 1)); |
| 791 | if (reserve) |
| 792 | skb_reserve(skb, reserve); |
| 793 | } |
| 794 | #else |
| 795 | static void sh_eth_set_receive_align(struct sk_buff *skb) |
| 796 | { |
| 797 | skb_reserve(skb, SH2_SH3_SKB_RX_ALIGN); |
| 798 | } |
| 799 | #endif |
| 800 | |
| 801 | |
Yoshinori Sato | 71557a3 | 2008-08-06 19:49:00 -0400 | [diff] [blame] | 802 | /* CPU <-> EDMAC endian convert */ |
| 803 | static inline __u32 cpu_to_edmac(struct sh_eth_private *mdp, u32 x) |
| 804 | { |
| 805 | switch (mdp->edmac_endian) { |
| 806 | case EDMAC_LITTLE_ENDIAN: |
| 807 | return cpu_to_le32(x); |
| 808 | case EDMAC_BIG_ENDIAN: |
| 809 | return cpu_to_be32(x); |
| 810 | } |
| 811 | return x; |
| 812 | } |
| 813 | |
| 814 | static inline __u32 edmac_to_cpu(struct sh_eth_private *mdp, u32 x) |
| 815 | { |
| 816 | switch (mdp->edmac_endian) { |
| 817 | case EDMAC_LITTLE_ENDIAN: |
| 818 | return le32_to_cpu(x); |
| 819 | case EDMAC_BIG_ENDIAN: |
| 820 | return be32_to_cpu(x); |
| 821 | } |
| 822 | return x; |
| 823 | } |
| 824 | |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 825 | /* |
| 826 | * Program the hardware MAC address from dev->dev_addr. |
| 827 | */ |
| 828 | static void update_mac_address(struct net_device *ndev) |
| 829 | { |
Yoshihiro Shimoda | 4a55530 | 2011-03-07 21:59:26 +0000 | [diff] [blame] | 830 | sh_eth_write(ndev, |
| 831 | (ndev->dev_addr[0] << 24) | (ndev->dev_addr[1] << 16) | |
| 832 | (ndev->dev_addr[2] << 8) | (ndev->dev_addr[3]), MAHR); |
| 833 | sh_eth_write(ndev, |
| 834 | (ndev->dev_addr[4] << 8) | (ndev->dev_addr[5]), MALR); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 835 | } |
| 836 | |
| 837 | /* |
| 838 | * Get MAC address from SuperH MAC address register |
| 839 | * |
| 840 | * SuperH's Ethernet device doesn't have 'ROM' to MAC address. |
| 841 | * This driver get MAC address that use by bootloader(U-boot or sh-ipl+g). |
| 842 | * When you want use this device, you must set MAC address in bootloader. |
| 843 | * |
| 844 | */ |
Magnus Damm | 748031f | 2009-10-09 00:17:14 +0000 | [diff] [blame] | 845 | static void read_mac_address(struct net_device *ndev, unsigned char *mac) |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 846 | { |
Magnus Damm | 748031f | 2009-10-09 00:17:14 +0000 | [diff] [blame] | 847 | if (mac[0] || mac[1] || mac[2] || mac[3] || mac[4] || mac[5]) { |
| 848 | memcpy(ndev->dev_addr, mac, 6); |
| 849 | } else { |
Yoshihiro Shimoda | 4a55530 | 2011-03-07 21:59:26 +0000 | [diff] [blame] | 850 | ndev->dev_addr[0] = (sh_eth_read(ndev, MAHR) >> 24); |
| 851 | ndev->dev_addr[1] = (sh_eth_read(ndev, MAHR) >> 16) & 0xFF; |
| 852 | ndev->dev_addr[2] = (sh_eth_read(ndev, MAHR) >> 8) & 0xFF; |
| 853 | ndev->dev_addr[3] = (sh_eth_read(ndev, MAHR) & 0xFF); |
| 854 | ndev->dev_addr[4] = (sh_eth_read(ndev, MALR) >> 8) & 0xFF; |
| 855 | ndev->dev_addr[5] = (sh_eth_read(ndev, MALR) & 0xFF); |
Magnus Damm | 748031f | 2009-10-09 00:17:14 +0000 | [diff] [blame] | 856 | } |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 857 | } |
| 858 | |
Yoshihiro Shimoda | c5ed536 | 2011-03-07 21:59:38 +0000 | [diff] [blame] | 859 | static unsigned long sh_eth_get_edtrr_trns(struct sh_eth_private *mdp) |
| 860 | { |
| 861 | if (sh_eth_is_gether(mdp)) |
| 862 | return EDTRR_TRNS_GETHER; |
| 863 | else |
| 864 | return EDTRR_TRNS_ETHER; |
| 865 | } |
| 866 | |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 867 | struct bb_info { |
Yoshihiro Shimoda | ae70644 | 2011-09-27 21:48:58 +0000 | [diff] [blame] | 868 | void (*set_gate)(void *addr); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 869 | struct mdiobb_ctrl ctrl; |
Yoshihiro Shimoda | ae70644 | 2011-09-27 21:48:58 +0000 | [diff] [blame] | 870 | void *addr; |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 871 | u32 mmd_msk;/* MMD */ |
| 872 | u32 mdo_msk; |
| 873 | u32 mdi_msk; |
| 874 | u32 mdc_msk; |
| 875 | }; |
| 876 | |
| 877 | /* PHY bit set */ |
Yoshihiro Shimoda | ae70644 | 2011-09-27 21:48:58 +0000 | [diff] [blame] | 878 | static void bb_set(void *addr, u32 msk) |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 879 | { |
Yoshihiro Shimoda | ae70644 | 2011-09-27 21:48:58 +0000 | [diff] [blame] | 880 | iowrite32(ioread32(addr) | msk, addr); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 881 | } |
| 882 | |
| 883 | /* PHY bit clear */ |
Yoshihiro Shimoda | ae70644 | 2011-09-27 21:48:58 +0000 | [diff] [blame] | 884 | static void bb_clr(void *addr, u32 msk) |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 885 | { |
Yoshihiro Shimoda | ae70644 | 2011-09-27 21:48:58 +0000 | [diff] [blame] | 886 | iowrite32((ioread32(addr) & ~msk), addr); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 887 | } |
| 888 | |
| 889 | /* PHY bit read */ |
Yoshihiro Shimoda | ae70644 | 2011-09-27 21:48:58 +0000 | [diff] [blame] | 890 | static int bb_read(void *addr, u32 msk) |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 891 | { |
Yoshihiro Shimoda | ae70644 | 2011-09-27 21:48:58 +0000 | [diff] [blame] | 892 | return (ioread32(addr) & msk) != 0; |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 893 | } |
| 894 | |
| 895 | /* Data I/O pin control */ |
| 896 | static void sh_mmd_ctrl(struct mdiobb_ctrl *ctrl, int bit) |
| 897 | { |
| 898 | struct bb_info *bitbang = container_of(ctrl, struct bb_info, ctrl); |
Yoshihiro Shimoda | b3017e6 | 2011-03-07 21:59:55 +0000 | [diff] [blame] | 899 | |
| 900 | if (bitbang->set_gate) |
| 901 | bitbang->set_gate(bitbang->addr); |
| 902 | |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 903 | if (bit) |
| 904 | bb_set(bitbang->addr, bitbang->mmd_msk); |
| 905 | else |
| 906 | bb_clr(bitbang->addr, bitbang->mmd_msk); |
| 907 | } |
| 908 | |
| 909 | /* Set bit data*/ |
| 910 | static void sh_set_mdio(struct mdiobb_ctrl *ctrl, int bit) |
| 911 | { |
| 912 | struct bb_info *bitbang = container_of(ctrl, struct bb_info, ctrl); |
| 913 | |
Yoshihiro Shimoda | b3017e6 | 2011-03-07 21:59:55 +0000 | [diff] [blame] | 914 | if (bitbang->set_gate) |
| 915 | bitbang->set_gate(bitbang->addr); |
| 916 | |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 917 | if (bit) |
| 918 | bb_set(bitbang->addr, bitbang->mdo_msk); |
| 919 | else |
| 920 | bb_clr(bitbang->addr, bitbang->mdo_msk); |
| 921 | } |
| 922 | |
| 923 | /* Get bit data*/ |
| 924 | static int sh_get_mdio(struct mdiobb_ctrl *ctrl) |
| 925 | { |
| 926 | struct bb_info *bitbang = container_of(ctrl, struct bb_info, ctrl); |
Yoshihiro Shimoda | b3017e6 | 2011-03-07 21:59:55 +0000 | [diff] [blame] | 927 | |
| 928 | if (bitbang->set_gate) |
| 929 | bitbang->set_gate(bitbang->addr); |
| 930 | |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 931 | return bb_read(bitbang->addr, bitbang->mdi_msk); |
| 932 | } |
| 933 | |
| 934 | /* MDC pin control */ |
| 935 | static void sh_mdc_ctrl(struct mdiobb_ctrl *ctrl, int bit) |
| 936 | { |
| 937 | struct bb_info *bitbang = container_of(ctrl, struct bb_info, ctrl); |
| 938 | |
Yoshihiro Shimoda | b3017e6 | 2011-03-07 21:59:55 +0000 | [diff] [blame] | 939 | if (bitbang->set_gate) |
| 940 | bitbang->set_gate(bitbang->addr); |
| 941 | |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 942 | if (bit) |
| 943 | bb_set(bitbang->addr, bitbang->mdc_msk); |
| 944 | else |
| 945 | bb_clr(bitbang->addr, bitbang->mdc_msk); |
| 946 | } |
| 947 | |
| 948 | /* mdio bus control struct */ |
| 949 | static struct mdiobb_ops bb_ops = { |
| 950 | .owner = THIS_MODULE, |
| 951 | .set_mdc = sh_mdc_ctrl, |
| 952 | .set_mdio_dir = sh_mmd_ctrl, |
| 953 | .set_mdio_data = sh_set_mdio, |
| 954 | .get_mdio_data = sh_get_mdio, |
| 955 | }; |
| 956 | |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 957 | /* free skb and descriptor buffer */ |
| 958 | static void sh_eth_ring_free(struct net_device *ndev) |
| 959 | { |
| 960 | struct sh_eth_private *mdp = netdev_priv(ndev); |
| 961 | int i; |
| 962 | |
| 963 | /* Free Rx skb ringbuffer */ |
| 964 | if (mdp->rx_skbuff) { |
Yoshihiro Shimoda | 525b807 | 2012-06-26 20:00:03 +0000 | [diff] [blame] | 965 | for (i = 0; i < mdp->num_rx_ring; i++) { |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 966 | if (mdp->rx_skbuff[i]) |
| 967 | dev_kfree_skb(mdp->rx_skbuff[i]); |
| 968 | } |
| 969 | } |
| 970 | kfree(mdp->rx_skbuff); |
Yoshihiro Shimoda | 91c7755 | 2012-06-26 20:00:01 +0000 | [diff] [blame] | 971 | mdp->rx_skbuff = NULL; |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 972 | |
| 973 | /* Free Tx skb ringbuffer */ |
| 974 | if (mdp->tx_skbuff) { |
Yoshihiro Shimoda | 525b807 | 2012-06-26 20:00:03 +0000 | [diff] [blame] | 975 | for (i = 0; i < mdp->num_tx_ring; i++) { |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 976 | if (mdp->tx_skbuff[i]) |
| 977 | dev_kfree_skb(mdp->tx_skbuff[i]); |
| 978 | } |
| 979 | } |
| 980 | kfree(mdp->tx_skbuff); |
Yoshihiro Shimoda | 91c7755 | 2012-06-26 20:00:01 +0000 | [diff] [blame] | 981 | mdp->tx_skbuff = NULL; |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 982 | } |
| 983 | |
| 984 | /* format skb and descriptor buffer */ |
| 985 | static void sh_eth_ring_format(struct net_device *ndev) |
| 986 | { |
| 987 | struct sh_eth_private *mdp = netdev_priv(ndev); |
| 988 | int i; |
| 989 | struct sk_buff *skb; |
| 990 | struct sh_eth_rxdesc *rxdesc = NULL; |
| 991 | struct sh_eth_txdesc *txdesc = NULL; |
Yoshihiro Shimoda | 525b807 | 2012-06-26 20:00:03 +0000 | [diff] [blame] | 992 | int rx_ringsize = sizeof(*rxdesc) * mdp->num_rx_ring; |
| 993 | int tx_ringsize = sizeof(*txdesc) * mdp->num_tx_ring; |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 994 | |
| 995 | mdp->cur_rx = mdp->cur_tx = 0; |
| 996 | mdp->dirty_rx = mdp->dirty_tx = 0; |
| 997 | |
| 998 | memset(mdp->rx_ring, 0, rx_ringsize); |
| 999 | |
| 1000 | /* build Rx ring buffer */ |
Yoshihiro Shimoda | 525b807 | 2012-06-26 20:00:03 +0000 | [diff] [blame] | 1001 | for (i = 0; i < mdp->num_rx_ring; i++) { |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1002 | /* skb */ |
| 1003 | mdp->rx_skbuff[i] = NULL; |
Pradeep A. Dalvi | dae2e9f | 2012-02-06 11:16:13 +0000 | [diff] [blame] | 1004 | skb = netdev_alloc_skb(ndev, mdp->rx_buf_sz); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1005 | mdp->rx_skbuff[i] = skb; |
| 1006 | if (skb == NULL) |
| 1007 | break; |
Eric Dumazet | bb7d92e | 2012-02-06 22:17:21 +0000 | [diff] [blame] | 1008 | dma_map_single(&ndev->dev, skb->data, mdp->rx_buf_sz, |
Yoshihiro Shimoda | e88aae7 | 2009-05-24 23:52:35 +0000 | [diff] [blame] | 1009 | DMA_FROM_DEVICE); |
Yoshihiro Shimoda | 380af9e | 2009-05-24 23:54:21 +0000 | [diff] [blame] | 1010 | sh_eth_set_receive_align(skb); |
| 1011 | |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1012 | /* RX descriptor */ |
| 1013 | rxdesc = &mdp->rx_ring[i]; |
Yoshihiro Shimoda | 0029d64 | 2009-05-24 23:53:20 +0000 | [diff] [blame] | 1014 | rxdesc->addr = virt_to_phys(PTR_ALIGN(skb->data, 4)); |
Yoshinori Sato | 71557a3 | 2008-08-06 19:49:00 -0400 | [diff] [blame] | 1015 | rxdesc->status = cpu_to_edmac(mdp, RD_RACT | RD_RFP); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1016 | |
| 1017 | /* The size of the buffer is 16 byte boundary. */ |
Yoshihiro Shimoda | 0029d64 | 2009-05-24 23:53:20 +0000 | [diff] [blame] | 1018 | rxdesc->buffer_length = ALIGN(mdp->rx_buf_sz, 16); |
Nobuhiro Iwamatsu | b0ca2a2 | 2008-06-30 11:08:17 +0900 | [diff] [blame] | 1019 | /* Rx descriptor address set */ |
| 1020 | if (i == 0) { |
Yoshihiro Shimoda | 4a55530 | 2011-03-07 21:59:26 +0000 | [diff] [blame] | 1021 | sh_eth_write(ndev, mdp->rx_desc_dma, RDLAR); |
Yoshihiro Shimoda | c5ed536 | 2011-03-07 21:59:38 +0000 | [diff] [blame] | 1022 | if (sh_eth_is_gether(mdp)) |
| 1023 | sh_eth_write(ndev, mdp->rx_desc_dma, RDFAR); |
Nobuhiro Iwamatsu | b0ca2a2 | 2008-06-30 11:08:17 +0900 | [diff] [blame] | 1024 | } |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1025 | } |
| 1026 | |
Yoshihiro Shimoda | 525b807 | 2012-06-26 20:00:03 +0000 | [diff] [blame] | 1027 | mdp->dirty_rx = (u32) (i - mdp->num_rx_ring); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1028 | |
| 1029 | /* Mark the last entry as wrapping the ring. */ |
Yoshinori Sato | 71557a3 | 2008-08-06 19:49:00 -0400 | [diff] [blame] | 1030 | rxdesc->status |= cpu_to_edmac(mdp, RD_RDEL); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1031 | |
| 1032 | memset(mdp->tx_ring, 0, tx_ringsize); |
| 1033 | |
| 1034 | /* build Tx ring buffer */ |
Yoshihiro Shimoda | 525b807 | 2012-06-26 20:00:03 +0000 | [diff] [blame] | 1035 | for (i = 0; i < mdp->num_tx_ring; i++) { |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1036 | mdp->tx_skbuff[i] = NULL; |
| 1037 | txdesc = &mdp->tx_ring[i]; |
Yoshinori Sato | 71557a3 | 2008-08-06 19:49:00 -0400 | [diff] [blame] | 1038 | txdesc->status = cpu_to_edmac(mdp, TD_TFP); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1039 | txdesc->buffer_length = 0; |
Nobuhiro Iwamatsu | b0ca2a2 | 2008-06-30 11:08:17 +0900 | [diff] [blame] | 1040 | if (i == 0) { |
Yoshinori Sato | 71557a3 | 2008-08-06 19:49:00 -0400 | [diff] [blame] | 1041 | /* Tx descriptor address set */ |
Yoshihiro Shimoda | 4a55530 | 2011-03-07 21:59:26 +0000 | [diff] [blame] | 1042 | sh_eth_write(ndev, mdp->tx_desc_dma, TDLAR); |
Yoshihiro Shimoda | c5ed536 | 2011-03-07 21:59:38 +0000 | [diff] [blame] | 1043 | if (sh_eth_is_gether(mdp)) |
| 1044 | sh_eth_write(ndev, mdp->tx_desc_dma, TDFAR); |
Nobuhiro Iwamatsu | b0ca2a2 | 2008-06-30 11:08:17 +0900 | [diff] [blame] | 1045 | } |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1046 | } |
| 1047 | |
Yoshinori Sato | 71557a3 | 2008-08-06 19:49:00 -0400 | [diff] [blame] | 1048 | txdesc->status |= cpu_to_edmac(mdp, TD_TDLE); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1049 | } |
| 1050 | |
| 1051 | /* Get skb and descriptor buffer */ |
| 1052 | static int sh_eth_ring_init(struct net_device *ndev) |
| 1053 | { |
| 1054 | struct sh_eth_private *mdp = netdev_priv(ndev); |
| 1055 | int rx_ringsize, tx_ringsize, ret = 0; |
| 1056 | |
| 1057 | /* |
| 1058 | * +26 gets the maximum ethernet encapsulation, +7 & ~7 because the |
| 1059 | * card needs room to do 8 byte alignment, +2 so we can reserve |
| 1060 | * the first 2 bytes, and +16 gets room for the status word from the |
| 1061 | * card. |
| 1062 | */ |
| 1063 | mdp->rx_buf_sz = (ndev->mtu <= 1492 ? PKT_BUF_SZ : |
| 1064 | (((ndev->mtu + 26 + 7) & ~7) + 2 + 16)); |
Magnus Damm | 503914c | 2009-12-15 21:16:55 -0800 | [diff] [blame] | 1065 | if (mdp->cd->rpadir) |
| 1066 | mdp->rx_buf_sz += NET_IP_ALIGN; |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1067 | |
| 1068 | /* Allocate RX and TX skb rings */ |
Joe Perches | b2adaca | 2013-02-03 17:43:58 +0000 | [diff] [blame] | 1069 | mdp->rx_skbuff = kmalloc_array(mdp->num_rx_ring, |
| 1070 | sizeof(*mdp->rx_skbuff), GFP_KERNEL); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1071 | if (!mdp->rx_skbuff) { |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1072 | ret = -ENOMEM; |
| 1073 | return ret; |
| 1074 | } |
| 1075 | |
Joe Perches | b2adaca | 2013-02-03 17:43:58 +0000 | [diff] [blame] | 1076 | mdp->tx_skbuff = kmalloc_array(mdp->num_tx_ring, |
| 1077 | sizeof(*mdp->tx_skbuff), GFP_KERNEL); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1078 | if (!mdp->tx_skbuff) { |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1079 | ret = -ENOMEM; |
| 1080 | goto skb_ring_free; |
| 1081 | } |
| 1082 | |
| 1083 | /* Allocate all Rx descriptors. */ |
Yoshihiro Shimoda | 525b807 | 2012-06-26 20:00:03 +0000 | [diff] [blame] | 1084 | rx_ringsize = sizeof(struct sh_eth_rxdesc) * mdp->num_rx_ring; |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1085 | 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] | 1086 | GFP_KERNEL); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1087 | if (!mdp->rx_ring) { |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1088 | ret = -ENOMEM; |
| 1089 | goto desc_ring_free; |
| 1090 | } |
| 1091 | |
| 1092 | mdp->dirty_rx = 0; |
| 1093 | |
| 1094 | /* Allocate all Tx descriptors. */ |
Yoshihiro Shimoda | 525b807 | 2012-06-26 20:00:03 +0000 | [diff] [blame] | 1095 | tx_ringsize = sizeof(struct sh_eth_txdesc) * mdp->num_tx_ring; |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1096 | 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] | 1097 | GFP_KERNEL); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1098 | if (!mdp->tx_ring) { |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1099 | ret = -ENOMEM; |
| 1100 | goto desc_ring_free; |
| 1101 | } |
| 1102 | return ret; |
| 1103 | |
| 1104 | desc_ring_free: |
| 1105 | /* free DMA buffer */ |
| 1106 | dma_free_coherent(NULL, rx_ringsize, mdp->rx_ring, mdp->rx_desc_dma); |
| 1107 | |
| 1108 | skb_ring_free: |
| 1109 | /* Free Rx and Tx skb ring buffer */ |
| 1110 | sh_eth_ring_free(ndev); |
Yoshihiro Shimoda | 91c7755 | 2012-06-26 20:00:01 +0000 | [diff] [blame] | 1111 | mdp->tx_ring = NULL; |
| 1112 | mdp->rx_ring = NULL; |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1113 | |
| 1114 | return ret; |
| 1115 | } |
| 1116 | |
Yoshihiro Shimoda | 91c7755 | 2012-06-26 20:00:01 +0000 | [diff] [blame] | 1117 | static void sh_eth_free_dma_buffer(struct sh_eth_private *mdp) |
| 1118 | { |
| 1119 | int ringsize; |
| 1120 | |
| 1121 | if (mdp->rx_ring) { |
Yoshihiro Shimoda | 525b807 | 2012-06-26 20:00:03 +0000 | [diff] [blame] | 1122 | ringsize = sizeof(struct sh_eth_rxdesc) * mdp->num_rx_ring; |
Yoshihiro Shimoda | 91c7755 | 2012-06-26 20:00:01 +0000 | [diff] [blame] | 1123 | dma_free_coherent(NULL, ringsize, mdp->rx_ring, |
| 1124 | mdp->rx_desc_dma); |
| 1125 | mdp->rx_ring = NULL; |
| 1126 | } |
| 1127 | |
| 1128 | if (mdp->tx_ring) { |
Yoshihiro Shimoda | 525b807 | 2012-06-26 20:00:03 +0000 | [diff] [blame] | 1129 | ringsize = sizeof(struct sh_eth_txdesc) * mdp->num_tx_ring; |
Yoshihiro Shimoda | 91c7755 | 2012-06-26 20:00:01 +0000 | [diff] [blame] | 1130 | dma_free_coherent(NULL, ringsize, mdp->tx_ring, |
| 1131 | mdp->tx_desc_dma); |
| 1132 | mdp->tx_ring = NULL; |
| 1133 | } |
| 1134 | } |
| 1135 | |
Yoshihiro Shimoda | 525b807 | 2012-06-26 20:00:03 +0000 | [diff] [blame] | 1136 | static int sh_eth_dev_init(struct net_device *ndev, bool start) |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1137 | { |
| 1138 | int ret = 0; |
| 1139 | struct sh_eth_private *mdp = netdev_priv(ndev); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1140 | u32 val; |
| 1141 | |
| 1142 | /* Soft Reset */ |
Nobuhiro Iwamatsu | 5cee1d3 | 2012-06-25 17:35:12 +0000 | [diff] [blame] | 1143 | ret = sh_eth_reset(ndev); |
| 1144 | if (ret) |
| 1145 | goto out; |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1146 | |
Nobuhiro Iwamatsu | b0ca2a2 | 2008-06-30 11:08:17 +0900 | [diff] [blame] | 1147 | /* Descriptor format */ |
| 1148 | sh_eth_ring_format(ndev); |
Yoshihiro Shimoda | 380af9e | 2009-05-24 23:54:21 +0000 | [diff] [blame] | 1149 | if (mdp->cd->rpadir) |
Yoshihiro Shimoda | 4a55530 | 2011-03-07 21:59:26 +0000 | [diff] [blame] | 1150 | sh_eth_write(ndev, mdp->cd->rpadir_value, RPADIR); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1151 | |
| 1152 | /* all sh_eth int mask */ |
Yoshihiro Shimoda | 4a55530 | 2011-03-07 21:59:26 +0000 | [diff] [blame] | 1153 | sh_eth_write(ndev, 0, EESIPR); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1154 | |
Yoshihiro Shimoda | 10b9194 | 2012-03-29 19:32:08 +0000 | [diff] [blame] | 1155 | #if defined(__LITTLE_ENDIAN) |
Yoshihiro Shimoda | 380af9e | 2009-05-24 23:54:21 +0000 | [diff] [blame] | 1156 | if (mdp->cd->hw_swap) |
Yoshihiro Shimoda | 4a55530 | 2011-03-07 21:59:26 +0000 | [diff] [blame] | 1157 | sh_eth_write(ndev, EDMR_EL, EDMR); |
Yoshihiro Shimoda | 380af9e | 2009-05-24 23:54:21 +0000 | [diff] [blame] | 1158 | else |
Nobuhiro Iwamatsu | b0ca2a2 | 2008-06-30 11:08:17 +0900 | [diff] [blame] | 1159 | #endif |
Yoshihiro Shimoda | 4a55530 | 2011-03-07 21:59:26 +0000 | [diff] [blame] | 1160 | sh_eth_write(ndev, 0, EDMR); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1161 | |
Nobuhiro Iwamatsu | b0ca2a2 | 2008-06-30 11:08:17 +0900 | [diff] [blame] | 1162 | /* FIFO size set */ |
Yoshihiro Shimoda | 4a55530 | 2011-03-07 21:59:26 +0000 | [diff] [blame] | 1163 | sh_eth_write(ndev, mdp->cd->fdr_value, FDR); |
| 1164 | sh_eth_write(ndev, 0, TFTR); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1165 | |
Nobuhiro Iwamatsu | b0ca2a2 | 2008-06-30 11:08:17 +0900 | [diff] [blame] | 1166 | /* Frame recv control */ |
Yoshihiro Shimoda | 4a55530 | 2011-03-07 21:59:26 +0000 | [diff] [blame] | 1167 | sh_eth_write(ndev, mdp->cd->rmcr_value, RMCR); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1168 | |
Yoshihiro Shimoda | 2ecbb78 | 2012-06-26 19:59:58 +0000 | [diff] [blame] | 1169 | sh_eth_write(ndev, DESC_I_RINT8 | DESC_I_RINT5 | DESC_I_TINT2, TRSCER); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1170 | |
Yoshihiro Shimoda | 380af9e | 2009-05-24 23:54:21 +0000 | [diff] [blame] | 1171 | if (mdp->cd->bculr) |
Yoshihiro Shimoda | 4a55530 | 2011-03-07 21:59:26 +0000 | [diff] [blame] | 1172 | sh_eth_write(ndev, 0x800, BCULR); /* Burst sycle set */ |
Nobuhiro Iwamatsu | b0ca2a2 | 2008-06-30 11:08:17 +0900 | [diff] [blame] | 1173 | |
Yoshihiro Shimoda | 4a55530 | 2011-03-07 21:59:26 +0000 | [diff] [blame] | 1174 | sh_eth_write(ndev, mdp->cd->fcftr_value, FCFTR); |
Nobuhiro Iwamatsu | b0ca2a2 | 2008-06-30 11:08:17 +0900 | [diff] [blame] | 1175 | |
Yoshihiro Shimoda | 380af9e | 2009-05-24 23:54:21 +0000 | [diff] [blame] | 1176 | if (!mdp->cd->no_trimd) |
Yoshihiro Shimoda | 4a55530 | 2011-03-07 21:59:26 +0000 | [diff] [blame] | 1177 | sh_eth_write(ndev, 0, TRIMD); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1178 | |
Nobuhiro Iwamatsu | b0ca2a2 | 2008-06-30 11:08:17 +0900 | [diff] [blame] | 1179 | /* Recv frame limit set register */ |
Yoshihiro Shimoda | fdb37a7 | 2012-02-06 23:55:15 +0000 | [diff] [blame] | 1180 | sh_eth_write(ndev, ndev->mtu + ETH_HLEN + VLAN_HLEN + ETH_FCS_LEN, |
| 1181 | RFLR); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1182 | |
Yoshihiro Shimoda | 4a55530 | 2011-03-07 21:59:26 +0000 | [diff] [blame] | 1183 | sh_eth_write(ndev, sh_eth_read(ndev, EESR), EESR); |
Yoshihiro Shimoda | 525b807 | 2012-06-26 20:00:03 +0000 | [diff] [blame] | 1184 | if (start) |
| 1185 | sh_eth_write(ndev, mdp->cd->eesipr_value, EESIPR); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1186 | |
| 1187 | /* PAUSE Prohibition */ |
Yoshihiro Shimoda | 4a55530 | 2011-03-07 21:59:26 +0000 | [diff] [blame] | 1188 | val = (sh_eth_read(ndev, ECMR) & ECMR_DM) | |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1189 | ECMR_ZPF | (mdp->duplex ? ECMR_DM : 0) | ECMR_TE | ECMR_RE; |
| 1190 | |
Yoshihiro Shimoda | 4a55530 | 2011-03-07 21:59:26 +0000 | [diff] [blame] | 1191 | sh_eth_write(ndev, val, ECMR); |
Nobuhiro Iwamatsu | b0ca2a2 | 2008-06-30 11:08:17 +0900 | [diff] [blame] | 1192 | |
Yoshihiro Shimoda | 380af9e | 2009-05-24 23:54:21 +0000 | [diff] [blame] | 1193 | if (mdp->cd->set_rate) |
| 1194 | mdp->cd->set_rate(ndev); |
| 1195 | |
Nobuhiro Iwamatsu | b0ca2a2 | 2008-06-30 11:08:17 +0900 | [diff] [blame] | 1196 | /* E-MAC Status Register clear */ |
Yoshihiro Shimoda | 4a55530 | 2011-03-07 21:59:26 +0000 | [diff] [blame] | 1197 | sh_eth_write(ndev, mdp->cd->ecsr_value, ECSR); |
Nobuhiro Iwamatsu | b0ca2a2 | 2008-06-30 11:08:17 +0900 | [diff] [blame] | 1198 | |
| 1199 | /* E-MAC Interrupt Enable register */ |
Yoshihiro Shimoda | 525b807 | 2012-06-26 20:00:03 +0000 | [diff] [blame] | 1200 | if (start) |
| 1201 | sh_eth_write(ndev, mdp->cd->ecsipr_value, ECSIPR); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1202 | |
| 1203 | /* Set MAC address */ |
| 1204 | update_mac_address(ndev); |
| 1205 | |
| 1206 | /* mask reset */ |
Yoshihiro Shimoda | 380af9e | 2009-05-24 23:54:21 +0000 | [diff] [blame] | 1207 | if (mdp->cd->apr) |
Yoshihiro Shimoda | 4a55530 | 2011-03-07 21:59:26 +0000 | [diff] [blame] | 1208 | sh_eth_write(ndev, APR_AP, APR); |
Yoshihiro Shimoda | 380af9e | 2009-05-24 23:54:21 +0000 | [diff] [blame] | 1209 | if (mdp->cd->mpr) |
Yoshihiro Shimoda | 4a55530 | 2011-03-07 21:59:26 +0000 | [diff] [blame] | 1210 | sh_eth_write(ndev, MPR_MP, MPR); |
Yoshihiro Shimoda | 380af9e | 2009-05-24 23:54:21 +0000 | [diff] [blame] | 1211 | if (mdp->cd->tpauser) |
Yoshihiro Shimoda | 4a55530 | 2011-03-07 21:59:26 +0000 | [diff] [blame] | 1212 | sh_eth_write(ndev, TPAUSER_UNLIMITED, TPAUSER); |
Nobuhiro Iwamatsu | b0ca2a2 | 2008-06-30 11:08:17 +0900 | [diff] [blame] | 1213 | |
Yoshihiro Shimoda | 525b807 | 2012-06-26 20:00:03 +0000 | [diff] [blame] | 1214 | if (start) { |
| 1215 | /* Setting the Rx mode will start the Rx process. */ |
| 1216 | sh_eth_write(ndev, EDRRR_R, EDRRR); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1217 | |
Yoshihiro Shimoda | 525b807 | 2012-06-26 20:00:03 +0000 | [diff] [blame] | 1218 | netif_start_queue(ndev); |
| 1219 | } |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1220 | |
Nobuhiro Iwamatsu | 5cee1d3 | 2012-06-25 17:35:12 +0000 | [diff] [blame] | 1221 | out: |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1222 | return ret; |
| 1223 | } |
| 1224 | |
| 1225 | /* free Tx skb function */ |
| 1226 | static int sh_eth_txfree(struct net_device *ndev) |
| 1227 | { |
| 1228 | struct sh_eth_private *mdp = netdev_priv(ndev); |
| 1229 | struct sh_eth_txdesc *txdesc; |
| 1230 | int freeNum = 0; |
| 1231 | int entry = 0; |
| 1232 | |
| 1233 | for (; mdp->cur_tx - mdp->dirty_tx > 0; mdp->dirty_tx++) { |
Yoshihiro Shimoda | 525b807 | 2012-06-26 20:00:03 +0000 | [diff] [blame] | 1234 | entry = mdp->dirty_tx % mdp->num_tx_ring; |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1235 | txdesc = &mdp->tx_ring[entry]; |
Yoshinori Sato | 71557a3 | 2008-08-06 19:49:00 -0400 | [diff] [blame] | 1236 | if (txdesc->status & cpu_to_edmac(mdp, TD_TACT)) |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1237 | break; |
| 1238 | /* Free the original skb. */ |
| 1239 | if (mdp->tx_skbuff[entry]) { |
Yoshihiro Shimoda | 31fcb99 | 2011-06-30 22:52:13 +0000 | [diff] [blame] | 1240 | dma_unmap_single(&ndev->dev, txdesc->addr, |
| 1241 | txdesc->buffer_length, DMA_TO_DEVICE); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1242 | dev_kfree_skb_irq(mdp->tx_skbuff[entry]); |
| 1243 | mdp->tx_skbuff[entry] = NULL; |
| 1244 | freeNum++; |
| 1245 | } |
Yoshinori Sato | 71557a3 | 2008-08-06 19:49:00 -0400 | [diff] [blame] | 1246 | txdesc->status = cpu_to_edmac(mdp, TD_TFP); |
Yoshihiro Shimoda | 525b807 | 2012-06-26 20:00:03 +0000 | [diff] [blame] | 1247 | if (entry >= mdp->num_tx_ring - 1) |
Yoshinori Sato | 71557a3 | 2008-08-06 19:49:00 -0400 | [diff] [blame] | 1248 | txdesc->status |= cpu_to_edmac(mdp, TD_TDLE); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1249 | |
Eric Dumazet | bb7d92e | 2012-02-06 22:17:21 +0000 | [diff] [blame] | 1250 | ndev->stats.tx_packets++; |
| 1251 | ndev->stats.tx_bytes += txdesc->buffer_length; |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1252 | } |
| 1253 | return freeNum; |
| 1254 | } |
| 1255 | |
| 1256 | /* Packet receive function */ |
Yoshihiro Shimoda | a18e08b | 2012-06-20 15:26:34 +0000 | [diff] [blame] | 1257 | static int sh_eth_rx(struct net_device *ndev, u32 intr_status) |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1258 | { |
| 1259 | struct sh_eth_private *mdp = netdev_priv(ndev); |
| 1260 | struct sh_eth_rxdesc *rxdesc; |
| 1261 | |
Yoshihiro Shimoda | 525b807 | 2012-06-26 20:00:03 +0000 | [diff] [blame] | 1262 | int entry = mdp->cur_rx % mdp->num_rx_ring; |
| 1263 | int boguscnt = (mdp->dirty_rx + mdp->num_rx_ring) - mdp->cur_rx; |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1264 | struct sk_buff *skb; |
| 1265 | u16 pkt_len = 0; |
Yoshihiro Shimoda | 380af9e | 2009-05-24 23:54:21 +0000 | [diff] [blame] | 1266 | u32 desc_status; |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1267 | |
| 1268 | rxdesc = &mdp->rx_ring[entry]; |
Yoshinori Sato | 71557a3 | 2008-08-06 19:49:00 -0400 | [diff] [blame] | 1269 | while (!(rxdesc->status & cpu_to_edmac(mdp, RD_RACT))) { |
| 1270 | desc_status = edmac_to_cpu(mdp, rxdesc->status); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1271 | pkt_len = rxdesc->frame_length; |
| 1272 | |
Yoshihiro Shimoda | 73a0d90 | 2012-04-04 18:37:10 +0000 | [diff] [blame] | 1273 | #if defined(CONFIG_ARCH_R8A7740) |
| 1274 | desc_status >>= 16; |
| 1275 | #endif |
| 1276 | |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1277 | if (--boguscnt < 0) |
| 1278 | break; |
| 1279 | |
| 1280 | if (!(desc_status & RDFEND)) |
Eric Dumazet | bb7d92e | 2012-02-06 22:17:21 +0000 | [diff] [blame] | 1281 | ndev->stats.rx_length_errors++; |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1282 | |
| 1283 | if (desc_status & (RD_RFS1 | RD_RFS2 | RD_RFS3 | RD_RFS4 | |
| 1284 | RD_RFS5 | RD_RFS6 | RD_RFS10)) { |
Eric Dumazet | bb7d92e | 2012-02-06 22:17:21 +0000 | [diff] [blame] | 1285 | ndev->stats.rx_errors++; |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1286 | if (desc_status & RD_RFS1) |
Eric Dumazet | bb7d92e | 2012-02-06 22:17:21 +0000 | [diff] [blame] | 1287 | ndev->stats.rx_crc_errors++; |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1288 | if (desc_status & RD_RFS2) |
Eric Dumazet | bb7d92e | 2012-02-06 22:17:21 +0000 | [diff] [blame] | 1289 | ndev->stats.rx_frame_errors++; |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1290 | if (desc_status & RD_RFS3) |
Eric Dumazet | bb7d92e | 2012-02-06 22:17:21 +0000 | [diff] [blame] | 1291 | ndev->stats.rx_length_errors++; |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1292 | if (desc_status & RD_RFS4) |
Eric Dumazet | bb7d92e | 2012-02-06 22:17:21 +0000 | [diff] [blame] | 1293 | ndev->stats.rx_length_errors++; |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1294 | if (desc_status & RD_RFS6) |
Eric Dumazet | bb7d92e | 2012-02-06 22:17:21 +0000 | [diff] [blame] | 1295 | ndev->stats.rx_missed_errors++; |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1296 | if (desc_status & RD_RFS10) |
Eric Dumazet | bb7d92e | 2012-02-06 22:17:21 +0000 | [diff] [blame] | 1297 | ndev->stats.rx_over_errors++; |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1298 | } else { |
Yoshihiro Shimoda | 380af9e | 2009-05-24 23:54:21 +0000 | [diff] [blame] | 1299 | if (!mdp->cd->hw_swap) |
| 1300 | sh_eth_soft_swap( |
| 1301 | phys_to_virt(ALIGN(rxdesc->addr, 4)), |
| 1302 | pkt_len + 2); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1303 | skb = mdp->rx_skbuff[entry]; |
| 1304 | mdp->rx_skbuff[entry] = NULL; |
Magnus Damm | 503914c | 2009-12-15 21:16:55 -0800 | [diff] [blame] | 1305 | if (mdp->cd->rpadir) |
| 1306 | skb_reserve(skb, NET_IP_ALIGN); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1307 | skb_put(skb, pkt_len); |
| 1308 | skb->protocol = eth_type_trans(skb, ndev); |
| 1309 | netif_rx(skb); |
Eric Dumazet | bb7d92e | 2012-02-06 22:17:21 +0000 | [diff] [blame] | 1310 | ndev->stats.rx_packets++; |
| 1311 | ndev->stats.rx_bytes += pkt_len; |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1312 | } |
Yoshinori Sato | 71557a3 | 2008-08-06 19:49:00 -0400 | [diff] [blame] | 1313 | rxdesc->status |= cpu_to_edmac(mdp, RD_RACT); |
Yoshihiro Shimoda | 525b807 | 2012-06-26 20:00:03 +0000 | [diff] [blame] | 1314 | entry = (++mdp->cur_rx) % mdp->num_rx_ring; |
Yoshihiro Shimoda | 862df49 | 2009-05-24 23:53:40 +0000 | [diff] [blame] | 1315 | rxdesc = &mdp->rx_ring[entry]; |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1316 | } |
| 1317 | |
| 1318 | /* Refill the Rx ring buffers. */ |
| 1319 | for (; mdp->cur_rx - mdp->dirty_rx > 0; mdp->dirty_rx++) { |
Yoshihiro Shimoda | 525b807 | 2012-06-26 20:00:03 +0000 | [diff] [blame] | 1320 | entry = mdp->dirty_rx % mdp->num_rx_ring; |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1321 | rxdesc = &mdp->rx_ring[entry]; |
Nobuhiro Iwamatsu | b0ca2a2 | 2008-06-30 11:08:17 +0900 | [diff] [blame] | 1322 | /* The size of the buffer is 16 byte boundary. */ |
Yoshihiro Shimoda | 0029d64 | 2009-05-24 23:53:20 +0000 | [diff] [blame] | 1323 | rxdesc->buffer_length = ALIGN(mdp->rx_buf_sz, 16); |
Nobuhiro Iwamatsu | b0ca2a2 | 2008-06-30 11:08:17 +0900 | [diff] [blame] | 1324 | |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1325 | if (mdp->rx_skbuff[entry] == NULL) { |
Pradeep A. Dalvi | dae2e9f | 2012-02-06 11:16:13 +0000 | [diff] [blame] | 1326 | skb = netdev_alloc_skb(ndev, mdp->rx_buf_sz); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1327 | mdp->rx_skbuff[entry] = skb; |
| 1328 | if (skb == NULL) |
| 1329 | break; /* Better luck next round. */ |
Eric Dumazet | bb7d92e | 2012-02-06 22:17:21 +0000 | [diff] [blame] | 1330 | dma_map_single(&ndev->dev, skb->data, mdp->rx_buf_sz, |
Yoshihiro Shimoda | e88aae7 | 2009-05-24 23:52:35 +0000 | [diff] [blame] | 1331 | DMA_FROM_DEVICE); |
Yoshihiro Shimoda | 380af9e | 2009-05-24 23:54:21 +0000 | [diff] [blame] | 1332 | sh_eth_set_receive_align(skb); |
| 1333 | |
Eric Dumazet | bc8acf2 | 2010-09-02 13:07:41 -0700 | [diff] [blame] | 1334 | skb_checksum_none_assert(skb); |
Yoshihiro Shimoda | 0029d64 | 2009-05-24 23:53:20 +0000 | [diff] [blame] | 1335 | rxdesc->addr = virt_to_phys(PTR_ALIGN(skb->data, 4)); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1336 | } |
Yoshihiro Shimoda | 525b807 | 2012-06-26 20:00:03 +0000 | [diff] [blame] | 1337 | if (entry >= mdp->num_rx_ring - 1) |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1338 | rxdesc->status |= |
Yoshinori Sato | 71557a3 | 2008-08-06 19:49:00 -0400 | [diff] [blame] | 1339 | cpu_to_edmac(mdp, RD_RACT | RD_RFP | RD_RDEL); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1340 | else |
| 1341 | rxdesc->status |= |
Yoshinori Sato | 71557a3 | 2008-08-06 19:49:00 -0400 | [diff] [blame] | 1342 | cpu_to_edmac(mdp, RD_RACT | RD_RFP); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1343 | } |
| 1344 | |
| 1345 | /* Restart Rx engine if stopped. */ |
| 1346 | /* If we don't need to check status, don't. -KDU */ |
Yoshihiro Shimoda | 79fba9f | 2012-05-28 23:07:55 +0000 | [diff] [blame] | 1347 | if (!(sh_eth_read(ndev, EDRRR) & EDRRR_R)) { |
Yoshihiro Shimoda | a18e08b | 2012-06-20 15:26:34 +0000 | [diff] [blame] | 1348 | /* fix the values for the next receiving if RDE is set */ |
| 1349 | if (intr_status & EESR_RDE) |
| 1350 | mdp->cur_rx = mdp->dirty_rx = |
| 1351 | (sh_eth_read(ndev, RDFAR) - |
| 1352 | sh_eth_read(ndev, RDLAR)) >> 4; |
Yoshihiro Shimoda | 4a55530 | 2011-03-07 21:59:26 +0000 | [diff] [blame] | 1353 | sh_eth_write(ndev, EDRRR_R, EDRRR); |
Yoshihiro Shimoda | 79fba9f | 2012-05-28 23:07:55 +0000 | [diff] [blame] | 1354 | } |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1355 | |
| 1356 | return 0; |
| 1357 | } |
| 1358 | |
Yoshihiro Shimoda | 4a55530 | 2011-03-07 21:59:26 +0000 | [diff] [blame] | 1359 | static void sh_eth_rcv_snd_disable(struct net_device *ndev) |
Nobuhiro Iwamatsu | dc19e4e | 2011-02-15 21:17:32 +0000 | [diff] [blame] | 1360 | { |
| 1361 | /* disable tx and rx */ |
Yoshihiro Shimoda | 4a55530 | 2011-03-07 21:59:26 +0000 | [diff] [blame] | 1362 | sh_eth_write(ndev, sh_eth_read(ndev, ECMR) & |
| 1363 | ~(ECMR_RE | ECMR_TE), ECMR); |
Nobuhiro Iwamatsu | dc19e4e | 2011-02-15 21:17:32 +0000 | [diff] [blame] | 1364 | } |
| 1365 | |
Yoshihiro Shimoda | 4a55530 | 2011-03-07 21:59:26 +0000 | [diff] [blame] | 1366 | static void sh_eth_rcv_snd_enable(struct net_device *ndev) |
Nobuhiro Iwamatsu | dc19e4e | 2011-02-15 21:17:32 +0000 | [diff] [blame] | 1367 | { |
| 1368 | /* enable tx and rx */ |
Yoshihiro Shimoda | 4a55530 | 2011-03-07 21:59:26 +0000 | [diff] [blame] | 1369 | sh_eth_write(ndev, sh_eth_read(ndev, ECMR) | |
| 1370 | (ECMR_RE | ECMR_TE), ECMR); |
Nobuhiro Iwamatsu | dc19e4e | 2011-02-15 21:17:32 +0000 | [diff] [blame] | 1371 | } |
| 1372 | |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1373 | /* error control function */ |
| 1374 | static void sh_eth_error(struct net_device *ndev, int intr_status) |
| 1375 | { |
| 1376 | struct sh_eth_private *mdp = netdev_priv(ndev); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1377 | u32 felic_stat; |
Yoshihiro Shimoda | 380af9e | 2009-05-24 23:54:21 +0000 | [diff] [blame] | 1378 | u32 link_stat; |
| 1379 | u32 mask; |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1380 | |
| 1381 | if (intr_status & EESR_ECI) { |
Yoshihiro Shimoda | 4a55530 | 2011-03-07 21:59:26 +0000 | [diff] [blame] | 1382 | felic_stat = sh_eth_read(ndev, ECSR); |
| 1383 | sh_eth_write(ndev, felic_stat, ECSR); /* clear int */ |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1384 | if (felic_stat & ECSR_ICD) |
Eric Dumazet | bb7d92e | 2012-02-06 22:17:21 +0000 | [diff] [blame] | 1385 | ndev->stats.tx_carrier_errors++; |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1386 | if (felic_stat & ECSR_LCHNG) { |
| 1387 | /* Link Changed */ |
Yoshihiro Shimoda | 4923576 | 2009-08-27 23:25:03 +0000 | [diff] [blame] | 1388 | if (mdp->cd->no_psr || mdp->no_ether_link) { |
Sergei Shtylyov | 1e1b812 | 2013-03-31 09:50:07 +0000 | [diff] [blame] | 1389 | goto ignore_link; |
Yoshihiro Shimoda | 380af9e | 2009-05-24 23:54:21 +0000 | [diff] [blame] | 1390 | } else { |
Yoshihiro Shimoda | 4a55530 | 2011-03-07 21:59:26 +0000 | [diff] [blame] | 1391 | link_stat = (sh_eth_read(ndev, PSR)); |
Yoshihiro Shimoda | 4923576 | 2009-08-27 23:25:03 +0000 | [diff] [blame] | 1392 | if (mdp->ether_link_active_low) |
| 1393 | link_stat = ~link_stat; |
Yoshihiro Shimoda | 380af9e | 2009-05-24 23:54:21 +0000 | [diff] [blame] | 1394 | } |
Nobuhiro Iwamatsu | dc19e4e | 2011-02-15 21:17:32 +0000 | [diff] [blame] | 1395 | if (!(link_stat & PHY_ST_LINK)) |
Yoshihiro Shimoda | 4a55530 | 2011-03-07 21:59:26 +0000 | [diff] [blame] | 1396 | sh_eth_rcv_snd_disable(ndev); |
Nobuhiro Iwamatsu | dc19e4e | 2011-02-15 21:17:32 +0000 | [diff] [blame] | 1397 | else { |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1398 | /* Link Up */ |
Yoshihiro Shimoda | 4a55530 | 2011-03-07 21:59:26 +0000 | [diff] [blame] | 1399 | sh_eth_write(ndev, sh_eth_read(ndev, EESIPR) & |
| 1400 | ~DMAC_M_ECI, EESIPR); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1401 | /*clear int */ |
Yoshihiro Shimoda | 4a55530 | 2011-03-07 21:59:26 +0000 | [diff] [blame] | 1402 | sh_eth_write(ndev, sh_eth_read(ndev, ECSR), |
| 1403 | ECSR); |
| 1404 | sh_eth_write(ndev, sh_eth_read(ndev, EESIPR) | |
| 1405 | DMAC_M_ECI, EESIPR); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1406 | /* enable tx and rx */ |
Yoshihiro Shimoda | 4a55530 | 2011-03-07 21:59:26 +0000 | [diff] [blame] | 1407 | sh_eth_rcv_snd_enable(ndev); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1408 | } |
| 1409 | } |
| 1410 | } |
| 1411 | |
Sergei Shtylyov | 1e1b812 | 2013-03-31 09:50:07 +0000 | [diff] [blame] | 1412 | ignore_link: |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1413 | if (intr_status & EESR_TWB) { |
| 1414 | /* Write buck end. unused write back interrupt */ |
| 1415 | if (intr_status & EESR_TABT) /* Transmit Abort int */ |
Eric Dumazet | bb7d92e | 2012-02-06 22:17:21 +0000 | [diff] [blame] | 1416 | ndev->stats.tx_aborted_errors++; |
Nobuhiro Iwamatsu | dc19e4e | 2011-02-15 21:17:32 +0000 | [diff] [blame] | 1417 | if (netif_msg_tx_err(mdp)) |
| 1418 | dev_err(&ndev->dev, "Transmit Abort\n"); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1419 | } |
| 1420 | |
| 1421 | if (intr_status & EESR_RABT) { |
| 1422 | /* Receive Abort int */ |
| 1423 | if (intr_status & EESR_RFRMER) { |
| 1424 | /* Receive Frame Overflow int */ |
Eric Dumazet | bb7d92e | 2012-02-06 22:17:21 +0000 | [diff] [blame] | 1425 | ndev->stats.rx_frame_errors++; |
Nobuhiro Iwamatsu | dc19e4e | 2011-02-15 21:17:32 +0000 | [diff] [blame] | 1426 | if (netif_msg_rx_err(mdp)) |
| 1427 | dev_err(&ndev->dev, "Receive Abort\n"); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1428 | } |
| 1429 | } |
Yoshihiro Shimoda | 380af9e | 2009-05-24 23:54:21 +0000 | [diff] [blame] | 1430 | |
Nobuhiro Iwamatsu | dc19e4e | 2011-02-15 21:17:32 +0000 | [diff] [blame] | 1431 | if (intr_status & EESR_TDE) { |
| 1432 | /* Transmit Descriptor Empty int */ |
Eric Dumazet | bb7d92e | 2012-02-06 22:17:21 +0000 | [diff] [blame] | 1433 | ndev->stats.tx_fifo_errors++; |
Nobuhiro Iwamatsu | dc19e4e | 2011-02-15 21:17:32 +0000 | [diff] [blame] | 1434 | if (netif_msg_tx_err(mdp)) |
| 1435 | dev_err(&ndev->dev, "Transmit Descriptor Empty\n"); |
| 1436 | } |
| 1437 | |
| 1438 | if (intr_status & EESR_TFE) { |
| 1439 | /* FIFO under flow */ |
Eric Dumazet | bb7d92e | 2012-02-06 22:17:21 +0000 | [diff] [blame] | 1440 | ndev->stats.tx_fifo_errors++; |
Nobuhiro Iwamatsu | dc19e4e | 2011-02-15 21:17:32 +0000 | [diff] [blame] | 1441 | if (netif_msg_tx_err(mdp)) |
| 1442 | dev_err(&ndev->dev, "Transmit FIFO Under flow\n"); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1443 | } |
| 1444 | |
| 1445 | if (intr_status & EESR_RDE) { |
| 1446 | /* Receive Descriptor Empty int */ |
Eric Dumazet | bb7d92e | 2012-02-06 22:17:21 +0000 | [diff] [blame] | 1447 | ndev->stats.rx_over_errors++; |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1448 | |
Nobuhiro Iwamatsu | dc19e4e | 2011-02-15 21:17:32 +0000 | [diff] [blame] | 1449 | if (netif_msg_rx_err(mdp)) |
| 1450 | dev_err(&ndev->dev, "Receive Descriptor Empty\n"); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1451 | } |
Nobuhiro Iwamatsu | dc19e4e | 2011-02-15 21:17:32 +0000 | [diff] [blame] | 1452 | |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1453 | if (intr_status & EESR_RFE) { |
| 1454 | /* Receive FIFO Overflow int */ |
Eric Dumazet | bb7d92e | 2012-02-06 22:17:21 +0000 | [diff] [blame] | 1455 | ndev->stats.rx_fifo_errors++; |
Nobuhiro Iwamatsu | dc19e4e | 2011-02-15 21:17:32 +0000 | [diff] [blame] | 1456 | if (netif_msg_rx_err(mdp)) |
| 1457 | dev_err(&ndev->dev, "Receive FIFO Overflow\n"); |
| 1458 | } |
| 1459 | |
| 1460 | if (!mdp->cd->no_ade && (intr_status & EESR_ADE)) { |
| 1461 | /* Address Error */ |
Eric Dumazet | bb7d92e | 2012-02-06 22:17:21 +0000 | [diff] [blame] | 1462 | ndev->stats.tx_fifo_errors++; |
Nobuhiro Iwamatsu | dc19e4e | 2011-02-15 21:17:32 +0000 | [diff] [blame] | 1463 | if (netif_msg_tx_err(mdp)) |
| 1464 | dev_err(&ndev->dev, "Address Error\n"); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1465 | } |
Yoshihiro Shimoda | 380af9e | 2009-05-24 23:54:21 +0000 | [diff] [blame] | 1466 | |
| 1467 | mask = EESR_TWB | EESR_TABT | EESR_ADE | EESR_TDE | EESR_TFE; |
| 1468 | if (mdp->cd->no_ade) |
| 1469 | mask &= ~EESR_ADE; |
| 1470 | if (intr_status & mask) { |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1471 | /* Tx error */ |
Yoshihiro Shimoda | 4a55530 | 2011-03-07 21:59:26 +0000 | [diff] [blame] | 1472 | u32 edtrr = sh_eth_read(ndev, EDTRR); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1473 | /* dmesg */ |
Yoshihiro Shimoda | 380af9e | 2009-05-24 23:54:21 +0000 | [diff] [blame] | 1474 | dev_err(&ndev->dev, "TX error. status=%8.8x cur_tx=%8.8x ", |
| 1475 | intr_status, mdp->cur_tx); |
| 1476 | dev_err(&ndev->dev, "dirty_tx=%8.8x state=%8.8x EDTRR=%8.8x.\n", |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1477 | mdp->dirty_tx, (u32) ndev->state, edtrr); |
| 1478 | /* dirty buffer free */ |
| 1479 | sh_eth_txfree(ndev); |
| 1480 | |
| 1481 | /* SH7712 BUG */ |
Yoshihiro Shimoda | c5ed536 | 2011-03-07 21:59:38 +0000 | [diff] [blame] | 1482 | if (edtrr ^ sh_eth_get_edtrr_trns(mdp)) { |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1483 | /* tx dma start */ |
Yoshihiro Shimoda | c5ed536 | 2011-03-07 21:59:38 +0000 | [diff] [blame] | 1484 | sh_eth_write(ndev, sh_eth_get_edtrr_trns(mdp), EDTRR); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1485 | } |
| 1486 | /* wakeup */ |
| 1487 | netif_wake_queue(ndev); |
| 1488 | } |
| 1489 | } |
| 1490 | |
| 1491 | static irqreturn_t sh_eth_interrupt(int irq, void *netdev) |
| 1492 | { |
| 1493 | struct net_device *ndev = netdev; |
| 1494 | struct sh_eth_private *mdp = netdev_priv(ndev); |
Yoshihiro Shimoda | 380af9e | 2009-05-24 23:54:21 +0000 | [diff] [blame] | 1495 | struct sh_eth_cpu_data *cd = mdp->cd; |
Nobuhiro Iwamatsu | 0e0fde3 | 2009-03-16 19:50:57 +0000 | [diff] [blame] | 1496 | irqreturn_t ret = IRQ_NONE; |
Sergei Shtylyov | 3893b27345 | 2013-03-31 09:54:20 +0000 | [diff] [blame] | 1497 | unsigned long intr_status; |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1498 | |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1499 | spin_lock(&mdp->lock); |
| 1500 | |
Sergei Shtylyov | 3893b27345 | 2013-03-31 09:54:20 +0000 | [diff] [blame] | 1501 | /* Get interrupt status */ |
Yoshihiro Shimoda | 4a55530 | 2011-03-07 21:59:26 +0000 | [diff] [blame] | 1502 | intr_status = sh_eth_read(ndev, EESR); |
Sergei Shtylyov | 3893b27345 | 2013-03-31 09:54:20 +0000 | [diff] [blame] | 1503 | /* Mask it with the interrupt mask, forcing ECI interrupt to be always |
| 1504 | * enabled since it's the one that comes thru regardless of the mask, |
| 1505 | * and we need to fully handle it in sh_eth_error() in order to quench |
| 1506 | * it as it doesn't get cleared by just writing 1 to the ECI bit... |
| 1507 | */ |
| 1508 | intr_status &= sh_eth_read(ndev, EESIPR) | DMAC_M_ECI; |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1509 | /* Clear interrupt */ |
Nobuhiro Iwamatsu | 0e0fde3 | 2009-03-16 19:50:57 +0000 | [diff] [blame] | 1510 | if (intr_status & (EESR_FRC | EESR_RMAF | EESR_RRF | |
| 1511 | EESR_RTLF | EESR_RTSF | EESR_PRE | EESR_CERF | |
Yoshihiro Shimoda | 380af9e | 2009-05-24 23:54:21 +0000 | [diff] [blame] | 1512 | cd->tx_check | cd->eesr_err_check)) { |
Yoshihiro Shimoda | 4a55530 | 2011-03-07 21:59:26 +0000 | [diff] [blame] | 1513 | sh_eth_write(ndev, intr_status, EESR); |
Nobuhiro Iwamatsu | 0e0fde3 | 2009-03-16 19:50:57 +0000 | [diff] [blame] | 1514 | ret = IRQ_HANDLED; |
| 1515 | } else |
| 1516 | goto other_irq; |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1517 | |
Nobuhiro Iwamatsu | b0ca2a2 | 2008-06-30 11:08:17 +0900 | [diff] [blame] | 1518 | if (intr_status & (EESR_FRC | /* Frame recv*/ |
| 1519 | EESR_RMAF | /* Multi cast address recv*/ |
| 1520 | EESR_RRF | /* Bit frame recv */ |
| 1521 | EESR_RTLF | /* Long frame recv*/ |
| 1522 | EESR_RTSF | /* short frame recv */ |
| 1523 | EESR_PRE | /* PHY-LSI recv error */ |
| 1524 | EESR_CERF)){ /* recv frame CRC error */ |
Yoshihiro Shimoda | a18e08b | 2012-06-20 15:26:34 +0000 | [diff] [blame] | 1525 | sh_eth_rx(ndev, intr_status); |
Nobuhiro Iwamatsu | b0ca2a2 | 2008-06-30 11:08:17 +0900 | [diff] [blame] | 1526 | } |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1527 | |
Nobuhiro Iwamatsu | b0ca2a2 | 2008-06-30 11:08:17 +0900 | [diff] [blame] | 1528 | /* Tx Check */ |
Yoshihiro Shimoda | 380af9e | 2009-05-24 23:54:21 +0000 | [diff] [blame] | 1529 | if (intr_status & cd->tx_check) { |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1530 | sh_eth_txfree(ndev); |
| 1531 | netif_wake_queue(ndev); |
| 1532 | } |
| 1533 | |
Yoshihiro Shimoda | 380af9e | 2009-05-24 23:54:21 +0000 | [diff] [blame] | 1534 | if (intr_status & cd->eesr_err_check) |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1535 | sh_eth_error(ndev, intr_status); |
| 1536 | |
Nobuhiro Iwamatsu | 0e0fde3 | 2009-03-16 19:50:57 +0000 | [diff] [blame] | 1537 | other_irq: |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1538 | spin_unlock(&mdp->lock); |
| 1539 | |
Nobuhiro Iwamatsu | 0e0fde3 | 2009-03-16 19:50:57 +0000 | [diff] [blame] | 1540 | return ret; |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1541 | } |
| 1542 | |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1543 | /* PHY state control function */ |
| 1544 | static void sh_eth_adjust_link(struct net_device *ndev) |
| 1545 | { |
| 1546 | struct sh_eth_private *mdp = netdev_priv(ndev); |
| 1547 | struct phy_device *phydev = mdp->phydev; |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1548 | int new_state = 0; |
| 1549 | |
Sergei Shtylyov | 3340d2a | 2013-03-31 10:11:04 +0000 | [diff] [blame] | 1550 | if (phydev->link) { |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1551 | if (phydev->duplex != mdp->duplex) { |
| 1552 | new_state = 1; |
| 1553 | mdp->duplex = phydev->duplex; |
Yoshihiro Shimoda | 380af9e | 2009-05-24 23:54:21 +0000 | [diff] [blame] | 1554 | if (mdp->cd->set_duplex) |
| 1555 | mdp->cd->set_duplex(ndev); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1556 | } |
| 1557 | |
| 1558 | if (phydev->speed != mdp->speed) { |
| 1559 | new_state = 1; |
| 1560 | mdp->speed = phydev->speed; |
Yoshihiro Shimoda | 380af9e | 2009-05-24 23:54:21 +0000 | [diff] [blame] | 1561 | if (mdp->cd->set_rate) |
| 1562 | mdp->cd->set_rate(ndev); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1563 | } |
Sergei Shtylyov | 3340d2a | 2013-03-31 10:11:04 +0000 | [diff] [blame] | 1564 | if (!mdp->link) { |
Yoshihiro Shimoda | 91a5615 | 2011-07-05 20:33:51 +0000 | [diff] [blame] | 1565 | sh_eth_write(ndev, |
| 1566 | (sh_eth_read(ndev, ECMR) & ~ECMR_TXF), ECMR); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1567 | new_state = 1; |
| 1568 | mdp->link = phydev->link; |
Sergei Shtylyov | 1e1b812 | 2013-03-31 09:50:07 +0000 | [diff] [blame] | 1569 | if (mdp->cd->no_psr || mdp->no_ether_link) |
| 1570 | sh_eth_rcv_snd_enable(ndev); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1571 | } |
| 1572 | } else if (mdp->link) { |
| 1573 | new_state = 1; |
Sergei Shtylyov | 3340d2a | 2013-03-31 10:11:04 +0000 | [diff] [blame] | 1574 | mdp->link = 0; |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1575 | mdp->speed = 0; |
| 1576 | mdp->duplex = -1; |
Sergei Shtylyov | 1e1b812 | 2013-03-31 09:50:07 +0000 | [diff] [blame] | 1577 | if (mdp->cd->no_psr || mdp->no_ether_link) |
| 1578 | sh_eth_rcv_snd_disable(ndev); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1579 | } |
| 1580 | |
Nobuhiro Iwamatsu | dc19e4e | 2011-02-15 21:17:32 +0000 | [diff] [blame] | 1581 | if (new_state && netif_msg_link(mdp)) |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1582 | phy_print_status(phydev); |
| 1583 | } |
| 1584 | |
| 1585 | /* PHY init function */ |
| 1586 | static int sh_eth_phy_init(struct net_device *ndev) |
| 1587 | { |
| 1588 | struct sh_eth_private *mdp = netdev_priv(ndev); |
David S. Miller | 0a372eb | 2009-05-26 21:11:09 -0700 | [diff] [blame] | 1589 | char phy_id[MII_BUS_ID_SIZE + 3]; |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1590 | struct phy_device *phydev = NULL; |
| 1591 | |
Kay Sievers | fb28ad3 | 2008-11-10 13:55:14 -0800 | [diff] [blame] | 1592 | snprintf(phy_id, sizeof(phy_id), PHY_ID_FMT, |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1593 | mdp->mii_bus->id , mdp->phy_id); |
| 1594 | |
Sergei Shtylyov | 3340d2a | 2013-03-31 10:11:04 +0000 | [diff] [blame] | 1595 | mdp->link = 0; |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1596 | mdp->speed = 0; |
| 1597 | mdp->duplex = -1; |
| 1598 | |
| 1599 | /* Try connect to PHY */ |
Joe Perches | c061b18 | 2010-08-23 18:20:03 +0000 | [diff] [blame] | 1600 | phydev = phy_connect(ndev, phy_id, sh_eth_adjust_link, |
Florian Fainelli | f9a8f83 | 2013-01-14 00:52:52 +0000 | [diff] [blame] | 1601 | mdp->phy_interface); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1602 | if (IS_ERR(phydev)) { |
| 1603 | dev_err(&ndev->dev, "phy_connect failed\n"); |
| 1604 | return PTR_ERR(phydev); |
| 1605 | } |
Yoshihiro Shimoda | 380af9e | 2009-05-24 23:54:21 +0000 | [diff] [blame] | 1606 | |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1607 | dev_info(&ndev->dev, "attached phy %i to driver %s\n", |
Yoshihiro Shimoda | 380af9e | 2009-05-24 23:54:21 +0000 | [diff] [blame] | 1608 | phydev->addr, phydev->drv->name); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1609 | |
| 1610 | mdp->phydev = phydev; |
| 1611 | |
| 1612 | return 0; |
| 1613 | } |
| 1614 | |
| 1615 | /* PHY control start function */ |
| 1616 | static int sh_eth_phy_start(struct net_device *ndev) |
| 1617 | { |
| 1618 | struct sh_eth_private *mdp = netdev_priv(ndev); |
| 1619 | int ret; |
| 1620 | |
| 1621 | ret = sh_eth_phy_init(ndev); |
| 1622 | if (ret) |
| 1623 | return ret; |
| 1624 | |
| 1625 | /* reset phy - this also wakes it from PDOWN */ |
| 1626 | phy_write(mdp->phydev, MII_BMCR, BMCR_RESET); |
| 1627 | phy_start(mdp->phydev); |
| 1628 | |
| 1629 | return 0; |
| 1630 | } |
| 1631 | |
Nobuhiro Iwamatsu | dc19e4e | 2011-02-15 21:17:32 +0000 | [diff] [blame] | 1632 | static int sh_eth_get_settings(struct net_device *ndev, |
| 1633 | struct ethtool_cmd *ecmd) |
| 1634 | { |
| 1635 | struct sh_eth_private *mdp = netdev_priv(ndev); |
| 1636 | unsigned long flags; |
| 1637 | int ret; |
| 1638 | |
| 1639 | spin_lock_irqsave(&mdp->lock, flags); |
| 1640 | ret = phy_ethtool_gset(mdp->phydev, ecmd); |
| 1641 | spin_unlock_irqrestore(&mdp->lock, flags); |
| 1642 | |
| 1643 | return ret; |
| 1644 | } |
| 1645 | |
| 1646 | static int sh_eth_set_settings(struct net_device *ndev, |
| 1647 | struct ethtool_cmd *ecmd) |
| 1648 | { |
| 1649 | struct sh_eth_private *mdp = netdev_priv(ndev); |
| 1650 | unsigned long flags; |
| 1651 | int ret; |
Nobuhiro Iwamatsu | dc19e4e | 2011-02-15 21:17:32 +0000 | [diff] [blame] | 1652 | |
| 1653 | spin_lock_irqsave(&mdp->lock, flags); |
| 1654 | |
| 1655 | /* disable tx and rx */ |
Yoshihiro Shimoda | 4a55530 | 2011-03-07 21:59:26 +0000 | [diff] [blame] | 1656 | sh_eth_rcv_snd_disable(ndev); |
Nobuhiro Iwamatsu | dc19e4e | 2011-02-15 21:17:32 +0000 | [diff] [blame] | 1657 | |
| 1658 | ret = phy_ethtool_sset(mdp->phydev, ecmd); |
| 1659 | if (ret) |
| 1660 | goto error_exit; |
| 1661 | |
| 1662 | if (ecmd->duplex == DUPLEX_FULL) |
| 1663 | mdp->duplex = 1; |
| 1664 | else |
| 1665 | mdp->duplex = 0; |
| 1666 | |
| 1667 | if (mdp->cd->set_duplex) |
| 1668 | mdp->cd->set_duplex(ndev); |
| 1669 | |
| 1670 | error_exit: |
| 1671 | mdelay(1); |
| 1672 | |
| 1673 | /* enable tx and rx */ |
Yoshihiro Shimoda | 4a55530 | 2011-03-07 21:59:26 +0000 | [diff] [blame] | 1674 | sh_eth_rcv_snd_enable(ndev); |
Nobuhiro Iwamatsu | dc19e4e | 2011-02-15 21:17:32 +0000 | [diff] [blame] | 1675 | |
| 1676 | spin_unlock_irqrestore(&mdp->lock, flags); |
| 1677 | |
| 1678 | return ret; |
| 1679 | } |
| 1680 | |
| 1681 | static int sh_eth_nway_reset(struct net_device *ndev) |
| 1682 | { |
| 1683 | struct sh_eth_private *mdp = netdev_priv(ndev); |
| 1684 | unsigned long flags; |
| 1685 | int ret; |
| 1686 | |
| 1687 | spin_lock_irqsave(&mdp->lock, flags); |
| 1688 | ret = phy_start_aneg(mdp->phydev); |
| 1689 | spin_unlock_irqrestore(&mdp->lock, flags); |
| 1690 | |
| 1691 | return ret; |
| 1692 | } |
| 1693 | |
| 1694 | static u32 sh_eth_get_msglevel(struct net_device *ndev) |
| 1695 | { |
| 1696 | struct sh_eth_private *mdp = netdev_priv(ndev); |
| 1697 | return mdp->msg_enable; |
| 1698 | } |
| 1699 | |
| 1700 | static void sh_eth_set_msglevel(struct net_device *ndev, u32 value) |
| 1701 | { |
| 1702 | struct sh_eth_private *mdp = netdev_priv(ndev); |
| 1703 | mdp->msg_enable = value; |
| 1704 | } |
| 1705 | |
| 1706 | static const char sh_eth_gstrings_stats[][ETH_GSTRING_LEN] = { |
| 1707 | "rx_current", "tx_current", |
| 1708 | "rx_dirty", "tx_dirty", |
| 1709 | }; |
| 1710 | #define SH_ETH_STATS_LEN ARRAY_SIZE(sh_eth_gstrings_stats) |
| 1711 | |
| 1712 | static int sh_eth_get_sset_count(struct net_device *netdev, int sset) |
| 1713 | { |
| 1714 | switch (sset) { |
| 1715 | case ETH_SS_STATS: |
| 1716 | return SH_ETH_STATS_LEN; |
| 1717 | default: |
| 1718 | return -EOPNOTSUPP; |
| 1719 | } |
| 1720 | } |
| 1721 | |
| 1722 | static void sh_eth_get_ethtool_stats(struct net_device *ndev, |
| 1723 | struct ethtool_stats *stats, u64 *data) |
| 1724 | { |
| 1725 | struct sh_eth_private *mdp = netdev_priv(ndev); |
| 1726 | int i = 0; |
| 1727 | |
| 1728 | /* device-specific stats */ |
| 1729 | data[i++] = mdp->cur_rx; |
| 1730 | data[i++] = mdp->cur_tx; |
| 1731 | data[i++] = mdp->dirty_rx; |
| 1732 | data[i++] = mdp->dirty_tx; |
| 1733 | } |
| 1734 | |
| 1735 | static void sh_eth_get_strings(struct net_device *ndev, u32 stringset, u8 *data) |
| 1736 | { |
| 1737 | switch (stringset) { |
| 1738 | case ETH_SS_STATS: |
| 1739 | memcpy(data, *sh_eth_gstrings_stats, |
| 1740 | sizeof(sh_eth_gstrings_stats)); |
| 1741 | break; |
| 1742 | } |
| 1743 | } |
| 1744 | |
Yoshihiro Shimoda | 525b807 | 2012-06-26 20:00:03 +0000 | [diff] [blame] | 1745 | static void sh_eth_get_ringparam(struct net_device *ndev, |
| 1746 | struct ethtool_ringparam *ring) |
| 1747 | { |
| 1748 | struct sh_eth_private *mdp = netdev_priv(ndev); |
| 1749 | |
| 1750 | ring->rx_max_pending = RX_RING_MAX; |
| 1751 | ring->tx_max_pending = TX_RING_MAX; |
| 1752 | ring->rx_pending = mdp->num_rx_ring; |
| 1753 | ring->tx_pending = mdp->num_tx_ring; |
| 1754 | } |
| 1755 | |
| 1756 | static int sh_eth_set_ringparam(struct net_device *ndev, |
| 1757 | struct ethtool_ringparam *ring) |
| 1758 | { |
| 1759 | struct sh_eth_private *mdp = netdev_priv(ndev); |
| 1760 | int ret; |
| 1761 | |
| 1762 | if (ring->tx_pending > TX_RING_MAX || |
| 1763 | ring->rx_pending > RX_RING_MAX || |
| 1764 | ring->tx_pending < TX_RING_MIN || |
| 1765 | ring->rx_pending < RX_RING_MIN) |
| 1766 | return -EINVAL; |
| 1767 | if (ring->rx_mini_pending || ring->rx_jumbo_pending) |
| 1768 | return -EINVAL; |
| 1769 | |
| 1770 | if (netif_running(ndev)) { |
| 1771 | netif_tx_disable(ndev); |
| 1772 | /* Disable interrupts by clearing the interrupt mask. */ |
| 1773 | sh_eth_write(ndev, 0x0000, EESIPR); |
| 1774 | /* Stop the chip's Tx and Rx processes. */ |
| 1775 | sh_eth_write(ndev, 0, EDTRR); |
| 1776 | sh_eth_write(ndev, 0, EDRRR); |
| 1777 | synchronize_irq(ndev->irq); |
| 1778 | } |
| 1779 | |
| 1780 | /* Free all the skbuffs in the Rx queue. */ |
| 1781 | sh_eth_ring_free(ndev); |
| 1782 | /* Free DMA buffer */ |
| 1783 | sh_eth_free_dma_buffer(mdp); |
| 1784 | |
| 1785 | /* Set new parameters */ |
| 1786 | mdp->num_rx_ring = ring->rx_pending; |
| 1787 | mdp->num_tx_ring = ring->tx_pending; |
| 1788 | |
| 1789 | ret = sh_eth_ring_init(ndev); |
| 1790 | if (ret < 0) { |
| 1791 | dev_err(&ndev->dev, "%s: sh_eth_ring_init failed.\n", __func__); |
| 1792 | return ret; |
| 1793 | } |
| 1794 | ret = sh_eth_dev_init(ndev, false); |
| 1795 | if (ret < 0) { |
| 1796 | dev_err(&ndev->dev, "%s: sh_eth_dev_init failed.\n", __func__); |
| 1797 | return ret; |
| 1798 | } |
| 1799 | |
| 1800 | if (netif_running(ndev)) { |
| 1801 | sh_eth_write(ndev, mdp->cd->eesipr_value, EESIPR); |
| 1802 | /* Setting the Rx mode will start the Rx process. */ |
| 1803 | sh_eth_write(ndev, EDRRR_R, EDRRR); |
| 1804 | netif_wake_queue(ndev); |
| 1805 | } |
| 1806 | |
| 1807 | return 0; |
| 1808 | } |
| 1809 | |
stephen hemminger | 9b07be4 | 2012-01-04 12:59:49 +0000 | [diff] [blame] | 1810 | static const struct ethtool_ops sh_eth_ethtool_ops = { |
Nobuhiro Iwamatsu | dc19e4e | 2011-02-15 21:17:32 +0000 | [diff] [blame] | 1811 | .get_settings = sh_eth_get_settings, |
| 1812 | .set_settings = sh_eth_set_settings, |
stephen hemminger | 9b07be4 | 2012-01-04 12:59:49 +0000 | [diff] [blame] | 1813 | .nway_reset = sh_eth_nway_reset, |
Nobuhiro Iwamatsu | dc19e4e | 2011-02-15 21:17:32 +0000 | [diff] [blame] | 1814 | .get_msglevel = sh_eth_get_msglevel, |
| 1815 | .set_msglevel = sh_eth_set_msglevel, |
stephen hemminger | 9b07be4 | 2012-01-04 12:59:49 +0000 | [diff] [blame] | 1816 | .get_link = ethtool_op_get_link, |
Nobuhiro Iwamatsu | dc19e4e | 2011-02-15 21:17:32 +0000 | [diff] [blame] | 1817 | .get_strings = sh_eth_get_strings, |
| 1818 | .get_ethtool_stats = sh_eth_get_ethtool_stats, |
| 1819 | .get_sset_count = sh_eth_get_sset_count, |
Yoshihiro Shimoda | 525b807 | 2012-06-26 20:00:03 +0000 | [diff] [blame] | 1820 | .get_ringparam = sh_eth_get_ringparam, |
| 1821 | .set_ringparam = sh_eth_set_ringparam, |
Nobuhiro Iwamatsu | dc19e4e | 2011-02-15 21:17:32 +0000 | [diff] [blame] | 1822 | }; |
| 1823 | |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1824 | /* network device open function */ |
| 1825 | static int sh_eth_open(struct net_device *ndev) |
| 1826 | { |
| 1827 | int ret = 0; |
| 1828 | struct sh_eth_private *mdp = netdev_priv(ndev); |
| 1829 | |
Magnus Damm | bcd5149 | 2009-10-09 00:20:04 +0000 | [diff] [blame] | 1830 | pm_runtime_get_sync(&mdp->pdev->dev); |
| 1831 | |
Joe Perches | a0607fd | 2009-11-18 23:29:17 -0800 | [diff] [blame] | 1832 | ret = request_irq(ndev->irq, sh_eth_interrupt, |
Nobuhiro Iwamatsu | 5b3dfd1 | 2013-06-06 09:49:30 +0000 | [diff] [blame] | 1833 | mdp->cd->irq_flags, ndev->name, ndev); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1834 | if (ret) { |
Yoshihiro Shimoda | 380af9e | 2009-05-24 23:54:21 +0000 | [diff] [blame] | 1835 | dev_err(&ndev->dev, "Can not assign IRQ number\n"); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1836 | return ret; |
| 1837 | } |
| 1838 | |
| 1839 | /* Descriptor set */ |
| 1840 | ret = sh_eth_ring_init(ndev); |
| 1841 | if (ret) |
| 1842 | goto out_free_irq; |
| 1843 | |
| 1844 | /* device init */ |
Yoshihiro Shimoda | 525b807 | 2012-06-26 20:00:03 +0000 | [diff] [blame] | 1845 | ret = sh_eth_dev_init(ndev, true); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1846 | if (ret) |
| 1847 | goto out_free_irq; |
| 1848 | |
| 1849 | /* PHY control start*/ |
| 1850 | ret = sh_eth_phy_start(ndev); |
| 1851 | if (ret) |
| 1852 | goto out_free_irq; |
| 1853 | |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1854 | return ret; |
| 1855 | |
| 1856 | out_free_irq: |
| 1857 | free_irq(ndev->irq, ndev); |
Magnus Damm | bcd5149 | 2009-10-09 00:20:04 +0000 | [diff] [blame] | 1858 | pm_runtime_put_sync(&mdp->pdev->dev); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1859 | return ret; |
| 1860 | } |
| 1861 | |
| 1862 | /* Timeout function */ |
| 1863 | static void sh_eth_tx_timeout(struct net_device *ndev) |
| 1864 | { |
| 1865 | struct sh_eth_private *mdp = netdev_priv(ndev); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1866 | struct sh_eth_rxdesc *rxdesc; |
| 1867 | int i; |
| 1868 | |
| 1869 | netif_stop_queue(ndev); |
| 1870 | |
Nobuhiro Iwamatsu | dc19e4e | 2011-02-15 21:17:32 +0000 | [diff] [blame] | 1871 | if (netif_msg_timer(mdp)) |
| 1872 | dev_err(&ndev->dev, "%s: transmit timed out, status %8.8x," |
Yoshihiro Shimoda | 4a55530 | 2011-03-07 21:59:26 +0000 | [diff] [blame] | 1873 | " resetting...\n", ndev->name, (int)sh_eth_read(ndev, EESR)); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1874 | |
| 1875 | /* tx_errors count up */ |
Eric Dumazet | bb7d92e | 2012-02-06 22:17:21 +0000 | [diff] [blame] | 1876 | ndev->stats.tx_errors++; |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1877 | |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1878 | /* Free all the skbuffs in the Rx queue. */ |
Yoshihiro Shimoda | 525b807 | 2012-06-26 20:00:03 +0000 | [diff] [blame] | 1879 | for (i = 0; i < mdp->num_rx_ring; i++) { |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1880 | rxdesc = &mdp->rx_ring[i]; |
| 1881 | rxdesc->status = 0; |
| 1882 | rxdesc->addr = 0xBADF00D0; |
| 1883 | if (mdp->rx_skbuff[i]) |
| 1884 | dev_kfree_skb(mdp->rx_skbuff[i]); |
| 1885 | mdp->rx_skbuff[i] = NULL; |
| 1886 | } |
Yoshihiro Shimoda | 525b807 | 2012-06-26 20:00:03 +0000 | [diff] [blame] | 1887 | for (i = 0; i < mdp->num_tx_ring; i++) { |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1888 | if (mdp->tx_skbuff[i]) |
| 1889 | dev_kfree_skb(mdp->tx_skbuff[i]); |
| 1890 | mdp->tx_skbuff[i] = NULL; |
| 1891 | } |
| 1892 | |
| 1893 | /* device init */ |
Yoshihiro Shimoda | 525b807 | 2012-06-26 20:00:03 +0000 | [diff] [blame] | 1894 | sh_eth_dev_init(ndev, true); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1895 | } |
| 1896 | |
| 1897 | /* Packet transmit function */ |
| 1898 | static int sh_eth_start_xmit(struct sk_buff *skb, struct net_device *ndev) |
| 1899 | { |
| 1900 | struct sh_eth_private *mdp = netdev_priv(ndev); |
| 1901 | struct sh_eth_txdesc *txdesc; |
| 1902 | u32 entry; |
Nobuhiro Iwamatsu | fb5e2f9 | 2008-11-17 20:29:58 +0000 | [diff] [blame] | 1903 | unsigned long flags; |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1904 | |
| 1905 | spin_lock_irqsave(&mdp->lock, flags); |
Yoshihiro Shimoda | 525b807 | 2012-06-26 20:00:03 +0000 | [diff] [blame] | 1906 | if ((mdp->cur_tx - mdp->dirty_tx) >= (mdp->num_tx_ring - 4)) { |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1907 | if (!sh_eth_txfree(ndev)) { |
Nobuhiro Iwamatsu | dc19e4e | 2011-02-15 21:17:32 +0000 | [diff] [blame] | 1908 | if (netif_msg_tx_queued(mdp)) |
| 1909 | dev_warn(&ndev->dev, "TxFD exhausted.\n"); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1910 | netif_stop_queue(ndev); |
| 1911 | spin_unlock_irqrestore(&mdp->lock, flags); |
Patrick McHardy | 5b54814 | 2009-06-12 06:22:29 +0000 | [diff] [blame] | 1912 | return NETDEV_TX_BUSY; |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1913 | } |
| 1914 | } |
| 1915 | spin_unlock_irqrestore(&mdp->lock, flags); |
| 1916 | |
Yoshihiro Shimoda | 525b807 | 2012-06-26 20:00:03 +0000 | [diff] [blame] | 1917 | entry = mdp->cur_tx % mdp->num_tx_ring; |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1918 | mdp->tx_skbuff[entry] = skb; |
| 1919 | txdesc = &mdp->tx_ring[entry]; |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1920 | /* soft swap. */ |
Yoshihiro Shimoda | 380af9e | 2009-05-24 23:54:21 +0000 | [diff] [blame] | 1921 | if (!mdp->cd->hw_swap) |
| 1922 | sh_eth_soft_swap(phys_to_virt(ALIGN(txdesc->addr, 4)), |
| 1923 | skb->len + 2); |
Yoshihiro Shimoda | 31fcb99 | 2011-06-30 22:52:13 +0000 | [diff] [blame] | 1924 | txdesc->addr = dma_map_single(&ndev->dev, skb->data, skb->len, |
| 1925 | DMA_TO_DEVICE); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1926 | if (skb->len < ETHERSMALL) |
| 1927 | txdesc->buffer_length = ETHERSMALL; |
| 1928 | else |
| 1929 | txdesc->buffer_length = skb->len; |
| 1930 | |
Yoshihiro Shimoda | 525b807 | 2012-06-26 20:00:03 +0000 | [diff] [blame] | 1931 | if (entry >= mdp->num_tx_ring - 1) |
Yoshinori Sato | 71557a3 | 2008-08-06 19:49:00 -0400 | [diff] [blame] | 1932 | txdesc->status |= cpu_to_edmac(mdp, TD_TACT | TD_TDLE); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1933 | else |
Yoshinori Sato | 71557a3 | 2008-08-06 19:49:00 -0400 | [diff] [blame] | 1934 | txdesc->status |= cpu_to_edmac(mdp, TD_TACT); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1935 | |
| 1936 | mdp->cur_tx++; |
| 1937 | |
Yoshihiro Shimoda | c5ed536 | 2011-03-07 21:59:38 +0000 | [diff] [blame] | 1938 | if (!(sh_eth_read(ndev, EDTRR) & sh_eth_get_edtrr_trns(mdp))) |
| 1939 | sh_eth_write(ndev, sh_eth_get_edtrr_trns(mdp), EDTRR); |
Nobuhiro Iwamatsu | b0ca2a2 | 2008-06-30 11:08:17 +0900 | [diff] [blame] | 1940 | |
Patrick McHardy | 6ed1065 | 2009-06-23 06:03:08 +0000 | [diff] [blame] | 1941 | return NETDEV_TX_OK; |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1942 | } |
| 1943 | |
| 1944 | /* device close function */ |
| 1945 | static int sh_eth_close(struct net_device *ndev) |
| 1946 | { |
| 1947 | struct sh_eth_private *mdp = netdev_priv(ndev); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1948 | |
| 1949 | netif_stop_queue(ndev); |
| 1950 | |
| 1951 | /* Disable interrupts by clearing the interrupt mask. */ |
Yoshihiro Shimoda | 4a55530 | 2011-03-07 21:59:26 +0000 | [diff] [blame] | 1952 | sh_eth_write(ndev, 0x0000, EESIPR); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1953 | |
| 1954 | /* Stop the chip's Tx and Rx processes. */ |
Yoshihiro Shimoda | 4a55530 | 2011-03-07 21:59:26 +0000 | [diff] [blame] | 1955 | sh_eth_write(ndev, 0, EDTRR); |
| 1956 | sh_eth_write(ndev, 0, EDRRR); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1957 | |
| 1958 | /* PHY Disconnect */ |
| 1959 | if (mdp->phydev) { |
| 1960 | phy_stop(mdp->phydev); |
| 1961 | phy_disconnect(mdp->phydev); |
| 1962 | } |
| 1963 | |
| 1964 | free_irq(ndev->irq, ndev); |
| 1965 | |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1966 | /* Free all the skbuffs in the Rx queue. */ |
| 1967 | sh_eth_ring_free(ndev); |
| 1968 | |
| 1969 | /* free DMA buffer */ |
Yoshihiro Shimoda | 91c7755 | 2012-06-26 20:00:01 +0000 | [diff] [blame] | 1970 | sh_eth_free_dma_buffer(mdp); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1971 | |
Magnus Damm | bcd5149 | 2009-10-09 00:20:04 +0000 | [diff] [blame] | 1972 | pm_runtime_put_sync(&mdp->pdev->dev); |
| 1973 | |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1974 | return 0; |
| 1975 | } |
| 1976 | |
| 1977 | static struct net_device_stats *sh_eth_get_stats(struct net_device *ndev) |
| 1978 | { |
| 1979 | struct sh_eth_private *mdp = netdev_priv(ndev); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1980 | |
Magnus Damm | bcd5149 | 2009-10-09 00:20:04 +0000 | [diff] [blame] | 1981 | pm_runtime_get_sync(&mdp->pdev->dev); |
| 1982 | |
Eric Dumazet | bb7d92e | 2012-02-06 22:17:21 +0000 | [diff] [blame] | 1983 | ndev->stats.tx_dropped += sh_eth_read(ndev, TROCR); |
Yoshihiro Shimoda | 4a55530 | 2011-03-07 21:59:26 +0000 | [diff] [blame] | 1984 | sh_eth_write(ndev, 0, TROCR); /* (write clear) */ |
Eric Dumazet | bb7d92e | 2012-02-06 22:17:21 +0000 | [diff] [blame] | 1985 | ndev->stats.collisions += sh_eth_read(ndev, CDCR); |
Yoshihiro Shimoda | 4a55530 | 2011-03-07 21:59:26 +0000 | [diff] [blame] | 1986 | sh_eth_write(ndev, 0, CDCR); /* (write clear) */ |
Eric Dumazet | bb7d92e | 2012-02-06 22:17:21 +0000 | [diff] [blame] | 1987 | ndev->stats.tx_carrier_errors += sh_eth_read(ndev, LCCR); |
Yoshihiro Shimoda | 4a55530 | 2011-03-07 21:59:26 +0000 | [diff] [blame] | 1988 | sh_eth_write(ndev, 0, LCCR); /* (write clear) */ |
Yoshihiro Shimoda | c5ed536 | 2011-03-07 21:59:38 +0000 | [diff] [blame] | 1989 | if (sh_eth_is_gether(mdp)) { |
Eric Dumazet | bb7d92e | 2012-02-06 22:17:21 +0000 | [diff] [blame] | 1990 | ndev->stats.tx_carrier_errors += sh_eth_read(ndev, CERCR); |
Yoshihiro Shimoda | c5ed536 | 2011-03-07 21:59:38 +0000 | [diff] [blame] | 1991 | sh_eth_write(ndev, 0, CERCR); /* (write clear) */ |
Eric Dumazet | bb7d92e | 2012-02-06 22:17:21 +0000 | [diff] [blame] | 1992 | ndev->stats.tx_carrier_errors += sh_eth_read(ndev, CEECR); |
Yoshihiro Shimoda | c5ed536 | 2011-03-07 21:59:38 +0000 | [diff] [blame] | 1993 | sh_eth_write(ndev, 0, CEECR); /* (write clear) */ |
| 1994 | } else { |
Eric Dumazet | bb7d92e | 2012-02-06 22:17:21 +0000 | [diff] [blame] | 1995 | ndev->stats.tx_carrier_errors += sh_eth_read(ndev, CNDCR); |
Yoshihiro Shimoda | c5ed536 | 2011-03-07 21:59:38 +0000 | [diff] [blame] | 1996 | sh_eth_write(ndev, 0, CNDCR); /* (write clear) */ |
| 1997 | } |
Magnus Damm | bcd5149 | 2009-10-09 00:20:04 +0000 | [diff] [blame] | 1998 | pm_runtime_put_sync(&mdp->pdev->dev); |
| 1999 | |
Eric Dumazet | bb7d92e | 2012-02-06 22:17:21 +0000 | [diff] [blame] | 2000 | return &ndev->stats; |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 2001 | } |
| 2002 | |
Eric Dumazet | bb7d92e | 2012-02-06 22:17:21 +0000 | [diff] [blame] | 2003 | /* ioctl to device function */ |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 2004 | static int sh_eth_do_ioctl(struct net_device *ndev, struct ifreq *rq, |
| 2005 | int cmd) |
| 2006 | { |
| 2007 | struct sh_eth_private *mdp = netdev_priv(ndev); |
| 2008 | struct phy_device *phydev = mdp->phydev; |
| 2009 | |
| 2010 | if (!netif_running(ndev)) |
| 2011 | return -EINVAL; |
| 2012 | |
| 2013 | if (!phydev) |
| 2014 | return -ENODEV; |
| 2015 | |
Richard Cochran | 28b0411 | 2010-07-17 08:48:55 +0000 | [diff] [blame] | 2016 | return phy_mii_ioctl(phydev, rq, cmd); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 2017 | } |
| 2018 | |
Yoshihiro Shimoda | 6743fe6 | 2012-02-15 17:55:03 +0000 | [diff] [blame] | 2019 | /* For TSU_POSTn. Please refer to the manual about this (strange) bitfields */ |
| 2020 | static void *sh_eth_tsu_get_post_reg_offset(struct sh_eth_private *mdp, |
| 2021 | int entry) |
| 2022 | { |
| 2023 | return sh_eth_tsu_get_offset(mdp, TSU_POST1) + (entry / 8 * 4); |
| 2024 | } |
| 2025 | |
| 2026 | static u32 sh_eth_tsu_get_post_mask(int entry) |
| 2027 | { |
| 2028 | return 0x0f << (28 - ((entry % 8) * 4)); |
| 2029 | } |
| 2030 | |
| 2031 | static u32 sh_eth_tsu_get_post_bit(struct sh_eth_private *mdp, int entry) |
| 2032 | { |
| 2033 | return (0x08 >> (mdp->port << 1)) << (28 - ((entry % 8) * 4)); |
| 2034 | } |
| 2035 | |
| 2036 | static void sh_eth_tsu_enable_cam_entry_post(struct net_device *ndev, |
| 2037 | int entry) |
| 2038 | { |
| 2039 | struct sh_eth_private *mdp = netdev_priv(ndev); |
| 2040 | u32 tmp; |
| 2041 | void *reg_offset; |
| 2042 | |
| 2043 | reg_offset = sh_eth_tsu_get_post_reg_offset(mdp, entry); |
| 2044 | tmp = ioread32(reg_offset); |
| 2045 | iowrite32(tmp | sh_eth_tsu_get_post_bit(mdp, entry), reg_offset); |
| 2046 | } |
| 2047 | |
| 2048 | static bool sh_eth_tsu_disable_cam_entry_post(struct net_device *ndev, |
| 2049 | int entry) |
| 2050 | { |
| 2051 | struct sh_eth_private *mdp = netdev_priv(ndev); |
| 2052 | u32 post_mask, ref_mask, tmp; |
| 2053 | void *reg_offset; |
| 2054 | |
| 2055 | reg_offset = sh_eth_tsu_get_post_reg_offset(mdp, entry); |
| 2056 | post_mask = sh_eth_tsu_get_post_mask(entry); |
| 2057 | ref_mask = sh_eth_tsu_get_post_bit(mdp, entry) & ~post_mask; |
| 2058 | |
| 2059 | tmp = ioread32(reg_offset); |
| 2060 | iowrite32(tmp & ~post_mask, reg_offset); |
| 2061 | |
| 2062 | /* If other port enables, the function returns "true" */ |
| 2063 | return tmp & ref_mask; |
| 2064 | } |
| 2065 | |
| 2066 | static int sh_eth_tsu_busy(struct net_device *ndev) |
| 2067 | { |
| 2068 | int timeout = SH_ETH_TSU_TIMEOUT_MS * 100; |
| 2069 | struct sh_eth_private *mdp = netdev_priv(ndev); |
| 2070 | |
| 2071 | while ((sh_eth_tsu_read(mdp, TSU_ADSBSY) & TSU_ADSBSY_0)) { |
| 2072 | udelay(10); |
| 2073 | timeout--; |
| 2074 | if (timeout <= 0) { |
| 2075 | dev_err(&ndev->dev, "%s: timeout\n", __func__); |
| 2076 | return -ETIMEDOUT; |
| 2077 | } |
| 2078 | } |
| 2079 | |
| 2080 | return 0; |
| 2081 | } |
| 2082 | |
| 2083 | static int sh_eth_tsu_write_entry(struct net_device *ndev, void *reg, |
| 2084 | const u8 *addr) |
| 2085 | { |
| 2086 | u32 val; |
| 2087 | |
| 2088 | val = addr[0] << 24 | addr[1] << 16 | addr[2] << 8 | addr[3]; |
| 2089 | iowrite32(val, reg); |
| 2090 | if (sh_eth_tsu_busy(ndev) < 0) |
| 2091 | return -EBUSY; |
| 2092 | |
| 2093 | val = addr[4] << 8 | addr[5]; |
| 2094 | iowrite32(val, reg + 4); |
| 2095 | if (sh_eth_tsu_busy(ndev) < 0) |
| 2096 | return -EBUSY; |
| 2097 | |
| 2098 | return 0; |
| 2099 | } |
| 2100 | |
| 2101 | static void sh_eth_tsu_read_entry(void *reg, u8 *addr) |
| 2102 | { |
| 2103 | u32 val; |
| 2104 | |
| 2105 | val = ioread32(reg); |
| 2106 | addr[0] = (val >> 24) & 0xff; |
| 2107 | addr[1] = (val >> 16) & 0xff; |
| 2108 | addr[2] = (val >> 8) & 0xff; |
| 2109 | addr[3] = val & 0xff; |
| 2110 | val = ioread32(reg + 4); |
| 2111 | addr[4] = (val >> 8) & 0xff; |
| 2112 | addr[5] = val & 0xff; |
| 2113 | } |
| 2114 | |
| 2115 | |
| 2116 | static int sh_eth_tsu_find_entry(struct net_device *ndev, const u8 *addr) |
| 2117 | { |
| 2118 | struct sh_eth_private *mdp = netdev_priv(ndev); |
| 2119 | void *reg_offset = sh_eth_tsu_get_offset(mdp, TSU_ADRH0); |
| 2120 | int i; |
| 2121 | u8 c_addr[ETH_ALEN]; |
| 2122 | |
| 2123 | for (i = 0; i < SH_ETH_TSU_CAM_ENTRIES; i++, reg_offset += 8) { |
| 2124 | sh_eth_tsu_read_entry(reg_offset, c_addr); |
| 2125 | if (memcmp(addr, c_addr, ETH_ALEN) == 0) |
| 2126 | return i; |
| 2127 | } |
| 2128 | |
| 2129 | return -ENOENT; |
| 2130 | } |
| 2131 | |
| 2132 | static int sh_eth_tsu_find_empty(struct net_device *ndev) |
| 2133 | { |
| 2134 | u8 blank[ETH_ALEN]; |
| 2135 | int entry; |
| 2136 | |
| 2137 | memset(blank, 0, sizeof(blank)); |
| 2138 | entry = sh_eth_tsu_find_entry(ndev, blank); |
| 2139 | return (entry < 0) ? -ENOMEM : entry; |
| 2140 | } |
| 2141 | |
| 2142 | static int sh_eth_tsu_disable_cam_entry_table(struct net_device *ndev, |
| 2143 | int entry) |
| 2144 | { |
| 2145 | struct sh_eth_private *mdp = netdev_priv(ndev); |
| 2146 | void *reg_offset = sh_eth_tsu_get_offset(mdp, TSU_ADRH0); |
| 2147 | int ret; |
| 2148 | u8 blank[ETH_ALEN]; |
| 2149 | |
| 2150 | sh_eth_tsu_write(mdp, sh_eth_tsu_read(mdp, TSU_TEN) & |
| 2151 | ~(1 << (31 - entry)), TSU_TEN); |
| 2152 | |
| 2153 | memset(blank, 0, sizeof(blank)); |
| 2154 | ret = sh_eth_tsu_write_entry(ndev, reg_offset + entry * 8, blank); |
| 2155 | if (ret < 0) |
| 2156 | return ret; |
| 2157 | return 0; |
| 2158 | } |
| 2159 | |
| 2160 | static int sh_eth_tsu_add_entry(struct net_device *ndev, const u8 *addr) |
| 2161 | { |
| 2162 | struct sh_eth_private *mdp = netdev_priv(ndev); |
| 2163 | void *reg_offset = sh_eth_tsu_get_offset(mdp, TSU_ADRH0); |
| 2164 | int i, ret; |
| 2165 | |
| 2166 | if (!mdp->cd->tsu) |
| 2167 | return 0; |
| 2168 | |
| 2169 | i = sh_eth_tsu_find_entry(ndev, addr); |
| 2170 | if (i < 0) { |
| 2171 | /* No entry found, create one */ |
| 2172 | i = sh_eth_tsu_find_empty(ndev); |
| 2173 | if (i < 0) |
| 2174 | return -ENOMEM; |
| 2175 | ret = sh_eth_tsu_write_entry(ndev, reg_offset + i * 8, addr); |
| 2176 | if (ret < 0) |
| 2177 | return ret; |
| 2178 | |
| 2179 | /* Enable the entry */ |
| 2180 | sh_eth_tsu_write(mdp, sh_eth_tsu_read(mdp, TSU_TEN) | |
| 2181 | (1 << (31 - i)), TSU_TEN); |
| 2182 | } |
| 2183 | |
| 2184 | /* Entry found or created, enable POST */ |
| 2185 | sh_eth_tsu_enable_cam_entry_post(ndev, i); |
| 2186 | |
| 2187 | return 0; |
| 2188 | } |
| 2189 | |
| 2190 | static int sh_eth_tsu_del_entry(struct net_device *ndev, const u8 *addr) |
| 2191 | { |
| 2192 | struct sh_eth_private *mdp = netdev_priv(ndev); |
| 2193 | int i, ret; |
| 2194 | |
| 2195 | if (!mdp->cd->tsu) |
| 2196 | return 0; |
| 2197 | |
| 2198 | i = sh_eth_tsu_find_entry(ndev, addr); |
| 2199 | if (i) { |
| 2200 | /* Entry found */ |
| 2201 | if (sh_eth_tsu_disable_cam_entry_post(ndev, i)) |
| 2202 | goto done; |
| 2203 | |
| 2204 | /* Disable the entry if both ports was disabled */ |
| 2205 | ret = sh_eth_tsu_disable_cam_entry_table(ndev, i); |
| 2206 | if (ret < 0) |
| 2207 | return ret; |
| 2208 | } |
| 2209 | done: |
| 2210 | return 0; |
| 2211 | } |
| 2212 | |
| 2213 | static int sh_eth_tsu_purge_all(struct net_device *ndev) |
| 2214 | { |
| 2215 | struct sh_eth_private *mdp = netdev_priv(ndev); |
| 2216 | int i, ret; |
| 2217 | |
| 2218 | if (unlikely(!mdp->cd->tsu)) |
| 2219 | return 0; |
| 2220 | |
| 2221 | for (i = 0; i < SH_ETH_TSU_CAM_ENTRIES; i++) { |
| 2222 | if (sh_eth_tsu_disable_cam_entry_post(ndev, i)) |
| 2223 | continue; |
| 2224 | |
| 2225 | /* Disable the entry if both ports was disabled */ |
| 2226 | ret = sh_eth_tsu_disable_cam_entry_table(ndev, i); |
| 2227 | if (ret < 0) |
| 2228 | return ret; |
| 2229 | } |
| 2230 | |
| 2231 | return 0; |
| 2232 | } |
| 2233 | |
| 2234 | static void sh_eth_tsu_purge_mcast(struct net_device *ndev) |
| 2235 | { |
| 2236 | struct sh_eth_private *mdp = netdev_priv(ndev); |
| 2237 | u8 addr[ETH_ALEN]; |
| 2238 | void *reg_offset = sh_eth_tsu_get_offset(mdp, TSU_ADRH0); |
| 2239 | int i; |
| 2240 | |
| 2241 | if (unlikely(!mdp->cd->tsu)) |
| 2242 | return; |
| 2243 | |
| 2244 | for (i = 0; i < SH_ETH_TSU_CAM_ENTRIES; i++, reg_offset += 8) { |
| 2245 | sh_eth_tsu_read_entry(reg_offset, addr); |
| 2246 | if (is_multicast_ether_addr(addr)) |
| 2247 | sh_eth_tsu_del_entry(ndev, addr); |
| 2248 | } |
| 2249 | } |
| 2250 | |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 2251 | /* Multicast reception directions set */ |
| 2252 | static void sh_eth_set_multicast_list(struct net_device *ndev) |
| 2253 | { |
Yoshihiro Shimoda | 6743fe6 | 2012-02-15 17:55:03 +0000 | [diff] [blame] | 2254 | struct sh_eth_private *mdp = netdev_priv(ndev); |
| 2255 | u32 ecmr_bits; |
| 2256 | int mcast_all = 0; |
| 2257 | unsigned long flags; |
| 2258 | |
| 2259 | spin_lock_irqsave(&mdp->lock, flags); |
| 2260 | /* |
| 2261 | * Initial condition is MCT = 1, PRM = 0. |
| 2262 | * Depending on ndev->flags, set PRM or clear MCT |
| 2263 | */ |
| 2264 | ecmr_bits = (sh_eth_read(ndev, ECMR) & ~ECMR_PRM) | ECMR_MCT; |
| 2265 | |
| 2266 | if (!(ndev->flags & IFF_MULTICAST)) { |
| 2267 | sh_eth_tsu_purge_mcast(ndev); |
| 2268 | mcast_all = 1; |
| 2269 | } |
| 2270 | if (ndev->flags & IFF_ALLMULTI) { |
| 2271 | sh_eth_tsu_purge_mcast(ndev); |
| 2272 | ecmr_bits &= ~ECMR_MCT; |
| 2273 | mcast_all = 1; |
| 2274 | } |
| 2275 | |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 2276 | if (ndev->flags & IFF_PROMISC) { |
Yoshihiro Shimoda | 6743fe6 | 2012-02-15 17:55:03 +0000 | [diff] [blame] | 2277 | sh_eth_tsu_purge_all(ndev); |
| 2278 | ecmr_bits = (ecmr_bits & ~ECMR_MCT) | ECMR_PRM; |
| 2279 | } else if (mdp->cd->tsu) { |
| 2280 | struct netdev_hw_addr *ha; |
| 2281 | netdev_for_each_mc_addr(ha, ndev) { |
| 2282 | if (mcast_all && is_multicast_ether_addr(ha->addr)) |
| 2283 | continue; |
| 2284 | |
| 2285 | if (sh_eth_tsu_add_entry(ndev, ha->addr) < 0) { |
| 2286 | if (!mcast_all) { |
| 2287 | sh_eth_tsu_purge_mcast(ndev); |
| 2288 | ecmr_bits &= ~ECMR_MCT; |
| 2289 | mcast_all = 1; |
| 2290 | } |
| 2291 | } |
| 2292 | } |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 2293 | } else { |
| 2294 | /* Normal, unicast/broadcast-only mode. */ |
Yoshihiro Shimoda | 6743fe6 | 2012-02-15 17:55:03 +0000 | [diff] [blame] | 2295 | ecmr_bits = (ecmr_bits & ~ECMR_PRM) | ECMR_MCT; |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 2296 | } |
Yoshihiro Shimoda | 6743fe6 | 2012-02-15 17:55:03 +0000 | [diff] [blame] | 2297 | |
| 2298 | /* update the ethernet mode */ |
| 2299 | sh_eth_write(ndev, ecmr_bits, ECMR); |
| 2300 | |
| 2301 | spin_unlock_irqrestore(&mdp->lock, flags); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 2302 | } |
Yoshihiro Shimoda | 71cc7c3 | 2012-02-15 17:55:06 +0000 | [diff] [blame] | 2303 | |
| 2304 | static int sh_eth_get_vtag_index(struct sh_eth_private *mdp) |
| 2305 | { |
| 2306 | if (!mdp->port) |
| 2307 | return TSU_VTAG0; |
| 2308 | else |
| 2309 | return TSU_VTAG1; |
| 2310 | } |
| 2311 | |
Patrick McHardy | 80d5c36 | 2013-04-19 02:04:28 +0000 | [diff] [blame] | 2312 | static int sh_eth_vlan_rx_add_vid(struct net_device *ndev, |
| 2313 | __be16 proto, u16 vid) |
Yoshihiro Shimoda | 71cc7c3 | 2012-02-15 17:55:06 +0000 | [diff] [blame] | 2314 | { |
| 2315 | struct sh_eth_private *mdp = netdev_priv(ndev); |
| 2316 | int vtag_reg_index = sh_eth_get_vtag_index(mdp); |
| 2317 | |
| 2318 | if (unlikely(!mdp->cd->tsu)) |
| 2319 | return -EPERM; |
| 2320 | |
| 2321 | /* No filtering if vid = 0 */ |
| 2322 | if (!vid) |
| 2323 | return 0; |
| 2324 | |
| 2325 | mdp->vlan_num_ids++; |
| 2326 | |
| 2327 | /* |
| 2328 | * The controller has one VLAN tag HW filter. So, if the filter is |
| 2329 | * already enabled, the driver disables it and the filte |
| 2330 | */ |
| 2331 | if (mdp->vlan_num_ids > 1) { |
| 2332 | /* disable VLAN filter */ |
| 2333 | sh_eth_tsu_write(mdp, 0, vtag_reg_index); |
| 2334 | return 0; |
| 2335 | } |
| 2336 | |
| 2337 | sh_eth_tsu_write(mdp, TSU_VTAG_ENABLE | (vid & TSU_VTAG_VID_MASK), |
| 2338 | vtag_reg_index); |
| 2339 | |
| 2340 | return 0; |
| 2341 | } |
| 2342 | |
Patrick McHardy | 80d5c36 | 2013-04-19 02:04:28 +0000 | [diff] [blame] | 2343 | static int sh_eth_vlan_rx_kill_vid(struct net_device *ndev, |
| 2344 | __be16 proto, u16 vid) |
Yoshihiro Shimoda | 71cc7c3 | 2012-02-15 17:55:06 +0000 | [diff] [blame] | 2345 | { |
| 2346 | struct sh_eth_private *mdp = netdev_priv(ndev); |
| 2347 | int vtag_reg_index = sh_eth_get_vtag_index(mdp); |
| 2348 | |
| 2349 | if (unlikely(!mdp->cd->tsu)) |
| 2350 | return -EPERM; |
| 2351 | |
| 2352 | /* No filtering if vid = 0 */ |
| 2353 | if (!vid) |
| 2354 | return 0; |
| 2355 | |
| 2356 | mdp->vlan_num_ids--; |
| 2357 | sh_eth_tsu_write(mdp, 0, vtag_reg_index); |
| 2358 | |
| 2359 | return 0; |
| 2360 | } |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 2361 | |
| 2362 | /* SuperH's TSU register init function */ |
Yoshihiro Shimoda | 4a55530 | 2011-03-07 21:59:26 +0000 | [diff] [blame] | 2363 | static void sh_eth_tsu_init(struct sh_eth_private *mdp) |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 2364 | { |
Yoshihiro Shimoda | 4a55530 | 2011-03-07 21:59:26 +0000 | [diff] [blame] | 2365 | sh_eth_tsu_write(mdp, 0, TSU_FWEN0); /* Disable forward(0->1) */ |
| 2366 | sh_eth_tsu_write(mdp, 0, TSU_FWEN1); /* Disable forward(1->0) */ |
| 2367 | sh_eth_tsu_write(mdp, 0, TSU_FCM); /* forward fifo 3k-3k */ |
| 2368 | sh_eth_tsu_write(mdp, 0xc, TSU_BSYSL0); |
| 2369 | sh_eth_tsu_write(mdp, 0xc, TSU_BSYSL1); |
| 2370 | sh_eth_tsu_write(mdp, 0, TSU_PRISL0); |
| 2371 | sh_eth_tsu_write(mdp, 0, TSU_PRISL1); |
| 2372 | sh_eth_tsu_write(mdp, 0, TSU_FWSL0); |
| 2373 | sh_eth_tsu_write(mdp, 0, TSU_FWSL1); |
| 2374 | 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] | 2375 | if (sh_eth_is_gether(mdp)) { |
| 2376 | sh_eth_tsu_write(mdp, 0, TSU_QTAG0); /* Disable QTAG(0->1) */ |
| 2377 | sh_eth_tsu_write(mdp, 0, TSU_QTAG1); /* Disable QTAG(1->0) */ |
| 2378 | } else { |
| 2379 | sh_eth_tsu_write(mdp, 0, TSU_QTAGM0); /* Disable QTAG(0->1) */ |
| 2380 | sh_eth_tsu_write(mdp, 0, TSU_QTAGM1); /* Disable QTAG(1->0) */ |
| 2381 | } |
Yoshihiro Shimoda | 4a55530 | 2011-03-07 21:59:26 +0000 | [diff] [blame] | 2382 | sh_eth_tsu_write(mdp, 0, TSU_FWSR); /* all interrupt status clear */ |
| 2383 | sh_eth_tsu_write(mdp, 0, TSU_FWINMK); /* Disable all interrupt */ |
| 2384 | sh_eth_tsu_write(mdp, 0, TSU_TEN); /* Disable all CAM entry */ |
| 2385 | sh_eth_tsu_write(mdp, 0, TSU_POST1); /* Disable CAM entry [ 0- 7] */ |
| 2386 | sh_eth_tsu_write(mdp, 0, TSU_POST2); /* Disable CAM entry [ 8-15] */ |
| 2387 | sh_eth_tsu_write(mdp, 0, TSU_POST3); /* Disable CAM entry [16-23] */ |
| 2388 | 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] | 2389 | } |
| 2390 | |
| 2391 | /* MDIO bus release function */ |
| 2392 | static int sh_mdio_release(struct net_device *ndev) |
| 2393 | { |
| 2394 | struct mii_bus *bus = dev_get_drvdata(&ndev->dev); |
| 2395 | |
| 2396 | /* unregister mdio bus */ |
| 2397 | mdiobus_unregister(bus); |
| 2398 | |
| 2399 | /* remove mdio bus info from net_device */ |
| 2400 | dev_set_drvdata(&ndev->dev, NULL); |
| 2401 | |
| 2402 | /* free bitbang info */ |
| 2403 | free_mdio_bitbang(bus); |
| 2404 | |
| 2405 | return 0; |
| 2406 | } |
| 2407 | |
| 2408 | /* MDIO bus init function */ |
Yoshihiro Shimoda | b3017e6 | 2011-03-07 21:59:55 +0000 | [diff] [blame] | 2409 | static int sh_mdio_init(struct net_device *ndev, int id, |
| 2410 | struct sh_eth_plat_data *pd) |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 2411 | { |
| 2412 | int ret, i; |
| 2413 | struct bb_info *bitbang; |
| 2414 | struct sh_eth_private *mdp = netdev_priv(ndev); |
| 2415 | |
| 2416 | /* create bit control struct for PHY */ |
Sergei Shtylyov | d5e07e6 | 2013-03-21 10:41:11 +0000 | [diff] [blame] | 2417 | bitbang = devm_kzalloc(&ndev->dev, sizeof(struct bb_info), |
| 2418 | GFP_KERNEL); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 2419 | if (!bitbang) { |
| 2420 | ret = -ENOMEM; |
| 2421 | goto out; |
| 2422 | } |
| 2423 | |
| 2424 | /* bitbang init */ |
Yoshihiro Shimoda | ae70644 | 2011-09-27 21:48:58 +0000 | [diff] [blame] | 2425 | bitbang->addr = mdp->addr + mdp->reg_offset[PIR]; |
Yoshihiro Shimoda | b3017e6 | 2011-03-07 21:59:55 +0000 | [diff] [blame] | 2426 | bitbang->set_gate = pd->set_mdio_gate; |
Sergei Shtylyov | dfed5e7 | 2013-03-21 10:37:54 +0000 | [diff] [blame] | 2427 | bitbang->mdi_msk = PIR_MDI; |
| 2428 | bitbang->mdo_msk = PIR_MDO; |
| 2429 | bitbang->mmd_msk = PIR_MMD; |
| 2430 | bitbang->mdc_msk = PIR_MDC; |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 2431 | bitbang->ctrl.ops = &bb_ops; |
| 2432 | |
Stefan Weil | c2e07b3 | 2010-08-03 19:44:52 +0200 | [diff] [blame] | 2433 | /* MII controller setting */ |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 2434 | mdp->mii_bus = alloc_mdio_bitbang(&bitbang->ctrl); |
| 2435 | if (!mdp->mii_bus) { |
| 2436 | ret = -ENOMEM; |
Sergei Shtylyov | d5e07e6 | 2013-03-21 10:41:11 +0000 | [diff] [blame] | 2437 | goto out; |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 2438 | } |
| 2439 | |
| 2440 | /* Hook up MII support for ethtool */ |
| 2441 | mdp->mii_bus->name = "sh_mii"; |
Lennert Buytenhek | 18ee49d | 2008-10-01 15:41:33 +0000 | [diff] [blame] | 2442 | mdp->mii_bus->parent = &ndev->dev; |
Florian Fainelli | 5278fb5 | 2012-01-09 23:59:17 +0000 | [diff] [blame] | 2443 | snprintf(mdp->mii_bus->id, MII_BUS_ID_SIZE, "%s-%x", |
Nobuhiro Iwamatsu | 34aa6f1 | 2012-01-16 16:50:16 +0000 | [diff] [blame] | 2444 | mdp->pdev->name, id); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 2445 | |
| 2446 | /* PHY IRQ */ |
Sergei Shtylyov | d5e07e6 | 2013-03-21 10:41:11 +0000 | [diff] [blame] | 2447 | mdp->mii_bus->irq = devm_kzalloc(&ndev->dev, |
| 2448 | sizeof(int) * PHY_MAX_ADDR, |
| 2449 | GFP_KERNEL); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 2450 | if (!mdp->mii_bus->irq) { |
| 2451 | ret = -ENOMEM; |
| 2452 | goto out_free_bus; |
| 2453 | } |
| 2454 | |
| 2455 | for (i = 0; i < PHY_MAX_ADDR; i++) |
| 2456 | mdp->mii_bus->irq[i] = PHY_POLL; |
| 2457 | |
YOSHIFUJI Hideaki / 吉藤英明 | 8f6352f | 2012-11-02 04:45:07 +0000 | [diff] [blame] | 2458 | /* register mdio bus */ |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 2459 | ret = mdiobus_register(mdp->mii_bus); |
| 2460 | if (ret) |
Sergei Shtylyov | d5e07e6 | 2013-03-21 10:41:11 +0000 | [diff] [blame] | 2461 | goto out_free_bus; |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 2462 | |
| 2463 | dev_set_drvdata(&ndev->dev, mdp->mii_bus); |
| 2464 | |
| 2465 | return 0; |
| 2466 | |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 2467 | out_free_bus: |
Lennert Buytenhek | 298cf9b | 2008-10-08 16:29:57 -0700 | [diff] [blame] | 2468 | free_mdio_bitbang(mdp->mii_bus); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 2469 | |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 2470 | out: |
| 2471 | return ret; |
| 2472 | } |
| 2473 | |
Yoshihiro Shimoda | 4a55530 | 2011-03-07 21:59:26 +0000 | [diff] [blame] | 2474 | static const u16 *sh_eth_get_register_offset(int register_type) |
| 2475 | { |
| 2476 | const u16 *reg_offset = NULL; |
| 2477 | |
| 2478 | switch (register_type) { |
| 2479 | case SH_ETH_REG_GIGABIT: |
| 2480 | reg_offset = sh_eth_offset_gigabit; |
| 2481 | break; |
Sergei Shtylyov | a3f109b | 2013-03-28 11:51:31 +0000 | [diff] [blame] | 2482 | case SH_ETH_REG_FAST_RCAR: |
| 2483 | reg_offset = sh_eth_offset_fast_rcar; |
| 2484 | break; |
Yoshihiro Shimoda | 4a55530 | 2011-03-07 21:59:26 +0000 | [diff] [blame] | 2485 | case SH_ETH_REG_FAST_SH4: |
| 2486 | reg_offset = sh_eth_offset_fast_sh4; |
| 2487 | break; |
| 2488 | case SH_ETH_REG_FAST_SH3_SH2: |
| 2489 | reg_offset = sh_eth_offset_fast_sh3_sh2; |
| 2490 | break; |
| 2491 | default: |
Nobuhiro Iwamatsu | 14c3326 | 2013-03-20 22:46:55 +0000 | [diff] [blame] | 2492 | pr_err("Unknown register type (%d)\n", register_type); |
Yoshihiro Shimoda | 4a55530 | 2011-03-07 21:59:26 +0000 | [diff] [blame] | 2493 | break; |
| 2494 | } |
| 2495 | |
| 2496 | return reg_offset; |
| 2497 | } |
| 2498 | |
Nobuhiro Iwamatsu | 9f86134 | 2013-06-06 09:48:09 +0000 | [diff] [blame] | 2499 | static struct net_device_ops sh_eth_netdev_ops = { |
Alexander Beregalov | ebf84ea | 2009-04-11 07:40:49 +0000 | [diff] [blame] | 2500 | .ndo_open = sh_eth_open, |
| 2501 | .ndo_stop = sh_eth_close, |
| 2502 | .ndo_start_xmit = sh_eth_start_xmit, |
| 2503 | .ndo_get_stats = sh_eth_get_stats, |
Alexander Beregalov | ebf84ea | 2009-04-11 07:40:49 +0000 | [diff] [blame] | 2504 | .ndo_tx_timeout = sh_eth_tx_timeout, |
| 2505 | .ndo_do_ioctl = sh_eth_do_ioctl, |
| 2506 | .ndo_validate_addr = eth_validate_addr, |
| 2507 | .ndo_set_mac_address = eth_mac_addr, |
| 2508 | .ndo_change_mtu = eth_change_mtu, |
| 2509 | }; |
| 2510 | |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 2511 | static int sh_eth_drv_probe(struct platform_device *pdev) |
| 2512 | { |
Kuninori Morimoto | 9c38657 | 2010-08-19 00:39:45 -0700 | [diff] [blame] | 2513 | int ret, devno = 0; |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 2514 | struct resource *res; |
| 2515 | struct net_device *ndev = NULL; |
Kuninori Morimoto | ec0d755 | 2011-06-23 16:02:38 +0000 | [diff] [blame] | 2516 | struct sh_eth_private *mdp = NULL; |
Sergei Shtylyov | 564044b | 2013-03-21 10:39:22 +0000 | [diff] [blame] | 2517 | struct sh_eth_plat_data *pd = pdev->dev.platform_data; |
Sergei Shtylyov | afe391a | 2013-06-07 13:54:02 +0000 | [diff] [blame] | 2518 | const struct platform_device_id *id = platform_get_device_id(pdev); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 2519 | |
| 2520 | /* get base addr */ |
| 2521 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 2522 | if (unlikely(res == NULL)) { |
| 2523 | dev_err(&pdev->dev, "invalid resource\n"); |
| 2524 | ret = -EINVAL; |
| 2525 | goto out; |
| 2526 | } |
| 2527 | |
| 2528 | ndev = alloc_etherdev(sizeof(struct sh_eth_private)); |
| 2529 | if (!ndev) { |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 2530 | ret = -ENOMEM; |
| 2531 | goto out; |
| 2532 | } |
| 2533 | |
| 2534 | /* The sh Ether-specific entries in the device structure. */ |
| 2535 | ndev->base_addr = res->start; |
| 2536 | devno = pdev->id; |
| 2537 | if (devno < 0) |
| 2538 | devno = 0; |
| 2539 | |
| 2540 | ndev->dma = -1; |
roel kluin | cc3c080 | 2008-09-10 19:22:44 +0200 | [diff] [blame] | 2541 | ret = platform_get_irq(pdev, 0); |
| 2542 | if (ret < 0) { |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 2543 | ret = -ENODEV; |
| 2544 | goto out_release; |
| 2545 | } |
roel kluin | cc3c080 | 2008-09-10 19:22:44 +0200 | [diff] [blame] | 2546 | ndev->irq = ret; |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 2547 | |
| 2548 | SET_NETDEV_DEV(ndev, &pdev->dev); |
| 2549 | |
| 2550 | /* Fill in the fields of the device structure with ethernet values. */ |
| 2551 | ether_setup(ndev); |
| 2552 | |
| 2553 | mdp = netdev_priv(ndev); |
Yoshihiro Shimoda | 525b807 | 2012-06-26 20:00:03 +0000 | [diff] [blame] | 2554 | mdp->num_tx_ring = TX_RING_SIZE; |
| 2555 | mdp->num_rx_ring = RX_RING_SIZE; |
Sergei Shtylyov | d5e07e6 | 2013-03-21 10:41:11 +0000 | [diff] [blame] | 2556 | mdp->addr = devm_ioremap_resource(&pdev->dev, res); |
| 2557 | if (IS_ERR(mdp->addr)) { |
| 2558 | ret = PTR_ERR(mdp->addr); |
Yoshihiro Shimoda | ae70644 | 2011-09-27 21:48:58 +0000 | [diff] [blame] | 2559 | goto out_release; |
| 2560 | } |
| 2561 | |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 2562 | spin_lock_init(&mdp->lock); |
Magnus Damm | bcd5149 | 2009-10-09 00:20:04 +0000 | [diff] [blame] | 2563 | mdp->pdev = pdev; |
| 2564 | pm_runtime_enable(&pdev->dev); |
| 2565 | pm_runtime_resume(&pdev->dev); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 2566 | |
| 2567 | /* get PHY ID */ |
Yoshinori Sato | 71557a3 | 2008-08-06 19:49:00 -0400 | [diff] [blame] | 2568 | mdp->phy_id = pd->phy; |
Yoshihiro Shimoda | e47c905 | 2011-03-07 21:59:45 +0000 | [diff] [blame] | 2569 | mdp->phy_interface = pd->phy_interface; |
Yoshinori Sato | 71557a3 | 2008-08-06 19:49:00 -0400 | [diff] [blame] | 2570 | /* EDMAC endian */ |
| 2571 | mdp->edmac_endian = pd->edmac_endian; |
Yoshihiro Shimoda | 4923576 | 2009-08-27 23:25:03 +0000 | [diff] [blame] | 2572 | mdp->no_ether_link = pd->no_ether_link; |
| 2573 | mdp->ether_link_active_low = pd->ether_link_active_low; |
Yoshihiro Shimoda | 4a55530 | 2011-03-07 21:59:26 +0000 | [diff] [blame] | 2574 | mdp->reg_offset = sh_eth_get_register_offset(pd->register_type); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 2575 | |
Yoshihiro Shimoda | 380af9e | 2009-05-24 23:54:21 +0000 | [diff] [blame] | 2576 | /* set cpu data */ |
Yoshihiro Shimoda | 8fcd496 | 2011-03-07 21:59:49 +0000 | [diff] [blame] | 2577 | #if defined(SH_ETH_HAS_BOTH_MODULES) |
| 2578 | mdp->cd = sh_eth_get_cpu_data(mdp); |
| 2579 | #else |
Yoshihiro Shimoda | 380af9e | 2009-05-24 23:54:21 +0000 | [diff] [blame] | 2580 | mdp->cd = &sh_eth_my_cpu_data; |
Yoshihiro Shimoda | 8fcd496 | 2011-03-07 21:59:49 +0000 | [diff] [blame] | 2581 | #endif |
Sergei Shtylyov | afe391a | 2013-06-07 13:54:02 +0000 | [diff] [blame] | 2582 | if (id->driver_data) |
| 2583 | mdp->cd = (struct sh_eth_cpu_data *)id->driver_data; |
Yoshihiro Shimoda | 380af9e | 2009-05-24 23:54:21 +0000 | [diff] [blame] | 2584 | sh_eth_set_default_cpu_data(mdp->cd); |
| 2585 | |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 2586 | /* set function */ |
Nobuhiro Iwamatsu | 9f86134 | 2013-06-06 09:48:09 +0000 | [diff] [blame] | 2587 | if (mdp->cd->tsu) { |
| 2588 | sh_eth_netdev_ops.ndo_set_rx_mode = sh_eth_set_multicast_list; |
| 2589 | sh_eth_netdev_ops.ndo_vlan_rx_add_vid = sh_eth_vlan_rx_add_vid; |
| 2590 | sh_eth_netdev_ops.ndo_vlan_rx_kill_vid = |
| 2591 | sh_eth_vlan_rx_kill_vid; |
| 2592 | } |
| 2593 | |
Alexander Beregalov | ebf84ea | 2009-04-11 07:40:49 +0000 | [diff] [blame] | 2594 | ndev->netdev_ops = &sh_eth_netdev_ops; |
Nobuhiro Iwamatsu | dc19e4e | 2011-02-15 21:17:32 +0000 | [diff] [blame] | 2595 | SET_ETHTOOL_OPS(ndev, &sh_eth_ethtool_ops); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 2596 | ndev->watchdog_timeo = TX_TIMEOUT; |
| 2597 | |
Nobuhiro Iwamatsu | dc19e4e | 2011-02-15 21:17:32 +0000 | [diff] [blame] | 2598 | /* debug message level */ |
| 2599 | mdp->msg_enable = SH_ETH_DEF_MSG_ENABLE; |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 2600 | |
| 2601 | /* read and set MAC address */ |
Magnus Damm | 748031f | 2009-10-09 00:17:14 +0000 | [diff] [blame] | 2602 | read_mac_address(ndev, pd->mac_addr); |
Sergei Shtylyov | ff6e722 | 2013-04-29 09:49:42 +0000 | [diff] [blame] | 2603 | if (!is_valid_ether_addr(ndev->dev_addr)) { |
| 2604 | dev_warn(&pdev->dev, |
| 2605 | "no valid MAC address supplied, using a random one.\n"); |
| 2606 | eth_hw_addr_random(ndev); |
| 2607 | } |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 2608 | |
Yoshihiro Shimoda | 6ba8802 | 2012-02-15 17:55:01 +0000 | [diff] [blame] | 2609 | /* ioremap the TSU registers */ |
| 2610 | if (mdp->cd->tsu) { |
| 2611 | struct resource *rtsu; |
| 2612 | rtsu = platform_get_resource(pdev, IORESOURCE_MEM, 1); |
Sergei Shtylyov | d5e07e6 | 2013-03-21 10:41:11 +0000 | [diff] [blame] | 2613 | mdp->tsu_addr = devm_ioremap_resource(&pdev->dev, rtsu); |
| 2614 | if (IS_ERR(mdp->tsu_addr)) { |
| 2615 | ret = PTR_ERR(mdp->tsu_addr); |
Sergei Shtylyov | fc0c090 | 2013-03-19 13:41:32 +0000 | [diff] [blame] | 2616 | goto out_release; |
| 2617 | } |
Yoshihiro Shimoda | 6743fe6 | 2012-02-15 17:55:03 +0000 | [diff] [blame] | 2618 | mdp->port = devno % 2; |
Patrick McHardy | f646968 | 2013-04-19 02:04:27 +0000 | [diff] [blame] | 2619 | ndev->features = NETIF_F_HW_VLAN_CTAG_FILTER; |
Yoshihiro Shimoda | 6ba8802 | 2012-02-15 17:55:01 +0000 | [diff] [blame] | 2620 | } |
| 2621 | |
Yoshihiro Shimoda | 150647f | 2012-02-15 17:54:56 +0000 | [diff] [blame] | 2622 | /* initialize first or needed device */ |
| 2623 | if (!devno || pd->needs_init) { |
Yoshihiro Shimoda | 380af9e | 2009-05-24 23:54:21 +0000 | [diff] [blame] | 2624 | if (mdp->cd->chip_reset) |
| 2625 | mdp->cd->chip_reset(ndev); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 2626 | |
Yoshihiro Shimoda | 4986b99 | 2011-03-07 21:59:34 +0000 | [diff] [blame] | 2627 | if (mdp->cd->tsu) { |
| 2628 | /* TSU init (Init only)*/ |
| 2629 | sh_eth_tsu_init(mdp); |
| 2630 | } |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 2631 | } |
| 2632 | |
| 2633 | /* network device register */ |
| 2634 | ret = register_netdev(ndev); |
| 2635 | if (ret) |
| 2636 | goto out_release; |
| 2637 | |
| 2638 | /* mdio bus init */ |
Yoshihiro Shimoda | b3017e6 | 2011-03-07 21:59:55 +0000 | [diff] [blame] | 2639 | ret = sh_mdio_init(ndev, pdev->id, pd); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 2640 | if (ret) |
| 2641 | goto out_unregister; |
| 2642 | |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 2643 | /* print device information */ |
H Hartley Sweeten | 6cd9b49 | 2009-12-29 20:10:35 -0800 | [diff] [blame] | 2644 | pr_info("Base address at 0x%x, %pM, IRQ %d.\n", |
| 2645 | (u32)ndev->base_addr, ndev->dev_addr, ndev->irq); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 2646 | |
| 2647 | platform_set_drvdata(pdev, ndev); |
| 2648 | |
| 2649 | return ret; |
| 2650 | |
| 2651 | out_unregister: |
| 2652 | unregister_netdev(ndev); |
| 2653 | |
| 2654 | out_release: |
| 2655 | /* net_dev free */ |
| 2656 | if (ndev) |
| 2657 | free_netdev(ndev); |
| 2658 | |
| 2659 | out: |
| 2660 | return ret; |
| 2661 | } |
| 2662 | |
| 2663 | static int sh_eth_drv_remove(struct platform_device *pdev) |
| 2664 | { |
| 2665 | struct net_device *ndev = platform_get_drvdata(pdev); |
| 2666 | |
| 2667 | sh_mdio_release(ndev); |
| 2668 | unregister_netdev(ndev); |
Magnus Damm | bcd5149 | 2009-10-09 00:20:04 +0000 | [diff] [blame] | 2669 | pm_runtime_disable(&pdev->dev); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 2670 | free_netdev(ndev); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 2671 | |
| 2672 | return 0; |
| 2673 | } |
| 2674 | |
Nobuhiro Iwamatsu | 540ad1b | 2013-06-06 09:52:37 +0000 | [diff] [blame] | 2675 | #ifdef CONFIG_PM |
Magnus Damm | bcd5149 | 2009-10-09 00:20:04 +0000 | [diff] [blame] | 2676 | static int sh_eth_runtime_nop(struct device *dev) |
| 2677 | { |
| 2678 | /* |
| 2679 | * Runtime PM callback shared between ->runtime_suspend() |
| 2680 | * and ->runtime_resume(). Simply returns success. |
| 2681 | * |
| 2682 | * This driver re-initializes all registers after |
| 2683 | * pm_runtime_get_sync() anyway so there is no need |
| 2684 | * to save and restore registers here. |
| 2685 | */ |
| 2686 | return 0; |
| 2687 | } |
| 2688 | |
Nobuhiro Iwamatsu | 540ad1b | 2013-06-06 09:52:37 +0000 | [diff] [blame] | 2689 | static const struct dev_pm_ops sh_eth_dev_pm_ops = { |
Magnus Damm | bcd5149 | 2009-10-09 00:20:04 +0000 | [diff] [blame] | 2690 | .runtime_suspend = sh_eth_runtime_nop, |
| 2691 | .runtime_resume = sh_eth_runtime_nop, |
| 2692 | }; |
Nobuhiro Iwamatsu | 540ad1b | 2013-06-06 09:52:37 +0000 | [diff] [blame] | 2693 | #define SH_ETH_PM_OPS (&sh_eth_dev_pm_ops) |
| 2694 | #else |
| 2695 | #define SH_ETH_PM_OPS NULL |
| 2696 | #endif |
Magnus Damm | bcd5149 | 2009-10-09 00:20:04 +0000 | [diff] [blame] | 2697 | |
Sergei Shtylyov | afe391a | 2013-06-07 13:54:02 +0000 | [diff] [blame] | 2698 | static struct platform_device_id sh_eth_id_table[] = { |
Sergei Shtylyov | c18a79a | 2013-06-07 13:56:05 +0000 | [diff] [blame] | 2699 | { "sh7619-ether", (kernel_ulong_t)&sh7619_data }, |
Sergei Shtylyov | 7bbe150 | 2013-06-07 13:55:08 +0000 | [diff] [blame] | 2700 | { "sh771x-ether", (kernel_ulong_t)&sh771x_data }, |
Sergei Shtylyov | e5c9b4c | 2013-06-07 13:57:12 +0000 | [diff] [blame^] | 2701 | { "r8a7740-gether", (kernel_ulong_t)&r8a7740_data }, |
Sergei Shtylyov | afe391a | 2013-06-07 13:54:02 +0000 | [diff] [blame] | 2702 | { CARDNAME }, |
| 2703 | { } |
| 2704 | }; |
| 2705 | MODULE_DEVICE_TABLE(platform, sh_eth_id_table); |
| 2706 | |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 2707 | static struct platform_driver sh_eth_driver = { |
| 2708 | .probe = sh_eth_drv_probe, |
| 2709 | .remove = sh_eth_drv_remove, |
Sergei Shtylyov | afe391a | 2013-06-07 13:54:02 +0000 | [diff] [blame] | 2710 | .id_table = sh_eth_id_table, |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 2711 | .driver = { |
| 2712 | .name = CARDNAME, |
Nobuhiro Iwamatsu | 540ad1b | 2013-06-06 09:52:37 +0000 | [diff] [blame] | 2713 | .pm = SH_ETH_PM_OPS, |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 2714 | }, |
| 2715 | }; |
| 2716 | |
Axel Lin | db62f68 | 2011-11-27 16:44:17 +0000 | [diff] [blame] | 2717 | module_platform_driver(sh_eth_driver); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 2718 | |
| 2719 | MODULE_AUTHOR("Nobuhiro Iwamatsu, Yoshihiro Shimoda"); |
| 2720 | MODULE_DESCRIPTION("Renesas SuperH Ethernet driver"); |
| 2721 | MODULE_LICENSE("GPL v2"); |