Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1 | /* |
| 2 | * SuperH Ethernet device driver |
| 3 | * |
Nobuhiro Iwamatsu | b0ca2a2 | 2008-06-30 11:08:17 +0900 | [diff] [blame] | 4 | * Copyright (C) 2006-2008 Nobuhiro Iwamatsu |
Yoshihiro Shimoda | 380af9e | 2009-05-24 23:54:21 +0000 | [diff] [blame] | 5 | * Copyright (C) 2008-2009 Renesas Solutions Corp. |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify it |
| 8 | * under the terms and conditions of the GNU General Public License, |
| 9 | * version 2, as published by the Free Software Foundation. |
| 10 | * |
| 11 | * This program is distributed in the hope it will be useful, but WITHOUT |
| 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
| 14 | * more details. |
| 15 | * You should have received a copy of the GNU General Public License along with |
| 16 | * this program; if not, write to the Free Software Foundation, Inc., |
| 17 | * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA. |
| 18 | * |
| 19 | * The full GNU General Public License is included in this distribution in |
| 20 | * the file called "COPYING". |
| 21 | */ |
| 22 | |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 23 | #include <linux/init.h> |
| 24 | #include <linux/dma-mapping.h> |
| 25 | #include <linux/etherdevice.h> |
| 26 | #include <linux/delay.h> |
| 27 | #include <linux/platform_device.h> |
| 28 | #include <linux/mdio-bitbang.h> |
| 29 | #include <linux/netdevice.h> |
| 30 | #include <linux/phy.h> |
| 31 | #include <linux/cache.h> |
| 32 | #include <linux/io.h> |
Magnus Damm | bcd5149 | 2009-10-09 00:20:04 +0000 | [diff] [blame] | 33 | #include <linux/pm_runtime.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 34 | #include <linux/slab.h> |
Nobuhiro Iwamatsu | dc19e4e | 2011-02-15 21:17:32 +0000 | [diff] [blame] | 35 | #include <linux/ethtool.h> |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 36 | |
| 37 | #include "sh_eth.h" |
| 38 | |
Nobuhiro Iwamatsu | dc19e4e | 2011-02-15 21:17:32 +0000 | [diff] [blame] | 39 | #define SH_ETH_DEF_MSG_ENABLE \ |
| 40 | (NETIF_MSG_LINK | \ |
| 41 | NETIF_MSG_TIMER | \ |
| 42 | NETIF_MSG_RX_ERR| \ |
| 43 | NETIF_MSG_TX_ERR) |
| 44 | |
Yoshihiro Shimoda | 380af9e | 2009-05-24 23:54:21 +0000 | [diff] [blame] | 45 | /* There is CPU dependent code */ |
Yoshihiro Shimoda | 65ac885 | 2009-05-24 23:54:30 +0000 | [diff] [blame] | 46 | #if defined(CONFIG_CPU_SUBTYPE_SH7724) |
| 47 | #define SH_ETH_RESET_DEFAULT 1 |
| 48 | static void sh_eth_set_duplex(struct net_device *ndev) |
| 49 | { |
| 50 | struct sh_eth_private *mdp = netdev_priv(ndev); |
Yoshihiro Shimoda | 65ac885 | 2009-05-24 23:54:30 +0000 | [diff] [blame] | 51 | |
| 52 | if (mdp->duplex) /* Full */ |
Yoshihiro Shimoda | 4a55530 | 2011-03-07 21:59:26 +0000 | [diff] [blame] | 53 | sh_eth_write(ndev, sh_eth_read(ndev, ECMR) | ECMR_DM, ECMR); |
Yoshihiro Shimoda | 65ac885 | 2009-05-24 23:54:30 +0000 | [diff] [blame] | 54 | else /* Half */ |
Yoshihiro Shimoda | 4a55530 | 2011-03-07 21:59:26 +0000 | [diff] [blame] | 55 | sh_eth_write(ndev, sh_eth_read(ndev, ECMR) & ~ECMR_DM, ECMR); |
Yoshihiro Shimoda | 65ac885 | 2009-05-24 23:54:30 +0000 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | static void sh_eth_set_rate(struct net_device *ndev) |
| 59 | { |
| 60 | struct sh_eth_private *mdp = netdev_priv(ndev); |
Yoshihiro Shimoda | 65ac885 | 2009-05-24 23:54:30 +0000 | [diff] [blame] | 61 | |
| 62 | switch (mdp->speed) { |
| 63 | case 10: /* 10BASE */ |
Yoshihiro Shimoda | 4a55530 | 2011-03-07 21:59:26 +0000 | [diff] [blame] | 64 | sh_eth_write(ndev, sh_eth_read(ndev, ECMR) & ~ECMR_RTM, ECMR); |
Yoshihiro Shimoda | 65ac885 | 2009-05-24 23:54:30 +0000 | [diff] [blame] | 65 | break; |
| 66 | case 100:/* 100BASE */ |
Yoshihiro Shimoda | 4a55530 | 2011-03-07 21:59:26 +0000 | [diff] [blame] | 67 | sh_eth_write(ndev, sh_eth_read(ndev, ECMR) | ECMR_RTM, ECMR); |
Yoshihiro Shimoda | 65ac885 | 2009-05-24 23:54:30 +0000 | [diff] [blame] | 68 | break; |
| 69 | default: |
| 70 | break; |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | /* SH7724 */ |
| 75 | static struct sh_eth_cpu_data sh_eth_my_cpu_data = { |
| 76 | .set_duplex = sh_eth_set_duplex, |
| 77 | .set_rate = sh_eth_set_rate, |
| 78 | |
| 79 | .ecsr_value = ECSR_PSRTO | ECSR_LCHNG | ECSR_ICD, |
| 80 | .ecsipr_value = ECSIPR_PSRTOIP | ECSIPR_LCHNGIP | ECSIPR_ICDIP, |
| 81 | .eesipr_value = DMAC_M_RFRMER | DMAC_M_ECI | 0x01ff009f, |
| 82 | |
| 83 | .tx_check = EESR_FTC | EESR_CND | EESR_DLC | EESR_CD | EESR_RTO, |
| 84 | .eesr_err_check = EESR_TWB | EESR_TABT | EESR_RABT | EESR_RDE | |
| 85 | EESR_RFRMER | EESR_TFE | EESR_TDE | EESR_ECI, |
| 86 | .tx_error_check = EESR_TWB | EESR_TABT | EESR_TDE | EESR_TFE, |
| 87 | |
| 88 | .apr = 1, |
| 89 | .mpr = 1, |
| 90 | .tpauser = 1, |
| 91 | .hw_swap = 1, |
Magnus Damm | 503914c | 2009-12-15 21:16:55 -0800 | [diff] [blame] | 92 | .rpadir = 1, |
| 93 | .rpadir_value = 0x00020000, /* NET_IP_ALIGN assumed to be 2 */ |
Yoshihiro Shimoda | 65ac885 | 2009-05-24 23:54:30 +0000 | [diff] [blame] | 94 | }; |
Yoshihiro Shimoda | f29a3d0 | 2010-07-05 18:32:50 +0000 | [diff] [blame] | 95 | #elif defined(CONFIG_CPU_SUBTYPE_SH7757) |
Yoshihiro Shimoda | 8fcd496 | 2011-03-07 21:59:49 +0000 | [diff] [blame] | 96 | #define SH_ETH_HAS_BOTH_MODULES 1 |
| 97 | #define SH_ETH_HAS_TSU 1 |
Yoshihiro Shimoda | f29a3d0 | 2010-07-05 18:32:50 +0000 | [diff] [blame] | 98 | static void sh_eth_set_duplex(struct net_device *ndev) |
| 99 | { |
| 100 | struct sh_eth_private *mdp = netdev_priv(ndev); |
Yoshihiro Shimoda | f29a3d0 | 2010-07-05 18:32:50 +0000 | [diff] [blame] | 101 | |
| 102 | if (mdp->duplex) /* Full */ |
Yoshihiro Shimoda | 4a55530 | 2011-03-07 21:59:26 +0000 | [diff] [blame] | 103 | sh_eth_write(ndev, sh_eth_read(ndev, ECMR) | ECMR_DM, ECMR); |
Yoshihiro Shimoda | f29a3d0 | 2010-07-05 18:32:50 +0000 | [diff] [blame] | 104 | else /* Half */ |
Yoshihiro Shimoda | 4a55530 | 2011-03-07 21:59:26 +0000 | [diff] [blame] | 105 | sh_eth_write(ndev, sh_eth_read(ndev, ECMR) & ~ECMR_DM, ECMR); |
Yoshihiro Shimoda | f29a3d0 | 2010-07-05 18:32:50 +0000 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | static void sh_eth_set_rate(struct net_device *ndev) |
| 109 | { |
| 110 | struct sh_eth_private *mdp = netdev_priv(ndev); |
Yoshihiro Shimoda | f29a3d0 | 2010-07-05 18:32:50 +0000 | [diff] [blame] | 111 | |
| 112 | switch (mdp->speed) { |
| 113 | case 10: /* 10BASE */ |
Yoshihiro Shimoda | 4a55530 | 2011-03-07 21:59:26 +0000 | [diff] [blame] | 114 | sh_eth_write(ndev, 0, RTRATE); |
Yoshihiro Shimoda | f29a3d0 | 2010-07-05 18:32:50 +0000 | [diff] [blame] | 115 | break; |
| 116 | case 100:/* 100BASE */ |
Yoshihiro Shimoda | 4a55530 | 2011-03-07 21:59:26 +0000 | [diff] [blame] | 117 | sh_eth_write(ndev, 1, RTRATE); |
Yoshihiro Shimoda | f29a3d0 | 2010-07-05 18:32:50 +0000 | [diff] [blame] | 118 | break; |
| 119 | default: |
| 120 | break; |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | /* SH7757 */ |
| 125 | static struct sh_eth_cpu_data sh_eth_my_cpu_data = { |
| 126 | .set_duplex = sh_eth_set_duplex, |
| 127 | .set_rate = sh_eth_set_rate, |
| 128 | |
| 129 | .eesipr_value = DMAC_M_RFRMER | DMAC_M_ECI | 0x003fffff, |
| 130 | .rmcr_value = 0x00000001, |
| 131 | |
| 132 | .tx_check = EESR_FTC | EESR_CND | EESR_DLC | EESR_CD | EESR_RTO, |
| 133 | .eesr_err_check = EESR_TWB | EESR_TABT | EESR_RABT | EESR_RDE | |
| 134 | EESR_RFRMER | EESR_TFE | EESR_TDE | EESR_ECI, |
| 135 | .tx_error_check = EESR_TWB | EESR_TABT | EESR_TDE | EESR_TFE, |
| 136 | |
| 137 | .apr = 1, |
| 138 | .mpr = 1, |
| 139 | .tpauser = 1, |
| 140 | .hw_swap = 1, |
| 141 | .no_ade = 1, |
Yoshihiro Shimoda | 2e98e79 | 2011-07-05 20:33:57 +0000 | [diff] [blame] | 142 | .rpadir = 1, |
| 143 | .rpadir_value = 2 << 16, |
Yoshihiro Shimoda | f29a3d0 | 2010-07-05 18:32:50 +0000 | [diff] [blame] | 144 | }; |
Yoshihiro Shimoda | 65ac885 | 2009-05-24 23:54:30 +0000 | [diff] [blame] | 145 | |
Yoshihiro Shimoda | 8fcd496 | 2011-03-07 21:59:49 +0000 | [diff] [blame] | 146 | #define SH_GIGA_ETH_BASE 0xfee00000 |
| 147 | #define GIGA_MALR(port) (SH_GIGA_ETH_BASE + 0x800 * (port) + 0x05c8) |
| 148 | #define GIGA_MAHR(port) (SH_GIGA_ETH_BASE + 0x800 * (port) + 0x05c0) |
| 149 | static void sh_eth_chip_reset_giga(struct net_device *ndev) |
| 150 | { |
| 151 | int i; |
| 152 | unsigned long mahr[2], malr[2]; |
| 153 | |
| 154 | /* save MAHR and MALR */ |
| 155 | for (i = 0; i < 2; i++) { |
| 156 | malr[i] = readl(GIGA_MALR(i)); |
| 157 | mahr[i] = readl(GIGA_MAHR(i)); |
| 158 | } |
| 159 | |
| 160 | /* reset device */ |
| 161 | writel(ARSTR_ARSTR, SH_GIGA_ETH_BASE + 0x1800); |
| 162 | mdelay(1); |
| 163 | |
| 164 | /* restore MAHR and MALR */ |
| 165 | for (i = 0; i < 2; i++) { |
| 166 | writel(malr[i], GIGA_MALR(i)); |
| 167 | writel(mahr[i], GIGA_MAHR(i)); |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | static int sh_eth_is_gether(struct sh_eth_private *mdp); |
| 172 | static void sh_eth_reset(struct net_device *ndev) |
| 173 | { |
| 174 | struct sh_eth_private *mdp = netdev_priv(ndev); |
| 175 | int cnt = 100; |
| 176 | |
| 177 | if (sh_eth_is_gether(mdp)) { |
| 178 | sh_eth_write(ndev, 0x03, EDSR); |
| 179 | sh_eth_write(ndev, sh_eth_read(ndev, EDMR) | EDMR_SRST_GETHER, |
| 180 | EDMR); |
| 181 | while (cnt > 0) { |
| 182 | if (!(sh_eth_read(ndev, EDMR) & 0x3)) |
| 183 | break; |
| 184 | mdelay(1); |
| 185 | cnt--; |
| 186 | } |
| 187 | if (cnt < 0) |
| 188 | printk(KERN_ERR "Device reset fail\n"); |
| 189 | |
| 190 | /* Table Init */ |
| 191 | sh_eth_write(ndev, 0x0, TDLAR); |
| 192 | sh_eth_write(ndev, 0x0, TDFAR); |
| 193 | sh_eth_write(ndev, 0x0, TDFXR); |
| 194 | sh_eth_write(ndev, 0x0, TDFFR); |
| 195 | sh_eth_write(ndev, 0x0, RDLAR); |
| 196 | sh_eth_write(ndev, 0x0, RDFAR); |
| 197 | sh_eth_write(ndev, 0x0, RDFXR); |
| 198 | sh_eth_write(ndev, 0x0, RDFFR); |
| 199 | } else { |
| 200 | sh_eth_write(ndev, sh_eth_read(ndev, EDMR) | EDMR_SRST_ETHER, |
| 201 | EDMR); |
| 202 | mdelay(3); |
| 203 | sh_eth_write(ndev, sh_eth_read(ndev, EDMR) & ~EDMR_SRST_ETHER, |
| 204 | EDMR); |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | static void sh_eth_set_duplex_giga(struct net_device *ndev) |
| 209 | { |
| 210 | struct sh_eth_private *mdp = netdev_priv(ndev); |
| 211 | |
| 212 | if (mdp->duplex) /* Full */ |
| 213 | sh_eth_write(ndev, sh_eth_read(ndev, ECMR) | ECMR_DM, ECMR); |
| 214 | else /* Half */ |
| 215 | sh_eth_write(ndev, sh_eth_read(ndev, ECMR) & ~ECMR_DM, ECMR); |
| 216 | } |
| 217 | |
| 218 | static void sh_eth_set_rate_giga(struct net_device *ndev) |
| 219 | { |
| 220 | struct sh_eth_private *mdp = netdev_priv(ndev); |
| 221 | |
| 222 | switch (mdp->speed) { |
| 223 | case 10: /* 10BASE */ |
| 224 | sh_eth_write(ndev, 0x00000000, GECMR); |
| 225 | break; |
| 226 | case 100:/* 100BASE */ |
| 227 | sh_eth_write(ndev, 0x00000010, GECMR); |
| 228 | break; |
| 229 | case 1000: /* 1000BASE */ |
| 230 | sh_eth_write(ndev, 0x00000020, GECMR); |
| 231 | break; |
| 232 | default: |
| 233 | break; |
| 234 | } |
| 235 | } |
| 236 | |
| 237 | /* SH7757(GETHERC) */ |
| 238 | static struct sh_eth_cpu_data sh_eth_my_cpu_data_giga = { |
| 239 | .chip_reset = sh_eth_chip_reset_giga, |
| 240 | .set_duplex = sh_eth_set_duplex_giga, |
| 241 | .set_rate = sh_eth_set_rate_giga, |
| 242 | |
| 243 | .ecsr_value = ECSR_ICD | ECSR_MPD, |
| 244 | .ecsipr_value = ECSIPR_LCHNGIP | ECSIPR_ICDIP | ECSIPR_MPDIP, |
| 245 | .eesipr_value = DMAC_M_RFRMER | DMAC_M_ECI | 0x003fffff, |
| 246 | |
| 247 | .tx_check = EESR_TC1 | EESR_FTC, |
| 248 | .eesr_err_check = EESR_TWB1 | EESR_TWB | EESR_TABT | EESR_RABT | \ |
| 249 | EESR_RDE | EESR_RFRMER | EESR_TFE | EESR_TDE | \ |
| 250 | EESR_ECI, |
| 251 | .tx_error_check = EESR_TWB1 | EESR_TWB | EESR_TABT | EESR_TDE | \ |
| 252 | EESR_TFE, |
| 253 | .fdr_value = 0x0000072f, |
| 254 | .rmcr_value = 0x00000001, |
| 255 | |
| 256 | .apr = 1, |
| 257 | .mpr = 1, |
| 258 | .tpauser = 1, |
| 259 | .bculr = 1, |
| 260 | .hw_swap = 1, |
| 261 | .rpadir = 1, |
| 262 | .rpadir_value = 2 << 16, |
| 263 | .no_trimd = 1, |
| 264 | .no_ade = 1, |
| 265 | }; |
| 266 | |
| 267 | static struct sh_eth_cpu_data *sh_eth_get_cpu_data(struct sh_eth_private *mdp) |
| 268 | { |
| 269 | if (sh_eth_is_gether(mdp)) |
| 270 | return &sh_eth_my_cpu_data_giga; |
| 271 | else |
| 272 | return &sh_eth_my_cpu_data; |
| 273 | } |
| 274 | |
Yoshihiro Shimoda | 65ac885 | 2009-05-24 23:54:30 +0000 | [diff] [blame] | 275 | #elif defined(CONFIG_CPU_SUBTYPE_SH7763) |
Yoshihiro Shimoda | 380af9e | 2009-05-24 23:54:21 +0000 | [diff] [blame] | 276 | #define SH_ETH_HAS_TSU 1 |
| 277 | static void sh_eth_chip_reset(struct net_device *ndev) |
| 278 | { |
Yoshihiro Shimoda | 4986b99 | 2011-03-07 21:59:34 +0000 | [diff] [blame] | 279 | struct sh_eth_private *mdp = netdev_priv(ndev); |
| 280 | |
Yoshihiro Shimoda | 380af9e | 2009-05-24 23:54:21 +0000 | [diff] [blame] | 281 | /* reset device */ |
Yoshihiro Shimoda | 4986b99 | 2011-03-07 21:59:34 +0000 | [diff] [blame] | 282 | sh_eth_tsu_write(mdp, ARSTR_ARSTR, ARSTR); |
Yoshihiro Shimoda | 380af9e | 2009-05-24 23:54:21 +0000 | [diff] [blame] | 283 | mdelay(1); |
| 284 | } |
| 285 | |
| 286 | static void sh_eth_reset(struct net_device *ndev) |
| 287 | { |
Yoshihiro Shimoda | 380af9e | 2009-05-24 23:54:21 +0000 | [diff] [blame] | 288 | int cnt = 100; |
| 289 | |
Yoshihiro Shimoda | 4a55530 | 2011-03-07 21:59:26 +0000 | [diff] [blame] | 290 | sh_eth_write(ndev, EDSR_ENALL, EDSR); |
Yoshihiro Shimoda | c5ed536 | 2011-03-07 21:59:38 +0000 | [diff] [blame] | 291 | sh_eth_write(ndev, sh_eth_read(ndev, EDMR) | EDMR_SRST_GETHER, EDMR); |
Yoshihiro Shimoda | 380af9e | 2009-05-24 23:54:21 +0000 | [diff] [blame] | 292 | while (cnt > 0) { |
Yoshihiro Shimoda | 4a55530 | 2011-03-07 21:59:26 +0000 | [diff] [blame] | 293 | if (!(sh_eth_read(ndev, EDMR) & 0x3)) |
Yoshihiro Shimoda | 380af9e | 2009-05-24 23:54:21 +0000 | [diff] [blame] | 294 | break; |
| 295 | mdelay(1); |
| 296 | cnt--; |
| 297 | } |
roel kluin | 890c8c1 | 2009-12-30 01:43:45 +0000 | [diff] [blame] | 298 | if (cnt == 0) |
Yoshihiro Shimoda | 380af9e | 2009-05-24 23:54:21 +0000 | [diff] [blame] | 299 | printk(KERN_ERR "Device reset fail\n"); |
| 300 | |
| 301 | /* Table Init */ |
Yoshihiro Shimoda | 4a55530 | 2011-03-07 21:59:26 +0000 | [diff] [blame] | 302 | sh_eth_write(ndev, 0x0, TDLAR); |
| 303 | sh_eth_write(ndev, 0x0, TDFAR); |
| 304 | sh_eth_write(ndev, 0x0, TDFXR); |
| 305 | sh_eth_write(ndev, 0x0, TDFFR); |
| 306 | sh_eth_write(ndev, 0x0, RDLAR); |
| 307 | sh_eth_write(ndev, 0x0, RDFAR); |
| 308 | sh_eth_write(ndev, 0x0, RDFXR); |
| 309 | sh_eth_write(ndev, 0x0, RDFFR); |
Yoshihiro Shimoda | 380af9e | 2009-05-24 23:54:21 +0000 | [diff] [blame] | 310 | } |
| 311 | |
| 312 | static void sh_eth_set_duplex(struct net_device *ndev) |
| 313 | { |
| 314 | struct sh_eth_private *mdp = netdev_priv(ndev); |
Yoshihiro Shimoda | 380af9e | 2009-05-24 23:54:21 +0000 | [diff] [blame] | 315 | |
| 316 | if (mdp->duplex) /* Full */ |
Yoshihiro Shimoda | 4a55530 | 2011-03-07 21:59:26 +0000 | [diff] [blame] | 317 | sh_eth_write(ndev, sh_eth_read(ndev, ECMR) | ECMR_DM, ECMR); |
Yoshihiro Shimoda | 380af9e | 2009-05-24 23:54:21 +0000 | [diff] [blame] | 318 | else /* Half */ |
Yoshihiro Shimoda | 4a55530 | 2011-03-07 21:59:26 +0000 | [diff] [blame] | 319 | sh_eth_write(ndev, sh_eth_read(ndev, ECMR) & ~ECMR_DM, ECMR); |
Yoshihiro Shimoda | 380af9e | 2009-05-24 23:54:21 +0000 | [diff] [blame] | 320 | } |
| 321 | |
| 322 | static void sh_eth_set_rate(struct net_device *ndev) |
| 323 | { |
| 324 | struct sh_eth_private *mdp = netdev_priv(ndev); |
Yoshihiro Shimoda | 380af9e | 2009-05-24 23:54:21 +0000 | [diff] [blame] | 325 | |
| 326 | switch (mdp->speed) { |
| 327 | case 10: /* 10BASE */ |
Yoshihiro Shimoda | 4a55530 | 2011-03-07 21:59:26 +0000 | [diff] [blame] | 328 | sh_eth_write(ndev, GECMR_10, GECMR); |
Yoshihiro Shimoda | 380af9e | 2009-05-24 23:54:21 +0000 | [diff] [blame] | 329 | break; |
| 330 | case 100:/* 100BASE */ |
Yoshihiro Shimoda | 4a55530 | 2011-03-07 21:59:26 +0000 | [diff] [blame] | 331 | sh_eth_write(ndev, GECMR_100, GECMR); |
Yoshihiro Shimoda | 380af9e | 2009-05-24 23:54:21 +0000 | [diff] [blame] | 332 | break; |
| 333 | case 1000: /* 1000BASE */ |
Yoshihiro Shimoda | 4a55530 | 2011-03-07 21:59:26 +0000 | [diff] [blame] | 334 | sh_eth_write(ndev, GECMR_1000, GECMR); |
Yoshihiro Shimoda | 380af9e | 2009-05-24 23:54:21 +0000 | [diff] [blame] | 335 | break; |
| 336 | default: |
| 337 | break; |
| 338 | } |
| 339 | } |
| 340 | |
| 341 | /* sh7763 */ |
| 342 | static struct sh_eth_cpu_data sh_eth_my_cpu_data = { |
| 343 | .chip_reset = sh_eth_chip_reset, |
| 344 | .set_duplex = sh_eth_set_duplex, |
| 345 | .set_rate = sh_eth_set_rate, |
| 346 | |
| 347 | .ecsr_value = ECSR_ICD | ECSR_MPD, |
| 348 | .ecsipr_value = ECSIPR_LCHNGIP | ECSIPR_ICDIP | ECSIPR_MPDIP, |
| 349 | .eesipr_value = DMAC_M_RFRMER | DMAC_M_ECI | 0x003fffff, |
| 350 | |
| 351 | .tx_check = EESR_TC1 | EESR_FTC, |
| 352 | .eesr_err_check = EESR_TWB1 | EESR_TWB | EESR_TABT | EESR_RABT | \ |
| 353 | EESR_RDE | EESR_RFRMER | EESR_TFE | EESR_TDE | \ |
| 354 | EESR_ECI, |
| 355 | .tx_error_check = EESR_TWB1 | EESR_TWB | EESR_TABT | EESR_TDE | \ |
| 356 | EESR_TFE, |
| 357 | |
| 358 | .apr = 1, |
| 359 | .mpr = 1, |
| 360 | .tpauser = 1, |
| 361 | .bculr = 1, |
| 362 | .hw_swap = 1, |
Yoshihiro Shimoda | 380af9e | 2009-05-24 23:54:21 +0000 | [diff] [blame] | 363 | .no_trimd = 1, |
| 364 | .no_ade = 1, |
Yoshihiro Shimoda | 4986b99 | 2011-03-07 21:59:34 +0000 | [diff] [blame] | 365 | .tsu = 1, |
Yoshihiro Shimoda | 380af9e | 2009-05-24 23:54:21 +0000 | [diff] [blame] | 366 | }; |
| 367 | |
| 368 | #elif defined(CONFIG_CPU_SUBTYPE_SH7619) |
| 369 | #define SH_ETH_RESET_DEFAULT 1 |
| 370 | static struct sh_eth_cpu_data sh_eth_my_cpu_data = { |
| 371 | .eesipr_value = DMAC_M_RFRMER | DMAC_M_ECI | 0x003fffff, |
| 372 | |
| 373 | .apr = 1, |
| 374 | .mpr = 1, |
| 375 | .tpauser = 1, |
| 376 | .hw_swap = 1, |
| 377 | }; |
| 378 | #elif defined(CONFIG_CPU_SUBTYPE_SH7710) || defined(CONFIG_CPU_SUBTYPE_SH7712) |
| 379 | #define SH_ETH_RESET_DEFAULT 1 |
| 380 | #define SH_ETH_HAS_TSU 1 |
| 381 | static struct sh_eth_cpu_data sh_eth_my_cpu_data = { |
| 382 | .eesipr_value = DMAC_M_RFRMER | DMAC_M_ECI | 0x003fffff, |
Yoshihiro Shimoda | 4986b99 | 2011-03-07 21:59:34 +0000 | [diff] [blame] | 383 | .tsu = 1, |
Yoshihiro Shimoda | 380af9e | 2009-05-24 23:54:21 +0000 | [diff] [blame] | 384 | }; |
| 385 | #endif |
| 386 | |
| 387 | static void sh_eth_set_default_cpu_data(struct sh_eth_cpu_data *cd) |
| 388 | { |
| 389 | if (!cd->ecsr_value) |
| 390 | cd->ecsr_value = DEFAULT_ECSR_INIT; |
| 391 | |
| 392 | if (!cd->ecsipr_value) |
| 393 | cd->ecsipr_value = DEFAULT_ECSIPR_INIT; |
| 394 | |
| 395 | if (!cd->fcftr_value) |
| 396 | cd->fcftr_value = DEFAULT_FIFO_F_D_RFF | \ |
| 397 | DEFAULT_FIFO_F_D_RFD; |
| 398 | |
| 399 | if (!cd->fdr_value) |
| 400 | cd->fdr_value = DEFAULT_FDR_INIT; |
| 401 | |
| 402 | if (!cd->rmcr_value) |
| 403 | cd->rmcr_value = DEFAULT_RMCR_VALUE; |
| 404 | |
| 405 | if (!cd->tx_check) |
| 406 | cd->tx_check = DEFAULT_TX_CHECK; |
| 407 | |
| 408 | if (!cd->eesr_err_check) |
| 409 | cd->eesr_err_check = DEFAULT_EESR_ERR_CHECK; |
| 410 | |
| 411 | if (!cd->tx_error_check) |
| 412 | cd->tx_error_check = DEFAULT_TX_ERROR_CHECK; |
| 413 | } |
| 414 | |
| 415 | #if defined(SH_ETH_RESET_DEFAULT) |
| 416 | /* Chip Reset */ |
| 417 | static void sh_eth_reset(struct net_device *ndev) |
| 418 | { |
Yoshihiro Shimoda | c5ed536 | 2011-03-07 21:59:38 +0000 | [diff] [blame] | 419 | sh_eth_write(ndev, sh_eth_read(ndev, EDMR) | EDMR_SRST_ETHER, EDMR); |
Yoshihiro Shimoda | 380af9e | 2009-05-24 23:54:21 +0000 | [diff] [blame] | 420 | mdelay(3); |
Yoshihiro Shimoda | c5ed536 | 2011-03-07 21:59:38 +0000 | [diff] [blame] | 421 | sh_eth_write(ndev, sh_eth_read(ndev, EDMR) & ~EDMR_SRST_ETHER, EDMR); |
Yoshihiro Shimoda | 380af9e | 2009-05-24 23:54:21 +0000 | [diff] [blame] | 422 | } |
| 423 | #endif |
| 424 | |
| 425 | #if defined(CONFIG_CPU_SH4) |
| 426 | static void sh_eth_set_receive_align(struct sk_buff *skb) |
| 427 | { |
| 428 | int reserve; |
| 429 | |
| 430 | reserve = SH4_SKB_RX_ALIGN - ((u32)skb->data & (SH4_SKB_RX_ALIGN - 1)); |
| 431 | if (reserve) |
| 432 | skb_reserve(skb, reserve); |
| 433 | } |
| 434 | #else |
| 435 | static void sh_eth_set_receive_align(struct sk_buff *skb) |
| 436 | { |
| 437 | skb_reserve(skb, SH2_SH3_SKB_RX_ALIGN); |
| 438 | } |
| 439 | #endif |
| 440 | |
| 441 | |
Yoshinori Sato | 71557a3 | 2008-08-06 19:49:00 -0400 | [diff] [blame] | 442 | /* CPU <-> EDMAC endian convert */ |
| 443 | static inline __u32 cpu_to_edmac(struct sh_eth_private *mdp, u32 x) |
| 444 | { |
| 445 | switch (mdp->edmac_endian) { |
| 446 | case EDMAC_LITTLE_ENDIAN: |
| 447 | return cpu_to_le32(x); |
| 448 | case EDMAC_BIG_ENDIAN: |
| 449 | return cpu_to_be32(x); |
| 450 | } |
| 451 | return x; |
| 452 | } |
| 453 | |
| 454 | static inline __u32 edmac_to_cpu(struct sh_eth_private *mdp, u32 x) |
| 455 | { |
| 456 | switch (mdp->edmac_endian) { |
| 457 | case EDMAC_LITTLE_ENDIAN: |
| 458 | return le32_to_cpu(x); |
| 459 | case EDMAC_BIG_ENDIAN: |
| 460 | return be32_to_cpu(x); |
| 461 | } |
| 462 | return x; |
| 463 | } |
| 464 | |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 465 | /* |
| 466 | * Program the hardware MAC address from dev->dev_addr. |
| 467 | */ |
| 468 | static void update_mac_address(struct net_device *ndev) |
| 469 | { |
Yoshihiro Shimoda | 4a55530 | 2011-03-07 21:59:26 +0000 | [diff] [blame] | 470 | sh_eth_write(ndev, |
| 471 | (ndev->dev_addr[0] << 24) | (ndev->dev_addr[1] << 16) | |
| 472 | (ndev->dev_addr[2] << 8) | (ndev->dev_addr[3]), MAHR); |
| 473 | sh_eth_write(ndev, |
| 474 | (ndev->dev_addr[4] << 8) | (ndev->dev_addr[5]), MALR); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 475 | } |
| 476 | |
| 477 | /* |
| 478 | * Get MAC address from SuperH MAC address register |
| 479 | * |
| 480 | * SuperH's Ethernet device doesn't have 'ROM' to MAC address. |
| 481 | * This driver get MAC address that use by bootloader(U-boot or sh-ipl+g). |
| 482 | * When you want use this device, you must set MAC address in bootloader. |
| 483 | * |
| 484 | */ |
Magnus Damm | 748031f | 2009-10-09 00:17:14 +0000 | [diff] [blame] | 485 | static void read_mac_address(struct net_device *ndev, unsigned char *mac) |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 486 | { |
Magnus Damm | 748031f | 2009-10-09 00:17:14 +0000 | [diff] [blame] | 487 | if (mac[0] || mac[1] || mac[2] || mac[3] || mac[4] || mac[5]) { |
| 488 | memcpy(ndev->dev_addr, mac, 6); |
| 489 | } else { |
Yoshihiro Shimoda | 4a55530 | 2011-03-07 21:59:26 +0000 | [diff] [blame] | 490 | ndev->dev_addr[0] = (sh_eth_read(ndev, MAHR) >> 24); |
| 491 | ndev->dev_addr[1] = (sh_eth_read(ndev, MAHR) >> 16) & 0xFF; |
| 492 | ndev->dev_addr[2] = (sh_eth_read(ndev, MAHR) >> 8) & 0xFF; |
| 493 | ndev->dev_addr[3] = (sh_eth_read(ndev, MAHR) & 0xFF); |
| 494 | ndev->dev_addr[4] = (sh_eth_read(ndev, MALR) >> 8) & 0xFF; |
| 495 | ndev->dev_addr[5] = (sh_eth_read(ndev, MALR) & 0xFF); |
Magnus Damm | 748031f | 2009-10-09 00:17:14 +0000 | [diff] [blame] | 496 | } |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 497 | } |
| 498 | |
Yoshihiro Shimoda | c5ed536 | 2011-03-07 21:59:38 +0000 | [diff] [blame] | 499 | static int sh_eth_is_gether(struct sh_eth_private *mdp) |
| 500 | { |
| 501 | if (mdp->reg_offset == sh_eth_offset_gigabit) |
| 502 | return 1; |
| 503 | else |
| 504 | return 0; |
| 505 | } |
| 506 | |
| 507 | static unsigned long sh_eth_get_edtrr_trns(struct sh_eth_private *mdp) |
| 508 | { |
| 509 | if (sh_eth_is_gether(mdp)) |
| 510 | return EDTRR_TRNS_GETHER; |
| 511 | else |
| 512 | return EDTRR_TRNS_ETHER; |
| 513 | } |
| 514 | |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 515 | struct bb_info { |
Yoshihiro Shimoda | b3017e6 | 2011-03-07 21:59:55 +0000 | [diff] [blame] | 516 | void (*set_gate)(unsigned long addr); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 517 | struct mdiobb_ctrl ctrl; |
| 518 | u32 addr; |
| 519 | u32 mmd_msk;/* MMD */ |
| 520 | u32 mdo_msk; |
| 521 | u32 mdi_msk; |
| 522 | u32 mdc_msk; |
| 523 | }; |
| 524 | |
| 525 | /* PHY bit set */ |
| 526 | static void bb_set(u32 addr, u32 msk) |
| 527 | { |
Paul Mundt | 900fcf0 | 2010-11-01 09:29:24 +0000 | [diff] [blame] | 528 | writel(readl(addr) | msk, addr); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 529 | } |
| 530 | |
| 531 | /* PHY bit clear */ |
| 532 | static void bb_clr(u32 addr, u32 msk) |
| 533 | { |
Paul Mundt | 900fcf0 | 2010-11-01 09:29:24 +0000 | [diff] [blame] | 534 | writel((readl(addr) & ~msk), addr); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 535 | } |
| 536 | |
| 537 | /* PHY bit read */ |
| 538 | static int bb_read(u32 addr, u32 msk) |
| 539 | { |
Paul Mundt | 900fcf0 | 2010-11-01 09:29:24 +0000 | [diff] [blame] | 540 | return (readl(addr) & msk) != 0; |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 541 | } |
| 542 | |
| 543 | /* Data I/O pin control */ |
| 544 | static void sh_mmd_ctrl(struct mdiobb_ctrl *ctrl, int bit) |
| 545 | { |
| 546 | struct bb_info *bitbang = container_of(ctrl, struct bb_info, ctrl); |
Yoshihiro Shimoda | b3017e6 | 2011-03-07 21:59:55 +0000 | [diff] [blame] | 547 | |
| 548 | if (bitbang->set_gate) |
| 549 | bitbang->set_gate(bitbang->addr); |
| 550 | |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 551 | if (bit) |
| 552 | bb_set(bitbang->addr, bitbang->mmd_msk); |
| 553 | else |
| 554 | bb_clr(bitbang->addr, bitbang->mmd_msk); |
| 555 | } |
| 556 | |
| 557 | /* Set bit data*/ |
| 558 | static void sh_set_mdio(struct mdiobb_ctrl *ctrl, int bit) |
| 559 | { |
| 560 | struct bb_info *bitbang = container_of(ctrl, struct bb_info, ctrl); |
| 561 | |
Yoshihiro Shimoda | b3017e6 | 2011-03-07 21:59:55 +0000 | [diff] [blame] | 562 | if (bitbang->set_gate) |
| 563 | bitbang->set_gate(bitbang->addr); |
| 564 | |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 565 | if (bit) |
| 566 | bb_set(bitbang->addr, bitbang->mdo_msk); |
| 567 | else |
| 568 | bb_clr(bitbang->addr, bitbang->mdo_msk); |
| 569 | } |
| 570 | |
| 571 | /* Get bit data*/ |
| 572 | static int sh_get_mdio(struct mdiobb_ctrl *ctrl) |
| 573 | { |
| 574 | struct bb_info *bitbang = container_of(ctrl, struct bb_info, ctrl); |
Yoshihiro Shimoda | b3017e6 | 2011-03-07 21:59:55 +0000 | [diff] [blame] | 575 | |
| 576 | if (bitbang->set_gate) |
| 577 | bitbang->set_gate(bitbang->addr); |
| 578 | |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 579 | return bb_read(bitbang->addr, bitbang->mdi_msk); |
| 580 | } |
| 581 | |
| 582 | /* MDC pin control */ |
| 583 | static void sh_mdc_ctrl(struct mdiobb_ctrl *ctrl, int bit) |
| 584 | { |
| 585 | struct bb_info *bitbang = container_of(ctrl, struct bb_info, ctrl); |
| 586 | |
Yoshihiro Shimoda | b3017e6 | 2011-03-07 21:59:55 +0000 | [diff] [blame] | 587 | if (bitbang->set_gate) |
| 588 | bitbang->set_gate(bitbang->addr); |
| 589 | |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 590 | if (bit) |
| 591 | bb_set(bitbang->addr, bitbang->mdc_msk); |
| 592 | else |
| 593 | bb_clr(bitbang->addr, bitbang->mdc_msk); |
| 594 | } |
| 595 | |
| 596 | /* mdio bus control struct */ |
| 597 | static struct mdiobb_ops bb_ops = { |
| 598 | .owner = THIS_MODULE, |
| 599 | .set_mdc = sh_mdc_ctrl, |
| 600 | .set_mdio_dir = sh_mmd_ctrl, |
| 601 | .set_mdio_data = sh_set_mdio, |
| 602 | .get_mdio_data = sh_get_mdio, |
| 603 | }; |
| 604 | |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 605 | /* free skb and descriptor buffer */ |
| 606 | static void sh_eth_ring_free(struct net_device *ndev) |
| 607 | { |
| 608 | struct sh_eth_private *mdp = netdev_priv(ndev); |
| 609 | int i; |
| 610 | |
| 611 | /* Free Rx skb ringbuffer */ |
| 612 | if (mdp->rx_skbuff) { |
| 613 | for (i = 0; i < RX_RING_SIZE; i++) { |
| 614 | if (mdp->rx_skbuff[i]) |
| 615 | dev_kfree_skb(mdp->rx_skbuff[i]); |
| 616 | } |
| 617 | } |
| 618 | kfree(mdp->rx_skbuff); |
| 619 | |
| 620 | /* Free Tx skb ringbuffer */ |
| 621 | if (mdp->tx_skbuff) { |
| 622 | for (i = 0; i < TX_RING_SIZE; i++) { |
| 623 | if (mdp->tx_skbuff[i]) |
| 624 | dev_kfree_skb(mdp->tx_skbuff[i]); |
| 625 | } |
| 626 | } |
| 627 | kfree(mdp->tx_skbuff); |
| 628 | } |
| 629 | |
| 630 | /* format skb and descriptor buffer */ |
| 631 | static void sh_eth_ring_format(struct net_device *ndev) |
| 632 | { |
| 633 | struct sh_eth_private *mdp = netdev_priv(ndev); |
| 634 | int i; |
| 635 | struct sk_buff *skb; |
| 636 | struct sh_eth_rxdesc *rxdesc = NULL; |
| 637 | struct sh_eth_txdesc *txdesc = NULL; |
| 638 | int rx_ringsize = sizeof(*rxdesc) * RX_RING_SIZE; |
| 639 | int tx_ringsize = sizeof(*txdesc) * TX_RING_SIZE; |
| 640 | |
| 641 | mdp->cur_rx = mdp->cur_tx = 0; |
| 642 | mdp->dirty_rx = mdp->dirty_tx = 0; |
| 643 | |
| 644 | memset(mdp->rx_ring, 0, rx_ringsize); |
| 645 | |
| 646 | /* build Rx ring buffer */ |
| 647 | for (i = 0; i < RX_RING_SIZE; i++) { |
| 648 | /* skb */ |
| 649 | mdp->rx_skbuff[i] = NULL; |
| 650 | skb = dev_alloc_skb(mdp->rx_buf_sz); |
| 651 | mdp->rx_skbuff[i] = skb; |
| 652 | if (skb == NULL) |
| 653 | break; |
Yoshihiro Shimoda | e88aae7 | 2009-05-24 23:52:35 +0000 | [diff] [blame] | 654 | dma_map_single(&ndev->dev, skb->tail, mdp->rx_buf_sz, |
| 655 | DMA_FROM_DEVICE); |
Nobuhiro Iwamatsu | b0ca2a2 | 2008-06-30 11:08:17 +0900 | [diff] [blame] | 656 | skb->dev = ndev; /* Mark as being used by this device. */ |
Yoshihiro Shimoda | 380af9e | 2009-05-24 23:54:21 +0000 | [diff] [blame] | 657 | sh_eth_set_receive_align(skb); |
| 658 | |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 659 | /* RX descriptor */ |
| 660 | rxdesc = &mdp->rx_ring[i]; |
Yoshihiro Shimoda | 0029d64 | 2009-05-24 23:53:20 +0000 | [diff] [blame] | 661 | rxdesc->addr = virt_to_phys(PTR_ALIGN(skb->data, 4)); |
Yoshinori Sato | 71557a3 | 2008-08-06 19:49:00 -0400 | [diff] [blame] | 662 | rxdesc->status = cpu_to_edmac(mdp, RD_RACT | RD_RFP); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 663 | |
| 664 | /* The size of the buffer is 16 byte boundary. */ |
Yoshihiro Shimoda | 0029d64 | 2009-05-24 23:53:20 +0000 | [diff] [blame] | 665 | rxdesc->buffer_length = ALIGN(mdp->rx_buf_sz, 16); |
Nobuhiro Iwamatsu | b0ca2a2 | 2008-06-30 11:08:17 +0900 | [diff] [blame] | 666 | /* Rx descriptor address set */ |
| 667 | if (i == 0) { |
Yoshihiro Shimoda | 4a55530 | 2011-03-07 21:59:26 +0000 | [diff] [blame] | 668 | sh_eth_write(ndev, mdp->rx_desc_dma, RDLAR); |
Yoshihiro Shimoda | c5ed536 | 2011-03-07 21:59:38 +0000 | [diff] [blame] | 669 | if (sh_eth_is_gether(mdp)) |
| 670 | sh_eth_write(ndev, mdp->rx_desc_dma, RDFAR); |
Nobuhiro Iwamatsu | b0ca2a2 | 2008-06-30 11:08:17 +0900 | [diff] [blame] | 671 | } |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 672 | } |
| 673 | |
| 674 | mdp->dirty_rx = (u32) (i - RX_RING_SIZE); |
| 675 | |
| 676 | /* Mark the last entry as wrapping the ring. */ |
Yoshinori Sato | 71557a3 | 2008-08-06 19:49:00 -0400 | [diff] [blame] | 677 | rxdesc->status |= cpu_to_edmac(mdp, RD_RDEL); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 678 | |
| 679 | memset(mdp->tx_ring, 0, tx_ringsize); |
| 680 | |
| 681 | /* build Tx ring buffer */ |
| 682 | for (i = 0; i < TX_RING_SIZE; i++) { |
| 683 | mdp->tx_skbuff[i] = NULL; |
| 684 | txdesc = &mdp->tx_ring[i]; |
Yoshinori Sato | 71557a3 | 2008-08-06 19:49:00 -0400 | [diff] [blame] | 685 | txdesc->status = cpu_to_edmac(mdp, TD_TFP); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 686 | txdesc->buffer_length = 0; |
Nobuhiro Iwamatsu | b0ca2a2 | 2008-06-30 11:08:17 +0900 | [diff] [blame] | 687 | if (i == 0) { |
Yoshinori Sato | 71557a3 | 2008-08-06 19:49:00 -0400 | [diff] [blame] | 688 | /* Tx descriptor address set */ |
Yoshihiro Shimoda | 4a55530 | 2011-03-07 21:59:26 +0000 | [diff] [blame] | 689 | sh_eth_write(ndev, mdp->tx_desc_dma, TDLAR); |
Yoshihiro Shimoda | c5ed536 | 2011-03-07 21:59:38 +0000 | [diff] [blame] | 690 | if (sh_eth_is_gether(mdp)) |
| 691 | sh_eth_write(ndev, mdp->tx_desc_dma, TDFAR); |
Nobuhiro Iwamatsu | b0ca2a2 | 2008-06-30 11:08:17 +0900 | [diff] [blame] | 692 | } |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 693 | } |
| 694 | |
Yoshinori Sato | 71557a3 | 2008-08-06 19:49:00 -0400 | [diff] [blame] | 695 | txdesc->status |= cpu_to_edmac(mdp, TD_TDLE); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 696 | } |
| 697 | |
| 698 | /* Get skb and descriptor buffer */ |
| 699 | static int sh_eth_ring_init(struct net_device *ndev) |
| 700 | { |
| 701 | struct sh_eth_private *mdp = netdev_priv(ndev); |
| 702 | int rx_ringsize, tx_ringsize, ret = 0; |
| 703 | |
| 704 | /* |
| 705 | * +26 gets the maximum ethernet encapsulation, +7 & ~7 because the |
| 706 | * card needs room to do 8 byte alignment, +2 so we can reserve |
| 707 | * the first 2 bytes, and +16 gets room for the status word from the |
| 708 | * card. |
| 709 | */ |
| 710 | mdp->rx_buf_sz = (ndev->mtu <= 1492 ? PKT_BUF_SZ : |
| 711 | (((ndev->mtu + 26 + 7) & ~7) + 2 + 16)); |
Magnus Damm | 503914c | 2009-12-15 21:16:55 -0800 | [diff] [blame] | 712 | if (mdp->cd->rpadir) |
| 713 | mdp->rx_buf_sz += NET_IP_ALIGN; |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 714 | |
| 715 | /* Allocate RX and TX skb rings */ |
| 716 | mdp->rx_skbuff = kmalloc(sizeof(*mdp->rx_skbuff) * RX_RING_SIZE, |
| 717 | GFP_KERNEL); |
| 718 | if (!mdp->rx_skbuff) { |
Yoshihiro Shimoda | 380af9e | 2009-05-24 23:54:21 +0000 | [diff] [blame] | 719 | dev_err(&ndev->dev, "Cannot allocate Rx skb\n"); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 720 | ret = -ENOMEM; |
| 721 | return ret; |
| 722 | } |
| 723 | |
| 724 | mdp->tx_skbuff = kmalloc(sizeof(*mdp->tx_skbuff) * TX_RING_SIZE, |
| 725 | GFP_KERNEL); |
| 726 | if (!mdp->tx_skbuff) { |
Yoshihiro Shimoda | 380af9e | 2009-05-24 23:54:21 +0000 | [diff] [blame] | 727 | dev_err(&ndev->dev, "Cannot allocate Tx skb\n"); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 728 | ret = -ENOMEM; |
| 729 | goto skb_ring_free; |
| 730 | } |
| 731 | |
| 732 | /* Allocate all Rx descriptors. */ |
| 733 | rx_ringsize = sizeof(struct sh_eth_rxdesc) * RX_RING_SIZE; |
| 734 | mdp->rx_ring = dma_alloc_coherent(NULL, rx_ringsize, &mdp->rx_desc_dma, |
| 735 | GFP_KERNEL); |
| 736 | |
| 737 | if (!mdp->rx_ring) { |
Yoshihiro Shimoda | 380af9e | 2009-05-24 23:54:21 +0000 | [diff] [blame] | 738 | dev_err(&ndev->dev, "Cannot allocate Rx Ring (size %d bytes)\n", |
| 739 | rx_ringsize); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 740 | ret = -ENOMEM; |
| 741 | goto desc_ring_free; |
| 742 | } |
| 743 | |
| 744 | mdp->dirty_rx = 0; |
| 745 | |
| 746 | /* Allocate all Tx descriptors. */ |
| 747 | tx_ringsize = sizeof(struct sh_eth_txdesc) * TX_RING_SIZE; |
| 748 | mdp->tx_ring = dma_alloc_coherent(NULL, tx_ringsize, &mdp->tx_desc_dma, |
| 749 | GFP_KERNEL); |
| 750 | if (!mdp->tx_ring) { |
Yoshihiro Shimoda | 380af9e | 2009-05-24 23:54:21 +0000 | [diff] [blame] | 751 | dev_err(&ndev->dev, "Cannot allocate Tx Ring (size %d bytes)\n", |
| 752 | tx_ringsize); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 753 | ret = -ENOMEM; |
| 754 | goto desc_ring_free; |
| 755 | } |
| 756 | return ret; |
| 757 | |
| 758 | desc_ring_free: |
| 759 | /* free DMA buffer */ |
| 760 | dma_free_coherent(NULL, rx_ringsize, mdp->rx_ring, mdp->rx_desc_dma); |
| 761 | |
| 762 | skb_ring_free: |
| 763 | /* Free Rx and Tx skb ring buffer */ |
| 764 | sh_eth_ring_free(ndev); |
| 765 | |
| 766 | return ret; |
| 767 | } |
| 768 | |
| 769 | static int sh_eth_dev_init(struct net_device *ndev) |
| 770 | { |
| 771 | int ret = 0; |
| 772 | struct sh_eth_private *mdp = netdev_priv(ndev); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 773 | u_int32_t rx_int_var, tx_int_var; |
| 774 | u32 val; |
| 775 | |
| 776 | /* Soft Reset */ |
| 777 | sh_eth_reset(ndev); |
| 778 | |
Nobuhiro Iwamatsu | b0ca2a2 | 2008-06-30 11:08:17 +0900 | [diff] [blame] | 779 | /* Descriptor format */ |
| 780 | sh_eth_ring_format(ndev); |
Yoshihiro Shimoda | 380af9e | 2009-05-24 23:54:21 +0000 | [diff] [blame] | 781 | if (mdp->cd->rpadir) |
Yoshihiro Shimoda | 4a55530 | 2011-03-07 21:59:26 +0000 | [diff] [blame] | 782 | sh_eth_write(ndev, mdp->cd->rpadir_value, RPADIR); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 783 | |
| 784 | /* all sh_eth int mask */ |
Yoshihiro Shimoda | 4a55530 | 2011-03-07 21:59:26 +0000 | [diff] [blame] | 785 | sh_eth_write(ndev, 0, EESIPR); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 786 | |
Yoshihiro Shimoda | 380af9e | 2009-05-24 23:54:21 +0000 | [diff] [blame] | 787 | #if defined(__LITTLE_ENDIAN__) |
| 788 | if (mdp->cd->hw_swap) |
Yoshihiro Shimoda | 4a55530 | 2011-03-07 21:59:26 +0000 | [diff] [blame] | 789 | sh_eth_write(ndev, EDMR_EL, EDMR); |
Yoshihiro Shimoda | 380af9e | 2009-05-24 23:54:21 +0000 | [diff] [blame] | 790 | else |
Nobuhiro Iwamatsu | b0ca2a2 | 2008-06-30 11:08:17 +0900 | [diff] [blame] | 791 | #endif |
Yoshihiro Shimoda | 4a55530 | 2011-03-07 21:59:26 +0000 | [diff] [blame] | 792 | sh_eth_write(ndev, 0, EDMR); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 793 | |
Nobuhiro Iwamatsu | b0ca2a2 | 2008-06-30 11:08:17 +0900 | [diff] [blame] | 794 | /* FIFO size set */ |
Yoshihiro Shimoda | 4a55530 | 2011-03-07 21:59:26 +0000 | [diff] [blame] | 795 | sh_eth_write(ndev, mdp->cd->fdr_value, FDR); |
| 796 | sh_eth_write(ndev, 0, TFTR); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 797 | |
Nobuhiro Iwamatsu | b0ca2a2 | 2008-06-30 11:08:17 +0900 | [diff] [blame] | 798 | /* Frame recv control */ |
Yoshihiro Shimoda | 4a55530 | 2011-03-07 21:59:26 +0000 | [diff] [blame] | 799 | sh_eth_write(ndev, mdp->cd->rmcr_value, RMCR); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 800 | |
| 801 | rx_int_var = mdp->rx_int_var = DESC_I_RINT8 | DESC_I_RINT5; |
| 802 | tx_int_var = mdp->tx_int_var = DESC_I_TINT2; |
Yoshihiro Shimoda | 4a55530 | 2011-03-07 21:59:26 +0000 | [diff] [blame] | 803 | sh_eth_write(ndev, rx_int_var | tx_int_var, TRSCER); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 804 | |
Yoshihiro Shimoda | 380af9e | 2009-05-24 23:54:21 +0000 | [diff] [blame] | 805 | if (mdp->cd->bculr) |
Yoshihiro Shimoda | 4a55530 | 2011-03-07 21:59:26 +0000 | [diff] [blame] | 806 | sh_eth_write(ndev, 0x800, BCULR); /* Burst sycle set */ |
Nobuhiro Iwamatsu | b0ca2a2 | 2008-06-30 11:08:17 +0900 | [diff] [blame] | 807 | |
Yoshihiro Shimoda | 4a55530 | 2011-03-07 21:59:26 +0000 | [diff] [blame] | 808 | sh_eth_write(ndev, mdp->cd->fcftr_value, FCFTR); |
Nobuhiro Iwamatsu | b0ca2a2 | 2008-06-30 11:08:17 +0900 | [diff] [blame] | 809 | |
Yoshihiro Shimoda | 380af9e | 2009-05-24 23:54:21 +0000 | [diff] [blame] | 810 | if (!mdp->cd->no_trimd) |
Yoshihiro Shimoda | 4a55530 | 2011-03-07 21:59:26 +0000 | [diff] [blame] | 811 | sh_eth_write(ndev, 0, TRIMD); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 812 | |
Nobuhiro Iwamatsu | b0ca2a2 | 2008-06-30 11:08:17 +0900 | [diff] [blame] | 813 | /* Recv frame limit set register */ |
Yoshihiro Shimoda | 4a55530 | 2011-03-07 21:59:26 +0000 | [diff] [blame] | 814 | sh_eth_write(ndev, RFLR_VALUE, RFLR); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 815 | |
Yoshihiro Shimoda | 4a55530 | 2011-03-07 21:59:26 +0000 | [diff] [blame] | 816 | sh_eth_write(ndev, sh_eth_read(ndev, EESR), EESR); |
| 817 | sh_eth_write(ndev, mdp->cd->eesipr_value, EESIPR); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 818 | |
| 819 | /* PAUSE Prohibition */ |
Yoshihiro Shimoda | 4a55530 | 2011-03-07 21:59:26 +0000 | [diff] [blame] | 820 | val = (sh_eth_read(ndev, ECMR) & ECMR_DM) | |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 821 | ECMR_ZPF | (mdp->duplex ? ECMR_DM : 0) | ECMR_TE | ECMR_RE; |
| 822 | |
Yoshihiro Shimoda | 4a55530 | 2011-03-07 21:59:26 +0000 | [diff] [blame] | 823 | sh_eth_write(ndev, val, ECMR); |
Nobuhiro Iwamatsu | b0ca2a2 | 2008-06-30 11:08:17 +0900 | [diff] [blame] | 824 | |
Yoshihiro Shimoda | 380af9e | 2009-05-24 23:54:21 +0000 | [diff] [blame] | 825 | if (mdp->cd->set_rate) |
| 826 | mdp->cd->set_rate(ndev); |
| 827 | |
Nobuhiro Iwamatsu | b0ca2a2 | 2008-06-30 11:08:17 +0900 | [diff] [blame] | 828 | /* E-MAC Status Register clear */ |
Yoshihiro Shimoda | 4a55530 | 2011-03-07 21:59:26 +0000 | [diff] [blame] | 829 | sh_eth_write(ndev, mdp->cd->ecsr_value, ECSR); |
Nobuhiro Iwamatsu | b0ca2a2 | 2008-06-30 11:08:17 +0900 | [diff] [blame] | 830 | |
| 831 | /* E-MAC Interrupt Enable register */ |
Yoshihiro Shimoda | 4a55530 | 2011-03-07 21:59:26 +0000 | [diff] [blame] | 832 | sh_eth_write(ndev, mdp->cd->ecsipr_value, ECSIPR); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 833 | |
| 834 | /* Set MAC address */ |
| 835 | update_mac_address(ndev); |
| 836 | |
| 837 | /* mask reset */ |
Yoshihiro Shimoda | 380af9e | 2009-05-24 23:54:21 +0000 | [diff] [blame] | 838 | if (mdp->cd->apr) |
Yoshihiro Shimoda | 4a55530 | 2011-03-07 21:59:26 +0000 | [diff] [blame] | 839 | sh_eth_write(ndev, APR_AP, APR); |
Yoshihiro Shimoda | 380af9e | 2009-05-24 23:54:21 +0000 | [diff] [blame] | 840 | if (mdp->cd->mpr) |
Yoshihiro Shimoda | 4a55530 | 2011-03-07 21:59:26 +0000 | [diff] [blame] | 841 | sh_eth_write(ndev, MPR_MP, MPR); |
Yoshihiro Shimoda | 380af9e | 2009-05-24 23:54:21 +0000 | [diff] [blame] | 842 | if (mdp->cd->tpauser) |
Yoshihiro Shimoda | 4a55530 | 2011-03-07 21:59:26 +0000 | [diff] [blame] | 843 | sh_eth_write(ndev, TPAUSER_UNLIMITED, TPAUSER); |
Nobuhiro Iwamatsu | b0ca2a2 | 2008-06-30 11:08:17 +0900 | [diff] [blame] | 844 | |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 845 | /* Setting the Rx mode will start the Rx process. */ |
Yoshihiro Shimoda | 4a55530 | 2011-03-07 21:59:26 +0000 | [diff] [blame] | 846 | sh_eth_write(ndev, EDRRR_R, EDRRR); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 847 | |
| 848 | netif_start_queue(ndev); |
| 849 | |
| 850 | return ret; |
| 851 | } |
| 852 | |
| 853 | /* free Tx skb function */ |
| 854 | static int sh_eth_txfree(struct net_device *ndev) |
| 855 | { |
| 856 | struct sh_eth_private *mdp = netdev_priv(ndev); |
| 857 | struct sh_eth_txdesc *txdesc; |
| 858 | int freeNum = 0; |
| 859 | int entry = 0; |
| 860 | |
| 861 | for (; mdp->cur_tx - mdp->dirty_tx > 0; mdp->dirty_tx++) { |
| 862 | entry = mdp->dirty_tx % TX_RING_SIZE; |
| 863 | txdesc = &mdp->tx_ring[entry]; |
Yoshinori Sato | 71557a3 | 2008-08-06 19:49:00 -0400 | [diff] [blame] | 864 | if (txdesc->status & cpu_to_edmac(mdp, TD_TACT)) |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 865 | break; |
| 866 | /* Free the original skb. */ |
| 867 | if (mdp->tx_skbuff[entry]) { |
Yoshihiro Shimoda | 31fcb99 | 2011-06-30 22:52:13 +0000 | [diff] [blame] | 868 | dma_unmap_single(&ndev->dev, txdesc->addr, |
| 869 | txdesc->buffer_length, DMA_TO_DEVICE); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 870 | dev_kfree_skb_irq(mdp->tx_skbuff[entry]); |
| 871 | mdp->tx_skbuff[entry] = NULL; |
| 872 | freeNum++; |
| 873 | } |
Yoshinori Sato | 71557a3 | 2008-08-06 19:49:00 -0400 | [diff] [blame] | 874 | txdesc->status = cpu_to_edmac(mdp, TD_TFP); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 875 | if (entry >= TX_RING_SIZE - 1) |
Yoshinori Sato | 71557a3 | 2008-08-06 19:49:00 -0400 | [diff] [blame] | 876 | txdesc->status |= cpu_to_edmac(mdp, TD_TDLE); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 877 | |
| 878 | mdp->stats.tx_packets++; |
| 879 | mdp->stats.tx_bytes += txdesc->buffer_length; |
| 880 | } |
| 881 | return freeNum; |
| 882 | } |
| 883 | |
| 884 | /* Packet receive function */ |
| 885 | static int sh_eth_rx(struct net_device *ndev) |
| 886 | { |
| 887 | struct sh_eth_private *mdp = netdev_priv(ndev); |
| 888 | struct sh_eth_rxdesc *rxdesc; |
| 889 | |
| 890 | int entry = mdp->cur_rx % RX_RING_SIZE; |
| 891 | int boguscnt = (mdp->dirty_rx + RX_RING_SIZE) - mdp->cur_rx; |
| 892 | struct sk_buff *skb; |
| 893 | u16 pkt_len = 0; |
Yoshihiro Shimoda | 380af9e | 2009-05-24 23:54:21 +0000 | [diff] [blame] | 894 | u32 desc_status; |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 895 | |
| 896 | rxdesc = &mdp->rx_ring[entry]; |
Yoshinori Sato | 71557a3 | 2008-08-06 19:49:00 -0400 | [diff] [blame] | 897 | while (!(rxdesc->status & cpu_to_edmac(mdp, RD_RACT))) { |
| 898 | desc_status = edmac_to_cpu(mdp, rxdesc->status); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 899 | pkt_len = rxdesc->frame_length; |
| 900 | |
| 901 | if (--boguscnt < 0) |
| 902 | break; |
| 903 | |
| 904 | if (!(desc_status & RDFEND)) |
| 905 | mdp->stats.rx_length_errors++; |
| 906 | |
| 907 | if (desc_status & (RD_RFS1 | RD_RFS2 | RD_RFS3 | RD_RFS4 | |
| 908 | RD_RFS5 | RD_RFS6 | RD_RFS10)) { |
| 909 | mdp->stats.rx_errors++; |
| 910 | if (desc_status & RD_RFS1) |
| 911 | mdp->stats.rx_crc_errors++; |
| 912 | if (desc_status & RD_RFS2) |
| 913 | mdp->stats.rx_frame_errors++; |
| 914 | if (desc_status & RD_RFS3) |
| 915 | mdp->stats.rx_length_errors++; |
| 916 | if (desc_status & RD_RFS4) |
| 917 | mdp->stats.rx_length_errors++; |
| 918 | if (desc_status & RD_RFS6) |
| 919 | mdp->stats.rx_missed_errors++; |
| 920 | if (desc_status & RD_RFS10) |
| 921 | mdp->stats.rx_over_errors++; |
| 922 | } else { |
Yoshihiro Shimoda | 380af9e | 2009-05-24 23:54:21 +0000 | [diff] [blame] | 923 | if (!mdp->cd->hw_swap) |
| 924 | sh_eth_soft_swap( |
| 925 | phys_to_virt(ALIGN(rxdesc->addr, 4)), |
| 926 | pkt_len + 2); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 927 | skb = mdp->rx_skbuff[entry]; |
| 928 | mdp->rx_skbuff[entry] = NULL; |
Magnus Damm | 503914c | 2009-12-15 21:16:55 -0800 | [diff] [blame] | 929 | if (mdp->cd->rpadir) |
| 930 | skb_reserve(skb, NET_IP_ALIGN); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 931 | skb_put(skb, pkt_len); |
| 932 | skb->protocol = eth_type_trans(skb, ndev); |
| 933 | netif_rx(skb); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 934 | mdp->stats.rx_packets++; |
| 935 | mdp->stats.rx_bytes += pkt_len; |
| 936 | } |
Yoshinori Sato | 71557a3 | 2008-08-06 19:49:00 -0400 | [diff] [blame] | 937 | rxdesc->status |= cpu_to_edmac(mdp, RD_RACT); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 938 | entry = (++mdp->cur_rx) % RX_RING_SIZE; |
Yoshihiro Shimoda | 862df49 | 2009-05-24 23:53:40 +0000 | [diff] [blame] | 939 | rxdesc = &mdp->rx_ring[entry]; |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 940 | } |
| 941 | |
| 942 | /* Refill the Rx ring buffers. */ |
| 943 | for (; mdp->cur_rx - mdp->dirty_rx > 0; mdp->dirty_rx++) { |
| 944 | entry = mdp->dirty_rx % RX_RING_SIZE; |
| 945 | rxdesc = &mdp->rx_ring[entry]; |
Nobuhiro Iwamatsu | b0ca2a2 | 2008-06-30 11:08:17 +0900 | [diff] [blame] | 946 | /* The size of the buffer is 16 byte boundary. */ |
Yoshihiro Shimoda | 0029d64 | 2009-05-24 23:53:20 +0000 | [diff] [blame] | 947 | rxdesc->buffer_length = ALIGN(mdp->rx_buf_sz, 16); |
Nobuhiro Iwamatsu | b0ca2a2 | 2008-06-30 11:08:17 +0900 | [diff] [blame] | 948 | |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 949 | if (mdp->rx_skbuff[entry] == NULL) { |
| 950 | skb = dev_alloc_skb(mdp->rx_buf_sz); |
| 951 | mdp->rx_skbuff[entry] = skb; |
| 952 | if (skb == NULL) |
| 953 | break; /* Better luck next round. */ |
Yoshihiro Shimoda | e88aae7 | 2009-05-24 23:52:35 +0000 | [diff] [blame] | 954 | dma_map_single(&ndev->dev, skb->tail, mdp->rx_buf_sz, |
| 955 | DMA_FROM_DEVICE); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 956 | skb->dev = ndev; |
Yoshihiro Shimoda | 380af9e | 2009-05-24 23:54:21 +0000 | [diff] [blame] | 957 | sh_eth_set_receive_align(skb); |
| 958 | |
Eric Dumazet | bc8acf2 | 2010-09-02 13:07:41 -0700 | [diff] [blame] | 959 | skb_checksum_none_assert(skb); |
Yoshihiro Shimoda | 0029d64 | 2009-05-24 23:53:20 +0000 | [diff] [blame] | 960 | rxdesc->addr = virt_to_phys(PTR_ALIGN(skb->data, 4)); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 961 | } |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 962 | if (entry >= RX_RING_SIZE - 1) |
| 963 | rxdesc->status |= |
Yoshinori Sato | 71557a3 | 2008-08-06 19:49:00 -0400 | [diff] [blame] | 964 | cpu_to_edmac(mdp, RD_RACT | RD_RFP | RD_RDEL); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 965 | else |
| 966 | rxdesc->status |= |
Yoshinori Sato | 71557a3 | 2008-08-06 19:49:00 -0400 | [diff] [blame] | 967 | cpu_to_edmac(mdp, RD_RACT | RD_RFP); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 968 | } |
| 969 | |
| 970 | /* Restart Rx engine if stopped. */ |
| 971 | /* If we don't need to check status, don't. -KDU */ |
Yoshihiro Shimoda | 4a55530 | 2011-03-07 21:59:26 +0000 | [diff] [blame] | 972 | if (!(sh_eth_read(ndev, EDRRR) & EDRRR_R)) |
| 973 | sh_eth_write(ndev, EDRRR_R, EDRRR); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 974 | |
| 975 | return 0; |
| 976 | } |
| 977 | |
Yoshihiro Shimoda | 4a55530 | 2011-03-07 21:59:26 +0000 | [diff] [blame] | 978 | static void sh_eth_rcv_snd_disable(struct net_device *ndev) |
Nobuhiro Iwamatsu | dc19e4e | 2011-02-15 21:17:32 +0000 | [diff] [blame] | 979 | { |
| 980 | /* disable tx and rx */ |
Yoshihiro Shimoda | 4a55530 | 2011-03-07 21:59:26 +0000 | [diff] [blame] | 981 | sh_eth_write(ndev, sh_eth_read(ndev, ECMR) & |
| 982 | ~(ECMR_RE | ECMR_TE), ECMR); |
Nobuhiro Iwamatsu | dc19e4e | 2011-02-15 21:17:32 +0000 | [diff] [blame] | 983 | } |
| 984 | |
Yoshihiro Shimoda | 4a55530 | 2011-03-07 21:59:26 +0000 | [diff] [blame] | 985 | static void sh_eth_rcv_snd_enable(struct net_device *ndev) |
Nobuhiro Iwamatsu | dc19e4e | 2011-02-15 21:17:32 +0000 | [diff] [blame] | 986 | { |
| 987 | /* enable tx and rx */ |
Yoshihiro Shimoda | 4a55530 | 2011-03-07 21:59:26 +0000 | [diff] [blame] | 988 | sh_eth_write(ndev, sh_eth_read(ndev, ECMR) | |
| 989 | (ECMR_RE | ECMR_TE), ECMR); |
Nobuhiro Iwamatsu | dc19e4e | 2011-02-15 21:17:32 +0000 | [diff] [blame] | 990 | } |
| 991 | |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 992 | /* error control function */ |
| 993 | static void sh_eth_error(struct net_device *ndev, int intr_status) |
| 994 | { |
| 995 | struct sh_eth_private *mdp = netdev_priv(ndev); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 996 | u32 felic_stat; |
Yoshihiro Shimoda | 380af9e | 2009-05-24 23:54:21 +0000 | [diff] [blame] | 997 | u32 link_stat; |
| 998 | u32 mask; |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 999 | |
| 1000 | if (intr_status & EESR_ECI) { |
Yoshihiro Shimoda | 4a55530 | 2011-03-07 21:59:26 +0000 | [diff] [blame] | 1001 | felic_stat = sh_eth_read(ndev, ECSR); |
| 1002 | sh_eth_write(ndev, felic_stat, ECSR); /* clear int */ |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1003 | if (felic_stat & ECSR_ICD) |
| 1004 | mdp->stats.tx_carrier_errors++; |
| 1005 | if (felic_stat & ECSR_LCHNG) { |
| 1006 | /* Link Changed */ |
Yoshihiro Shimoda | 4923576 | 2009-08-27 23:25:03 +0000 | [diff] [blame] | 1007 | if (mdp->cd->no_psr || mdp->no_ether_link) { |
Yoshihiro Shimoda | 380af9e | 2009-05-24 23:54:21 +0000 | [diff] [blame] | 1008 | if (mdp->link == PHY_DOWN) |
| 1009 | link_stat = 0; |
| 1010 | else |
| 1011 | link_stat = PHY_ST_LINK; |
| 1012 | } else { |
Yoshihiro Shimoda | 4a55530 | 2011-03-07 21:59:26 +0000 | [diff] [blame] | 1013 | link_stat = (sh_eth_read(ndev, PSR)); |
Yoshihiro Shimoda | 4923576 | 2009-08-27 23:25:03 +0000 | [diff] [blame] | 1014 | if (mdp->ether_link_active_low) |
| 1015 | link_stat = ~link_stat; |
Yoshihiro Shimoda | 380af9e | 2009-05-24 23:54:21 +0000 | [diff] [blame] | 1016 | } |
Nobuhiro Iwamatsu | dc19e4e | 2011-02-15 21:17:32 +0000 | [diff] [blame] | 1017 | if (!(link_stat & PHY_ST_LINK)) |
Yoshihiro Shimoda | 4a55530 | 2011-03-07 21:59:26 +0000 | [diff] [blame] | 1018 | sh_eth_rcv_snd_disable(ndev); |
Nobuhiro Iwamatsu | dc19e4e | 2011-02-15 21:17:32 +0000 | [diff] [blame] | 1019 | else { |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1020 | /* Link Up */ |
Yoshihiro Shimoda | 4a55530 | 2011-03-07 21:59:26 +0000 | [diff] [blame] | 1021 | sh_eth_write(ndev, sh_eth_read(ndev, EESIPR) & |
| 1022 | ~DMAC_M_ECI, EESIPR); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1023 | /*clear int */ |
Yoshihiro Shimoda | 4a55530 | 2011-03-07 21:59:26 +0000 | [diff] [blame] | 1024 | sh_eth_write(ndev, sh_eth_read(ndev, ECSR), |
| 1025 | ECSR); |
| 1026 | sh_eth_write(ndev, sh_eth_read(ndev, EESIPR) | |
| 1027 | DMAC_M_ECI, EESIPR); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1028 | /* enable tx and rx */ |
Yoshihiro Shimoda | 4a55530 | 2011-03-07 21:59:26 +0000 | [diff] [blame] | 1029 | sh_eth_rcv_snd_enable(ndev); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1030 | } |
| 1031 | } |
| 1032 | } |
| 1033 | |
| 1034 | if (intr_status & EESR_TWB) { |
| 1035 | /* Write buck end. unused write back interrupt */ |
| 1036 | if (intr_status & EESR_TABT) /* Transmit Abort int */ |
| 1037 | mdp->stats.tx_aborted_errors++; |
Nobuhiro Iwamatsu | dc19e4e | 2011-02-15 21:17:32 +0000 | [diff] [blame] | 1038 | if (netif_msg_tx_err(mdp)) |
| 1039 | dev_err(&ndev->dev, "Transmit Abort\n"); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1040 | } |
| 1041 | |
| 1042 | if (intr_status & EESR_RABT) { |
| 1043 | /* Receive Abort int */ |
| 1044 | if (intr_status & EESR_RFRMER) { |
| 1045 | /* Receive Frame Overflow int */ |
| 1046 | mdp->stats.rx_frame_errors++; |
Nobuhiro Iwamatsu | dc19e4e | 2011-02-15 21:17:32 +0000 | [diff] [blame] | 1047 | if (netif_msg_rx_err(mdp)) |
| 1048 | dev_err(&ndev->dev, "Receive Abort\n"); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1049 | } |
| 1050 | } |
Yoshihiro Shimoda | 380af9e | 2009-05-24 23:54:21 +0000 | [diff] [blame] | 1051 | |
Nobuhiro Iwamatsu | dc19e4e | 2011-02-15 21:17:32 +0000 | [diff] [blame] | 1052 | if (intr_status & EESR_TDE) { |
| 1053 | /* Transmit Descriptor Empty int */ |
| 1054 | mdp->stats.tx_fifo_errors++; |
| 1055 | if (netif_msg_tx_err(mdp)) |
| 1056 | dev_err(&ndev->dev, "Transmit Descriptor Empty\n"); |
| 1057 | } |
| 1058 | |
| 1059 | if (intr_status & EESR_TFE) { |
| 1060 | /* FIFO under flow */ |
| 1061 | mdp->stats.tx_fifo_errors++; |
| 1062 | if (netif_msg_tx_err(mdp)) |
| 1063 | dev_err(&ndev->dev, "Transmit FIFO Under flow\n"); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1064 | } |
| 1065 | |
| 1066 | if (intr_status & EESR_RDE) { |
| 1067 | /* Receive Descriptor Empty int */ |
| 1068 | mdp->stats.rx_over_errors++; |
| 1069 | |
Yoshihiro Shimoda | 4a55530 | 2011-03-07 21:59:26 +0000 | [diff] [blame] | 1070 | if (sh_eth_read(ndev, EDRRR) ^ EDRRR_R) |
| 1071 | sh_eth_write(ndev, EDRRR_R, EDRRR); |
Nobuhiro Iwamatsu | dc19e4e | 2011-02-15 21:17:32 +0000 | [diff] [blame] | 1072 | if (netif_msg_rx_err(mdp)) |
| 1073 | dev_err(&ndev->dev, "Receive Descriptor Empty\n"); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1074 | } |
Nobuhiro Iwamatsu | dc19e4e | 2011-02-15 21:17:32 +0000 | [diff] [blame] | 1075 | |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1076 | if (intr_status & EESR_RFE) { |
| 1077 | /* Receive FIFO Overflow int */ |
| 1078 | mdp->stats.rx_fifo_errors++; |
Nobuhiro Iwamatsu | dc19e4e | 2011-02-15 21:17:32 +0000 | [diff] [blame] | 1079 | if (netif_msg_rx_err(mdp)) |
| 1080 | dev_err(&ndev->dev, "Receive FIFO Overflow\n"); |
| 1081 | } |
| 1082 | |
| 1083 | if (!mdp->cd->no_ade && (intr_status & EESR_ADE)) { |
| 1084 | /* Address Error */ |
| 1085 | mdp->stats.tx_fifo_errors++; |
| 1086 | if (netif_msg_tx_err(mdp)) |
| 1087 | dev_err(&ndev->dev, "Address Error\n"); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1088 | } |
Yoshihiro Shimoda | 380af9e | 2009-05-24 23:54:21 +0000 | [diff] [blame] | 1089 | |
| 1090 | mask = EESR_TWB | EESR_TABT | EESR_ADE | EESR_TDE | EESR_TFE; |
| 1091 | if (mdp->cd->no_ade) |
| 1092 | mask &= ~EESR_ADE; |
| 1093 | if (intr_status & mask) { |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1094 | /* Tx error */ |
Yoshihiro Shimoda | 4a55530 | 2011-03-07 21:59:26 +0000 | [diff] [blame] | 1095 | u32 edtrr = sh_eth_read(ndev, EDTRR); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1096 | /* dmesg */ |
Yoshihiro Shimoda | 380af9e | 2009-05-24 23:54:21 +0000 | [diff] [blame] | 1097 | dev_err(&ndev->dev, "TX error. status=%8.8x cur_tx=%8.8x ", |
| 1098 | intr_status, mdp->cur_tx); |
| 1099 | dev_err(&ndev->dev, "dirty_tx=%8.8x state=%8.8x EDTRR=%8.8x.\n", |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1100 | mdp->dirty_tx, (u32) ndev->state, edtrr); |
| 1101 | /* dirty buffer free */ |
| 1102 | sh_eth_txfree(ndev); |
| 1103 | |
| 1104 | /* SH7712 BUG */ |
Yoshihiro Shimoda | c5ed536 | 2011-03-07 21:59:38 +0000 | [diff] [blame] | 1105 | if (edtrr ^ sh_eth_get_edtrr_trns(mdp)) { |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1106 | /* tx dma start */ |
Yoshihiro Shimoda | c5ed536 | 2011-03-07 21:59:38 +0000 | [diff] [blame] | 1107 | sh_eth_write(ndev, sh_eth_get_edtrr_trns(mdp), EDTRR); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1108 | } |
| 1109 | /* wakeup */ |
| 1110 | netif_wake_queue(ndev); |
| 1111 | } |
| 1112 | } |
| 1113 | |
| 1114 | static irqreturn_t sh_eth_interrupt(int irq, void *netdev) |
| 1115 | { |
| 1116 | struct net_device *ndev = netdev; |
| 1117 | struct sh_eth_private *mdp = netdev_priv(ndev); |
Yoshihiro Shimoda | 380af9e | 2009-05-24 23:54:21 +0000 | [diff] [blame] | 1118 | struct sh_eth_cpu_data *cd = mdp->cd; |
Nobuhiro Iwamatsu | 0e0fde3 | 2009-03-16 19:50:57 +0000 | [diff] [blame] | 1119 | irqreturn_t ret = IRQ_NONE; |
Yoshihiro Shimoda | 4a55530 | 2011-03-07 21:59:26 +0000 | [diff] [blame] | 1120 | u32 intr_status = 0; |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1121 | |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1122 | spin_lock(&mdp->lock); |
| 1123 | |
Nobuhiro Iwamatsu | b0ca2a2 | 2008-06-30 11:08:17 +0900 | [diff] [blame] | 1124 | /* Get interrpt stat */ |
Yoshihiro Shimoda | 4a55530 | 2011-03-07 21:59:26 +0000 | [diff] [blame] | 1125 | intr_status = sh_eth_read(ndev, EESR); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1126 | /* Clear interrupt */ |
Nobuhiro Iwamatsu | 0e0fde3 | 2009-03-16 19:50:57 +0000 | [diff] [blame] | 1127 | if (intr_status & (EESR_FRC | EESR_RMAF | EESR_RRF | |
| 1128 | EESR_RTLF | EESR_RTSF | EESR_PRE | EESR_CERF | |
Yoshihiro Shimoda | 380af9e | 2009-05-24 23:54:21 +0000 | [diff] [blame] | 1129 | cd->tx_check | cd->eesr_err_check)) { |
Yoshihiro Shimoda | 4a55530 | 2011-03-07 21:59:26 +0000 | [diff] [blame] | 1130 | sh_eth_write(ndev, intr_status, EESR); |
Nobuhiro Iwamatsu | 0e0fde3 | 2009-03-16 19:50:57 +0000 | [diff] [blame] | 1131 | ret = IRQ_HANDLED; |
| 1132 | } else |
| 1133 | goto other_irq; |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1134 | |
Nobuhiro Iwamatsu | b0ca2a2 | 2008-06-30 11:08:17 +0900 | [diff] [blame] | 1135 | if (intr_status & (EESR_FRC | /* Frame recv*/ |
| 1136 | EESR_RMAF | /* Multi cast address recv*/ |
| 1137 | EESR_RRF | /* Bit frame recv */ |
| 1138 | EESR_RTLF | /* Long frame recv*/ |
| 1139 | EESR_RTSF | /* short frame recv */ |
| 1140 | EESR_PRE | /* PHY-LSI recv error */ |
| 1141 | EESR_CERF)){ /* recv frame CRC error */ |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1142 | sh_eth_rx(ndev); |
Nobuhiro Iwamatsu | b0ca2a2 | 2008-06-30 11:08:17 +0900 | [diff] [blame] | 1143 | } |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1144 | |
Nobuhiro Iwamatsu | b0ca2a2 | 2008-06-30 11:08:17 +0900 | [diff] [blame] | 1145 | /* Tx Check */ |
Yoshihiro Shimoda | 380af9e | 2009-05-24 23:54:21 +0000 | [diff] [blame] | 1146 | if (intr_status & cd->tx_check) { |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1147 | sh_eth_txfree(ndev); |
| 1148 | netif_wake_queue(ndev); |
| 1149 | } |
| 1150 | |
Yoshihiro Shimoda | 380af9e | 2009-05-24 23:54:21 +0000 | [diff] [blame] | 1151 | if (intr_status & cd->eesr_err_check) |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1152 | sh_eth_error(ndev, intr_status); |
| 1153 | |
Nobuhiro Iwamatsu | 0e0fde3 | 2009-03-16 19:50:57 +0000 | [diff] [blame] | 1154 | other_irq: |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1155 | spin_unlock(&mdp->lock); |
| 1156 | |
Nobuhiro Iwamatsu | 0e0fde3 | 2009-03-16 19:50:57 +0000 | [diff] [blame] | 1157 | return ret; |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1158 | } |
| 1159 | |
| 1160 | static void sh_eth_timer(unsigned long data) |
| 1161 | { |
| 1162 | struct net_device *ndev = (struct net_device *)data; |
| 1163 | struct sh_eth_private *mdp = netdev_priv(ndev); |
| 1164 | |
| 1165 | mod_timer(&mdp->timer, jiffies + (10 * HZ)); |
| 1166 | } |
| 1167 | |
| 1168 | /* PHY state control function */ |
| 1169 | static void sh_eth_adjust_link(struct net_device *ndev) |
| 1170 | { |
| 1171 | struct sh_eth_private *mdp = netdev_priv(ndev); |
| 1172 | struct phy_device *phydev = mdp->phydev; |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1173 | int new_state = 0; |
| 1174 | |
| 1175 | if (phydev->link != PHY_DOWN) { |
| 1176 | if (phydev->duplex != mdp->duplex) { |
| 1177 | new_state = 1; |
| 1178 | mdp->duplex = phydev->duplex; |
Yoshihiro Shimoda | 380af9e | 2009-05-24 23:54:21 +0000 | [diff] [blame] | 1179 | if (mdp->cd->set_duplex) |
| 1180 | mdp->cd->set_duplex(ndev); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1181 | } |
| 1182 | |
| 1183 | if (phydev->speed != mdp->speed) { |
| 1184 | new_state = 1; |
| 1185 | mdp->speed = phydev->speed; |
Yoshihiro Shimoda | 380af9e | 2009-05-24 23:54:21 +0000 | [diff] [blame] | 1186 | if (mdp->cd->set_rate) |
| 1187 | mdp->cd->set_rate(ndev); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1188 | } |
| 1189 | if (mdp->link == PHY_DOWN) { |
Yoshihiro Shimoda | 91a5615 | 2011-07-05 20:33:51 +0000 | [diff] [blame] | 1190 | sh_eth_write(ndev, |
| 1191 | (sh_eth_read(ndev, ECMR) & ~ECMR_TXF), ECMR); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1192 | new_state = 1; |
| 1193 | mdp->link = phydev->link; |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1194 | } |
| 1195 | } else if (mdp->link) { |
| 1196 | new_state = 1; |
| 1197 | mdp->link = PHY_DOWN; |
| 1198 | mdp->speed = 0; |
| 1199 | mdp->duplex = -1; |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1200 | } |
| 1201 | |
Nobuhiro Iwamatsu | dc19e4e | 2011-02-15 21:17:32 +0000 | [diff] [blame] | 1202 | if (new_state && netif_msg_link(mdp)) |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1203 | phy_print_status(phydev); |
| 1204 | } |
| 1205 | |
| 1206 | /* PHY init function */ |
| 1207 | static int sh_eth_phy_init(struct net_device *ndev) |
| 1208 | { |
| 1209 | struct sh_eth_private *mdp = netdev_priv(ndev); |
David S. Miller | 0a372eb | 2009-05-26 21:11:09 -0700 | [diff] [blame] | 1210 | char phy_id[MII_BUS_ID_SIZE + 3]; |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1211 | struct phy_device *phydev = NULL; |
| 1212 | |
Kay Sievers | fb28ad3 | 2008-11-10 13:55:14 -0800 | [diff] [blame] | 1213 | snprintf(phy_id, sizeof(phy_id), PHY_ID_FMT, |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1214 | mdp->mii_bus->id , mdp->phy_id); |
| 1215 | |
| 1216 | mdp->link = PHY_DOWN; |
| 1217 | mdp->speed = 0; |
| 1218 | mdp->duplex = -1; |
| 1219 | |
| 1220 | /* Try connect to PHY */ |
Joe Perches | c061b18 | 2010-08-23 18:20:03 +0000 | [diff] [blame] | 1221 | phydev = phy_connect(ndev, phy_id, sh_eth_adjust_link, |
Yoshihiro Shimoda | e47c905 | 2011-03-07 21:59:45 +0000 | [diff] [blame] | 1222 | 0, mdp->phy_interface); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1223 | if (IS_ERR(phydev)) { |
| 1224 | dev_err(&ndev->dev, "phy_connect failed\n"); |
| 1225 | return PTR_ERR(phydev); |
| 1226 | } |
Yoshihiro Shimoda | 380af9e | 2009-05-24 23:54:21 +0000 | [diff] [blame] | 1227 | |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1228 | dev_info(&ndev->dev, "attached phy %i to driver %s\n", |
Yoshihiro Shimoda | 380af9e | 2009-05-24 23:54:21 +0000 | [diff] [blame] | 1229 | phydev->addr, phydev->drv->name); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1230 | |
| 1231 | mdp->phydev = phydev; |
| 1232 | |
| 1233 | return 0; |
| 1234 | } |
| 1235 | |
| 1236 | /* PHY control start function */ |
| 1237 | static int sh_eth_phy_start(struct net_device *ndev) |
| 1238 | { |
| 1239 | struct sh_eth_private *mdp = netdev_priv(ndev); |
| 1240 | int ret; |
| 1241 | |
| 1242 | ret = sh_eth_phy_init(ndev); |
| 1243 | if (ret) |
| 1244 | return ret; |
| 1245 | |
| 1246 | /* reset phy - this also wakes it from PDOWN */ |
| 1247 | phy_write(mdp->phydev, MII_BMCR, BMCR_RESET); |
| 1248 | phy_start(mdp->phydev); |
| 1249 | |
| 1250 | return 0; |
| 1251 | } |
| 1252 | |
Nobuhiro Iwamatsu | dc19e4e | 2011-02-15 21:17:32 +0000 | [diff] [blame] | 1253 | static int sh_eth_get_settings(struct net_device *ndev, |
| 1254 | struct ethtool_cmd *ecmd) |
| 1255 | { |
| 1256 | struct sh_eth_private *mdp = netdev_priv(ndev); |
| 1257 | unsigned long flags; |
| 1258 | int ret; |
| 1259 | |
| 1260 | spin_lock_irqsave(&mdp->lock, flags); |
| 1261 | ret = phy_ethtool_gset(mdp->phydev, ecmd); |
| 1262 | spin_unlock_irqrestore(&mdp->lock, flags); |
| 1263 | |
| 1264 | return ret; |
| 1265 | } |
| 1266 | |
| 1267 | static int sh_eth_set_settings(struct net_device *ndev, |
| 1268 | struct ethtool_cmd *ecmd) |
| 1269 | { |
| 1270 | struct sh_eth_private *mdp = netdev_priv(ndev); |
| 1271 | unsigned long flags; |
| 1272 | int ret; |
Nobuhiro Iwamatsu | dc19e4e | 2011-02-15 21:17:32 +0000 | [diff] [blame] | 1273 | |
| 1274 | spin_lock_irqsave(&mdp->lock, flags); |
| 1275 | |
| 1276 | /* disable tx and rx */ |
Yoshihiro Shimoda | 4a55530 | 2011-03-07 21:59:26 +0000 | [diff] [blame] | 1277 | sh_eth_rcv_snd_disable(ndev); |
Nobuhiro Iwamatsu | dc19e4e | 2011-02-15 21:17:32 +0000 | [diff] [blame] | 1278 | |
| 1279 | ret = phy_ethtool_sset(mdp->phydev, ecmd); |
| 1280 | if (ret) |
| 1281 | goto error_exit; |
| 1282 | |
| 1283 | if (ecmd->duplex == DUPLEX_FULL) |
| 1284 | mdp->duplex = 1; |
| 1285 | else |
| 1286 | mdp->duplex = 0; |
| 1287 | |
| 1288 | if (mdp->cd->set_duplex) |
| 1289 | mdp->cd->set_duplex(ndev); |
| 1290 | |
| 1291 | error_exit: |
| 1292 | mdelay(1); |
| 1293 | |
| 1294 | /* enable tx and rx */ |
Yoshihiro Shimoda | 4a55530 | 2011-03-07 21:59:26 +0000 | [diff] [blame] | 1295 | sh_eth_rcv_snd_enable(ndev); |
Nobuhiro Iwamatsu | dc19e4e | 2011-02-15 21:17:32 +0000 | [diff] [blame] | 1296 | |
| 1297 | spin_unlock_irqrestore(&mdp->lock, flags); |
| 1298 | |
| 1299 | return ret; |
| 1300 | } |
| 1301 | |
| 1302 | static int sh_eth_nway_reset(struct net_device *ndev) |
| 1303 | { |
| 1304 | struct sh_eth_private *mdp = netdev_priv(ndev); |
| 1305 | unsigned long flags; |
| 1306 | int ret; |
| 1307 | |
| 1308 | spin_lock_irqsave(&mdp->lock, flags); |
| 1309 | ret = phy_start_aneg(mdp->phydev); |
| 1310 | spin_unlock_irqrestore(&mdp->lock, flags); |
| 1311 | |
| 1312 | return ret; |
| 1313 | } |
| 1314 | |
| 1315 | static u32 sh_eth_get_msglevel(struct net_device *ndev) |
| 1316 | { |
| 1317 | struct sh_eth_private *mdp = netdev_priv(ndev); |
| 1318 | return mdp->msg_enable; |
| 1319 | } |
| 1320 | |
| 1321 | static void sh_eth_set_msglevel(struct net_device *ndev, u32 value) |
| 1322 | { |
| 1323 | struct sh_eth_private *mdp = netdev_priv(ndev); |
| 1324 | mdp->msg_enable = value; |
| 1325 | } |
| 1326 | |
| 1327 | static const char sh_eth_gstrings_stats[][ETH_GSTRING_LEN] = { |
| 1328 | "rx_current", "tx_current", |
| 1329 | "rx_dirty", "tx_dirty", |
| 1330 | }; |
| 1331 | #define SH_ETH_STATS_LEN ARRAY_SIZE(sh_eth_gstrings_stats) |
| 1332 | |
| 1333 | static int sh_eth_get_sset_count(struct net_device *netdev, int sset) |
| 1334 | { |
| 1335 | switch (sset) { |
| 1336 | case ETH_SS_STATS: |
| 1337 | return SH_ETH_STATS_LEN; |
| 1338 | default: |
| 1339 | return -EOPNOTSUPP; |
| 1340 | } |
| 1341 | } |
| 1342 | |
| 1343 | static void sh_eth_get_ethtool_stats(struct net_device *ndev, |
| 1344 | struct ethtool_stats *stats, u64 *data) |
| 1345 | { |
| 1346 | struct sh_eth_private *mdp = netdev_priv(ndev); |
| 1347 | int i = 0; |
| 1348 | |
| 1349 | /* device-specific stats */ |
| 1350 | data[i++] = mdp->cur_rx; |
| 1351 | data[i++] = mdp->cur_tx; |
| 1352 | data[i++] = mdp->dirty_rx; |
| 1353 | data[i++] = mdp->dirty_tx; |
| 1354 | } |
| 1355 | |
| 1356 | static void sh_eth_get_strings(struct net_device *ndev, u32 stringset, u8 *data) |
| 1357 | { |
| 1358 | switch (stringset) { |
| 1359 | case ETH_SS_STATS: |
| 1360 | memcpy(data, *sh_eth_gstrings_stats, |
| 1361 | sizeof(sh_eth_gstrings_stats)); |
| 1362 | break; |
| 1363 | } |
| 1364 | } |
| 1365 | |
| 1366 | static struct ethtool_ops sh_eth_ethtool_ops = { |
| 1367 | .get_settings = sh_eth_get_settings, |
| 1368 | .set_settings = sh_eth_set_settings, |
| 1369 | .nway_reset = sh_eth_nway_reset, |
| 1370 | .get_msglevel = sh_eth_get_msglevel, |
| 1371 | .set_msglevel = sh_eth_set_msglevel, |
| 1372 | .get_link = ethtool_op_get_link, |
| 1373 | .get_strings = sh_eth_get_strings, |
| 1374 | .get_ethtool_stats = sh_eth_get_ethtool_stats, |
| 1375 | .get_sset_count = sh_eth_get_sset_count, |
| 1376 | }; |
| 1377 | |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1378 | /* network device open function */ |
| 1379 | static int sh_eth_open(struct net_device *ndev) |
| 1380 | { |
| 1381 | int ret = 0; |
| 1382 | struct sh_eth_private *mdp = netdev_priv(ndev); |
| 1383 | |
Magnus Damm | bcd5149 | 2009-10-09 00:20:04 +0000 | [diff] [blame] | 1384 | pm_runtime_get_sync(&mdp->pdev->dev); |
| 1385 | |
Joe Perches | a0607fd | 2009-11-18 23:29:17 -0800 | [diff] [blame] | 1386 | ret = request_irq(ndev->irq, sh_eth_interrupt, |
Yoshihiro Shimoda | f29a3d0 | 2010-07-05 18:32:50 +0000 | [diff] [blame] | 1387 | #if defined(CONFIG_CPU_SUBTYPE_SH7763) || \ |
Nobuhiro Iwamatsu | dc19e4e | 2011-02-15 21:17:32 +0000 | [diff] [blame] | 1388 | defined(CONFIG_CPU_SUBTYPE_SH7764) || \ |
| 1389 | defined(CONFIG_CPU_SUBTYPE_SH7757) |
Nobuhiro Iwamatsu | 0e0fde3 | 2009-03-16 19:50:57 +0000 | [diff] [blame] | 1390 | IRQF_SHARED, |
| 1391 | #else |
| 1392 | 0, |
| 1393 | #endif |
| 1394 | ndev->name, ndev); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1395 | if (ret) { |
Yoshihiro Shimoda | 380af9e | 2009-05-24 23:54:21 +0000 | [diff] [blame] | 1396 | dev_err(&ndev->dev, "Can not assign IRQ number\n"); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1397 | return ret; |
| 1398 | } |
| 1399 | |
| 1400 | /* Descriptor set */ |
| 1401 | ret = sh_eth_ring_init(ndev); |
| 1402 | if (ret) |
| 1403 | goto out_free_irq; |
| 1404 | |
| 1405 | /* device init */ |
| 1406 | ret = sh_eth_dev_init(ndev); |
| 1407 | if (ret) |
| 1408 | goto out_free_irq; |
| 1409 | |
| 1410 | /* PHY control start*/ |
| 1411 | ret = sh_eth_phy_start(ndev); |
| 1412 | if (ret) |
| 1413 | goto out_free_irq; |
| 1414 | |
| 1415 | /* Set the timer to check for link beat. */ |
| 1416 | init_timer(&mdp->timer); |
| 1417 | mdp->timer.expires = (jiffies + (24 * HZ)) / 10;/* 2.4 sec. */ |
Nobuhiro Iwamatsu | b0ca2a2 | 2008-06-30 11:08:17 +0900 | [diff] [blame] | 1418 | setup_timer(&mdp->timer, sh_eth_timer, (unsigned long)ndev); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1419 | |
| 1420 | return ret; |
| 1421 | |
| 1422 | out_free_irq: |
| 1423 | free_irq(ndev->irq, ndev); |
Magnus Damm | bcd5149 | 2009-10-09 00:20:04 +0000 | [diff] [blame] | 1424 | pm_runtime_put_sync(&mdp->pdev->dev); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1425 | return ret; |
| 1426 | } |
| 1427 | |
| 1428 | /* Timeout function */ |
| 1429 | static void sh_eth_tx_timeout(struct net_device *ndev) |
| 1430 | { |
| 1431 | struct sh_eth_private *mdp = netdev_priv(ndev); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1432 | struct sh_eth_rxdesc *rxdesc; |
| 1433 | int i; |
| 1434 | |
| 1435 | netif_stop_queue(ndev); |
| 1436 | |
Nobuhiro Iwamatsu | dc19e4e | 2011-02-15 21:17:32 +0000 | [diff] [blame] | 1437 | if (netif_msg_timer(mdp)) |
| 1438 | dev_err(&ndev->dev, "%s: transmit timed out, status %8.8x," |
Yoshihiro Shimoda | 4a55530 | 2011-03-07 21:59:26 +0000 | [diff] [blame] | 1439 | " resetting...\n", ndev->name, (int)sh_eth_read(ndev, EESR)); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1440 | |
| 1441 | /* tx_errors count up */ |
| 1442 | mdp->stats.tx_errors++; |
| 1443 | |
| 1444 | /* timer off */ |
| 1445 | del_timer_sync(&mdp->timer); |
| 1446 | |
| 1447 | /* Free all the skbuffs in the Rx queue. */ |
| 1448 | for (i = 0; i < RX_RING_SIZE; i++) { |
| 1449 | rxdesc = &mdp->rx_ring[i]; |
| 1450 | rxdesc->status = 0; |
| 1451 | rxdesc->addr = 0xBADF00D0; |
| 1452 | if (mdp->rx_skbuff[i]) |
| 1453 | dev_kfree_skb(mdp->rx_skbuff[i]); |
| 1454 | mdp->rx_skbuff[i] = NULL; |
| 1455 | } |
| 1456 | for (i = 0; i < TX_RING_SIZE; i++) { |
| 1457 | if (mdp->tx_skbuff[i]) |
| 1458 | dev_kfree_skb(mdp->tx_skbuff[i]); |
| 1459 | mdp->tx_skbuff[i] = NULL; |
| 1460 | } |
| 1461 | |
| 1462 | /* device init */ |
| 1463 | sh_eth_dev_init(ndev); |
| 1464 | |
| 1465 | /* timer on */ |
| 1466 | mdp->timer.expires = (jiffies + (24 * HZ)) / 10;/* 2.4 sec. */ |
| 1467 | add_timer(&mdp->timer); |
| 1468 | } |
| 1469 | |
| 1470 | /* Packet transmit function */ |
| 1471 | static int sh_eth_start_xmit(struct sk_buff *skb, struct net_device *ndev) |
| 1472 | { |
| 1473 | struct sh_eth_private *mdp = netdev_priv(ndev); |
| 1474 | struct sh_eth_txdesc *txdesc; |
| 1475 | u32 entry; |
Nobuhiro Iwamatsu | fb5e2f9 | 2008-11-17 20:29:58 +0000 | [diff] [blame] | 1476 | unsigned long flags; |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1477 | |
| 1478 | spin_lock_irqsave(&mdp->lock, flags); |
| 1479 | if ((mdp->cur_tx - mdp->dirty_tx) >= (TX_RING_SIZE - 4)) { |
| 1480 | if (!sh_eth_txfree(ndev)) { |
Nobuhiro Iwamatsu | dc19e4e | 2011-02-15 21:17:32 +0000 | [diff] [blame] | 1481 | if (netif_msg_tx_queued(mdp)) |
| 1482 | dev_warn(&ndev->dev, "TxFD exhausted.\n"); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1483 | netif_stop_queue(ndev); |
| 1484 | spin_unlock_irqrestore(&mdp->lock, flags); |
Patrick McHardy | 5b54814 | 2009-06-12 06:22:29 +0000 | [diff] [blame] | 1485 | return NETDEV_TX_BUSY; |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1486 | } |
| 1487 | } |
| 1488 | spin_unlock_irqrestore(&mdp->lock, flags); |
| 1489 | |
| 1490 | entry = mdp->cur_tx % TX_RING_SIZE; |
| 1491 | mdp->tx_skbuff[entry] = skb; |
| 1492 | txdesc = &mdp->tx_ring[entry]; |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1493 | /* soft swap. */ |
Yoshihiro Shimoda | 380af9e | 2009-05-24 23:54:21 +0000 | [diff] [blame] | 1494 | if (!mdp->cd->hw_swap) |
| 1495 | sh_eth_soft_swap(phys_to_virt(ALIGN(txdesc->addr, 4)), |
| 1496 | skb->len + 2); |
Yoshihiro Shimoda | 31fcb99 | 2011-06-30 22:52:13 +0000 | [diff] [blame] | 1497 | txdesc->addr = dma_map_single(&ndev->dev, skb->data, skb->len, |
| 1498 | DMA_TO_DEVICE); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1499 | if (skb->len < ETHERSMALL) |
| 1500 | txdesc->buffer_length = ETHERSMALL; |
| 1501 | else |
| 1502 | txdesc->buffer_length = skb->len; |
| 1503 | |
| 1504 | if (entry >= TX_RING_SIZE - 1) |
Yoshinori Sato | 71557a3 | 2008-08-06 19:49:00 -0400 | [diff] [blame] | 1505 | txdesc->status |= cpu_to_edmac(mdp, TD_TACT | TD_TDLE); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1506 | else |
Yoshinori Sato | 71557a3 | 2008-08-06 19:49:00 -0400 | [diff] [blame] | 1507 | txdesc->status |= cpu_to_edmac(mdp, TD_TACT); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1508 | |
| 1509 | mdp->cur_tx++; |
| 1510 | |
Yoshihiro Shimoda | c5ed536 | 2011-03-07 21:59:38 +0000 | [diff] [blame] | 1511 | if (!(sh_eth_read(ndev, EDTRR) & sh_eth_get_edtrr_trns(mdp))) |
| 1512 | sh_eth_write(ndev, sh_eth_get_edtrr_trns(mdp), EDTRR); |
Nobuhiro Iwamatsu | b0ca2a2 | 2008-06-30 11:08:17 +0900 | [diff] [blame] | 1513 | |
Patrick McHardy | 6ed1065 | 2009-06-23 06:03:08 +0000 | [diff] [blame] | 1514 | return NETDEV_TX_OK; |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1515 | } |
| 1516 | |
| 1517 | /* device close function */ |
| 1518 | static int sh_eth_close(struct net_device *ndev) |
| 1519 | { |
| 1520 | struct sh_eth_private *mdp = netdev_priv(ndev); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1521 | int ringsize; |
| 1522 | |
| 1523 | netif_stop_queue(ndev); |
| 1524 | |
| 1525 | /* Disable interrupts by clearing the interrupt mask. */ |
Yoshihiro Shimoda | 4a55530 | 2011-03-07 21:59:26 +0000 | [diff] [blame] | 1526 | sh_eth_write(ndev, 0x0000, EESIPR); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1527 | |
| 1528 | /* Stop the chip's Tx and Rx processes. */ |
Yoshihiro Shimoda | 4a55530 | 2011-03-07 21:59:26 +0000 | [diff] [blame] | 1529 | sh_eth_write(ndev, 0, EDTRR); |
| 1530 | sh_eth_write(ndev, 0, EDRRR); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1531 | |
| 1532 | /* PHY Disconnect */ |
| 1533 | if (mdp->phydev) { |
| 1534 | phy_stop(mdp->phydev); |
| 1535 | phy_disconnect(mdp->phydev); |
| 1536 | } |
| 1537 | |
| 1538 | free_irq(ndev->irq, ndev); |
| 1539 | |
| 1540 | del_timer_sync(&mdp->timer); |
| 1541 | |
| 1542 | /* Free all the skbuffs in the Rx queue. */ |
| 1543 | sh_eth_ring_free(ndev); |
| 1544 | |
| 1545 | /* free DMA buffer */ |
| 1546 | ringsize = sizeof(struct sh_eth_rxdesc) * RX_RING_SIZE; |
| 1547 | dma_free_coherent(NULL, ringsize, mdp->rx_ring, mdp->rx_desc_dma); |
| 1548 | |
| 1549 | /* free DMA buffer */ |
| 1550 | ringsize = sizeof(struct sh_eth_txdesc) * TX_RING_SIZE; |
| 1551 | dma_free_coherent(NULL, ringsize, mdp->tx_ring, mdp->tx_desc_dma); |
| 1552 | |
Magnus Damm | bcd5149 | 2009-10-09 00:20:04 +0000 | [diff] [blame] | 1553 | pm_runtime_put_sync(&mdp->pdev->dev); |
| 1554 | |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1555 | return 0; |
| 1556 | } |
| 1557 | |
| 1558 | static struct net_device_stats *sh_eth_get_stats(struct net_device *ndev) |
| 1559 | { |
| 1560 | struct sh_eth_private *mdp = netdev_priv(ndev); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1561 | |
Magnus Damm | bcd5149 | 2009-10-09 00:20:04 +0000 | [diff] [blame] | 1562 | pm_runtime_get_sync(&mdp->pdev->dev); |
| 1563 | |
Yoshihiro Shimoda | 4a55530 | 2011-03-07 21:59:26 +0000 | [diff] [blame] | 1564 | mdp->stats.tx_dropped += sh_eth_read(ndev, TROCR); |
| 1565 | sh_eth_write(ndev, 0, TROCR); /* (write clear) */ |
| 1566 | mdp->stats.collisions += sh_eth_read(ndev, CDCR); |
| 1567 | sh_eth_write(ndev, 0, CDCR); /* (write clear) */ |
| 1568 | mdp->stats.tx_carrier_errors += sh_eth_read(ndev, LCCR); |
| 1569 | sh_eth_write(ndev, 0, LCCR); /* (write clear) */ |
Yoshihiro Shimoda | c5ed536 | 2011-03-07 21:59:38 +0000 | [diff] [blame] | 1570 | if (sh_eth_is_gether(mdp)) { |
| 1571 | mdp->stats.tx_carrier_errors += sh_eth_read(ndev, CERCR); |
| 1572 | sh_eth_write(ndev, 0, CERCR); /* (write clear) */ |
| 1573 | mdp->stats.tx_carrier_errors += sh_eth_read(ndev, CEECR); |
| 1574 | sh_eth_write(ndev, 0, CEECR); /* (write clear) */ |
| 1575 | } else { |
| 1576 | mdp->stats.tx_carrier_errors += sh_eth_read(ndev, CNDCR); |
| 1577 | sh_eth_write(ndev, 0, CNDCR); /* (write clear) */ |
| 1578 | } |
Magnus Damm | bcd5149 | 2009-10-09 00:20:04 +0000 | [diff] [blame] | 1579 | pm_runtime_put_sync(&mdp->pdev->dev); |
| 1580 | |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1581 | return &mdp->stats; |
| 1582 | } |
| 1583 | |
| 1584 | /* ioctl to device funciotn*/ |
| 1585 | static int sh_eth_do_ioctl(struct net_device *ndev, struct ifreq *rq, |
| 1586 | int cmd) |
| 1587 | { |
| 1588 | struct sh_eth_private *mdp = netdev_priv(ndev); |
| 1589 | struct phy_device *phydev = mdp->phydev; |
| 1590 | |
| 1591 | if (!netif_running(ndev)) |
| 1592 | return -EINVAL; |
| 1593 | |
| 1594 | if (!phydev) |
| 1595 | return -ENODEV; |
| 1596 | |
Richard Cochran | 28b0411 | 2010-07-17 08:48:55 +0000 | [diff] [blame] | 1597 | return phy_mii_ioctl(phydev, rq, cmd); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1598 | } |
| 1599 | |
Yoshihiro Shimoda | 380af9e | 2009-05-24 23:54:21 +0000 | [diff] [blame] | 1600 | #if defined(SH_ETH_HAS_TSU) |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1601 | /* Multicast reception directions set */ |
| 1602 | static void sh_eth_set_multicast_list(struct net_device *ndev) |
| 1603 | { |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1604 | if (ndev->flags & IFF_PROMISC) { |
| 1605 | /* Set promiscuous. */ |
Yoshihiro Shimoda | 4a55530 | 2011-03-07 21:59:26 +0000 | [diff] [blame] | 1606 | sh_eth_write(ndev, (sh_eth_read(ndev, ECMR) & ~ECMR_MCT) | |
| 1607 | ECMR_PRM, ECMR); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1608 | } else { |
| 1609 | /* Normal, unicast/broadcast-only mode. */ |
Yoshihiro Shimoda | 4a55530 | 2011-03-07 21:59:26 +0000 | [diff] [blame] | 1610 | sh_eth_write(ndev, (sh_eth_read(ndev, ECMR) & ~ECMR_PRM) | |
| 1611 | ECMR_MCT, ECMR); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1612 | } |
| 1613 | } |
Yoshihiro Shimoda | 4986b99 | 2011-03-07 21:59:34 +0000 | [diff] [blame] | 1614 | #endif /* SH_ETH_HAS_TSU */ |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1615 | |
| 1616 | /* SuperH's TSU register init function */ |
Yoshihiro Shimoda | 4a55530 | 2011-03-07 21:59:26 +0000 | [diff] [blame] | 1617 | static void sh_eth_tsu_init(struct sh_eth_private *mdp) |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1618 | { |
Yoshihiro Shimoda | 4a55530 | 2011-03-07 21:59:26 +0000 | [diff] [blame] | 1619 | sh_eth_tsu_write(mdp, 0, TSU_FWEN0); /* Disable forward(0->1) */ |
| 1620 | sh_eth_tsu_write(mdp, 0, TSU_FWEN1); /* Disable forward(1->0) */ |
| 1621 | sh_eth_tsu_write(mdp, 0, TSU_FCM); /* forward fifo 3k-3k */ |
| 1622 | sh_eth_tsu_write(mdp, 0xc, TSU_BSYSL0); |
| 1623 | sh_eth_tsu_write(mdp, 0xc, TSU_BSYSL1); |
| 1624 | sh_eth_tsu_write(mdp, 0, TSU_PRISL0); |
| 1625 | sh_eth_tsu_write(mdp, 0, TSU_PRISL1); |
| 1626 | sh_eth_tsu_write(mdp, 0, TSU_FWSL0); |
| 1627 | sh_eth_tsu_write(mdp, 0, TSU_FWSL1); |
| 1628 | 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] | 1629 | if (sh_eth_is_gether(mdp)) { |
| 1630 | sh_eth_tsu_write(mdp, 0, TSU_QTAG0); /* Disable QTAG(0->1) */ |
| 1631 | sh_eth_tsu_write(mdp, 0, TSU_QTAG1); /* Disable QTAG(1->0) */ |
| 1632 | } else { |
| 1633 | sh_eth_tsu_write(mdp, 0, TSU_QTAGM0); /* Disable QTAG(0->1) */ |
| 1634 | sh_eth_tsu_write(mdp, 0, TSU_QTAGM1); /* Disable QTAG(1->0) */ |
| 1635 | } |
Yoshihiro Shimoda | 4a55530 | 2011-03-07 21:59:26 +0000 | [diff] [blame] | 1636 | sh_eth_tsu_write(mdp, 0, TSU_FWSR); /* all interrupt status clear */ |
| 1637 | sh_eth_tsu_write(mdp, 0, TSU_FWINMK); /* Disable all interrupt */ |
| 1638 | sh_eth_tsu_write(mdp, 0, TSU_TEN); /* Disable all CAM entry */ |
| 1639 | sh_eth_tsu_write(mdp, 0, TSU_POST1); /* Disable CAM entry [ 0- 7] */ |
| 1640 | sh_eth_tsu_write(mdp, 0, TSU_POST2); /* Disable CAM entry [ 8-15] */ |
| 1641 | sh_eth_tsu_write(mdp, 0, TSU_POST3); /* Disable CAM entry [16-23] */ |
| 1642 | 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] | 1643 | } |
| 1644 | |
| 1645 | /* MDIO bus release function */ |
| 1646 | static int sh_mdio_release(struct net_device *ndev) |
| 1647 | { |
| 1648 | struct mii_bus *bus = dev_get_drvdata(&ndev->dev); |
| 1649 | |
| 1650 | /* unregister mdio bus */ |
| 1651 | mdiobus_unregister(bus); |
| 1652 | |
| 1653 | /* remove mdio bus info from net_device */ |
| 1654 | dev_set_drvdata(&ndev->dev, NULL); |
| 1655 | |
Denis Kirjanov | 0f0b405 | 2010-05-20 04:00:59 +0000 | [diff] [blame] | 1656 | /* free interrupts memory */ |
| 1657 | kfree(bus->irq); |
| 1658 | |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1659 | /* free bitbang info */ |
| 1660 | free_mdio_bitbang(bus); |
| 1661 | |
| 1662 | return 0; |
| 1663 | } |
| 1664 | |
| 1665 | /* MDIO bus init function */ |
Yoshihiro Shimoda | b3017e6 | 2011-03-07 21:59:55 +0000 | [diff] [blame] | 1666 | static int sh_mdio_init(struct net_device *ndev, int id, |
| 1667 | struct sh_eth_plat_data *pd) |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1668 | { |
| 1669 | int ret, i; |
| 1670 | struct bb_info *bitbang; |
| 1671 | struct sh_eth_private *mdp = netdev_priv(ndev); |
| 1672 | |
| 1673 | /* create bit control struct for PHY */ |
| 1674 | bitbang = kzalloc(sizeof(struct bb_info), GFP_KERNEL); |
| 1675 | if (!bitbang) { |
| 1676 | ret = -ENOMEM; |
| 1677 | goto out; |
| 1678 | } |
| 1679 | |
| 1680 | /* bitbang init */ |
Yoshihiro Shimoda | 4a55530 | 2011-03-07 21:59:26 +0000 | [diff] [blame] | 1681 | bitbang->addr = ndev->base_addr + mdp->reg_offset[PIR]; |
Yoshihiro Shimoda | b3017e6 | 2011-03-07 21:59:55 +0000 | [diff] [blame] | 1682 | bitbang->set_gate = pd->set_mdio_gate; |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1683 | bitbang->mdi_msk = 0x08; |
| 1684 | bitbang->mdo_msk = 0x04; |
| 1685 | bitbang->mmd_msk = 0x02;/* MMD */ |
| 1686 | bitbang->mdc_msk = 0x01; |
| 1687 | bitbang->ctrl.ops = &bb_ops; |
| 1688 | |
Stefan Weil | c2e07b3 | 2010-08-03 19:44:52 +0200 | [diff] [blame] | 1689 | /* MII controller setting */ |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1690 | mdp->mii_bus = alloc_mdio_bitbang(&bitbang->ctrl); |
| 1691 | if (!mdp->mii_bus) { |
| 1692 | ret = -ENOMEM; |
| 1693 | goto out_free_bitbang; |
| 1694 | } |
| 1695 | |
| 1696 | /* Hook up MII support for ethtool */ |
| 1697 | mdp->mii_bus->name = "sh_mii"; |
Lennert Buytenhek | 18ee49d | 2008-10-01 15:41:33 +0000 | [diff] [blame] | 1698 | mdp->mii_bus->parent = &ndev->dev; |
Nobuhiro Iwamatsu | fb5e2f9 | 2008-11-17 20:29:58 +0000 | [diff] [blame] | 1699 | snprintf(mdp->mii_bus->id, MII_BUS_ID_SIZE, "%x", id); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1700 | |
| 1701 | /* PHY IRQ */ |
| 1702 | mdp->mii_bus->irq = kmalloc(sizeof(int)*PHY_MAX_ADDR, GFP_KERNEL); |
| 1703 | if (!mdp->mii_bus->irq) { |
| 1704 | ret = -ENOMEM; |
| 1705 | goto out_free_bus; |
| 1706 | } |
| 1707 | |
| 1708 | for (i = 0; i < PHY_MAX_ADDR; i++) |
| 1709 | mdp->mii_bus->irq[i] = PHY_POLL; |
| 1710 | |
| 1711 | /* regist mdio bus */ |
| 1712 | ret = mdiobus_register(mdp->mii_bus); |
| 1713 | if (ret) |
| 1714 | goto out_free_irq; |
| 1715 | |
| 1716 | dev_set_drvdata(&ndev->dev, mdp->mii_bus); |
| 1717 | |
| 1718 | return 0; |
| 1719 | |
| 1720 | out_free_irq: |
| 1721 | kfree(mdp->mii_bus->irq); |
| 1722 | |
| 1723 | out_free_bus: |
Lennert Buytenhek | 298cf9b | 2008-10-08 16:29:57 -0700 | [diff] [blame] | 1724 | free_mdio_bitbang(mdp->mii_bus); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1725 | |
| 1726 | out_free_bitbang: |
| 1727 | kfree(bitbang); |
| 1728 | |
| 1729 | out: |
| 1730 | return ret; |
| 1731 | } |
| 1732 | |
Yoshihiro Shimoda | 4a55530 | 2011-03-07 21:59:26 +0000 | [diff] [blame] | 1733 | static const u16 *sh_eth_get_register_offset(int register_type) |
| 1734 | { |
| 1735 | const u16 *reg_offset = NULL; |
| 1736 | |
| 1737 | switch (register_type) { |
| 1738 | case SH_ETH_REG_GIGABIT: |
| 1739 | reg_offset = sh_eth_offset_gigabit; |
| 1740 | break; |
| 1741 | case SH_ETH_REG_FAST_SH4: |
| 1742 | reg_offset = sh_eth_offset_fast_sh4; |
| 1743 | break; |
| 1744 | case SH_ETH_REG_FAST_SH3_SH2: |
| 1745 | reg_offset = sh_eth_offset_fast_sh3_sh2; |
| 1746 | break; |
| 1747 | default: |
| 1748 | printk(KERN_ERR "Unknown register type (%d)\n", register_type); |
| 1749 | break; |
| 1750 | } |
| 1751 | |
| 1752 | return reg_offset; |
| 1753 | } |
| 1754 | |
Alexander Beregalov | ebf84ea | 2009-04-11 07:40:49 +0000 | [diff] [blame] | 1755 | static const struct net_device_ops sh_eth_netdev_ops = { |
| 1756 | .ndo_open = sh_eth_open, |
| 1757 | .ndo_stop = sh_eth_close, |
| 1758 | .ndo_start_xmit = sh_eth_start_xmit, |
| 1759 | .ndo_get_stats = sh_eth_get_stats, |
Yoshihiro Shimoda | 380af9e | 2009-05-24 23:54:21 +0000 | [diff] [blame] | 1760 | #if defined(SH_ETH_HAS_TSU) |
Jiri Pirko | afc4b13 | 2011-08-16 06:29:01 +0000 | [diff] [blame^] | 1761 | .ndo_set_rx_mode = sh_eth_set_multicast_list, |
Yoshihiro Shimoda | 380af9e | 2009-05-24 23:54:21 +0000 | [diff] [blame] | 1762 | #endif |
Alexander Beregalov | ebf84ea | 2009-04-11 07:40:49 +0000 | [diff] [blame] | 1763 | .ndo_tx_timeout = sh_eth_tx_timeout, |
| 1764 | .ndo_do_ioctl = sh_eth_do_ioctl, |
| 1765 | .ndo_validate_addr = eth_validate_addr, |
| 1766 | .ndo_set_mac_address = eth_mac_addr, |
| 1767 | .ndo_change_mtu = eth_change_mtu, |
| 1768 | }; |
| 1769 | |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1770 | static int sh_eth_drv_probe(struct platform_device *pdev) |
| 1771 | { |
Kuninori Morimoto | 9c38657 | 2010-08-19 00:39:45 -0700 | [diff] [blame] | 1772 | int ret, devno = 0; |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1773 | struct resource *res; |
| 1774 | struct net_device *ndev = NULL; |
Kuninori Morimoto | ec0d755 | 2011-06-23 16:02:38 +0000 | [diff] [blame] | 1775 | struct sh_eth_private *mdp = NULL; |
Yoshinori Sato | 71557a3 | 2008-08-06 19:49:00 -0400 | [diff] [blame] | 1776 | struct sh_eth_plat_data *pd; |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1777 | |
| 1778 | /* get base addr */ |
| 1779 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 1780 | if (unlikely(res == NULL)) { |
| 1781 | dev_err(&pdev->dev, "invalid resource\n"); |
| 1782 | ret = -EINVAL; |
| 1783 | goto out; |
| 1784 | } |
| 1785 | |
| 1786 | ndev = alloc_etherdev(sizeof(struct sh_eth_private)); |
| 1787 | if (!ndev) { |
Yoshihiro Shimoda | 380af9e | 2009-05-24 23:54:21 +0000 | [diff] [blame] | 1788 | dev_err(&pdev->dev, "Could not allocate device.\n"); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1789 | ret = -ENOMEM; |
| 1790 | goto out; |
| 1791 | } |
| 1792 | |
| 1793 | /* The sh Ether-specific entries in the device structure. */ |
| 1794 | ndev->base_addr = res->start; |
| 1795 | devno = pdev->id; |
| 1796 | if (devno < 0) |
| 1797 | devno = 0; |
| 1798 | |
| 1799 | ndev->dma = -1; |
roel kluin | cc3c080 | 2008-09-10 19:22:44 +0200 | [diff] [blame] | 1800 | ret = platform_get_irq(pdev, 0); |
| 1801 | if (ret < 0) { |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1802 | ret = -ENODEV; |
| 1803 | goto out_release; |
| 1804 | } |
roel kluin | cc3c080 | 2008-09-10 19:22:44 +0200 | [diff] [blame] | 1805 | ndev->irq = ret; |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1806 | |
| 1807 | SET_NETDEV_DEV(ndev, &pdev->dev); |
| 1808 | |
| 1809 | /* Fill in the fields of the device structure with ethernet values. */ |
| 1810 | ether_setup(ndev); |
| 1811 | |
| 1812 | mdp = netdev_priv(ndev); |
| 1813 | spin_lock_init(&mdp->lock); |
Magnus Damm | bcd5149 | 2009-10-09 00:20:04 +0000 | [diff] [blame] | 1814 | mdp->pdev = pdev; |
| 1815 | pm_runtime_enable(&pdev->dev); |
| 1816 | pm_runtime_resume(&pdev->dev); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1817 | |
Yoshinori Sato | 71557a3 | 2008-08-06 19:49:00 -0400 | [diff] [blame] | 1818 | pd = (struct sh_eth_plat_data *)(pdev->dev.platform_data); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1819 | /* get PHY ID */ |
Yoshinori Sato | 71557a3 | 2008-08-06 19:49:00 -0400 | [diff] [blame] | 1820 | mdp->phy_id = pd->phy; |
Yoshihiro Shimoda | e47c905 | 2011-03-07 21:59:45 +0000 | [diff] [blame] | 1821 | mdp->phy_interface = pd->phy_interface; |
Yoshinori Sato | 71557a3 | 2008-08-06 19:49:00 -0400 | [diff] [blame] | 1822 | /* EDMAC endian */ |
| 1823 | mdp->edmac_endian = pd->edmac_endian; |
Yoshihiro Shimoda | 4923576 | 2009-08-27 23:25:03 +0000 | [diff] [blame] | 1824 | mdp->no_ether_link = pd->no_ether_link; |
| 1825 | mdp->ether_link_active_low = pd->ether_link_active_low; |
Yoshihiro Shimoda | 4a55530 | 2011-03-07 21:59:26 +0000 | [diff] [blame] | 1826 | mdp->reg_offset = sh_eth_get_register_offset(pd->register_type); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1827 | |
Yoshihiro Shimoda | 380af9e | 2009-05-24 23:54:21 +0000 | [diff] [blame] | 1828 | /* set cpu data */ |
Yoshihiro Shimoda | 8fcd496 | 2011-03-07 21:59:49 +0000 | [diff] [blame] | 1829 | #if defined(SH_ETH_HAS_BOTH_MODULES) |
| 1830 | mdp->cd = sh_eth_get_cpu_data(mdp); |
| 1831 | #else |
Yoshihiro Shimoda | 380af9e | 2009-05-24 23:54:21 +0000 | [diff] [blame] | 1832 | mdp->cd = &sh_eth_my_cpu_data; |
Yoshihiro Shimoda | 8fcd496 | 2011-03-07 21:59:49 +0000 | [diff] [blame] | 1833 | #endif |
Yoshihiro Shimoda | 380af9e | 2009-05-24 23:54:21 +0000 | [diff] [blame] | 1834 | sh_eth_set_default_cpu_data(mdp->cd); |
| 1835 | |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1836 | /* set function */ |
Alexander Beregalov | ebf84ea | 2009-04-11 07:40:49 +0000 | [diff] [blame] | 1837 | ndev->netdev_ops = &sh_eth_netdev_ops; |
Nobuhiro Iwamatsu | dc19e4e | 2011-02-15 21:17:32 +0000 | [diff] [blame] | 1838 | SET_ETHTOOL_OPS(ndev, &sh_eth_ethtool_ops); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1839 | ndev->watchdog_timeo = TX_TIMEOUT; |
| 1840 | |
Nobuhiro Iwamatsu | dc19e4e | 2011-02-15 21:17:32 +0000 | [diff] [blame] | 1841 | /* debug message level */ |
| 1842 | mdp->msg_enable = SH_ETH_DEF_MSG_ENABLE; |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1843 | mdp->post_rx = POST_RX >> (devno << 1); |
| 1844 | mdp->post_fw = POST_FW >> (devno << 1); |
| 1845 | |
| 1846 | /* read and set MAC address */ |
Magnus Damm | 748031f | 2009-10-09 00:17:14 +0000 | [diff] [blame] | 1847 | read_mac_address(ndev, pd->mac_addr); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1848 | |
| 1849 | /* First device only init */ |
| 1850 | if (!devno) { |
Yoshihiro Shimoda | 4986b99 | 2011-03-07 21:59:34 +0000 | [diff] [blame] | 1851 | if (mdp->cd->tsu) { |
| 1852 | struct resource *rtsu; |
| 1853 | rtsu = platform_get_resource(pdev, IORESOURCE_MEM, 1); |
| 1854 | if (!rtsu) { |
| 1855 | dev_err(&pdev->dev, "Not found TSU resource\n"); |
| 1856 | goto out_release; |
| 1857 | } |
| 1858 | mdp->tsu_addr = ioremap(rtsu->start, |
| 1859 | resource_size(rtsu)); |
| 1860 | } |
Yoshihiro Shimoda | 380af9e | 2009-05-24 23:54:21 +0000 | [diff] [blame] | 1861 | if (mdp->cd->chip_reset) |
| 1862 | mdp->cd->chip_reset(ndev); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1863 | |
Yoshihiro Shimoda | 4986b99 | 2011-03-07 21:59:34 +0000 | [diff] [blame] | 1864 | if (mdp->cd->tsu) { |
| 1865 | /* TSU init (Init only)*/ |
| 1866 | sh_eth_tsu_init(mdp); |
| 1867 | } |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1868 | } |
| 1869 | |
| 1870 | /* network device register */ |
| 1871 | ret = register_netdev(ndev); |
| 1872 | if (ret) |
| 1873 | goto out_release; |
| 1874 | |
| 1875 | /* mdio bus init */ |
Yoshihiro Shimoda | b3017e6 | 2011-03-07 21:59:55 +0000 | [diff] [blame] | 1876 | ret = sh_mdio_init(ndev, pdev->id, pd); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1877 | if (ret) |
| 1878 | goto out_unregister; |
| 1879 | |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 1880 | /* print device information */ |
H Hartley Sweeten | 6cd9b49 | 2009-12-29 20:10:35 -0800 | [diff] [blame] | 1881 | pr_info("Base address at 0x%x, %pM, IRQ %d.\n", |
| 1882 | (u32)ndev->base_addr, ndev->dev_addr, ndev->irq); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1883 | |
| 1884 | platform_set_drvdata(pdev, ndev); |
| 1885 | |
| 1886 | return ret; |
| 1887 | |
| 1888 | out_unregister: |
| 1889 | unregister_netdev(ndev); |
| 1890 | |
| 1891 | out_release: |
| 1892 | /* net_dev free */ |
Kuninori Morimoto | ec0d755 | 2011-06-23 16:02:38 +0000 | [diff] [blame] | 1893 | if (mdp && mdp->tsu_addr) |
Yoshihiro Shimoda | 4986b99 | 2011-03-07 21:59:34 +0000 | [diff] [blame] | 1894 | iounmap(mdp->tsu_addr); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1895 | if (ndev) |
| 1896 | free_netdev(ndev); |
| 1897 | |
| 1898 | out: |
| 1899 | return ret; |
| 1900 | } |
| 1901 | |
| 1902 | static int sh_eth_drv_remove(struct platform_device *pdev) |
| 1903 | { |
| 1904 | struct net_device *ndev = platform_get_drvdata(pdev); |
Yoshihiro Shimoda | 4986b99 | 2011-03-07 21:59:34 +0000 | [diff] [blame] | 1905 | struct sh_eth_private *mdp = netdev_priv(ndev); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1906 | |
Yoshihiro Shimoda | 4986b99 | 2011-03-07 21:59:34 +0000 | [diff] [blame] | 1907 | iounmap(mdp->tsu_addr); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1908 | sh_mdio_release(ndev); |
| 1909 | unregister_netdev(ndev); |
Magnus Damm | bcd5149 | 2009-10-09 00:20:04 +0000 | [diff] [blame] | 1910 | pm_runtime_disable(&pdev->dev); |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1911 | free_netdev(ndev); |
| 1912 | platform_set_drvdata(pdev, NULL); |
| 1913 | |
| 1914 | return 0; |
| 1915 | } |
| 1916 | |
Magnus Damm | bcd5149 | 2009-10-09 00:20:04 +0000 | [diff] [blame] | 1917 | static int sh_eth_runtime_nop(struct device *dev) |
| 1918 | { |
| 1919 | /* |
| 1920 | * Runtime PM callback shared between ->runtime_suspend() |
| 1921 | * and ->runtime_resume(). Simply returns success. |
| 1922 | * |
| 1923 | * This driver re-initializes all registers after |
| 1924 | * pm_runtime_get_sync() anyway so there is no need |
| 1925 | * to save and restore registers here. |
| 1926 | */ |
| 1927 | return 0; |
| 1928 | } |
| 1929 | |
| 1930 | static struct dev_pm_ops sh_eth_dev_pm_ops = { |
| 1931 | .runtime_suspend = sh_eth_runtime_nop, |
| 1932 | .runtime_resume = sh_eth_runtime_nop, |
| 1933 | }; |
| 1934 | |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1935 | static struct platform_driver sh_eth_driver = { |
| 1936 | .probe = sh_eth_drv_probe, |
| 1937 | .remove = sh_eth_drv_remove, |
| 1938 | .driver = { |
| 1939 | .name = CARDNAME, |
Magnus Damm | bcd5149 | 2009-10-09 00:20:04 +0000 | [diff] [blame] | 1940 | .pm = &sh_eth_dev_pm_ops, |
Nobuhiro Iwamatsu | 86a74ff | 2008-06-09 16:33:56 -0700 | [diff] [blame] | 1941 | }, |
| 1942 | }; |
| 1943 | |
| 1944 | static int __init sh_eth_init(void) |
| 1945 | { |
| 1946 | return platform_driver_register(&sh_eth_driver); |
| 1947 | } |
| 1948 | |
| 1949 | static void __exit sh_eth_cleanup(void) |
| 1950 | { |
| 1951 | platform_driver_unregister(&sh_eth_driver); |
| 1952 | } |
| 1953 | |
| 1954 | module_init(sh_eth_init); |
| 1955 | module_exit(sh_eth_cleanup); |
| 1956 | |
| 1957 | MODULE_AUTHOR("Nobuhiro Iwamatsu, Yoshihiro Shimoda"); |
| 1958 | MODULE_DESCRIPTION("Renesas SuperH Ethernet driver"); |
| 1959 | MODULE_LICENSE("GPL v2"); |