Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Combined Ethernet driver for Motorola MPC8xx and MPC82xx. |
| 3 | * |
Vitaly Bordug | 9b8ee8e | 2007-09-18 20:05:35 +0400 | [diff] [blame] | 4 | * Copyright (c) 2003 Intracom S.A. |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 5 | * by Pantelis Antoniou <panto@intracom.gr> |
Vitaly Bordug | 9b8ee8e | 2007-09-18 20:05:35 +0400 | [diff] [blame] | 6 | * |
| 7 | * 2005 (c) MontaVista Software, Inc. |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 8 | * Vitaly Bordug <vbordug@ru.mvista.com> |
| 9 | * |
| 10 | * Heavily based on original FEC driver by Dan Malek <dan@embeddededge.com> |
| 11 | * and modifications by Joakim Tjernlund <joakim.tjernlund@lumentis.se> |
| 12 | * |
Vitaly Bordug | 9b8ee8e | 2007-09-18 20:05:35 +0400 | [diff] [blame] | 13 | * This file is licensed under the terms of the GNU General Public License |
| 14 | * version 2. This program is licensed "as is" without any warranty of any |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 15 | * kind, whether express or implied. |
| 16 | */ |
| 17 | |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 18 | #include <linux/module.h> |
| 19 | #include <linux/kernel.h> |
| 20 | #include <linux/types.h> |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 21 | #include <linux/string.h> |
| 22 | #include <linux/ptrace.h> |
| 23 | #include <linux/errno.h> |
| 24 | #include <linux/ioport.h> |
| 25 | #include <linux/slab.h> |
| 26 | #include <linux/interrupt.h> |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 27 | #include <linux/init.h> |
| 28 | #include <linux/delay.h> |
| 29 | #include <linux/netdevice.h> |
| 30 | #include <linux/etherdevice.h> |
| 31 | #include <linux/skbuff.h> |
| 32 | #include <linux/spinlock.h> |
| 33 | #include <linux/mii.h> |
| 34 | #include <linux/ethtool.h> |
| 35 | #include <linux/bitops.h> |
| 36 | #include <linux/fs.h> |
Marcelo Tosatti | f7b9996 | 2005-11-09 11:00:16 -0200 | [diff] [blame] | 37 | #include <linux/platform_device.h> |
Vitaly Bordug | 5b4b845 | 2006-08-14 23:00:30 -0700 | [diff] [blame] | 38 | #include <linux/phy.h> |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 39 | |
| 40 | #include <linux/vmalloc.h> |
| 41 | #include <asm/pgtable.h> |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 42 | #include <asm/irq.h> |
| 43 | #include <asm/uaccess.h> |
| 44 | |
Scott Wood | 976de6a | 2007-10-02 10:55:58 -0500 | [diff] [blame] | 45 | #ifdef CONFIG_PPC_CPM_NEW_BINDING |
| 46 | #include <asm/of_platform.h> |
| 47 | #endif |
| 48 | |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 49 | #include "fs_enet.h" |
| 50 | |
| 51 | /*************************************************/ |
| 52 | |
Scott Wood | 976de6a | 2007-10-02 10:55:58 -0500 | [diff] [blame] | 53 | #ifndef CONFIG_PPC_CPM_NEW_BINDING |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 54 | static char version[] __devinitdata = |
| 55 | DRV_MODULE_NAME ".c:v" DRV_MODULE_VERSION " (" DRV_MODULE_RELDATE ")" "\n"; |
Scott Wood | 976de6a | 2007-10-02 10:55:58 -0500 | [diff] [blame] | 56 | #endif |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 57 | |
| 58 | MODULE_AUTHOR("Pantelis Antoniou <panto@intracom.gr>"); |
| 59 | MODULE_DESCRIPTION("Freescale Ethernet Driver"); |
| 60 | MODULE_LICENSE("GPL"); |
| 61 | MODULE_VERSION(DRV_MODULE_VERSION); |
| 62 | |
Scott Wood | 31a5bb0 | 2007-10-01 14:20:58 -0500 | [diff] [blame] | 63 | static int fs_enet_debug = -1; /* -1 == use FS_ENET_DEF_MSG_ENABLE as value */ |
Rusty Russell | 8d3b33f | 2006-03-25 03:07:05 -0800 | [diff] [blame] | 64 | module_param(fs_enet_debug, int, 0); |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 65 | MODULE_PARM_DESC(fs_enet_debug, |
| 66 | "Freescale bitmapped debugging message enable value"); |
| 67 | |
Vitaly Bordug | 9b8ee8e | 2007-09-18 20:05:35 +0400 | [diff] [blame] | 68 | #ifdef CONFIG_NET_POLL_CONTROLLER |
| 69 | static void fs_enet_netpoll(struct net_device *dev); |
| 70 | #endif |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 71 | |
| 72 | static void fs_set_multicast_list(struct net_device *dev) |
| 73 | { |
| 74 | struct fs_enet_private *fep = netdev_priv(dev); |
| 75 | |
| 76 | (*fep->ops->set_multicast_list)(dev); |
| 77 | } |
| 78 | |
Scott Wood | 0d0d9c1 | 2007-10-01 14:20:52 -0500 | [diff] [blame] | 79 | static void skb_align(struct sk_buff *skb, int align) |
| 80 | { |
| 81 | int off = ((unsigned long)skb->data) & (align - 1); |
| 82 | |
| 83 | if (off) |
| 84 | skb_reserve(skb, align - off); |
| 85 | } |
| 86 | |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 87 | /* NAPI receive function */ |
Stephen Hemminger | bea3348 | 2007-10-03 16:41:36 -0700 | [diff] [blame] | 88 | static int fs_enet_rx_napi(struct napi_struct *napi, int budget) |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 89 | { |
Stephen Hemminger | bea3348 | 2007-10-03 16:41:36 -0700 | [diff] [blame] | 90 | struct fs_enet_private *fep = container_of(napi, struct fs_enet_private, napi); |
Scott Wood | f860f49 | 2007-10-17 12:42:43 -0500 | [diff] [blame] | 91 | struct net_device *dev = fep->ndev; |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 92 | const struct fs_platform_info *fpi = fep->fpi; |
Scott Wood | 31a5bb0 | 2007-10-01 14:20:58 -0500 | [diff] [blame] | 93 | cbd_t __iomem *bdp; |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 94 | struct sk_buff *skb, *skbn, *skbt; |
| 95 | int received = 0; |
| 96 | u16 pkt_len, sc; |
| 97 | int curidx; |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 98 | |
| 99 | if (!netif_running(dev)) |
| 100 | return 0; |
| 101 | |
| 102 | /* |
| 103 | * First, grab all of the stats for the incoming packet. |
| 104 | * These get messed up if we get called due to a busy condition. |
| 105 | */ |
| 106 | bdp = fep->cur_rx; |
| 107 | |
| 108 | /* clear RX status bits for napi*/ |
| 109 | (*fep->ops->napi_clear_rx_event)(dev); |
| 110 | |
| 111 | while (((sc = CBDR_SC(bdp)) & BD_ENET_RX_EMPTY) == 0) { |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 112 | curidx = bdp - fep->rx_bd_base; |
| 113 | |
| 114 | /* |
| 115 | * Since we have allocated space to hold a complete frame, |
| 116 | * the last indicator should be set. |
| 117 | */ |
| 118 | if ((sc & BD_ENET_RX_LAST) == 0) |
| 119 | printk(KERN_WARNING DRV_MODULE_NAME |
| 120 | ": %s rcv is not +last\n", |
| 121 | dev->name); |
| 122 | |
| 123 | /* |
Vitaly Bordug | 9b8ee8e | 2007-09-18 20:05:35 +0400 | [diff] [blame] | 124 | * Check for errors. |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 125 | */ |
| 126 | if (sc & (BD_ENET_RX_LG | BD_ENET_RX_SH | BD_ENET_RX_CL | |
| 127 | BD_ENET_RX_NO | BD_ENET_RX_CR | BD_ENET_RX_OV)) { |
| 128 | fep->stats.rx_errors++; |
| 129 | /* Frame too long or too short. */ |
| 130 | if (sc & (BD_ENET_RX_LG | BD_ENET_RX_SH)) |
| 131 | fep->stats.rx_length_errors++; |
| 132 | /* Frame alignment */ |
| 133 | if (sc & (BD_ENET_RX_NO | BD_ENET_RX_CL)) |
| 134 | fep->stats.rx_frame_errors++; |
| 135 | /* CRC Error */ |
| 136 | if (sc & BD_ENET_RX_CR) |
| 137 | fep->stats.rx_crc_errors++; |
| 138 | /* FIFO overrun */ |
| 139 | if (sc & BD_ENET_RX_OV) |
| 140 | fep->stats.rx_crc_errors++; |
| 141 | |
| 142 | skb = fep->rx_skbuff[curidx]; |
| 143 | |
Pantelis Antoniou | 34e30d6 | 2005-10-30 01:22:40 +0300 | [diff] [blame] | 144 | dma_unmap_single(fep->dev, CBDR_BUFADDR(bdp), |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 145 | L1_CACHE_ALIGN(PKT_MAXBUF_SIZE), |
| 146 | DMA_FROM_DEVICE); |
| 147 | |
| 148 | skbn = skb; |
| 149 | |
| 150 | } else { |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 151 | skb = fep->rx_skbuff[curidx]; |
| 152 | |
Pantelis Antoniou | 34e30d6 | 2005-10-30 01:22:40 +0300 | [diff] [blame] | 153 | dma_unmap_single(fep->dev, CBDR_BUFADDR(bdp), |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 154 | L1_CACHE_ALIGN(PKT_MAXBUF_SIZE), |
| 155 | DMA_FROM_DEVICE); |
| 156 | |
| 157 | /* |
| 158 | * Process the incoming frame. |
| 159 | */ |
| 160 | fep->stats.rx_packets++; |
| 161 | pkt_len = CBDR_DATLEN(bdp) - 4; /* remove CRC */ |
| 162 | fep->stats.rx_bytes += pkt_len + 4; |
| 163 | |
| 164 | if (pkt_len <= fpi->rx_copybreak) { |
| 165 | /* +2 to make IP header L1 cache aligned */ |
| 166 | skbn = dev_alloc_skb(pkt_len + 2); |
| 167 | if (skbn != NULL) { |
| 168 | skb_reserve(skbn, 2); /* align IP header */ |
Arnaldo Carvalho de Melo | d626f62 | 2007-03-27 18:55:52 -0300 | [diff] [blame] | 169 | skb_copy_from_linear_data(skb, |
| 170 | skbn->data, pkt_len); |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 171 | /* swap */ |
| 172 | skbt = skb; |
| 173 | skb = skbn; |
| 174 | skbn = skbt; |
| 175 | } |
Scott Wood | 0d0d9c1 | 2007-10-01 14:20:52 -0500 | [diff] [blame] | 176 | } else { |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 177 | skbn = dev_alloc_skb(ENET_RX_FRSIZE); |
| 178 | |
Scott Wood | 0d0d9c1 | 2007-10-01 14:20:52 -0500 | [diff] [blame] | 179 | if (skbn) |
| 180 | skb_align(skbn, ENET_RX_ALIGN); |
| 181 | } |
| 182 | |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 183 | if (skbn != NULL) { |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 184 | skb_put(skb, pkt_len); /* Make room */ |
| 185 | skb->protocol = eth_type_trans(skb, dev); |
| 186 | received++; |
| 187 | netif_receive_skb(skb); |
| 188 | } else { |
| 189 | printk(KERN_WARNING DRV_MODULE_NAME |
| 190 | ": %s Memory squeeze, dropping packet.\n", |
| 191 | dev->name); |
| 192 | fep->stats.rx_dropped++; |
| 193 | skbn = skb; |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | fep->rx_skbuff[curidx] = skbn; |
| 198 | CBDW_BUFADDR(bdp, dma_map_single(fep->dev, skbn->data, |
| 199 | L1_CACHE_ALIGN(PKT_MAXBUF_SIZE), |
| 200 | DMA_FROM_DEVICE)); |
| 201 | CBDW_DATLEN(bdp, 0); |
| 202 | CBDW_SC(bdp, (sc & ~BD_ENET_RX_STATS) | BD_ENET_RX_EMPTY); |
| 203 | |
| 204 | /* |
Vitaly Bordug | 9b8ee8e | 2007-09-18 20:05:35 +0400 | [diff] [blame] | 205 | * Update BD pointer to next entry. |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 206 | */ |
| 207 | if ((sc & BD_ENET_RX_WRAP) == 0) |
| 208 | bdp++; |
| 209 | else |
| 210 | bdp = fep->rx_bd_base; |
| 211 | |
| 212 | (*fep->ops->rx_bd_done)(dev); |
Stephen Hemminger | bea3348 | 2007-10-03 16:41:36 -0700 | [diff] [blame] | 213 | |
| 214 | if (received >= budget) |
| 215 | break; |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | fep->cur_rx = bdp; |
| 219 | |
Scott Wood | f860f49 | 2007-10-17 12:42:43 -0500 | [diff] [blame] | 220 | if (received < budget) { |
Stephen Hemminger | bea3348 | 2007-10-03 16:41:36 -0700 | [diff] [blame] | 221 | /* done */ |
| 222 | netif_rx_complete(dev, napi); |
| 223 | (*fep->ops->napi_enable_rx)(dev); |
| 224 | } |
| 225 | return received; |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 226 | } |
| 227 | |
| 228 | /* non NAPI receive function */ |
| 229 | static int fs_enet_rx_non_napi(struct net_device *dev) |
| 230 | { |
| 231 | struct fs_enet_private *fep = netdev_priv(dev); |
| 232 | const struct fs_platform_info *fpi = fep->fpi; |
Scott Wood | 31a5bb0 | 2007-10-01 14:20:58 -0500 | [diff] [blame] | 233 | cbd_t __iomem *bdp; |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 234 | struct sk_buff *skb, *skbn, *skbt; |
| 235 | int received = 0; |
| 236 | u16 pkt_len, sc; |
| 237 | int curidx; |
| 238 | /* |
| 239 | * First, grab all of the stats for the incoming packet. |
| 240 | * These get messed up if we get called due to a busy condition. |
| 241 | */ |
| 242 | bdp = fep->cur_rx; |
| 243 | |
| 244 | while (((sc = CBDR_SC(bdp)) & BD_ENET_RX_EMPTY) == 0) { |
| 245 | |
| 246 | curidx = bdp - fep->rx_bd_base; |
| 247 | |
| 248 | /* |
| 249 | * Since we have allocated space to hold a complete frame, |
| 250 | * the last indicator should be set. |
| 251 | */ |
| 252 | if ((sc & BD_ENET_RX_LAST) == 0) |
| 253 | printk(KERN_WARNING DRV_MODULE_NAME |
| 254 | ": %s rcv is not +last\n", |
| 255 | dev->name); |
| 256 | |
| 257 | /* |
Vitaly Bordug | 9b8ee8e | 2007-09-18 20:05:35 +0400 | [diff] [blame] | 258 | * Check for errors. |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 259 | */ |
| 260 | if (sc & (BD_ENET_RX_LG | BD_ENET_RX_SH | BD_ENET_RX_CL | |
| 261 | BD_ENET_RX_NO | BD_ENET_RX_CR | BD_ENET_RX_OV)) { |
| 262 | fep->stats.rx_errors++; |
| 263 | /* Frame too long or too short. */ |
| 264 | if (sc & (BD_ENET_RX_LG | BD_ENET_RX_SH)) |
| 265 | fep->stats.rx_length_errors++; |
| 266 | /* Frame alignment */ |
| 267 | if (sc & (BD_ENET_RX_NO | BD_ENET_RX_CL)) |
| 268 | fep->stats.rx_frame_errors++; |
| 269 | /* CRC Error */ |
| 270 | if (sc & BD_ENET_RX_CR) |
| 271 | fep->stats.rx_crc_errors++; |
| 272 | /* FIFO overrun */ |
| 273 | if (sc & BD_ENET_RX_OV) |
| 274 | fep->stats.rx_crc_errors++; |
| 275 | |
| 276 | skb = fep->rx_skbuff[curidx]; |
| 277 | |
Pantelis Antoniou | 34e30d6 | 2005-10-30 01:22:40 +0300 | [diff] [blame] | 278 | dma_unmap_single(fep->dev, CBDR_BUFADDR(bdp), |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 279 | L1_CACHE_ALIGN(PKT_MAXBUF_SIZE), |
| 280 | DMA_FROM_DEVICE); |
| 281 | |
| 282 | skbn = skb; |
| 283 | |
| 284 | } else { |
| 285 | |
| 286 | skb = fep->rx_skbuff[curidx]; |
| 287 | |
Pantelis Antoniou | 34e30d6 | 2005-10-30 01:22:40 +0300 | [diff] [blame] | 288 | dma_unmap_single(fep->dev, CBDR_BUFADDR(bdp), |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 289 | L1_CACHE_ALIGN(PKT_MAXBUF_SIZE), |
| 290 | DMA_FROM_DEVICE); |
| 291 | |
| 292 | /* |
| 293 | * Process the incoming frame. |
| 294 | */ |
| 295 | fep->stats.rx_packets++; |
| 296 | pkt_len = CBDR_DATLEN(bdp) - 4; /* remove CRC */ |
| 297 | fep->stats.rx_bytes += pkt_len + 4; |
| 298 | |
| 299 | if (pkt_len <= fpi->rx_copybreak) { |
| 300 | /* +2 to make IP header L1 cache aligned */ |
| 301 | skbn = dev_alloc_skb(pkt_len + 2); |
| 302 | if (skbn != NULL) { |
| 303 | skb_reserve(skbn, 2); /* align IP header */ |
Arnaldo Carvalho de Melo | d626f62 | 2007-03-27 18:55:52 -0300 | [diff] [blame] | 304 | skb_copy_from_linear_data(skb, |
| 305 | skbn->data, pkt_len); |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 306 | /* swap */ |
| 307 | skbt = skb; |
| 308 | skb = skbn; |
| 309 | skbn = skbt; |
| 310 | } |
Scott Wood | 0d0d9c1 | 2007-10-01 14:20:52 -0500 | [diff] [blame] | 311 | } else { |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 312 | skbn = dev_alloc_skb(ENET_RX_FRSIZE); |
| 313 | |
Scott Wood | 0d0d9c1 | 2007-10-01 14:20:52 -0500 | [diff] [blame] | 314 | if (skbn) |
| 315 | skb_align(skbn, ENET_RX_ALIGN); |
| 316 | } |
| 317 | |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 318 | if (skbn != NULL) { |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 319 | skb_put(skb, pkt_len); /* Make room */ |
| 320 | skb->protocol = eth_type_trans(skb, dev); |
| 321 | received++; |
| 322 | netif_rx(skb); |
| 323 | } else { |
| 324 | printk(KERN_WARNING DRV_MODULE_NAME |
| 325 | ": %s Memory squeeze, dropping packet.\n", |
| 326 | dev->name); |
| 327 | fep->stats.rx_dropped++; |
| 328 | skbn = skb; |
| 329 | } |
| 330 | } |
| 331 | |
| 332 | fep->rx_skbuff[curidx] = skbn; |
| 333 | CBDW_BUFADDR(bdp, dma_map_single(fep->dev, skbn->data, |
| 334 | L1_CACHE_ALIGN(PKT_MAXBUF_SIZE), |
| 335 | DMA_FROM_DEVICE)); |
| 336 | CBDW_DATLEN(bdp, 0); |
| 337 | CBDW_SC(bdp, (sc & ~BD_ENET_RX_STATS) | BD_ENET_RX_EMPTY); |
| 338 | |
| 339 | /* |
Vitaly Bordug | 9b8ee8e | 2007-09-18 20:05:35 +0400 | [diff] [blame] | 340 | * Update BD pointer to next entry. |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 341 | */ |
| 342 | if ((sc & BD_ENET_RX_WRAP) == 0) |
| 343 | bdp++; |
| 344 | else |
| 345 | bdp = fep->rx_bd_base; |
| 346 | |
| 347 | (*fep->ops->rx_bd_done)(dev); |
| 348 | } |
| 349 | |
| 350 | fep->cur_rx = bdp; |
| 351 | |
| 352 | return 0; |
| 353 | } |
| 354 | |
| 355 | static void fs_enet_tx(struct net_device *dev) |
| 356 | { |
| 357 | struct fs_enet_private *fep = netdev_priv(dev); |
Scott Wood | 31a5bb0 | 2007-10-01 14:20:58 -0500 | [diff] [blame] | 358 | cbd_t __iomem *bdp; |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 359 | struct sk_buff *skb; |
| 360 | int dirtyidx, do_wake, do_restart; |
| 361 | u16 sc; |
| 362 | |
Vitaly Bordug | aa90f50 | 2007-09-18 20:05:27 +0400 | [diff] [blame] | 363 | spin_lock(&fep->tx_lock); |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 364 | bdp = fep->dirty_tx; |
| 365 | |
| 366 | do_wake = do_restart = 0; |
| 367 | while (((sc = CBDR_SC(bdp)) & BD_ENET_TX_READY) == 0) { |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 368 | dirtyidx = bdp - fep->tx_bd_base; |
| 369 | |
| 370 | if (fep->tx_free == fep->tx_ring) |
| 371 | break; |
| 372 | |
| 373 | skb = fep->tx_skbuff[dirtyidx]; |
| 374 | |
| 375 | /* |
Vitaly Bordug | 9b8ee8e | 2007-09-18 20:05:35 +0400 | [diff] [blame] | 376 | * Check for errors. |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 377 | */ |
| 378 | if (sc & (BD_ENET_TX_HB | BD_ENET_TX_LC | |
| 379 | BD_ENET_TX_RL | BD_ENET_TX_UN | BD_ENET_TX_CSL)) { |
| 380 | |
| 381 | if (sc & BD_ENET_TX_HB) /* No heartbeat */ |
| 382 | fep->stats.tx_heartbeat_errors++; |
| 383 | if (sc & BD_ENET_TX_LC) /* Late collision */ |
| 384 | fep->stats.tx_window_errors++; |
| 385 | if (sc & BD_ENET_TX_RL) /* Retrans limit */ |
| 386 | fep->stats.tx_aborted_errors++; |
| 387 | if (sc & BD_ENET_TX_UN) /* Underrun */ |
| 388 | fep->stats.tx_fifo_errors++; |
| 389 | if (sc & BD_ENET_TX_CSL) /* Carrier lost */ |
| 390 | fep->stats.tx_carrier_errors++; |
| 391 | |
| 392 | if (sc & (BD_ENET_TX_LC | BD_ENET_TX_RL | BD_ENET_TX_UN)) { |
| 393 | fep->stats.tx_errors++; |
| 394 | do_restart = 1; |
| 395 | } |
| 396 | } else |
| 397 | fep->stats.tx_packets++; |
| 398 | |
| 399 | if (sc & BD_ENET_TX_READY) |
| 400 | printk(KERN_WARNING DRV_MODULE_NAME |
| 401 | ": %s HEY! Enet xmit interrupt and TX_READY.\n", |
| 402 | dev->name); |
| 403 | |
| 404 | /* |
| 405 | * Deferred means some collisions occurred during transmit, |
| 406 | * but we eventually sent the packet OK. |
| 407 | */ |
| 408 | if (sc & BD_ENET_TX_DEF) |
| 409 | fep->stats.collisions++; |
| 410 | |
| 411 | /* unmap */ |
Pantelis Antoniou | 34e30d6 | 2005-10-30 01:22:40 +0300 | [diff] [blame] | 412 | dma_unmap_single(fep->dev, CBDR_BUFADDR(bdp), |
| 413 | skb->len, DMA_TO_DEVICE); |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 414 | |
| 415 | /* |
Vitaly Bordug | 9b8ee8e | 2007-09-18 20:05:35 +0400 | [diff] [blame] | 416 | * Free the sk buffer associated with this last transmit. |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 417 | */ |
| 418 | dev_kfree_skb_irq(skb); |
| 419 | fep->tx_skbuff[dirtyidx] = NULL; |
| 420 | |
| 421 | /* |
Vitaly Bordug | 9b8ee8e | 2007-09-18 20:05:35 +0400 | [diff] [blame] | 422 | * Update pointer to next buffer descriptor to be transmitted. |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 423 | */ |
| 424 | if ((sc & BD_ENET_TX_WRAP) == 0) |
| 425 | bdp++; |
| 426 | else |
| 427 | bdp = fep->tx_bd_base; |
| 428 | |
| 429 | /* |
| 430 | * Since we have freed up a buffer, the ring is no longer |
| 431 | * full. |
| 432 | */ |
| 433 | if (!fep->tx_free++) |
| 434 | do_wake = 1; |
| 435 | } |
| 436 | |
| 437 | fep->dirty_tx = bdp; |
| 438 | |
| 439 | if (do_restart) |
| 440 | (*fep->ops->tx_restart)(dev); |
| 441 | |
Vitaly Bordug | aa90f50 | 2007-09-18 20:05:27 +0400 | [diff] [blame] | 442 | spin_unlock(&fep->tx_lock); |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 443 | |
| 444 | if (do_wake) |
| 445 | netif_wake_queue(dev); |
| 446 | } |
| 447 | |
| 448 | /* |
| 449 | * The interrupt handler. |
| 450 | * This is called from the MPC core interrupt. |
| 451 | */ |
| 452 | static irqreturn_t |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 453 | fs_enet_interrupt(int irq, void *dev_id) |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 454 | { |
| 455 | struct net_device *dev = dev_id; |
| 456 | struct fs_enet_private *fep; |
| 457 | const struct fs_platform_info *fpi; |
| 458 | u32 int_events; |
| 459 | u32 int_clr_events; |
| 460 | int nr, napi_ok; |
| 461 | int handled; |
| 462 | |
| 463 | fep = netdev_priv(dev); |
| 464 | fpi = fep->fpi; |
| 465 | |
| 466 | nr = 0; |
| 467 | while ((int_events = (*fep->ops->get_int_events)(dev)) != 0) { |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 468 | nr++; |
| 469 | |
| 470 | int_clr_events = int_events; |
| 471 | if (fpi->use_napi) |
| 472 | int_clr_events &= ~fep->ev_napi_rx; |
| 473 | |
| 474 | (*fep->ops->clear_int_events)(dev, int_clr_events); |
| 475 | |
| 476 | if (int_events & fep->ev_err) |
| 477 | (*fep->ops->ev_error)(dev, int_events); |
| 478 | |
| 479 | if (int_events & fep->ev_rx) { |
| 480 | if (!fpi->use_napi) |
| 481 | fs_enet_rx_non_napi(dev); |
| 482 | else { |
Stephen Hemminger | bea3348 | 2007-10-03 16:41:36 -0700 | [diff] [blame] | 483 | napi_ok = napi_schedule_prep(&fep->napi); |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 484 | |
| 485 | (*fep->ops->napi_disable_rx)(dev); |
| 486 | (*fep->ops->clear_int_events)(dev, fep->ev_napi_rx); |
| 487 | |
| 488 | /* NOTE: it is possible for FCCs in NAPI mode */ |
| 489 | /* to submit a spurious interrupt while in poll */ |
| 490 | if (napi_ok) |
Stephen Hemminger | bea3348 | 2007-10-03 16:41:36 -0700 | [diff] [blame] | 491 | __netif_rx_schedule(dev, &fep->napi); |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 492 | } |
| 493 | } |
| 494 | |
| 495 | if (int_events & fep->ev_tx) |
| 496 | fs_enet_tx(dev); |
| 497 | } |
| 498 | |
| 499 | handled = nr > 0; |
| 500 | return IRQ_RETVAL(handled); |
| 501 | } |
| 502 | |
| 503 | void fs_init_bds(struct net_device *dev) |
| 504 | { |
| 505 | struct fs_enet_private *fep = netdev_priv(dev); |
Scott Wood | 31a5bb0 | 2007-10-01 14:20:58 -0500 | [diff] [blame] | 506 | cbd_t __iomem *bdp; |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 507 | struct sk_buff *skb; |
| 508 | int i; |
| 509 | |
| 510 | fs_cleanup_bds(dev); |
| 511 | |
| 512 | fep->dirty_tx = fep->cur_tx = fep->tx_bd_base; |
| 513 | fep->tx_free = fep->tx_ring; |
| 514 | fep->cur_rx = fep->rx_bd_base; |
| 515 | |
| 516 | /* |
Vitaly Bordug | 9b8ee8e | 2007-09-18 20:05:35 +0400 | [diff] [blame] | 517 | * Initialize the receive buffer descriptors. |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 518 | */ |
| 519 | for (i = 0, bdp = fep->rx_bd_base; i < fep->rx_ring; i++, bdp++) { |
| 520 | skb = dev_alloc_skb(ENET_RX_FRSIZE); |
| 521 | if (skb == NULL) { |
| 522 | printk(KERN_WARNING DRV_MODULE_NAME |
| 523 | ": %s Memory squeeze, unable to allocate skb\n", |
| 524 | dev->name); |
| 525 | break; |
| 526 | } |
Scott Wood | 0d0d9c1 | 2007-10-01 14:20:52 -0500 | [diff] [blame] | 527 | skb_align(skb, ENET_RX_ALIGN); |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 528 | fep->rx_skbuff[i] = skb; |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 529 | CBDW_BUFADDR(bdp, |
| 530 | dma_map_single(fep->dev, skb->data, |
| 531 | L1_CACHE_ALIGN(PKT_MAXBUF_SIZE), |
| 532 | DMA_FROM_DEVICE)); |
| 533 | CBDW_DATLEN(bdp, 0); /* zero */ |
| 534 | CBDW_SC(bdp, BD_ENET_RX_EMPTY | |
| 535 | ((i < fep->rx_ring - 1) ? 0 : BD_SC_WRAP)); |
| 536 | } |
| 537 | /* |
Vitaly Bordug | 9b8ee8e | 2007-09-18 20:05:35 +0400 | [diff] [blame] | 538 | * if we failed, fillup remainder |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 539 | */ |
| 540 | for (; i < fep->rx_ring; i++, bdp++) { |
| 541 | fep->rx_skbuff[i] = NULL; |
| 542 | CBDW_SC(bdp, (i < fep->rx_ring - 1) ? 0 : BD_SC_WRAP); |
| 543 | } |
| 544 | |
| 545 | /* |
Vitaly Bordug | 9b8ee8e | 2007-09-18 20:05:35 +0400 | [diff] [blame] | 546 | * ...and the same for transmit. |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 547 | */ |
| 548 | for (i = 0, bdp = fep->tx_bd_base; i < fep->tx_ring; i++, bdp++) { |
| 549 | fep->tx_skbuff[i] = NULL; |
| 550 | CBDW_BUFADDR(bdp, 0); |
| 551 | CBDW_DATLEN(bdp, 0); |
| 552 | CBDW_SC(bdp, (i < fep->tx_ring - 1) ? 0 : BD_SC_WRAP); |
| 553 | } |
| 554 | } |
| 555 | |
| 556 | void fs_cleanup_bds(struct net_device *dev) |
| 557 | { |
| 558 | struct fs_enet_private *fep = netdev_priv(dev); |
| 559 | struct sk_buff *skb; |
Scott Wood | 31a5bb0 | 2007-10-01 14:20:58 -0500 | [diff] [blame] | 560 | cbd_t __iomem *bdp; |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 561 | int i; |
| 562 | |
| 563 | /* |
Vitaly Bordug | 9b8ee8e | 2007-09-18 20:05:35 +0400 | [diff] [blame] | 564 | * Reset SKB transmit buffers. |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 565 | */ |
Pantelis Antoniou | 34e30d6 | 2005-10-30 01:22:40 +0300 | [diff] [blame] | 566 | for (i = 0, bdp = fep->tx_bd_base; i < fep->tx_ring; i++, bdp++) { |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 567 | if ((skb = fep->tx_skbuff[i]) == NULL) |
| 568 | continue; |
| 569 | |
| 570 | /* unmap */ |
Pantelis Antoniou | 34e30d6 | 2005-10-30 01:22:40 +0300 | [diff] [blame] | 571 | dma_unmap_single(fep->dev, CBDR_BUFADDR(bdp), |
| 572 | skb->len, DMA_TO_DEVICE); |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 573 | |
| 574 | fep->tx_skbuff[i] = NULL; |
| 575 | dev_kfree_skb(skb); |
| 576 | } |
| 577 | |
| 578 | /* |
Vitaly Bordug | 9b8ee8e | 2007-09-18 20:05:35 +0400 | [diff] [blame] | 579 | * Reset SKB receive buffers |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 580 | */ |
Pantelis Antoniou | 34e30d6 | 2005-10-30 01:22:40 +0300 | [diff] [blame] | 581 | for (i = 0, bdp = fep->rx_bd_base; i < fep->rx_ring; i++, bdp++) { |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 582 | if ((skb = fep->rx_skbuff[i]) == NULL) |
| 583 | continue; |
| 584 | |
| 585 | /* unmap */ |
Pantelis Antoniou | 34e30d6 | 2005-10-30 01:22:40 +0300 | [diff] [blame] | 586 | dma_unmap_single(fep->dev, CBDR_BUFADDR(bdp), |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 587 | L1_CACHE_ALIGN(PKT_MAXBUF_SIZE), |
| 588 | DMA_FROM_DEVICE); |
| 589 | |
| 590 | fep->rx_skbuff[i] = NULL; |
| 591 | |
| 592 | dev_kfree_skb(skb); |
| 593 | } |
| 594 | } |
| 595 | |
| 596 | /**********************************************************************************/ |
| 597 | |
| 598 | static int fs_enet_start_xmit(struct sk_buff *skb, struct net_device *dev) |
| 599 | { |
| 600 | struct fs_enet_private *fep = netdev_priv(dev); |
Scott Wood | 31a5bb0 | 2007-10-01 14:20:58 -0500 | [diff] [blame] | 601 | cbd_t __iomem *bdp; |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 602 | int curidx; |
| 603 | u16 sc; |
| 604 | unsigned long flags; |
| 605 | |
| 606 | spin_lock_irqsave(&fep->tx_lock, flags); |
| 607 | |
| 608 | /* |
Vitaly Bordug | 9b8ee8e | 2007-09-18 20:05:35 +0400 | [diff] [blame] | 609 | * Fill in a Tx ring entry |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 610 | */ |
| 611 | bdp = fep->cur_tx; |
| 612 | |
| 613 | if (!fep->tx_free || (CBDR_SC(bdp) & BD_ENET_TX_READY)) { |
| 614 | netif_stop_queue(dev); |
| 615 | spin_unlock_irqrestore(&fep->tx_lock, flags); |
| 616 | |
| 617 | /* |
| 618 | * Ooops. All transmit buffers are full. Bail out. |
| 619 | * This should not happen, since the tx queue should be stopped. |
| 620 | */ |
| 621 | printk(KERN_WARNING DRV_MODULE_NAME |
| 622 | ": %s tx queue full!.\n", dev->name); |
| 623 | return NETDEV_TX_BUSY; |
| 624 | } |
| 625 | |
| 626 | curidx = bdp - fep->tx_bd_base; |
| 627 | /* |
Vitaly Bordug | 9b8ee8e | 2007-09-18 20:05:35 +0400 | [diff] [blame] | 628 | * Clear all of the status flags. |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 629 | */ |
| 630 | CBDC_SC(bdp, BD_ENET_TX_STATS); |
| 631 | |
| 632 | /* |
Vitaly Bordug | 9b8ee8e | 2007-09-18 20:05:35 +0400 | [diff] [blame] | 633 | * Save skb pointer. |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 634 | */ |
| 635 | fep->tx_skbuff[curidx] = skb; |
| 636 | |
| 637 | fep->stats.tx_bytes += skb->len; |
| 638 | |
| 639 | /* |
Vitaly Bordug | 9b8ee8e | 2007-09-18 20:05:35 +0400 | [diff] [blame] | 640 | * Push the data cache so the CPM does not get stale memory data. |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 641 | */ |
| 642 | CBDW_BUFADDR(bdp, dma_map_single(fep->dev, |
| 643 | skb->data, skb->len, DMA_TO_DEVICE)); |
| 644 | CBDW_DATLEN(bdp, skb->len); |
| 645 | |
| 646 | dev->trans_start = jiffies; |
| 647 | |
| 648 | /* |
Vitaly Bordug | 9b8ee8e | 2007-09-18 20:05:35 +0400 | [diff] [blame] | 649 | * If this was the last BD in the ring, start at the beginning again. |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 650 | */ |
| 651 | if ((CBDR_SC(bdp) & BD_ENET_TX_WRAP) == 0) |
| 652 | fep->cur_tx++; |
| 653 | else |
| 654 | fep->cur_tx = fep->tx_bd_base; |
| 655 | |
| 656 | if (!--fep->tx_free) |
| 657 | netif_stop_queue(dev); |
| 658 | |
| 659 | /* Trigger transmission start */ |
| 660 | sc = BD_ENET_TX_READY | BD_ENET_TX_INTR | |
| 661 | BD_ENET_TX_LAST | BD_ENET_TX_TC; |
| 662 | |
| 663 | /* note that while FEC does not have this bit |
| 664 | * it marks it as available for software use |
| 665 | * yay for hw reuse :) */ |
| 666 | if (skb->len <= 60) |
| 667 | sc |= BD_ENET_TX_PAD; |
| 668 | CBDS_SC(bdp, sc); |
| 669 | |
| 670 | (*fep->ops->tx_kickstart)(dev); |
| 671 | |
| 672 | spin_unlock_irqrestore(&fep->tx_lock, flags); |
| 673 | |
| 674 | return NETDEV_TX_OK; |
| 675 | } |
| 676 | |
| 677 | static int fs_request_irq(struct net_device *dev, int irq, const char *name, |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 678 | irq_handler_t irqf) |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 679 | { |
| 680 | struct fs_enet_private *fep = netdev_priv(dev); |
| 681 | |
| 682 | (*fep->ops->pre_request_irq)(dev, irq); |
Thomas Gleixner | 1fb9df5 | 2006-07-01 19:29:39 -0700 | [diff] [blame] | 683 | return request_irq(irq, irqf, IRQF_SHARED, name, dev); |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 684 | } |
| 685 | |
| 686 | static void fs_free_irq(struct net_device *dev, int irq) |
| 687 | { |
| 688 | struct fs_enet_private *fep = netdev_priv(dev); |
| 689 | |
| 690 | free_irq(irq, dev); |
| 691 | (*fep->ops->post_free_irq)(dev, irq); |
| 692 | } |
| 693 | |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 694 | static void fs_timeout(struct net_device *dev) |
| 695 | { |
| 696 | struct fs_enet_private *fep = netdev_priv(dev); |
| 697 | unsigned long flags; |
| 698 | int wake = 0; |
| 699 | |
| 700 | fep->stats.tx_errors++; |
| 701 | |
| 702 | spin_lock_irqsave(&fep->lock, flags); |
| 703 | |
| 704 | if (dev->flags & IFF_UP) { |
Vitaly Bordug | 5b4b845 | 2006-08-14 23:00:30 -0700 | [diff] [blame] | 705 | phy_stop(fep->phydev); |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 706 | (*fep->ops->stop)(dev); |
| 707 | (*fep->ops->restart)(dev); |
Vitaly Bordug | 5b4b845 | 2006-08-14 23:00:30 -0700 | [diff] [blame] | 708 | phy_start(fep->phydev); |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 709 | } |
| 710 | |
Vitaly Bordug | 5b4b845 | 2006-08-14 23:00:30 -0700 | [diff] [blame] | 711 | phy_start(fep->phydev); |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 712 | wake = fep->tx_free && !(CBDR_SC(fep->cur_tx) & BD_ENET_TX_READY); |
| 713 | spin_unlock_irqrestore(&fep->lock, flags); |
| 714 | |
| 715 | if (wake) |
| 716 | netif_wake_queue(dev); |
| 717 | } |
| 718 | |
Vitaly Bordug | 5b4b845 | 2006-08-14 23:00:30 -0700 | [diff] [blame] | 719 | /*----------------------------------------------------------------------------- |
| 720 | * generic link-change handler - should be sufficient for most cases |
| 721 | *-----------------------------------------------------------------------------*/ |
| 722 | static void generic_adjust_link(struct net_device *dev) |
| 723 | { |
Scott Wood | 0fb300f | 2007-10-01 14:20:17 -0500 | [diff] [blame] | 724 | struct fs_enet_private *fep = netdev_priv(dev); |
| 725 | struct phy_device *phydev = fep->phydev; |
| 726 | int new_state = 0; |
Vitaly Bordug | 5b4b845 | 2006-08-14 23:00:30 -0700 | [diff] [blame] | 727 | |
Scott Wood | 0fb300f | 2007-10-01 14:20:17 -0500 | [diff] [blame] | 728 | if (phydev->link) { |
| 729 | /* adjust to duplex mode */ |
| 730 | if (phydev->duplex != fep->oldduplex) { |
| 731 | new_state = 1; |
| 732 | fep->oldduplex = phydev->duplex; |
| 733 | } |
Vitaly Bordug | 5b4b845 | 2006-08-14 23:00:30 -0700 | [diff] [blame] | 734 | |
Scott Wood | 0fb300f | 2007-10-01 14:20:17 -0500 | [diff] [blame] | 735 | if (phydev->speed != fep->oldspeed) { |
| 736 | new_state = 1; |
| 737 | fep->oldspeed = phydev->speed; |
| 738 | } |
Vitaly Bordug | 5b4b845 | 2006-08-14 23:00:30 -0700 | [diff] [blame] | 739 | |
Scott Wood | 0fb300f | 2007-10-01 14:20:17 -0500 | [diff] [blame] | 740 | if (!fep->oldlink) { |
| 741 | new_state = 1; |
| 742 | fep->oldlink = 1; |
| 743 | netif_schedule(dev); |
| 744 | netif_carrier_on(dev); |
| 745 | netif_start_queue(dev); |
| 746 | } |
Vitaly Bordug | 5b4b845 | 2006-08-14 23:00:30 -0700 | [diff] [blame] | 747 | |
Scott Wood | 0fb300f | 2007-10-01 14:20:17 -0500 | [diff] [blame] | 748 | if (new_state) |
| 749 | fep->ops->restart(dev); |
| 750 | } else if (fep->oldlink) { |
| 751 | new_state = 1; |
| 752 | fep->oldlink = 0; |
| 753 | fep->oldspeed = 0; |
| 754 | fep->oldduplex = -1; |
| 755 | netif_carrier_off(dev); |
| 756 | netif_stop_queue(dev); |
| 757 | } |
Vitaly Bordug | 5b4b845 | 2006-08-14 23:00:30 -0700 | [diff] [blame] | 758 | |
Scott Wood | 0fb300f | 2007-10-01 14:20:17 -0500 | [diff] [blame] | 759 | if (new_state && netif_msg_link(fep)) |
| 760 | phy_print_status(phydev); |
Vitaly Bordug | 5b4b845 | 2006-08-14 23:00:30 -0700 | [diff] [blame] | 761 | } |
| 762 | |
| 763 | |
| 764 | static void fs_adjust_link(struct net_device *dev) |
| 765 | { |
| 766 | struct fs_enet_private *fep = netdev_priv(dev); |
| 767 | unsigned long flags; |
| 768 | |
| 769 | spin_lock_irqsave(&fep->lock, flags); |
| 770 | |
| 771 | if(fep->ops->adjust_link) |
| 772 | fep->ops->adjust_link(dev); |
| 773 | else |
| 774 | generic_adjust_link(dev); |
| 775 | |
| 776 | spin_unlock_irqrestore(&fep->lock, flags); |
| 777 | } |
| 778 | |
| 779 | static int fs_init_phy(struct net_device *dev) |
| 780 | { |
| 781 | struct fs_enet_private *fep = netdev_priv(dev); |
| 782 | struct phy_device *phydev; |
| 783 | |
| 784 | fep->oldlink = 0; |
| 785 | fep->oldspeed = 0; |
| 786 | fep->oldduplex = -1; |
| 787 | if(fep->fpi->bus_id) |
Andy Fleming | e8a2b6a | 2006-12-01 12:01:06 -0600 | [diff] [blame] | 788 | phydev = phy_connect(dev, fep->fpi->bus_id, &fs_adjust_link, 0, |
| 789 | PHY_INTERFACE_MODE_MII); |
Vitaly Bordug | 5b4b845 | 2006-08-14 23:00:30 -0700 | [diff] [blame] | 790 | else { |
| 791 | printk("No phy bus ID specified in BSP code\n"); |
| 792 | return -EINVAL; |
| 793 | } |
| 794 | if (IS_ERR(phydev)) { |
| 795 | printk(KERN_ERR "%s: Could not attach to PHY\n", dev->name); |
| 796 | return PTR_ERR(phydev); |
| 797 | } |
| 798 | |
| 799 | fep->phydev = phydev; |
| 800 | |
| 801 | return 0; |
| 802 | } |
| 803 | |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 804 | static int fs_enet_open(struct net_device *dev) |
| 805 | { |
| 806 | struct fs_enet_private *fep = netdev_priv(dev); |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 807 | int r; |
Vitaly Bordug | 5b4b845 | 2006-08-14 23:00:30 -0700 | [diff] [blame] | 808 | int err; |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 809 | |
Scott Wood | f860f49 | 2007-10-17 12:42:43 -0500 | [diff] [blame] | 810 | if (fep->fpi->use_napi) |
| 811 | napi_enable(&fep->napi); |
Stephen Hemminger | bea3348 | 2007-10-03 16:41:36 -0700 | [diff] [blame] | 812 | |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 813 | /* Install our interrupt handler. */ |
| 814 | r = fs_request_irq(dev, fep->interrupt, "fs_enet-mac", fs_enet_interrupt); |
| 815 | if (r != 0) { |
| 816 | printk(KERN_ERR DRV_MODULE_NAME |
Vitaly Bordug | 5b4b845 | 2006-08-14 23:00:30 -0700 | [diff] [blame] | 817 | ": %s Could not allocate FS_ENET IRQ!", dev->name); |
Scott Wood | f860f49 | 2007-10-17 12:42:43 -0500 | [diff] [blame] | 818 | if (fep->fpi->use_napi) |
| 819 | napi_disable(&fep->napi); |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 820 | return -EINVAL; |
| 821 | } |
| 822 | |
Vitaly Bordug | 5b4b845 | 2006-08-14 23:00:30 -0700 | [diff] [blame] | 823 | err = fs_init_phy(dev); |
Scott Wood | f860f49 | 2007-10-17 12:42:43 -0500 | [diff] [blame] | 824 | if (err) { |
| 825 | if (fep->fpi->use_napi) |
| 826 | napi_disable(&fep->napi); |
Vitaly Bordug | 5b4b845 | 2006-08-14 23:00:30 -0700 | [diff] [blame] | 827 | return err; |
Stephen Hemminger | bea3348 | 2007-10-03 16:41:36 -0700 | [diff] [blame] | 828 | } |
Vitaly Bordug | 5b4b845 | 2006-08-14 23:00:30 -0700 | [diff] [blame] | 829 | phy_start(fep->phydev); |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 830 | |
| 831 | return 0; |
| 832 | } |
| 833 | |
| 834 | static int fs_enet_close(struct net_device *dev) |
| 835 | { |
| 836 | struct fs_enet_private *fep = netdev_priv(dev); |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 837 | unsigned long flags; |
| 838 | |
| 839 | netif_stop_queue(dev); |
| 840 | netif_carrier_off(dev); |
Stephen Hemminger | bea3348 | 2007-10-03 16:41:36 -0700 | [diff] [blame] | 841 | napi_disable(&fep->napi); |
Vitaly Bordug | 5b4b845 | 2006-08-14 23:00:30 -0700 | [diff] [blame] | 842 | phy_stop(fep->phydev); |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 843 | |
| 844 | spin_lock_irqsave(&fep->lock, flags); |
Vitaly Bordug | aa90f50 | 2007-09-18 20:05:27 +0400 | [diff] [blame] | 845 | spin_lock(&fep->tx_lock); |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 846 | (*fep->ops->stop)(dev); |
Vitaly Bordug | aa90f50 | 2007-09-18 20:05:27 +0400 | [diff] [blame] | 847 | spin_unlock(&fep->tx_lock); |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 848 | spin_unlock_irqrestore(&fep->lock, flags); |
| 849 | |
| 850 | /* release any irqs */ |
Vitaly Bordug | 5b4b845 | 2006-08-14 23:00:30 -0700 | [diff] [blame] | 851 | phy_disconnect(fep->phydev); |
| 852 | fep->phydev = NULL; |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 853 | fs_free_irq(dev, fep->interrupt); |
| 854 | |
| 855 | return 0; |
| 856 | } |
| 857 | |
| 858 | static struct net_device_stats *fs_enet_get_stats(struct net_device *dev) |
| 859 | { |
| 860 | struct fs_enet_private *fep = netdev_priv(dev); |
| 861 | return &fep->stats; |
| 862 | } |
| 863 | |
| 864 | /*************************************************************************/ |
| 865 | |
| 866 | static void fs_get_drvinfo(struct net_device *dev, |
| 867 | struct ethtool_drvinfo *info) |
| 868 | { |
| 869 | strcpy(info->driver, DRV_MODULE_NAME); |
| 870 | strcpy(info->version, DRV_MODULE_VERSION); |
| 871 | } |
| 872 | |
| 873 | static int fs_get_regs_len(struct net_device *dev) |
| 874 | { |
| 875 | struct fs_enet_private *fep = netdev_priv(dev); |
| 876 | |
| 877 | return (*fep->ops->get_regs_len)(dev); |
| 878 | } |
| 879 | |
| 880 | static void fs_get_regs(struct net_device *dev, struct ethtool_regs *regs, |
| 881 | void *p) |
| 882 | { |
| 883 | struct fs_enet_private *fep = netdev_priv(dev); |
| 884 | unsigned long flags; |
| 885 | int r, len; |
| 886 | |
| 887 | len = regs->len; |
| 888 | |
| 889 | spin_lock_irqsave(&fep->lock, flags); |
| 890 | r = (*fep->ops->get_regs)(dev, p, &len); |
| 891 | spin_unlock_irqrestore(&fep->lock, flags); |
| 892 | |
| 893 | if (r == 0) |
| 894 | regs->version = 0; |
| 895 | } |
| 896 | |
| 897 | static int fs_get_settings(struct net_device *dev, struct ethtool_cmd *cmd) |
| 898 | { |
| 899 | struct fs_enet_private *fep = netdev_priv(dev); |
Vitaly Bordug | 5b4b845 | 2006-08-14 23:00:30 -0700 | [diff] [blame] | 900 | return phy_ethtool_gset(fep->phydev, cmd); |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 901 | } |
| 902 | |
| 903 | static int fs_set_settings(struct net_device *dev, struct ethtool_cmd *cmd) |
| 904 | { |
| 905 | struct fs_enet_private *fep = netdev_priv(dev); |
Vitaly Bordug | 5b4b845 | 2006-08-14 23:00:30 -0700 | [diff] [blame] | 906 | phy_ethtool_sset(fep->phydev, cmd); |
| 907 | return 0; |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 908 | } |
| 909 | |
| 910 | static int fs_nway_reset(struct net_device *dev) |
| 911 | { |
Vitaly Bordug | 5b4b845 | 2006-08-14 23:00:30 -0700 | [diff] [blame] | 912 | return 0; |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 913 | } |
| 914 | |
| 915 | static u32 fs_get_msglevel(struct net_device *dev) |
| 916 | { |
| 917 | struct fs_enet_private *fep = netdev_priv(dev); |
| 918 | return fep->msg_enable; |
| 919 | } |
| 920 | |
| 921 | static void fs_set_msglevel(struct net_device *dev, u32 value) |
| 922 | { |
| 923 | struct fs_enet_private *fep = netdev_priv(dev); |
| 924 | fep->msg_enable = value; |
| 925 | } |
| 926 | |
Jeff Garzik | 7282d49 | 2006-09-13 14:30:00 -0400 | [diff] [blame] | 927 | static const struct ethtool_ops fs_ethtool_ops = { |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 928 | .get_drvinfo = fs_get_drvinfo, |
| 929 | .get_regs_len = fs_get_regs_len, |
| 930 | .get_settings = fs_get_settings, |
| 931 | .set_settings = fs_set_settings, |
| 932 | .nway_reset = fs_nway_reset, |
| 933 | .get_link = ethtool_op_get_link, |
| 934 | .get_msglevel = fs_get_msglevel, |
| 935 | .set_msglevel = fs_set_msglevel, |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 936 | .set_tx_csum = ethtool_op_set_tx_csum, /* local! */ |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 937 | .set_sg = ethtool_op_set_sg, |
| 938 | .get_regs = fs_get_regs, |
| 939 | }; |
| 940 | |
| 941 | static int fs_ioctl(struct net_device *dev, struct ifreq *rq, int cmd) |
| 942 | { |
| 943 | struct fs_enet_private *fep = netdev_priv(dev); |
| 944 | struct mii_ioctl_data *mii = (struct mii_ioctl_data *)&rq->ifr_data; |
| 945 | unsigned long flags; |
| 946 | int rc; |
| 947 | |
| 948 | if (!netif_running(dev)) |
| 949 | return -EINVAL; |
| 950 | |
| 951 | spin_lock_irqsave(&fep->lock, flags); |
Vitaly Bordug | 5b4b845 | 2006-08-14 23:00:30 -0700 | [diff] [blame] | 952 | rc = phy_mii_ioctl(fep->phydev, mii, cmd); |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 953 | spin_unlock_irqrestore(&fep->lock, flags); |
| 954 | return rc; |
| 955 | } |
| 956 | |
| 957 | extern int fs_mii_connect(struct net_device *dev); |
| 958 | extern void fs_mii_disconnect(struct net_device *dev); |
| 959 | |
Scott Wood | 976de6a | 2007-10-02 10:55:58 -0500 | [diff] [blame] | 960 | #ifndef CONFIG_PPC_CPM_NEW_BINDING |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 961 | static struct net_device *fs_init_instance(struct device *dev, |
Vitaly Bordug | 611a15a | 2006-09-21 22:38:05 +0400 | [diff] [blame] | 962 | struct fs_platform_info *fpi) |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 963 | { |
| 964 | struct net_device *ndev = NULL; |
| 965 | struct fs_enet_private *fep = NULL; |
| 966 | int privsize, i, r, err = 0, registered = 0; |
| 967 | |
Vitaly Bordug | 611a15a | 2006-09-21 22:38:05 +0400 | [diff] [blame] | 968 | fpi->fs_no = fs_get_id(fpi); |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 969 | /* guard */ |
| 970 | if ((unsigned int)fpi->fs_no >= FS_MAX_INDEX) |
| 971 | return ERR_PTR(-EINVAL); |
| 972 | |
| 973 | privsize = sizeof(*fep) + (sizeof(struct sk_buff **) * |
| 974 | (fpi->rx_ring + fpi->tx_ring)); |
| 975 | |
| 976 | ndev = alloc_etherdev(privsize); |
| 977 | if (!ndev) { |
| 978 | err = -ENOMEM; |
| 979 | goto err; |
| 980 | } |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 981 | |
| 982 | fep = netdev_priv(ndev); |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 983 | |
| 984 | fep->dev = dev; |
| 985 | dev_set_drvdata(dev, ndev); |
| 986 | fep->fpi = fpi; |
| 987 | if (fpi->init_ioports) |
Vitaly Bordug | d3465c9 | 2006-09-21 22:38:05 +0400 | [diff] [blame] | 988 | fpi->init_ioports((struct fs_platform_info *)fpi); |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 989 | |
| 990 | #ifdef CONFIG_FS_ENET_HAS_FEC |
| 991 | if (fs_get_fec_index(fpi->fs_no) >= 0) |
| 992 | fep->ops = &fs_fec_ops; |
| 993 | #endif |
| 994 | |
| 995 | #ifdef CONFIG_FS_ENET_HAS_SCC |
Scott Wood | 0fb300f | 2007-10-01 14:20:17 -0500 | [diff] [blame] | 996 | if (fs_get_scc_index(fpi->fs_no) >=0) |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 997 | fep->ops = &fs_scc_ops; |
| 998 | #endif |
| 999 | |
| 1000 | #ifdef CONFIG_FS_ENET_HAS_FCC |
| 1001 | if (fs_get_fcc_index(fpi->fs_no) >= 0) |
| 1002 | fep->ops = &fs_fcc_ops; |
| 1003 | #endif |
| 1004 | |
| 1005 | if (fep->ops == NULL) { |
| 1006 | printk(KERN_ERR DRV_MODULE_NAME |
| 1007 | ": %s No matching ops found (%d).\n", |
| 1008 | ndev->name, fpi->fs_no); |
| 1009 | err = -EINVAL; |
| 1010 | goto err; |
| 1011 | } |
| 1012 | |
| 1013 | r = (*fep->ops->setup_data)(ndev); |
| 1014 | if (r != 0) { |
| 1015 | printk(KERN_ERR DRV_MODULE_NAME |
| 1016 | ": %s setup_data failed\n", |
| 1017 | ndev->name); |
| 1018 | err = r; |
| 1019 | goto err; |
| 1020 | } |
| 1021 | |
| 1022 | /* point rx_skbuff, tx_skbuff */ |
| 1023 | fep->rx_skbuff = (struct sk_buff **)&fep[1]; |
| 1024 | fep->tx_skbuff = fep->rx_skbuff + fpi->rx_ring; |
| 1025 | |
| 1026 | /* init locks */ |
| 1027 | spin_lock_init(&fep->lock); |
| 1028 | spin_lock_init(&fep->tx_lock); |
| 1029 | |
| 1030 | /* |
Vitaly Bordug | 9b8ee8e | 2007-09-18 20:05:35 +0400 | [diff] [blame] | 1031 | * Set the Ethernet address. |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 1032 | */ |
| 1033 | for (i = 0; i < 6; i++) |
| 1034 | ndev->dev_addr[i] = fpi->macaddr[i]; |
Vitaly Bordug | 9b8ee8e | 2007-09-18 20:05:35 +0400 | [diff] [blame] | 1035 | |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 1036 | r = (*fep->ops->allocate_bd)(ndev); |
Vitaly Bordug | 9b8ee8e | 2007-09-18 20:05:35 +0400 | [diff] [blame] | 1037 | |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 1038 | if (fep->ring_base == NULL) { |
| 1039 | printk(KERN_ERR DRV_MODULE_NAME |
| 1040 | ": %s buffer descriptor alloc failed (%d).\n", ndev->name, r); |
| 1041 | err = r; |
| 1042 | goto err; |
| 1043 | } |
| 1044 | |
| 1045 | /* |
| 1046 | * Set receive and transmit descriptor base. |
| 1047 | */ |
| 1048 | fep->rx_bd_base = fep->ring_base; |
| 1049 | fep->tx_bd_base = fep->rx_bd_base + fpi->rx_ring; |
| 1050 | |
| 1051 | /* initialize ring size variables */ |
| 1052 | fep->tx_ring = fpi->tx_ring; |
| 1053 | fep->rx_ring = fpi->rx_ring; |
| 1054 | |
| 1055 | /* |
Vitaly Bordug | 9b8ee8e | 2007-09-18 20:05:35 +0400 | [diff] [blame] | 1056 | * The FEC Ethernet specific entries in the device structure. |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 1057 | */ |
| 1058 | ndev->open = fs_enet_open; |
| 1059 | ndev->hard_start_xmit = fs_enet_start_xmit; |
| 1060 | ndev->tx_timeout = fs_timeout; |
| 1061 | ndev->watchdog_timeo = 2 * HZ; |
| 1062 | ndev->stop = fs_enet_close; |
| 1063 | ndev->get_stats = fs_enet_get_stats; |
| 1064 | ndev->set_multicast_list = fs_set_multicast_list; |
Vitaly Bordug | 9b8ee8e | 2007-09-18 20:05:35 +0400 | [diff] [blame] | 1065 | |
| 1066 | #ifdef CONFIG_NET_POLL_CONTROLLER |
| 1067 | ndev->poll_controller = fs_enet_netpoll; |
| 1068 | #endif |
| 1069 | |
Stephen Hemminger | bea3348 | 2007-10-03 16:41:36 -0700 | [diff] [blame] | 1070 | netif_napi_add(ndev, &fep->napi, |
| 1071 | fs_enet_rx_napi, fpi->napi_weight); |
| 1072 | |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 1073 | ndev->ethtool_ops = &fs_ethtool_ops; |
| 1074 | ndev->do_ioctl = fs_ioctl; |
| 1075 | |
| 1076 | init_timer(&fep->phy_timer_list); |
| 1077 | |
| 1078 | netif_carrier_off(ndev); |
| 1079 | |
| 1080 | err = register_netdev(ndev); |
| 1081 | if (err != 0) { |
| 1082 | printk(KERN_ERR DRV_MODULE_NAME |
| 1083 | ": %s register_netdev failed.\n", ndev->name); |
| 1084 | goto err; |
| 1085 | } |
| 1086 | registered = 1; |
| 1087 | |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 1088 | |
| 1089 | return ndev; |
| 1090 | |
Scott Wood | 0fb300f | 2007-10-01 14:20:17 -0500 | [diff] [blame] | 1091 | err: |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 1092 | if (ndev != NULL) { |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 1093 | if (registered) |
| 1094 | unregister_netdev(ndev); |
| 1095 | |
| 1096 | if (fep != NULL) { |
| 1097 | (*fep->ops->free_bd)(ndev); |
| 1098 | (*fep->ops->cleanup_data)(ndev); |
| 1099 | } |
| 1100 | |
| 1101 | free_netdev(ndev); |
| 1102 | } |
| 1103 | |
| 1104 | dev_set_drvdata(dev, NULL); |
| 1105 | |
| 1106 | return ERR_PTR(err); |
| 1107 | } |
| 1108 | |
| 1109 | static int fs_cleanup_instance(struct net_device *ndev) |
| 1110 | { |
| 1111 | struct fs_enet_private *fep; |
| 1112 | const struct fs_platform_info *fpi; |
| 1113 | struct device *dev; |
| 1114 | |
| 1115 | if (ndev == NULL) |
| 1116 | return -EINVAL; |
| 1117 | |
| 1118 | fep = netdev_priv(ndev); |
| 1119 | if (fep == NULL) |
| 1120 | return -EINVAL; |
| 1121 | |
| 1122 | fpi = fep->fpi; |
| 1123 | |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 1124 | unregister_netdev(ndev); |
| 1125 | |
| 1126 | dma_free_coherent(fep->dev, (fpi->tx_ring + fpi->rx_ring) * sizeof(cbd_t), |
Scott Wood | 31a5bb0 | 2007-10-01 14:20:58 -0500 | [diff] [blame] | 1127 | (void __force *)fep->ring_base, fep->ring_mem_addr); |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 1128 | |
| 1129 | /* reset it */ |
| 1130 | (*fep->ops->cleanup_data)(ndev); |
| 1131 | |
| 1132 | dev = fep->dev; |
| 1133 | if (dev != NULL) { |
| 1134 | dev_set_drvdata(dev, NULL); |
| 1135 | fep->dev = NULL; |
| 1136 | } |
| 1137 | |
| 1138 | free_netdev(ndev); |
| 1139 | |
| 1140 | return 0; |
| 1141 | } |
Scott Wood | 976de6a | 2007-10-02 10:55:58 -0500 | [diff] [blame] | 1142 | #endif |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 1143 | |
| 1144 | /**************************************************************************************/ |
| 1145 | |
| 1146 | /* handy pointer to the immap */ |
Scott Wood | 31a5bb0 | 2007-10-01 14:20:58 -0500 | [diff] [blame] | 1147 | void __iomem *fs_enet_immap = NULL; |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 1148 | |
| 1149 | static int setup_immap(void) |
| 1150 | { |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 1151 | #ifdef CONFIG_CPM1 |
Scott Wood | 976de6a | 2007-10-02 10:55:58 -0500 | [diff] [blame] | 1152 | fs_enet_immap = ioremap(IMAP_ADDR, 0x4000); |
| 1153 | WARN_ON(!fs_enet_immap); |
| 1154 | #elif defined(CONFIG_CPM2) |
| 1155 | fs_enet_immap = cpm2_immr; |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 1156 | #endif |
| 1157 | |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 1158 | return 0; |
| 1159 | } |
| 1160 | |
| 1161 | static void cleanup_immap(void) |
| 1162 | { |
Scott Wood | 976de6a | 2007-10-02 10:55:58 -0500 | [diff] [blame] | 1163 | #if defined(CONFIG_CPM1) |
| 1164 | iounmap(fs_enet_immap); |
| 1165 | #endif |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 1166 | } |
| 1167 | |
| 1168 | /**************************************************************************************/ |
| 1169 | |
Scott Wood | 976de6a | 2007-10-02 10:55:58 -0500 | [diff] [blame] | 1170 | #ifdef CONFIG_PPC_CPM_NEW_BINDING |
| 1171 | static int __devinit find_phy(struct device_node *np, |
| 1172 | struct fs_platform_info *fpi) |
| 1173 | { |
| 1174 | struct device_node *phynode, *mdionode; |
| 1175 | struct resource res; |
| 1176 | int ret = 0, len; |
Jochen Friedrich | 70f782e | 2007-12-18 16:25:43 +0100 | [diff] [blame^] | 1177 | const u32 *data; |
Scott Wood | 976de6a | 2007-10-02 10:55:58 -0500 | [diff] [blame] | 1178 | |
Jochen Friedrich | 70f782e | 2007-12-18 16:25:43 +0100 | [diff] [blame^] | 1179 | data = of_get_property(np, "fixed-link", NULL); |
| 1180 | if (data) { |
| 1181 | snprintf(fpi->bus_id, 16, PHY_ID_FMT, 0, *data); |
| 1182 | return 0; |
| 1183 | } |
| 1184 | |
| 1185 | data = of_get_property(np, "phy-handle", &len); |
Scott Wood | 976de6a | 2007-10-02 10:55:58 -0500 | [diff] [blame] | 1186 | if (!data || len != 4) |
| 1187 | return -EINVAL; |
| 1188 | |
| 1189 | phynode = of_find_node_by_phandle(*data); |
| 1190 | if (!phynode) |
| 1191 | return -EINVAL; |
| 1192 | |
| 1193 | mdionode = of_get_parent(phynode); |
| 1194 | if (!mdionode) |
| 1195 | goto out_put_phy; |
| 1196 | |
| 1197 | ret = of_address_to_resource(mdionode, 0, &res); |
| 1198 | if (ret) |
| 1199 | goto out_put_mdio; |
| 1200 | |
| 1201 | data = of_get_property(phynode, "reg", &len); |
| 1202 | if (!data || len != 4) |
| 1203 | goto out_put_mdio; |
| 1204 | |
| 1205 | snprintf(fpi->bus_id, 16, PHY_ID_FMT, res.start, *data); |
| 1206 | |
| 1207 | out_put_mdio: |
| 1208 | of_node_put(mdionode); |
| 1209 | out_put_phy: |
| 1210 | of_node_put(phynode); |
| 1211 | return ret; |
| 1212 | } |
| 1213 | |
| 1214 | #ifdef CONFIG_FS_ENET_HAS_FEC |
| 1215 | #define IS_FEC(match) ((match)->data == &fs_fec_ops) |
| 1216 | #else |
| 1217 | #define IS_FEC(match) 0 |
| 1218 | #endif |
| 1219 | |
| 1220 | static int __devinit fs_enet_probe(struct of_device *ofdev, |
| 1221 | const struct of_device_id *match) |
| 1222 | { |
| 1223 | struct net_device *ndev; |
| 1224 | struct fs_enet_private *fep; |
| 1225 | struct fs_platform_info *fpi; |
| 1226 | const u32 *data; |
| 1227 | const u8 *mac_addr; |
| 1228 | int privsize, len, ret = -ENODEV; |
| 1229 | |
| 1230 | fpi = kzalloc(sizeof(*fpi), GFP_KERNEL); |
| 1231 | if (!fpi) |
| 1232 | return -ENOMEM; |
| 1233 | |
| 1234 | if (!IS_FEC(match)) { |
| 1235 | data = of_get_property(ofdev->node, "fsl,cpm-command", &len); |
| 1236 | if (!data || len != 4) |
| 1237 | goto out_free_fpi; |
| 1238 | |
| 1239 | fpi->cp_command = *data; |
| 1240 | } |
| 1241 | |
| 1242 | fpi->rx_ring = 32; |
| 1243 | fpi->tx_ring = 32; |
| 1244 | fpi->rx_copybreak = 240; |
Scott Wood | f860f49 | 2007-10-17 12:42:43 -0500 | [diff] [blame] | 1245 | fpi->use_napi = 1; |
Scott Wood | 976de6a | 2007-10-02 10:55:58 -0500 | [diff] [blame] | 1246 | fpi->napi_weight = 17; |
| 1247 | |
| 1248 | ret = find_phy(ofdev->node, fpi); |
| 1249 | if (ret) |
| 1250 | goto out_free_fpi; |
| 1251 | |
| 1252 | privsize = sizeof(*fep) + |
| 1253 | sizeof(struct sk_buff **) * |
| 1254 | (fpi->rx_ring + fpi->tx_ring); |
| 1255 | |
| 1256 | ndev = alloc_etherdev(privsize); |
| 1257 | if (!ndev) { |
| 1258 | ret = -ENOMEM; |
| 1259 | goto out_free_fpi; |
| 1260 | } |
| 1261 | |
Scott Wood | 976de6a | 2007-10-02 10:55:58 -0500 | [diff] [blame] | 1262 | dev_set_drvdata(&ofdev->dev, ndev); |
| 1263 | |
| 1264 | fep = netdev_priv(ndev); |
| 1265 | fep->dev = &ofdev->dev; |
Scott Wood | f860f49 | 2007-10-17 12:42:43 -0500 | [diff] [blame] | 1266 | fep->ndev = ndev; |
Scott Wood | 976de6a | 2007-10-02 10:55:58 -0500 | [diff] [blame] | 1267 | fep->fpi = fpi; |
| 1268 | fep->ops = match->data; |
| 1269 | |
| 1270 | ret = fep->ops->setup_data(ndev); |
| 1271 | if (ret) |
| 1272 | goto out_free_dev; |
| 1273 | |
| 1274 | fep->rx_skbuff = (struct sk_buff **)&fep[1]; |
| 1275 | fep->tx_skbuff = fep->rx_skbuff + fpi->rx_ring; |
| 1276 | |
| 1277 | spin_lock_init(&fep->lock); |
| 1278 | spin_lock_init(&fep->tx_lock); |
| 1279 | |
| 1280 | mac_addr = of_get_mac_address(ofdev->node); |
| 1281 | if (mac_addr) |
| 1282 | memcpy(ndev->dev_addr, mac_addr, 6); |
| 1283 | |
| 1284 | ret = fep->ops->allocate_bd(ndev); |
| 1285 | if (ret) |
| 1286 | goto out_cleanup_data; |
| 1287 | |
| 1288 | fep->rx_bd_base = fep->ring_base; |
| 1289 | fep->tx_bd_base = fep->rx_bd_base + fpi->rx_ring; |
| 1290 | |
| 1291 | fep->tx_ring = fpi->tx_ring; |
| 1292 | fep->rx_ring = fpi->rx_ring; |
| 1293 | |
| 1294 | ndev->open = fs_enet_open; |
| 1295 | ndev->hard_start_xmit = fs_enet_start_xmit; |
| 1296 | ndev->tx_timeout = fs_timeout; |
| 1297 | ndev->watchdog_timeo = 2 * HZ; |
| 1298 | ndev->stop = fs_enet_close; |
| 1299 | ndev->get_stats = fs_enet_get_stats; |
| 1300 | ndev->set_multicast_list = fs_set_multicast_list; |
Scott Wood | f860f49 | 2007-10-17 12:42:43 -0500 | [diff] [blame] | 1301 | |
| 1302 | if (fpi->use_napi) |
| 1303 | netif_napi_add(ndev, &fep->napi, fs_enet_rx_napi, |
| 1304 | fpi->napi_weight); |
| 1305 | |
Scott Wood | 976de6a | 2007-10-02 10:55:58 -0500 | [diff] [blame] | 1306 | ndev->ethtool_ops = &fs_ethtool_ops; |
| 1307 | ndev->do_ioctl = fs_ioctl; |
| 1308 | |
| 1309 | init_timer(&fep->phy_timer_list); |
| 1310 | |
| 1311 | netif_carrier_off(ndev); |
| 1312 | |
| 1313 | ret = register_netdev(ndev); |
| 1314 | if (ret) |
| 1315 | goto out_free_bd; |
| 1316 | |
| 1317 | printk(KERN_INFO "%s: fs_enet: %02x:%02x:%02x:%02x:%02x:%02x\n", |
| 1318 | ndev->name, |
| 1319 | ndev->dev_addr[0], ndev->dev_addr[1], ndev->dev_addr[2], |
| 1320 | ndev->dev_addr[3], ndev->dev_addr[4], ndev->dev_addr[5]); |
| 1321 | |
| 1322 | return 0; |
| 1323 | |
| 1324 | out_free_bd: |
| 1325 | fep->ops->free_bd(ndev); |
| 1326 | out_cleanup_data: |
| 1327 | fep->ops->cleanup_data(ndev); |
| 1328 | out_free_dev: |
| 1329 | free_netdev(ndev); |
| 1330 | dev_set_drvdata(&ofdev->dev, NULL); |
| 1331 | out_free_fpi: |
| 1332 | kfree(fpi); |
| 1333 | return ret; |
| 1334 | } |
| 1335 | |
| 1336 | static int fs_enet_remove(struct of_device *ofdev) |
| 1337 | { |
| 1338 | struct net_device *ndev = dev_get_drvdata(&ofdev->dev); |
| 1339 | struct fs_enet_private *fep = netdev_priv(ndev); |
| 1340 | |
| 1341 | unregister_netdev(ndev); |
| 1342 | |
| 1343 | fep->ops->free_bd(ndev); |
| 1344 | fep->ops->cleanup_data(ndev); |
| 1345 | dev_set_drvdata(fep->dev, NULL); |
| 1346 | |
| 1347 | free_netdev(ndev); |
| 1348 | return 0; |
| 1349 | } |
| 1350 | |
| 1351 | static struct of_device_id fs_enet_match[] = { |
| 1352 | #ifdef CONFIG_FS_ENET_HAS_SCC |
| 1353 | { |
| 1354 | .compatible = "fsl,cpm1-scc-enet", |
| 1355 | .data = (void *)&fs_scc_ops, |
| 1356 | }, |
| 1357 | #endif |
| 1358 | #ifdef CONFIG_FS_ENET_HAS_FCC |
| 1359 | { |
| 1360 | .compatible = "fsl,cpm2-fcc-enet", |
| 1361 | .data = (void *)&fs_fcc_ops, |
| 1362 | }, |
| 1363 | #endif |
| 1364 | #ifdef CONFIG_FS_ENET_HAS_FEC |
| 1365 | { |
| 1366 | .compatible = "fsl,pq1-fec-enet", |
| 1367 | .data = (void *)&fs_fec_ops, |
| 1368 | }, |
| 1369 | #endif |
| 1370 | {} |
| 1371 | }; |
| 1372 | |
| 1373 | static struct of_platform_driver fs_enet_driver = { |
| 1374 | .name = "fs_enet", |
| 1375 | .match_table = fs_enet_match, |
| 1376 | .probe = fs_enet_probe, |
| 1377 | .remove = fs_enet_remove, |
| 1378 | }; |
| 1379 | |
| 1380 | static int __init fs_init(void) |
| 1381 | { |
| 1382 | int r = setup_immap(); |
| 1383 | if (r != 0) |
| 1384 | return r; |
| 1385 | |
| 1386 | r = of_register_platform_driver(&fs_enet_driver); |
| 1387 | if (r != 0) |
| 1388 | goto out; |
| 1389 | |
| 1390 | return 0; |
| 1391 | |
| 1392 | out: |
| 1393 | cleanup_immap(); |
| 1394 | return r; |
| 1395 | } |
| 1396 | |
| 1397 | static void __exit fs_cleanup(void) |
| 1398 | { |
| 1399 | of_unregister_platform_driver(&fs_enet_driver); |
| 1400 | cleanup_immap(); |
| 1401 | } |
| 1402 | #else |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 1403 | static int __devinit fs_enet_probe(struct device *dev) |
| 1404 | { |
| 1405 | struct net_device *ndev; |
| 1406 | |
| 1407 | /* no fixup - no device */ |
| 1408 | if (dev->platform_data == NULL) { |
| 1409 | printk(KERN_INFO "fs_enet: " |
| 1410 | "probe called with no platform data; " |
| 1411 | "remove unused devices\n"); |
| 1412 | return -ENODEV; |
| 1413 | } |
| 1414 | |
| 1415 | ndev = fs_init_instance(dev, dev->platform_data); |
| 1416 | if (IS_ERR(ndev)) |
| 1417 | return PTR_ERR(ndev); |
| 1418 | return 0; |
| 1419 | } |
| 1420 | |
| 1421 | static int fs_enet_remove(struct device *dev) |
| 1422 | { |
| 1423 | return fs_cleanup_instance(dev_get_drvdata(dev)); |
| 1424 | } |
| 1425 | |
| 1426 | static struct device_driver fs_enet_fec_driver = { |
| 1427 | .name = "fsl-cpm-fec", |
| 1428 | .bus = &platform_bus_type, |
| 1429 | .probe = fs_enet_probe, |
| 1430 | .remove = fs_enet_remove, |
| 1431 | #ifdef CONFIG_PM |
| 1432 | /* .suspend = fs_enet_suspend, TODO */ |
| 1433 | /* .resume = fs_enet_resume, TODO */ |
| 1434 | #endif |
| 1435 | }; |
| 1436 | |
| 1437 | static struct device_driver fs_enet_scc_driver = { |
| 1438 | .name = "fsl-cpm-scc", |
| 1439 | .bus = &platform_bus_type, |
| 1440 | .probe = fs_enet_probe, |
| 1441 | .remove = fs_enet_remove, |
| 1442 | #ifdef CONFIG_PM |
| 1443 | /* .suspend = fs_enet_suspend, TODO */ |
| 1444 | /* .resume = fs_enet_resume, TODO */ |
| 1445 | #endif |
| 1446 | }; |
| 1447 | |
| 1448 | static struct device_driver fs_enet_fcc_driver = { |
| 1449 | .name = "fsl-cpm-fcc", |
| 1450 | .bus = &platform_bus_type, |
| 1451 | .probe = fs_enet_probe, |
| 1452 | .remove = fs_enet_remove, |
| 1453 | #ifdef CONFIG_PM |
| 1454 | /* .suspend = fs_enet_suspend, TODO */ |
| 1455 | /* .resume = fs_enet_resume, TODO */ |
| 1456 | #endif |
| 1457 | }; |
| 1458 | |
| 1459 | static int __init fs_init(void) |
| 1460 | { |
| 1461 | int r; |
| 1462 | |
| 1463 | printk(KERN_INFO |
| 1464 | "%s", version); |
| 1465 | |
| 1466 | r = setup_immap(); |
| 1467 | if (r != 0) |
| 1468 | return r; |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 1469 | |
Vitaly Bordug | 5b4b845 | 2006-08-14 23:00:30 -0700 | [diff] [blame] | 1470 | #ifdef CONFIG_FS_ENET_HAS_FCC |
| 1471 | /* let's insert mii stuff */ |
| 1472 | r = fs_enet_mdio_bb_init(); |
| 1473 | |
| 1474 | if (r != 0) { |
| 1475 | printk(KERN_ERR DRV_MODULE_NAME |
| 1476 | "BB PHY init failed.\n"); |
| 1477 | return r; |
| 1478 | } |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 1479 | r = driver_register(&fs_enet_fcc_driver); |
| 1480 | if (r != 0) |
| 1481 | goto err; |
Vitaly Bordug | 5b4b845 | 2006-08-14 23:00:30 -0700 | [diff] [blame] | 1482 | #endif |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 1483 | |
Vitaly Bordug | 5b4b845 | 2006-08-14 23:00:30 -0700 | [diff] [blame] | 1484 | #ifdef CONFIG_FS_ENET_HAS_FEC |
| 1485 | r = fs_enet_mdio_fec_init(); |
| 1486 | if (r != 0) { |
| 1487 | printk(KERN_ERR DRV_MODULE_NAME |
| 1488 | "FEC PHY init failed.\n"); |
| 1489 | return r; |
| 1490 | } |
| 1491 | |
| 1492 | r = driver_register(&fs_enet_fec_driver); |
| 1493 | if (r != 0) |
| 1494 | goto err; |
| 1495 | #endif |
| 1496 | |
| 1497 | #ifdef CONFIG_FS_ENET_HAS_SCC |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 1498 | r = driver_register(&fs_enet_scc_driver); |
| 1499 | if (r != 0) |
| 1500 | goto err; |
Vitaly Bordug | 5b4b845 | 2006-08-14 23:00:30 -0700 | [diff] [blame] | 1501 | #endif |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 1502 | |
| 1503 | return 0; |
| 1504 | err: |
| 1505 | cleanup_immap(); |
| 1506 | return r; |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 1507 | } |
| 1508 | |
| 1509 | static void __exit fs_cleanup(void) |
| 1510 | { |
| 1511 | driver_unregister(&fs_enet_fec_driver); |
| 1512 | driver_unregister(&fs_enet_fcc_driver); |
| 1513 | driver_unregister(&fs_enet_scc_driver); |
| 1514 | cleanup_immap(); |
| 1515 | } |
Scott Wood | 976de6a | 2007-10-02 10:55:58 -0500 | [diff] [blame] | 1516 | #endif |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 1517 | |
Vitaly Bordug | 9b8ee8e | 2007-09-18 20:05:35 +0400 | [diff] [blame] | 1518 | #ifdef CONFIG_NET_POLL_CONTROLLER |
| 1519 | static void fs_enet_netpoll(struct net_device *dev) |
| 1520 | { |
| 1521 | disable_irq(dev->irq); |
| 1522 | fs_enet_interrupt(dev->irq, dev, NULL); |
| 1523 | enable_irq(dev->irq); |
| 1524 | } |
| 1525 | #endif |
| 1526 | |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 1527 | /**************************************************************************************/ |
| 1528 | |
| 1529 | module_init(fs_init); |
| 1530 | module_exit(fs_cleanup); |