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 | |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 99 | /* |
| 100 | * First, grab all of the stats for the incoming packet. |
| 101 | * These get messed up if we get called due to a busy condition. |
| 102 | */ |
| 103 | bdp = fep->cur_rx; |
| 104 | |
| 105 | /* clear RX status bits for napi*/ |
| 106 | (*fep->ops->napi_clear_rx_event)(dev); |
| 107 | |
| 108 | while (((sc = CBDR_SC(bdp)) & BD_ENET_RX_EMPTY) == 0) { |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 109 | curidx = bdp - fep->rx_bd_base; |
| 110 | |
| 111 | /* |
| 112 | * Since we have allocated space to hold a complete frame, |
| 113 | * the last indicator should be set. |
| 114 | */ |
| 115 | if ((sc & BD_ENET_RX_LAST) == 0) |
| 116 | printk(KERN_WARNING DRV_MODULE_NAME |
| 117 | ": %s rcv is not +last\n", |
| 118 | dev->name); |
| 119 | |
| 120 | /* |
Vitaly Bordug | 9b8ee8e | 2007-09-18 20:05:35 +0400 | [diff] [blame] | 121 | * Check for errors. |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 122 | */ |
| 123 | if (sc & (BD_ENET_RX_LG | BD_ENET_RX_SH | BD_ENET_RX_CL | |
| 124 | BD_ENET_RX_NO | BD_ENET_RX_CR | BD_ENET_RX_OV)) { |
| 125 | fep->stats.rx_errors++; |
| 126 | /* Frame too long or too short. */ |
| 127 | if (sc & (BD_ENET_RX_LG | BD_ENET_RX_SH)) |
| 128 | fep->stats.rx_length_errors++; |
| 129 | /* Frame alignment */ |
| 130 | if (sc & (BD_ENET_RX_NO | BD_ENET_RX_CL)) |
| 131 | fep->stats.rx_frame_errors++; |
| 132 | /* CRC Error */ |
| 133 | if (sc & BD_ENET_RX_CR) |
| 134 | fep->stats.rx_crc_errors++; |
| 135 | /* FIFO overrun */ |
| 136 | if (sc & BD_ENET_RX_OV) |
| 137 | fep->stats.rx_crc_errors++; |
| 138 | |
| 139 | skb = fep->rx_skbuff[curidx]; |
| 140 | |
Pantelis Antoniou | 34e30d6 | 2005-10-30 01:22:40 +0300 | [diff] [blame] | 141 | dma_unmap_single(fep->dev, CBDR_BUFADDR(bdp), |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 142 | L1_CACHE_ALIGN(PKT_MAXBUF_SIZE), |
| 143 | DMA_FROM_DEVICE); |
| 144 | |
| 145 | skbn = skb; |
| 146 | |
| 147 | } else { |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 148 | skb = fep->rx_skbuff[curidx]; |
| 149 | |
Pantelis Antoniou | 34e30d6 | 2005-10-30 01:22:40 +0300 | [diff] [blame] | 150 | dma_unmap_single(fep->dev, CBDR_BUFADDR(bdp), |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 151 | L1_CACHE_ALIGN(PKT_MAXBUF_SIZE), |
| 152 | DMA_FROM_DEVICE); |
| 153 | |
| 154 | /* |
| 155 | * Process the incoming frame. |
| 156 | */ |
| 157 | fep->stats.rx_packets++; |
| 158 | pkt_len = CBDR_DATLEN(bdp) - 4; /* remove CRC */ |
| 159 | fep->stats.rx_bytes += pkt_len + 4; |
| 160 | |
| 161 | if (pkt_len <= fpi->rx_copybreak) { |
| 162 | /* +2 to make IP header L1 cache aligned */ |
| 163 | skbn = dev_alloc_skb(pkt_len + 2); |
| 164 | if (skbn != NULL) { |
| 165 | skb_reserve(skbn, 2); /* align IP header */ |
Arnaldo Carvalho de Melo | d626f62 | 2007-03-27 18:55:52 -0300 | [diff] [blame] | 166 | skb_copy_from_linear_data(skb, |
| 167 | skbn->data, pkt_len); |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 168 | /* swap */ |
| 169 | skbt = skb; |
| 170 | skb = skbn; |
| 171 | skbn = skbt; |
| 172 | } |
Scott Wood | 0d0d9c1 | 2007-10-01 14:20:52 -0500 | [diff] [blame] | 173 | } else { |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 174 | skbn = dev_alloc_skb(ENET_RX_FRSIZE); |
| 175 | |
Scott Wood | 0d0d9c1 | 2007-10-01 14:20:52 -0500 | [diff] [blame] | 176 | if (skbn) |
| 177 | skb_align(skbn, ENET_RX_ALIGN); |
| 178 | } |
| 179 | |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 180 | if (skbn != NULL) { |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 181 | skb_put(skb, pkt_len); /* Make room */ |
| 182 | skb->protocol = eth_type_trans(skb, dev); |
| 183 | received++; |
| 184 | netif_receive_skb(skb); |
| 185 | } else { |
| 186 | printk(KERN_WARNING DRV_MODULE_NAME |
| 187 | ": %s Memory squeeze, dropping packet.\n", |
| 188 | dev->name); |
| 189 | fep->stats.rx_dropped++; |
| 190 | skbn = skb; |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | fep->rx_skbuff[curidx] = skbn; |
| 195 | CBDW_BUFADDR(bdp, dma_map_single(fep->dev, skbn->data, |
| 196 | L1_CACHE_ALIGN(PKT_MAXBUF_SIZE), |
| 197 | DMA_FROM_DEVICE)); |
| 198 | CBDW_DATLEN(bdp, 0); |
| 199 | CBDW_SC(bdp, (sc & ~BD_ENET_RX_STATS) | BD_ENET_RX_EMPTY); |
| 200 | |
| 201 | /* |
Vitaly Bordug | 9b8ee8e | 2007-09-18 20:05:35 +0400 | [diff] [blame] | 202 | * Update BD pointer to next entry. |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 203 | */ |
| 204 | if ((sc & BD_ENET_RX_WRAP) == 0) |
| 205 | bdp++; |
| 206 | else |
| 207 | bdp = fep->rx_bd_base; |
| 208 | |
| 209 | (*fep->ops->rx_bd_done)(dev); |
Stephen Hemminger | bea3348 | 2007-10-03 16:41:36 -0700 | [diff] [blame] | 210 | |
| 211 | if (received >= budget) |
| 212 | break; |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | fep->cur_rx = bdp; |
| 216 | |
Scott Wood | f860f49 | 2007-10-17 12:42:43 -0500 | [diff] [blame] | 217 | if (received < budget) { |
Stephen Hemminger | bea3348 | 2007-10-03 16:41:36 -0700 | [diff] [blame] | 218 | /* done */ |
| 219 | netif_rx_complete(dev, napi); |
| 220 | (*fep->ops->napi_enable_rx)(dev); |
| 221 | } |
| 222 | return received; |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | /* non NAPI receive function */ |
| 226 | static int fs_enet_rx_non_napi(struct net_device *dev) |
| 227 | { |
| 228 | struct fs_enet_private *fep = netdev_priv(dev); |
| 229 | const struct fs_platform_info *fpi = fep->fpi; |
Scott Wood | 31a5bb0 | 2007-10-01 14:20:58 -0500 | [diff] [blame] | 230 | cbd_t __iomem *bdp; |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 231 | struct sk_buff *skb, *skbn, *skbt; |
| 232 | int received = 0; |
| 233 | u16 pkt_len, sc; |
| 234 | int curidx; |
| 235 | /* |
| 236 | * First, grab all of the stats for the incoming packet. |
| 237 | * These get messed up if we get called due to a busy condition. |
| 238 | */ |
| 239 | bdp = fep->cur_rx; |
| 240 | |
| 241 | while (((sc = CBDR_SC(bdp)) & BD_ENET_RX_EMPTY) == 0) { |
| 242 | |
| 243 | curidx = bdp - fep->rx_bd_base; |
| 244 | |
| 245 | /* |
| 246 | * Since we have allocated space to hold a complete frame, |
| 247 | * the last indicator should be set. |
| 248 | */ |
| 249 | if ((sc & BD_ENET_RX_LAST) == 0) |
| 250 | printk(KERN_WARNING DRV_MODULE_NAME |
| 251 | ": %s rcv is not +last\n", |
| 252 | dev->name); |
| 253 | |
| 254 | /* |
Vitaly Bordug | 9b8ee8e | 2007-09-18 20:05:35 +0400 | [diff] [blame] | 255 | * Check for errors. |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 256 | */ |
| 257 | if (sc & (BD_ENET_RX_LG | BD_ENET_RX_SH | BD_ENET_RX_CL | |
| 258 | BD_ENET_RX_NO | BD_ENET_RX_CR | BD_ENET_RX_OV)) { |
| 259 | fep->stats.rx_errors++; |
| 260 | /* Frame too long or too short. */ |
| 261 | if (sc & (BD_ENET_RX_LG | BD_ENET_RX_SH)) |
| 262 | fep->stats.rx_length_errors++; |
| 263 | /* Frame alignment */ |
| 264 | if (sc & (BD_ENET_RX_NO | BD_ENET_RX_CL)) |
| 265 | fep->stats.rx_frame_errors++; |
| 266 | /* CRC Error */ |
| 267 | if (sc & BD_ENET_RX_CR) |
| 268 | fep->stats.rx_crc_errors++; |
| 269 | /* FIFO overrun */ |
| 270 | if (sc & BD_ENET_RX_OV) |
| 271 | fep->stats.rx_crc_errors++; |
| 272 | |
| 273 | skb = fep->rx_skbuff[curidx]; |
| 274 | |
Pantelis Antoniou | 34e30d6 | 2005-10-30 01:22:40 +0300 | [diff] [blame] | 275 | dma_unmap_single(fep->dev, CBDR_BUFADDR(bdp), |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 276 | L1_CACHE_ALIGN(PKT_MAXBUF_SIZE), |
| 277 | DMA_FROM_DEVICE); |
| 278 | |
| 279 | skbn = skb; |
| 280 | |
| 281 | } else { |
| 282 | |
| 283 | skb = fep->rx_skbuff[curidx]; |
| 284 | |
Pantelis Antoniou | 34e30d6 | 2005-10-30 01:22:40 +0300 | [diff] [blame] | 285 | dma_unmap_single(fep->dev, CBDR_BUFADDR(bdp), |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 286 | L1_CACHE_ALIGN(PKT_MAXBUF_SIZE), |
| 287 | DMA_FROM_DEVICE); |
| 288 | |
| 289 | /* |
| 290 | * Process the incoming frame. |
| 291 | */ |
| 292 | fep->stats.rx_packets++; |
| 293 | pkt_len = CBDR_DATLEN(bdp) - 4; /* remove CRC */ |
| 294 | fep->stats.rx_bytes += pkt_len + 4; |
| 295 | |
| 296 | if (pkt_len <= fpi->rx_copybreak) { |
| 297 | /* +2 to make IP header L1 cache aligned */ |
| 298 | skbn = dev_alloc_skb(pkt_len + 2); |
| 299 | if (skbn != NULL) { |
| 300 | skb_reserve(skbn, 2); /* align IP header */ |
Arnaldo Carvalho de Melo | d626f62 | 2007-03-27 18:55:52 -0300 | [diff] [blame] | 301 | skb_copy_from_linear_data(skb, |
| 302 | skbn->data, pkt_len); |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 303 | /* swap */ |
| 304 | skbt = skb; |
| 305 | skb = skbn; |
| 306 | skbn = skbt; |
| 307 | } |
Scott Wood | 0d0d9c1 | 2007-10-01 14:20:52 -0500 | [diff] [blame] | 308 | } else { |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 309 | skbn = dev_alloc_skb(ENET_RX_FRSIZE); |
| 310 | |
Scott Wood | 0d0d9c1 | 2007-10-01 14:20:52 -0500 | [diff] [blame] | 311 | if (skbn) |
| 312 | skb_align(skbn, ENET_RX_ALIGN); |
| 313 | } |
| 314 | |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 315 | if (skbn != NULL) { |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 316 | skb_put(skb, pkt_len); /* Make room */ |
| 317 | skb->protocol = eth_type_trans(skb, dev); |
| 318 | received++; |
| 319 | netif_rx(skb); |
| 320 | } else { |
| 321 | printk(KERN_WARNING DRV_MODULE_NAME |
| 322 | ": %s Memory squeeze, dropping packet.\n", |
| 323 | dev->name); |
| 324 | fep->stats.rx_dropped++; |
| 325 | skbn = skb; |
| 326 | } |
| 327 | } |
| 328 | |
| 329 | fep->rx_skbuff[curidx] = skbn; |
| 330 | CBDW_BUFADDR(bdp, dma_map_single(fep->dev, skbn->data, |
| 331 | L1_CACHE_ALIGN(PKT_MAXBUF_SIZE), |
| 332 | DMA_FROM_DEVICE)); |
| 333 | CBDW_DATLEN(bdp, 0); |
| 334 | CBDW_SC(bdp, (sc & ~BD_ENET_RX_STATS) | BD_ENET_RX_EMPTY); |
| 335 | |
| 336 | /* |
Vitaly Bordug | 9b8ee8e | 2007-09-18 20:05:35 +0400 | [diff] [blame] | 337 | * Update BD pointer to next entry. |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 338 | */ |
| 339 | if ((sc & BD_ENET_RX_WRAP) == 0) |
| 340 | bdp++; |
| 341 | else |
| 342 | bdp = fep->rx_bd_base; |
| 343 | |
| 344 | (*fep->ops->rx_bd_done)(dev); |
| 345 | } |
| 346 | |
| 347 | fep->cur_rx = bdp; |
| 348 | |
| 349 | return 0; |
| 350 | } |
| 351 | |
| 352 | static void fs_enet_tx(struct net_device *dev) |
| 353 | { |
| 354 | struct fs_enet_private *fep = netdev_priv(dev); |
Scott Wood | 31a5bb0 | 2007-10-01 14:20:58 -0500 | [diff] [blame] | 355 | cbd_t __iomem *bdp; |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 356 | struct sk_buff *skb; |
| 357 | int dirtyidx, do_wake, do_restart; |
| 358 | u16 sc; |
| 359 | |
Vitaly Bordug | aa90f50 | 2007-09-18 20:05:27 +0400 | [diff] [blame] | 360 | spin_lock(&fep->tx_lock); |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 361 | bdp = fep->dirty_tx; |
| 362 | |
| 363 | do_wake = do_restart = 0; |
| 364 | while (((sc = CBDR_SC(bdp)) & BD_ENET_TX_READY) == 0) { |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 365 | dirtyidx = bdp - fep->tx_bd_base; |
| 366 | |
| 367 | if (fep->tx_free == fep->tx_ring) |
| 368 | break; |
| 369 | |
| 370 | skb = fep->tx_skbuff[dirtyidx]; |
| 371 | |
| 372 | /* |
Vitaly Bordug | 9b8ee8e | 2007-09-18 20:05:35 +0400 | [diff] [blame] | 373 | * Check for errors. |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 374 | */ |
| 375 | if (sc & (BD_ENET_TX_HB | BD_ENET_TX_LC | |
| 376 | BD_ENET_TX_RL | BD_ENET_TX_UN | BD_ENET_TX_CSL)) { |
| 377 | |
| 378 | if (sc & BD_ENET_TX_HB) /* No heartbeat */ |
| 379 | fep->stats.tx_heartbeat_errors++; |
| 380 | if (sc & BD_ENET_TX_LC) /* Late collision */ |
| 381 | fep->stats.tx_window_errors++; |
| 382 | if (sc & BD_ENET_TX_RL) /* Retrans limit */ |
| 383 | fep->stats.tx_aborted_errors++; |
| 384 | if (sc & BD_ENET_TX_UN) /* Underrun */ |
| 385 | fep->stats.tx_fifo_errors++; |
| 386 | if (sc & BD_ENET_TX_CSL) /* Carrier lost */ |
| 387 | fep->stats.tx_carrier_errors++; |
| 388 | |
| 389 | if (sc & (BD_ENET_TX_LC | BD_ENET_TX_RL | BD_ENET_TX_UN)) { |
| 390 | fep->stats.tx_errors++; |
| 391 | do_restart = 1; |
| 392 | } |
| 393 | } else |
| 394 | fep->stats.tx_packets++; |
| 395 | |
| 396 | if (sc & BD_ENET_TX_READY) |
| 397 | printk(KERN_WARNING DRV_MODULE_NAME |
| 398 | ": %s HEY! Enet xmit interrupt and TX_READY.\n", |
| 399 | dev->name); |
| 400 | |
| 401 | /* |
| 402 | * Deferred means some collisions occurred during transmit, |
| 403 | * but we eventually sent the packet OK. |
| 404 | */ |
| 405 | if (sc & BD_ENET_TX_DEF) |
| 406 | fep->stats.collisions++; |
| 407 | |
| 408 | /* unmap */ |
Pantelis Antoniou | 34e30d6 | 2005-10-30 01:22:40 +0300 | [diff] [blame] | 409 | dma_unmap_single(fep->dev, CBDR_BUFADDR(bdp), |
| 410 | skb->len, DMA_TO_DEVICE); |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 411 | |
| 412 | /* |
Vitaly Bordug | 9b8ee8e | 2007-09-18 20:05:35 +0400 | [diff] [blame] | 413 | * Free the sk buffer associated with this last transmit. |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 414 | */ |
| 415 | dev_kfree_skb_irq(skb); |
| 416 | fep->tx_skbuff[dirtyidx] = NULL; |
| 417 | |
| 418 | /* |
Vitaly Bordug | 9b8ee8e | 2007-09-18 20:05:35 +0400 | [diff] [blame] | 419 | * Update pointer to next buffer descriptor to be transmitted. |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 420 | */ |
| 421 | if ((sc & BD_ENET_TX_WRAP) == 0) |
| 422 | bdp++; |
| 423 | else |
| 424 | bdp = fep->tx_bd_base; |
| 425 | |
| 426 | /* |
| 427 | * Since we have freed up a buffer, the ring is no longer |
| 428 | * full. |
| 429 | */ |
| 430 | if (!fep->tx_free++) |
| 431 | do_wake = 1; |
| 432 | } |
| 433 | |
| 434 | fep->dirty_tx = bdp; |
| 435 | |
| 436 | if (do_restart) |
| 437 | (*fep->ops->tx_restart)(dev); |
| 438 | |
Vitaly Bordug | aa90f50 | 2007-09-18 20:05:27 +0400 | [diff] [blame] | 439 | spin_unlock(&fep->tx_lock); |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 440 | |
| 441 | if (do_wake) |
| 442 | netif_wake_queue(dev); |
| 443 | } |
| 444 | |
| 445 | /* |
| 446 | * The interrupt handler. |
| 447 | * This is called from the MPC core interrupt. |
| 448 | */ |
| 449 | static irqreturn_t |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 450 | fs_enet_interrupt(int irq, void *dev_id) |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 451 | { |
| 452 | struct net_device *dev = dev_id; |
| 453 | struct fs_enet_private *fep; |
| 454 | const struct fs_platform_info *fpi; |
| 455 | u32 int_events; |
| 456 | u32 int_clr_events; |
| 457 | int nr, napi_ok; |
| 458 | int handled; |
| 459 | |
| 460 | fep = netdev_priv(dev); |
| 461 | fpi = fep->fpi; |
| 462 | |
| 463 | nr = 0; |
| 464 | while ((int_events = (*fep->ops->get_int_events)(dev)) != 0) { |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 465 | nr++; |
| 466 | |
| 467 | int_clr_events = int_events; |
| 468 | if (fpi->use_napi) |
| 469 | int_clr_events &= ~fep->ev_napi_rx; |
| 470 | |
| 471 | (*fep->ops->clear_int_events)(dev, int_clr_events); |
| 472 | |
| 473 | if (int_events & fep->ev_err) |
| 474 | (*fep->ops->ev_error)(dev, int_events); |
| 475 | |
| 476 | if (int_events & fep->ev_rx) { |
| 477 | if (!fpi->use_napi) |
| 478 | fs_enet_rx_non_napi(dev); |
| 479 | else { |
Stephen Hemminger | bea3348 | 2007-10-03 16:41:36 -0700 | [diff] [blame] | 480 | napi_ok = napi_schedule_prep(&fep->napi); |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 481 | |
| 482 | (*fep->ops->napi_disable_rx)(dev); |
| 483 | (*fep->ops->clear_int_events)(dev, fep->ev_napi_rx); |
| 484 | |
| 485 | /* NOTE: it is possible for FCCs in NAPI mode */ |
| 486 | /* to submit a spurious interrupt while in poll */ |
| 487 | if (napi_ok) |
Stephen Hemminger | bea3348 | 2007-10-03 16:41:36 -0700 | [diff] [blame] | 488 | __netif_rx_schedule(dev, &fep->napi); |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 489 | } |
| 490 | } |
| 491 | |
| 492 | if (int_events & fep->ev_tx) |
| 493 | fs_enet_tx(dev); |
| 494 | } |
| 495 | |
| 496 | handled = nr > 0; |
| 497 | return IRQ_RETVAL(handled); |
| 498 | } |
| 499 | |
| 500 | void fs_init_bds(struct net_device *dev) |
| 501 | { |
| 502 | struct fs_enet_private *fep = netdev_priv(dev); |
Scott Wood | 31a5bb0 | 2007-10-01 14:20:58 -0500 | [diff] [blame] | 503 | cbd_t __iomem *bdp; |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 504 | struct sk_buff *skb; |
| 505 | int i; |
| 506 | |
| 507 | fs_cleanup_bds(dev); |
| 508 | |
| 509 | fep->dirty_tx = fep->cur_tx = fep->tx_bd_base; |
| 510 | fep->tx_free = fep->tx_ring; |
| 511 | fep->cur_rx = fep->rx_bd_base; |
| 512 | |
| 513 | /* |
Vitaly Bordug | 9b8ee8e | 2007-09-18 20:05:35 +0400 | [diff] [blame] | 514 | * Initialize the receive buffer descriptors. |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 515 | */ |
| 516 | for (i = 0, bdp = fep->rx_bd_base; i < fep->rx_ring; i++, bdp++) { |
| 517 | skb = dev_alloc_skb(ENET_RX_FRSIZE); |
| 518 | if (skb == NULL) { |
| 519 | printk(KERN_WARNING DRV_MODULE_NAME |
| 520 | ": %s Memory squeeze, unable to allocate skb\n", |
| 521 | dev->name); |
| 522 | break; |
| 523 | } |
Scott Wood | 0d0d9c1 | 2007-10-01 14:20:52 -0500 | [diff] [blame] | 524 | skb_align(skb, ENET_RX_ALIGN); |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 525 | fep->rx_skbuff[i] = skb; |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 526 | CBDW_BUFADDR(bdp, |
| 527 | dma_map_single(fep->dev, skb->data, |
| 528 | L1_CACHE_ALIGN(PKT_MAXBUF_SIZE), |
| 529 | DMA_FROM_DEVICE)); |
| 530 | CBDW_DATLEN(bdp, 0); /* zero */ |
| 531 | CBDW_SC(bdp, BD_ENET_RX_EMPTY | |
| 532 | ((i < fep->rx_ring - 1) ? 0 : BD_SC_WRAP)); |
| 533 | } |
| 534 | /* |
Vitaly Bordug | 9b8ee8e | 2007-09-18 20:05:35 +0400 | [diff] [blame] | 535 | * if we failed, fillup remainder |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 536 | */ |
| 537 | for (; i < fep->rx_ring; i++, bdp++) { |
| 538 | fep->rx_skbuff[i] = NULL; |
| 539 | CBDW_SC(bdp, (i < fep->rx_ring - 1) ? 0 : BD_SC_WRAP); |
| 540 | } |
| 541 | |
| 542 | /* |
Vitaly Bordug | 9b8ee8e | 2007-09-18 20:05:35 +0400 | [diff] [blame] | 543 | * ...and the same for transmit. |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 544 | */ |
| 545 | for (i = 0, bdp = fep->tx_bd_base; i < fep->tx_ring; i++, bdp++) { |
| 546 | fep->tx_skbuff[i] = NULL; |
| 547 | CBDW_BUFADDR(bdp, 0); |
| 548 | CBDW_DATLEN(bdp, 0); |
| 549 | CBDW_SC(bdp, (i < fep->tx_ring - 1) ? 0 : BD_SC_WRAP); |
| 550 | } |
| 551 | } |
| 552 | |
| 553 | void fs_cleanup_bds(struct net_device *dev) |
| 554 | { |
| 555 | struct fs_enet_private *fep = netdev_priv(dev); |
| 556 | struct sk_buff *skb; |
Scott Wood | 31a5bb0 | 2007-10-01 14:20:58 -0500 | [diff] [blame] | 557 | cbd_t __iomem *bdp; |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 558 | int i; |
| 559 | |
| 560 | /* |
Vitaly Bordug | 9b8ee8e | 2007-09-18 20:05:35 +0400 | [diff] [blame] | 561 | * Reset SKB transmit buffers. |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 562 | */ |
Pantelis Antoniou | 34e30d6 | 2005-10-30 01:22:40 +0300 | [diff] [blame] | 563 | 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] | 564 | if ((skb = fep->tx_skbuff[i]) == NULL) |
| 565 | continue; |
| 566 | |
| 567 | /* unmap */ |
Pantelis Antoniou | 34e30d6 | 2005-10-30 01:22:40 +0300 | [diff] [blame] | 568 | dma_unmap_single(fep->dev, CBDR_BUFADDR(bdp), |
| 569 | skb->len, DMA_TO_DEVICE); |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 570 | |
| 571 | fep->tx_skbuff[i] = NULL; |
| 572 | dev_kfree_skb(skb); |
| 573 | } |
| 574 | |
| 575 | /* |
Vitaly Bordug | 9b8ee8e | 2007-09-18 20:05:35 +0400 | [diff] [blame] | 576 | * Reset SKB receive buffers |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 577 | */ |
Pantelis Antoniou | 34e30d6 | 2005-10-30 01:22:40 +0300 | [diff] [blame] | 578 | 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] | 579 | if ((skb = fep->rx_skbuff[i]) == NULL) |
| 580 | continue; |
| 581 | |
| 582 | /* unmap */ |
Pantelis Antoniou | 34e30d6 | 2005-10-30 01:22:40 +0300 | [diff] [blame] | 583 | dma_unmap_single(fep->dev, CBDR_BUFADDR(bdp), |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 584 | L1_CACHE_ALIGN(PKT_MAXBUF_SIZE), |
| 585 | DMA_FROM_DEVICE); |
| 586 | |
| 587 | fep->rx_skbuff[i] = NULL; |
| 588 | |
| 589 | dev_kfree_skb(skb); |
| 590 | } |
| 591 | } |
| 592 | |
| 593 | /**********************************************************************************/ |
| 594 | |
| 595 | static int fs_enet_start_xmit(struct sk_buff *skb, struct net_device *dev) |
| 596 | { |
| 597 | struct fs_enet_private *fep = netdev_priv(dev); |
Scott Wood | 31a5bb0 | 2007-10-01 14:20:58 -0500 | [diff] [blame] | 598 | cbd_t __iomem *bdp; |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 599 | int curidx; |
| 600 | u16 sc; |
| 601 | unsigned long flags; |
| 602 | |
| 603 | spin_lock_irqsave(&fep->tx_lock, flags); |
| 604 | |
| 605 | /* |
Vitaly Bordug | 9b8ee8e | 2007-09-18 20:05:35 +0400 | [diff] [blame] | 606 | * Fill in a Tx ring entry |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 607 | */ |
| 608 | bdp = fep->cur_tx; |
| 609 | |
| 610 | if (!fep->tx_free || (CBDR_SC(bdp) & BD_ENET_TX_READY)) { |
| 611 | netif_stop_queue(dev); |
| 612 | spin_unlock_irqrestore(&fep->tx_lock, flags); |
| 613 | |
| 614 | /* |
| 615 | * Ooops. All transmit buffers are full. Bail out. |
| 616 | * This should not happen, since the tx queue should be stopped. |
| 617 | */ |
| 618 | printk(KERN_WARNING DRV_MODULE_NAME |
| 619 | ": %s tx queue full!.\n", dev->name); |
| 620 | return NETDEV_TX_BUSY; |
| 621 | } |
| 622 | |
| 623 | curidx = bdp - fep->tx_bd_base; |
| 624 | /* |
Vitaly Bordug | 9b8ee8e | 2007-09-18 20:05:35 +0400 | [diff] [blame] | 625 | * Clear all of the status flags. |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 626 | */ |
| 627 | CBDC_SC(bdp, BD_ENET_TX_STATS); |
| 628 | |
| 629 | /* |
Vitaly Bordug | 9b8ee8e | 2007-09-18 20:05:35 +0400 | [diff] [blame] | 630 | * Save skb pointer. |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 631 | */ |
| 632 | fep->tx_skbuff[curidx] = skb; |
| 633 | |
| 634 | fep->stats.tx_bytes += skb->len; |
| 635 | |
| 636 | /* |
Vitaly Bordug | 9b8ee8e | 2007-09-18 20:05:35 +0400 | [diff] [blame] | 637 | * 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] | 638 | */ |
| 639 | CBDW_BUFADDR(bdp, dma_map_single(fep->dev, |
| 640 | skb->data, skb->len, DMA_TO_DEVICE)); |
| 641 | CBDW_DATLEN(bdp, skb->len); |
| 642 | |
| 643 | dev->trans_start = jiffies; |
| 644 | |
| 645 | /* |
Vitaly Bordug | 9b8ee8e | 2007-09-18 20:05:35 +0400 | [diff] [blame] | 646 | * 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] | 647 | */ |
| 648 | if ((CBDR_SC(bdp) & BD_ENET_TX_WRAP) == 0) |
| 649 | fep->cur_tx++; |
| 650 | else |
| 651 | fep->cur_tx = fep->tx_bd_base; |
| 652 | |
| 653 | if (!--fep->tx_free) |
| 654 | netif_stop_queue(dev); |
| 655 | |
| 656 | /* Trigger transmission start */ |
| 657 | sc = BD_ENET_TX_READY | BD_ENET_TX_INTR | |
| 658 | BD_ENET_TX_LAST | BD_ENET_TX_TC; |
| 659 | |
| 660 | /* note that while FEC does not have this bit |
| 661 | * it marks it as available for software use |
| 662 | * yay for hw reuse :) */ |
| 663 | if (skb->len <= 60) |
| 664 | sc |= BD_ENET_TX_PAD; |
| 665 | CBDS_SC(bdp, sc); |
| 666 | |
| 667 | (*fep->ops->tx_kickstart)(dev); |
| 668 | |
| 669 | spin_unlock_irqrestore(&fep->tx_lock, flags); |
| 670 | |
| 671 | return NETDEV_TX_OK; |
| 672 | } |
| 673 | |
| 674 | 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] | 675 | irq_handler_t irqf) |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 676 | { |
| 677 | struct fs_enet_private *fep = netdev_priv(dev); |
| 678 | |
| 679 | (*fep->ops->pre_request_irq)(dev, irq); |
Thomas Gleixner | 1fb9df5 | 2006-07-01 19:29:39 -0700 | [diff] [blame] | 680 | return request_irq(irq, irqf, IRQF_SHARED, name, dev); |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 681 | } |
| 682 | |
| 683 | static void fs_free_irq(struct net_device *dev, int irq) |
| 684 | { |
| 685 | struct fs_enet_private *fep = netdev_priv(dev); |
| 686 | |
| 687 | free_irq(irq, dev); |
| 688 | (*fep->ops->post_free_irq)(dev, irq); |
| 689 | } |
| 690 | |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 691 | static void fs_timeout(struct net_device *dev) |
| 692 | { |
| 693 | struct fs_enet_private *fep = netdev_priv(dev); |
| 694 | unsigned long flags; |
| 695 | int wake = 0; |
| 696 | |
| 697 | fep->stats.tx_errors++; |
| 698 | |
| 699 | spin_lock_irqsave(&fep->lock, flags); |
| 700 | |
| 701 | if (dev->flags & IFF_UP) { |
Vitaly Bordug | 5b4b845 | 2006-08-14 23:00:30 -0700 | [diff] [blame] | 702 | phy_stop(fep->phydev); |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 703 | (*fep->ops->stop)(dev); |
| 704 | (*fep->ops->restart)(dev); |
Vitaly Bordug | 5b4b845 | 2006-08-14 23:00:30 -0700 | [diff] [blame] | 705 | phy_start(fep->phydev); |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 706 | } |
| 707 | |
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 | wake = fep->tx_free && !(CBDR_SC(fep->cur_tx) & BD_ENET_TX_READY); |
| 710 | spin_unlock_irqrestore(&fep->lock, flags); |
| 711 | |
| 712 | if (wake) |
| 713 | netif_wake_queue(dev); |
| 714 | } |
| 715 | |
Vitaly Bordug | 5b4b845 | 2006-08-14 23:00:30 -0700 | [diff] [blame] | 716 | /*----------------------------------------------------------------------------- |
| 717 | * generic link-change handler - should be sufficient for most cases |
| 718 | *-----------------------------------------------------------------------------*/ |
| 719 | static void generic_adjust_link(struct net_device *dev) |
| 720 | { |
Scott Wood | 0fb300f | 2007-10-01 14:20:17 -0500 | [diff] [blame] | 721 | struct fs_enet_private *fep = netdev_priv(dev); |
| 722 | struct phy_device *phydev = fep->phydev; |
| 723 | int new_state = 0; |
Vitaly Bordug | 5b4b845 | 2006-08-14 23:00:30 -0700 | [diff] [blame] | 724 | |
Scott Wood | 0fb300f | 2007-10-01 14:20:17 -0500 | [diff] [blame] | 725 | if (phydev->link) { |
| 726 | /* adjust to duplex mode */ |
| 727 | if (phydev->duplex != fep->oldduplex) { |
| 728 | new_state = 1; |
| 729 | fep->oldduplex = phydev->duplex; |
| 730 | } |
Vitaly Bordug | 5b4b845 | 2006-08-14 23:00:30 -0700 | [diff] [blame] | 731 | |
Scott Wood | 0fb300f | 2007-10-01 14:20:17 -0500 | [diff] [blame] | 732 | if (phydev->speed != fep->oldspeed) { |
| 733 | new_state = 1; |
| 734 | fep->oldspeed = phydev->speed; |
| 735 | } |
Vitaly Bordug | 5b4b845 | 2006-08-14 23:00:30 -0700 | [diff] [blame] | 736 | |
Scott Wood | 0fb300f | 2007-10-01 14:20:17 -0500 | [diff] [blame] | 737 | if (!fep->oldlink) { |
| 738 | new_state = 1; |
| 739 | fep->oldlink = 1; |
| 740 | netif_schedule(dev); |
| 741 | netif_carrier_on(dev); |
| 742 | netif_start_queue(dev); |
| 743 | } |
Vitaly Bordug | 5b4b845 | 2006-08-14 23:00:30 -0700 | [diff] [blame] | 744 | |
Scott Wood | 0fb300f | 2007-10-01 14:20:17 -0500 | [diff] [blame] | 745 | if (new_state) |
| 746 | fep->ops->restart(dev); |
| 747 | } else if (fep->oldlink) { |
| 748 | new_state = 1; |
| 749 | fep->oldlink = 0; |
| 750 | fep->oldspeed = 0; |
| 751 | fep->oldduplex = -1; |
| 752 | netif_carrier_off(dev); |
| 753 | netif_stop_queue(dev); |
| 754 | } |
Vitaly Bordug | 5b4b845 | 2006-08-14 23:00:30 -0700 | [diff] [blame] | 755 | |
Scott Wood | 0fb300f | 2007-10-01 14:20:17 -0500 | [diff] [blame] | 756 | if (new_state && netif_msg_link(fep)) |
| 757 | phy_print_status(phydev); |
Vitaly Bordug | 5b4b845 | 2006-08-14 23:00:30 -0700 | [diff] [blame] | 758 | } |
| 759 | |
| 760 | |
| 761 | static void fs_adjust_link(struct net_device *dev) |
| 762 | { |
| 763 | struct fs_enet_private *fep = netdev_priv(dev); |
| 764 | unsigned long flags; |
| 765 | |
| 766 | spin_lock_irqsave(&fep->lock, flags); |
| 767 | |
| 768 | if(fep->ops->adjust_link) |
| 769 | fep->ops->adjust_link(dev); |
| 770 | else |
| 771 | generic_adjust_link(dev); |
| 772 | |
| 773 | spin_unlock_irqrestore(&fep->lock, flags); |
| 774 | } |
| 775 | |
| 776 | static int fs_init_phy(struct net_device *dev) |
| 777 | { |
| 778 | struct fs_enet_private *fep = netdev_priv(dev); |
| 779 | struct phy_device *phydev; |
| 780 | |
| 781 | fep->oldlink = 0; |
| 782 | fep->oldspeed = 0; |
| 783 | fep->oldduplex = -1; |
| 784 | if(fep->fpi->bus_id) |
Andy Fleming | e8a2b6a | 2006-12-01 12:01:06 -0600 | [diff] [blame] | 785 | phydev = phy_connect(dev, fep->fpi->bus_id, &fs_adjust_link, 0, |
| 786 | PHY_INTERFACE_MODE_MII); |
Vitaly Bordug | 5b4b845 | 2006-08-14 23:00:30 -0700 | [diff] [blame] | 787 | else { |
| 788 | printk("No phy bus ID specified in BSP code\n"); |
| 789 | return -EINVAL; |
| 790 | } |
| 791 | if (IS_ERR(phydev)) { |
| 792 | printk(KERN_ERR "%s: Could not attach to PHY\n", dev->name); |
| 793 | return PTR_ERR(phydev); |
| 794 | } |
| 795 | |
| 796 | fep->phydev = phydev; |
| 797 | |
| 798 | return 0; |
| 799 | } |
| 800 | |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 801 | static int fs_enet_open(struct net_device *dev) |
| 802 | { |
| 803 | struct fs_enet_private *fep = netdev_priv(dev); |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 804 | int r; |
Vitaly Bordug | 5b4b845 | 2006-08-14 23:00:30 -0700 | [diff] [blame] | 805 | int err; |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 806 | |
Scott Wood | f860f49 | 2007-10-17 12:42:43 -0500 | [diff] [blame] | 807 | if (fep->fpi->use_napi) |
| 808 | napi_enable(&fep->napi); |
Stephen Hemminger | bea3348 | 2007-10-03 16:41:36 -0700 | [diff] [blame] | 809 | |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 810 | /* Install our interrupt handler. */ |
| 811 | r = fs_request_irq(dev, fep->interrupt, "fs_enet-mac", fs_enet_interrupt); |
| 812 | if (r != 0) { |
| 813 | printk(KERN_ERR DRV_MODULE_NAME |
Vitaly Bordug | 5b4b845 | 2006-08-14 23:00:30 -0700 | [diff] [blame] | 814 | ": %s Could not allocate FS_ENET IRQ!", dev->name); |
Scott Wood | f860f49 | 2007-10-17 12:42:43 -0500 | [diff] [blame] | 815 | if (fep->fpi->use_napi) |
| 816 | napi_disable(&fep->napi); |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 817 | return -EINVAL; |
| 818 | } |
| 819 | |
Vitaly Bordug | 5b4b845 | 2006-08-14 23:00:30 -0700 | [diff] [blame] | 820 | err = fs_init_phy(dev); |
Scott Wood | f860f49 | 2007-10-17 12:42:43 -0500 | [diff] [blame] | 821 | if (err) { |
| 822 | if (fep->fpi->use_napi) |
| 823 | napi_disable(&fep->napi); |
Vitaly Bordug | 5b4b845 | 2006-08-14 23:00:30 -0700 | [diff] [blame] | 824 | return err; |
Stephen Hemminger | bea3348 | 2007-10-03 16:41:36 -0700 | [diff] [blame] | 825 | } |
Vitaly Bordug | 5b4b845 | 2006-08-14 23:00:30 -0700 | [diff] [blame] | 826 | phy_start(fep->phydev); |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 827 | |
| 828 | return 0; |
| 829 | } |
| 830 | |
| 831 | static int fs_enet_close(struct net_device *dev) |
| 832 | { |
| 833 | struct fs_enet_private *fep = netdev_priv(dev); |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 834 | unsigned long flags; |
| 835 | |
| 836 | netif_stop_queue(dev); |
| 837 | netif_carrier_off(dev); |
Laurent Pinchart | 9a3c243 | 2008-04-11 14:05:50 +0200 | [diff] [blame] | 838 | if (fep->fpi->use_napi) |
| 839 | napi_disable(&fep->napi); |
Vitaly Bordug | 5b4b845 | 2006-08-14 23:00:30 -0700 | [diff] [blame] | 840 | phy_stop(fep->phydev); |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 841 | |
| 842 | spin_lock_irqsave(&fep->lock, flags); |
Vitaly Bordug | aa90f50 | 2007-09-18 20:05:27 +0400 | [diff] [blame] | 843 | spin_lock(&fep->tx_lock); |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 844 | (*fep->ops->stop)(dev); |
Vitaly Bordug | aa90f50 | 2007-09-18 20:05:27 +0400 | [diff] [blame] | 845 | spin_unlock(&fep->tx_lock); |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 846 | spin_unlock_irqrestore(&fep->lock, flags); |
| 847 | |
| 848 | /* release any irqs */ |
Vitaly Bordug | 5b4b845 | 2006-08-14 23:00:30 -0700 | [diff] [blame] | 849 | phy_disconnect(fep->phydev); |
| 850 | fep->phydev = NULL; |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 851 | fs_free_irq(dev, fep->interrupt); |
| 852 | |
| 853 | return 0; |
| 854 | } |
| 855 | |
| 856 | static struct net_device_stats *fs_enet_get_stats(struct net_device *dev) |
| 857 | { |
| 858 | struct fs_enet_private *fep = netdev_priv(dev); |
| 859 | return &fep->stats; |
| 860 | } |
| 861 | |
| 862 | /*************************************************************************/ |
| 863 | |
| 864 | static void fs_get_drvinfo(struct net_device *dev, |
| 865 | struct ethtool_drvinfo *info) |
| 866 | { |
| 867 | strcpy(info->driver, DRV_MODULE_NAME); |
| 868 | strcpy(info->version, DRV_MODULE_VERSION); |
| 869 | } |
| 870 | |
| 871 | static int fs_get_regs_len(struct net_device *dev) |
| 872 | { |
| 873 | struct fs_enet_private *fep = netdev_priv(dev); |
| 874 | |
| 875 | return (*fep->ops->get_regs_len)(dev); |
| 876 | } |
| 877 | |
| 878 | static void fs_get_regs(struct net_device *dev, struct ethtool_regs *regs, |
| 879 | void *p) |
| 880 | { |
| 881 | struct fs_enet_private *fep = netdev_priv(dev); |
| 882 | unsigned long flags; |
| 883 | int r, len; |
| 884 | |
| 885 | len = regs->len; |
| 886 | |
| 887 | spin_lock_irqsave(&fep->lock, flags); |
| 888 | r = (*fep->ops->get_regs)(dev, p, &len); |
| 889 | spin_unlock_irqrestore(&fep->lock, flags); |
| 890 | |
| 891 | if (r == 0) |
| 892 | regs->version = 0; |
| 893 | } |
| 894 | |
| 895 | static int fs_get_settings(struct net_device *dev, struct ethtool_cmd *cmd) |
| 896 | { |
| 897 | struct fs_enet_private *fep = netdev_priv(dev); |
Anton Vorontsov | dfd9a421 | 2008-01-08 22:05:55 +0300 | [diff] [blame] | 898 | |
| 899 | if (!fep->phydev) |
| 900 | return -ENODEV; |
| 901 | |
Vitaly Bordug | 5b4b845 | 2006-08-14 23:00:30 -0700 | [diff] [blame] | 902 | return phy_ethtool_gset(fep->phydev, cmd); |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 903 | } |
| 904 | |
| 905 | static int fs_set_settings(struct net_device *dev, struct ethtool_cmd *cmd) |
| 906 | { |
| 907 | struct fs_enet_private *fep = netdev_priv(dev); |
Anton Vorontsov | dfd9a421 | 2008-01-08 22:05:55 +0300 | [diff] [blame] | 908 | |
| 909 | if (!fep->phydev) |
| 910 | return -ENODEV; |
| 911 | |
| 912 | return phy_ethtool_sset(fep->phydev, cmd); |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 913 | } |
| 914 | |
| 915 | static int fs_nway_reset(struct net_device *dev) |
| 916 | { |
Vitaly Bordug | 5b4b845 | 2006-08-14 23:00:30 -0700 | [diff] [blame] | 917 | return 0; |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 918 | } |
| 919 | |
| 920 | static u32 fs_get_msglevel(struct net_device *dev) |
| 921 | { |
| 922 | struct fs_enet_private *fep = netdev_priv(dev); |
| 923 | return fep->msg_enable; |
| 924 | } |
| 925 | |
| 926 | static void fs_set_msglevel(struct net_device *dev, u32 value) |
| 927 | { |
| 928 | struct fs_enet_private *fep = netdev_priv(dev); |
| 929 | fep->msg_enable = value; |
| 930 | } |
| 931 | |
Jeff Garzik | 7282d49 | 2006-09-13 14:30:00 -0400 | [diff] [blame] | 932 | static const struct ethtool_ops fs_ethtool_ops = { |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 933 | .get_drvinfo = fs_get_drvinfo, |
| 934 | .get_regs_len = fs_get_regs_len, |
| 935 | .get_settings = fs_get_settings, |
| 936 | .set_settings = fs_set_settings, |
| 937 | .nway_reset = fs_nway_reset, |
| 938 | .get_link = ethtool_op_get_link, |
| 939 | .get_msglevel = fs_get_msglevel, |
| 940 | .set_msglevel = fs_set_msglevel, |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 941 | .set_tx_csum = ethtool_op_set_tx_csum, /* local! */ |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 942 | .set_sg = ethtool_op_set_sg, |
| 943 | .get_regs = fs_get_regs, |
| 944 | }; |
| 945 | |
| 946 | static int fs_ioctl(struct net_device *dev, struct ifreq *rq, int cmd) |
| 947 | { |
| 948 | struct fs_enet_private *fep = netdev_priv(dev); |
| 949 | struct mii_ioctl_data *mii = (struct mii_ioctl_data *)&rq->ifr_data; |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 950 | |
| 951 | if (!netif_running(dev)) |
| 952 | return -EINVAL; |
| 953 | |
Scott Wood | 5bd3670 | 2008-02-15 15:08:04 -0600 | [diff] [blame] | 954 | return phy_mii_ioctl(fep->phydev, mii, cmd); |
Pantelis Antoniou | 48257c4 | 2005-10-28 16:25:58 -0400 | [diff] [blame] | 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) { |
Andy Fleming | 9d9326d | 2008-04-09 19:38:13 -0500 | [diff] [blame] | 1181 | snprintf(fpi->bus_id, 16, "%x:%02x", 0, *data); |
Jochen Friedrich | 70f782e | 2007-12-18 16:25:43 +0100 | [diff] [blame] | 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 | |
Andy Fleming | 9d9326d | 2008-04-09 19:38:13 -0500 | [diff] [blame] | 1205 | snprintf(fpi->bus_id, 16, "%x:%02x", res.start, *data); |
Scott Wood | 976de6a | 2007-10-02 10:55:58 -0500 | [diff] [blame] | 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); |