Richard Röjfors | b07878e | 2009-06-04 03:35:55 +0000 | [diff] [blame] | 1 | /* |
| 2 | * ks8842_main.c timberdale KS8842 ethernet driver |
| 3 | * Copyright (c) 2009 Intel Corporation |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License version 2 as |
| 7 | * published by the Free Software Foundation. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 17 | */ |
| 18 | |
| 19 | /* Supports: |
| 20 | * The Micrel KS8842 behind the timberdale FPGA |
| 21 | */ |
| 22 | |
| 23 | #include <linux/kernel.h> |
| 24 | #include <linux/module.h> |
| 25 | #include <linux/platform_device.h> |
| 26 | #include <linux/netdevice.h> |
| 27 | #include <linux/etherdevice.h> |
| 28 | #include <linux/ethtool.h> |
| 29 | |
| 30 | #define DRV_NAME "ks8842" |
| 31 | |
| 32 | /* Timberdale specific Registers */ |
| 33 | #define REG_TIMB_RST 0x1c |
| 34 | |
| 35 | /* KS8842 registers */ |
| 36 | |
| 37 | #define REG_SELECT_BANK 0x0e |
| 38 | |
| 39 | /* bank 0 registers */ |
| 40 | #define REG_QRFCR 0x04 |
| 41 | |
| 42 | /* bank 2 registers */ |
| 43 | #define REG_MARL 0x00 |
| 44 | #define REG_MARM 0x02 |
| 45 | #define REG_MARH 0x04 |
| 46 | |
| 47 | /* bank 3 registers */ |
| 48 | #define REG_GRR 0x06 |
| 49 | |
| 50 | /* bank 16 registers */ |
| 51 | #define REG_TXCR 0x00 |
| 52 | #define REG_TXSR 0x02 |
| 53 | #define REG_RXCR 0x04 |
| 54 | #define REG_TXMIR 0x08 |
| 55 | #define REG_RXMIR 0x0A |
| 56 | |
| 57 | /* bank 17 registers */ |
| 58 | #define REG_TXQCR 0x00 |
| 59 | #define REG_RXQCR 0x02 |
| 60 | #define REG_TXFDPR 0x04 |
| 61 | #define REG_RXFDPR 0x06 |
| 62 | #define REG_QMU_DATA_LO 0x08 |
| 63 | #define REG_QMU_DATA_HI 0x0A |
| 64 | |
| 65 | /* bank 18 registers */ |
| 66 | #define REG_IER 0x00 |
| 67 | #define IRQ_LINK_CHANGE 0x8000 |
| 68 | #define IRQ_TX 0x4000 |
| 69 | #define IRQ_RX 0x2000 |
| 70 | #define IRQ_RX_OVERRUN 0x0800 |
| 71 | #define IRQ_TX_STOPPED 0x0200 |
| 72 | #define IRQ_RX_STOPPED 0x0100 |
| 73 | #define IRQ_RX_ERROR 0x0080 |
| 74 | #define ENABLED_IRQS (IRQ_LINK_CHANGE | IRQ_TX | IRQ_RX | IRQ_RX_STOPPED | \ |
| 75 | IRQ_TX_STOPPED | IRQ_RX_OVERRUN | IRQ_RX_ERROR) |
| 76 | #define REG_ISR 0x02 |
| 77 | #define REG_RXSR 0x04 |
| 78 | #define RXSR_VALID 0x8000 |
| 79 | #define RXSR_BROADCAST 0x80 |
| 80 | #define RXSR_MULTICAST 0x40 |
| 81 | #define RXSR_UNICAST 0x20 |
| 82 | #define RXSR_FRAMETYPE 0x08 |
| 83 | #define RXSR_TOO_LONG 0x04 |
| 84 | #define RXSR_RUNT 0x02 |
| 85 | #define RXSR_CRC_ERROR 0x01 |
| 86 | #define RXSR_ERROR (RXSR_TOO_LONG | RXSR_RUNT | RXSR_CRC_ERROR) |
| 87 | |
| 88 | /* bank 32 registers */ |
| 89 | #define REG_SW_ID_AND_ENABLE 0x00 |
| 90 | #define REG_SGCR1 0x02 |
| 91 | #define REG_SGCR2 0x04 |
| 92 | #define REG_SGCR3 0x06 |
| 93 | |
| 94 | /* bank 39 registers */ |
| 95 | #define REG_MACAR1 0x00 |
| 96 | #define REG_MACAR2 0x02 |
| 97 | #define REG_MACAR3 0x04 |
| 98 | |
| 99 | /* bank 45 registers */ |
| 100 | #define REG_P1MBCR 0x00 |
| 101 | #define REG_P1MBSR 0x02 |
| 102 | |
| 103 | /* bank 46 registers */ |
| 104 | #define REG_P2MBCR 0x00 |
| 105 | #define REG_P2MBSR 0x02 |
| 106 | |
| 107 | /* bank 48 registers */ |
| 108 | #define REG_P1CR2 0x02 |
| 109 | |
| 110 | /* bank 49 registers */ |
| 111 | #define REG_P1CR4 0x02 |
| 112 | #define REG_P1SR 0x04 |
| 113 | |
| 114 | struct ks8842_adapter { |
| 115 | void __iomem *hw_addr; |
| 116 | int irq; |
| 117 | struct tasklet_struct tasklet; |
| 118 | spinlock_t lock; /* spinlock to be interrupt safe */ |
| 119 | struct platform_device *pdev; |
| 120 | }; |
| 121 | |
| 122 | static inline void ks8842_select_bank(struct ks8842_adapter *adapter, u16 bank) |
| 123 | { |
| 124 | iowrite16(bank, adapter->hw_addr + REG_SELECT_BANK); |
| 125 | } |
| 126 | |
| 127 | static inline void ks8842_write8(struct ks8842_adapter *adapter, u16 bank, |
| 128 | u8 value, int offset) |
| 129 | { |
| 130 | ks8842_select_bank(adapter, bank); |
| 131 | iowrite8(value, adapter->hw_addr + offset); |
| 132 | } |
| 133 | |
| 134 | static inline void ks8842_write16(struct ks8842_adapter *adapter, u16 bank, |
| 135 | u16 value, int offset) |
| 136 | { |
| 137 | ks8842_select_bank(adapter, bank); |
| 138 | iowrite16(value, adapter->hw_addr + offset); |
| 139 | } |
| 140 | |
| 141 | static inline void ks8842_enable_bits(struct ks8842_adapter *adapter, u16 bank, |
| 142 | u16 bits, int offset) |
| 143 | { |
| 144 | u16 reg; |
| 145 | ks8842_select_bank(adapter, bank); |
| 146 | reg = ioread16(adapter->hw_addr + offset); |
| 147 | reg |= bits; |
| 148 | iowrite16(reg, adapter->hw_addr + offset); |
| 149 | } |
| 150 | |
| 151 | static inline void ks8842_clear_bits(struct ks8842_adapter *adapter, u16 bank, |
| 152 | u16 bits, int offset) |
| 153 | { |
| 154 | u16 reg; |
| 155 | ks8842_select_bank(adapter, bank); |
| 156 | reg = ioread16(adapter->hw_addr + offset); |
| 157 | reg &= ~bits; |
| 158 | iowrite16(reg, adapter->hw_addr + offset); |
| 159 | } |
| 160 | |
| 161 | static inline void ks8842_write32(struct ks8842_adapter *adapter, u16 bank, |
| 162 | u32 value, int offset) |
| 163 | { |
| 164 | ks8842_select_bank(adapter, bank); |
| 165 | iowrite32(value, adapter->hw_addr + offset); |
| 166 | } |
| 167 | |
| 168 | static inline u8 ks8842_read8(struct ks8842_adapter *adapter, u16 bank, |
| 169 | int offset) |
| 170 | { |
| 171 | ks8842_select_bank(adapter, bank); |
| 172 | return ioread8(adapter->hw_addr + offset); |
| 173 | } |
| 174 | |
| 175 | static inline u16 ks8842_read16(struct ks8842_adapter *adapter, u16 bank, |
| 176 | int offset) |
| 177 | { |
| 178 | ks8842_select_bank(adapter, bank); |
| 179 | return ioread16(adapter->hw_addr + offset); |
| 180 | } |
| 181 | |
| 182 | static inline u32 ks8842_read32(struct ks8842_adapter *adapter, u16 bank, |
| 183 | int offset) |
| 184 | { |
| 185 | ks8842_select_bank(adapter, bank); |
| 186 | return ioread32(adapter->hw_addr + offset); |
| 187 | } |
| 188 | |
| 189 | static void ks8842_reset(struct ks8842_adapter *adapter) |
| 190 | { |
| 191 | /* The KS8842 goes haywire when doing softare reset |
| 192 | * a work around in the timberdale IP is implemented to |
| 193 | * do a hardware reset instead |
| 194 | ks8842_write16(adapter, 3, 1, REG_GRR); |
| 195 | msleep(10); |
| 196 | iowrite16(0, adapter->hw_addr + REG_GRR); |
| 197 | */ |
| 198 | iowrite16(32, adapter->hw_addr + REG_SELECT_BANK); |
| 199 | iowrite32(0x1, adapter->hw_addr + REG_TIMB_RST); |
| 200 | msleep(20); |
| 201 | } |
| 202 | |
| 203 | static void ks8842_update_link_status(struct net_device *netdev, |
| 204 | struct ks8842_adapter *adapter) |
| 205 | { |
| 206 | /* check the status of the link */ |
| 207 | if (ks8842_read16(adapter, 45, REG_P1MBSR) & 0x4) { |
| 208 | netif_carrier_on(netdev); |
| 209 | netif_wake_queue(netdev); |
| 210 | } else { |
| 211 | netif_stop_queue(netdev); |
| 212 | netif_carrier_off(netdev); |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | static void ks8842_enable_tx(struct ks8842_adapter *adapter) |
| 217 | { |
| 218 | ks8842_enable_bits(adapter, 16, 0x01, REG_TXCR); |
| 219 | } |
| 220 | |
| 221 | static void ks8842_disable_tx(struct ks8842_adapter *adapter) |
| 222 | { |
| 223 | ks8842_clear_bits(adapter, 16, 0x01, REG_TXCR); |
| 224 | } |
| 225 | |
| 226 | static void ks8842_enable_rx(struct ks8842_adapter *adapter) |
| 227 | { |
| 228 | ks8842_enable_bits(adapter, 16, 0x01, REG_RXCR); |
| 229 | } |
| 230 | |
| 231 | static void ks8842_disable_rx(struct ks8842_adapter *adapter) |
| 232 | { |
| 233 | ks8842_clear_bits(adapter, 16, 0x01, REG_RXCR); |
| 234 | } |
| 235 | |
| 236 | static void ks8842_reset_hw(struct ks8842_adapter *adapter) |
| 237 | { |
| 238 | /* reset the HW */ |
| 239 | ks8842_reset(adapter); |
| 240 | |
| 241 | /* Enable QMU Transmit flow control / transmit padding / Transmit CRC */ |
| 242 | ks8842_write16(adapter, 16, 0x000E, REG_TXCR); |
| 243 | |
| 244 | /* enable the receiver, uni + multi + broadcast + flow ctrl |
| 245 | + crc strip */ |
| 246 | ks8842_write16(adapter, 16, 0x8 | 0x20 | 0x40 | 0x80 | 0x400, |
| 247 | REG_RXCR); |
| 248 | |
| 249 | /* TX frame pointer autoincrement */ |
| 250 | ks8842_write16(adapter, 17, 0x4000, REG_TXFDPR); |
| 251 | |
| 252 | /* RX frame pointer autoincrement */ |
| 253 | ks8842_write16(adapter, 17, 0x4000, REG_RXFDPR); |
| 254 | |
| 255 | /* RX 2 kb high watermark */ |
| 256 | ks8842_write16(adapter, 0, 0x1000, REG_QRFCR); |
| 257 | |
| 258 | /* aggresive back off in half duplex */ |
| 259 | ks8842_enable_bits(adapter, 32, 1 << 8, REG_SGCR1); |
| 260 | |
| 261 | /* enable no excessive collison drop */ |
| 262 | ks8842_enable_bits(adapter, 32, 1 << 3, REG_SGCR2); |
| 263 | |
| 264 | /* Enable port 1 force flow control / back pressure / transmit / recv */ |
| 265 | ks8842_write16(adapter, 48, 0x1E07, REG_P1CR2); |
| 266 | |
| 267 | /* restart port auto-negotiation */ |
| 268 | ks8842_enable_bits(adapter, 49, 1 << 13, REG_P1CR4); |
| 269 | /* only advertise 10Mbps */ |
| 270 | ks8842_clear_bits(adapter, 49, 3 << 2, REG_P1CR4); |
| 271 | |
| 272 | /* Enable the transmitter */ |
| 273 | ks8842_enable_tx(adapter); |
| 274 | |
| 275 | /* Enable the receiver */ |
| 276 | ks8842_enable_rx(adapter); |
| 277 | |
| 278 | /* clear all interrupts */ |
| 279 | ks8842_write16(adapter, 18, 0xffff, REG_ISR); |
| 280 | |
| 281 | /* enable interrupts */ |
| 282 | ks8842_write16(adapter, 18, ENABLED_IRQS, REG_IER); |
| 283 | |
| 284 | /* enable the switch */ |
| 285 | ks8842_write16(adapter, 32, 0x1, REG_SW_ID_AND_ENABLE); |
| 286 | } |
| 287 | |
| 288 | static void ks8842_read_mac_addr(struct ks8842_adapter *adapter, u8 *dest) |
| 289 | { |
| 290 | int i; |
| 291 | u16 mac; |
| 292 | |
| 293 | for (i = 0; i < ETH_ALEN; i++) |
| 294 | dest[ETH_ALEN - i - 1] = ks8842_read8(adapter, 2, REG_MARL + i); |
| 295 | |
| 296 | /* make sure the switch port uses the same MAC as the QMU */ |
| 297 | mac = ks8842_read16(adapter, 2, REG_MARL); |
| 298 | ks8842_write16(adapter, 39, mac, REG_MACAR1); |
| 299 | mac = ks8842_read16(adapter, 2, REG_MARM); |
| 300 | ks8842_write16(adapter, 39, mac, REG_MACAR2); |
| 301 | mac = ks8842_read16(adapter, 2, REG_MARH); |
| 302 | ks8842_write16(adapter, 39, mac, REG_MACAR3); |
| 303 | } |
| 304 | |
| 305 | static inline u16 ks8842_tx_fifo_space(struct ks8842_adapter *adapter) |
| 306 | { |
| 307 | return ks8842_read16(adapter, 16, REG_TXMIR) & 0x1fff; |
| 308 | } |
| 309 | |
| 310 | static int ks8842_tx_frame(struct sk_buff *skb, struct net_device *netdev) |
| 311 | { |
| 312 | struct ks8842_adapter *adapter = netdev_priv(netdev); |
| 313 | int len = skb->len; |
| 314 | u32 *ptr = (u32 *)skb->data; |
| 315 | u32 ctrl; |
| 316 | |
| 317 | dev_dbg(&adapter->pdev->dev, |
| 318 | "%s: len %u head %p data %p tail %p end %p\n", |
| 319 | __func__, skb->len, skb->head, skb->data, |
| 320 | skb_tail_pointer(skb), skb_end_pointer(skb)); |
| 321 | |
| 322 | /* check FIFO buffer space, we need space for CRC and command bits */ |
| 323 | if (ks8842_tx_fifo_space(adapter) < len + 8) |
| 324 | return NETDEV_TX_BUSY; |
| 325 | |
| 326 | /* the control word, enable IRQ, port 1 and the length */ |
| 327 | ctrl = 0x8000 | 0x100 | (len << 16); |
| 328 | ks8842_write32(adapter, 17, ctrl, REG_QMU_DATA_LO); |
| 329 | |
| 330 | netdev->stats.tx_bytes += len; |
| 331 | |
| 332 | /* copy buffer */ |
| 333 | while (len > 0) { |
| 334 | iowrite32(*ptr, adapter->hw_addr + REG_QMU_DATA_LO); |
| 335 | len -= sizeof(u32); |
| 336 | ptr++; |
| 337 | } |
| 338 | |
| 339 | /* enqueue packet */ |
| 340 | ks8842_write16(adapter, 17, 1, REG_TXQCR); |
| 341 | |
| 342 | dev_kfree_skb(skb); |
| 343 | |
| 344 | return NETDEV_TX_OK; |
| 345 | } |
| 346 | |
| 347 | static void ks8842_rx_frame(struct net_device *netdev, |
| 348 | struct ks8842_adapter *adapter) |
| 349 | { |
| 350 | u32 status = ks8842_read32(adapter, 17, REG_QMU_DATA_LO); |
| 351 | int len = (status >> 16) & 0x7ff; |
| 352 | |
| 353 | status &= 0xffff; |
| 354 | |
| 355 | dev_dbg(&adapter->pdev->dev, "%s - rx_data: status: %x\n", |
| 356 | __func__, status); |
| 357 | |
| 358 | /* check the status */ |
| 359 | if ((status & RXSR_VALID) && !(status & RXSR_ERROR)) { |
| 360 | struct sk_buff *skb = netdev_alloc_skb(netdev, len + 2); |
| 361 | |
| 362 | dev_dbg(&adapter->pdev->dev, "%s, got package, len: %d\n", |
| 363 | __func__, len); |
| 364 | if (skb) { |
| 365 | u32 *data; |
| 366 | |
| 367 | netdev->stats.rx_packets++; |
| 368 | netdev->stats.rx_bytes += len; |
| 369 | if (status & RXSR_MULTICAST) |
| 370 | netdev->stats.multicast++; |
| 371 | |
| 372 | /* Align socket buffer in 4-byte boundary for |
| 373 | better performance. */ |
| 374 | skb_reserve(skb, 2); |
| 375 | data = (u32 *)skb_put(skb, len); |
| 376 | |
| 377 | ks8842_select_bank(adapter, 17); |
| 378 | while (len > 0) { |
| 379 | *data++ = ioread32(adapter->hw_addr + |
| 380 | REG_QMU_DATA_LO); |
| 381 | len -= sizeof(u32); |
| 382 | } |
| 383 | |
| 384 | skb->protocol = eth_type_trans(skb, netdev); |
| 385 | netif_rx(skb); |
| 386 | } else |
| 387 | netdev->stats.rx_dropped++; |
| 388 | } else { |
| 389 | dev_dbg(&adapter->pdev->dev, "RX error, status: %x\n", status); |
| 390 | netdev->stats.rx_errors++; |
| 391 | if (status & RXSR_TOO_LONG) |
| 392 | netdev->stats.rx_length_errors++; |
| 393 | if (status & RXSR_CRC_ERROR) |
| 394 | netdev->stats.rx_crc_errors++; |
| 395 | if (status & RXSR_RUNT) |
| 396 | netdev->stats.rx_frame_errors++; |
| 397 | } |
| 398 | |
| 399 | /* set high watermark to 3K */ |
| 400 | ks8842_clear_bits(adapter, 0, 1 << 12, REG_QRFCR); |
| 401 | |
| 402 | /* release the frame */ |
| 403 | ks8842_write16(adapter, 17, 0x01, REG_RXQCR); |
| 404 | |
| 405 | /* set high watermark to 2K */ |
| 406 | ks8842_enable_bits(adapter, 0, 1 << 12, REG_QRFCR); |
| 407 | } |
| 408 | |
| 409 | void ks8842_handle_rx(struct net_device *netdev, struct ks8842_adapter *adapter) |
| 410 | { |
| 411 | u16 rx_data = ks8842_read16(adapter, 16, REG_RXMIR) & 0x1fff; |
| 412 | dev_dbg(&adapter->pdev->dev, "%s Entry - rx_data: %d\n", |
| 413 | __func__, rx_data); |
| 414 | while (rx_data) { |
| 415 | ks8842_rx_frame(netdev, adapter); |
| 416 | rx_data = ks8842_read16(adapter, 16, REG_RXMIR) & 0x1fff; |
| 417 | } |
| 418 | } |
| 419 | |
| 420 | void ks8842_handle_tx(struct net_device *netdev, struct ks8842_adapter *adapter) |
| 421 | { |
| 422 | u16 sr = ks8842_read16(adapter, 16, REG_TXSR); |
| 423 | dev_dbg(&adapter->pdev->dev, "%s - entry, sr: %x\n", __func__, sr); |
| 424 | netdev->stats.tx_packets++; |
| 425 | if (netif_queue_stopped(netdev)) |
| 426 | netif_wake_queue(netdev); |
| 427 | } |
| 428 | |
| 429 | void ks8842_handle_rx_overrun(struct net_device *netdev, |
| 430 | struct ks8842_adapter *adapter) |
| 431 | { |
| 432 | dev_dbg(&adapter->pdev->dev, "%s: entry\n", __func__); |
| 433 | netdev->stats.rx_errors++; |
| 434 | netdev->stats.rx_fifo_errors++; |
| 435 | } |
| 436 | |
| 437 | void ks8842_tasklet(unsigned long arg) |
| 438 | { |
| 439 | struct net_device *netdev = (struct net_device *)arg; |
| 440 | struct ks8842_adapter *adapter = netdev_priv(netdev); |
| 441 | u16 isr; |
| 442 | unsigned long flags; |
| 443 | u16 entry_bank; |
| 444 | |
| 445 | /* read current bank to be able to set it back */ |
| 446 | spin_lock_irqsave(&adapter->lock, flags); |
| 447 | entry_bank = ioread16(adapter->hw_addr + REG_SELECT_BANK); |
| 448 | spin_unlock_irqrestore(&adapter->lock, flags); |
| 449 | |
| 450 | isr = ks8842_read16(adapter, 18, REG_ISR); |
| 451 | dev_dbg(&adapter->pdev->dev, "%s - ISR: 0x%x\n", __func__, isr); |
| 452 | |
| 453 | /* Ack */ |
| 454 | ks8842_write16(adapter, 18, isr, REG_ISR); |
| 455 | |
| 456 | if (!netif_running(netdev)) |
| 457 | return; |
| 458 | |
| 459 | if (isr & IRQ_LINK_CHANGE) |
| 460 | ks8842_update_link_status(netdev, adapter); |
| 461 | |
| 462 | if (isr & (IRQ_RX | IRQ_RX_ERROR)) |
| 463 | ks8842_handle_rx(netdev, adapter); |
| 464 | |
| 465 | if (isr & IRQ_TX) |
| 466 | ks8842_handle_tx(netdev, adapter); |
| 467 | |
| 468 | if (isr & IRQ_RX_OVERRUN) |
| 469 | ks8842_handle_rx_overrun(netdev, adapter); |
| 470 | |
| 471 | if (isr & IRQ_TX_STOPPED) { |
| 472 | ks8842_disable_tx(adapter); |
| 473 | ks8842_enable_tx(adapter); |
| 474 | } |
| 475 | |
| 476 | if (isr & IRQ_RX_STOPPED) { |
| 477 | ks8842_disable_rx(adapter); |
| 478 | ks8842_enable_rx(adapter); |
| 479 | } |
| 480 | |
| 481 | /* re-enable interrupts, put back the bank selection register */ |
| 482 | spin_lock_irqsave(&adapter->lock, flags); |
| 483 | ks8842_write16(adapter, 18, ENABLED_IRQS, REG_IER); |
| 484 | iowrite16(entry_bank, adapter->hw_addr + REG_SELECT_BANK); |
| 485 | spin_unlock_irqrestore(&adapter->lock, flags); |
| 486 | } |
| 487 | |
| 488 | static irqreturn_t ks8842_irq(int irq, void *devid) |
| 489 | { |
| 490 | struct ks8842_adapter *adapter = devid; |
| 491 | u16 isr; |
| 492 | u16 entry_bank = ioread16(adapter->hw_addr + REG_SELECT_BANK); |
| 493 | irqreturn_t ret = IRQ_NONE; |
| 494 | |
| 495 | isr = ks8842_read16(adapter, 18, REG_ISR); |
| 496 | dev_dbg(&adapter->pdev->dev, "%s - ISR: 0x%x\n", __func__, isr); |
| 497 | |
| 498 | if (isr) { |
| 499 | /* disable IRQ */ |
| 500 | ks8842_write16(adapter, 18, 0x00, REG_IER); |
| 501 | |
| 502 | /* schedule tasklet */ |
| 503 | tasklet_schedule(&adapter->tasklet); |
| 504 | |
| 505 | ret = IRQ_HANDLED; |
| 506 | } |
| 507 | |
| 508 | iowrite16(entry_bank, adapter->hw_addr + REG_SELECT_BANK); |
| 509 | |
| 510 | return ret; |
| 511 | } |
| 512 | |
| 513 | |
| 514 | /* Netdevice operations */ |
| 515 | |
| 516 | static int ks8842_open(struct net_device *netdev) |
| 517 | { |
| 518 | struct ks8842_adapter *adapter = netdev_priv(netdev); |
| 519 | int err; |
| 520 | |
| 521 | dev_dbg(&adapter->pdev->dev, "%s - entry\n", __func__); |
| 522 | |
| 523 | /* reset the HW */ |
| 524 | ks8842_reset_hw(adapter); |
| 525 | |
| 526 | ks8842_update_link_status(netdev, adapter); |
| 527 | |
| 528 | err = request_irq(adapter->irq, ks8842_irq, IRQF_SHARED, DRV_NAME, |
| 529 | adapter); |
| 530 | if (err) { |
| 531 | printk(KERN_ERR "Failed to request IRQ: %d: %d\n", |
| 532 | adapter->irq, err); |
| 533 | return err; |
| 534 | } |
| 535 | |
| 536 | return 0; |
| 537 | } |
| 538 | |
| 539 | static int ks8842_close(struct net_device *netdev) |
| 540 | { |
| 541 | struct ks8842_adapter *adapter = netdev_priv(netdev); |
| 542 | |
| 543 | dev_dbg(&adapter->pdev->dev, "%s - entry\n", __func__); |
| 544 | |
| 545 | /* free the irq */ |
| 546 | free_irq(adapter->irq, adapter); |
| 547 | |
| 548 | /* disable the switch */ |
| 549 | ks8842_write16(adapter, 32, 0x0, REG_SW_ID_AND_ENABLE); |
| 550 | |
| 551 | return 0; |
| 552 | } |
| 553 | |
Stephen Hemminger | 61357325 | 2009-08-31 19:50:58 +0000 | [diff] [blame] | 554 | static netdev_tx_t ks8842_xmit_frame(struct sk_buff *skb, |
| 555 | struct net_device *netdev) |
Richard Röjfors | b07878e | 2009-06-04 03:35:55 +0000 | [diff] [blame] | 556 | { |
| 557 | int ret; |
| 558 | struct ks8842_adapter *adapter = netdev_priv(netdev); |
| 559 | |
| 560 | dev_dbg(&adapter->pdev->dev, "%s: entry\n", __func__); |
| 561 | |
| 562 | ret = ks8842_tx_frame(skb, netdev); |
| 563 | |
| 564 | if (ks8842_tx_fifo_space(adapter) < netdev->mtu + 8) |
| 565 | netif_stop_queue(netdev); |
| 566 | |
| 567 | return ret; |
| 568 | } |
| 569 | |
| 570 | static int ks8842_set_mac(struct net_device *netdev, void *p) |
| 571 | { |
| 572 | struct ks8842_adapter *adapter = netdev_priv(netdev); |
| 573 | unsigned long flags; |
| 574 | struct sockaddr *addr = p; |
| 575 | char *mac = (u8 *)addr->sa_data; |
| 576 | int i; |
| 577 | |
| 578 | dev_dbg(&adapter->pdev->dev, "%s: entry\n", __func__); |
| 579 | |
| 580 | if (!is_valid_ether_addr(addr->sa_data)) |
| 581 | return -EADDRNOTAVAIL; |
| 582 | |
| 583 | memcpy(netdev->dev_addr, mac, netdev->addr_len); |
| 584 | |
| 585 | spin_lock_irqsave(&adapter->lock, flags); |
| 586 | for (i = 0; i < ETH_ALEN; i++) { |
| 587 | ks8842_write8(adapter, 2, mac[ETH_ALEN - i - 1], REG_MARL + i); |
| 588 | ks8842_write8(adapter, 39, mac[ETH_ALEN - i - 1], |
| 589 | REG_MACAR1 + i); |
| 590 | } |
| 591 | spin_unlock_irqrestore(&adapter->lock, flags); |
| 592 | return 0; |
| 593 | } |
| 594 | |
| 595 | static void ks8842_tx_timeout(struct net_device *netdev) |
| 596 | { |
| 597 | struct ks8842_adapter *adapter = netdev_priv(netdev); |
| 598 | unsigned long flags; |
| 599 | |
| 600 | dev_dbg(&adapter->pdev->dev, "%s: entry\n", __func__); |
| 601 | |
| 602 | spin_lock_irqsave(&adapter->lock, flags); |
| 603 | /* disable interrupts */ |
| 604 | ks8842_write16(adapter, 18, 0, REG_IER); |
| 605 | ks8842_write16(adapter, 18, 0xFFFF, REG_ISR); |
| 606 | spin_unlock_irqrestore(&adapter->lock, flags); |
| 607 | |
| 608 | ks8842_reset_hw(adapter); |
| 609 | |
| 610 | ks8842_update_link_status(netdev, adapter); |
| 611 | } |
| 612 | |
| 613 | static const struct net_device_ops ks8842_netdev_ops = { |
| 614 | .ndo_open = ks8842_open, |
| 615 | .ndo_stop = ks8842_close, |
| 616 | .ndo_start_xmit = ks8842_xmit_frame, |
| 617 | .ndo_set_mac_address = ks8842_set_mac, |
| 618 | .ndo_tx_timeout = ks8842_tx_timeout, |
| 619 | .ndo_validate_addr = eth_validate_addr |
| 620 | }; |
| 621 | |
Stephen Hemminger | 0fc0b73 | 2009-09-02 01:03:33 -0700 | [diff] [blame^] | 622 | static const struct ethtool_ops ks8842_ethtool_ops = { |
Richard Röjfors | b07878e | 2009-06-04 03:35:55 +0000 | [diff] [blame] | 623 | .get_link = ethtool_op_get_link, |
| 624 | }; |
| 625 | |
| 626 | static int __devinit ks8842_probe(struct platform_device *pdev) |
| 627 | { |
| 628 | int err = -ENOMEM; |
| 629 | struct resource *iomem; |
| 630 | struct net_device *netdev; |
| 631 | struct ks8842_adapter *adapter; |
| 632 | u16 id; |
| 633 | |
| 634 | iomem = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 635 | if (!request_mem_region(iomem->start, resource_size(iomem), DRV_NAME)) |
| 636 | goto err_mem_region; |
| 637 | |
| 638 | netdev = alloc_etherdev(sizeof(struct ks8842_adapter)); |
| 639 | if (!netdev) |
| 640 | goto err_alloc_etherdev; |
| 641 | |
| 642 | SET_NETDEV_DEV(netdev, &pdev->dev); |
| 643 | |
| 644 | adapter = netdev_priv(netdev); |
| 645 | adapter->hw_addr = ioremap(iomem->start, resource_size(iomem)); |
| 646 | if (!adapter->hw_addr) |
| 647 | goto err_ioremap; |
| 648 | |
| 649 | adapter->irq = platform_get_irq(pdev, 0); |
| 650 | if (adapter->irq < 0) { |
| 651 | err = adapter->irq; |
| 652 | goto err_get_irq; |
| 653 | } |
| 654 | |
| 655 | adapter->pdev = pdev; |
| 656 | |
| 657 | tasklet_init(&adapter->tasklet, ks8842_tasklet, (unsigned long)netdev); |
| 658 | spin_lock_init(&adapter->lock); |
| 659 | |
| 660 | netdev->netdev_ops = &ks8842_netdev_ops; |
| 661 | netdev->ethtool_ops = &ks8842_ethtool_ops; |
| 662 | |
| 663 | ks8842_read_mac_addr(adapter, netdev->dev_addr); |
| 664 | |
| 665 | id = ks8842_read16(adapter, 32, REG_SW_ID_AND_ENABLE); |
| 666 | |
| 667 | strcpy(netdev->name, "eth%d"); |
| 668 | err = register_netdev(netdev); |
| 669 | if (err) |
| 670 | goto err_register; |
| 671 | |
| 672 | platform_set_drvdata(pdev, netdev); |
| 673 | |
| 674 | printk(KERN_INFO DRV_NAME |
| 675 | " Found chip, family: 0x%x, id: 0x%x, rev: 0x%x\n", |
| 676 | (id >> 8) & 0xff, (id >> 4) & 0xf, (id >> 1) & 0x7); |
| 677 | |
| 678 | return 0; |
| 679 | |
| 680 | err_register: |
| 681 | err_get_irq: |
| 682 | iounmap(adapter->hw_addr); |
| 683 | err_ioremap: |
| 684 | free_netdev(netdev); |
| 685 | err_alloc_etherdev: |
| 686 | release_mem_region(iomem->start, resource_size(iomem)); |
| 687 | err_mem_region: |
| 688 | return err; |
| 689 | } |
| 690 | |
| 691 | static int __devexit ks8842_remove(struct platform_device *pdev) |
| 692 | { |
| 693 | struct net_device *netdev = platform_get_drvdata(pdev); |
| 694 | struct ks8842_adapter *adapter = netdev_priv(netdev); |
| 695 | struct resource *iomem = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 696 | |
| 697 | unregister_netdev(netdev); |
| 698 | tasklet_kill(&adapter->tasklet); |
| 699 | iounmap(adapter->hw_addr); |
| 700 | free_netdev(netdev); |
| 701 | release_mem_region(iomem->start, resource_size(iomem)); |
| 702 | platform_set_drvdata(pdev, NULL); |
| 703 | return 0; |
| 704 | } |
| 705 | |
| 706 | |
| 707 | static struct platform_driver ks8842_platform_driver = { |
| 708 | .driver = { |
| 709 | .name = DRV_NAME, |
| 710 | .owner = THIS_MODULE, |
| 711 | }, |
| 712 | .probe = ks8842_probe, |
| 713 | .remove = ks8842_remove, |
| 714 | }; |
| 715 | |
| 716 | static int __init ks8842_init(void) |
| 717 | { |
| 718 | return platform_driver_register(&ks8842_platform_driver); |
| 719 | } |
| 720 | |
| 721 | static void __exit ks8842_exit(void) |
| 722 | { |
| 723 | platform_driver_unregister(&ks8842_platform_driver); |
| 724 | } |
| 725 | |
| 726 | module_init(ks8842_init); |
| 727 | module_exit(ks8842_exit); |
| 728 | |
| 729 | MODULE_DESCRIPTION("Timberdale KS8842 ethernet driver"); |
| 730 | MODULE_AUTHOR("Mocean Laboratories <info@mocean-labs.com>"); |
| 731 | MODULE_LICENSE("GPL v2"); |
| 732 | MODULE_ALIAS("platform:ks8842"); |
| 733 | |