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)) { |
Eric Dumazet | 89d71a6 | 2009-10-13 05:34:20 +0000 | [diff] [blame] | 360 | struct sk_buff *skb = netdev_alloc_skb_ip_align(netdev, len); |
Richard Röjfors | b07878e | 2009-06-04 03:35:55 +0000 | [diff] [blame] | 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 | |
Richard Röjfors | b07878e | 2009-06-04 03:35:55 +0000 | [diff] [blame] | 372 | data = (u32 *)skb_put(skb, len); |
| 373 | |
| 374 | ks8842_select_bank(adapter, 17); |
| 375 | while (len > 0) { |
| 376 | *data++ = ioread32(adapter->hw_addr + |
| 377 | REG_QMU_DATA_LO); |
| 378 | len -= sizeof(u32); |
| 379 | } |
| 380 | |
| 381 | skb->protocol = eth_type_trans(skb, netdev); |
| 382 | netif_rx(skb); |
| 383 | } else |
| 384 | netdev->stats.rx_dropped++; |
| 385 | } else { |
| 386 | dev_dbg(&adapter->pdev->dev, "RX error, status: %x\n", status); |
| 387 | netdev->stats.rx_errors++; |
| 388 | if (status & RXSR_TOO_LONG) |
| 389 | netdev->stats.rx_length_errors++; |
| 390 | if (status & RXSR_CRC_ERROR) |
| 391 | netdev->stats.rx_crc_errors++; |
| 392 | if (status & RXSR_RUNT) |
| 393 | netdev->stats.rx_frame_errors++; |
| 394 | } |
| 395 | |
| 396 | /* set high watermark to 3K */ |
| 397 | ks8842_clear_bits(adapter, 0, 1 << 12, REG_QRFCR); |
| 398 | |
| 399 | /* release the frame */ |
| 400 | ks8842_write16(adapter, 17, 0x01, REG_RXQCR); |
| 401 | |
| 402 | /* set high watermark to 2K */ |
| 403 | ks8842_enable_bits(adapter, 0, 1 << 12, REG_QRFCR); |
| 404 | } |
| 405 | |
| 406 | void ks8842_handle_rx(struct net_device *netdev, struct ks8842_adapter *adapter) |
| 407 | { |
| 408 | u16 rx_data = ks8842_read16(adapter, 16, REG_RXMIR) & 0x1fff; |
| 409 | dev_dbg(&adapter->pdev->dev, "%s Entry - rx_data: %d\n", |
| 410 | __func__, rx_data); |
| 411 | while (rx_data) { |
| 412 | ks8842_rx_frame(netdev, adapter); |
| 413 | rx_data = ks8842_read16(adapter, 16, REG_RXMIR) & 0x1fff; |
| 414 | } |
| 415 | } |
| 416 | |
| 417 | void ks8842_handle_tx(struct net_device *netdev, struct ks8842_adapter *adapter) |
| 418 | { |
| 419 | u16 sr = ks8842_read16(adapter, 16, REG_TXSR); |
| 420 | dev_dbg(&adapter->pdev->dev, "%s - entry, sr: %x\n", __func__, sr); |
| 421 | netdev->stats.tx_packets++; |
| 422 | if (netif_queue_stopped(netdev)) |
| 423 | netif_wake_queue(netdev); |
| 424 | } |
| 425 | |
| 426 | void ks8842_handle_rx_overrun(struct net_device *netdev, |
| 427 | struct ks8842_adapter *adapter) |
| 428 | { |
| 429 | dev_dbg(&adapter->pdev->dev, "%s: entry\n", __func__); |
| 430 | netdev->stats.rx_errors++; |
| 431 | netdev->stats.rx_fifo_errors++; |
| 432 | } |
| 433 | |
| 434 | void ks8842_tasklet(unsigned long arg) |
| 435 | { |
| 436 | struct net_device *netdev = (struct net_device *)arg; |
| 437 | struct ks8842_adapter *adapter = netdev_priv(netdev); |
| 438 | u16 isr; |
| 439 | unsigned long flags; |
| 440 | u16 entry_bank; |
| 441 | |
| 442 | /* read current bank to be able to set it back */ |
| 443 | spin_lock_irqsave(&adapter->lock, flags); |
| 444 | entry_bank = ioread16(adapter->hw_addr + REG_SELECT_BANK); |
| 445 | spin_unlock_irqrestore(&adapter->lock, flags); |
| 446 | |
| 447 | isr = ks8842_read16(adapter, 18, REG_ISR); |
| 448 | dev_dbg(&adapter->pdev->dev, "%s - ISR: 0x%x\n", __func__, isr); |
| 449 | |
| 450 | /* Ack */ |
| 451 | ks8842_write16(adapter, 18, isr, REG_ISR); |
| 452 | |
| 453 | if (!netif_running(netdev)) |
| 454 | return; |
| 455 | |
| 456 | if (isr & IRQ_LINK_CHANGE) |
| 457 | ks8842_update_link_status(netdev, adapter); |
| 458 | |
| 459 | if (isr & (IRQ_RX | IRQ_RX_ERROR)) |
| 460 | ks8842_handle_rx(netdev, adapter); |
| 461 | |
| 462 | if (isr & IRQ_TX) |
| 463 | ks8842_handle_tx(netdev, adapter); |
| 464 | |
| 465 | if (isr & IRQ_RX_OVERRUN) |
| 466 | ks8842_handle_rx_overrun(netdev, adapter); |
| 467 | |
| 468 | if (isr & IRQ_TX_STOPPED) { |
| 469 | ks8842_disable_tx(adapter); |
| 470 | ks8842_enable_tx(adapter); |
| 471 | } |
| 472 | |
| 473 | if (isr & IRQ_RX_STOPPED) { |
| 474 | ks8842_disable_rx(adapter); |
| 475 | ks8842_enable_rx(adapter); |
| 476 | } |
| 477 | |
| 478 | /* re-enable interrupts, put back the bank selection register */ |
| 479 | spin_lock_irqsave(&adapter->lock, flags); |
| 480 | ks8842_write16(adapter, 18, ENABLED_IRQS, REG_IER); |
| 481 | iowrite16(entry_bank, adapter->hw_addr + REG_SELECT_BANK); |
| 482 | spin_unlock_irqrestore(&adapter->lock, flags); |
| 483 | } |
| 484 | |
| 485 | static irqreturn_t ks8842_irq(int irq, void *devid) |
| 486 | { |
| 487 | struct ks8842_adapter *adapter = devid; |
| 488 | u16 isr; |
| 489 | u16 entry_bank = ioread16(adapter->hw_addr + REG_SELECT_BANK); |
| 490 | irqreturn_t ret = IRQ_NONE; |
| 491 | |
| 492 | isr = ks8842_read16(adapter, 18, REG_ISR); |
| 493 | dev_dbg(&adapter->pdev->dev, "%s - ISR: 0x%x\n", __func__, isr); |
| 494 | |
| 495 | if (isr) { |
| 496 | /* disable IRQ */ |
| 497 | ks8842_write16(adapter, 18, 0x00, REG_IER); |
| 498 | |
| 499 | /* schedule tasklet */ |
| 500 | tasklet_schedule(&adapter->tasklet); |
| 501 | |
| 502 | ret = IRQ_HANDLED; |
| 503 | } |
| 504 | |
| 505 | iowrite16(entry_bank, adapter->hw_addr + REG_SELECT_BANK); |
| 506 | |
| 507 | return ret; |
| 508 | } |
| 509 | |
| 510 | |
| 511 | /* Netdevice operations */ |
| 512 | |
| 513 | static int ks8842_open(struct net_device *netdev) |
| 514 | { |
| 515 | struct ks8842_adapter *adapter = netdev_priv(netdev); |
| 516 | int err; |
| 517 | |
| 518 | dev_dbg(&adapter->pdev->dev, "%s - entry\n", __func__); |
| 519 | |
| 520 | /* reset the HW */ |
| 521 | ks8842_reset_hw(adapter); |
| 522 | |
| 523 | ks8842_update_link_status(netdev, adapter); |
| 524 | |
| 525 | err = request_irq(adapter->irq, ks8842_irq, IRQF_SHARED, DRV_NAME, |
| 526 | adapter); |
| 527 | if (err) { |
| 528 | printk(KERN_ERR "Failed to request IRQ: %d: %d\n", |
| 529 | adapter->irq, err); |
| 530 | return err; |
| 531 | } |
| 532 | |
| 533 | return 0; |
| 534 | } |
| 535 | |
| 536 | static int ks8842_close(struct net_device *netdev) |
| 537 | { |
| 538 | struct ks8842_adapter *adapter = netdev_priv(netdev); |
| 539 | |
| 540 | dev_dbg(&adapter->pdev->dev, "%s - entry\n", __func__); |
| 541 | |
| 542 | /* free the irq */ |
| 543 | free_irq(adapter->irq, adapter); |
| 544 | |
| 545 | /* disable the switch */ |
| 546 | ks8842_write16(adapter, 32, 0x0, REG_SW_ID_AND_ENABLE); |
| 547 | |
| 548 | return 0; |
| 549 | } |
| 550 | |
Stephen Hemminger | 61357325 | 2009-08-31 19:50:58 +0000 | [diff] [blame] | 551 | static netdev_tx_t ks8842_xmit_frame(struct sk_buff *skb, |
| 552 | struct net_device *netdev) |
Richard Röjfors | b07878e | 2009-06-04 03:35:55 +0000 | [diff] [blame] | 553 | { |
| 554 | int ret; |
| 555 | struct ks8842_adapter *adapter = netdev_priv(netdev); |
| 556 | |
| 557 | dev_dbg(&adapter->pdev->dev, "%s: entry\n", __func__); |
| 558 | |
| 559 | ret = ks8842_tx_frame(skb, netdev); |
| 560 | |
| 561 | if (ks8842_tx_fifo_space(adapter) < netdev->mtu + 8) |
| 562 | netif_stop_queue(netdev); |
| 563 | |
| 564 | return ret; |
| 565 | } |
| 566 | |
| 567 | static int ks8842_set_mac(struct net_device *netdev, void *p) |
| 568 | { |
| 569 | struct ks8842_adapter *adapter = netdev_priv(netdev); |
| 570 | unsigned long flags; |
| 571 | struct sockaddr *addr = p; |
| 572 | char *mac = (u8 *)addr->sa_data; |
| 573 | int i; |
| 574 | |
| 575 | dev_dbg(&adapter->pdev->dev, "%s: entry\n", __func__); |
| 576 | |
| 577 | if (!is_valid_ether_addr(addr->sa_data)) |
| 578 | return -EADDRNOTAVAIL; |
| 579 | |
| 580 | memcpy(netdev->dev_addr, mac, netdev->addr_len); |
| 581 | |
| 582 | spin_lock_irqsave(&adapter->lock, flags); |
| 583 | for (i = 0; i < ETH_ALEN; i++) { |
| 584 | ks8842_write8(adapter, 2, mac[ETH_ALEN - i - 1], REG_MARL + i); |
| 585 | ks8842_write8(adapter, 39, mac[ETH_ALEN - i - 1], |
| 586 | REG_MACAR1 + i); |
| 587 | } |
| 588 | spin_unlock_irqrestore(&adapter->lock, flags); |
| 589 | return 0; |
| 590 | } |
| 591 | |
| 592 | static void ks8842_tx_timeout(struct net_device *netdev) |
| 593 | { |
| 594 | struct ks8842_adapter *adapter = netdev_priv(netdev); |
| 595 | unsigned long flags; |
| 596 | |
| 597 | dev_dbg(&adapter->pdev->dev, "%s: entry\n", __func__); |
| 598 | |
| 599 | spin_lock_irqsave(&adapter->lock, flags); |
| 600 | /* disable interrupts */ |
| 601 | ks8842_write16(adapter, 18, 0, REG_IER); |
| 602 | ks8842_write16(adapter, 18, 0xFFFF, REG_ISR); |
| 603 | spin_unlock_irqrestore(&adapter->lock, flags); |
| 604 | |
| 605 | ks8842_reset_hw(adapter); |
| 606 | |
| 607 | ks8842_update_link_status(netdev, adapter); |
| 608 | } |
| 609 | |
| 610 | static const struct net_device_ops ks8842_netdev_ops = { |
| 611 | .ndo_open = ks8842_open, |
| 612 | .ndo_stop = ks8842_close, |
| 613 | .ndo_start_xmit = ks8842_xmit_frame, |
| 614 | .ndo_set_mac_address = ks8842_set_mac, |
| 615 | .ndo_tx_timeout = ks8842_tx_timeout, |
| 616 | .ndo_validate_addr = eth_validate_addr |
| 617 | }; |
| 618 | |
Stephen Hemminger | 0fc0b73 | 2009-09-02 01:03:33 -0700 | [diff] [blame] | 619 | static const struct ethtool_ops ks8842_ethtool_ops = { |
Richard Röjfors | b07878e | 2009-06-04 03:35:55 +0000 | [diff] [blame] | 620 | .get_link = ethtool_op_get_link, |
| 621 | }; |
| 622 | |
| 623 | static int __devinit ks8842_probe(struct platform_device *pdev) |
| 624 | { |
| 625 | int err = -ENOMEM; |
| 626 | struct resource *iomem; |
| 627 | struct net_device *netdev; |
| 628 | struct ks8842_adapter *adapter; |
| 629 | u16 id; |
| 630 | |
| 631 | iomem = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 632 | if (!request_mem_region(iomem->start, resource_size(iomem), DRV_NAME)) |
| 633 | goto err_mem_region; |
| 634 | |
| 635 | netdev = alloc_etherdev(sizeof(struct ks8842_adapter)); |
| 636 | if (!netdev) |
| 637 | goto err_alloc_etherdev; |
| 638 | |
| 639 | SET_NETDEV_DEV(netdev, &pdev->dev); |
| 640 | |
| 641 | adapter = netdev_priv(netdev); |
| 642 | adapter->hw_addr = ioremap(iomem->start, resource_size(iomem)); |
| 643 | if (!adapter->hw_addr) |
| 644 | goto err_ioremap; |
| 645 | |
| 646 | adapter->irq = platform_get_irq(pdev, 0); |
| 647 | if (adapter->irq < 0) { |
| 648 | err = adapter->irq; |
| 649 | goto err_get_irq; |
| 650 | } |
| 651 | |
| 652 | adapter->pdev = pdev; |
| 653 | |
| 654 | tasklet_init(&adapter->tasklet, ks8842_tasklet, (unsigned long)netdev); |
| 655 | spin_lock_init(&adapter->lock); |
| 656 | |
| 657 | netdev->netdev_ops = &ks8842_netdev_ops; |
| 658 | netdev->ethtool_ops = &ks8842_ethtool_ops; |
| 659 | |
| 660 | ks8842_read_mac_addr(adapter, netdev->dev_addr); |
| 661 | |
| 662 | id = ks8842_read16(adapter, 32, REG_SW_ID_AND_ENABLE); |
| 663 | |
| 664 | strcpy(netdev->name, "eth%d"); |
| 665 | err = register_netdev(netdev); |
| 666 | if (err) |
| 667 | goto err_register; |
| 668 | |
| 669 | platform_set_drvdata(pdev, netdev); |
| 670 | |
| 671 | printk(KERN_INFO DRV_NAME |
| 672 | " Found chip, family: 0x%x, id: 0x%x, rev: 0x%x\n", |
| 673 | (id >> 8) & 0xff, (id >> 4) & 0xf, (id >> 1) & 0x7); |
| 674 | |
| 675 | return 0; |
| 676 | |
| 677 | err_register: |
| 678 | err_get_irq: |
| 679 | iounmap(adapter->hw_addr); |
| 680 | err_ioremap: |
| 681 | free_netdev(netdev); |
| 682 | err_alloc_etherdev: |
| 683 | release_mem_region(iomem->start, resource_size(iomem)); |
| 684 | err_mem_region: |
| 685 | return err; |
| 686 | } |
| 687 | |
| 688 | static int __devexit ks8842_remove(struct platform_device *pdev) |
| 689 | { |
| 690 | struct net_device *netdev = platform_get_drvdata(pdev); |
| 691 | struct ks8842_adapter *adapter = netdev_priv(netdev); |
| 692 | struct resource *iomem = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 693 | |
| 694 | unregister_netdev(netdev); |
| 695 | tasklet_kill(&adapter->tasklet); |
| 696 | iounmap(adapter->hw_addr); |
| 697 | free_netdev(netdev); |
| 698 | release_mem_region(iomem->start, resource_size(iomem)); |
| 699 | platform_set_drvdata(pdev, NULL); |
| 700 | return 0; |
| 701 | } |
| 702 | |
| 703 | |
| 704 | static struct platform_driver ks8842_platform_driver = { |
| 705 | .driver = { |
| 706 | .name = DRV_NAME, |
| 707 | .owner = THIS_MODULE, |
| 708 | }, |
| 709 | .probe = ks8842_probe, |
| 710 | .remove = ks8842_remove, |
| 711 | }; |
| 712 | |
| 713 | static int __init ks8842_init(void) |
| 714 | { |
| 715 | return platform_driver_register(&ks8842_platform_driver); |
| 716 | } |
| 717 | |
| 718 | static void __exit ks8842_exit(void) |
| 719 | { |
| 720 | platform_driver_unregister(&ks8842_platform_driver); |
| 721 | } |
| 722 | |
| 723 | module_init(ks8842_init); |
| 724 | module_exit(ks8842_exit); |
| 725 | |
| 726 | MODULE_DESCRIPTION("Timberdale KS8842 ethernet driver"); |
| 727 | MODULE_AUTHOR("Mocean Laboratories <info@mocean-labs.com>"); |
| 728 | MODULE_LICENSE("GPL v2"); |
| 729 | MODULE_ALIAS("platform:ks8842"); |
| 730 | |