Florian Fainelli | ef11291 | 2008-03-19 17:14:51 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Driver for the IDT RC32434 (Korina) on-chip ethernet controller. |
| 3 | * |
| 4 | * Copyright 2004 IDT Inc. (rischelp@idt.com) |
| 5 | * Copyright 2006 Felix Fietkau <nbd@openwrt.org> |
| 6 | * Copyright 2008 Florian Fainelli <florian@openwrt.org> |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify it |
| 9 | * under the terms of the GNU General Public License as published by the |
| 10 | * Free Software Foundation; either version 2 of the License, or (at your |
| 11 | * option) any later version. |
| 12 | * |
| 13 | * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED |
| 14 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 15 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN |
| 16 | * NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 17 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 18 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF |
| 19 | * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON |
| 20 | * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 21 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
| 22 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 23 | * |
| 24 | * You should have received a copy of the GNU General Public License along |
| 25 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 26 | * 675 Mass Ave, Cambridge, MA 02139, USA. |
| 27 | * |
| 28 | * Writing to a DMA status register: |
| 29 | * |
| 30 | * When writing to the status register, you should mask the bit you have |
| 31 | * been testing the status register with. Both Tx and Rx DMA registers |
| 32 | * should stick to this procedure. |
| 33 | */ |
| 34 | |
| 35 | #include <linux/module.h> |
| 36 | #include <linux/kernel.h> |
| 37 | #include <linux/moduleparam.h> |
| 38 | #include <linux/sched.h> |
| 39 | #include <linux/ctype.h> |
| 40 | #include <linux/types.h> |
| 41 | #include <linux/interrupt.h> |
| 42 | #include <linux/init.h> |
| 43 | #include <linux/ioport.h> |
| 44 | #include <linux/in.h> |
| 45 | #include <linux/slab.h> |
| 46 | #include <linux/string.h> |
| 47 | #include <linux/delay.h> |
| 48 | #include <linux/netdevice.h> |
| 49 | #include <linux/etherdevice.h> |
| 50 | #include <linux/skbuff.h> |
| 51 | #include <linux/errno.h> |
| 52 | #include <linux/platform_device.h> |
| 53 | #include <linux/mii.h> |
| 54 | #include <linux/ethtool.h> |
| 55 | #include <linux/crc32.h> |
| 56 | |
| 57 | #include <asm/bootinfo.h> |
Florian Fainelli | ef11291 | 2008-03-19 17:14:51 +0100 | [diff] [blame] | 58 | #include <asm/bitops.h> |
| 59 | #include <asm/pgtable.h> |
Florian Fainelli | ef11291 | 2008-03-19 17:14:51 +0100 | [diff] [blame] | 60 | #include <asm/io.h> |
| 61 | #include <asm/dma.h> |
| 62 | |
| 63 | #include <asm/mach-rc32434/rb.h> |
| 64 | #include <asm/mach-rc32434/rc32434.h> |
| 65 | #include <asm/mach-rc32434/eth.h> |
| 66 | #include <asm/mach-rc32434/dma_v.h> |
| 67 | |
| 68 | #define DRV_NAME "korina" |
| 69 | #define DRV_VERSION "0.10" |
| 70 | #define DRV_RELDATE "04Mar2008" |
| 71 | |
| 72 | #define STATION_ADDRESS_HIGH(dev) (((dev)->dev_addr[0] << 8) | \ |
| 73 | ((dev)->dev_addr[1])) |
| 74 | #define STATION_ADDRESS_LOW(dev) (((dev)->dev_addr[2] << 24) | \ |
| 75 | ((dev)->dev_addr[3] << 16) | \ |
| 76 | ((dev)->dev_addr[4] << 8) | \ |
| 77 | ((dev)->dev_addr[5])) |
| 78 | |
| 79 | #define MII_CLOCK 1250000 /* no more than 2.5MHz */ |
| 80 | |
| 81 | /* the following must be powers of two */ |
| 82 | #define KORINA_NUM_RDS 64 /* number of receive descriptors */ |
| 83 | #define KORINA_NUM_TDS 64 /* number of transmit descriptors */ |
| 84 | |
Phil Sutter | a13b278 | 2009-01-14 21:47:50 -0800 | [diff] [blame] | 85 | /* KORINA_RBSIZE is the hardware's default maximum receive |
| 86 | * frame size in bytes. Having this hardcoded means that there |
| 87 | * is no support for MTU sizes greater than 1500. */ |
| 88 | #define KORINA_RBSIZE 1536 /* size of one resource buffer = Ether MTU */ |
Florian Fainelli | ef11291 | 2008-03-19 17:14:51 +0100 | [diff] [blame] | 89 | #define KORINA_RDS_MASK (KORINA_NUM_RDS - 1) |
| 90 | #define KORINA_TDS_MASK (KORINA_NUM_TDS - 1) |
| 91 | #define RD_RING_SIZE (KORINA_NUM_RDS * sizeof(struct dma_desc)) |
| 92 | #define TD_RING_SIZE (KORINA_NUM_TDS * sizeof(struct dma_desc)) |
| 93 | |
| 94 | #define TX_TIMEOUT (6000 * HZ / 1000) |
| 95 | |
| 96 | enum chain_status { desc_filled, desc_empty }; |
| 97 | #define IS_DMA_FINISHED(X) (((X) & (DMA_DESC_FINI)) != 0) |
| 98 | #define IS_DMA_DONE(X) (((X) & (DMA_DESC_DONE)) != 0) |
| 99 | #define RCVPKT_LENGTH(X) (((X) & ETH_RX_LEN) >> ETH_RX_LEN_BIT) |
| 100 | |
| 101 | /* Information that need to be kept for each board. */ |
| 102 | struct korina_private { |
| 103 | struct eth_regs *eth_regs; |
| 104 | struct dma_reg *rx_dma_regs; |
| 105 | struct dma_reg *tx_dma_regs; |
| 106 | struct dma_desc *td_ring; /* transmit descriptor ring */ |
| 107 | struct dma_desc *rd_ring; /* receive descriptor ring */ |
| 108 | |
| 109 | struct sk_buff *tx_skb[KORINA_NUM_TDS]; |
| 110 | struct sk_buff *rx_skb[KORINA_NUM_RDS]; |
| 111 | |
| 112 | int rx_next_done; |
| 113 | int rx_chain_head; |
| 114 | int rx_chain_tail; |
| 115 | enum chain_status rx_chain_status; |
| 116 | |
| 117 | int tx_next_done; |
| 118 | int tx_chain_head; |
| 119 | int tx_chain_tail; |
| 120 | enum chain_status tx_chain_status; |
| 121 | int tx_count; |
| 122 | int tx_full; |
| 123 | |
| 124 | int rx_irq; |
| 125 | int tx_irq; |
| 126 | int ovr_irq; |
| 127 | int und_irq; |
| 128 | |
| 129 | spinlock_t lock; /* NIC xmit lock */ |
| 130 | |
| 131 | int dma_halt_cnt; |
| 132 | int dma_run_cnt; |
| 133 | struct napi_struct napi; |
Florian Fainelli | 4d5ef9f | 2009-05-28 00:58:41 +0000 | [diff] [blame] | 134 | struct timer_list media_check_timer; |
Florian Fainelli | ef11291 | 2008-03-19 17:14:51 +0100 | [diff] [blame] | 135 | struct mii_if_info mii_if; |
Phil Sutter | ceb3d23 | 2010-05-29 13:23:34 +0000 | [diff] [blame] | 136 | struct work_struct restart_task; |
Florian Fainelli | ef11291 | 2008-03-19 17:14:51 +0100 | [diff] [blame] | 137 | struct net_device *dev; |
| 138 | int phy_addr; |
| 139 | }; |
| 140 | |
| 141 | extern unsigned int idt_cpu_freq; |
| 142 | |
| 143 | static inline void korina_start_dma(struct dma_reg *ch, u32 dma_addr) |
| 144 | { |
| 145 | writel(0, &ch->dmandptr); |
| 146 | writel(dma_addr, &ch->dmadptr); |
| 147 | } |
| 148 | |
| 149 | static inline void korina_abort_dma(struct net_device *dev, |
| 150 | struct dma_reg *ch) |
| 151 | { |
| 152 | if (readl(&ch->dmac) & DMA_CHAN_RUN_BIT) { |
| 153 | writel(0x10, &ch->dmac); |
| 154 | |
| 155 | while (!(readl(&ch->dmas) & DMA_STAT_HALT)) |
| 156 | dev->trans_start = jiffies; |
| 157 | |
| 158 | writel(0, &ch->dmas); |
| 159 | } |
| 160 | |
| 161 | writel(0, &ch->dmadptr); |
| 162 | writel(0, &ch->dmandptr); |
| 163 | } |
| 164 | |
| 165 | static inline void korina_chain_dma(struct dma_reg *ch, u32 dma_addr) |
| 166 | { |
| 167 | writel(dma_addr, &ch->dmandptr); |
| 168 | } |
| 169 | |
| 170 | static void korina_abort_tx(struct net_device *dev) |
| 171 | { |
| 172 | struct korina_private *lp = netdev_priv(dev); |
| 173 | |
| 174 | korina_abort_dma(dev, lp->tx_dma_regs); |
| 175 | } |
| 176 | |
| 177 | static void korina_abort_rx(struct net_device *dev) |
| 178 | { |
| 179 | struct korina_private *lp = netdev_priv(dev); |
| 180 | |
| 181 | korina_abort_dma(dev, lp->rx_dma_regs); |
| 182 | } |
| 183 | |
| 184 | static void korina_start_rx(struct korina_private *lp, |
| 185 | struct dma_desc *rd) |
| 186 | { |
| 187 | korina_start_dma(lp->rx_dma_regs, CPHYSADDR(rd)); |
| 188 | } |
| 189 | |
| 190 | static void korina_chain_rx(struct korina_private *lp, |
| 191 | struct dma_desc *rd) |
| 192 | { |
| 193 | korina_chain_dma(lp->rx_dma_regs, CPHYSADDR(rd)); |
| 194 | } |
| 195 | |
| 196 | /* transmit packet */ |
| 197 | static int korina_send_packet(struct sk_buff *skb, struct net_device *dev) |
| 198 | { |
| 199 | struct korina_private *lp = netdev_priv(dev); |
| 200 | unsigned long flags; |
| 201 | u32 length; |
Phil Sutter | 97bc477 | 2009-01-14 21:50:41 -0800 | [diff] [blame] | 202 | u32 chain_prev, chain_next; |
Florian Fainelli | ef11291 | 2008-03-19 17:14:51 +0100 | [diff] [blame] | 203 | struct dma_desc *td; |
| 204 | |
| 205 | spin_lock_irqsave(&lp->lock, flags); |
| 206 | |
| 207 | td = &lp->td_ring[lp->tx_chain_tail]; |
| 208 | |
| 209 | /* stop queue when full, drop pkts if queue already full */ |
| 210 | if (lp->tx_count >= (KORINA_NUM_TDS - 2)) { |
| 211 | lp->tx_full = 1; |
| 212 | |
| 213 | if (lp->tx_count == (KORINA_NUM_TDS - 2)) |
| 214 | netif_stop_queue(dev); |
| 215 | else { |
| 216 | dev->stats.tx_dropped++; |
| 217 | dev_kfree_skb_any(skb); |
| 218 | spin_unlock_irqrestore(&lp->lock, flags); |
| 219 | |
| 220 | return NETDEV_TX_BUSY; |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | lp->tx_count++; |
| 225 | |
| 226 | lp->tx_skb[lp->tx_chain_tail] = skb; |
| 227 | |
| 228 | length = skb->len; |
| 229 | dma_cache_wback((u32)skb->data, skb->len); |
| 230 | |
| 231 | /* Setup the transmit descriptor. */ |
| 232 | dma_cache_inv((u32) td, sizeof(*td)); |
| 233 | td->ca = CPHYSADDR(skb->data); |
Phil Sutter | 97bc477 | 2009-01-14 21:50:41 -0800 | [diff] [blame] | 234 | chain_prev = (lp->tx_chain_tail - 1) & KORINA_TDS_MASK; |
| 235 | chain_next = (lp->tx_chain_tail + 1) & KORINA_TDS_MASK; |
Florian Fainelli | ef11291 | 2008-03-19 17:14:51 +0100 | [diff] [blame] | 236 | |
| 237 | if (readl(&(lp->tx_dma_regs->dmandptr)) == 0) { |
| 238 | if (lp->tx_chain_status == desc_empty) { |
| 239 | /* Update tail */ |
| 240 | td->control = DMA_COUNT(length) | |
| 241 | DMA_DESC_COF | DMA_DESC_IOF; |
| 242 | /* Move tail */ |
Phil Sutter | 97bc477 | 2009-01-14 21:50:41 -0800 | [diff] [blame] | 243 | lp->tx_chain_tail = chain_next; |
Florian Fainelli | ef11291 | 2008-03-19 17:14:51 +0100 | [diff] [blame] | 244 | /* Write to NDPTR */ |
| 245 | writel(CPHYSADDR(&lp->td_ring[lp->tx_chain_head]), |
| 246 | &lp->tx_dma_regs->dmandptr); |
| 247 | /* Move head to tail */ |
| 248 | lp->tx_chain_head = lp->tx_chain_tail; |
| 249 | } else { |
| 250 | /* Update tail */ |
| 251 | td->control = DMA_COUNT(length) | |
| 252 | DMA_DESC_COF | DMA_DESC_IOF; |
| 253 | /* Link to prev */ |
Phil Sutter | 97bc477 | 2009-01-14 21:50:41 -0800 | [diff] [blame] | 254 | lp->td_ring[chain_prev].control &= |
Florian Fainelli | ef11291 | 2008-03-19 17:14:51 +0100 | [diff] [blame] | 255 | ~DMA_DESC_COF; |
| 256 | /* Link to prev */ |
Phil Sutter | 97bc477 | 2009-01-14 21:50:41 -0800 | [diff] [blame] | 257 | lp->td_ring[chain_prev].link = CPHYSADDR(td); |
Florian Fainelli | ef11291 | 2008-03-19 17:14:51 +0100 | [diff] [blame] | 258 | /* Move tail */ |
Phil Sutter | 97bc477 | 2009-01-14 21:50:41 -0800 | [diff] [blame] | 259 | lp->tx_chain_tail = chain_next; |
Florian Fainelli | ef11291 | 2008-03-19 17:14:51 +0100 | [diff] [blame] | 260 | /* Write to NDPTR */ |
| 261 | writel(CPHYSADDR(&lp->td_ring[lp->tx_chain_head]), |
| 262 | &(lp->tx_dma_regs->dmandptr)); |
| 263 | /* Move head to tail */ |
| 264 | lp->tx_chain_head = lp->tx_chain_tail; |
| 265 | lp->tx_chain_status = desc_empty; |
| 266 | } |
| 267 | } else { |
| 268 | if (lp->tx_chain_status == desc_empty) { |
| 269 | /* Update tail */ |
| 270 | td->control = DMA_COUNT(length) | |
| 271 | DMA_DESC_COF | DMA_DESC_IOF; |
| 272 | /* Move tail */ |
Phil Sutter | 97bc477 | 2009-01-14 21:50:41 -0800 | [diff] [blame] | 273 | lp->tx_chain_tail = chain_next; |
Florian Fainelli | ef11291 | 2008-03-19 17:14:51 +0100 | [diff] [blame] | 274 | lp->tx_chain_status = desc_filled; |
Florian Fainelli | ef11291 | 2008-03-19 17:14:51 +0100 | [diff] [blame] | 275 | } else { |
| 276 | /* Update tail */ |
| 277 | td->control = DMA_COUNT(length) | |
| 278 | DMA_DESC_COF | DMA_DESC_IOF; |
Phil Sutter | 97bc477 | 2009-01-14 21:50:41 -0800 | [diff] [blame] | 279 | lp->td_ring[chain_prev].control &= |
Florian Fainelli | ef11291 | 2008-03-19 17:14:51 +0100 | [diff] [blame] | 280 | ~DMA_DESC_COF; |
Phil Sutter | 97bc477 | 2009-01-14 21:50:41 -0800 | [diff] [blame] | 281 | lp->td_ring[chain_prev].link = CPHYSADDR(td); |
| 282 | lp->tx_chain_tail = chain_next; |
Florian Fainelli | ef11291 | 2008-03-19 17:14:51 +0100 | [diff] [blame] | 283 | } |
| 284 | } |
| 285 | dma_cache_wback((u32) td, sizeof(*td)); |
| 286 | |
| 287 | dev->trans_start = jiffies; |
| 288 | spin_unlock_irqrestore(&lp->lock, flags); |
| 289 | |
| 290 | return NETDEV_TX_OK; |
| 291 | } |
| 292 | |
| 293 | static int mdio_read(struct net_device *dev, int mii_id, int reg) |
| 294 | { |
| 295 | struct korina_private *lp = netdev_priv(dev); |
| 296 | int ret; |
| 297 | |
| 298 | mii_id = ((lp->rx_irq == 0x2c ? 1 : 0) << 8); |
| 299 | |
| 300 | writel(0, &lp->eth_regs->miimcfg); |
| 301 | writel(0, &lp->eth_regs->miimcmd); |
| 302 | writel(mii_id | reg, &lp->eth_regs->miimaddr); |
| 303 | writel(ETH_MII_CMD_SCN, &lp->eth_regs->miimcmd); |
| 304 | |
| 305 | ret = (int)(readl(&lp->eth_regs->miimrdd)); |
| 306 | return ret; |
| 307 | } |
| 308 | |
| 309 | static void mdio_write(struct net_device *dev, int mii_id, int reg, int val) |
| 310 | { |
| 311 | struct korina_private *lp = netdev_priv(dev); |
| 312 | |
| 313 | mii_id = ((lp->rx_irq == 0x2c ? 1 : 0) << 8); |
| 314 | |
| 315 | writel(0, &lp->eth_regs->miimcfg); |
| 316 | writel(1, &lp->eth_regs->miimcmd); |
| 317 | writel(mii_id | reg, &lp->eth_regs->miimaddr); |
| 318 | writel(ETH_MII_CMD_SCN, &lp->eth_regs->miimcmd); |
| 319 | writel(val, &lp->eth_regs->miimwtd); |
| 320 | } |
| 321 | |
| 322 | /* Ethernet Rx DMA interrupt */ |
| 323 | static irqreturn_t korina_rx_dma_interrupt(int irq, void *dev_id) |
| 324 | { |
| 325 | struct net_device *dev = dev_id; |
| 326 | struct korina_private *lp = netdev_priv(dev); |
| 327 | u32 dmas, dmasm; |
| 328 | irqreturn_t retval; |
| 329 | |
| 330 | dmas = readl(&lp->rx_dma_regs->dmas); |
| 331 | if (dmas & (DMA_STAT_DONE | DMA_STAT_HALT | DMA_STAT_ERR)) { |
Florian Fainelli | ef11291 | 2008-03-19 17:14:51 +0100 | [diff] [blame] | 332 | dmasm = readl(&lp->rx_dma_regs->dmasm); |
| 333 | writel(dmasm | (DMA_STAT_DONE | |
| 334 | DMA_STAT_HALT | DMA_STAT_ERR), |
| 335 | &lp->rx_dma_regs->dmasm); |
| 336 | |
Ben Hutchings | 288379f | 2009-01-19 16:43:59 -0800 | [diff] [blame] | 337 | napi_schedule(&lp->napi); |
Phil Sutter | 60d3f98 | 2009-01-14 21:50:12 -0800 | [diff] [blame] | 338 | |
Florian Fainelli | ef11291 | 2008-03-19 17:14:51 +0100 | [diff] [blame] | 339 | if (dmas & DMA_STAT_ERR) |
Phil Sutter | f16aea4 | 2009-08-12 12:22:46 +0000 | [diff] [blame] | 340 | printk(KERN_ERR "%s: DMA error\n", dev->name); |
Florian Fainelli | ef11291 | 2008-03-19 17:14:51 +0100 | [diff] [blame] | 341 | |
| 342 | retval = IRQ_HANDLED; |
| 343 | } else |
| 344 | retval = IRQ_NONE; |
| 345 | |
| 346 | return retval; |
| 347 | } |
| 348 | |
| 349 | static int korina_rx(struct net_device *dev, int limit) |
| 350 | { |
| 351 | struct korina_private *lp = netdev_priv(dev); |
| 352 | struct dma_desc *rd = &lp->rd_ring[lp->rx_next_done]; |
| 353 | struct sk_buff *skb, *skb_new; |
| 354 | u8 *pkt_buf; |
Phil Sutter | 4cf83b6 | 2009-01-14 21:48:59 -0800 | [diff] [blame] | 355 | u32 devcs, pkt_len, dmas; |
Florian Fainelli | ef11291 | 2008-03-19 17:14:51 +0100 | [diff] [blame] | 356 | int count; |
| 357 | |
| 358 | dma_cache_inv((u32)rd, sizeof(*rd)); |
| 359 | |
| 360 | for (count = 0; count < limit; count++) { |
Phil Sutter | 4cf83b6 | 2009-01-14 21:48:59 -0800 | [diff] [blame] | 361 | skb = lp->rx_skb[lp->rx_next_done]; |
| 362 | skb_new = NULL; |
Florian Fainelli | ef11291 | 2008-03-19 17:14:51 +0100 | [diff] [blame] | 363 | |
| 364 | devcs = rd->devcs; |
| 365 | |
Phil Sutter | 4cf83b6 | 2009-01-14 21:48:59 -0800 | [diff] [blame] | 366 | if ((KORINA_RBSIZE - (u32)DMA_COUNT(rd->control)) == 0) |
| 367 | break; |
| 368 | |
Florian Fainelli | ef11291 | 2008-03-19 17:14:51 +0100 | [diff] [blame] | 369 | /* Update statistics counters */ |
| 370 | if (devcs & ETH_RX_CRC) |
| 371 | dev->stats.rx_crc_errors++; |
| 372 | if (devcs & ETH_RX_LOR) |
| 373 | dev->stats.rx_length_errors++; |
| 374 | if (devcs & ETH_RX_LE) |
| 375 | dev->stats.rx_length_errors++; |
| 376 | if (devcs & ETH_RX_OVR) |
Phil Sutter | b1011b3 | 2010-05-29 13:23:36 +0000 | [diff] [blame] | 377 | dev->stats.rx_fifo_errors++; |
Florian Fainelli | ef11291 | 2008-03-19 17:14:51 +0100 | [diff] [blame] | 378 | if (devcs & ETH_RX_CV) |
| 379 | dev->stats.rx_frame_errors++; |
| 380 | if (devcs & ETH_RX_CES) |
| 381 | dev->stats.rx_length_errors++; |
| 382 | if (devcs & ETH_RX_MP) |
| 383 | dev->stats.multicast++; |
| 384 | |
| 385 | if ((devcs & ETH_RX_LD) != ETH_RX_LD) { |
| 386 | /* check that this is a whole packet |
| 387 | * WARNING: DMA_FD bit incorrectly set |
| 388 | * in Rc32434 (errata ref #077) */ |
| 389 | dev->stats.rx_errors++; |
| 390 | dev->stats.rx_dropped++; |
Phil Sutter | 4cf83b6 | 2009-01-14 21:48:59 -0800 | [diff] [blame] | 391 | } else if ((devcs & ETH_RX_ROK)) { |
Florian Fainelli | ef11291 | 2008-03-19 17:14:51 +0100 | [diff] [blame] | 392 | pkt_len = RCVPKT_LENGTH(devcs); |
Florian Fainelli | ef11291 | 2008-03-19 17:14:51 +0100 | [diff] [blame] | 393 | |
Phil Sutter | 4cf83b6 | 2009-01-14 21:48:59 -0800 | [diff] [blame] | 394 | /* must be the (first and) last |
| 395 | * descriptor then */ |
| 396 | pkt_buf = (u8 *)lp->rx_skb[lp->rx_next_done]->data; |
Florian Fainelli | ef11291 | 2008-03-19 17:14:51 +0100 | [diff] [blame] | 397 | |
Phil Sutter | 4cf83b6 | 2009-01-14 21:48:59 -0800 | [diff] [blame] | 398 | /* invalidate the cache */ |
| 399 | dma_cache_inv((unsigned long)pkt_buf, pkt_len - 4); |
Florian Fainelli | ef11291 | 2008-03-19 17:14:51 +0100 | [diff] [blame] | 400 | |
Phil Sutter | 4cf83b6 | 2009-01-14 21:48:59 -0800 | [diff] [blame] | 401 | /* Malloc up new buffer. */ |
Eric Dumazet | 89d71a6 | 2009-10-13 05:34:20 +0000 | [diff] [blame] | 402 | skb_new = netdev_alloc_skb_ip_align(dev, KORINA_RBSIZE); |
Florian Fainelli | ef11291 | 2008-03-19 17:14:51 +0100 | [diff] [blame] | 403 | |
Phil Sutter | 4cf83b6 | 2009-01-14 21:48:59 -0800 | [diff] [blame] | 404 | if (!skb_new) |
| 405 | break; |
| 406 | /* Do not count the CRC */ |
| 407 | skb_put(skb, pkt_len - 4); |
| 408 | skb->protocol = eth_type_trans(skb, dev); |
Florian Fainelli | ef11291 | 2008-03-19 17:14:51 +0100 | [diff] [blame] | 409 | |
Phil Sutter | 4cf83b6 | 2009-01-14 21:48:59 -0800 | [diff] [blame] | 410 | /* Pass the packet to upper layers */ |
| 411 | netif_receive_skb(skb); |
| 412 | dev->stats.rx_packets++; |
| 413 | dev->stats.rx_bytes += pkt_len; |
Florian Fainelli | ef11291 | 2008-03-19 17:14:51 +0100 | [diff] [blame] | 414 | |
Phil Sutter | 4cf83b6 | 2009-01-14 21:48:59 -0800 | [diff] [blame] | 415 | /* Update the mcast stats */ |
| 416 | if (devcs & ETH_RX_MP) |
| 417 | dev->stats.multicast++; |
Florian Fainelli | ef11291 | 2008-03-19 17:14:51 +0100 | [diff] [blame] | 418 | |
Phil Sutter | 4cf83b6 | 2009-01-14 21:48:59 -0800 | [diff] [blame] | 419 | lp->rx_skb[lp->rx_next_done] = skb_new; |
Florian Fainelli | ef11291 | 2008-03-19 17:14:51 +0100 | [diff] [blame] | 420 | } |
Phil Sutter | 4cf83b6 | 2009-01-14 21:48:59 -0800 | [diff] [blame] | 421 | |
| 422 | rd->devcs = 0; |
| 423 | |
| 424 | /* Restore descriptor's curr_addr */ |
| 425 | if (skb_new) |
| 426 | rd->ca = CPHYSADDR(skb_new->data); |
| 427 | else |
| 428 | rd->ca = CPHYSADDR(skb->data); |
| 429 | |
| 430 | rd->control = DMA_COUNT(KORINA_RBSIZE) | |
| 431 | DMA_DESC_COD | DMA_DESC_IOD; |
| 432 | lp->rd_ring[(lp->rx_next_done - 1) & |
| 433 | KORINA_RDS_MASK].control &= |
| 434 | ~DMA_DESC_COD; |
| 435 | |
| 436 | lp->rx_next_done = (lp->rx_next_done + 1) & KORINA_RDS_MASK; |
| 437 | dma_cache_wback((u32)rd, sizeof(*rd)); |
| 438 | rd = &lp->rd_ring[lp->rx_next_done]; |
| 439 | writel(~DMA_STAT_DONE, &lp->rx_dma_regs->dmas); |
Florian Fainelli | ef11291 | 2008-03-19 17:14:51 +0100 | [diff] [blame] | 440 | } |
| 441 | |
| 442 | dmas = readl(&lp->rx_dma_regs->dmas); |
| 443 | |
| 444 | if (dmas & DMA_STAT_HALT) { |
| 445 | writel(~(DMA_STAT_HALT | DMA_STAT_ERR), |
| 446 | &lp->rx_dma_regs->dmas); |
| 447 | |
| 448 | lp->dma_halt_cnt++; |
| 449 | rd->devcs = 0; |
| 450 | skb = lp->rx_skb[lp->rx_next_done]; |
| 451 | rd->ca = CPHYSADDR(skb->data); |
| 452 | dma_cache_wback((u32)rd, sizeof(*rd)); |
| 453 | korina_chain_rx(lp, rd); |
| 454 | } |
| 455 | |
| 456 | return count; |
| 457 | } |
| 458 | |
| 459 | static int korina_poll(struct napi_struct *napi, int budget) |
| 460 | { |
| 461 | struct korina_private *lp = |
| 462 | container_of(napi, struct korina_private, napi); |
| 463 | struct net_device *dev = lp->dev; |
| 464 | int work_done; |
| 465 | |
| 466 | work_done = korina_rx(dev, budget); |
| 467 | if (work_done < budget) { |
Ben Hutchings | 288379f | 2009-01-19 16:43:59 -0800 | [diff] [blame] | 468 | napi_complete(napi); |
Florian Fainelli | ef11291 | 2008-03-19 17:14:51 +0100 | [diff] [blame] | 469 | |
| 470 | writel(readl(&lp->rx_dma_regs->dmasm) & |
| 471 | ~(DMA_STAT_DONE | DMA_STAT_HALT | DMA_STAT_ERR), |
| 472 | &lp->rx_dma_regs->dmasm); |
| 473 | } |
| 474 | return work_done; |
| 475 | } |
| 476 | |
| 477 | /* |
| 478 | * Set or clear the multicast filter for this adaptor. |
| 479 | */ |
| 480 | static void korina_multicast_list(struct net_device *dev) |
| 481 | { |
| 482 | struct korina_private *lp = netdev_priv(dev); |
| 483 | unsigned long flags; |
Jiri Pirko | 22bedad3 | 2010-04-01 21:22:57 +0000 | [diff] [blame] | 484 | struct netdev_hw_addr *ha; |
Florian Fainelli | ef11291 | 2008-03-19 17:14:51 +0100 | [diff] [blame] | 485 | u32 recognise = ETH_ARC_AB; /* always accept broadcasts */ |
| 486 | int i; |
| 487 | |
| 488 | /* Set promiscuous mode */ |
| 489 | if (dev->flags & IFF_PROMISC) |
| 490 | recognise |= ETH_ARC_PRO; |
| 491 | |
Jiri Pirko | 4cd24ea | 2010-02-08 04:30:35 +0000 | [diff] [blame] | 492 | else if ((dev->flags & IFF_ALLMULTI) || (netdev_mc_count(dev) > 4)) |
Florian Fainelli | ef11291 | 2008-03-19 17:14:51 +0100 | [diff] [blame] | 493 | /* All multicast and broadcast */ |
| 494 | recognise |= ETH_ARC_AM; |
| 495 | |
| 496 | /* Build the hash table */ |
Jiri Pirko | 4cd24ea | 2010-02-08 04:30:35 +0000 | [diff] [blame] | 497 | if (netdev_mc_count(dev) > 4) { |
Emilio López | e998fd4 | 2013-05-17 10:42:56 +0000 | [diff] [blame] | 498 | u16 hash_table[4] = { 0 }; |
Florian Fainelli | ef11291 | 2008-03-19 17:14:51 +0100 | [diff] [blame] | 499 | u32 crc; |
| 500 | |
Jiri Pirko | 22bedad3 | 2010-04-01 21:22:57 +0000 | [diff] [blame] | 501 | netdev_for_each_mc_addr(ha, dev) { |
Tobias Klauser | 498d8e2 | 2011-07-07 22:06:26 +0000 | [diff] [blame] | 502 | crc = ether_crc_le(6, ha->addr); |
Florian Fainelli | ef11291 | 2008-03-19 17:14:51 +0100 | [diff] [blame] | 503 | crc >>= 26; |
| 504 | hash_table[crc >> 4] |= 1 << (15 - (crc & 0xf)); |
| 505 | } |
| 506 | /* Accept filtered multicast */ |
| 507 | recognise |= ETH_ARC_AFM; |
| 508 | |
| 509 | /* Fill the MAC hash tables with their values */ |
| 510 | writel((u32)(hash_table[1] << 16 | hash_table[0]), |
| 511 | &lp->eth_regs->ethhash0); |
| 512 | writel((u32)(hash_table[3] << 16 | hash_table[2]), |
| 513 | &lp->eth_regs->ethhash1); |
| 514 | } |
| 515 | |
| 516 | spin_lock_irqsave(&lp->lock, flags); |
| 517 | writel(recognise, &lp->eth_regs->etharc); |
| 518 | spin_unlock_irqrestore(&lp->lock, flags); |
| 519 | } |
| 520 | |
| 521 | static void korina_tx(struct net_device *dev) |
| 522 | { |
| 523 | struct korina_private *lp = netdev_priv(dev); |
| 524 | struct dma_desc *td = &lp->td_ring[lp->tx_next_done]; |
| 525 | u32 devcs; |
| 526 | u32 dmas; |
| 527 | |
| 528 | spin_lock(&lp->lock); |
| 529 | |
| 530 | /* Process all desc that are done */ |
| 531 | while (IS_DMA_FINISHED(td->control)) { |
| 532 | if (lp->tx_full == 1) { |
| 533 | netif_wake_queue(dev); |
| 534 | lp->tx_full = 0; |
| 535 | } |
| 536 | |
| 537 | devcs = lp->td_ring[lp->tx_next_done].devcs; |
| 538 | if ((devcs & (ETH_TX_FD | ETH_TX_LD)) != |
| 539 | (ETH_TX_FD | ETH_TX_LD)) { |
| 540 | dev->stats.tx_errors++; |
| 541 | dev->stats.tx_dropped++; |
| 542 | |
| 543 | /* Should never happen */ |
Phil Sutter | f16aea4 | 2009-08-12 12:22:46 +0000 | [diff] [blame] | 544 | printk(KERN_ERR "%s: split tx ignored\n", |
Florian Fainelli | ef11291 | 2008-03-19 17:14:51 +0100 | [diff] [blame] | 545 | dev->name); |
| 546 | } else if (devcs & ETH_TX_TOK) { |
| 547 | dev->stats.tx_packets++; |
| 548 | dev->stats.tx_bytes += |
| 549 | lp->tx_skb[lp->tx_next_done]->len; |
| 550 | } else { |
| 551 | dev->stats.tx_errors++; |
| 552 | dev->stats.tx_dropped++; |
| 553 | |
| 554 | /* Underflow */ |
| 555 | if (devcs & ETH_TX_UND) |
| 556 | dev->stats.tx_fifo_errors++; |
| 557 | |
| 558 | /* Oversized frame */ |
| 559 | if (devcs & ETH_TX_OF) |
| 560 | dev->stats.tx_aborted_errors++; |
| 561 | |
| 562 | /* Excessive deferrals */ |
| 563 | if (devcs & ETH_TX_ED) |
| 564 | dev->stats.tx_carrier_errors++; |
| 565 | |
| 566 | /* Collisions: medium busy */ |
| 567 | if (devcs & ETH_TX_EC) |
| 568 | dev->stats.collisions++; |
| 569 | |
| 570 | /* Late collision */ |
| 571 | if (devcs & ETH_TX_LC) |
| 572 | dev->stats.tx_window_errors++; |
| 573 | } |
| 574 | |
| 575 | /* We must always free the original skb */ |
| 576 | if (lp->tx_skb[lp->tx_next_done]) { |
| 577 | dev_kfree_skb_any(lp->tx_skb[lp->tx_next_done]); |
| 578 | lp->tx_skb[lp->tx_next_done] = NULL; |
| 579 | } |
| 580 | |
| 581 | lp->td_ring[lp->tx_next_done].control = DMA_DESC_IOF; |
| 582 | lp->td_ring[lp->tx_next_done].devcs = ETH_TX_FD | ETH_TX_LD; |
| 583 | lp->td_ring[lp->tx_next_done].link = 0; |
| 584 | lp->td_ring[lp->tx_next_done].ca = 0; |
| 585 | lp->tx_count--; |
| 586 | |
| 587 | /* Go on to next transmission */ |
| 588 | lp->tx_next_done = (lp->tx_next_done + 1) & KORINA_TDS_MASK; |
| 589 | td = &lp->td_ring[lp->tx_next_done]; |
| 590 | |
| 591 | } |
| 592 | |
| 593 | /* Clear the DMA status register */ |
| 594 | dmas = readl(&lp->tx_dma_regs->dmas); |
| 595 | writel(~dmas, &lp->tx_dma_regs->dmas); |
| 596 | |
| 597 | writel(readl(&lp->tx_dma_regs->dmasm) & |
| 598 | ~(DMA_STAT_FINI | DMA_STAT_ERR), |
| 599 | &lp->tx_dma_regs->dmasm); |
| 600 | |
| 601 | spin_unlock(&lp->lock); |
| 602 | } |
| 603 | |
| 604 | static irqreturn_t |
| 605 | korina_tx_dma_interrupt(int irq, void *dev_id) |
| 606 | { |
| 607 | struct net_device *dev = dev_id; |
| 608 | struct korina_private *lp = netdev_priv(dev); |
| 609 | u32 dmas, dmasm; |
| 610 | irqreturn_t retval; |
| 611 | |
| 612 | dmas = readl(&lp->tx_dma_regs->dmas); |
| 613 | |
| 614 | if (dmas & (DMA_STAT_FINI | DMA_STAT_ERR)) { |
Florian Fainelli | ef11291 | 2008-03-19 17:14:51 +0100 | [diff] [blame] | 615 | dmasm = readl(&lp->tx_dma_regs->dmasm); |
| 616 | writel(dmasm | (DMA_STAT_FINI | DMA_STAT_ERR), |
| 617 | &lp->tx_dma_regs->dmasm); |
| 618 | |
Phil Sutter | 60d3f98 | 2009-01-14 21:50:12 -0800 | [diff] [blame] | 619 | korina_tx(dev); |
| 620 | |
Florian Fainelli | ef11291 | 2008-03-19 17:14:51 +0100 | [diff] [blame] | 621 | if (lp->tx_chain_status == desc_filled && |
| 622 | (readl(&(lp->tx_dma_regs->dmandptr)) == 0)) { |
| 623 | writel(CPHYSADDR(&lp->td_ring[lp->tx_chain_head]), |
| 624 | &(lp->tx_dma_regs->dmandptr)); |
| 625 | lp->tx_chain_status = desc_empty; |
| 626 | lp->tx_chain_head = lp->tx_chain_tail; |
| 627 | dev->trans_start = jiffies; |
| 628 | } |
| 629 | if (dmas & DMA_STAT_ERR) |
Phil Sutter | f16aea4 | 2009-08-12 12:22:46 +0000 | [diff] [blame] | 630 | printk(KERN_ERR "%s: DMA error\n", dev->name); |
Florian Fainelli | ef11291 | 2008-03-19 17:14:51 +0100 | [diff] [blame] | 631 | |
| 632 | retval = IRQ_HANDLED; |
| 633 | } else |
| 634 | retval = IRQ_NONE; |
| 635 | |
| 636 | return retval; |
| 637 | } |
| 638 | |
| 639 | |
| 640 | static void korina_check_media(struct net_device *dev, unsigned int init_media) |
| 641 | { |
| 642 | struct korina_private *lp = netdev_priv(dev); |
| 643 | |
| 644 | mii_check_media(&lp->mii_if, 0, init_media); |
| 645 | |
| 646 | if (lp->mii_if.full_duplex) |
| 647 | writel(readl(&lp->eth_regs->ethmac2) | ETH_MAC2_FD, |
| 648 | &lp->eth_regs->ethmac2); |
| 649 | else |
| 650 | writel(readl(&lp->eth_regs->ethmac2) & ~ETH_MAC2_FD, |
| 651 | &lp->eth_regs->ethmac2); |
| 652 | } |
| 653 | |
Florian Fainelli | 4d5ef9f | 2009-05-28 00:58:41 +0000 | [diff] [blame] | 654 | static void korina_poll_media(unsigned long data) |
| 655 | { |
| 656 | struct net_device *dev = (struct net_device *) data; |
| 657 | struct korina_private *lp = netdev_priv(dev); |
| 658 | |
| 659 | korina_check_media(dev, 0); |
| 660 | mod_timer(&lp->media_check_timer, jiffies + HZ); |
| 661 | } |
| 662 | |
Florian Fainelli | ef11291 | 2008-03-19 17:14:51 +0100 | [diff] [blame] | 663 | static void korina_set_carrier(struct mii_if_info *mii) |
| 664 | { |
| 665 | if (mii->force_media) { |
| 666 | /* autoneg is off: Link is always assumed to be up */ |
| 667 | if (!netif_carrier_ok(mii->dev)) |
| 668 | netif_carrier_on(mii->dev); |
| 669 | } else /* Let MMI library update carrier status */ |
| 670 | korina_check_media(mii->dev, 0); |
| 671 | } |
| 672 | |
| 673 | static int korina_ioctl(struct net_device *dev, struct ifreq *rq, int cmd) |
| 674 | { |
| 675 | struct korina_private *lp = netdev_priv(dev); |
| 676 | struct mii_ioctl_data *data = if_mii(rq); |
| 677 | int rc; |
| 678 | |
| 679 | if (!netif_running(dev)) |
| 680 | return -EINVAL; |
| 681 | spin_lock_irq(&lp->lock); |
| 682 | rc = generic_mii_ioctl(&lp->mii_if, data, cmd, NULL); |
| 683 | spin_unlock_irq(&lp->lock); |
| 684 | korina_set_carrier(&lp->mii_if); |
| 685 | |
| 686 | return rc; |
| 687 | } |
| 688 | |
| 689 | /* ethtool helpers */ |
| 690 | static void netdev_get_drvinfo(struct net_device *dev, |
| 691 | struct ethtool_drvinfo *info) |
| 692 | { |
| 693 | struct korina_private *lp = netdev_priv(dev); |
| 694 | |
Jiri Pirko | 7826d43 | 2013-01-06 00:44:26 +0000 | [diff] [blame] | 695 | strlcpy(info->driver, DRV_NAME, sizeof(info->driver)); |
| 696 | strlcpy(info->version, DRV_VERSION, sizeof(info->version)); |
| 697 | strlcpy(info->bus_info, lp->dev->name, sizeof(info->bus_info)); |
Florian Fainelli | ef11291 | 2008-03-19 17:14:51 +0100 | [diff] [blame] | 698 | } |
| 699 | |
| 700 | static int netdev_get_settings(struct net_device *dev, struct ethtool_cmd *cmd) |
| 701 | { |
| 702 | struct korina_private *lp = netdev_priv(dev); |
| 703 | int rc; |
| 704 | |
| 705 | spin_lock_irq(&lp->lock); |
| 706 | rc = mii_ethtool_gset(&lp->mii_if, cmd); |
| 707 | spin_unlock_irq(&lp->lock); |
| 708 | |
| 709 | return rc; |
| 710 | } |
| 711 | |
| 712 | static int netdev_set_settings(struct net_device *dev, struct ethtool_cmd *cmd) |
| 713 | { |
| 714 | struct korina_private *lp = netdev_priv(dev); |
| 715 | int rc; |
| 716 | |
| 717 | spin_lock_irq(&lp->lock); |
| 718 | rc = mii_ethtool_sset(&lp->mii_if, cmd); |
| 719 | spin_unlock_irq(&lp->lock); |
| 720 | korina_set_carrier(&lp->mii_if); |
| 721 | |
| 722 | return rc; |
| 723 | } |
| 724 | |
| 725 | static u32 netdev_get_link(struct net_device *dev) |
| 726 | { |
| 727 | struct korina_private *lp = netdev_priv(dev); |
| 728 | |
| 729 | return mii_link_ok(&lp->mii_if); |
| 730 | } |
| 731 | |
Stephen Hemminger | 0fc0b73 | 2009-09-02 01:03:33 -0700 | [diff] [blame] | 732 | static const struct ethtool_ops netdev_ethtool_ops = { |
Florian Fainelli | ef11291 | 2008-03-19 17:14:51 +0100 | [diff] [blame] | 733 | .get_drvinfo = netdev_get_drvinfo, |
| 734 | .get_settings = netdev_get_settings, |
| 735 | .set_settings = netdev_set_settings, |
| 736 | .get_link = netdev_get_link, |
| 737 | }; |
| 738 | |
Phil Sutter | 7010837 | 2009-08-12 12:52:32 +0000 | [diff] [blame] | 739 | static int korina_alloc_ring(struct net_device *dev) |
Florian Fainelli | ef11291 | 2008-03-19 17:14:51 +0100 | [diff] [blame] | 740 | { |
| 741 | struct korina_private *lp = netdev_priv(dev); |
Phil Sutter | e85bf47 | 2009-01-15 12:29:57 +0000 | [diff] [blame] | 742 | struct sk_buff *skb; |
Florian Fainelli | ef11291 | 2008-03-19 17:14:51 +0100 | [diff] [blame] | 743 | int i; |
| 744 | |
| 745 | /* Initialize the transmit descriptors */ |
| 746 | for (i = 0; i < KORINA_NUM_TDS; i++) { |
| 747 | lp->td_ring[i].control = DMA_DESC_IOF; |
| 748 | lp->td_ring[i].devcs = ETH_TX_FD | ETH_TX_LD; |
| 749 | lp->td_ring[i].ca = 0; |
| 750 | lp->td_ring[i].link = 0; |
| 751 | } |
| 752 | lp->tx_next_done = lp->tx_chain_head = lp->tx_chain_tail = |
| 753 | lp->tx_full = lp->tx_count = 0; |
| 754 | lp->tx_chain_status = desc_empty; |
| 755 | |
| 756 | /* Initialize the receive descriptors */ |
| 757 | for (i = 0; i < KORINA_NUM_RDS; i++) { |
Phil Sutter | 53ee490 | 2010-05-29 13:23:35 +0000 | [diff] [blame] | 758 | skb = netdev_alloc_skb_ip_align(dev, KORINA_RBSIZE); |
Florian Fainelli | ef11291 | 2008-03-19 17:14:51 +0100 | [diff] [blame] | 759 | if (!skb) |
Phil Sutter | 7010837 | 2009-08-12 12:52:32 +0000 | [diff] [blame] | 760 | return -ENOMEM; |
Florian Fainelli | ef11291 | 2008-03-19 17:14:51 +0100 | [diff] [blame] | 761 | lp->rx_skb[i] = skb; |
| 762 | lp->rd_ring[i].control = DMA_DESC_IOD | |
| 763 | DMA_COUNT(KORINA_RBSIZE); |
| 764 | lp->rd_ring[i].devcs = 0; |
| 765 | lp->rd_ring[i].ca = CPHYSADDR(skb->data); |
| 766 | lp->rd_ring[i].link = CPHYSADDR(&lp->rd_ring[i+1]); |
| 767 | } |
| 768 | |
Phil Sutter | 6a2fe98 | 2009-01-15 12:29:55 +0000 | [diff] [blame] | 769 | /* loop back receive descriptors, so the last |
| 770 | * descriptor points to the first one */ |
| 771 | lp->rd_ring[i - 1].link = CPHYSADDR(&lp->rd_ring[0]); |
| 772 | lp->rd_ring[i - 1].control |= DMA_DESC_COD; |
Florian Fainelli | ef11291 | 2008-03-19 17:14:51 +0100 | [diff] [blame] | 773 | |
Phil Sutter | 6a2fe98 | 2009-01-15 12:29:55 +0000 | [diff] [blame] | 774 | lp->rx_next_done = 0; |
Florian Fainelli | ef11291 | 2008-03-19 17:14:51 +0100 | [diff] [blame] | 775 | lp->rx_chain_head = 0; |
| 776 | lp->rx_chain_tail = 0; |
| 777 | lp->rx_chain_status = desc_empty; |
Phil Sutter | 7010837 | 2009-08-12 12:52:32 +0000 | [diff] [blame] | 778 | |
| 779 | return 0; |
Florian Fainelli | ef11291 | 2008-03-19 17:14:51 +0100 | [diff] [blame] | 780 | } |
| 781 | |
| 782 | static void korina_free_ring(struct net_device *dev) |
| 783 | { |
| 784 | struct korina_private *lp = netdev_priv(dev); |
| 785 | int i; |
| 786 | |
| 787 | for (i = 0; i < KORINA_NUM_RDS; i++) { |
| 788 | lp->rd_ring[i].control = 0; |
| 789 | if (lp->rx_skb[i]) |
| 790 | dev_kfree_skb_any(lp->rx_skb[i]); |
| 791 | lp->rx_skb[i] = NULL; |
| 792 | } |
| 793 | |
| 794 | for (i = 0; i < KORINA_NUM_TDS; i++) { |
| 795 | lp->td_ring[i].control = 0; |
| 796 | if (lp->tx_skb[i]) |
| 797 | dev_kfree_skb_any(lp->tx_skb[i]); |
| 798 | lp->tx_skb[i] = NULL; |
| 799 | } |
| 800 | } |
| 801 | |
| 802 | /* |
| 803 | * Initialize the RC32434 ethernet controller. |
| 804 | */ |
| 805 | static int korina_init(struct net_device *dev) |
| 806 | { |
| 807 | struct korina_private *lp = netdev_priv(dev); |
| 808 | |
| 809 | /* Disable DMA */ |
| 810 | korina_abort_tx(dev); |
| 811 | korina_abort_rx(dev); |
| 812 | |
| 813 | /* reset ethernet logic */ |
| 814 | writel(0, &lp->eth_regs->ethintfc); |
| 815 | while ((readl(&lp->eth_regs->ethintfc) & ETH_INT_FC_RIP)) |
| 816 | dev->trans_start = jiffies; |
| 817 | |
| 818 | /* Enable Ethernet Interface */ |
| 819 | writel(ETH_INT_FC_EN, &lp->eth_regs->ethintfc); |
| 820 | |
| 821 | /* Allocate rings */ |
Phil Sutter | 7010837 | 2009-08-12 12:52:32 +0000 | [diff] [blame] | 822 | if (korina_alloc_ring(dev)) { |
| 823 | printk(KERN_ERR "%s: descriptor allocation failed\n", dev->name); |
| 824 | korina_free_ring(dev); |
| 825 | return -ENOMEM; |
| 826 | } |
Florian Fainelli | ef11291 | 2008-03-19 17:14:51 +0100 | [diff] [blame] | 827 | |
| 828 | writel(0, &lp->rx_dma_regs->dmas); |
| 829 | /* Start Rx DMA */ |
| 830 | korina_start_rx(lp, &lp->rd_ring[0]); |
| 831 | |
| 832 | writel(readl(&lp->tx_dma_regs->dmasm) & |
| 833 | ~(DMA_STAT_FINI | DMA_STAT_ERR), |
| 834 | &lp->tx_dma_regs->dmasm); |
| 835 | writel(readl(&lp->rx_dma_regs->dmasm) & |
| 836 | ~(DMA_STAT_DONE | DMA_STAT_HALT | DMA_STAT_ERR), |
| 837 | &lp->rx_dma_regs->dmasm); |
| 838 | |
| 839 | /* Accept only packets destined for this Ethernet device address */ |
| 840 | writel(ETH_ARC_AB, &lp->eth_regs->etharc); |
| 841 | |
| 842 | /* Set all Ether station address registers to their initial values */ |
| 843 | writel(STATION_ADDRESS_LOW(dev), &lp->eth_regs->ethsal0); |
| 844 | writel(STATION_ADDRESS_HIGH(dev), &lp->eth_regs->ethsah0); |
| 845 | |
| 846 | writel(STATION_ADDRESS_LOW(dev), &lp->eth_regs->ethsal1); |
| 847 | writel(STATION_ADDRESS_HIGH(dev), &lp->eth_regs->ethsah1); |
| 848 | |
| 849 | writel(STATION_ADDRESS_LOW(dev), &lp->eth_regs->ethsal2); |
| 850 | writel(STATION_ADDRESS_HIGH(dev), &lp->eth_regs->ethsah2); |
| 851 | |
| 852 | writel(STATION_ADDRESS_LOW(dev), &lp->eth_regs->ethsal3); |
| 853 | writel(STATION_ADDRESS_HIGH(dev), &lp->eth_regs->ethsah3); |
| 854 | |
| 855 | |
| 856 | /* Frame Length Checking, Pad Enable, CRC Enable, Full Duplex set */ |
| 857 | writel(ETH_MAC2_PE | ETH_MAC2_CEN | ETH_MAC2_FD, |
| 858 | &lp->eth_regs->ethmac2); |
| 859 | |
| 860 | /* Back to back inter-packet-gap */ |
| 861 | writel(0x15, &lp->eth_regs->ethipgt); |
| 862 | /* Non - Back to back inter-packet-gap */ |
| 863 | writel(0x12, &lp->eth_regs->ethipgr); |
| 864 | |
| 865 | /* Management Clock Prescaler Divisor |
| 866 | * Clock independent setting */ |
| 867 | writel(((idt_cpu_freq) / MII_CLOCK + 1) & ~1, |
| 868 | &lp->eth_regs->ethmcp); |
| 869 | |
| 870 | /* don't transmit until fifo contains 48b */ |
| 871 | writel(48, &lp->eth_regs->ethfifott); |
| 872 | |
| 873 | writel(ETH_MAC1_RE, &lp->eth_regs->ethmac1); |
| 874 | |
| 875 | napi_enable(&lp->napi); |
| 876 | netif_start_queue(dev); |
| 877 | |
| 878 | return 0; |
| 879 | } |
| 880 | |
| 881 | /* |
| 882 | * Restart the RC32434 ethernet controller. |
Florian Fainelli | ef11291 | 2008-03-19 17:14:51 +0100 | [diff] [blame] | 883 | */ |
Phil Sutter | ceb3d23 | 2010-05-29 13:23:34 +0000 | [diff] [blame] | 884 | static void korina_restart_task(struct work_struct *work) |
Florian Fainelli | ef11291 | 2008-03-19 17:14:51 +0100 | [diff] [blame] | 885 | { |
Phil Sutter | ceb3d23 | 2010-05-29 13:23:34 +0000 | [diff] [blame] | 886 | struct korina_private *lp = container_of(work, |
| 887 | struct korina_private, restart_task); |
| 888 | struct net_device *dev = lp->dev; |
Florian Fainelli | ef11291 | 2008-03-19 17:14:51 +0100 | [diff] [blame] | 889 | |
| 890 | /* |
| 891 | * Disable interrupts |
| 892 | */ |
| 893 | disable_irq(lp->rx_irq); |
| 894 | disable_irq(lp->tx_irq); |
| 895 | disable_irq(lp->ovr_irq); |
| 896 | disable_irq(lp->und_irq); |
| 897 | |
| 898 | writel(readl(&lp->tx_dma_regs->dmasm) | |
| 899 | DMA_STAT_FINI | DMA_STAT_ERR, |
| 900 | &lp->tx_dma_regs->dmasm); |
| 901 | writel(readl(&lp->rx_dma_regs->dmasm) | |
| 902 | DMA_STAT_DONE | DMA_STAT_HALT | DMA_STAT_ERR, |
| 903 | &lp->rx_dma_regs->dmasm); |
| 904 | |
| 905 | korina_free_ring(dev); |
| 906 | |
Phil Sutter | beb0bab | 2009-01-14 21:48:24 -0800 | [diff] [blame] | 907 | napi_disable(&lp->napi); |
| 908 | |
Phil Sutter | ceb3d23 | 2010-05-29 13:23:34 +0000 | [diff] [blame] | 909 | if (korina_init(dev) < 0) { |
Phil Sutter | f16aea4 | 2009-08-12 12:22:46 +0000 | [diff] [blame] | 910 | printk(KERN_ERR "%s: cannot restart device\n", dev->name); |
Phil Sutter | ceb3d23 | 2010-05-29 13:23:34 +0000 | [diff] [blame] | 911 | return; |
Florian Fainelli | ef11291 | 2008-03-19 17:14:51 +0100 | [diff] [blame] | 912 | } |
| 913 | korina_multicast_list(dev); |
| 914 | |
| 915 | enable_irq(lp->und_irq); |
| 916 | enable_irq(lp->ovr_irq); |
| 917 | enable_irq(lp->tx_irq); |
| 918 | enable_irq(lp->rx_irq); |
Florian Fainelli | ef11291 | 2008-03-19 17:14:51 +0100 | [diff] [blame] | 919 | } |
| 920 | |
| 921 | static void korina_clear_and_restart(struct net_device *dev, u32 value) |
| 922 | { |
| 923 | struct korina_private *lp = netdev_priv(dev); |
| 924 | |
| 925 | netif_stop_queue(dev); |
| 926 | writel(value, &lp->eth_regs->ethintfc); |
Phil Sutter | ceb3d23 | 2010-05-29 13:23:34 +0000 | [diff] [blame] | 927 | schedule_work(&lp->restart_task); |
Florian Fainelli | ef11291 | 2008-03-19 17:14:51 +0100 | [diff] [blame] | 928 | } |
| 929 | |
| 930 | /* Ethernet Tx Underflow interrupt */ |
| 931 | static irqreturn_t korina_und_interrupt(int irq, void *dev_id) |
| 932 | { |
| 933 | struct net_device *dev = dev_id; |
| 934 | struct korina_private *lp = netdev_priv(dev); |
| 935 | unsigned int und; |
| 936 | |
| 937 | spin_lock(&lp->lock); |
| 938 | |
| 939 | und = readl(&lp->eth_regs->ethintfc); |
| 940 | |
| 941 | if (und & ETH_INT_FC_UND) |
| 942 | korina_clear_and_restart(dev, und & ~ETH_INT_FC_UND); |
| 943 | |
| 944 | spin_unlock(&lp->lock); |
| 945 | |
| 946 | return IRQ_HANDLED; |
| 947 | } |
| 948 | |
| 949 | static void korina_tx_timeout(struct net_device *dev) |
| 950 | { |
| 951 | struct korina_private *lp = netdev_priv(dev); |
Florian Fainelli | ef11291 | 2008-03-19 17:14:51 +0100 | [diff] [blame] | 952 | |
Phil Sutter | ceb3d23 | 2010-05-29 13:23:34 +0000 | [diff] [blame] | 953 | schedule_work(&lp->restart_task); |
Florian Fainelli | ef11291 | 2008-03-19 17:14:51 +0100 | [diff] [blame] | 954 | } |
| 955 | |
| 956 | /* Ethernet Rx Overflow interrupt */ |
| 957 | static irqreturn_t |
| 958 | korina_ovr_interrupt(int irq, void *dev_id) |
| 959 | { |
| 960 | struct net_device *dev = dev_id; |
| 961 | struct korina_private *lp = netdev_priv(dev); |
| 962 | unsigned int ovr; |
| 963 | |
| 964 | spin_lock(&lp->lock); |
| 965 | ovr = readl(&lp->eth_regs->ethintfc); |
| 966 | |
| 967 | if (ovr & ETH_INT_FC_OVR) |
| 968 | korina_clear_and_restart(dev, ovr & ~ETH_INT_FC_OVR); |
| 969 | |
| 970 | spin_unlock(&lp->lock); |
| 971 | |
| 972 | return IRQ_HANDLED; |
| 973 | } |
| 974 | |
| 975 | #ifdef CONFIG_NET_POLL_CONTROLLER |
| 976 | static void korina_poll_controller(struct net_device *dev) |
| 977 | { |
| 978 | disable_irq(dev->irq); |
| 979 | korina_tx_dma_interrupt(dev->irq, dev); |
| 980 | enable_irq(dev->irq); |
| 981 | } |
| 982 | #endif |
| 983 | |
| 984 | static int korina_open(struct net_device *dev) |
| 985 | { |
| 986 | struct korina_private *lp = netdev_priv(dev); |
Francois Romieu | e3152ab | 2008-04-20 18:06:13 +0200 | [diff] [blame] | 987 | int ret; |
Florian Fainelli | ef11291 | 2008-03-19 17:14:51 +0100 | [diff] [blame] | 988 | |
| 989 | /* Initialize */ |
| 990 | ret = korina_init(dev); |
| 991 | if (ret < 0) { |
Phil Sutter | f16aea4 | 2009-08-12 12:22:46 +0000 | [diff] [blame] | 992 | printk(KERN_ERR "%s: cannot open device\n", dev->name); |
Florian Fainelli | ef11291 | 2008-03-19 17:14:51 +0100 | [diff] [blame] | 993 | goto out; |
| 994 | } |
| 995 | |
| 996 | /* Install the interrupt handler |
| 997 | * that handles the Done Finished |
| 998 | * Ovr and Und Events */ |
Joe Perches | a0607fd | 2009-11-18 23:29:17 -0800 | [diff] [blame] | 999 | ret = request_irq(lp->rx_irq, korina_rx_dma_interrupt, |
Phil Sutter | 1c5625c | 2009-01-14 21:51:48 -0800 | [diff] [blame] | 1000 | IRQF_DISABLED, "Korina ethernet Rx", dev); |
Florian Fainelli | ef11291 | 2008-03-19 17:14:51 +0100 | [diff] [blame] | 1001 | if (ret < 0) { |
Phil Sutter | f16aea4 | 2009-08-12 12:22:46 +0000 | [diff] [blame] | 1002 | printk(KERN_ERR "%s: unable to get Rx DMA IRQ %d\n", |
Florian Fainelli | ef11291 | 2008-03-19 17:14:51 +0100 | [diff] [blame] | 1003 | dev->name, lp->rx_irq); |
| 1004 | goto err_release; |
| 1005 | } |
Joe Perches | a0607fd | 2009-11-18 23:29:17 -0800 | [diff] [blame] | 1006 | ret = request_irq(lp->tx_irq, korina_tx_dma_interrupt, |
Phil Sutter | 1c5625c | 2009-01-14 21:51:48 -0800 | [diff] [blame] | 1007 | IRQF_DISABLED, "Korina ethernet Tx", dev); |
Florian Fainelli | ef11291 | 2008-03-19 17:14:51 +0100 | [diff] [blame] | 1008 | if (ret < 0) { |
Phil Sutter | f16aea4 | 2009-08-12 12:22:46 +0000 | [diff] [blame] | 1009 | printk(KERN_ERR "%s: unable to get Tx DMA IRQ %d\n", |
Florian Fainelli | ef11291 | 2008-03-19 17:14:51 +0100 | [diff] [blame] | 1010 | dev->name, lp->tx_irq); |
| 1011 | goto err_free_rx_irq; |
| 1012 | } |
| 1013 | |
| 1014 | /* Install handler for overrun error. */ |
Joe Perches | a0607fd | 2009-11-18 23:29:17 -0800 | [diff] [blame] | 1015 | ret = request_irq(lp->ovr_irq, korina_ovr_interrupt, |
Phil Sutter | 1c5625c | 2009-01-14 21:51:48 -0800 | [diff] [blame] | 1016 | IRQF_DISABLED, "Ethernet Overflow", dev); |
Florian Fainelli | ef11291 | 2008-03-19 17:14:51 +0100 | [diff] [blame] | 1017 | if (ret < 0) { |
Phil Sutter | f16aea4 | 2009-08-12 12:22:46 +0000 | [diff] [blame] | 1018 | printk(KERN_ERR "%s: unable to get OVR IRQ %d\n", |
Florian Fainelli | ef11291 | 2008-03-19 17:14:51 +0100 | [diff] [blame] | 1019 | dev->name, lp->ovr_irq); |
| 1020 | goto err_free_tx_irq; |
| 1021 | } |
| 1022 | |
| 1023 | /* Install handler for underflow error. */ |
Joe Perches | a0607fd | 2009-11-18 23:29:17 -0800 | [diff] [blame] | 1024 | ret = request_irq(lp->und_irq, korina_und_interrupt, |
Phil Sutter | 1c5625c | 2009-01-14 21:51:48 -0800 | [diff] [blame] | 1025 | IRQF_DISABLED, "Ethernet Underflow", dev); |
Florian Fainelli | ef11291 | 2008-03-19 17:14:51 +0100 | [diff] [blame] | 1026 | if (ret < 0) { |
Phil Sutter | f16aea4 | 2009-08-12 12:22:46 +0000 | [diff] [blame] | 1027 | printk(KERN_ERR "%s: unable to get UND IRQ %d\n", |
Florian Fainelli | ef11291 | 2008-03-19 17:14:51 +0100 | [diff] [blame] | 1028 | dev->name, lp->und_irq); |
| 1029 | goto err_free_ovr_irq; |
| 1030 | } |
Florian Fainelli | 4d5ef9f | 2009-05-28 00:58:41 +0000 | [diff] [blame] | 1031 | mod_timer(&lp->media_check_timer, jiffies + 1); |
Francois Romieu | 751c2e4 | 2008-04-20 18:05:31 +0200 | [diff] [blame] | 1032 | out: |
| 1033 | return ret; |
Florian Fainelli | ef11291 | 2008-03-19 17:14:51 +0100 | [diff] [blame] | 1034 | |
| 1035 | err_free_ovr_irq: |
| 1036 | free_irq(lp->ovr_irq, dev); |
| 1037 | err_free_tx_irq: |
| 1038 | free_irq(lp->tx_irq, dev); |
| 1039 | err_free_rx_irq: |
| 1040 | free_irq(lp->rx_irq, dev); |
| 1041 | err_release: |
| 1042 | korina_free_ring(dev); |
| 1043 | goto out; |
Florian Fainelli | ef11291 | 2008-03-19 17:14:51 +0100 | [diff] [blame] | 1044 | } |
| 1045 | |
| 1046 | static int korina_close(struct net_device *dev) |
| 1047 | { |
| 1048 | struct korina_private *lp = netdev_priv(dev); |
| 1049 | u32 tmp; |
| 1050 | |
Florian Fainelli | 4d5ef9f | 2009-05-28 00:58:41 +0000 | [diff] [blame] | 1051 | del_timer(&lp->media_check_timer); |
| 1052 | |
Florian Fainelli | ef11291 | 2008-03-19 17:14:51 +0100 | [diff] [blame] | 1053 | /* Disable interrupts */ |
| 1054 | disable_irq(lp->rx_irq); |
| 1055 | disable_irq(lp->tx_irq); |
| 1056 | disable_irq(lp->ovr_irq); |
| 1057 | disable_irq(lp->und_irq); |
| 1058 | |
| 1059 | korina_abort_tx(dev); |
| 1060 | tmp = readl(&lp->tx_dma_regs->dmasm); |
| 1061 | tmp = tmp | DMA_STAT_FINI | DMA_STAT_ERR; |
| 1062 | writel(tmp, &lp->tx_dma_regs->dmasm); |
| 1063 | |
| 1064 | korina_abort_rx(dev); |
| 1065 | tmp = readl(&lp->rx_dma_regs->dmasm); |
| 1066 | tmp = tmp | DMA_STAT_DONE | DMA_STAT_HALT | DMA_STAT_ERR; |
| 1067 | writel(tmp, &lp->rx_dma_regs->dmasm); |
| 1068 | |
| 1069 | korina_free_ring(dev); |
| 1070 | |
Phil Sutter | beb0bab | 2009-01-14 21:48:24 -0800 | [diff] [blame] | 1071 | napi_disable(&lp->napi); |
| 1072 | |
Phil Sutter | ceb3d23 | 2010-05-29 13:23:34 +0000 | [diff] [blame] | 1073 | cancel_work_sync(&lp->restart_task); |
| 1074 | |
Florian Fainelli | ef11291 | 2008-03-19 17:14:51 +0100 | [diff] [blame] | 1075 | free_irq(lp->rx_irq, dev); |
| 1076 | free_irq(lp->tx_irq, dev); |
| 1077 | free_irq(lp->ovr_irq, dev); |
| 1078 | free_irq(lp->und_irq, dev); |
| 1079 | |
| 1080 | return 0; |
| 1081 | } |
| 1082 | |
Alexander Beregalov | 52b031f | 2009-04-15 12:52:46 +0000 | [diff] [blame] | 1083 | static const struct net_device_ops korina_netdev_ops = { |
| 1084 | .ndo_open = korina_open, |
| 1085 | .ndo_stop = korina_close, |
| 1086 | .ndo_start_xmit = korina_send_packet, |
Jiri Pirko | afc4b13 | 2011-08-16 06:29:01 +0000 | [diff] [blame] | 1087 | .ndo_set_rx_mode = korina_multicast_list, |
Alexander Beregalov | 52b031f | 2009-04-15 12:52:46 +0000 | [diff] [blame] | 1088 | .ndo_tx_timeout = korina_tx_timeout, |
| 1089 | .ndo_do_ioctl = korina_ioctl, |
| 1090 | .ndo_change_mtu = eth_change_mtu, |
| 1091 | .ndo_validate_addr = eth_validate_addr, |
| 1092 | .ndo_set_mac_address = eth_mac_addr, |
| 1093 | #ifdef CONFIG_NET_POLL_CONTROLLER |
| 1094 | .ndo_poll_controller = korina_poll_controller, |
| 1095 | #endif |
| 1096 | }; |
| 1097 | |
Florian Fainelli | ef11291 | 2008-03-19 17:14:51 +0100 | [diff] [blame] | 1098 | static int korina_probe(struct platform_device *pdev) |
| 1099 | { |
| 1100 | struct korina_device *bif = platform_get_drvdata(pdev); |
| 1101 | struct korina_private *lp; |
| 1102 | struct net_device *dev; |
| 1103 | struct resource *r; |
Francois Romieu | e3152ab | 2008-04-20 18:06:13 +0200 | [diff] [blame] | 1104 | int rc; |
Florian Fainelli | ef11291 | 2008-03-19 17:14:51 +0100 | [diff] [blame] | 1105 | |
| 1106 | dev = alloc_etherdev(sizeof(struct korina_private)); |
Joe Perches | 41de8d4 | 2012-01-29 13:47:52 +0000 | [diff] [blame] | 1107 | if (!dev) |
Florian Fainelli | ef11291 | 2008-03-19 17:14:51 +0100 | [diff] [blame] | 1108 | return -ENOMEM; |
Joe Perches | 41de8d4 | 2012-01-29 13:47:52 +0000 | [diff] [blame] | 1109 | |
Florian Fainelli | ef11291 | 2008-03-19 17:14:51 +0100 | [diff] [blame] | 1110 | SET_NETDEV_DEV(dev, &pdev->dev); |
Florian Fainelli | ef11291 | 2008-03-19 17:14:51 +0100 | [diff] [blame] | 1111 | lp = netdev_priv(dev); |
| 1112 | |
| 1113 | bif->dev = dev; |
| 1114 | memcpy(dev->dev_addr, bif->mac, 6); |
| 1115 | |
| 1116 | lp->rx_irq = platform_get_irq_byname(pdev, "korina_rx"); |
| 1117 | lp->tx_irq = platform_get_irq_byname(pdev, "korina_tx"); |
| 1118 | lp->ovr_irq = platform_get_irq_byname(pdev, "korina_ovr"); |
| 1119 | lp->und_irq = platform_get_irq_byname(pdev, "korina_und"); |
| 1120 | |
| 1121 | r = platform_get_resource_byname(pdev, IORESOURCE_MEM, "korina_regs"); |
| 1122 | dev->base_addr = r->start; |
Dan Carpenter | 3801326 | 2010-03-22 02:11:45 +0000 | [diff] [blame] | 1123 | lp->eth_regs = ioremap_nocache(r->start, resource_size(r)); |
Florian Fainelli | ef11291 | 2008-03-19 17:14:51 +0100 | [diff] [blame] | 1124 | if (!lp->eth_regs) { |
Phil Sutter | f16aea4 | 2009-08-12 12:22:46 +0000 | [diff] [blame] | 1125 | printk(KERN_ERR DRV_NAME ": cannot remap registers\n"); |
Francois Romieu | e3152ab | 2008-04-20 18:06:13 +0200 | [diff] [blame] | 1126 | rc = -ENXIO; |
Florian Fainelli | ef11291 | 2008-03-19 17:14:51 +0100 | [diff] [blame] | 1127 | goto probe_err_out; |
| 1128 | } |
| 1129 | |
| 1130 | r = platform_get_resource_byname(pdev, IORESOURCE_MEM, "korina_dma_rx"); |
Dan Carpenter | 3801326 | 2010-03-22 02:11:45 +0000 | [diff] [blame] | 1131 | lp->rx_dma_regs = ioremap_nocache(r->start, resource_size(r)); |
Florian Fainelli | ef11291 | 2008-03-19 17:14:51 +0100 | [diff] [blame] | 1132 | if (!lp->rx_dma_regs) { |
Phil Sutter | f16aea4 | 2009-08-12 12:22:46 +0000 | [diff] [blame] | 1133 | printk(KERN_ERR DRV_NAME ": cannot remap Rx DMA registers\n"); |
Francois Romieu | e3152ab | 2008-04-20 18:06:13 +0200 | [diff] [blame] | 1134 | rc = -ENXIO; |
Florian Fainelli | ef11291 | 2008-03-19 17:14:51 +0100 | [diff] [blame] | 1135 | goto probe_err_dma_rx; |
| 1136 | } |
| 1137 | |
| 1138 | r = platform_get_resource_byname(pdev, IORESOURCE_MEM, "korina_dma_tx"); |
Dan Carpenter | 3801326 | 2010-03-22 02:11:45 +0000 | [diff] [blame] | 1139 | lp->tx_dma_regs = ioremap_nocache(r->start, resource_size(r)); |
Florian Fainelli | ef11291 | 2008-03-19 17:14:51 +0100 | [diff] [blame] | 1140 | if (!lp->tx_dma_regs) { |
Phil Sutter | f16aea4 | 2009-08-12 12:22:46 +0000 | [diff] [blame] | 1141 | printk(KERN_ERR DRV_NAME ": cannot remap Tx DMA registers\n"); |
Francois Romieu | e3152ab | 2008-04-20 18:06:13 +0200 | [diff] [blame] | 1142 | rc = -ENXIO; |
Florian Fainelli | ef11291 | 2008-03-19 17:14:51 +0100 | [diff] [blame] | 1143 | goto probe_err_dma_tx; |
| 1144 | } |
| 1145 | |
| 1146 | lp->td_ring = kmalloc(TD_RING_SIZE + RD_RING_SIZE, GFP_KERNEL); |
| 1147 | if (!lp->td_ring) { |
Francois Romieu | e3152ab | 2008-04-20 18:06:13 +0200 | [diff] [blame] | 1148 | rc = -ENXIO; |
Florian Fainelli | ef11291 | 2008-03-19 17:14:51 +0100 | [diff] [blame] | 1149 | goto probe_err_td_ring; |
| 1150 | } |
| 1151 | |
| 1152 | dma_cache_inv((unsigned long)(lp->td_ring), |
| 1153 | TD_RING_SIZE + RD_RING_SIZE); |
| 1154 | |
| 1155 | /* now convert TD_RING pointer to KSEG1 */ |
| 1156 | lp->td_ring = (struct dma_desc *)KSEG1ADDR(lp->td_ring); |
| 1157 | lp->rd_ring = &lp->td_ring[KORINA_NUM_TDS]; |
| 1158 | |
| 1159 | spin_lock_init(&lp->lock); |
| 1160 | /* just use the rx dma irq */ |
| 1161 | dev->irq = lp->rx_irq; |
| 1162 | lp->dev = dev; |
| 1163 | |
Alexander Beregalov | 52b031f | 2009-04-15 12:52:46 +0000 | [diff] [blame] | 1164 | dev->netdev_ops = &korina_netdev_ops; |
Florian Fainelli | ef11291 | 2008-03-19 17:14:51 +0100 | [diff] [blame] | 1165 | dev->ethtool_ops = &netdev_ethtool_ops; |
Florian Fainelli | ef11291 | 2008-03-19 17:14:51 +0100 | [diff] [blame] | 1166 | dev->watchdog_timeo = TX_TIMEOUT; |
Florian Fainelli | ef11291 | 2008-03-19 17:14:51 +0100 | [diff] [blame] | 1167 | netif_napi_add(dev, &lp->napi, korina_poll, 64); |
| 1168 | |
| 1169 | lp->phy_addr = (((lp->rx_irq == 0x2c? 1:0) << 8) | 0x05); |
| 1170 | lp->mii_if.dev = dev; |
| 1171 | lp->mii_if.mdio_read = mdio_read; |
| 1172 | lp->mii_if.mdio_write = mdio_write; |
| 1173 | lp->mii_if.phy_id = lp->phy_addr; |
| 1174 | lp->mii_if.phy_id_mask = 0x1f; |
| 1175 | lp->mii_if.reg_num_mask = 0x1f; |
| 1176 | |
Francois Romieu | e3152ab | 2008-04-20 18:06:13 +0200 | [diff] [blame] | 1177 | rc = register_netdev(dev); |
| 1178 | if (rc < 0) { |
Florian Fainelli | ef11291 | 2008-03-19 17:14:51 +0100 | [diff] [blame] | 1179 | printk(KERN_ERR DRV_NAME |
Phil Sutter | f16aea4 | 2009-08-12 12:22:46 +0000 | [diff] [blame] | 1180 | ": cannot register net device: %d\n", rc); |
Florian Fainelli | ef11291 | 2008-03-19 17:14:51 +0100 | [diff] [blame] | 1181 | goto probe_err_register; |
| 1182 | } |
Florian Fainelli | 4d5ef9f | 2009-05-28 00:58:41 +0000 | [diff] [blame] | 1183 | setup_timer(&lp->media_check_timer, korina_poll_media, (unsigned long) dev); |
Phil Sutter | f16aea4 | 2009-08-12 12:22:46 +0000 | [diff] [blame] | 1184 | |
Phil Sutter | ceb3d23 | 2010-05-29 13:23:34 +0000 | [diff] [blame] | 1185 | INIT_WORK(&lp->restart_task, korina_restart_task); |
| 1186 | |
Phil Sutter | f16aea4 | 2009-08-12 12:22:46 +0000 | [diff] [blame] | 1187 | printk(KERN_INFO "%s: " DRV_NAME "-" DRV_VERSION " " DRV_RELDATE "\n", |
| 1188 | dev->name); |
Francois Romieu | e3152ab | 2008-04-20 18:06:13 +0200 | [diff] [blame] | 1189 | out: |
| 1190 | return rc; |
Florian Fainelli | ef11291 | 2008-03-19 17:14:51 +0100 | [diff] [blame] | 1191 | |
| 1192 | probe_err_register: |
| 1193 | kfree(lp->td_ring); |
| 1194 | probe_err_td_ring: |
| 1195 | iounmap(lp->tx_dma_regs); |
| 1196 | probe_err_dma_tx: |
| 1197 | iounmap(lp->rx_dma_regs); |
| 1198 | probe_err_dma_rx: |
| 1199 | iounmap(lp->eth_regs); |
| 1200 | probe_err_out: |
| 1201 | free_netdev(dev); |
Francois Romieu | e3152ab | 2008-04-20 18:06:13 +0200 | [diff] [blame] | 1202 | goto out; |
Florian Fainelli | ef11291 | 2008-03-19 17:14:51 +0100 | [diff] [blame] | 1203 | } |
| 1204 | |
| 1205 | static int korina_remove(struct platform_device *pdev) |
| 1206 | { |
| 1207 | struct korina_device *bif = platform_get_drvdata(pdev); |
| 1208 | struct korina_private *lp = netdev_priv(bif->dev); |
| 1209 | |
Francois Romieu | e3152ab | 2008-04-20 18:06:13 +0200 | [diff] [blame] | 1210 | iounmap(lp->eth_regs); |
| 1211 | iounmap(lp->rx_dma_regs); |
| 1212 | iounmap(lp->tx_dma_regs); |
Florian Fainelli | ef11291 | 2008-03-19 17:14:51 +0100 | [diff] [blame] | 1213 | |
Florian Fainelli | ef11291 | 2008-03-19 17:14:51 +0100 | [diff] [blame] | 1214 | unregister_netdev(bif->dev); |
| 1215 | free_netdev(bif->dev); |
| 1216 | |
| 1217 | return 0; |
| 1218 | } |
| 1219 | |
| 1220 | static struct platform_driver korina_driver = { |
| 1221 | .driver.name = "korina", |
| 1222 | .probe = korina_probe, |
| 1223 | .remove = korina_remove, |
| 1224 | }; |
| 1225 | |
Axel Lin | db62f68 | 2011-11-27 16:44:17 +0000 | [diff] [blame] | 1226 | module_platform_driver(korina_driver); |
Florian Fainelli | ef11291 | 2008-03-19 17:14:51 +0100 | [diff] [blame] | 1227 | |
| 1228 | MODULE_AUTHOR("Philip Rischel <rischelp@idt.com>"); |
| 1229 | MODULE_AUTHOR("Felix Fietkau <nbd@openwrt.org>"); |
| 1230 | MODULE_AUTHOR("Florian Fainelli <florian@openwrt.org>"); |
| 1231 | MODULE_DESCRIPTION("IDT RC32434 (Korina) Ethernet driver"); |
| 1232 | MODULE_LICENSE("GPL"); |