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