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