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 | }; |
| 622 | |
Nobuhiro Iwamatsu | f0e81fe | 2012-03-25 18:59:51 +0000 | [diff] [blame] | 623 | |
Yoshihiro Shimoda | 73a0d90 | 2012-04-04 18:37:10 +0000 | [diff] [blame] | 624 | #elif defined(CONFIG_ARCH_R8A7740) |
Nobuhiro Iwamatsu | 5cee1d3 | 2012-06-25 17:35:12 +0000 | [diff] [blame] | 625 | |
Yoshihiro Shimoda | 73a0d90 | 2012-04-04 18:37:10 +0000 | [diff] [blame] | 626 | static void sh_eth_chip_reset(struct net_device *ndev) |
| 627 | { |
| 628 | struct sh_eth_private *mdp = netdev_priv(ndev); |
Yoshihiro Shimoda | 73a0d90 | 2012-04-04 18:37:10 +0000 | [diff] [blame] | 629 | |
| 630 | /* reset device */ |
| 631 | sh_eth_tsu_write(mdp, ARSTR_ARSTR, ARSTR); |
| 632 | mdelay(1); |
| 633 | |
Nobuhiro Iwamatsu | 5e7a76b | 2012-06-25 17:34:14 +0000 | [diff] [blame] | 634 | sh_eth_select_mii(ndev); |
Yoshihiro Shimoda | 73a0d90 | 2012-04-04 18:37:10 +0000 | [diff] [blame] | 635 | } |
| 636 | |
Yoshihiro Shimoda | 73a0d90 | 2012-04-04 18:37:10 +0000 | [diff] [blame] | 637 | static void sh_eth_set_rate(struct net_device *ndev) |
| 638 | { |
| 639 | struct sh_eth_private *mdp = netdev_priv(ndev); |
| 640 | |
| 641 | switch (mdp->speed) { |
| 642 | case 10: /* 10BASE */ |
| 643 | sh_eth_write(ndev, GECMR_10, GECMR); |
| 644 | break; |
| 645 | case 100:/* 100BASE */ |
| 646 | sh_eth_write(ndev, GECMR_100, GECMR); |
| 647 | break; |
| 648 | case 1000: /* 1000BASE */ |
| 649 | sh_eth_write(ndev, GECMR_1000, GECMR); |
| 650 | break; |
| 651 | default: |
| 652 | break; |
| 653 | } |
| 654 | } |
| 655 | |
| 656 | /* R8A7740 */ |
| 657 | static struct sh_eth_cpu_data sh_eth_my_cpu_data = { |
| 658 | .chip_reset = sh_eth_chip_reset, |
| 659 | .set_duplex = sh_eth_set_duplex, |
| 660 | .set_rate = sh_eth_set_rate, |
| 661 | |
| 662 | .ecsr_value = ECSR_ICD | ECSR_MPD, |
| 663 | .ecsipr_value = ECSIPR_LCHNGIP | ECSIPR_ICDIP | ECSIPR_MPDIP, |
| 664 | .eesipr_value = DMAC_M_RFRMER | DMAC_M_ECI | 0x003fffff, |
| 665 | |
| 666 | .tx_check = EESR_TC1 | EESR_FTC, |
| 667 | .eesr_err_check = EESR_TWB1 | EESR_TWB | EESR_TABT | EESR_RABT | \ |
| 668 | EESR_RDE | EESR_RFRMER | EESR_TFE | EESR_TDE | \ |
| 669 | EESR_ECI, |
| 670 | .tx_error_check = EESR_TWB1 | EESR_TWB | EESR_TABT | EESR_TDE | \ |
| 671 | EESR_TFE, |
| 672 | |
| 673 | .apr = 1, |
| 674 | .mpr = 1, |
| 675 | .tpauser = 1, |
| 676 | .bculr = 1, |
| 677 | .hw_swap = 1, |
| 678 | .no_trimd = 1, |
| 679 | .no_ade = 1, |
| 680 | .tsu = 1, |
Nobuhiro Iwamatsu | 5e7a76b | 2012-06-25 17:34:14 +0000 | [diff] [blame] | 681 | .select_mii = 1, |
Yoshihiro Shimoda | 73a0d90 | 2012-04-04 18:37:10 +0000 | [diff] [blame] | 682 | }; |
Sergei Shtylyov | c18a79a | 2013-06-07 13:56:05 +0000 | [diff] [blame^] | 683 | #endif |
Yoshihiro Shimoda | 73a0d90 | 2012-04-04 18:37:10 +0000 | [diff] [blame] | 684 | |
Sergei Shtylyov | c18a79a | 2013-06-07 13:56:05 +0000 | [diff] [blame^] | 685 | static struct sh_eth_cpu_data sh7619_data = { |
Yoshihiro Shimoda | 380af9e | 2009-05-24 23:54:21 +0000 | [diff] [blame] | 686 | .eesipr_value = DMAC_M_RFRMER | DMAC_M_ECI | 0x003fffff, |
| 687 | |
| 688 | .apr = 1, |
| 689 | .mpr = 1, |
| 690 | .tpauser = 1, |
| 691 | .hw_swap = 1, |
| 692 | }; |
Sergei Shtylyov | 7bbe150 | 2013-06-07 13:55:08 +0000 | [diff] [blame] | 693 | |
| 694 | static struct sh_eth_cpu_data sh771x_data = { |
Yoshihiro Shimoda | 380af9e | 2009-05-24 23:54:21 +0000 | [diff] [blame] | 695 | .eesipr_value = DMAC_M_RFRMER | DMAC_M_ECI | 0x003fffff, |
Yoshihiro Shimoda | 4986b99 | 2011-03-07 21:59:34 +0000 | [diff] [blame] | 696 | .tsu = 1, |
Yoshihiro Shimoda | 380af9e | 2009-05-24 23:54:21 +0000 | [diff] [blame] | 697 | }; |
Yoshihiro Shimoda | 380af9e | 2009-05-24 23:54:21 +0000 | [diff] [blame] | 698 | |
| 699 | static void sh_eth_set_default_cpu_data(struct sh_eth_cpu_data *cd) |
| 700 | { |
| 701 | if (!cd->ecsr_value) |
| 702 | cd->ecsr_value = DEFAULT_ECSR_INIT; |
| 703 | |
| 704 | if (!cd->ecsipr_value) |
| 705 | cd->ecsipr_value = DEFAULT_ECSIPR_INIT; |
| 706 | |
| 707 | if (!cd->fcftr_value) |
| 708 | cd->fcftr_value = DEFAULT_FIFO_F_D_RFF | \ |
| 709 | DEFAULT_FIFO_F_D_RFD; |
| 710 | |
| 711 | if (!cd->fdr_value) |
| 712 | cd->fdr_value = DEFAULT_FDR_INIT; |
| 713 | |
| 714 | if (!cd->rmcr_value) |
| 715 | cd->rmcr_value = DEFAULT_RMCR_VALUE; |
| 716 | |
| 717 | if (!cd->tx_check) |
| 718 | cd->tx_check = DEFAULT_TX_CHECK; |
| 719 | |
| 720 | if (!cd->eesr_err_check) |
| 721 | cd->eesr_err_check = DEFAULT_EESR_ERR_CHECK; |
| 722 | |
| 723 | if (!cd->tx_error_check) |
| 724 | cd->tx_error_check = DEFAULT_TX_ERROR_CHECK; |
| 725 | } |
| 726 | |
Nobuhiro Iwamatsu | 5cee1d3 | 2012-06-25 17:35:12 +0000 | [diff] [blame] | 727 | static int sh_eth_check_reset(struct net_device *ndev) |
| 728 | { |
| 729 | int ret = 0; |
| 730 | int cnt = 100; |
| 731 | |
| 732 | while (cnt > 0) { |
| 733 | if (!(sh_eth_read(ndev, EDMR) & 0x3)) |
| 734 | break; |
| 735 | mdelay(1); |
| 736 | cnt--; |
| 737 | } |
| 738 | if (cnt < 0) { |
Nobuhiro Iwamatsu | 14c3326 | 2013-03-20 22:46:55 +0000 | [diff] [blame] | 739 | pr_err("Device reset fail\n"); |
Nobuhiro Iwamatsu | 5cee1d3 | 2012-06-25 17:35:12 +0000 | [diff] [blame] | 740 | ret = -ETIMEDOUT; |
| 741 | } |
| 742 | return ret; |
Yoshihiro Shimoda | 380af9e | 2009-05-24 23:54:21 +0000 | [diff] [blame] | 743 | } |
Nobuhiro Iwamatsu | dabdde9 | 2013-06-06 09:51:39 +0000 | [diff] [blame] | 744 | |
| 745 | static int sh_eth_reset(struct net_device *ndev) |
| 746 | { |
| 747 | struct sh_eth_private *mdp = netdev_priv(ndev); |
| 748 | int ret = 0; |
| 749 | |
| 750 | if (sh_eth_is_gether(mdp)) { |
| 751 | sh_eth_write(ndev, EDSR_ENALL, EDSR); |
| 752 | sh_eth_write(ndev, sh_eth_read(ndev, EDMR) | EDMR_SRST_GETHER, |
| 753 | EDMR); |
| 754 | |
| 755 | ret = sh_eth_check_reset(ndev); |
| 756 | if (ret) |
| 757 | goto out; |
| 758 | |
| 759 | /* Table Init */ |
| 760 | sh_eth_write(ndev, 0x0, TDLAR); |
| 761 | sh_eth_write(ndev, 0x0, TDFAR); |
| 762 | sh_eth_write(ndev, 0x0, TDFXR); |
| 763 | sh_eth_write(ndev, 0x0, TDFFR); |
| 764 | sh_eth_write(ndev, 0x0, RDLAR); |
| 765 | sh_eth_write(ndev, 0x0, RDFAR); |
| 766 | sh_eth_write(ndev, 0x0, RDFXR); |
| 767 | sh_eth_write(ndev, 0x0, RDFFR); |
| 768 | |
| 769 | /* Reset HW CRC register */ |
| 770 | if (mdp->cd->hw_crc) |
| 771 | sh_eth_write(ndev, 0x0, CSMR); |
| 772 | |
| 773 | /* Select MII mode */ |
| 774 | if (mdp->cd->select_mii) |
| 775 | sh_eth_select_mii(ndev); |
| 776 | } else { |
| 777 | sh_eth_write(ndev, sh_eth_read(ndev, EDMR) | EDMR_SRST_ETHER, |
| 778 | EDMR); |
| 779 | mdelay(3); |
| 780 | sh_eth_write(ndev, sh_eth_read(ndev, EDMR) & ~EDMR_SRST_ETHER, |
| 781 | EDMR); |
| 782 | } |
| 783 | |
| 784 | out: |
| 785 | return ret; |
| 786 | } |
Yoshihiro Shimoda | 380af9e | 2009-05-24 23:54:21 +0000 | [diff] [blame] | 787 | |
Yoshihiro Shimoda | 73a0d90 | 2012-04-04 18:37:10 +0000 | [diff] [blame] | 788 | #if defined(CONFIG_CPU_SH4) || defined(CONFIG_ARCH_SHMOBILE) |
Yoshihiro Shimoda | 380af9e | 2009-05-24 23:54:21 +0000 | [diff] [blame] | 789 | static void sh_eth_set_receive_align(struct sk_buff *skb) |
| 790 | { |
| 791 | int reserve; |
| 792 | |
| 793 | reserve = SH4_SKB_RX_ALIGN - ((u32)skb->data & (SH4_SKB_RX_ALIGN - 1)); |
| 794 | if (reserve) |
| 795 | skb_reserve(skb, reserve); |
| 796 | } |
| 797 | #else |
| 798 | static void sh_eth_set_receive_align(struct sk_buff *skb) |
| 799 | { |
| 800 | skb_reserve(skb, SH2_SH3_SKB_RX_ALIGN); |
| 801 | } |
| 802 | #endif |
| 803 | |
| 804 | |
Yoshinori Sato | 71557a3 | 2008-08-06 19:49:00 -0400 | [diff] [blame] | 805 | /* CPU <-> EDMAC endian convert */ |
| 806 | static inline __u32 cpu_to_edmac(struct sh_eth_private *mdp, u32 x) |
| 807 | { |
| 808 | switch (mdp->edmac_endian) { |
| 809 | case EDMAC_LITTLE_ENDIAN: |
| 810 | return cpu_to_le32(x); |
| 811 | case EDMAC_BIG_ENDIAN: |
| 812 | return cpu_to_be32(x); |
| 813 | } |
| 814 | return x; |
| 815 | } |
| 816 | |
| 817 | static inline __u32 edmac_to_cpu(struct sh_eth_private *mdp, u32 x) |
| 818 | { |
| 819 | switch (mdp->edmac_endian) { |
| 820 | case EDMAC_LITTLE_ENDIAN: |
| 821 | return le32_to_cpu(x); |
| 822 | case EDMAC_BIG_ENDIAN: |
| 823 | return be32_to_cpu(x); |
| 824 | } |
| 825 | return x; |
| 826 | } |
| 827 | |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 828 | /* |
| 829 | * Program the hardware MAC address from dev->dev_addr. |
| 830 | */ |
| 831 | static void update_mac_address(struct net_device *ndev) |
| 832 | { |
Yoshihiro Shimoda | 4a55530 | 2011-03-07 21:59:26 +0000 | [diff] [blame] | 833 | sh_eth_write(ndev, |
| 834 | (ndev->dev_addr[0] << 24) | (ndev->dev_addr[1] << 16) | |
| 835 | (ndev->dev_addr[2] << 8) | (ndev->dev_addr[3]), MAHR); |
| 836 | sh_eth_write(ndev, |
| 837 | (ndev->dev_addr[4] << 8) | (ndev->dev_addr[5]), MALR); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 838 | } |
| 839 | |
| 840 | /* |
| 841 | * Get MAC address from SuperH MAC address register |
| 842 | * |
| 843 | * SuperH's Ethernet device doesn't have 'ROM' to MAC address. |
| 844 | * This driver get MAC address that use by bootloader(U-boot or sh-ipl+g). |
| 845 | * When you want use this device, you must set MAC address in bootloader. |
| 846 | * |
| 847 | */ |
Magnus Damm | 748031f | 2009-10-09 00:17:14 +0000 | [diff] [blame] | 848 | static void read_mac_address(struct net_device *ndev, unsigned char *mac) |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 849 | { |
Magnus Damm | 748031f | 2009-10-09 00:17:14 +0000 | [diff] [blame] | 850 | if (mac[0] || mac[1] || mac[2] || mac[3] || mac[4] || mac[5]) { |
| 851 | memcpy(ndev->dev_addr, mac, 6); |
| 852 | } else { |
Yoshihiro Shimoda | 4a55530 | 2011-03-07 21:59:26 +0000 | [diff] [blame] | 853 | ndev->dev_addr[0] = (sh_eth_read(ndev, MAHR) >> 24); |
| 854 | ndev->dev_addr[1] = (sh_eth_read(ndev, MAHR) >> 16) & 0xFF; |
| 855 | ndev->dev_addr[2] = (sh_eth_read(ndev, MAHR) >> 8) & 0xFF; |
| 856 | ndev->dev_addr[3] = (sh_eth_read(ndev, MAHR) & 0xFF); |
| 857 | ndev->dev_addr[4] = (sh_eth_read(ndev, MALR) >> 8) & 0xFF; |
| 858 | ndev->dev_addr[5] = (sh_eth_read(ndev, MALR) & 0xFF); |
Magnus Damm | 748031f | 2009-10-09 00:17:14 +0000 | [diff] [blame] | 859 | } |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 860 | } |
| 861 | |
Yoshihiro Shimoda | c5ed536 | 2011-03-07 21:59:38 +0000 | [diff] [blame] | 862 | static unsigned long sh_eth_get_edtrr_trns(struct sh_eth_private *mdp) |
| 863 | { |
| 864 | if (sh_eth_is_gether(mdp)) |
| 865 | return EDTRR_TRNS_GETHER; |
| 866 | else |
| 867 | return EDTRR_TRNS_ETHER; |
| 868 | } |
| 869 | |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 870 | struct bb_info { |
Yoshihiro Shimoda | ae70644 | 2011-09-27 21:48:58 +0000 | [diff] [blame] | 871 | void (*set_gate)(void *addr); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 872 | struct mdiobb_ctrl ctrl; |
Yoshihiro Shimoda | ae70644 | 2011-09-27 21:48:58 +0000 | [diff] [blame] | 873 | void *addr; |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 874 | u32 mmd_msk;/* MMD */ |
| 875 | u32 mdo_msk; |
| 876 | u32 mdi_msk; |
| 877 | u32 mdc_msk; |
| 878 | }; |
| 879 | |
| 880 | /* PHY bit set */ |
Yoshihiro Shimoda | ae70644 | 2011-09-27 21:48:58 +0000 | [diff] [blame] | 881 | static void bb_set(void *addr, u32 msk) |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 882 | { |
Yoshihiro Shimoda | ae70644 | 2011-09-27 21:48:58 +0000 | [diff] [blame] | 883 | iowrite32(ioread32(addr) | msk, addr); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 884 | } |
| 885 | |
| 886 | /* PHY bit clear */ |
Yoshihiro Shimoda | ae70644 | 2011-09-27 21:48:58 +0000 | [diff] [blame] | 887 | static void bb_clr(void *addr, u32 msk) |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 888 | { |
Yoshihiro Shimoda | ae70644 | 2011-09-27 21:48:58 +0000 | [diff] [blame] | 889 | iowrite32((ioread32(addr) & ~msk), addr); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 890 | } |
| 891 | |
| 892 | /* PHY bit read */ |
Yoshihiro Shimoda | ae70644 | 2011-09-27 21:48:58 +0000 | [diff] [blame] | 893 | static int bb_read(void *addr, u32 msk) |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 894 | { |
Yoshihiro Shimoda | ae70644 | 2011-09-27 21:48:58 +0000 | [diff] [blame] | 895 | return (ioread32(addr) & msk) != 0; |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 896 | } |
| 897 | |
| 898 | /* Data I/O pin control */ |
| 899 | static void sh_mmd_ctrl(struct mdiobb_ctrl *ctrl, int bit) |
| 900 | { |
| 901 | struct bb_info *bitbang = container_of(ctrl, struct bb_info, ctrl); |
Yoshihiro Shimoda | b3017e6 | 2011-03-07 21:59:55 +0000 | [diff] [blame] | 902 | |
| 903 | if (bitbang->set_gate) |
| 904 | bitbang->set_gate(bitbang->addr); |
| 905 | |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 906 | if (bit) |
| 907 | bb_set(bitbang->addr, bitbang->mmd_msk); |
| 908 | else |
| 909 | bb_clr(bitbang->addr, bitbang->mmd_msk); |
| 910 | } |
| 911 | |
| 912 | /* Set bit data*/ |
| 913 | static void sh_set_mdio(struct mdiobb_ctrl *ctrl, int bit) |
| 914 | { |
| 915 | struct bb_info *bitbang = container_of(ctrl, struct bb_info, ctrl); |
| 916 | |
Yoshihiro Shimoda | b3017e6 | 2011-03-07 21:59:55 +0000 | [diff] [blame] | 917 | if (bitbang->set_gate) |
| 918 | bitbang->set_gate(bitbang->addr); |
| 919 | |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 920 | if (bit) |
| 921 | bb_set(bitbang->addr, bitbang->mdo_msk); |
| 922 | else |
| 923 | bb_clr(bitbang->addr, bitbang->mdo_msk); |
| 924 | } |
| 925 | |
| 926 | /* Get bit data*/ |
| 927 | static int sh_get_mdio(struct mdiobb_ctrl *ctrl) |
| 928 | { |
| 929 | struct bb_info *bitbang = container_of(ctrl, struct bb_info, ctrl); |
Yoshihiro Shimoda | b3017e6 | 2011-03-07 21:59:55 +0000 | [diff] [blame] | 930 | |
| 931 | if (bitbang->set_gate) |
| 932 | bitbang->set_gate(bitbang->addr); |
| 933 | |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 934 | return bb_read(bitbang->addr, bitbang->mdi_msk); |
| 935 | } |
| 936 | |
| 937 | /* MDC pin control */ |
| 938 | static void sh_mdc_ctrl(struct mdiobb_ctrl *ctrl, int bit) |
| 939 | { |
| 940 | struct bb_info *bitbang = container_of(ctrl, struct bb_info, ctrl); |
| 941 | |
Yoshihiro Shimoda | b3017e6 | 2011-03-07 21:59:55 +0000 | [diff] [blame] | 942 | if (bitbang->set_gate) |
| 943 | bitbang->set_gate(bitbang->addr); |
| 944 | |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 945 | if (bit) |
| 946 | bb_set(bitbang->addr, bitbang->mdc_msk); |
| 947 | else |
| 948 | bb_clr(bitbang->addr, bitbang->mdc_msk); |
| 949 | } |
| 950 | |
| 951 | /* mdio bus control struct */ |
| 952 | static struct mdiobb_ops bb_ops = { |
| 953 | .owner = THIS_MODULE, |
| 954 | .set_mdc = sh_mdc_ctrl, |
| 955 | .set_mdio_dir = sh_mmd_ctrl, |
| 956 | .set_mdio_data = sh_set_mdio, |
| 957 | .get_mdio_data = sh_get_mdio, |
| 958 | }; |
| 959 | |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 960 | /* free skb and descriptor buffer */ |
| 961 | static void sh_eth_ring_free(struct net_device *ndev) |
| 962 | { |
| 963 | struct sh_eth_private *mdp = netdev_priv(ndev); |
| 964 | int i; |
| 965 | |
| 966 | /* Free Rx skb ringbuffer */ |
| 967 | if (mdp->rx_skbuff) { |
Yoshihiro Shimoda | 525b807 | 2012-06-26 20:00:03 +0000 | [diff] [blame] | 968 | for (i = 0; i < mdp->num_rx_ring; i++) { |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 969 | if (mdp->rx_skbuff[i]) |
| 970 | dev_kfree_skb(mdp->rx_skbuff[i]); |
| 971 | } |
| 972 | } |
| 973 | kfree(mdp->rx_skbuff); |
Yoshihiro Shimoda | 91c7755 | 2012-06-26 20:00:01 +0000 | [diff] [blame] | 974 | mdp->rx_skbuff = NULL; |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 975 | |
| 976 | /* Free Tx skb ringbuffer */ |
| 977 | if (mdp->tx_skbuff) { |
Yoshihiro Shimoda | 525b807 | 2012-06-26 20:00:03 +0000 | [diff] [blame] | 978 | for (i = 0; i < mdp->num_tx_ring; i++) { |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 979 | if (mdp->tx_skbuff[i]) |
| 980 | dev_kfree_skb(mdp->tx_skbuff[i]); |
| 981 | } |
| 982 | } |
| 983 | kfree(mdp->tx_skbuff); |
Yoshihiro Shimoda | 91c7755 | 2012-06-26 20:00:01 +0000 | [diff] [blame] | 984 | mdp->tx_skbuff = NULL; |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 985 | } |
| 986 | |
| 987 | /* format skb and descriptor buffer */ |
| 988 | static void sh_eth_ring_format(struct net_device *ndev) |
| 989 | { |
| 990 | struct sh_eth_private *mdp = netdev_priv(ndev); |
| 991 | int i; |
| 992 | struct sk_buff *skb; |
| 993 | struct sh_eth_rxdesc *rxdesc = NULL; |
| 994 | struct sh_eth_txdesc *txdesc = NULL; |
Yoshihiro Shimoda | 525b807 | 2012-06-26 20:00:03 +0000 | [diff] [blame] | 995 | int rx_ringsize = sizeof(*rxdesc) * mdp->num_rx_ring; |
| 996 | int tx_ringsize = sizeof(*txdesc) * mdp->num_tx_ring; |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 997 | |
| 998 | mdp->cur_rx = mdp->cur_tx = 0; |
| 999 | mdp->dirty_rx = mdp->dirty_tx = 0; |
| 1000 | |
| 1001 | memset(mdp->rx_ring, 0, rx_ringsize); |
| 1002 | |
| 1003 | /* build Rx ring buffer */ |
Yoshihiro Shimoda | 525b807 | 2012-06-26 20:00:03 +0000 | [diff] [blame] | 1004 | for (i = 0; i < mdp->num_rx_ring; i++) { |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1005 | /* skb */ |
| 1006 | mdp->rx_skbuff[i] = NULL; |
Pradeep A. Dalvi | dae2e9f | 2012-02-06 11:16:13 +0000 | [diff] [blame] | 1007 | skb = netdev_alloc_skb(ndev, mdp->rx_buf_sz); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1008 | mdp->rx_skbuff[i] = skb; |
| 1009 | if (skb == NULL) |
| 1010 | break; |
Eric Dumazet | bb7d92e | 2012-02-06 22:17:21 +0000 | [diff] [blame] | 1011 | dma_map_single(&ndev->dev, skb->data, mdp->rx_buf_sz, |
Yoshihiro Shimoda | e88aae7 | 2009-05-24 23:52:35 +0000 | [diff] [blame] | 1012 | DMA_FROM_DEVICE); |
Yoshihiro Shimoda | 380af9e | 2009-05-24 23:54:21 +0000 | [diff] [blame] | 1013 | sh_eth_set_receive_align(skb); |
| 1014 | |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1015 | /* RX descriptor */ |
| 1016 | rxdesc = &mdp->rx_ring[i]; |
Yoshihiro Shimoda | 0029d64 | 2009-05-24 23:53:20 +0000 | [diff] [blame] | 1017 | rxdesc->addr = virt_to_phys(PTR_ALIGN(skb->data, 4)); |
Yoshinori Sato | 71557a3 | 2008-08-06 19:49:00 -0400 | [diff] [blame] | 1018 | rxdesc->status = cpu_to_edmac(mdp, RD_RACT | RD_RFP); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1019 | |
| 1020 | /* The size of the buffer is 16 byte boundary. */ |
Yoshihiro Shimoda | 0029d64 | 2009-05-24 23:53:20 +0000 | [diff] [blame] | 1021 | rxdesc->buffer_length = ALIGN(mdp->rx_buf_sz, 16); |
Nobuhiro Iwamatsu | b0ca2a2 | 2008-06-30 11:08:17 +0900 | [diff] [blame] | 1022 | /* Rx descriptor address set */ |
| 1023 | if (i == 0) { |
Yoshihiro Shimoda | 4a55530 | 2011-03-07 21:59:26 +0000 | [diff] [blame] | 1024 | sh_eth_write(ndev, mdp->rx_desc_dma, RDLAR); |
Yoshihiro Shimoda | c5ed536 | 2011-03-07 21:59:38 +0000 | [diff] [blame] | 1025 | if (sh_eth_is_gether(mdp)) |
| 1026 | sh_eth_write(ndev, mdp->rx_desc_dma, RDFAR); |
Nobuhiro Iwamatsu | b0ca2a2 | 2008-06-30 11:08:17 +0900 | [diff] [blame] | 1027 | } |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1028 | } |
| 1029 | |
Yoshihiro Shimoda | 525b807 | 2012-06-26 20:00:03 +0000 | [diff] [blame] | 1030 | mdp->dirty_rx = (u32) (i - mdp->num_rx_ring); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1031 | |
| 1032 | /* Mark the last entry as wrapping the ring. */ |
Yoshinori Sato | 71557a3 | 2008-08-06 19:49:00 -0400 | [diff] [blame] | 1033 | rxdesc->status |= cpu_to_edmac(mdp, RD_RDEL); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1034 | |
| 1035 | memset(mdp->tx_ring, 0, tx_ringsize); |
| 1036 | |
| 1037 | /* build Tx ring buffer */ |
Yoshihiro Shimoda | 525b807 | 2012-06-26 20:00:03 +0000 | [diff] [blame] | 1038 | for (i = 0; i < mdp->num_tx_ring; i++) { |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1039 | mdp->tx_skbuff[i] = NULL; |
| 1040 | txdesc = &mdp->tx_ring[i]; |
Yoshinori Sato | 71557a3 | 2008-08-06 19:49:00 -0400 | [diff] [blame] | 1041 | txdesc->status = cpu_to_edmac(mdp, TD_TFP); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1042 | txdesc->buffer_length = 0; |
Nobuhiro Iwamatsu | b0ca2a2 | 2008-06-30 11:08:17 +0900 | [diff] [blame] | 1043 | if (i == 0) { |
Yoshinori Sato | 71557a3 | 2008-08-06 19:49:00 -0400 | [diff] [blame] | 1044 | /* Tx descriptor address set */ |
Yoshihiro Shimoda | 4a55530 | 2011-03-07 21:59:26 +0000 | [diff] [blame] | 1045 | sh_eth_write(ndev, mdp->tx_desc_dma, TDLAR); |
Yoshihiro Shimoda | c5ed536 | 2011-03-07 21:59:38 +0000 | [diff] [blame] | 1046 | if (sh_eth_is_gether(mdp)) |
| 1047 | sh_eth_write(ndev, mdp->tx_desc_dma, TDFAR); |
Nobuhiro Iwamatsu | b0ca2a2 | 2008-06-30 11:08:17 +0900 | [diff] [blame] | 1048 | } |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1049 | } |
| 1050 | |
Yoshinori Sato | 71557a3 | 2008-08-06 19:49:00 -0400 | [diff] [blame] | 1051 | txdesc->status |= cpu_to_edmac(mdp, TD_TDLE); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1052 | } |
| 1053 | |
| 1054 | /* Get skb and descriptor buffer */ |
| 1055 | static int sh_eth_ring_init(struct net_device *ndev) |
| 1056 | { |
| 1057 | struct sh_eth_private *mdp = netdev_priv(ndev); |
| 1058 | int rx_ringsize, tx_ringsize, ret = 0; |
| 1059 | |
| 1060 | /* |
| 1061 | * +26 gets the maximum ethernet encapsulation, +7 & ~7 because the |
| 1062 | * card needs room to do 8 byte alignment, +2 so we can reserve |
| 1063 | * the first 2 bytes, and +16 gets room for the status word from the |
| 1064 | * card. |
| 1065 | */ |
| 1066 | mdp->rx_buf_sz = (ndev->mtu <= 1492 ? PKT_BUF_SZ : |
| 1067 | (((ndev->mtu + 26 + 7) & ~7) + 2 + 16)); |
Magnus Damm | 503914c | 2009-12-15 21:16:55 -0800 | [diff] [blame] | 1068 | if (mdp->cd->rpadir) |
| 1069 | mdp->rx_buf_sz += NET_IP_ALIGN; |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1070 | |
| 1071 | /* Allocate RX and TX skb rings */ |
Joe Perches | b2adaca | 2013-02-03 17:43:58 +0000 | [diff] [blame] | 1072 | mdp->rx_skbuff = kmalloc_array(mdp->num_rx_ring, |
| 1073 | sizeof(*mdp->rx_skbuff), GFP_KERNEL); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1074 | if (!mdp->rx_skbuff) { |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1075 | ret = -ENOMEM; |
| 1076 | return ret; |
| 1077 | } |
| 1078 | |
Joe Perches | b2adaca | 2013-02-03 17:43:58 +0000 | [diff] [blame] | 1079 | mdp->tx_skbuff = kmalloc_array(mdp->num_tx_ring, |
| 1080 | sizeof(*mdp->tx_skbuff), GFP_KERNEL); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1081 | if (!mdp->tx_skbuff) { |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1082 | ret = -ENOMEM; |
| 1083 | goto skb_ring_free; |
| 1084 | } |
| 1085 | |
| 1086 | /* Allocate all Rx descriptors. */ |
Yoshihiro Shimoda | 525b807 | 2012-06-26 20:00:03 +0000 | [diff] [blame] | 1087 | rx_ringsize = sizeof(struct sh_eth_rxdesc) * mdp->num_rx_ring; |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1088 | 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] | 1089 | GFP_KERNEL); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1090 | if (!mdp->rx_ring) { |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1091 | ret = -ENOMEM; |
| 1092 | goto desc_ring_free; |
| 1093 | } |
| 1094 | |
| 1095 | mdp->dirty_rx = 0; |
| 1096 | |
| 1097 | /* Allocate all Tx descriptors. */ |
Yoshihiro Shimoda | 525b807 | 2012-06-26 20:00:03 +0000 | [diff] [blame] | 1098 | tx_ringsize = sizeof(struct sh_eth_txdesc) * mdp->num_tx_ring; |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1099 | 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] | 1100 | GFP_KERNEL); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1101 | if (!mdp->tx_ring) { |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1102 | ret = -ENOMEM; |
| 1103 | goto desc_ring_free; |
| 1104 | } |
| 1105 | return ret; |
| 1106 | |
| 1107 | desc_ring_free: |
| 1108 | /* free DMA buffer */ |
| 1109 | dma_free_coherent(NULL, rx_ringsize, mdp->rx_ring, mdp->rx_desc_dma); |
| 1110 | |
| 1111 | skb_ring_free: |
| 1112 | /* Free Rx and Tx skb ring buffer */ |
| 1113 | sh_eth_ring_free(ndev); |
Yoshihiro Shimoda | 91c7755 | 2012-06-26 20:00:01 +0000 | [diff] [blame] | 1114 | mdp->tx_ring = NULL; |
| 1115 | mdp->rx_ring = NULL; |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1116 | |
| 1117 | return ret; |
| 1118 | } |
| 1119 | |
Yoshihiro Shimoda | 91c7755 | 2012-06-26 20:00:01 +0000 | [diff] [blame] | 1120 | static void sh_eth_free_dma_buffer(struct sh_eth_private *mdp) |
| 1121 | { |
| 1122 | int ringsize; |
| 1123 | |
| 1124 | if (mdp->rx_ring) { |
Yoshihiro Shimoda | 525b807 | 2012-06-26 20:00:03 +0000 | [diff] [blame] | 1125 | ringsize = sizeof(struct sh_eth_rxdesc) * mdp->num_rx_ring; |
Yoshihiro Shimoda | 91c7755 | 2012-06-26 20:00:01 +0000 | [diff] [blame] | 1126 | dma_free_coherent(NULL, ringsize, mdp->rx_ring, |
| 1127 | mdp->rx_desc_dma); |
| 1128 | mdp->rx_ring = NULL; |
| 1129 | } |
| 1130 | |
| 1131 | if (mdp->tx_ring) { |
Yoshihiro Shimoda | 525b807 | 2012-06-26 20:00:03 +0000 | [diff] [blame] | 1132 | ringsize = sizeof(struct sh_eth_txdesc) * mdp->num_tx_ring; |
Yoshihiro Shimoda | 91c7755 | 2012-06-26 20:00:01 +0000 | [diff] [blame] | 1133 | dma_free_coherent(NULL, ringsize, mdp->tx_ring, |
| 1134 | mdp->tx_desc_dma); |
| 1135 | mdp->tx_ring = NULL; |
| 1136 | } |
| 1137 | } |
| 1138 | |
Yoshihiro Shimoda | 525b807 | 2012-06-26 20:00:03 +0000 | [diff] [blame] | 1139 | static int sh_eth_dev_init(struct net_device *ndev, bool start) |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1140 | { |
| 1141 | int ret = 0; |
| 1142 | struct sh_eth_private *mdp = netdev_priv(ndev); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1143 | u32 val; |
| 1144 | |
| 1145 | /* Soft Reset */ |
Nobuhiro Iwamatsu | 5cee1d3 | 2012-06-25 17:35:12 +0000 | [diff] [blame] | 1146 | ret = sh_eth_reset(ndev); |
| 1147 | if (ret) |
| 1148 | goto out; |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1149 | |
Nobuhiro Iwamatsu | b0ca2a2 | 2008-06-30 11:08:17 +0900 | [diff] [blame] | 1150 | /* Descriptor format */ |
| 1151 | sh_eth_ring_format(ndev); |
Yoshihiro Shimoda | 380af9e | 2009-05-24 23:54:21 +0000 | [diff] [blame] | 1152 | if (mdp->cd->rpadir) |
Yoshihiro Shimoda | 4a55530 | 2011-03-07 21:59:26 +0000 | [diff] [blame] | 1153 | sh_eth_write(ndev, mdp->cd->rpadir_value, RPADIR); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1154 | |
| 1155 | /* all sh_eth int mask */ |
Yoshihiro Shimoda | 4a55530 | 2011-03-07 21:59:26 +0000 | [diff] [blame] | 1156 | sh_eth_write(ndev, 0, EESIPR); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1157 | |
Yoshihiro Shimoda | 10b9194 | 2012-03-29 19:32:08 +0000 | [diff] [blame] | 1158 | #if defined(__LITTLE_ENDIAN) |
Yoshihiro Shimoda | 380af9e | 2009-05-24 23:54:21 +0000 | [diff] [blame] | 1159 | if (mdp->cd->hw_swap) |
Yoshihiro Shimoda | 4a55530 | 2011-03-07 21:59:26 +0000 | [diff] [blame] | 1160 | sh_eth_write(ndev, EDMR_EL, EDMR); |
Yoshihiro Shimoda | 380af9e | 2009-05-24 23:54:21 +0000 | [diff] [blame] | 1161 | else |
Nobuhiro Iwamatsu | b0ca2a2 | 2008-06-30 11:08:17 +0900 | [diff] [blame] | 1162 | #endif |
Yoshihiro Shimoda | 4a55530 | 2011-03-07 21:59:26 +0000 | [diff] [blame] | 1163 | sh_eth_write(ndev, 0, EDMR); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1164 | |
Nobuhiro Iwamatsu | b0ca2a2 | 2008-06-30 11:08:17 +0900 | [diff] [blame] | 1165 | /* FIFO size set */ |
Yoshihiro Shimoda | 4a55530 | 2011-03-07 21:59:26 +0000 | [diff] [blame] | 1166 | sh_eth_write(ndev, mdp->cd->fdr_value, FDR); |
| 1167 | sh_eth_write(ndev, 0, TFTR); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1168 | |
Nobuhiro Iwamatsu | b0ca2a2 | 2008-06-30 11:08:17 +0900 | [diff] [blame] | 1169 | /* Frame recv control */ |
Yoshihiro Shimoda | 4a55530 | 2011-03-07 21:59:26 +0000 | [diff] [blame] | 1170 | sh_eth_write(ndev, mdp->cd->rmcr_value, RMCR); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1171 | |
Yoshihiro Shimoda | 2ecbb78 | 2012-06-26 19:59:58 +0000 | [diff] [blame] | 1172 | 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] | 1173 | |
Yoshihiro Shimoda | 380af9e | 2009-05-24 23:54:21 +0000 | [diff] [blame] | 1174 | if (mdp->cd->bculr) |
Yoshihiro Shimoda | 4a55530 | 2011-03-07 21:59:26 +0000 | [diff] [blame] | 1175 | sh_eth_write(ndev, 0x800, BCULR); /* Burst sycle set */ |
Nobuhiro Iwamatsu | b0ca2a2 | 2008-06-30 11:08:17 +0900 | [diff] [blame] | 1176 | |
Yoshihiro Shimoda | 4a55530 | 2011-03-07 21:59:26 +0000 | [diff] [blame] | 1177 | sh_eth_write(ndev, mdp->cd->fcftr_value, FCFTR); |
Nobuhiro Iwamatsu | b0ca2a2 | 2008-06-30 11:08:17 +0900 | [diff] [blame] | 1178 | |
Yoshihiro Shimoda | 380af9e | 2009-05-24 23:54:21 +0000 | [diff] [blame] | 1179 | if (!mdp->cd->no_trimd) |
Yoshihiro Shimoda | 4a55530 | 2011-03-07 21:59:26 +0000 | [diff] [blame] | 1180 | sh_eth_write(ndev, 0, TRIMD); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1181 | |
Nobuhiro Iwamatsu | b0ca2a2 | 2008-06-30 11:08:17 +0900 | [diff] [blame] | 1182 | /* Recv frame limit set register */ |
Yoshihiro Shimoda | fdb37a7 | 2012-02-06 23:55:15 +0000 | [diff] [blame] | 1183 | sh_eth_write(ndev, ndev->mtu + ETH_HLEN + VLAN_HLEN + ETH_FCS_LEN, |
| 1184 | RFLR); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1185 | |
Yoshihiro Shimoda | 4a55530 | 2011-03-07 21:59:26 +0000 | [diff] [blame] | 1186 | sh_eth_write(ndev, sh_eth_read(ndev, EESR), EESR); |
Yoshihiro Shimoda | 525b807 | 2012-06-26 20:00:03 +0000 | [diff] [blame] | 1187 | if (start) |
| 1188 | sh_eth_write(ndev, mdp->cd->eesipr_value, EESIPR); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1189 | |
| 1190 | /* PAUSE Prohibition */ |
Yoshihiro Shimoda | 4a55530 | 2011-03-07 21:59:26 +0000 | [diff] [blame] | 1191 | val = (sh_eth_read(ndev, ECMR) & ECMR_DM) | |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1192 | ECMR_ZPF | (mdp->duplex ? ECMR_DM : 0) | ECMR_TE | ECMR_RE; |
| 1193 | |
Yoshihiro Shimoda | 4a55530 | 2011-03-07 21:59:26 +0000 | [diff] [blame] | 1194 | sh_eth_write(ndev, val, ECMR); |
Nobuhiro Iwamatsu | b0ca2a2 | 2008-06-30 11:08:17 +0900 | [diff] [blame] | 1195 | |
Yoshihiro Shimoda | 380af9e | 2009-05-24 23:54:21 +0000 | [diff] [blame] | 1196 | if (mdp->cd->set_rate) |
| 1197 | mdp->cd->set_rate(ndev); |
| 1198 | |
Nobuhiro Iwamatsu | b0ca2a2 | 2008-06-30 11:08:17 +0900 | [diff] [blame] | 1199 | /* E-MAC Status Register clear */ |
Yoshihiro Shimoda | 4a55530 | 2011-03-07 21:59:26 +0000 | [diff] [blame] | 1200 | sh_eth_write(ndev, mdp->cd->ecsr_value, ECSR); |
Nobuhiro Iwamatsu | b0ca2a2 | 2008-06-30 11:08:17 +0900 | [diff] [blame] | 1201 | |
| 1202 | /* E-MAC Interrupt Enable register */ |
Yoshihiro Shimoda | 525b807 | 2012-06-26 20:00:03 +0000 | [diff] [blame] | 1203 | if (start) |
| 1204 | sh_eth_write(ndev, mdp->cd->ecsipr_value, ECSIPR); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1205 | |
| 1206 | /* Set MAC address */ |
| 1207 | update_mac_address(ndev); |
| 1208 | |
| 1209 | /* mask reset */ |
Yoshihiro Shimoda | 380af9e | 2009-05-24 23:54:21 +0000 | [diff] [blame] | 1210 | if (mdp->cd->apr) |
Yoshihiro Shimoda | 4a55530 | 2011-03-07 21:59:26 +0000 | [diff] [blame] | 1211 | sh_eth_write(ndev, APR_AP, APR); |
Yoshihiro Shimoda | 380af9e | 2009-05-24 23:54:21 +0000 | [diff] [blame] | 1212 | if (mdp->cd->mpr) |
Yoshihiro Shimoda | 4a55530 | 2011-03-07 21:59:26 +0000 | [diff] [blame] | 1213 | sh_eth_write(ndev, MPR_MP, MPR); |
Yoshihiro Shimoda | 380af9e | 2009-05-24 23:54:21 +0000 | [diff] [blame] | 1214 | if (mdp->cd->tpauser) |
Yoshihiro Shimoda | 4a55530 | 2011-03-07 21:59:26 +0000 | [diff] [blame] | 1215 | sh_eth_write(ndev, TPAUSER_UNLIMITED, TPAUSER); |
Nobuhiro Iwamatsu | b0ca2a2 | 2008-06-30 11:08:17 +0900 | [diff] [blame] | 1216 | |
Yoshihiro Shimoda | 525b807 | 2012-06-26 20:00:03 +0000 | [diff] [blame] | 1217 | if (start) { |
| 1218 | /* Setting the Rx mode will start the Rx process. */ |
| 1219 | sh_eth_write(ndev, EDRRR_R, EDRRR); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1220 | |
Yoshihiro Shimoda | 525b807 | 2012-06-26 20:00:03 +0000 | [diff] [blame] | 1221 | netif_start_queue(ndev); |
| 1222 | } |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1223 | |
Nobuhiro Iwamatsu | 5cee1d3 | 2012-06-25 17:35:12 +0000 | [diff] [blame] | 1224 | out: |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1225 | return ret; |
| 1226 | } |
| 1227 | |
| 1228 | /* free Tx skb function */ |
| 1229 | static int sh_eth_txfree(struct net_device *ndev) |
| 1230 | { |
| 1231 | struct sh_eth_private *mdp = netdev_priv(ndev); |
| 1232 | struct sh_eth_txdesc *txdesc; |
| 1233 | int freeNum = 0; |
| 1234 | int entry = 0; |
| 1235 | |
| 1236 | for (; mdp->cur_tx - mdp->dirty_tx > 0; mdp->dirty_tx++) { |
Yoshihiro Shimoda | 525b807 | 2012-06-26 20:00:03 +0000 | [diff] [blame] | 1237 | entry = mdp->dirty_tx % mdp->num_tx_ring; |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1238 | txdesc = &mdp->tx_ring[entry]; |
Yoshinori Sato | 71557a3 | 2008-08-06 19:49:00 -0400 | [diff] [blame] | 1239 | if (txdesc->status & cpu_to_edmac(mdp, TD_TACT)) |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1240 | break; |
| 1241 | /* Free the original skb. */ |
| 1242 | if (mdp->tx_skbuff[entry]) { |
Yoshihiro Shimoda | 31fcb99 | 2011-06-30 22:52:13 +0000 | [diff] [blame] | 1243 | dma_unmap_single(&ndev->dev, txdesc->addr, |
| 1244 | txdesc->buffer_length, DMA_TO_DEVICE); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1245 | dev_kfree_skb_irq(mdp->tx_skbuff[entry]); |
| 1246 | mdp->tx_skbuff[entry] = NULL; |
| 1247 | freeNum++; |
| 1248 | } |
Yoshinori Sato | 71557a3 | 2008-08-06 19:49:00 -0400 | [diff] [blame] | 1249 | txdesc->status = cpu_to_edmac(mdp, TD_TFP); |
Yoshihiro Shimoda | 525b807 | 2012-06-26 20:00:03 +0000 | [diff] [blame] | 1250 | if (entry >= mdp->num_tx_ring - 1) |
Yoshinori Sato | 71557a3 | 2008-08-06 19:49:00 -0400 | [diff] [blame] | 1251 | txdesc->status |= cpu_to_edmac(mdp, TD_TDLE); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1252 | |
Eric Dumazet | bb7d92e | 2012-02-06 22:17:21 +0000 | [diff] [blame] | 1253 | ndev->stats.tx_packets++; |
| 1254 | ndev->stats.tx_bytes += txdesc->buffer_length; |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1255 | } |
| 1256 | return freeNum; |
| 1257 | } |
| 1258 | |
| 1259 | /* Packet receive function */ |
Yoshihiro Shimoda | a18e08b | 2012-06-20 15:26:34 +0000 | [diff] [blame] | 1260 | static int sh_eth_rx(struct net_device *ndev, u32 intr_status) |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1261 | { |
| 1262 | struct sh_eth_private *mdp = netdev_priv(ndev); |
| 1263 | struct sh_eth_rxdesc *rxdesc; |
| 1264 | |
Yoshihiro Shimoda | 525b807 | 2012-06-26 20:00:03 +0000 | [diff] [blame] | 1265 | int entry = mdp->cur_rx % mdp->num_rx_ring; |
| 1266 | int boguscnt = (mdp->dirty_rx + mdp->num_rx_ring) - mdp->cur_rx; |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1267 | struct sk_buff *skb; |
| 1268 | u16 pkt_len = 0; |
Yoshihiro Shimoda | 380af9e | 2009-05-24 23:54:21 +0000 | [diff] [blame] | 1269 | u32 desc_status; |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1270 | |
| 1271 | rxdesc = &mdp->rx_ring[entry]; |
Yoshinori Sato | 71557a3 | 2008-08-06 19:49:00 -0400 | [diff] [blame] | 1272 | while (!(rxdesc->status & cpu_to_edmac(mdp, RD_RACT))) { |
| 1273 | desc_status = edmac_to_cpu(mdp, rxdesc->status); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1274 | pkt_len = rxdesc->frame_length; |
| 1275 | |
Yoshihiro Shimoda | 73a0d90 | 2012-04-04 18:37:10 +0000 | [diff] [blame] | 1276 | #if defined(CONFIG_ARCH_R8A7740) |
| 1277 | desc_status >>= 16; |
| 1278 | #endif |
| 1279 | |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1280 | if (--boguscnt < 0) |
| 1281 | break; |
| 1282 | |
| 1283 | if (!(desc_status & RDFEND)) |
Eric Dumazet | bb7d92e | 2012-02-06 22:17:21 +0000 | [diff] [blame] | 1284 | ndev->stats.rx_length_errors++; |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1285 | |
| 1286 | if (desc_status & (RD_RFS1 | RD_RFS2 | RD_RFS3 | RD_RFS4 | |
| 1287 | RD_RFS5 | RD_RFS6 | RD_RFS10)) { |
Eric Dumazet | bb7d92e | 2012-02-06 22:17:21 +0000 | [diff] [blame] | 1288 | ndev->stats.rx_errors++; |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1289 | if (desc_status & RD_RFS1) |
Eric Dumazet | bb7d92e | 2012-02-06 22:17:21 +0000 | [diff] [blame] | 1290 | ndev->stats.rx_crc_errors++; |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1291 | if (desc_status & RD_RFS2) |
Eric Dumazet | bb7d92e | 2012-02-06 22:17:21 +0000 | [diff] [blame] | 1292 | ndev->stats.rx_frame_errors++; |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1293 | if (desc_status & RD_RFS3) |
Eric Dumazet | bb7d92e | 2012-02-06 22:17:21 +0000 | [diff] [blame] | 1294 | ndev->stats.rx_length_errors++; |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1295 | if (desc_status & RD_RFS4) |
Eric Dumazet | bb7d92e | 2012-02-06 22:17:21 +0000 | [diff] [blame] | 1296 | ndev->stats.rx_length_errors++; |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1297 | if (desc_status & RD_RFS6) |
Eric Dumazet | bb7d92e | 2012-02-06 22:17:21 +0000 | [diff] [blame] | 1298 | ndev->stats.rx_missed_errors++; |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1299 | if (desc_status & RD_RFS10) |
Eric Dumazet | bb7d92e | 2012-02-06 22:17:21 +0000 | [diff] [blame] | 1300 | ndev->stats.rx_over_errors++; |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1301 | } else { |
Yoshihiro Shimoda | 380af9e | 2009-05-24 23:54:21 +0000 | [diff] [blame] | 1302 | if (!mdp->cd->hw_swap) |
| 1303 | sh_eth_soft_swap( |
| 1304 | phys_to_virt(ALIGN(rxdesc->addr, 4)), |
| 1305 | pkt_len + 2); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1306 | skb = mdp->rx_skbuff[entry]; |
| 1307 | mdp->rx_skbuff[entry] = NULL; |
Magnus Damm | 503914c | 2009-12-15 21:16:55 -0800 | [diff] [blame] | 1308 | if (mdp->cd->rpadir) |
| 1309 | skb_reserve(skb, NET_IP_ALIGN); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1310 | skb_put(skb, pkt_len); |
| 1311 | skb->protocol = eth_type_trans(skb, ndev); |
| 1312 | netif_rx(skb); |
Eric Dumazet | bb7d92e | 2012-02-06 22:17:21 +0000 | [diff] [blame] | 1313 | ndev->stats.rx_packets++; |
| 1314 | ndev->stats.rx_bytes += pkt_len; |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1315 | } |
Yoshinori Sato | 71557a3 | 2008-08-06 19:49:00 -0400 | [diff] [blame] | 1316 | rxdesc->status |= cpu_to_edmac(mdp, RD_RACT); |
Yoshihiro Shimoda | 525b807 | 2012-06-26 20:00:03 +0000 | [diff] [blame] | 1317 | entry = (++mdp->cur_rx) % mdp->num_rx_ring; |
Yoshihiro Shimoda | 862df49 | 2009-05-24 23:53:40 +0000 | [diff] [blame] | 1318 | rxdesc = &mdp->rx_ring[entry]; |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1319 | } |
| 1320 | |
| 1321 | /* Refill the Rx ring buffers. */ |
| 1322 | for (; mdp->cur_rx - mdp->dirty_rx > 0; mdp->dirty_rx++) { |
Yoshihiro Shimoda | 525b807 | 2012-06-26 20:00:03 +0000 | [diff] [blame] | 1323 | entry = mdp->dirty_rx % mdp->num_rx_ring; |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1324 | rxdesc = &mdp->rx_ring[entry]; |
Nobuhiro Iwamatsu | b0ca2a2 | 2008-06-30 11:08:17 +0900 | [diff] [blame] | 1325 | /* The size of the buffer is 16 byte boundary. */ |
Yoshihiro Shimoda | 0029d64 | 2009-05-24 23:53:20 +0000 | [diff] [blame] | 1326 | rxdesc->buffer_length = ALIGN(mdp->rx_buf_sz, 16); |
Nobuhiro Iwamatsu | b0ca2a2 | 2008-06-30 11:08:17 +0900 | [diff] [blame] | 1327 | |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1328 | if (mdp->rx_skbuff[entry] == NULL) { |
Pradeep A. Dalvi | dae2e9f | 2012-02-06 11:16:13 +0000 | [diff] [blame] | 1329 | skb = netdev_alloc_skb(ndev, mdp->rx_buf_sz); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1330 | mdp->rx_skbuff[entry] = skb; |
| 1331 | if (skb == NULL) |
| 1332 | break; /* Better luck next round. */ |
Eric Dumazet | bb7d92e | 2012-02-06 22:17:21 +0000 | [diff] [blame] | 1333 | dma_map_single(&ndev->dev, skb->data, mdp->rx_buf_sz, |
Yoshihiro Shimoda | e88aae7 | 2009-05-24 23:52:35 +0000 | [diff] [blame] | 1334 | DMA_FROM_DEVICE); |
Yoshihiro Shimoda | 380af9e | 2009-05-24 23:54:21 +0000 | [diff] [blame] | 1335 | sh_eth_set_receive_align(skb); |
| 1336 | |
Eric Dumazet | bc8acf2 | 2010-09-02 13:07:41 -0700 | [diff] [blame] | 1337 | skb_checksum_none_assert(skb); |
Yoshihiro Shimoda | 0029d64 | 2009-05-24 23:53:20 +0000 | [diff] [blame] | 1338 | rxdesc->addr = virt_to_phys(PTR_ALIGN(skb->data, 4)); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1339 | } |
Yoshihiro Shimoda | 525b807 | 2012-06-26 20:00:03 +0000 | [diff] [blame] | 1340 | if (entry >= mdp->num_rx_ring - 1) |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1341 | rxdesc->status |= |
Yoshinori Sato | 71557a3 | 2008-08-06 19:49:00 -0400 | [diff] [blame] | 1342 | cpu_to_edmac(mdp, RD_RACT | RD_RFP | RD_RDEL); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1343 | else |
| 1344 | rxdesc->status |= |
Yoshinori Sato | 71557a3 | 2008-08-06 19:49:00 -0400 | [diff] [blame] | 1345 | cpu_to_edmac(mdp, RD_RACT | RD_RFP); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1346 | } |
| 1347 | |
| 1348 | /* Restart Rx engine if stopped. */ |
| 1349 | /* If we don't need to check status, don't. -KDU */ |
Yoshihiro Shimoda | 79fba9f | 2012-05-28 23:07:55 +0000 | [diff] [blame] | 1350 | if (!(sh_eth_read(ndev, EDRRR) & EDRRR_R)) { |
Yoshihiro Shimoda | a18e08b | 2012-06-20 15:26:34 +0000 | [diff] [blame] | 1351 | /* fix the values for the next receiving if RDE is set */ |
| 1352 | if (intr_status & EESR_RDE) |
| 1353 | mdp->cur_rx = mdp->dirty_rx = |
| 1354 | (sh_eth_read(ndev, RDFAR) - |
| 1355 | sh_eth_read(ndev, RDLAR)) >> 4; |
Yoshihiro Shimoda | 4a55530 | 2011-03-07 21:59:26 +0000 | [diff] [blame] | 1356 | sh_eth_write(ndev, EDRRR_R, EDRRR); |
Yoshihiro Shimoda | 79fba9f | 2012-05-28 23:07:55 +0000 | [diff] [blame] | 1357 | } |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1358 | |
| 1359 | return 0; |
| 1360 | } |
| 1361 | |
Yoshihiro Shimoda | 4a55530 | 2011-03-07 21:59:26 +0000 | [diff] [blame] | 1362 | static void sh_eth_rcv_snd_disable(struct net_device *ndev) |
Nobuhiro Iwamatsu | dc19e4e | 2011-02-15 21:17:32 +0000 | [diff] [blame] | 1363 | { |
| 1364 | /* disable tx and rx */ |
Yoshihiro Shimoda | 4a55530 | 2011-03-07 21:59:26 +0000 | [diff] [blame] | 1365 | sh_eth_write(ndev, sh_eth_read(ndev, ECMR) & |
| 1366 | ~(ECMR_RE | ECMR_TE), ECMR); |
Nobuhiro Iwamatsu | dc19e4e | 2011-02-15 21:17:32 +0000 | [diff] [blame] | 1367 | } |
| 1368 | |
Yoshihiro Shimoda | 4a55530 | 2011-03-07 21:59:26 +0000 | [diff] [blame] | 1369 | static void sh_eth_rcv_snd_enable(struct net_device *ndev) |
Nobuhiro Iwamatsu | dc19e4e | 2011-02-15 21:17:32 +0000 | [diff] [blame] | 1370 | { |
| 1371 | /* enable tx and rx */ |
Yoshihiro Shimoda | 4a55530 | 2011-03-07 21:59:26 +0000 | [diff] [blame] | 1372 | sh_eth_write(ndev, sh_eth_read(ndev, ECMR) | |
| 1373 | (ECMR_RE | ECMR_TE), ECMR); |
Nobuhiro Iwamatsu | dc19e4e | 2011-02-15 21:17:32 +0000 | [diff] [blame] | 1374 | } |
| 1375 | |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1376 | /* error control function */ |
| 1377 | static void sh_eth_error(struct net_device *ndev, int intr_status) |
| 1378 | { |
| 1379 | struct sh_eth_private *mdp = netdev_priv(ndev); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1380 | u32 felic_stat; |
Yoshihiro Shimoda | 380af9e | 2009-05-24 23:54:21 +0000 | [diff] [blame] | 1381 | u32 link_stat; |
| 1382 | u32 mask; |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1383 | |
| 1384 | if (intr_status & EESR_ECI) { |
Yoshihiro Shimoda | 4a55530 | 2011-03-07 21:59:26 +0000 | [diff] [blame] | 1385 | felic_stat = sh_eth_read(ndev, ECSR); |
| 1386 | sh_eth_write(ndev, felic_stat, ECSR); /* clear int */ |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1387 | if (felic_stat & ECSR_ICD) |
Eric Dumazet | bb7d92e | 2012-02-06 22:17:21 +0000 | [diff] [blame] | 1388 | ndev->stats.tx_carrier_errors++; |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1389 | if (felic_stat & ECSR_LCHNG) { |
| 1390 | /* Link Changed */ |
Yoshihiro Shimoda | 4923576 | 2009-08-27 23:25:03 +0000 | [diff] [blame] | 1391 | if (mdp->cd->no_psr || mdp->no_ether_link) { |
Sergei Shtylyov | 1e1b812 | 2013-03-31 09:50:07 +0000 | [diff] [blame] | 1392 | goto ignore_link; |
Yoshihiro Shimoda | 380af9e | 2009-05-24 23:54:21 +0000 | [diff] [blame] | 1393 | } else { |
Yoshihiro Shimoda | 4a55530 | 2011-03-07 21:59:26 +0000 | [diff] [blame] | 1394 | link_stat = (sh_eth_read(ndev, PSR)); |
Yoshihiro Shimoda | 4923576 | 2009-08-27 23:25:03 +0000 | [diff] [blame] | 1395 | if (mdp->ether_link_active_low) |
| 1396 | link_stat = ~link_stat; |
Yoshihiro Shimoda | 380af9e | 2009-05-24 23:54:21 +0000 | [diff] [blame] | 1397 | } |
Nobuhiro Iwamatsu | dc19e4e | 2011-02-15 21:17:32 +0000 | [diff] [blame] | 1398 | if (!(link_stat & PHY_ST_LINK)) |
Yoshihiro Shimoda | 4a55530 | 2011-03-07 21:59:26 +0000 | [diff] [blame] | 1399 | sh_eth_rcv_snd_disable(ndev); |
Nobuhiro Iwamatsu | dc19e4e | 2011-02-15 21:17:32 +0000 | [diff] [blame] | 1400 | else { |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1401 | /* Link Up */ |
Yoshihiro Shimoda | 4a55530 | 2011-03-07 21:59:26 +0000 | [diff] [blame] | 1402 | sh_eth_write(ndev, sh_eth_read(ndev, EESIPR) & |
| 1403 | ~DMAC_M_ECI, EESIPR); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1404 | /*clear int */ |
Yoshihiro Shimoda | 4a55530 | 2011-03-07 21:59:26 +0000 | [diff] [blame] | 1405 | sh_eth_write(ndev, sh_eth_read(ndev, ECSR), |
| 1406 | ECSR); |
| 1407 | sh_eth_write(ndev, sh_eth_read(ndev, EESIPR) | |
| 1408 | DMAC_M_ECI, EESIPR); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1409 | /* enable tx and rx */ |
Yoshihiro Shimoda | 4a55530 | 2011-03-07 21:59:26 +0000 | [diff] [blame] | 1410 | sh_eth_rcv_snd_enable(ndev); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1411 | } |
| 1412 | } |
| 1413 | } |
| 1414 | |
Sergei Shtylyov | 1e1b812 | 2013-03-31 09:50:07 +0000 | [diff] [blame] | 1415 | ignore_link: |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1416 | if (intr_status & EESR_TWB) { |
| 1417 | /* Write buck end. unused write back interrupt */ |
| 1418 | if (intr_status & EESR_TABT) /* Transmit Abort int */ |
Eric Dumazet | bb7d92e | 2012-02-06 22:17:21 +0000 | [diff] [blame] | 1419 | ndev->stats.tx_aborted_errors++; |
Nobuhiro Iwamatsu | dc19e4e | 2011-02-15 21:17:32 +0000 | [diff] [blame] | 1420 | if (netif_msg_tx_err(mdp)) |
| 1421 | dev_err(&ndev->dev, "Transmit Abort\n"); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1422 | } |
| 1423 | |
| 1424 | if (intr_status & EESR_RABT) { |
| 1425 | /* Receive Abort int */ |
| 1426 | if (intr_status & EESR_RFRMER) { |
| 1427 | /* Receive Frame Overflow int */ |
Eric Dumazet | bb7d92e | 2012-02-06 22:17:21 +0000 | [diff] [blame] | 1428 | ndev->stats.rx_frame_errors++; |
Nobuhiro Iwamatsu | dc19e4e | 2011-02-15 21:17:32 +0000 | [diff] [blame] | 1429 | if (netif_msg_rx_err(mdp)) |
| 1430 | dev_err(&ndev->dev, "Receive Abort\n"); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1431 | } |
| 1432 | } |
Yoshihiro Shimoda | 380af9e | 2009-05-24 23:54:21 +0000 | [diff] [blame] | 1433 | |
Nobuhiro Iwamatsu | dc19e4e | 2011-02-15 21:17:32 +0000 | [diff] [blame] | 1434 | if (intr_status & EESR_TDE) { |
| 1435 | /* Transmit Descriptor Empty int */ |
Eric Dumazet | bb7d92e | 2012-02-06 22:17:21 +0000 | [diff] [blame] | 1436 | ndev->stats.tx_fifo_errors++; |
Nobuhiro Iwamatsu | dc19e4e | 2011-02-15 21:17:32 +0000 | [diff] [blame] | 1437 | if (netif_msg_tx_err(mdp)) |
| 1438 | dev_err(&ndev->dev, "Transmit Descriptor Empty\n"); |
| 1439 | } |
| 1440 | |
| 1441 | if (intr_status & EESR_TFE) { |
| 1442 | /* FIFO under flow */ |
Eric Dumazet | bb7d92e | 2012-02-06 22:17:21 +0000 | [diff] [blame] | 1443 | ndev->stats.tx_fifo_errors++; |
Nobuhiro Iwamatsu | dc19e4e | 2011-02-15 21:17:32 +0000 | [diff] [blame] | 1444 | if (netif_msg_tx_err(mdp)) |
| 1445 | dev_err(&ndev->dev, "Transmit FIFO Under flow\n"); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1446 | } |
| 1447 | |
| 1448 | if (intr_status & EESR_RDE) { |
| 1449 | /* Receive Descriptor Empty int */ |
Eric Dumazet | bb7d92e | 2012-02-06 22:17:21 +0000 | [diff] [blame] | 1450 | ndev->stats.rx_over_errors++; |
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 | if (netif_msg_rx_err(mdp)) |
| 1453 | dev_err(&ndev->dev, "Receive Descriptor Empty\n"); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1454 | } |
Nobuhiro Iwamatsu | dc19e4e | 2011-02-15 21:17:32 +0000 | [diff] [blame] | 1455 | |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1456 | if (intr_status & EESR_RFE) { |
| 1457 | /* Receive FIFO Overflow int */ |
Eric Dumazet | bb7d92e | 2012-02-06 22:17:21 +0000 | [diff] [blame] | 1458 | ndev->stats.rx_fifo_errors++; |
Nobuhiro Iwamatsu | dc19e4e | 2011-02-15 21:17:32 +0000 | [diff] [blame] | 1459 | if (netif_msg_rx_err(mdp)) |
| 1460 | dev_err(&ndev->dev, "Receive FIFO Overflow\n"); |
| 1461 | } |
| 1462 | |
| 1463 | if (!mdp->cd->no_ade && (intr_status & EESR_ADE)) { |
| 1464 | /* Address Error */ |
Eric Dumazet | bb7d92e | 2012-02-06 22:17:21 +0000 | [diff] [blame] | 1465 | ndev->stats.tx_fifo_errors++; |
Nobuhiro Iwamatsu | dc19e4e | 2011-02-15 21:17:32 +0000 | [diff] [blame] | 1466 | if (netif_msg_tx_err(mdp)) |
| 1467 | dev_err(&ndev->dev, "Address Error\n"); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1468 | } |
Yoshihiro Shimoda | 380af9e | 2009-05-24 23:54:21 +0000 | [diff] [blame] | 1469 | |
| 1470 | mask = EESR_TWB | EESR_TABT | EESR_ADE | EESR_TDE | EESR_TFE; |
| 1471 | if (mdp->cd->no_ade) |
| 1472 | mask &= ~EESR_ADE; |
| 1473 | if (intr_status & mask) { |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1474 | /* Tx error */ |
Yoshihiro Shimoda | 4a55530 | 2011-03-07 21:59:26 +0000 | [diff] [blame] | 1475 | u32 edtrr = sh_eth_read(ndev, EDTRR); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1476 | /* dmesg */ |
Yoshihiro Shimoda | 380af9e | 2009-05-24 23:54:21 +0000 | [diff] [blame] | 1477 | dev_err(&ndev->dev, "TX error. status=%8.8x cur_tx=%8.8x ", |
| 1478 | intr_status, mdp->cur_tx); |
| 1479 | 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] | 1480 | mdp->dirty_tx, (u32) ndev->state, edtrr); |
| 1481 | /* dirty buffer free */ |
| 1482 | sh_eth_txfree(ndev); |
| 1483 | |
| 1484 | /* SH7712 BUG */ |
Yoshihiro Shimoda | c5ed536 | 2011-03-07 21:59:38 +0000 | [diff] [blame] | 1485 | if (edtrr ^ sh_eth_get_edtrr_trns(mdp)) { |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1486 | /* tx dma start */ |
Yoshihiro Shimoda | c5ed536 | 2011-03-07 21:59:38 +0000 | [diff] [blame] | 1487 | sh_eth_write(ndev, sh_eth_get_edtrr_trns(mdp), EDTRR); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1488 | } |
| 1489 | /* wakeup */ |
| 1490 | netif_wake_queue(ndev); |
| 1491 | } |
| 1492 | } |
| 1493 | |
| 1494 | static irqreturn_t sh_eth_interrupt(int irq, void *netdev) |
| 1495 | { |
| 1496 | struct net_device *ndev = netdev; |
| 1497 | struct sh_eth_private *mdp = netdev_priv(ndev); |
Yoshihiro Shimoda | 380af9e | 2009-05-24 23:54:21 +0000 | [diff] [blame] | 1498 | struct sh_eth_cpu_data *cd = mdp->cd; |
Nobuhiro Iwamatsu | 0e0fde3 | 2009-03-16 19:50:57 +0000 | [diff] [blame] | 1499 | irqreturn_t ret = IRQ_NONE; |
Sergei Shtylyov | 3893b27345 | 2013-03-31 09:54:20 +0000 | [diff] [blame] | 1500 | unsigned long intr_status; |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1501 | |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1502 | spin_lock(&mdp->lock); |
| 1503 | |
Sergei Shtylyov | 3893b27345 | 2013-03-31 09:54:20 +0000 | [diff] [blame] | 1504 | /* Get interrupt status */ |
Yoshihiro Shimoda | 4a55530 | 2011-03-07 21:59:26 +0000 | [diff] [blame] | 1505 | intr_status = sh_eth_read(ndev, EESR); |
Sergei Shtylyov | 3893b27345 | 2013-03-31 09:54:20 +0000 | [diff] [blame] | 1506 | /* Mask it with the interrupt mask, forcing ECI interrupt to be always |
| 1507 | * enabled since it's the one that comes thru regardless of the mask, |
| 1508 | * and we need to fully handle it in sh_eth_error() in order to quench |
| 1509 | * it as it doesn't get cleared by just writing 1 to the ECI bit... |
| 1510 | */ |
| 1511 | intr_status &= sh_eth_read(ndev, EESIPR) | DMAC_M_ECI; |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1512 | /* Clear interrupt */ |
Nobuhiro Iwamatsu | 0e0fde3 | 2009-03-16 19:50:57 +0000 | [diff] [blame] | 1513 | if (intr_status & (EESR_FRC | EESR_RMAF | EESR_RRF | |
| 1514 | EESR_RTLF | EESR_RTSF | EESR_PRE | EESR_CERF | |
Yoshihiro Shimoda | 380af9e | 2009-05-24 23:54:21 +0000 | [diff] [blame] | 1515 | cd->tx_check | cd->eesr_err_check)) { |
Yoshihiro Shimoda | 4a55530 | 2011-03-07 21:59:26 +0000 | [diff] [blame] | 1516 | sh_eth_write(ndev, intr_status, EESR); |
Nobuhiro Iwamatsu | 0e0fde3 | 2009-03-16 19:50:57 +0000 | [diff] [blame] | 1517 | ret = IRQ_HANDLED; |
| 1518 | } else |
| 1519 | goto other_irq; |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1520 | |
Nobuhiro Iwamatsu | b0ca2a2 | 2008-06-30 11:08:17 +0900 | [diff] [blame] | 1521 | if (intr_status & (EESR_FRC | /* Frame recv*/ |
| 1522 | EESR_RMAF | /* Multi cast address recv*/ |
| 1523 | EESR_RRF | /* Bit frame recv */ |
| 1524 | EESR_RTLF | /* Long frame recv*/ |
| 1525 | EESR_RTSF | /* short frame recv */ |
| 1526 | EESR_PRE | /* PHY-LSI recv error */ |
| 1527 | EESR_CERF)){ /* recv frame CRC error */ |
Yoshihiro Shimoda | a18e08b | 2012-06-20 15:26:34 +0000 | [diff] [blame] | 1528 | sh_eth_rx(ndev, intr_status); |
Nobuhiro Iwamatsu | b0ca2a2 | 2008-06-30 11:08:17 +0900 | [diff] [blame] | 1529 | } |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1530 | |
Nobuhiro Iwamatsu | b0ca2a2 | 2008-06-30 11:08:17 +0900 | [diff] [blame] | 1531 | /* Tx Check */ |
Yoshihiro Shimoda | 380af9e | 2009-05-24 23:54:21 +0000 | [diff] [blame] | 1532 | if (intr_status & cd->tx_check) { |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1533 | sh_eth_txfree(ndev); |
| 1534 | netif_wake_queue(ndev); |
| 1535 | } |
| 1536 | |
Yoshihiro Shimoda | 380af9e | 2009-05-24 23:54:21 +0000 | [diff] [blame] | 1537 | if (intr_status & cd->eesr_err_check) |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1538 | sh_eth_error(ndev, intr_status); |
| 1539 | |
Nobuhiro Iwamatsu | 0e0fde3 | 2009-03-16 19:50:57 +0000 | [diff] [blame] | 1540 | other_irq: |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1541 | spin_unlock(&mdp->lock); |
| 1542 | |
Nobuhiro Iwamatsu | 0e0fde3 | 2009-03-16 19:50:57 +0000 | [diff] [blame] | 1543 | return ret; |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1544 | } |
| 1545 | |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1546 | /* PHY state control function */ |
| 1547 | static void sh_eth_adjust_link(struct net_device *ndev) |
| 1548 | { |
| 1549 | struct sh_eth_private *mdp = netdev_priv(ndev); |
| 1550 | struct phy_device *phydev = mdp->phydev; |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1551 | int new_state = 0; |
| 1552 | |
Sergei Shtylyov | 3340d2a | 2013-03-31 10:11:04 +0000 | [diff] [blame] | 1553 | if (phydev->link) { |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1554 | if (phydev->duplex != mdp->duplex) { |
| 1555 | new_state = 1; |
| 1556 | mdp->duplex = phydev->duplex; |
Yoshihiro Shimoda | 380af9e | 2009-05-24 23:54:21 +0000 | [diff] [blame] | 1557 | if (mdp->cd->set_duplex) |
| 1558 | mdp->cd->set_duplex(ndev); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1559 | } |
| 1560 | |
| 1561 | if (phydev->speed != mdp->speed) { |
| 1562 | new_state = 1; |
| 1563 | mdp->speed = phydev->speed; |
Yoshihiro Shimoda | 380af9e | 2009-05-24 23:54:21 +0000 | [diff] [blame] | 1564 | if (mdp->cd->set_rate) |
| 1565 | mdp->cd->set_rate(ndev); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1566 | } |
Sergei Shtylyov | 3340d2a | 2013-03-31 10:11:04 +0000 | [diff] [blame] | 1567 | if (!mdp->link) { |
Yoshihiro Shimoda | 91a5615 | 2011-07-05 20:33:51 +0000 | [diff] [blame] | 1568 | sh_eth_write(ndev, |
| 1569 | (sh_eth_read(ndev, ECMR) & ~ECMR_TXF), ECMR); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1570 | new_state = 1; |
| 1571 | mdp->link = phydev->link; |
Sergei Shtylyov | 1e1b812 | 2013-03-31 09:50:07 +0000 | [diff] [blame] | 1572 | if (mdp->cd->no_psr || mdp->no_ether_link) |
| 1573 | sh_eth_rcv_snd_enable(ndev); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1574 | } |
| 1575 | } else if (mdp->link) { |
| 1576 | new_state = 1; |
Sergei Shtylyov | 3340d2a | 2013-03-31 10:11:04 +0000 | [diff] [blame] | 1577 | mdp->link = 0; |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1578 | mdp->speed = 0; |
| 1579 | mdp->duplex = -1; |
Sergei Shtylyov | 1e1b812 | 2013-03-31 09:50:07 +0000 | [diff] [blame] | 1580 | if (mdp->cd->no_psr || mdp->no_ether_link) |
| 1581 | sh_eth_rcv_snd_disable(ndev); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1582 | } |
| 1583 | |
Nobuhiro Iwamatsu | dc19e4e | 2011-02-15 21:17:32 +0000 | [diff] [blame] | 1584 | if (new_state && netif_msg_link(mdp)) |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1585 | phy_print_status(phydev); |
| 1586 | } |
| 1587 | |
| 1588 | /* PHY init function */ |
| 1589 | static int sh_eth_phy_init(struct net_device *ndev) |
| 1590 | { |
| 1591 | struct sh_eth_private *mdp = netdev_priv(ndev); |
David S. Miller | 0a372eb | 2009-05-26 21:11:09 -0700 | [diff] [blame] | 1592 | char phy_id[MII_BUS_ID_SIZE + 3]; |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1593 | struct phy_device *phydev = NULL; |
| 1594 | |
Kay Sievers | fb28ad3 | 2008-11-10 13:55:14 -0800 | [diff] [blame] | 1595 | snprintf(phy_id, sizeof(phy_id), PHY_ID_FMT, |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1596 | mdp->mii_bus->id , mdp->phy_id); |
| 1597 | |
Sergei Shtylyov | 3340d2a | 2013-03-31 10:11:04 +0000 | [diff] [blame] | 1598 | mdp->link = 0; |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1599 | mdp->speed = 0; |
| 1600 | mdp->duplex = -1; |
| 1601 | |
| 1602 | /* Try connect to PHY */ |
Joe Perches | c061b18 | 2010-08-23 18:20:03 +0000 | [diff] [blame] | 1603 | phydev = phy_connect(ndev, phy_id, sh_eth_adjust_link, |
Florian Fainelli | f9a8f83 | 2013-01-14 00:52:52 +0000 | [diff] [blame] | 1604 | mdp->phy_interface); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1605 | if (IS_ERR(phydev)) { |
| 1606 | dev_err(&ndev->dev, "phy_connect failed\n"); |
| 1607 | return PTR_ERR(phydev); |
| 1608 | } |
Yoshihiro Shimoda | 380af9e | 2009-05-24 23:54:21 +0000 | [diff] [blame] | 1609 | |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1610 | dev_info(&ndev->dev, "attached phy %i to driver %s\n", |
Yoshihiro Shimoda | 380af9e | 2009-05-24 23:54:21 +0000 | [diff] [blame] | 1611 | phydev->addr, phydev->drv->name); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1612 | |
| 1613 | mdp->phydev = phydev; |
| 1614 | |
| 1615 | return 0; |
| 1616 | } |
| 1617 | |
| 1618 | /* PHY control start function */ |
| 1619 | static int sh_eth_phy_start(struct net_device *ndev) |
| 1620 | { |
| 1621 | struct sh_eth_private *mdp = netdev_priv(ndev); |
| 1622 | int ret; |
| 1623 | |
| 1624 | ret = sh_eth_phy_init(ndev); |
| 1625 | if (ret) |
| 1626 | return ret; |
| 1627 | |
| 1628 | /* reset phy - this also wakes it from PDOWN */ |
| 1629 | phy_write(mdp->phydev, MII_BMCR, BMCR_RESET); |
| 1630 | phy_start(mdp->phydev); |
| 1631 | |
| 1632 | return 0; |
| 1633 | } |
| 1634 | |
Nobuhiro Iwamatsu | dc19e4e | 2011-02-15 21:17:32 +0000 | [diff] [blame] | 1635 | static int sh_eth_get_settings(struct net_device *ndev, |
| 1636 | struct ethtool_cmd *ecmd) |
| 1637 | { |
| 1638 | struct sh_eth_private *mdp = netdev_priv(ndev); |
| 1639 | unsigned long flags; |
| 1640 | int ret; |
| 1641 | |
| 1642 | spin_lock_irqsave(&mdp->lock, flags); |
| 1643 | ret = phy_ethtool_gset(mdp->phydev, ecmd); |
| 1644 | spin_unlock_irqrestore(&mdp->lock, flags); |
| 1645 | |
| 1646 | return ret; |
| 1647 | } |
| 1648 | |
| 1649 | static int sh_eth_set_settings(struct net_device *ndev, |
| 1650 | struct ethtool_cmd *ecmd) |
| 1651 | { |
| 1652 | struct sh_eth_private *mdp = netdev_priv(ndev); |
| 1653 | unsigned long flags; |
| 1654 | int ret; |
Nobuhiro Iwamatsu | dc19e4e | 2011-02-15 21:17:32 +0000 | [diff] [blame] | 1655 | |
| 1656 | spin_lock_irqsave(&mdp->lock, flags); |
| 1657 | |
| 1658 | /* disable tx and rx */ |
Yoshihiro Shimoda | 4a55530 | 2011-03-07 21:59:26 +0000 | [diff] [blame] | 1659 | sh_eth_rcv_snd_disable(ndev); |
Nobuhiro Iwamatsu | dc19e4e | 2011-02-15 21:17:32 +0000 | [diff] [blame] | 1660 | |
| 1661 | ret = phy_ethtool_sset(mdp->phydev, ecmd); |
| 1662 | if (ret) |
| 1663 | goto error_exit; |
| 1664 | |
| 1665 | if (ecmd->duplex == DUPLEX_FULL) |
| 1666 | mdp->duplex = 1; |
| 1667 | else |
| 1668 | mdp->duplex = 0; |
| 1669 | |
| 1670 | if (mdp->cd->set_duplex) |
| 1671 | mdp->cd->set_duplex(ndev); |
| 1672 | |
| 1673 | error_exit: |
| 1674 | mdelay(1); |
| 1675 | |
| 1676 | /* enable tx and rx */ |
Yoshihiro Shimoda | 4a55530 | 2011-03-07 21:59:26 +0000 | [diff] [blame] | 1677 | sh_eth_rcv_snd_enable(ndev); |
Nobuhiro Iwamatsu | dc19e4e | 2011-02-15 21:17:32 +0000 | [diff] [blame] | 1678 | |
| 1679 | spin_unlock_irqrestore(&mdp->lock, flags); |
| 1680 | |
| 1681 | return ret; |
| 1682 | } |
| 1683 | |
| 1684 | static int sh_eth_nway_reset(struct net_device *ndev) |
| 1685 | { |
| 1686 | struct sh_eth_private *mdp = netdev_priv(ndev); |
| 1687 | unsigned long flags; |
| 1688 | int ret; |
| 1689 | |
| 1690 | spin_lock_irqsave(&mdp->lock, flags); |
| 1691 | ret = phy_start_aneg(mdp->phydev); |
| 1692 | spin_unlock_irqrestore(&mdp->lock, flags); |
| 1693 | |
| 1694 | return ret; |
| 1695 | } |
| 1696 | |
| 1697 | static u32 sh_eth_get_msglevel(struct net_device *ndev) |
| 1698 | { |
| 1699 | struct sh_eth_private *mdp = netdev_priv(ndev); |
| 1700 | return mdp->msg_enable; |
| 1701 | } |
| 1702 | |
| 1703 | static void sh_eth_set_msglevel(struct net_device *ndev, u32 value) |
| 1704 | { |
| 1705 | struct sh_eth_private *mdp = netdev_priv(ndev); |
| 1706 | mdp->msg_enable = value; |
| 1707 | } |
| 1708 | |
| 1709 | static const char sh_eth_gstrings_stats[][ETH_GSTRING_LEN] = { |
| 1710 | "rx_current", "tx_current", |
| 1711 | "rx_dirty", "tx_dirty", |
| 1712 | }; |
| 1713 | #define SH_ETH_STATS_LEN ARRAY_SIZE(sh_eth_gstrings_stats) |
| 1714 | |
| 1715 | static int sh_eth_get_sset_count(struct net_device *netdev, int sset) |
| 1716 | { |
| 1717 | switch (sset) { |
| 1718 | case ETH_SS_STATS: |
| 1719 | return SH_ETH_STATS_LEN; |
| 1720 | default: |
| 1721 | return -EOPNOTSUPP; |
| 1722 | } |
| 1723 | } |
| 1724 | |
| 1725 | static void sh_eth_get_ethtool_stats(struct net_device *ndev, |
| 1726 | struct ethtool_stats *stats, u64 *data) |
| 1727 | { |
| 1728 | struct sh_eth_private *mdp = netdev_priv(ndev); |
| 1729 | int i = 0; |
| 1730 | |
| 1731 | /* device-specific stats */ |
| 1732 | data[i++] = mdp->cur_rx; |
| 1733 | data[i++] = mdp->cur_tx; |
| 1734 | data[i++] = mdp->dirty_rx; |
| 1735 | data[i++] = mdp->dirty_tx; |
| 1736 | } |
| 1737 | |
| 1738 | static void sh_eth_get_strings(struct net_device *ndev, u32 stringset, u8 *data) |
| 1739 | { |
| 1740 | switch (stringset) { |
| 1741 | case ETH_SS_STATS: |
| 1742 | memcpy(data, *sh_eth_gstrings_stats, |
| 1743 | sizeof(sh_eth_gstrings_stats)); |
| 1744 | break; |
| 1745 | } |
| 1746 | } |
| 1747 | |
Yoshihiro Shimoda | 525b807 | 2012-06-26 20:00:03 +0000 | [diff] [blame] | 1748 | static void sh_eth_get_ringparam(struct net_device *ndev, |
| 1749 | struct ethtool_ringparam *ring) |
| 1750 | { |
| 1751 | struct sh_eth_private *mdp = netdev_priv(ndev); |
| 1752 | |
| 1753 | ring->rx_max_pending = RX_RING_MAX; |
| 1754 | ring->tx_max_pending = TX_RING_MAX; |
| 1755 | ring->rx_pending = mdp->num_rx_ring; |
| 1756 | ring->tx_pending = mdp->num_tx_ring; |
| 1757 | } |
| 1758 | |
| 1759 | static int sh_eth_set_ringparam(struct net_device *ndev, |
| 1760 | struct ethtool_ringparam *ring) |
| 1761 | { |
| 1762 | struct sh_eth_private *mdp = netdev_priv(ndev); |
| 1763 | int ret; |
| 1764 | |
| 1765 | if (ring->tx_pending > TX_RING_MAX || |
| 1766 | ring->rx_pending > RX_RING_MAX || |
| 1767 | ring->tx_pending < TX_RING_MIN || |
| 1768 | ring->rx_pending < RX_RING_MIN) |
| 1769 | return -EINVAL; |
| 1770 | if (ring->rx_mini_pending || ring->rx_jumbo_pending) |
| 1771 | return -EINVAL; |
| 1772 | |
| 1773 | if (netif_running(ndev)) { |
| 1774 | netif_tx_disable(ndev); |
| 1775 | /* Disable interrupts by clearing the interrupt mask. */ |
| 1776 | sh_eth_write(ndev, 0x0000, EESIPR); |
| 1777 | /* Stop the chip's Tx and Rx processes. */ |
| 1778 | sh_eth_write(ndev, 0, EDTRR); |
| 1779 | sh_eth_write(ndev, 0, EDRRR); |
| 1780 | synchronize_irq(ndev->irq); |
| 1781 | } |
| 1782 | |
| 1783 | /* Free all the skbuffs in the Rx queue. */ |
| 1784 | sh_eth_ring_free(ndev); |
| 1785 | /* Free DMA buffer */ |
| 1786 | sh_eth_free_dma_buffer(mdp); |
| 1787 | |
| 1788 | /* Set new parameters */ |
| 1789 | mdp->num_rx_ring = ring->rx_pending; |
| 1790 | mdp->num_tx_ring = ring->tx_pending; |
| 1791 | |
| 1792 | ret = sh_eth_ring_init(ndev); |
| 1793 | if (ret < 0) { |
| 1794 | dev_err(&ndev->dev, "%s: sh_eth_ring_init failed.\n", __func__); |
| 1795 | return ret; |
| 1796 | } |
| 1797 | ret = sh_eth_dev_init(ndev, false); |
| 1798 | if (ret < 0) { |
| 1799 | dev_err(&ndev->dev, "%s: sh_eth_dev_init failed.\n", __func__); |
| 1800 | return ret; |
| 1801 | } |
| 1802 | |
| 1803 | if (netif_running(ndev)) { |
| 1804 | sh_eth_write(ndev, mdp->cd->eesipr_value, EESIPR); |
| 1805 | /* Setting the Rx mode will start the Rx process. */ |
| 1806 | sh_eth_write(ndev, EDRRR_R, EDRRR); |
| 1807 | netif_wake_queue(ndev); |
| 1808 | } |
| 1809 | |
| 1810 | return 0; |
| 1811 | } |
| 1812 | |
stephen hemminger | 9b07be4 | 2012-01-04 12:59:49 +0000 | [diff] [blame] | 1813 | static const struct ethtool_ops sh_eth_ethtool_ops = { |
Nobuhiro Iwamatsu | dc19e4e | 2011-02-15 21:17:32 +0000 | [diff] [blame] | 1814 | .get_settings = sh_eth_get_settings, |
| 1815 | .set_settings = sh_eth_set_settings, |
stephen hemminger | 9b07be4 | 2012-01-04 12:59:49 +0000 | [diff] [blame] | 1816 | .nway_reset = sh_eth_nway_reset, |
Nobuhiro Iwamatsu | dc19e4e | 2011-02-15 21:17:32 +0000 | [diff] [blame] | 1817 | .get_msglevel = sh_eth_get_msglevel, |
| 1818 | .set_msglevel = sh_eth_set_msglevel, |
stephen hemminger | 9b07be4 | 2012-01-04 12:59:49 +0000 | [diff] [blame] | 1819 | .get_link = ethtool_op_get_link, |
Nobuhiro Iwamatsu | dc19e4e | 2011-02-15 21:17:32 +0000 | [diff] [blame] | 1820 | .get_strings = sh_eth_get_strings, |
| 1821 | .get_ethtool_stats = sh_eth_get_ethtool_stats, |
| 1822 | .get_sset_count = sh_eth_get_sset_count, |
Yoshihiro Shimoda | 525b807 | 2012-06-26 20:00:03 +0000 | [diff] [blame] | 1823 | .get_ringparam = sh_eth_get_ringparam, |
| 1824 | .set_ringparam = sh_eth_set_ringparam, |
Nobuhiro Iwamatsu | dc19e4e | 2011-02-15 21:17:32 +0000 | [diff] [blame] | 1825 | }; |
| 1826 | |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1827 | /* network device open function */ |
| 1828 | static int sh_eth_open(struct net_device *ndev) |
| 1829 | { |
| 1830 | int ret = 0; |
| 1831 | struct sh_eth_private *mdp = netdev_priv(ndev); |
| 1832 | |
Magnus Damm | bcd5149 | 2009-10-09 00:20:04 +0000 | [diff] [blame] | 1833 | pm_runtime_get_sync(&mdp->pdev->dev); |
| 1834 | |
Joe Perches | a0607fd | 2009-11-18 23:29:17 -0800 | [diff] [blame] | 1835 | ret = request_irq(ndev->irq, sh_eth_interrupt, |
Nobuhiro Iwamatsu | 5b3dfd1 | 2013-06-06 09:49:30 +0000 | [diff] [blame] | 1836 | mdp->cd->irq_flags, ndev->name, ndev); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1837 | if (ret) { |
Yoshihiro Shimoda | 380af9e | 2009-05-24 23:54:21 +0000 | [diff] [blame] | 1838 | dev_err(&ndev->dev, "Can not assign IRQ number\n"); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1839 | return ret; |
| 1840 | } |
| 1841 | |
| 1842 | /* Descriptor set */ |
| 1843 | ret = sh_eth_ring_init(ndev); |
| 1844 | if (ret) |
| 1845 | goto out_free_irq; |
| 1846 | |
| 1847 | /* device init */ |
Yoshihiro Shimoda | 525b807 | 2012-06-26 20:00:03 +0000 | [diff] [blame] | 1848 | ret = sh_eth_dev_init(ndev, true); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1849 | if (ret) |
| 1850 | goto out_free_irq; |
| 1851 | |
| 1852 | /* PHY control start*/ |
| 1853 | ret = sh_eth_phy_start(ndev); |
| 1854 | if (ret) |
| 1855 | goto out_free_irq; |
| 1856 | |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1857 | return ret; |
| 1858 | |
| 1859 | out_free_irq: |
| 1860 | free_irq(ndev->irq, ndev); |
Magnus Damm | bcd5149 | 2009-10-09 00:20:04 +0000 | [diff] [blame] | 1861 | pm_runtime_put_sync(&mdp->pdev->dev); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1862 | return ret; |
| 1863 | } |
| 1864 | |
| 1865 | /* Timeout function */ |
| 1866 | static void sh_eth_tx_timeout(struct net_device *ndev) |
| 1867 | { |
| 1868 | struct sh_eth_private *mdp = netdev_priv(ndev); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1869 | struct sh_eth_rxdesc *rxdesc; |
| 1870 | int i; |
| 1871 | |
| 1872 | netif_stop_queue(ndev); |
| 1873 | |
Nobuhiro Iwamatsu | dc19e4e | 2011-02-15 21:17:32 +0000 | [diff] [blame] | 1874 | if (netif_msg_timer(mdp)) |
| 1875 | dev_err(&ndev->dev, "%s: transmit timed out, status %8.8x," |
Yoshihiro Shimoda | 4a55530 | 2011-03-07 21:59:26 +0000 | [diff] [blame] | 1876 | " resetting...\n", ndev->name, (int)sh_eth_read(ndev, EESR)); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1877 | |
| 1878 | /* tx_errors count up */ |
Eric Dumazet | bb7d92e | 2012-02-06 22:17:21 +0000 | [diff] [blame] | 1879 | ndev->stats.tx_errors++; |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1880 | |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1881 | /* Free all the skbuffs in the Rx queue. */ |
Yoshihiro Shimoda | 525b807 | 2012-06-26 20:00:03 +0000 | [diff] [blame] | 1882 | for (i = 0; i < mdp->num_rx_ring; i++) { |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1883 | rxdesc = &mdp->rx_ring[i]; |
| 1884 | rxdesc->status = 0; |
| 1885 | rxdesc->addr = 0xBADF00D0; |
| 1886 | if (mdp->rx_skbuff[i]) |
| 1887 | dev_kfree_skb(mdp->rx_skbuff[i]); |
| 1888 | mdp->rx_skbuff[i] = NULL; |
| 1889 | } |
Yoshihiro Shimoda | 525b807 | 2012-06-26 20:00:03 +0000 | [diff] [blame] | 1890 | for (i = 0; i < mdp->num_tx_ring; i++) { |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1891 | if (mdp->tx_skbuff[i]) |
| 1892 | dev_kfree_skb(mdp->tx_skbuff[i]); |
| 1893 | mdp->tx_skbuff[i] = NULL; |
| 1894 | } |
| 1895 | |
| 1896 | /* device init */ |
Yoshihiro Shimoda | 525b807 | 2012-06-26 20:00:03 +0000 | [diff] [blame] | 1897 | sh_eth_dev_init(ndev, true); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1898 | } |
| 1899 | |
| 1900 | /* Packet transmit function */ |
| 1901 | static int sh_eth_start_xmit(struct sk_buff *skb, struct net_device *ndev) |
| 1902 | { |
| 1903 | struct sh_eth_private *mdp = netdev_priv(ndev); |
| 1904 | struct sh_eth_txdesc *txdesc; |
| 1905 | u32 entry; |
Nobuhiro Iwamatsu | fb5e2f9 | 2008-11-17 20:29:58 +0000 | [diff] [blame] | 1906 | unsigned long flags; |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1907 | |
| 1908 | spin_lock_irqsave(&mdp->lock, flags); |
Yoshihiro Shimoda | 525b807 | 2012-06-26 20:00:03 +0000 | [diff] [blame] | 1909 | if ((mdp->cur_tx - mdp->dirty_tx) >= (mdp->num_tx_ring - 4)) { |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1910 | if (!sh_eth_txfree(ndev)) { |
Nobuhiro Iwamatsu | dc19e4e | 2011-02-15 21:17:32 +0000 | [diff] [blame] | 1911 | if (netif_msg_tx_queued(mdp)) |
| 1912 | dev_warn(&ndev->dev, "TxFD exhausted.\n"); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1913 | netif_stop_queue(ndev); |
| 1914 | spin_unlock_irqrestore(&mdp->lock, flags); |
Patrick McHardy | 5b54814 | 2009-06-12 06:22:29 +0000 | [diff] [blame] | 1915 | return NETDEV_TX_BUSY; |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1916 | } |
| 1917 | } |
| 1918 | spin_unlock_irqrestore(&mdp->lock, flags); |
| 1919 | |
Yoshihiro Shimoda | 525b807 | 2012-06-26 20:00:03 +0000 | [diff] [blame] | 1920 | entry = mdp->cur_tx % mdp->num_tx_ring; |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1921 | mdp->tx_skbuff[entry] = skb; |
| 1922 | txdesc = &mdp->tx_ring[entry]; |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1923 | /* soft swap. */ |
Yoshihiro Shimoda | 380af9e | 2009-05-24 23:54:21 +0000 | [diff] [blame] | 1924 | if (!mdp->cd->hw_swap) |
| 1925 | sh_eth_soft_swap(phys_to_virt(ALIGN(txdesc->addr, 4)), |
| 1926 | skb->len + 2); |
Yoshihiro Shimoda | 31fcb99 | 2011-06-30 22:52:13 +0000 | [diff] [blame] | 1927 | txdesc->addr = dma_map_single(&ndev->dev, skb->data, skb->len, |
| 1928 | DMA_TO_DEVICE); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1929 | if (skb->len < ETHERSMALL) |
| 1930 | txdesc->buffer_length = ETHERSMALL; |
| 1931 | else |
| 1932 | txdesc->buffer_length = skb->len; |
| 1933 | |
Yoshihiro Shimoda | 525b807 | 2012-06-26 20:00:03 +0000 | [diff] [blame] | 1934 | if (entry >= mdp->num_tx_ring - 1) |
Yoshinori Sato | 71557a3 | 2008-08-06 19:49:00 -0400 | [diff] [blame] | 1935 | txdesc->status |= cpu_to_edmac(mdp, TD_TACT | TD_TDLE); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1936 | else |
Yoshinori Sato | 71557a3 | 2008-08-06 19:49:00 -0400 | [diff] [blame] | 1937 | txdesc->status |= cpu_to_edmac(mdp, TD_TACT); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1938 | |
| 1939 | mdp->cur_tx++; |
| 1940 | |
Yoshihiro Shimoda | c5ed536 | 2011-03-07 21:59:38 +0000 | [diff] [blame] | 1941 | if (!(sh_eth_read(ndev, EDTRR) & sh_eth_get_edtrr_trns(mdp))) |
| 1942 | sh_eth_write(ndev, sh_eth_get_edtrr_trns(mdp), EDTRR); |
Nobuhiro Iwamatsu | b0ca2a2 | 2008-06-30 11:08:17 +0900 | [diff] [blame] | 1943 | |
Patrick McHardy | 6ed1065 | 2009-06-23 06:03:08 +0000 | [diff] [blame] | 1944 | return NETDEV_TX_OK; |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1945 | } |
| 1946 | |
| 1947 | /* device close function */ |
| 1948 | static int sh_eth_close(struct net_device *ndev) |
| 1949 | { |
| 1950 | struct sh_eth_private *mdp = netdev_priv(ndev); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1951 | |
| 1952 | netif_stop_queue(ndev); |
| 1953 | |
| 1954 | /* Disable interrupts by clearing the interrupt mask. */ |
Yoshihiro Shimoda | 4a55530 | 2011-03-07 21:59:26 +0000 | [diff] [blame] | 1955 | sh_eth_write(ndev, 0x0000, EESIPR); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1956 | |
| 1957 | /* Stop the chip's Tx and Rx processes. */ |
Yoshihiro Shimoda | 4a55530 | 2011-03-07 21:59:26 +0000 | [diff] [blame] | 1958 | sh_eth_write(ndev, 0, EDTRR); |
| 1959 | sh_eth_write(ndev, 0, EDRRR); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1960 | |
| 1961 | /* PHY Disconnect */ |
| 1962 | if (mdp->phydev) { |
| 1963 | phy_stop(mdp->phydev); |
| 1964 | phy_disconnect(mdp->phydev); |
| 1965 | } |
| 1966 | |
| 1967 | free_irq(ndev->irq, ndev); |
| 1968 | |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1969 | /* Free all the skbuffs in the Rx queue. */ |
| 1970 | sh_eth_ring_free(ndev); |
| 1971 | |
| 1972 | /* free DMA buffer */ |
Yoshihiro Shimoda | 91c7755 | 2012-06-26 20:00:01 +0000 | [diff] [blame] | 1973 | sh_eth_free_dma_buffer(mdp); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1974 | |
Magnus Damm | bcd5149 | 2009-10-09 00:20:04 +0000 | [diff] [blame] | 1975 | pm_runtime_put_sync(&mdp->pdev->dev); |
| 1976 | |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1977 | return 0; |
| 1978 | } |
| 1979 | |
| 1980 | static struct net_device_stats *sh_eth_get_stats(struct net_device *ndev) |
| 1981 | { |
| 1982 | struct sh_eth_private *mdp = netdev_priv(ndev); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1983 | |
Magnus Damm | bcd5149 | 2009-10-09 00:20:04 +0000 | [diff] [blame] | 1984 | pm_runtime_get_sync(&mdp->pdev->dev); |
| 1985 | |
Eric Dumazet | bb7d92e | 2012-02-06 22:17:21 +0000 | [diff] [blame] | 1986 | ndev->stats.tx_dropped += sh_eth_read(ndev, TROCR); |
Yoshihiro Shimoda | 4a55530 | 2011-03-07 21:59:26 +0000 | [diff] [blame] | 1987 | sh_eth_write(ndev, 0, TROCR); /* (write clear) */ |
Eric Dumazet | bb7d92e | 2012-02-06 22:17:21 +0000 | [diff] [blame] | 1988 | ndev->stats.collisions += sh_eth_read(ndev, CDCR); |
Yoshihiro Shimoda | 4a55530 | 2011-03-07 21:59:26 +0000 | [diff] [blame] | 1989 | sh_eth_write(ndev, 0, CDCR); /* (write clear) */ |
Eric Dumazet | bb7d92e | 2012-02-06 22:17:21 +0000 | [diff] [blame] | 1990 | ndev->stats.tx_carrier_errors += sh_eth_read(ndev, LCCR); |
Yoshihiro Shimoda | 4a55530 | 2011-03-07 21:59:26 +0000 | [diff] [blame] | 1991 | sh_eth_write(ndev, 0, LCCR); /* (write clear) */ |
Yoshihiro Shimoda | c5ed536 | 2011-03-07 21:59:38 +0000 | [diff] [blame] | 1992 | if (sh_eth_is_gether(mdp)) { |
Eric Dumazet | bb7d92e | 2012-02-06 22:17:21 +0000 | [diff] [blame] | 1993 | ndev->stats.tx_carrier_errors += sh_eth_read(ndev, CERCR); |
Yoshihiro Shimoda | c5ed536 | 2011-03-07 21:59:38 +0000 | [diff] [blame] | 1994 | sh_eth_write(ndev, 0, CERCR); /* (write clear) */ |
Eric Dumazet | bb7d92e | 2012-02-06 22:17:21 +0000 | [diff] [blame] | 1995 | ndev->stats.tx_carrier_errors += sh_eth_read(ndev, CEECR); |
Yoshihiro Shimoda | c5ed536 | 2011-03-07 21:59:38 +0000 | [diff] [blame] | 1996 | sh_eth_write(ndev, 0, CEECR); /* (write clear) */ |
| 1997 | } else { |
Eric Dumazet | bb7d92e | 2012-02-06 22:17:21 +0000 | [diff] [blame] | 1998 | ndev->stats.tx_carrier_errors += sh_eth_read(ndev, CNDCR); |
Yoshihiro Shimoda | c5ed536 | 2011-03-07 21:59:38 +0000 | [diff] [blame] | 1999 | sh_eth_write(ndev, 0, CNDCR); /* (write clear) */ |
| 2000 | } |
Magnus Damm | bcd5149 | 2009-10-09 00:20:04 +0000 | [diff] [blame] | 2001 | pm_runtime_put_sync(&mdp->pdev->dev); |
| 2002 | |
Eric Dumazet | bb7d92e | 2012-02-06 22:17:21 +0000 | [diff] [blame] | 2003 | return &ndev->stats; |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 2004 | } |
| 2005 | |
Eric Dumazet | bb7d92e | 2012-02-06 22:17:21 +0000 | [diff] [blame] | 2006 | /* ioctl to device function */ |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 2007 | static int sh_eth_do_ioctl(struct net_device *ndev, struct ifreq *rq, |
| 2008 | int cmd) |
| 2009 | { |
| 2010 | struct sh_eth_private *mdp = netdev_priv(ndev); |
| 2011 | struct phy_device *phydev = mdp->phydev; |
| 2012 | |
| 2013 | if (!netif_running(ndev)) |
| 2014 | return -EINVAL; |
| 2015 | |
| 2016 | if (!phydev) |
| 2017 | return -ENODEV; |
| 2018 | |
Richard Cochran | 28b0411 | 2010-07-17 08:48:55 +0000 | [diff] [blame] | 2019 | return phy_mii_ioctl(phydev, rq, cmd); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 2020 | } |
| 2021 | |
Yoshihiro Shimoda | 6743fe6 | 2012-02-15 17:55:03 +0000 | [diff] [blame] | 2022 | /* For TSU_POSTn. Please refer to the manual about this (strange) bitfields */ |
| 2023 | static void *sh_eth_tsu_get_post_reg_offset(struct sh_eth_private *mdp, |
| 2024 | int entry) |
| 2025 | { |
| 2026 | return sh_eth_tsu_get_offset(mdp, TSU_POST1) + (entry / 8 * 4); |
| 2027 | } |
| 2028 | |
| 2029 | static u32 sh_eth_tsu_get_post_mask(int entry) |
| 2030 | { |
| 2031 | return 0x0f << (28 - ((entry % 8) * 4)); |
| 2032 | } |
| 2033 | |
| 2034 | static u32 sh_eth_tsu_get_post_bit(struct sh_eth_private *mdp, int entry) |
| 2035 | { |
| 2036 | return (0x08 >> (mdp->port << 1)) << (28 - ((entry % 8) * 4)); |
| 2037 | } |
| 2038 | |
| 2039 | static void sh_eth_tsu_enable_cam_entry_post(struct net_device *ndev, |
| 2040 | int entry) |
| 2041 | { |
| 2042 | struct sh_eth_private *mdp = netdev_priv(ndev); |
| 2043 | u32 tmp; |
| 2044 | void *reg_offset; |
| 2045 | |
| 2046 | reg_offset = sh_eth_tsu_get_post_reg_offset(mdp, entry); |
| 2047 | tmp = ioread32(reg_offset); |
| 2048 | iowrite32(tmp | sh_eth_tsu_get_post_bit(mdp, entry), reg_offset); |
| 2049 | } |
| 2050 | |
| 2051 | static bool sh_eth_tsu_disable_cam_entry_post(struct net_device *ndev, |
| 2052 | int entry) |
| 2053 | { |
| 2054 | struct sh_eth_private *mdp = netdev_priv(ndev); |
| 2055 | u32 post_mask, ref_mask, tmp; |
| 2056 | void *reg_offset; |
| 2057 | |
| 2058 | reg_offset = sh_eth_tsu_get_post_reg_offset(mdp, entry); |
| 2059 | post_mask = sh_eth_tsu_get_post_mask(entry); |
| 2060 | ref_mask = sh_eth_tsu_get_post_bit(mdp, entry) & ~post_mask; |
| 2061 | |
| 2062 | tmp = ioread32(reg_offset); |
| 2063 | iowrite32(tmp & ~post_mask, reg_offset); |
| 2064 | |
| 2065 | /* If other port enables, the function returns "true" */ |
| 2066 | return tmp & ref_mask; |
| 2067 | } |
| 2068 | |
| 2069 | static int sh_eth_tsu_busy(struct net_device *ndev) |
| 2070 | { |
| 2071 | int timeout = SH_ETH_TSU_TIMEOUT_MS * 100; |
| 2072 | struct sh_eth_private *mdp = netdev_priv(ndev); |
| 2073 | |
| 2074 | while ((sh_eth_tsu_read(mdp, TSU_ADSBSY) & TSU_ADSBSY_0)) { |
| 2075 | udelay(10); |
| 2076 | timeout--; |
| 2077 | if (timeout <= 0) { |
| 2078 | dev_err(&ndev->dev, "%s: timeout\n", __func__); |
| 2079 | return -ETIMEDOUT; |
| 2080 | } |
| 2081 | } |
| 2082 | |
| 2083 | return 0; |
| 2084 | } |
| 2085 | |
| 2086 | static int sh_eth_tsu_write_entry(struct net_device *ndev, void *reg, |
| 2087 | const u8 *addr) |
| 2088 | { |
| 2089 | u32 val; |
| 2090 | |
| 2091 | val = addr[0] << 24 | addr[1] << 16 | addr[2] << 8 | addr[3]; |
| 2092 | iowrite32(val, reg); |
| 2093 | if (sh_eth_tsu_busy(ndev) < 0) |
| 2094 | return -EBUSY; |
| 2095 | |
| 2096 | val = addr[4] << 8 | addr[5]; |
| 2097 | iowrite32(val, reg + 4); |
| 2098 | if (sh_eth_tsu_busy(ndev) < 0) |
| 2099 | return -EBUSY; |
| 2100 | |
| 2101 | return 0; |
| 2102 | } |
| 2103 | |
| 2104 | static void sh_eth_tsu_read_entry(void *reg, u8 *addr) |
| 2105 | { |
| 2106 | u32 val; |
| 2107 | |
| 2108 | val = ioread32(reg); |
| 2109 | addr[0] = (val >> 24) & 0xff; |
| 2110 | addr[1] = (val >> 16) & 0xff; |
| 2111 | addr[2] = (val >> 8) & 0xff; |
| 2112 | addr[3] = val & 0xff; |
| 2113 | val = ioread32(reg + 4); |
| 2114 | addr[4] = (val >> 8) & 0xff; |
| 2115 | addr[5] = val & 0xff; |
| 2116 | } |
| 2117 | |
| 2118 | |
| 2119 | static int sh_eth_tsu_find_entry(struct net_device *ndev, const u8 *addr) |
| 2120 | { |
| 2121 | struct sh_eth_private *mdp = netdev_priv(ndev); |
| 2122 | void *reg_offset = sh_eth_tsu_get_offset(mdp, TSU_ADRH0); |
| 2123 | int i; |
| 2124 | u8 c_addr[ETH_ALEN]; |
| 2125 | |
| 2126 | for (i = 0; i < SH_ETH_TSU_CAM_ENTRIES; i++, reg_offset += 8) { |
| 2127 | sh_eth_tsu_read_entry(reg_offset, c_addr); |
| 2128 | if (memcmp(addr, c_addr, ETH_ALEN) == 0) |
| 2129 | return i; |
| 2130 | } |
| 2131 | |
| 2132 | return -ENOENT; |
| 2133 | } |
| 2134 | |
| 2135 | static int sh_eth_tsu_find_empty(struct net_device *ndev) |
| 2136 | { |
| 2137 | u8 blank[ETH_ALEN]; |
| 2138 | int entry; |
| 2139 | |
| 2140 | memset(blank, 0, sizeof(blank)); |
| 2141 | entry = sh_eth_tsu_find_entry(ndev, blank); |
| 2142 | return (entry < 0) ? -ENOMEM : entry; |
| 2143 | } |
| 2144 | |
| 2145 | static int sh_eth_tsu_disable_cam_entry_table(struct net_device *ndev, |
| 2146 | int entry) |
| 2147 | { |
| 2148 | struct sh_eth_private *mdp = netdev_priv(ndev); |
| 2149 | void *reg_offset = sh_eth_tsu_get_offset(mdp, TSU_ADRH0); |
| 2150 | int ret; |
| 2151 | u8 blank[ETH_ALEN]; |
| 2152 | |
| 2153 | sh_eth_tsu_write(mdp, sh_eth_tsu_read(mdp, TSU_TEN) & |
| 2154 | ~(1 << (31 - entry)), TSU_TEN); |
| 2155 | |
| 2156 | memset(blank, 0, sizeof(blank)); |
| 2157 | ret = sh_eth_tsu_write_entry(ndev, reg_offset + entry * 8, blank); |
| 2158 | if (ret < 0) |
| 2159 | return ret; |
| 2160 | return 0; |
| 2161 | } |
| 2162 | |
| 2163 | static int sh_eth_tsu_add_entry(struct net_device *ndev, const u8 *addr) |
| 2164 | { |
| 2165 | struct sh_eth_private *mdp = netdev_priv(ndev); |
| 2166 | void *reg_offset = sh_eth_tsu_get_offset(mdp, TSU_ADRH0); |
| 2167 | int i, ret; |
| 2168 | |
| 2169 | if (!mdp->cd->tsu) |
| 2170 | return 0; |
| 2171 | |
| 2172 | i = sh_eth_tsu_find_entry(ndev, addr); |
| 2173 | if (i < 0) { |
| 2174 | /* No entry found, create one */ |
| 2175 | i = sh_eth_tsu_find_empty(ndev); |
| 2176 | if (i < 0) |
| 2177 | return -ENOMEM; |
| 2178 | ret = sh_eth_tsu_write_entry(ndev, reg_offset + i * 8, addr); |
| 2179 | if (ret < 0) |
| 2180 | return ret; |
| 2181 | |
| 2182 | /* Enable the entry */ |
| 2183 | sh_eth_tsu_write(mdp, sh_eth_tsu_read(mdp, TSU_TEN) | |
| 2184 | (1 << (31 - i)), TSU_TEN); |
| 2185 | } |
| 2186 | |
| 2187 | /* Entry found or created, enable POST */ |
| 2188 | sh_eth_tsu_enable_cam_entry_post(ndev, i); |
| 2189 | |
| 2190 | return 0; |
| 2191 | } |
| 2192 | |
| 2193 | static int sh_eth_tsu_del_entry(struct net_device *ndev, const u8 *addr) |
| 2194 | { |
| 2195 | struct sh_eth_private *mdp = netdev_priv(ndev); |
| 2196 | int i, ret; |
| 2197 | |
| 2198 | if (!mdp->cd->tsu) |
| 2199 | return 0; |
| 2200 | |
| 2201 | i = sh_eth_tsu_find_entry(ndev, addr); |
| 2202 | if (i) { |
| 2203 | /* Entry found */ |
| 2204 | if (sh_eth_tsu_disable_cam_entry_post(ndev, i)) |
| 2205 | goto done; |
| 2206 | |
| 2207 | /* Disable the entry if both ports was disabled */ |
| 2208 | ret = sh_eth_tsu_disable_cam_entry_table(ndev, i); |
| 2209 | if (ret < 0) |
| 2210 | return ret; |
| 2211 | } |
| 2212 | done: |
| 2213 | return 0; |
| 2214 | } |
| 2215 | |
| 2216 | static int sh_eth_tsu_purge_all(struct net_device *ndev) |
| 2217 | { |
| 2218 | struct sh_eth_private *mdp = netdev_priv(ndev); |
| 2219 | int i, ret; |
| 2220 | |
| 2221 | if (unlikely(!mdp->cd->tsu)) |
| 2222 | return 0; |
| 2223 | |
| 2224 | for (i = 0; i < SH_ETH_TSU_CAM_ENTRIES; i++) { |
| 2225 | if (sh_eth_tsu_disable_cam_entry_post(ndev, i)) |
| 2226 | continue; |
| 2227 | |
| 2228 | /* Disable the entry if both ports was disabled */ |
| 2229 | ret = sh_eth_tsu_disable_cam_entry_table(ndev, i); |
| 2230 | if (ret < 0) |
| 2231 | return ret; |
| 2232 | } |
| 2233 | |
| 2234 | return 0; |
| 2235 | } |
| 2236 | |
| 2237 | static void sh_eth_tsu_purge_mcast(struct net_device *ndev) |
| 2238 | { |
| 2239 | struct sh_eth_private *mdp = netdev_priv(ndev); |
| 2240 | u8 addr[ETH_ALEN]; |
| 2241 | void *reg_offset = sh_eth_tsu_get_offset(mdp, TSU_ADRH0); |
| 2242 | int i; |
| 2243 | |
| 2244 | if (unlikely(!mdp->cd->tsu)) |
| 2245 | return; |
| 2246 | |
| 2247 | for (i = 0; i < SH_ETH_TSU_CAM_ENTRIES; i++, reg_offset += 8) { |
| 2248 | sh_eth_tsu_read_entry(reg_offset, addr); |
| 2249 | if (is_multicast_ether_addr(addr)) |
| 2250 | sh_eth_tsu_del_entry(ndev, addr); |
| 2251 | } |
| 2252 | } |
| 2253 | |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 2254 | /* Multicast reception directions set */ |
| 2255 | static void sh_eth_set_multicast_list(struct net_device *ndev) |
| 2256 | { |
Yoshihiro Shimoda | 6743fe6 | 2012-02-15 17:55:03 +0000 | [diff] [blame] | 2257 | struct sh_eth_private *mdp = netdev_priv(ndev); |
| 2258 | u32 ecmr_bits; |
| 2259 | int mcast_all = 0; |
| 2260 | unsigned long flags; |
| 2261 | |
| 2262 | spin_lock_irqsave(&mdp->lock, flags); |
| 2263 | /* |
| 2264 | * Initial condition is MCT = 1, PRM = 0. |
| 2265 | * Depending on ndev->flags, set PRM or clear MCT |
| 2266 | */ |
| 2267 | ecmr_bits = (sh_eth_read(ndev, ECMR) & ~ECMR_PRM) | ECMR_MCT; |
| 2268 | |
| 2269 | if (!(ndev->flags & IFF_MULTICAST)) { |
| 2270 | sh_eth_tsu_purge_mcast(ndev); |
| 2271 | mcast_all = 1; |
| 2272 | } |
| 2273 | if (ndev->flags & IFF_ALLMULTI) { |
| 2274 | sh_eth_tsu_purge_mcast(ndev); |
| 2275 | ecmr_bits &= ~ECMR_MCT; |
| 2276 | mcast_all = 1; |
| 2277 | } |
| 2278 | |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 2279 | if (ndev->flags & IFF_PROMISC) { |
Yoshihiro Shimoda | 6743fe6 | 2012-02-15 17:55:03 +0000 | [diff] [blame] | 2280 | sh_eth_tsu_purge_all(ndev); |
| 2281 | ecmr_bits = (ecmr_bits & ~ECMR_MCT) | ECMR_PRM; |
| 2282 | } else if (mdp->cd->tsu) { |
| 2283 | struct netdev_hw_addr *ha; |
| 2284 | netdev_for_each_mc_addr(ha, ndev) { |
| 2285 | if (mcast_all && is_multicast_ether_addr(ha->addr)) |
| 2286 | continue; |
| 2287 | |
| 2288 | if (sh_eth_tsu_add_entry(ndev, ha->addr) < 0) { |
| 2289 | if (!mcast_all) { |
| 2290 | sh_eth_tsu_purge_mcast(ndev); |
| 2291 | ecmr_bits &= ~ECMR_MCT; |
| 2292 | mcast_all = 1; |
| 2293 | } |
| 2294 | } |
| 2295 | } |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 2296 | } else { |
| 2297 | /* Normal, unicast/broadcast-only mode. */ |
Yoshihiro Shimoda | 6743fe6 | 2012-02-15 17:55:03 +0000 | [diff] [blame] | 2298 | ecmr_bits = (ecmr_bits & ~ECMR_PRM) | ECMR_MCT; |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 2299 | } |
Yoshihiro Shimoda | 6743fe6 | 2012-02-15 17:55:03 +0000 | [diff] [blame] | 2300 | |
| 2301 | /* update the ethernet mode */ |
| 2302 | sh_eth_write(ndev, ecmr_bits, ECMR); |
| 2303 | |
| 2304 | spin_unlock_irqrestore(&mdp->lock, flags); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 2305 | } |
Yoshihiro Shimoda | 71cc7c3 | 2012-02-15 17:55:06 +0000 | [diff] [blame] | 2306 | |
| 2307 | static int sh_eth_get_vtag_index(struct sh_eth_private *mdp) |
| 2308 | { |
| 2309 | if (!mdp->port) |
| 2310 | return TSU_VTAG0; |
| 2311 | else |
| 2312 | return TSU_VTAG1; |
| 2313 | } |
| 2314 | |
Patrick McHardy | 80d5c36 | 2013-04-19 02:04:28 +0000 | [diff] [blame] | 2315 | static int sh_eth_vlan_rx_add_vid(struct net_device *ndev, |
| 2316 | __be16 proto, u16 vid) |
Yoshihiro Shimoda | 71cc7c3 | 2012-02-15 17:55:06 +0000 | [diff] [blame] | 2317 | { |
| 2318 | struct sh_eth_private *mdp = netdev_priv(ndev); |
| 2319 | int vtag_reg_index = sh_eth_get_vtag_index(mdp); |
| 2320 | |
| 2321 | if (unlikely(!mdp->cd->tsu)) |
| 2322 | return -EPERM; |
| 2323 | |
| 2324 | /* No filtering if vid = 0 */ |
| 2325 | if (!vid) |
| 2326 | return 0; |
| 2327 | |
| 2328 | mdp->vlan_num_ids++; |
| 2329 | |
| 2330 | /* |
| 2331 | * The controller has one VLAN tag HW filter. So, if the filter is |
| 2332 | * already enabled, the driver disables it and the filte |
| 2333 | */ |
| 2334 | if (mdp->vlan_num_ids > 1) { |
| 2335 | /* disable VLAN filter */ |
| 2336 | sh_eth_tsu_write(mdp, 0, vtag_reg_index); |
| 2337 | return 0; |
| 2338 | } |
| 2339 | |
| 2340 | sh_eth_tsu_write(mdp, TSU_VTAG_ENABLE | (vid & TSU_VTAG_VID_MASK), |
| 2341 | vtag_reg_index); |
| 2342 | |
| 2343 | return 0; |
| 2344 | } |
| 2345 | |
Patrick McHardy | 80d5c36 | 2013-04-19 02:04:28 +0000 | [diff] [blame] | 2346 | static int sh_eth_vlan_rx_kill_vid(struct net_device *ndev, |
| 2347 | __be16 proto, u16 vid) |
Yoshihiro Shimoda | 71cc7c3 | 2012-02-15 17:55:06 +0000 | [diff] [blame] | 2348 | { |
| 2349 | struct sh_eth_private *mdp = netdev_priv(ndev); |
| 2350 | int vtag_reg_index = sh_eth_get_vtag_index(mdp); |
| 2351 | |
| 2352 | if (unlikely(!mdp->cd->tsu)) |
| 2353 | return -EPERM; |
| 2354 | |
| 2355 | /* No filtering if vid = 0 */ |
| 2356 | if (!vid) |
| 2357 | return 0; |
| 2358 | |
| 2359 | mdp->vlan_num_ids--; |
| 2360 | sh_eth_tsu_write(mdp, 0, vtag_reg_index); |
| 2361 | |
| 2362 | return 0; |
| 2363 | } |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 2364 | |
| 2365 | /* SuperH's TSU register init function */ |
Yoshihiro Shimoda | 4a55530 | 2011-03-07 21:59:26 +0000 | [diff] [blame] | 2366 | static void sh_eth_tsu_init(struct sh_eth_private *mdp) |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 2367 | { |
Yoshihiro Shimoda | 4a55530 | 2011-03-07 21:59:26 +0000 | [diff] [blame] | 2368 | sh_eth_tsu_write(mdp, 0, TSU_FWEN0); /* Disable forward(0->1) */ |
| 2369 | sh_eth_tsu_write(mdp, 0, TSU_FWEN1); /* Disable forward(1->0) */ |
| 2370 | sh_eth_tsu_write(mdp, 0, TSU_FCM); /* forward fifo 3k-3k */ |
| 2371 | sh_eth_tsu_write(mdp, 0xc, TSU_BSYSL0); |
| 2372 | sh_eth_tsu_write(mdp, 0xc, TSU_BSYSL1); |
| 2373 | sh_eth_tsu_write(mdp, 0, TSU_PRISL0); |
| 2374 | sh_eth_tsu_write(mdp, 0, TSU_PRISL1); |
| 2375 | sh_eth_tsu_write(mdp, 0, TSU_FWSL0); |
| 2376 | sh_eth_tsu_write(mdp, 0, TSU_FWSL1); |
| 2377 | 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] | 2378 | if (sh_eth_is_gether(mdp)) { |
| 2379 | sh_eth_tsu_write(mdp, 0, TSU_QTAG0); /* Disable QTAG(0->1) */ |
| 2380 | sh_eth_tsu_write(mdp, 0, TSU_QTAG1); /* Disable QTAG(1->0) */ |
| 2381 | } else { |
| 2382 | sh_eth_tsu_write(mdp, 0, TSU_QTAGM0); /* Disable QTAG(0->1) */ |
| 2383 | sh_eth_tsu_write(mdp, 0, TSU_QTAGM1); /* Disable QTAG(1->0) */ |
| 2384 | } |
Yoshihiro Shimoda | 4a55530 | 2011-03-07 21:59:26 +0000 | [diff] [blame] | 2385 | sh_eth_tsu_write(mdp, 0, TSU_FWSR); /* all interrupt status clear */ |
| 2386 | sh_eth_tsu_write(mdp, 0, TSU_FWINMK); /* Disable all interrupt */ |
| 2387 | sh_eth_tsu_write(mdp, 0, TSU_TEN); /* Disable all CAM entry */ |
| 2388 | sh_eth_tsu_write(mdp, 0, TSU_POST1); /* Disable CAM entry [ 0- 7] */ |
| 2389 | sh_eth_tsu_write(mdp, 0, TSU_POST2); /* Disable CAM entry [ 8-15] */ |
| 2390 | sh_eth_tsu_write(mdp, 0, TSU_POST3); /* Disable CAM entry [16-23] */ |
| 2391 | 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] | 2392 | } |
| 2393 | |
| 2394 | /* MDIO bus release function */ |
| 2395 | static int sh_mdio_release(struct net_device *ndev) |
| 2396 | { |
| 2397 | struct mii_bus *bus = dev_get_drvdata(&ndev->dev); |
| 2398 | |
| 2399 | /* unregister mdio bus */ |
| 2400 | mdiobus_unregister(bus); |
| 2401 | |
| 2402 | /* remove mdio bus info from net_device */ |
| 2403 | dev_set_drvdata(&ndev->dev, NULL); |
| 2404 | |
| 2405 | /* free bitbang info */ |
| 2406 | free_mdio_bitbang(bus); |
| 2407 | |
| 2408 | return 0; |
| 2409 | } |
| 2410 | |
| 2411 | /* MDIO bus init function */ |
Yoshihiro Shimoda | b3017e6 | 2011-03-07 21:59:55 +0000 | [diff] [blame] | 2412 | static int sh_mdio_init(struct net_device *ndev, int id, |
| 2413 | struct sh_eth_plat_data *pd) |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 2414 | { |
| 2415 | int ret, i; |
| 2416 | struct bb_info *bitbang; |
| 2417 | struct sh_eth_private *mdp = netdev_priv(ndev); |
| 2418 | |
| 2419 | /* create bit control struct for PHY */ |
Sergei Shtylyov | d5e07e6 | 2013-03-21 10:41:11 +0000 | [diff] [blame] | 2420 | bitbang = devm_kzalloc(&ndev->dev, sizeof(struct bb_info), |
| 2421 | GFP_KERNEL); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 2422 | if (!bitbang) { |
| 2423 | ret = -ENOMEM; |
| 2424 | goto out; |
| 2425 | } |
| 2426 | |
| 2427 | /* bitbang init */ |
Yoshihiro Shimoda | ae70644 | 2011-09-27 21:48:58 +0000 | [diff] [blame] | 2428 | bitbang->addr = mdp->addr + mdp->reg_offset[PIR]; |
Yoshihiro Shimoda | b3017e6 | 2011-03-07 21:59:55 +0000 | [diff] [blame] | 2429 | bitbang->set_gate = pd->set_mdio_gate; |
Sergei Shtylyov | dfed5e7 | 2013-03-21 10:37:54 +0000 | [diff] [blame] | 2430 | bitbang->mdi_msk = PIR_MDI; |
| 2431 | bitbang->mdo_msk = PIR_MDO; |
| 2432 | bitbang->mmd_msk = PIR_MMD; |
| 2433 | bitbang->mdc_msk = PIR_MDC; |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 2434 | bitbang->ctrl.ops = &bb_ops; |
| 2435 | |
Stefan Weil | c2e07b3 | 2010-08-03 19:44:52 +0200 | [diff] [blame] | 2436 | /* MII controller setting */ |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 2437 | mdp->mii_bus = alloc_mdio_bitbang(&bitbang->ctrl); |
| 2438 | if (!mdp->mii_bus) { |
| 2439 | ret = -ENOMEM; |
Sergei Shtylyov | d5e07e6 | 2013-03-21 10:41:11 +0000 | [diff] [blame] | 2440 | goto out; |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 2441 | } |
| 2442 | |
| 2443 | /* Hook up MII support for ethtool */ |
| 2444 | mdp->mii_bus->name = "sh_mii"; |
Lennert Buytenhek | 18ee49d | 2008-10-01 15:41:33 +0000 | [diff] [blame] | 2445 | mdp->mii_bus->parent = &ndev->dev; |
Florian Fainelli | 5278fb5 | 2012-01-09 23:59:17 +0000 | [diff] [blame] | 2446 | snprintf(mdp->mii_bus->id, MII_BUS_ID_SIZE, "%s-%x", |
Nobuhiro Iwamatsu | 34aa6f1 | 2012-01-16 16:50:16 +0000 | [diff] [blame] | 2447 | mdp->pdev->name, id); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 2448 | |
| 2449 | /* PHY IRQ */ |
Sergei Shtylyov | d5e07e6 | 2013-03-21 10:41:11 +0000 | [diff] [blame] | 2450 | mdp->mii_bus->irq = devm_kzalloc(&ndev->dev, |
| 2451 | sizeof(int) * PHY_MAX_ADDR, |
| 2452 | GFP_KERNEL); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 2453 | if (!mdp->mii_bus->irq) { |
| 2454 | ret = -ENOMEM; |
| 2455 | goto out_free_bus; |
| 2456 | } |
| 2457 | |
| 2458 | for (i = 0; i < PHY_MAX_ADDR; i++) |
| 2459 | mdp->mii_bus->irq[i] = PHY_POLL; |
| 2460 | |
YOSHIFUJI Hideaki / 吉藤英明 | 8f6352f | 2012-11-02 04:45:07 +0000 | [diff] [blame] | 2461 | /* register mdio bus */ |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 2462 | ret = mdiobus_register(mdp->mii_bus); |
| 2463 | if (ret) |
Sergei Shtylyov | d5e07e6 | 2013-03-21 10:41:11 +0000 | [diff] [blame] | 2464 | goto out_free_bus; |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 2465 | |
| 2466 | dev_set_drvdata(&ndev->dev, mdp->mii_bus); |
| 2467 | |
| 2468 | return 0; |
| 2469 | |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 2470 | out_free_bus: |
Lennert Buytenhek | 298cf9b | 2008-10-08 16:29:57 -0700 | [diff] [blame] | 2471 | free_mdio_bitbang(mdp->mii_bus); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 2472 | |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 2473 | out: |
| 2474 | return ret; |
| 2475 | } |
| 2476 | |
Yoshihiro Shimoda | 4a55530 | 2011-03-07 21:59:26 +0000 | [diff] [blame] | 2477 | static const u16 *sh_eth_get_register_offset(int register_type) |
| 2478 | { |
| 2479 | const u16 *reg_offset = NULL; |
| 2480 | |
| 2481 | switch (register_type) { |
| 2482 | case SH_ETH_REG_GIGABIT: |
| 2483 | reg_offset = sh_eth_offset_gigabit; |
| 2484 | break; |
Sergei Shtylyov | a3f109b | 2013-03-28 11:51:31 +0000 | [diff] [blame] | 2485 | case SH_ETH_REG_FAST_RCAR: |
| 2486 | reg_offset = sh_eth_offset_fast_rcar; |
| 2487 | break; |
Yoshihiro Shimoda | 4a55530 | 2011-03-07 21:59:26 +0000 | [diff] [blame] | 2488 | case SH_ETH_REG_FAST_SH4: |
| 2489 | reg_offset = sh_eth_offset_fast_sh4; |
| 2490 | break; |
| 2491 | case SH_ETH_REG_FAST_SH3_SH2: |
| 2492 | reg_offset = sh_eth_offset_fast_sh3_sh2; |
| 2493 | break; |
| 2494 | default: |
Nobuhiro Iwamatsu | 14c3326 | 2013-03-20 22:46:55 +0000 | [diff] [blame] | 2495 | pr_err("Unknown register type (%d)\n", register_type); |
Yoshihiro Shimoda | 4a55530 | 2011-03-07 21:59:26 +0000 | [diff] [blame] | 2496 | break; |
| 2497 | } |
| 2498 | |
| 2499 | return reg_offset; |
| 2500 | } |
| 2501 | |
Nobuhiro Iwamatsu | 9f86134 | 2013-06-06 09:48:09 +0000 | [diff] [blame] | 2502 | static struct net_device_ops sh_eth_netdev_ops = { |
Alexander Beregalov | ebf84ea | 2009-04-11 07:40:49 +0000 | [diff] [blame] | 2503 | .ndo_open = sh_eth_open, |
| 2504 | .ndo_stop = sh_eth_close, |
| 2505 | .ndo_start_xmit = sh_eth_start_xmit, |
| 2506 | .ndo_get_stats = sh_eth_get_stats, |
Alexander Beregalov | ebf84ea | 2009-04-11 07:40:49 +0000 | [diff] [blame] | 2507 | .ndo_tx_timeout = sh_eth_tx_timeout, |
| 2508 | .ndo_do_ioctl = sh_eth_do_ioctl, |
| 2509 | .ndo_validate_addr = eth_validate_addr, |
| 2510 | .ndo_set_mac_address = eth_mac_addr, |
| 2511 | .ndo_change_mtu = eth_change_mtu, |
| 2512 | }; |
| 2513 | |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 2514 | static int sh_eth_drv_probe(struct platform_device *pdev) |
| 2515 | { |
Kuninori Morimoto | 9c38657 | 2010-08-19 00:39:45 -0700 | [diff] [blame] | 2516 | int ret, devno = 0; |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 2517 | struct resource *res; |
| 2518 | struct net_device *ndev = NULL; |
Kuninori Morimoto | ec0d755 | 2011-06-23 16:02:38 +0000 | [diff] [blame] | 2519 | struct sh_eth_private *mdp = NULL; |
Sergei Shtylyov | 564044b | 2013-03-21 10:39:22 +0000 | [diff] [blame] | 2520 | struct sh_eth_plat_data *pd = pdev->dev.platform_data; |
Sergei Shtylyov | afe391a | 2013-06-07 13:54:02 +0000 | [diff] [blame] | 2521 | const struct platform_device_id *id = platform_get_device_id(pdev); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 2522 | |
| 2523 | /* get base addr */ |
| 2524 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 2525 | if (unlikely(res == NULL)) { |
| 2526 | dev_err(&pdev->dev, "invalid resource\n"); |
| 2527 | ret = -EINVAL; |
| 2528 | goto out; |
| 2529 | } |
| 2530 | |
| 2531 | ndev = alloc_etherdev(sizeof(struct sh_eth_private)); |
| 2532 | if (!ndev) { |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 2533 | ret = -ENOMEM; |
| 2534 | goto out; |
| 2535 | } |
| 2536 | |
| 2537 | /* The sh Ether-specific entries in the device structure. */ |
| 2538 | ndev->base_addr = res->start; |
| 2539 | devno = pdev->id; |
| 2540 | if (devno < 0) |
| 2541 | devno = 0; |
| 2542 | |
| 2543 | ndev->dma = -1; |
roel kluin | cc3c080 | 2008-09-10 19:22:44 +0200 | [diff] [blame] | 2544 | ret = platform_get_irq(pdev, 0); |
| 2545 | if (ret < 0) { |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 2546 | ret = -ENODEV; |
| 2547 | goto out_release; |
| 2548 | } |
roel kluin | cc3c080 | 2008-09-10 19:22:44 +0200 | [diff] [blame] | 2549 | ndev->irq = ret; |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 2550 | |
| 2551 | SET_NETDEV_DEV(ndev, &pdev->dev); |
| 2552 | |
| 2553 | /* Fill in the fields of the device structure with ethernet values. */ |
| 2554 | ether_setup(ndev); |
| 2555 | |
| 2556 | mdp = netdev_priv(ndev); |
Yoshihiro Shimoda | 525b807 | 2012-06-26 20:00:03 +0000 | [diff] [blame] | 2557 | mdp->num_tx_ring = TX_RING_SIZE; |
| 2558 | mdp->num_rx_ring = RX_RING_SIZE; |
Sergei Shtylyov | d5e07e6 | 2013-03-21 10:41:11 +0000 | [diff] [blame] | 2559 | mdp->addr = devm_ioremap_resource(&pdev->dev, res); |
| 2560 | if (IS_ERR(mdp->addr)) { |
| 2561 | ret = PTR_ERR(mdp->addr); |
Yoshihiro Shimoda | ae70644 | 2011-09-27 21:48:58 +0000 | [diff] [blame] | 2562 | goto out_release; |
| 2563 | } |
| 2564 | |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 2565 | spin_lock_init(&mdp->lock); |
Magnus Damm | bcd5149 | 2009-10-09 00:20:04 +0000 | [diff] [blame] | 2566 | mdp->pdev = pdev; |
| 2567 | pm_runtime_enable(&pdev->dev); |
| 2568 | pm_runtime_resume(&pdev->dev); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 2569 | |
| 2570 | /* get PHY ID */ |
Yoshinori Sato | 71557a3 | 2008-08-06 19:49:00 -0400 | [diff] [blame] | 2571 | mdp->phy_id = pd->phy; |
Yoshihiro Shimoda | e47c905 | 2011-03-07 21:59:45 +0000 | [diff] [blame] | 2572 | mdp->phy_interface = pd->phy_interface; |
Yoshinori Sato | 71557a3 | 2008-08-06 19:49:00 -0400 | [diff] [blame] | 2573 | /* EDMAC endian */ |
| 2574 | mdp->edmac_endian = pd->edmac_endian; |
Yoshihiro Shimoda | 4923576 | 2009-08-27 23:25:03 +0000 | [diff] [blame] | 2575 | mdp->no_ether_link = pd->no_ether_link; |
| 2576 | mdp->ether_link_active_low = pd->ether_link_active_low; |
Yoshihiro Shimoda | 4a55530 | 2011-03-07 21:59:26 +0000 | [diff] [blame] | 2577 | mdp->reg_offset = sh_eth_get_register_offset(pd->register_type); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 2578 | |
Yoshihiro Shimoda | 380af9e | 2009-05-24 23:54:21 +0000 | [diff] [blame] | 2579 | /* set cpu data */ |
Yoshihiro Shimoda | 8fcd496 | 2011-03-07 21:59:49 +0000 | [diff] [blame] | 2580 | #if defined(SH_ETH_HAS_BOTH_MODULES) |
| 2581 | mdp->cd = sh_eth_get_cpu_data(mdp); |
| 2582 | #else |
Yoshihiro Shimoda | 380af9e | 2009-05-24 23:54:21 +0000 | [diff] [blame] | 2583 | mdp->cd = &sh_eth_my_cpu_data; |
Yoshihiro Shimoda | 8fcd496 | 2011-03-07 21:59:49 +0000 | [diff] [blame] | 2584 | #endif |
Sergei Shtylyov | afe391a | 2013-06-07 13:54:02 +0000 | [diff] [blame] | 2585 | if (id->driver_data) |
| 2586 | mdp->cd = (struct sh_eth_cpu_data *)id->driver_data; |
Yoshihiro Shimoda | 380af9e | 2009-05-24 23:54:21 +0000 | [diff] [blame] | 2587 | sh_eth_set_default_cpu_data(mdp->cd); |
| 2588 | |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 2589 | /* set function */ |
Nobuhiro Iwamatsu | 9f86134 | 2013-06-06 09:48:09 +0000 | [diff] [blame] | 2590 | if (mdp->cd->tsu) { |
| 2591 | sh_eth_netdev_ops.ndo_set_rx_mode = sh_eth_set_multicast_list; |
| 2592 | sh_eth_netdev_ops.ndo_vlan_rx_add_vid = sh_eth_vlan_rx_add_vid; |
| 2593 | sh_eth_netdev_ops.ndo_vlan_rx_kill_vid = |
| 2594 | sh_eth_vlan_rx_kill_vid; |
| 2595 | } |
| 2596 | |
Alexander Beregalov | ebf84ea | 2009-04-11 07:40:49 +0000 | [diff] [blame] | 2597 | ndev->netdev_ops = &sh_eth_netdev_ops; |
Nobuhiro Iwamatsu | dc19e4e | 2011-02-15 21:17:32 +0000 | [diff] [blame] | 2598 | SET_ETHTOOL_OPS(ndev, &sh_eth_ethtool_ops); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 2599 | ndev->watchdog_timeo = TX_TIMEOUT; |
| 2600 | |
Nobuhiro Iwamatsu | dc19e4e | 2011-02-15 21:17:32 +0000 | [diff] [blame] | 2601 | /* debug message level */ |
| 2602 | mdp->msg_enable = SH_ETH_DEF_MSG_ENABLE; |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 2603 | |
| 2604 | /* read and set MAC address */ |
Magnus Damm | 748031f | 2009-10-09 00:17:14 +0000 | [diff] [blame] | 2605 | read_mac_address(ndev, pd->mac_addr); |
Sergei Shtylyov | ff6e722 | 2013-04-29 09:49:42 +0000 | [diff] [blame] | 2606 | if (!is_valid_ether_addr(ndev->dev_addr)) { |
| 2607 | dev_warn(&pdev->dev, |
| 2608 | "no valid MAC address supplied, using a random one.\n"); |
| 2609 | eth_hw_addr_random(ndev); |
| 2610 | } |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 2611 | |
Yoshihiro Shimoda | 6ba8802 | 2012-02-15 17:55:01 +0000 | [diff] [blame] | 2612 | /* ioremap the TSU registers */ |
| 2613 | if (mdp->cd->tsu) { |
| 2614 | struct resource *rtsu; |
| 2615 | rtsu = platform_get_resource(pdev, IORESOURCE_MEM, 1); |
Sergei Shtylyov | d5e07e6 | 2013-03-21 10:41:11 +0000 | [diff] [blame] | 2616 | mdp->tsu_addr = devm_ioremap_resource(&pdev->dev, rtsu); |
| 2617 | if (IS_ERR(mdp->tsu_addr)) { |
| 2618 | ret = PTR_ERR(mdp->tsu_addr); |
Sergei Shtylyov | fc0c090 | 2013-03-19 13:41:32 +0000 | [diff] [blame] | 2619 | goto out_release; |
| 2620 | } |
Yoshihiro Shimoda | 6743fe6 | 2012-02-15 17:55:03 +0000 | [diff] [blame] | 2621 | mdp->port = devno % 2; |
Patrick McHardy | f646968 | 2013-04-19 02:04:27 +0000 | [diff] [blame] | 2622 | ndev->features = NETIF_F_HW_VLAN_CTAG_FILTER; |
Yoshihiro Shimoda | 6ba8802 | 2012-02-15 17:55:01 +0000 | [diff] [blame] | 2623 | } |
| 2624 | |
Yoshihiro Shimoda | 150647f | 2012-02-15 17:54:56 +0000 | [diff] [blame] | 2625 | /* initialize first or needed device */ |
| 2626 | if (!devno || pd->needs_init) { |
Yoshihiro Shimoda | 380af9e | 2009-05-24 23:54:21 +0000 | [diff] [blame] | 2627 | if (mdp->cd->chip_reset) |
| 2628 | mdp->cd->chip_reset(ndev); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 2629 | |
Yoshihiro Shimoda | 4986b99 | 2011-03-07 21:59:34 +0000 | [diff] [blame] | 2630 | if (mdp->cd->tsu) { |
| 2631 | /* TSU init (Init only)*/ |
| 2632 | sh_eth_tsu_init(mdp); |
| 2633 | } |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 2634 | } |
| 2635 | |
| 2636 | /* network device register */ |
| 2637 | ret = register_netdev(ndev); |
| 2638 | if (ret) |
| 2639 | goto out_release; |
| 2640 | |
| 2641 | /* mdio bus init */ |
Yoshihiro Shimoda | b3017e6 | 2011-03-07 21:59:55 +0000 | [diff] [blame] | 2642 | ret = sh_mdio_init(ndev, pdev->id, pd); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 2643 | if (ret) |
| 2644 | goto out_unregister; |
| 2645 | |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 2646 | /* print device information */ |
H Hartley Sweeten | 6cd9b49 | 2009-12-29 20:10:35 -0800 | [diff] [blame] | 2647 | pr_info("Base address at 0x%x, %pM, IRQ %d.\n", |
| 2648 | (u32)ndev->base_addr, ndev->dev_addr, ndev->irq); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 2649 | |
| 2650 | platform_set_drvdata(pdev, ndev); |
| 2651 | |
| 2652 | return ret; |
| 2653 | |
| 2654 | out_unregister: |
| 2655 | unregister_netdev(ndev); |
| 2656 | |
| 2657 | out_release: |
| 2658 | /* net_dev free */ |
| 2659 | if (ndev) |
| 2660 | free_netdev(ndev); |
| 2661 | |
| 2662 | out: |
| 2663 | return ret; |
| 2664 | } |
| 2665 | |
| 2666 | static int sh_eth_drv_remove(struct platform_device *pdev) |
| 2667 | { |
| 2668 | struct net_device *ndev = platform_get_drvdata(pdev); |
| 2669 | |
| 2670 | sh_mdio_release(ndev); |
| 2671 | unregister_netdev(ndev); |
Magnus Damm | bcd5149 | 2009-10-09 00:20:04 +0000 | [diff] [blame] | 2672 | pm_runtime_disable(&pdev->dev); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 2673 | free_netdev(ndev); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 2674 | |
| 2675 | return 0; |
| 2676 | } |
| 2677 | |
Nobuhiro Iwamatsu | 540ad1b | 2013-06-06 09:52:37 +0000 | [diff] [blame] | 2678 | #ifdef CONFIG_PM |
Magnus Damm | bcd5149 | 2009-10-09 00:20:04 +0000 | [diff] [blame] | 2679 | static int sh_eth_runtime_nop(struct device *dev) |
| 2680 | { |
| 2681 | /* |
| 2682 | * Runtime PM callback shared between ->runtime_suspend() |
| 2683 | * and ->runtime_resume(). Simply returns success. |
| 2684 | * |
| 2685 | * This driver re-initializes all registers after |
| 2686 | * pm_runtime_get_sync() anyway so there is no need |
| 2687 | * to save and restore registers here. |
| 2688 | */ |
| 2689 | return 0; |
| 2690 | } |
| 2691 | |
Nobuhiro Iwamatsu | 540ad1b | 2013-06-06 09:52:37 +0000 | [diff] [blame] | 2692 | static const struct dev_pm_ops sh_eth_dev_pm_ops = { |
Magnus Damm | bcd5149 | 2009-10-09 00:20:04 +0000 | [diff] [blame] | 2693 | .runtime_suspend = sh_eth_runtime_nop, |
| 2694 | .runtime_resume = sh_eth_runtime_nop, |
| 2695 | }; |
Nobuhiro Iwamatsu | 540ad1b | 2013-06-06 09:52:37 +0000 | [diff] [blame] | 2696 | #define SH_ETH_PM_OPS (&sh_eth_dev_pm_ops) |
| 2697 | #else |
| 2698 | #define SH_ETH_PM_OPS NULL |
| 2699 | #endif |
Magnus Damm | bcd5149 | 2009-10-09 00:20:04 +0000 | [diff] [blame] | 2700 | |
Sergei Shtylyov | afe391a | 2013-06-07 13:54:02 +0000 | [diff] [blame] | 2701 | static struct platform_device_id sh_eth_id_table[] = { |
Sergei Shtylyov | c18a79a | 2013-06-07 13:56:05 +0000 | [diff] [blame^] | 2702 | { "sh7619-ether", (kernel_ulong_t)&sh7619_data }, |
Sergei Shtylyov | 7bbe150 | 2013-06-07 13:55:08 +0000 | [diff] [blame] | 2703 | { "sh771x-ether", (kernel_ulong_t)&sh771x_data }, |
Sergei Shtylyov | afe391a | 2013-06-07 13:54:02 +0000 | [diff] [blame] | 2704 | { CARDNAME }, |
| 2705 | { } |
| 2706 | }; |
| 2707 | MODULE_DEVICE_TABLE(platform, sh_eth_id_table); |
| 2708 | |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 2709 | static struct platform_driver sh_eth_driver = { |
| 2710 | .probe = sh_eth_drv_probe, |
| 2711 | .remove = sh_eth_drv_remove, |
Sergei Shtylyov | afe391a | 2013-06-07 13:54:02 +0000 | [diff] [blame] | 2712 | .id_table = sh_eth_id_table, |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 2713 | .driver = { |
| 2714 | .name = CARDNAME, |
Nobuhiro Iwamatsu | 540ad1b | 2013-06-06 09:52:37 +0000 | [diff] [blame] | 2715 | .pm = SH_ETH_PM_OPS, |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 2716 | }, |
| 2717 | }; |
| 2718 | |
Axel Lin | db62f68 | 2011-11-27 16:44:17 +0000 | [diff] [blame] | 2719 | module_platform_driver(sh_eth_driver); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 2720 | |
| 2721 | MODULE_AUTHOR("Nobuhiro Iwamatsu, Yoshihiro Shimoda"); |
| 2722 | MODULE_DESCRIPTION("Renesas SuperH Ethernet driver"); |
| 2723 | MODULE_LICENSE("GPL v2"); |