Tristram Ha | 3320eae | 2009-12-03 11:06:42 +0000 | [diff] [blame] | 1 | /* drivers/net/ks8851.c |
Ben Dooks | 3ba81f3 | 2009-07-16 05:24:08 +0000 | [diff] [blame] | 2 | * |
| 3 | * Copyright 2009 Simtec Electronics |
| 4 | * http://www.simtec.co.uk/ |
| 5 | * Ben Dooks <ben@simtec.co.uk> |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License version 2 as |
| 9 | * published by the Free Software Foundation. |
| 10 | */ |
| 11 | |
Joe Perches | 0dc7d2b | 2010-02-27 14:43:51 +0000 | [diff] [blame] | 12 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 13 | |
Ben Dooks | 3ba81f3 | 2009-07-16 05:24:08 +0000 | [diff] [blame] | 14 | #define DEBUG |
| 15 | |
| 16 | #include <linux/module.h> |
| 17 | #include <linux/kernel.h> |
| 18 | #include <linux/netdevice.h> |
| 19 | #include <linux/etherdevice.h> |
| 20 | #include <linux/ethtool.h> |
| 21 | #include <linux/cache.h> |
| 22 | #include <linux/crc32.h> |
| 23 | #include <linux/mii.h> |
| 24 | |
| 25 | #include <linux/spi/spi.h> |
| 26 | |
| 27 | #include "ks8851.h" |
| 28 | |
| 29 | /** |
| 30 | * struct ks8851_rxctrl - KS8851 driver rx control |
| 31 | * @mchash: Multicast hash-table data. |
| 32 | * @rxcr1: KS_RXCR1 register setting |
| 33 | * @rxcr2: KS_RXCR2 register setting |
| 34 | * |
| 35 | * Representation of the settings needs to control the receive filtering |
| 36 | * such as the multicast hash-filter and the receive register settings. This |
| 37 | * is used to make the job of working out if the receive settings change and |
| 38 | * then issuing the new settings to the worker that will send the necessary |
| 39 | * commands. |
| 40 | */ |
| 41 | struct ks8851_rxctrl { |
| 42 | u16 mchash[4]; |
| 43 | u16 rxcr1; |
| 44 | u16 rxcr2; |
| 45 | }; |
| 46 | |
| 47 | /** |
| 48 | * union ks8851_tx_hdr - tx header data |
| 49 | * @txb: The header as bytes |
| 50 | * @txw: The header as 16bit, little-endian words |
| 51 | * |
| 52 | * A dual representation of the tx header data to allow |
| 53 | * access to individual bytes, and to allow 16bit accesses |
| 54 | * with 16bit alignment. |
| 55 | */ |
| 56 | union ks8851_tx_hdr { |
| 57 | u8 txb[6]; |
| 58 | __le16 txw[3]; |
| 59 | }; |
| 60 | |
| 61 | /** |
| 62 | * struct ks8851_net - KS8851 driver private data |
| 63 | * @netdev: The network device we're bound to |
| 64 | * @spidev: The spi device we're bound to. |
| 65 | * @lock: Lock to ensure that the device is not accessed when busy. |
| 66 | * @statelock: Lock on this structure for tx list. |
| 67 | * @mii: The MII state information for the mii calls. |
| 68 | * @rxctrl: RX settings for @rxctrl_work. |
| 69 | * @tx_work: Work queue for tx packets |
| 70 | * @irq_work: Work queue for servicing interrupts |
| 71 | * @rxctrl_work: Work queue for updating RX mode and multicast lists |
| 72 | * @txq: Queue of packets for transmission. |
| 73 | * @spi_msg1: pre-setup SPI transfer with one message, @spi_xfer1. |
| 74 | * @spi_msg2: pre-setup SPI transfer with two messages, @spi_xfer2. |
| 75 | * @txh: Space for generating packet TX header in DMA-able data |
| 76 | * @rxd: Space for receiving SPI data, in DMA-able space. |
| 77 | * @txd: Space for transmitting SPI data, in DMA-able space. |
| 78 | * @msg_enable: The message flags controlling driver output (see ethtool). |
| 79 | * @fid: Incrementing frame id tag. |
| 80 | * @rc_ier: Cached copy of KS_IER. |
| 81 | * @rc_rxqcr: Cached copy of KS_RXQCR. |
| 82 | * |
| 83 | * The @lock ensures that the chip is protected when certain operations are |
| 84 | * in progress. When the read or write packet transfer is in progress, most |
| 85 | * of the chip registers are not ccessible until the transfer is finished and |
| 86 | * the DMA has been de-asserted. |
| 87 | * |
| 88 | * The @statelock is used to protect information in the structure which may |
| 89 | * need to be accessed via several sources, such as the network driver layer |
| 90 | * or one of the work queues. |
| 91 | * |
| 92 | * We align the buffers we may use for rx/tx to ensure that if the SPI driver |
| 93 | * wants to DMA map them, it will not have any problems with data the driver |
| 94 | * modifies. |
| 95 | */ |
| 96 | struct ks8851_net { |
| 97 | struct net_device *netdev; |
| 98 | struct spi_device *spidev; |
| 99 | struct mutex lock; |
| 100 | spinlock_t statelock; |
| 101 | |
| 102 | union ks8851_tx_hdr txh ____cacheline_aligned; |
| 103 | u8 rxd[8]; |
| 104 | u8 txd[8]; |
| 105 | |
| 106 | u32 msg_enable ____cacheline_aligned; |
| 107 | u16 tx_space; |
| 108 | u8 fid; |
| 109 | |
| 110 | u16 rc_ier; |
| 111 | u16 rc_rxqcr; |
| 112 | |
| 113 | struct mii_if_info mii; |
| 114 | struct ks8851_rxctrl rxctrl; |
| 115 | |
| 116 | struct work_struct tx_work; |
| 117 | struct work_struct irq_work; |
| 118 | struct work_struct rxctrl_work; |
| 119 | |
| 120 | struct sk_buff_head txq; |
| 121 | |
| 122 | struct spi_message spi_msg1; |
| 123 | struct spi_message spi_msg2; |
| 124 | struct spi_transfer spi_xfer1; |
| 125 | struct spi_transfer spi_xfer2[2]; |
| 126 | }; |
| 127 | |
| 128 | static int msg_enable; |
| 129 | |
Ben Dooks | 3ba81f3 | 2009-07-16 05:24:08 +0000 | [diff] [blame] | 130 | /* shift for byte-enable data */ |
| 131 | #define BYTE_EN(_x) ((_x) << 2) |
| 132 | |
| 133 | /* turn register number and byte-enable mask into data for start of packet */ |
| 134 | #define MK_OP(_byteen, _reg) (BYTE_EN(_byteen) | (_reg) << (8+2) | (_reg) >> 6) |
| 135 | |
| 136 | /* SPI register read/write calls. |
| 137 | * |
| 138 | * All these calls issue SPI transactions to access the chip's registers. They |
| 139 | * all require that the necessary lock is held to prevent accesses when the |
| 140 | * chip is busy transfering packet data (RX/TX FIFO accesses). |
| 141 | */ |
| 142 | |
| 143 | /** |
| 144 | * ks8851_wrreg16 - write 16bit register value to chip |
| 145 | * @ks: The chip state |
| 146 | * @reg: The register address |
| 147 | * @val: The value to write |
| 148 | * |
| 149 | * Issue a write to put the value @val into the register specified in @reg. |
| 150 | */ |
| 151 | static void ks8851_wrreg16(struct ks8851_net *ks, unsigned reg, unsigned val) |
| 152 | { |
| 153 | struct spi_transfer *xfer = &ks->spi_xfer1; |
| 154 | struct spi_message *msg = &ks->spi_msg1; |
| 155 | __le16 txb[2]; |
| 156 | int ret; |
| 157 | |
| 158 | txb[0] = cpu_to_le16(MK_OP(reg & 2 ? 0xC : 0x03, reg) | KS_SPIOP_WR); |
| 159 | txb[1] = cpu_to_le16(val); |
| 160 | |
| 161 | xfer->tx_buf = txb; |
| 162 | xfer->rx_buf = NULL; |
| 163 | xfer->len = 4; |
| 164 | |
| 165 | ret = spi_sync(ks->spidev, msg); |
| 166 | if (ret < 0) |
Joe Perches | 0dc7d2b | 2010-02-27 14:43:51 +0000 | [diff] [blame] | 167 | netdev_err(ks->netdev, "spi_sync() failed\n"); |
Ben Dooks | 3ba81f3 | 2009-07-16 05:24:08 +0000 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | /** |
Ben Dooks | 160d0fa | 2009-10-19 23:49:04 +0000 | [diff] [blame] | 171 | * ks8851_wrreg8 - write 8bit register value to chip |
| 172 | * @ks: The chip state |
| 173 | * @reg: The register address |
| 174 | * @val: The value to write |
| 175 | * |
| 176 | * Issue a write to put the value @val into the register specified in @reg. |
| 177 | */ |
| 178 | static void ks8851_wrreg8(struct ks8851_net *ks, unsigned reg, unsigned val) |
| 179 | { |
| 180 | struct spi_transfer *xfer = &ks->spi_xfer1; |
| 181 | struct spi_message *msg = &ks->spi_msg1; |
| 182 | __le16 txb[2]; |
| 183 | int ret; |
| 184 | int bit; |
| 185 | |
| 186 | bit = 1 << (reg & 3); |
| 187 | |
| 188 | txb[0] = cpu_to_le16(MK_OP(bit, reg) | KS_SPIOP_WR); |
| 189 | txb[1] = val; |
| 190 | |
| 191 | xfer->tx_buf = txb; |
| 192 | xfer->rx_buf = NULL; |
| 193 | xfer->len = 3; |
| 194 | |
| 195 | ret = spi_sync(ks->spidev, msg); |
| 196 | if (ret < 0) |
Joe Perches | 0dc7d2b | 2010-02-27 14:43:51 +0000 | [diff] [blame] | 197 | netdev_err(ks->netdev, "spi_sync() failed\n"); |
Ben Dooks | 160d0fa | 2009-10-19 23:49:04 +0000 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | /** |
Ben Dooks | 3ba81f3 | 2009-07-16 05:24:08 +0000 | [diff] [blame] | 201 | * ks8851_rx_1msg - select whether to use one or two messages for spi read |
| 202 | * @ks: The device structure |
| 203 | * |
| 204 | * Return whether to generate a single message with a tx and rx buffer |
| 205 | * supplied to spi_sync(), or alternatively send the tx and rx buffers |
| 206 | * as separate messages. |
| 207 | * |
| 208 | * Depending on the hardware in use, a single message may be more efficient |
| 209 | * on interrupts or work done by the driver. |
| 210 | * |
| 211 | * This currently always returns true until we add some per-device data passed |
| 212 | * from the platform code to specify which mode is better. |
| 213 | */ |
| 214 | static inline bool ks8851_rx_1msg(struct ks8851_net *ks) |
| 215 | { |
| 216 | return true; |
| 217 | } |
| 218 | |
| 219 | /** |
| 220 | * ks8851_rdreg - issue read register command and return the data |
| 221 | * @ks: The device state |
| 222 | * @op: The register address and byte enables in message format. |
| 223 | * @rxb: The RX buffer to return the result into |
| 224 | * @rxl: The length of data expected. |
| 225 | * |
| 226 | * This is the low level read call that issues the necessary spi message(s) |
| 227 | * to read data from the register specified in @op. |
| 228 | */ |
| 229 | static void ks8851_rdreg(struct ks8851_net *ks, unsigned op, |
| 230 | u8 *rxb, unsigned rxl) |
| 231 | { |
| 232 | struct spi_transfer *xfer; |
| 233 | struct spi_message *msg; |
| 234 | __le16 *txb = (__le16 *)ks->txd; |
| 235 | u8 *trx = ks->rxd; |
| 236 | int ret; |
| 237 | |
| 238 | txb[0] = cpu_to_le16(op | KS_SPIOP_RD); |
| 239 | |
| 240 | if (ks8851_rx_1msg(ks)) { |
| 241 | msg = &ks->spi_msg1; |
| 242 | xfer = &ks->spi_xfer1; |
| 243 | |
| 244 | xfer->tx_buf = txb; |
| 245 | xfer->rx_buf = trx; |
| 246 | xfer->len = rxl + 2; |
| 247 | } else { |
| 248 | msg = &ks->spi_msg2; |
| 249 | xfer = ks->spi_xfer2; |
| 250 | |
| 251 | xfer->tx_buf = txb; |
| 252 | xfer->rx_buf = NULL; |
| 253 | xfer->len = 2; |
| 254 | |
| 255 | xfer++; |
| 256 | xfer->tx_buf = NULL; |
| 257 | xfer->rx_buf = trx; |
| 258 | xfer->len = rxl; |
| 259 | } |
| 260 | |
| 261 | ret = spi_sync(ks->spidev, msg); |
| 262 | if (ret < 0) |
Joe Perches | 0dc7d2b | 2010-02-27 14:43:51 +0000 | [diff] [blame] | 263 | netdev_err(ks->netdev, "read: spi_sync() failed\n"); |
Ben Dooks | 3ba81f3 | 2009-07-16 05:24:08 +0000 | [diff] [blame] | 264 | else if (ks8851_rx_1msg(ks)) |
| 265 | memcpy(rxb, trx + 2, rxl); |
| 266 | else |
| 267 | memcpy(rxb, trx, rxl); |
| 268 | } |
| 269 | |
| 270 | /** |
| 271 | * ks8851_rdreg8 - read 8 bit register from device |
| 272 | * @ks: The chip information |
| 273 | * @reg: The register address |
| 274 | * |
| 275 | * Read a 8bit register from the chip, returning the result |
| 276 | */ |
| 277 | static unsigned ks8851_rdreg8(struct ks8851_net *ks, unsigned reg) |
| 278 | { |
| 279 | u8 rxb[1]; |
| 280 | |
| 281 | ks8851_rdreg(ks, MK_OP(1 << (reg & 3), reg), rxb, 1); |
| 282 | return rxb[0]; |
| 283 | } |
| 284 | |
| 285 | /** |
| 286 | * ks8851_rdreg16 - read 16 bit register from device |
| 287 | * @ks: The chip information |
| 288 | * @reg: The register address |
| 289 | * |
| 290 | * Read a 16bit register from the chip, returning the result |
| 291 | */ |
| 292 | static unsigned ks8851_rdreg16(struct ks8851_net *ks, unsigned reg) |
| 293 | { |
| 294 | __le16 rx = 0; |
| 295 | |
| 296 | ks8851_rdreg(ks, MK_OP(reg & 2 ? 0xC : 0x3, reg), (u8 *)&rx, 2); |
| 297 | return le16_to_cpu(rx); |
| 298 | } |
| 299 | |
| 300 | /** |
| 301 | * ks8851_rdreg32 - read 32 bit register from device |
| 302 | * @ks: The chip information |
| 303 | * @reg: The register address |
| 304 | * |
| 305 | * Read a 32bit register from the chip. |
| 306 | * |
| 307 | * Note, this read requires the address be aligned to 4 bytes. |
| 308 | */ |
| 309 | static unsigned ks8851_rdreg32(struct ks8851_net *ks, unsigned reg) |
| 310 | { |
| 311 | __le32 rx = 0; |
| 312 | |
| 313 | WARN_ON(reg & 3); |
| 314 | |
| 315 | ks8851_rdreg(ks, MK_OP(0xf, reg), (u8 *)&rx, 4); |
| 316 | return le32_to_cpu(rx); |
| 317 | } |
| 318 | |
| 319 | /** |
| 320 | * ks8851_soft_reset - issue one of the soft reset to the device |
| 321 | * @ks: The device state. |
| 322 | * @op: The bit(s) to set in the GRR |
| 323 | * |
| 324 | * Issue the relevant soft-reset command to the device's GRR register |
| 325 | * specified by @op. |
| 326 | * |
| 327 | * Note, the delays are in there as a caution to ensure that the reset |
| 328 | * has time to take effect and then complete. Since the datasheet does |
| 329 | * not currently specify the exact sequence, we have chosen something |
| 330 | * that seems to work with our device. |
| 331 | */ |
| 332 | static void ks8851_soft_reset(struct ks8851_net *ks, unsigned op) |
| 333 | { |
| 334 | ks8851_wrreg16(ks, KS_GRR, op); |
| 335 | mdelay(1); /* wait a short time to effect reset */ |
| 336 | ks8851_wrreg16(ks, KS_GRR, 0); |
| 337 | mdelay(1); /* wait for condition to clear */ |
| 338 | } |
| 339 | |
| 340 | /** |
| 341 | * ks8851_write_mac_addr - write mac address to device registers |
| 342 | * @dev: The network device |
| 343 | * |
| 344 | * Update the KS8851 MAC address registers from the address in @dev. |
| 345 | * |
| 346 | * This call assumes that the chip is not running, so there is no need to |
| 347 | * shutdown the RXQ process whilst setting this. |
| 348 | */ |
| 349 | static int ks8851_write_mac_addr(struct net_device *dev) |
| 350 | { |
| 351 | struct ks8851_net *ks = netdev_priv(dev); |
Ben Dooks | 160d0fa | 2009-10-19 23:49:04 +0000 | [diff] [blame] | 352 | int i; |
Ben Dooks | 3ba81f3 | 2009-07-16 05:24:08 +0000 | [diff] [blame] | 353 | |
| 354 | mutex_lock(&ks->lock); |
| 355 | |
Ben Dooks | 160d0fa | 2009-10-19 23:49:04 +0000 | [diff] [blame] | 356 | for (i = 0; i < ETH_ALEN; i++) |
| 357 | ks8851_wrreg8(ks, KS_MAR(i), dev->dev_addr[i]); |
Ben Dooks | 3ba81f3 | 2009-07-16 05:24:08 +0000 | [diff] [blame] | 358 | |
| 359 | mutex_unlock(&ks->lock); |
| 360 | |
| 361 | return 0; |
| 362 | } |
| 363 | |
| 364 | /** |
| 365 | * ks8851_init_mac - initialise the mac address |
| 366 | * @ks: The device structure |
| 367 | * |
| 368 | * Get or create the initial mac address for the device and then set that |
| 369 | * into the station address register. Currently we assume that the device |
| 370 | * does not have a valid mac address in it, and so we use random_ether_addr() |
| 371 | * to create a new one. |
| 372 | * |
| 373 | * In future, the driver should check to see if the device has an EEPROM |
| 374 | * attached and whether that has a valid ethernet address in it. |
| 375 | */ |
| 376 | static void ks8851_init_mac(struct ks8851_net *ks) |
| 377 | { |
| 378 | struct net_device *dev = ks->netdev; |
| 379 | |
| 380 | random_ether_addr(dev->dev_addr); |
| 381 | ks8851_write_mac_addr(dev); |
| 382 | } |
| 383 | |
| 384 | /** |
| 385 | * ks8851_irq - device interrupt handler |
| 386 | * @irq: Interrupt number passed from the IRQ hnalder. |
| 387 | * @pw: The private word passed to register_irq(), our struct ks8851_net. |
| 388 | * |
| 389 | * Disable the interrupt from happening again until we've processed the |
| 390 | * current status by scheduling ks8851_irq_work(). |
| 391 | */ |
| 392 | static irqreturn_t ks8851_irq(int irq, void *pw) |
| 393 | { |
| 394 | struct ks8851_net *ks = pw; |
| 395 | |
| 396 | disable_irq_nosync(irq); |
| 397 | schedule_work(&ks->irq_work); |
| 398 | return IRQ_HANDLED; |
| 399 | } |
| 400 | |
| 401 | /** |
| 402 | * ks8851_rdfifo - read data from the receive fifo |
| 403 | * @ks: The device state. |
| 404 | * @buff: The buffer address |
| 405 | * @len: The length of the data to read |
| 406 | * |
Uwe Kleine-König | 9ddc5b6 | 2010-01-20 17:02:24 +0100 | [diff] [blame] | 407 | * Issue an RXQ FIFO read command and read the @len amount of data from |
Ben Dooks | 3ba81f3 | 2009-07-16 05:24:08 +0000 | [diff] [blame] | 408 | * the FIFO into the buffer specified by @buff. |
| 409 | */ |
| 410 | static void ks8851_rdfifo(struct ks8851_net *ks, u8 *buff, unsigned len) |
| 411 | { |
| 412 | struct spi_transfer *xfer = ks->spi_xfer2; |
| 413 | struct spi_message *msg = &ks->spi_msg2; |
| 414 | u8 txb[1]; |
| 415 | int ret; |
| 416 | |
Joe Perches | 0dc7d2b | 2010-02-27 14:43:51 +0000 | [diff] [blame] | 417 | netif_dbg(ks, rx_status, ks->netdev, |
| 418 | "%s: %d@%p\n", __func__, len, buff); |
Ben Dooks | 3ba81f3 | 2009-07-16 05:24:08 +0000 | [diff] [blame] | 419 | |
| 420 | /* set the operation we're issuing */ |
| 421 | txb[0] = KS_SPIOP_RXFIFO; |
| 422 | |
| 423 | xfer->tx_buf = txb; |
| 424 | xfer->rx_buf = NULL; |
| 425 | xfer->len = 1; |
| 426 | |
| 427 | xfer++; |
| 428 | xfer->rx_buf = buff; |
| 429 | xfer->tx_buf = NULL; |
| 430 | xfer->len = len; |
| 431 | |
| 432 | ret = spi_sync(ks->spidev, msg); |
| 433 | if (ret < 0) |
Joe Perches | 0dc7d2b | 2010-02-27 14:43:51 +0000 | [diff] [blame] | 434 | netdev_err(ks->netdev, "%s: spi_sync() failed\n", __func__); |
Ben Dooks | 3ba81f3 | 2009-07-16 05:24:08 +0000 | [diff] [blame] | 435 | } |
| 436 | |
| 437 | /** |
| 438 | * ks8851_dbg_dumpkkt - dump initial packet contents to debug |
| 439 | * @ks: The device state |
| 440 | * @rxpkt: The data for the received packet |
| 441 | * |
| 442 | * Dump the initial data from the packet to dev_dbg(). |
| 443 | */ |
| 444 | static void ks8851_dbg_dumpkkt(struct ks8851_net *ks, u8 *rxpkt) |
| 445 | { |
Joe Perches | 0dc7d2b | 2010-02-27 14:43:51 +0000 | [diff] [blame] | 446 | netdev_dbg(ks->netdev, |
| 447 | "pkt %02x%02x%02x%02x %02x%02x%02x%02x %02x%02x%02x%02x\n", |
| 448 | rxpkt[4], rxpkt[5], rxpkt[6], rxpkt[7], |
| 449 | rxpkt[8], rxpkt[9], rxpkt[10], rxpkt[11], |
| 450 | rxpkt[12], rxpkt[13], rxpkt[14], rxpkt[15]); |
Ben Dooks | 3ba81f3 | 2009-07-16 05:24:08 +0000 | [diff] [blame] | 451 | } |
| 452 | |
| 453 | /** |
| 454 | * ks8851_rx_pkts - receive packets from the host |
| 455 | * @ks: The device information. |
| 456 | * |
| 457 | * This is called from the IRQ work queue when the system detects that there |
| 458 | * are packets in the receive queue. Find out how many packets there are and |
| 459 | * read them from the FIFO. |
| 460 | */ |
| 461 | static void ks8851_rx_pkts(struct ks8851_net *ks) |
| 462 | { |
| 463 | struct sk_buff *skb; |
| 464 | unsigned rxfc; |
| 465 | unsigned rxlen; |
| 466 | unsigned rxstat; |
| 467 | u32 rxh; |
| 468 | u8 *rxpkt; |
| 469 | |
| 470 | rxfc = ks8851_rdreg8(ks, KS_RXFC); |
| 471 | |
Joe Perches | 0dc7d2b | 2010-02-27 14:43:51 +0000 | [diff] [blame] | 472 | netif_dbg(ks, rx_status, ks->netdev, |
| 473 | "%s: %d packets\n", __func__, rxfc); |
Ben Dooks | 3ba81f3 | 2009-07-16 05:24:08 +0000 | [diff] [blame] | 474 | |
| 475 | /* Currently we're issuing a read per packet, but we could possibly |
| 476 | * improve the code by issuing a single read, getting the receive |
| 477 | * header, allocating the packet and then reading the packet data |
| 478 | * out in one go. |
| 479 | * |
| 480 | * This form of operation would require us to hold the SPI bus' |
| 481 | * chipselect low during the entie transaction to avoid any |
| 482 | * reset to the data stream comming from the chip. |
| 483 | */ |
| 484 | |
| 485 | for (; rxfc != 0; rxfc--) { |
| 486 | rxh = ks8851_rdreg32(ks, KS_RXFHSR); |
| 487 | rxstat = rxh & 0xffff; |
| 488 | rxlen = rxh >> 16; |
| 489 | |
Joe Perches | 0dc7d2b | 2010-02-27 14:43:51 +0000 | [diff] [blame] | 490 | netif_dbg(ks, rx_status, ks->netdev, |
| 491 | "rx: stat 0x%04x, len 0x%04x\n", rxstat, rxlen); |
Ben Dooks | 3ba81f3 | 2009-07-16 05:24:08 +0000 | [diff] [blame] | 492 | |
| 493 | /* the length of the packet includes the 32bit CRC */ |
| 494 | |
| 495 | /* set dma read address */ |
| 496 | ks8851_wrreg16(ks, KS_RXFDPR, RXFDPR_RXFPAI | 0x00); |
| 497 | |
| 498 | /* start the packet dma process, and set auto-dequeue rx */ |
| 499 | ks8851_wrreg16(ks, KS_RXQCR, |
| 500 | ks->rc_rxqcr | RXQCR_SDA | RXQCR_ADRFE); |
| 501 | |
| 502 | if (rxlen > 0) { |
| 503 | skb = netdev_alloc_skb(ks->netdev, rxlen + 2 + 8); |
| 504 | if (!skb) { |
| 505 | /* todo - dump frame and move on */ |
| 506 | } |
| 507 | |
| 508 | /* two bytes to ensure ip is aligned, and four bytes |
| 509 | * for the status header and 4 bytes of garbage */ |
| 510 | skb_reserve(skb, 2 + 4 + 4); |
| 511 | |
| 512 | rxpkt = skb_put(skb, rxlen - 4) - 8; |
| 513 | |
| 514 | /* align the packet length to 4 bytes, and add 4 bytes |
| 515 | * as we're getting the rx status header as well */ |
| 516 | ks8851_rdfifo(ks, rxpkt, ALIGN(rxlen, 4) + 8); |
| 517 | |
| 518 | if (netif_msg_pktdata(ks)) |
| 519 | ks8851_dbg_dumpkkt(ks, rxpkt); |
| 520 | |
| 521 | skb->protocol = eth_type_trans(skb, ks->netdev); |
| 522 | netif_rx(skb); |
| 523 | |
| 524 | ks->netdev->stats.rx_packets++; |
| 525 | ks->netdev->stats.rx_bytes += rxlen - 4; |
| 526 | } |
| 527 | |
| 528 | ks8851_wrreg16(ks, KS_RXQCR, ks->rc_rxqcr); |
| 529 | } |
| 530 | } |
| 531 | |
| 532 | /** |
| 533 | * ks8851_irq_work - work queue handler for dealing with interrupt requests |
| 534 | * @work: The work structure that was scheduled by schedule_work() |
| 535 | * |
| 536 | * This is the handler invoked when the ks8851_irq() is called to find out |
| 537 | * what happened, as we cannot allow ourselves to sleep whilst waiting for |
| 538 | * anything other process has the chip's lock. |
| 539 | * |
| 540 | * Read the interrupt status, work out what needs to be done and then clear |
| 541 | * any of the interrupts that are not needed. |
| 542 | */ |
| 543 | static void ks8851_irq_work(struct work_struct *work) |
| 544 | { |
| 545 | struct ks8851_net *ks = container_of(work, struct ks8851_net, irq_work); |
| 546 | unsigned status; |
| 547 | unsigned handled = 0; |
| 548 | |
| 549 | mutex_lock(&ks->lock); |
| 550 | |
| 551 | status = ks8851_rdreg16(ks, KS_ISR); |
| 552 | |
Joe Perches | 0dc7d2b | 2010-02-27 14:43:51 +0000 | [diff] [blame] | 553 | netif_dbg(ks, intr, ks->netdev, |
| 554 | "%s: status 0x%04x\n", __func__, status); |
Ben Dooks | 3ba81f3 | 2009-07-16 05:24:08 +0000 | [diff] [blame] | 555 | |
| 556 | if (status & IRQ_LCI) { |
| 557 | /* should do something about checking link status */ |
| 558 | handled |= IRQ_LCI; |
| 559 | } |
| 560 | |
| 561 | if (status & IRQ_LDI) { |
| 562 | u16 pmecr = ks8851_rdreg16(ks, KS_PMECR); |
| 563 | pmecr &= ~PMECR_WKEVT_MASK; |
| 564 | ks8851_wrreg16(ks, KS_PMECR, pmecr | PMECR_WKEVT_LINK); |
| 565 | |
| 566 | handled |= IRQ_LDI; |
| 567 | } |
| 568 | |
| 569 | if (status & IRQ_RXPSI) |
| 570 | handled |= IRQ_RXPSI; |
| 571 | |
| 572 | if (status & IRQ_TXI) { |
| 573 | handled |= IRQ_TXI; |
| 574 | |
| 575 | /* no lock here, tx queue should have been stopped */ |
| 576 | |
| 577 | /* update our idea of how much tx space is available to the |
| 578 | * system */ |
| 579 | ks->tx_space = ks8851_rdreg16(ks, KS_TXMIR); |
| 580 | |
Joe Perches | 0dc7d2b | 2010-02-27 14:43:51 +0000 | [diff] [blame] | 581 | netif_dbg(ks, intr, ks->netdev, |
| 582 | "%s: txspace %d\n", __func__, ks->tx_space); |
Ben Dooks | 3ba81f3 | 2009-07-16 05:24:08 +0000 | [diff] [blame] | 583 | } |
| 584 | |
| 585 | if (status & IRQ_RXI) |
| 586 | handled |= IRQ_RXI; |
| 587 | |
| 588 | if (status & IRQ_SPIBEI) { |
| 589 | dev_err(&ks->spidev->dev, "%s: spi bus error\n", __func__); |
| 590 | handled |= IRQ_SPIBEI; |
| 591 | } |
| 592 | |
| 593 | ks8851_wrreg16(ks, KS_ISR, handled); |
| 594 | |
| 595 | if (status & IRQ_RXI) { |
| 596 | /* the datasheet says to disable the rx interrupt during |
| 597 | * packet read-out, however we're masking the interrupt |
| 598 | * from the device so do not bother masking just the RX |
| 599 | * from the device. */ |
| 600 | |
| 601 | ks8851_rx_pkts(ks); |
| 602 | } |
| 603 | |
| 604 | /* if something stopped the rx process, probably due to wanting |
| 605 | * to change the rx settings, then do something about restarting |
| 606 | * it. */ |
| 607 | if (status & IRQ_RXPSI) { |
| 608 | struct ks8851_rxctrl *rxc = &ks->rxctrl; |
| 609 | |
| 610 | /* update the multicast hash table */ |
| 611 | ks8851_wrreg16(ks, KS_MAHTR0, rxc->mchash[0]); |
| 612 | ks8851_wrreg16(ks, KS_MAHTR1, rxc->mchash[1]); |
| 613 | ks8851_wrreg16(ks, KS_MAHTR2, rxc->mchash[2]); |
| 614 | ks8851_wrreg16(ks, KS_MAHTR3, rxc->mchash[3]); |
| 615 | |
| 616 | ks8851_wrreg16(ks, KS_RXCR2, rxc->rxcr2); |
| 617 | ks8851_wrreg16(ks, KS_RXCR1, rxc->rxcr1); |
| 618 | } |
| 619 | |
| 620 | mutex_unlock(&ks->lock); |
| 621 | |
| 622 | if (status & IRQ_TXI) |
| 623 | netif_wake_queue(ks->netdev); |
| 624 | |
| 625 | enable_irq(ks->netdev->irq); |
| 626 | } |
| 627 | |
| 628 | /** |
| 629 | * calc_txlen - calculate size of message to send packet |
| 630 | * @len: Lenght of data |
| 631 | * |
| 632 | * Returns the size of the TXFIFO message needed to send |
| 633 | * this packet. |
| 634 | */ |
| 635 | static inline unsigned calc_txlen(unsigned len) |
| 636 | { |
| 637 | return ALIGN(len + 4, 4); |
| 638 | } |
| 639 | |
| 640 | /** |
| 641 | * ks8851_wrpkt - write packet to TX FIFO |
| 642 | * @ks: The device state. |
| 643 | * @txp: The sk_buff to transmit. |
| 644 | * @irq: IRQ on completion of the packet. |
| 645 | * |
| 646 | * Send the @txp to the chip. This means creating the relevant packet header |
| 647 | * specifying the length of the packet and the other information the chip |
| 648 | * needs, such as IRQ on completion. Send the header and the packet data to |
| 649 | * the device. |
| 650 | */ |
| 651 | static void ks8851_wrpkt(struct ks8851_net *ks, struct sk_buff *txp, bool irq) |
| 652 | { |
| 653 | struct spi_transfer *xfer = ks->spi_xfer2; |
| 654 | struct spi_message *msg = &ks->spi_msg2; |
| 655 | unsigned fid = 0; |
| 656 | int ret; |
| 657 | |
Joe Perches | 0dc7d2b | 2010-02-27 14:43:51 +0000 | [diff] [blame] | 658 | netif_dbg(ks, tx_queued, ks->netdev, "%s: skb %p, %d@%p, irq %d\n", |
| 659 | __func__, txp, txp->len, txp->data, irq); |
Ben Dooks | 3ba81f3 | 2009-07-16 05:24:08 +0000 | [diff] [blame] | 660 | |
| 661 | fid = ks->fid++; |
| 662 | fid &= TXFR_TXFID_MASK; |
| 663 | |
| 664 | if (irq) |
| 665 | fid |= TXFR_TXIC; /* irq on completion */ |
| 666 | |
| 667 | /* start header at txb[1] to align txw entries */ |
| 668 | ks->txh.txb[1] = KS_SPIOP_TXFIFO; |
| 669 | ks->txh.txw[1] = cpu_to_le16(fid); |
| 670 | ks->txh.txw[2] = cpu_to_le16(txp->len); |
| 671 | |
| 672 | xfer->tx_buf = &ks->txh.txb[1]; |
| 673 | xfer->rx_buf = NULL; |
| 674 | xfer->len = 5; |
| 675 | |
| 676 | xfer++; |
| 677 | xfer->tx_buf = txp->data; |
| 678 | xfer->rx_buf = NULL; |
| 679 | xfer->len = ALIGN(txp->len, 4); |
| 680 | |
| 681 | ret = spi_sync(ks->spidev, msg); |
| 682 | if (ret < 0) |
Joe Perches | 0dc7d2b | 2010-02-27 14:43:51 +0000 | [diff] [blame] | 683 | netdev_err(ks->netdev, "%s: spi_sync() failed\n", __func__); |
Ben Dooks | 3ba81f3 | 2009-07-16 05:24:08 +0000 | [diff] [blame] | 684 | } |
| 685 | |
| 686 | /** |
| 687 | * ks8851_done_tx - update and then free skbuff after transmitting |
| 688 | * @ks: The device state |
| 689 | * @txb: The buffer transmitted |
| 690 | */ |
| 691 | static void ks8851_done_tx(struct ks8851_net *ks, struct sk_buff *txb) |
| 692 | { |
| 693 | struct net_device *dev = ks->netdev; |
| 694 | |
| 695 | dev->stats.tx_bytes += txb->len; |
| 696 | dev->stats.tx_packets++; |
| 697 | |
| 698 | dev_kfree_skb(txb); |
| 699 | } |
| 700 | |
| 701 | /** |
| 702 | * ks8851_tx_work - process tx packet(s) |
| 703 | * @work: The work strucutre what was scheduled. |
| 704 | * |
| 705 | * This is called when a number of packets have been scheduled for |
| 706 | * transmission and need to be sent to the device. |
| 707 | */ |
| 708 | static void ks8851_tx_work(struct work_struct *work) |
| 709 | { |
| 710 | struct ks8851_net *ks = container_of(work, struct ks8851_net, tx_work); |
| 711 | struct sk_buff *txb; |
Tristram Ha | 3320eae | 2009-12-03 11:06:42 +0000 | [diff] [blame] | 712 | bool last = skb_queue_empty(&ks->txq); |
Ben Dooks | 3ba81f3 | 2009-07-16 05:24:08 +0000 | [diff] [blame] | 713 | |
| 714 | mutex_lock(&ks->lock); |
| 715 | |
| 716 | while (!last) { |
| 717 | txb = skb_dequeue(&ks->txq); |
| 718 | last = skb_queue_empty(&ks->txq); |
| 719 | |
| 720 | ks8851_wrreg16(ks, KS_RXQCR, ks->rc_rxqcr | RXQCR_SDA); |
| 721 | ks8851_wrpkt(ks, txb, last); |
| 722 | ks8851_wrreg16(ks, KS_RXQCR, ks->rc_rxqcr); |
| 723 | ks8851_wrreg16(ks, KS_TXQCR, TXQCR_METFE); |
| 724 | |
| 725 | ks8851_done_tx(ks, txb); |
| 726 | } |
| 727 | |
| 728 | mutex_unlock(&ks->lock); |
| 729 | } |
| 730 | |
| 731 | /** |
| 732 | * ks8851_set_powermode - set power mode of the device |
| 733 | * @ks: The device state |
| 734 | * @pwrmode: The power mode value to write to KS_PMECR. |
| 735 | * |
| 736 | * Change the power mode of the chip. |
| 737 | */ |
| 738 | static void ks8851_set_powermode(struct ks8851_net *ks, unsigned pwrmode) |
| 739 | { |
| 740 | unsigned pmecr; |
| 741 | |
Joe Perches | 0dc7d2b | 2010-02-27 14:43:51 +0000 | [diff] [blame] | 742 | netif_dbg(ks, hw, ks->netdev, "setting power mode %d\n", pwrmode); |
Ben Dooks | 3ba81f3 | 2009-07-16 05:24:08 +0000 | [diff] [blame] | 743 | |
| 744 | pmecr = ks8851_rdreg16(ks, KS_PMECR); |
| 745 | pmecr &= ~PMECR_PM_MASK; |
| 746 | pmecr |= pwrmode; |
| 747 | |
| 748 | ks8851_wrreg16(ks, KS_PMECR, pmecr); |
| 749 | } |
| 750 | |
| 751 | /** |
| 752 | * ks8851_net_open - open network device |
| 753 | * @dev: The network device being opened. |
| 754 | * |
| 755 | * Called when the network device is marked active, such as a user executing |
| 756 | * 'ifconfig up' on the device. |
| 757 | */ |
| 758 | static int ks8851_net_open(struct net_device *dev) |
| 759 | { |
| 760 | struct ks8851_net *ks = netdev_priv(dev); |
| 761 | |
| 762 | /* lock the card, even if we may not actually be doing anything |
| 763 | * else at the moment */ |
| 764 | mutex_lock(&ks->lock); |
| 765 | |
Joe Perches | 0dc7d2b | 2010-02-27 14:43:51 +0000 | [diff] [blame] | 766 | netif_dbg(ks, ifup, ks->netdev, "opening\n"); |
Ben Dooks | 3ba81f3 | 2009-07-16 05:24:08 +0000 | [diff] [blame] | 767 | |
| 768 | /* bring chip out of any power saving mode it was in */ |
| 769 | ks8851_set_powermode(ks, PMECR_PM_NORMAL); |
| 770 | |
| 771 | /* issue a soft reset to the RX/TX QMU to put it into a known |
| 772 | * state. */ |
| 773 | ks8851_soft_reset(ks, GRR_QMU); |
| 774 | |
| 775 | /* setup transmission parameters */ |
| 776 | |
| 777 | ks8851_wrreg16(ks, KS_TXCR, (TXCR_TXE | /* enable transmit process */ |
| 778 | TXCR_TXPE | /* pad to min length */ |
| 779 | TXCR_TXCRC | /* add CRC */ |
| 780 | TXCR_TXFCE)); /* enable flow control */ |
| 781 | |
| 782 | /* auto-increment tx data, reset tx pointer */ |
| 783 | ks8851_wrreg16(ks, KS_TXFDPR, TXFDPR_TXFPAI); |
| 784 | |
| 785 | /* setup receiver control */ |
| 786 | |
| 787 | ks8851_wrreg16(ks, KS_RXCR1, (RXCR1_RXPAFMA | /* from mac filter */ |
| 788 | RXCR1_RXFCE | /* enable flow control */ |
| 789 | RXCR1_RXBE | /* broadcast enable */ |
| 790 | RXCR1_RXUE | /* unicast enable */ |
| 791 | RXCR1_RXE)); /* enable rx block */ |
| 792 | |
| 793 | /* transfer entire frames out in one go */ |
| 794 | ks8851_wrreg16(ks, KS_RXCR2, RXCR2_SRDBL_FRAME); |
| 795 | |
| 796 | /* set receive counter timeouts */ |
| 797 | ks8851_wrreg16(ks, KS_RXDTTR, 1000); /* 1ms after first frame to IRQ */ |
| 798 | ks8851_wrreg16(ks, KS_RXDBCTR, 4096); /* >4Kbytes in buffer to IRQ */ |
| 799 | ks8851_wrreg16(ks, KS_RXFCTR, 10); /* 10 frames to IRQ */ |
| 800 | |
| 801 | ks->rc_rxqcr = (RXQCR_RXFCTE | /* IRQ on frame count exceeded */ |
| 802 | RXQCR_RXDBCTE | /* IRQ on byte count exceeded */ |
| 803 | RXQCR_RXDTTE); /* IRQ on time exceeded */ |
| 804 | |
| 805 | ks8851_wrreg16(ks, KS_RXQCR, ks->rc_rxqcr); |
| 806 | |
| 807 | /* clear then enable interrupts */ |
| 808 | |
| 809 | #define STD_IRQ (IRQ_LCI | /* Link Change */ \ |
| 810 | IRQ_TXI | /* TX done */ \ |
| 811 | IRQ_RXI | /* RX done */ \ |
| 812 | IRQ_SPIBEI | /* SPI bus error */ \ |
| 813 | IRQ_TXPSI | /* TX process stop */ \ |
| 814 | IRQ_RXPSI) /* RX process stop */ |
| 815 | |
| 816 | ks->rc_ier = STD_IRQ; |
| 817 | ks8851_wrreg16(ks, KS_ISR, STD_IRQ); |
| 818 | ks8851_wrreg16(ks, KS_IER, STD_IRQ); |
| 819 | |
| 820 | netif_start_queue(ks->netdev); |
| 821 | |
Joe Perches | 0dc7d2b | 2010-02-27 14:43:51 +0000 | [diff] [blame] | 822 | netif_dbg(ks, ifup, ks->netdev, "network device up\n"); |
Ben Dooks | 3ba81f3 | 2009-07-16 05:24:08 +0000 | [diff] [blame] | 823 | |
| 824 | mutex_unlock(&ks->lock); |
| 825 | return 0; |
| 826 | } |
| 827 | |
| 828 | /** |
| 829 | * ks8851_net_stop - close network device |
| 830 | * @dev: The device being closed. |
| 831 | * |
| 832 | * Called to close down a network device which has been active. Cancell any |
| 833 | * work, shutdown the RX and TX process and then place the chip into a low |
| 834 | * power state whilst it is not being used. |
| 835 | */ |
| 836 | static int ks8851_net_stop(struct net_device *dev) |
| 837 | { |
| 838 | struct ks8851_net *ks = netdev_priv(dev); |
| 839 | |
Joe Perches | 0dc7d2b | 2010-02-27 14:43:51 +0000 | [diff] [blame] | 840 | netif_info(ks, ifdown, dev, "shutting down\n"); |
Ben Dooks | 3ba81f3 | 2009-07-16 05:24:08 +0000 | [diff] [blame] | 841 | |
| 842 | netif_stop_queue(dev); |
| 843 | |
| 844 | mutex_lock(&ks->lock); |
| 845 | |
| 846 | /* stop any outstanding work */ |
| 847 | flush_work(&ks->irq_work); |
| 848 | flush_work(&ks->tx_work); |
| 849 | flush_work(&ks->rxctrl_work); |
| 850 | |
| 851 | /* turn off the IRQs and ack any outstanding */ |
| 852 | ks8851_wrreg16(ks, KS_IER, 0x0000); |
| 853 | ks8851_wrreg16(ks, KS_ISR, 0xffff); |
| 854 | |
| 855 | /* shutdown RX process */ |
| 856 | ks8851_wrreg16(ks, KS_RXCR1, 0x0000); |
| 857 | |
| 858 | /* shutdown TX process */ |
| 859 | ks8851_wrreg16(ks, KS_TXCR, 0x0000); |
| 860 | |
| 861 | /* set powermode to soft power down to save power */ |
| 862 | ks8851_set_powermode(ks, PMECR_PM_SOFTDOWN); |
| 863 | |
| 864 | /* ensure any queued tx buffers are dumped */ |
| 865 | while (!skb_queue_empty(&ks->txq)) { |
| 866 | struct sk_buff *txb = skb_dequeue(&ks->txq); |
| 867 | |
Joe Perches | 0dc7d2b | 2010-02-27 14:43:51 +0000 | [diff] [blame] | 868 | netif_dbg(ks, ifdown, ks->netdev, |
| 869 | "%s: freeing txb %p\n", __func__, txb); |
Ben Dooks | 3ba81f3 | 2009-07-16 05:24:08 +0000 | [diff] [blame] | 870 | |
| 871 | dev_kfree_skb(txb); |
| 872 | } |
| 873 | |
| 874 | mutex_unlock(&ks->lock); |
| 875 | return 0; |
| 876 | } |
| 877 | |
| 878 | /** |
| 879 | * ks8851_start_xmit - transmit packet |
| 880 | * @skb: The buffer to transmit |
| 881 | * @dev: The device used to transmit the packet. |
| 882 | * |
| 883 | * Called by the network layer to transmit the @skb. Queue the packet for |
| 884 | * the device and schedule the necessary work to transmit the packet when |
| 885 | * it is free. |
| 886 | * |
| 887 | * We do this to firstly avoid sleeping with the network device locked, |
| 888 | * and secondly so we can round up more than one packet to transmit which |
| 889 | * means we can try and avoid generating too many transmit done interrupts. |
| 890 | */ |
Stephen Hemminger | 61357325 | 2009-08-31 19:50:58 +0000 | [diff] [blame] | 891 | static netdev_tx_t ks8851_start_xmit(struct sk_buff *skb, |
| 892 | struct net_device *dev) |
Ben Dooks | 3ba81f3 | 2009-07-16 05:24:08 +0000 | [diff] [blame] | 893 | { |
| 894 | struct ks8851_net *ks = netdev_priv(dev); |
| 895 | unsigned needed = calc_txlen(skb->len); |
Stephen Hemminger | 61357325 | 2009-08-31 19:50:58 +0000 | [diff] [blame] | 896 | netdev_tx_t ret = NETDEV_TX_OK; |
Ben Dooks | 3ba81f3 | 2009-07-16 05:24:08 +0000 | [diff] [blame] | 897 | |
Joe Perches | 0dc7d2b | 2010-02-27 14:43:51 +0000 | [diff] [blame] | 898 | netif_dbg(ks, tx_queued, ks->netdev, |
| 899 | "%s: skb %p, %d@%p\n", __func__, skb, skb->len, skb->data); |
Ben Dooks | 3ba81f3 | 2009-07-16 05:24:08 +0000 | [diff] [blame] | 900 | |
| 901 | spin_lock(&ks->statelock); |
| 902 | |
| 903 | if (needed > ks->tx_space) { |
| 904 | netif_stop_queue(dev); |
| 905 | ret = NETDEV_TX_BUSY; |
| 906 | } else { |
| 907 | ks->tx_space -= needed; |
| 908 | skb_queue_tail(&ks->txq, skb); |
| 909 | } |
| 910 | |
| 911 | spin_unlock(&ks->statelock); |
| 912 | schedule_work(&ks->tx_work); |
| 913 | |
| 914 | return ret; |
| 915 | } |
| 916 | |
| 917 | /** |
| 918 | * ks8851_rxctrl_work - work handler to change rx mode |
| 919 | * @work: The work structure this belongs to. |
| 920 | * |
| 921 | * Lock the device and issue the necessary changes to the receive mode from |
| 922 | * the network device layer. This is done so that we can do this without |
| 923 | * having to sleep whilst holding the network device lock. |
| 924 | * |
| 925 | * Since the recommendation from Micrel is that the RXQ is shutdown whilst the |
| 926 | * receive parameters are programmed, we issue a write to disable the RXQ and |
| 927 | * then wait for the interrupt handler to be triggered once the RXQ shutdown is |
| 928 | * complete. The interrupt handler then writes the new values into the chip. |
| 929 | */ |
| 930 | static void ks8851_rxctrl_work(struct work_struct *work) |
| 931 | { |
| 932 | struct ks8851_net *ks = container_of(work, struct ks8851_net, rxctrl_work); |
| 933 | |
| 934 | mutex_lock(&ks->lock); |
| 935 | |
| 936 | /* need to shutdown RXQ before modifying filter parameters */ |
| 937 | ks8851_wrreg16(ks, KS_RXCR1, 0x00); |
| 938 | |
| 939 | mutex_unlock(&ks->lock); |
| 940 | } |
| 941 | |
| 942 | static void ks8851_set_rx_mode(struct net_device *dev) |
| 943 | { |
| 944 | struct ks8851_net *ks = netdev_priv(dev); |
| 945 | struct ks8851_rxctrl rxctrl; |
| 946 | |
| 947 | memset(&rxctrl, 0, sizeof(rxctrl)); |
| 948 | |
| 949 | if (dev->flags & IFF_PROMISC) { |
| 950 | /* interface to receive everything */ |
| 951 | |
| 952 | rxctrl.rxcr1 = RXCR1_RXAE | RXCR1_RXINVF; |
| 953 | } else if (dev->flags & IFF_ALLMULTI) { |
| 954 | /* accept all multicast packets */ |
| 955 | |
| 956 | rxctrl.rxcr1 = (RXCR1_RXME | RXCR1_RXAE | |
| 957 | RXCR1_RXPAFMA | RXCR1_RXMAFMA); |
Jiri Pirko | 4cd24ea | 2010-02-08 04:30:35 +0000 | [diff] [blame] | 958 | } else if (dev->flags & IFF_MULTICAST && !netdev_mc_empty(dev)) { |
Jiri Pirko | f9dcbcc | 2010-02-23 09:19:49 +0000 | [diff] [blame] | 959 | struct dev_mc_list *mcptr; |
Ben Dooks | 3ba81f3 | 2009-07-16 05:24:08 +0000 | [diff] [blame] | 960 | u32 crc; |
Ben Dooks | 3ba81f3 | 2009-07-16 05:24:08 +0000 | [diff] [blame] | 961 | |
| 962 | /* accept some multicast */ |
| 963 | |
Jiri Pirko | f9dcbcc | 2010-02-23 09:19:49 +0000 | [diff] [blame] | 964 | netdev_for_each_mc_addr(mcptr, dev) { |
Ben Dooks | 3ba81f3 | 2009-07-16 05:24:08 +0000 | [diff] [blame] | 965 | crc = ether_crc(ETH_ALEN, mcptr->dmi_addr); |
| 966 | crc >>= (32 - 6); /* get top six bits */ |
| 967 | |
| 968 | rxctrl.mchash[crc >> 4] |= (1 << (crc & 0xf)); |
Ben Dooks | 3ba81f3 | 2009-07-16 05:24:08 +0000 | [diff] [blame] | 969 | } |
| 970 | |
Ben Dooks | b6a71bf | 2009-10-19 23:49:05 +0000 | [diff] [blame] | 971 | rxctrl.rxcr1 = RXCR1_RXME | RXCR1_RXPAFMA; |
Ben Dooks | 3ba81f3 | 2009-07-16 05:24:08 +0000 | [diff] [blame] | 972 | } else { |
| 973 | /* just accept broadcast / unicast */ |
| 974 | rxctrl.rxcr1 = RXCR1_RXPAFMA; |
| 975 | } |
| 976 | |
| 977 | rxctrl.rxcr1 |= (RXCR1_RXUE | /* unicast enable */ |
| 978 | RXCR1_RXBE | /* broadcast enable */ |
| 979 | RXCR1_RXE | /* RX process enable */ |
| 980 | RXCR1_RXFCE); /* enable flow control */ |
| 981 | |
| 982 | rxctrl.rxcr2 |= RXCR2_SRDBL_FRAME; |
| 983 | |
| 984 | /* schedule work to do the actual set of the data if needed */ |
| 985 | |
| 986 | spin_lock(&ks->statelock); |
| 987 | |
| 988 | if (memcmp(&rxctrl, &ks->rxctrl, sizeof(rxctrl)) != 0) { |
| 989 | memcpy(&ks->rxctrl, &rxctrl, sizeof(ks->rxctrl)); |
| 990 | schedule_work(&ks->rxctrl_work); |
| 991 | } |
| 992 | |
| 993 | spin_unlock(&ks->statelock); |
| 994 | } |
| 995 | |
| 996 | static int ks8851_set_mac_address(struct net_device *dev, void *addr) |
| 997 | { |
| 998 | struct sockaddr *sa = addr; |
| 999 | |
| 1000 | if (netif_running(dev)) |
| 1001 | return -EBUSY; |
| 1002 | |
| 1003 | if (!is_valid_ether_addr(sa->sa_data)) |
| 1004 | return -EADDRNOTAVAIL; |
| 1005 | |
| 1006 | memcpy(dev->dev_addr, sa->sa_data, ETH_ALEN); |
| 1007 | return ks8851_write_mac_addr(dev); |
| 1008 | } |
| 1009 | |
| 1010 | static int ks8851_net_ioctl(struct net_device *dev, struct ifreq *req, int cmd) |
| 1011 | { |
| 1012 | struct ks8851_net *ks = netdev_priv(dev); |
| 1013 | |
| 1014 | if (!netif_running(dev)) |
| 1015 | return -EINVAL; |
| 1016 | |
| 1017 | return generic_mii_ioctl(&ks->mii, if_mii(req), cmd, NULL); |
| 1018 | } |
| 1019 | |
| 1020 | static const struct net_device_ops ks8851_netdev_ops = { |
| 1021 | .ndo_open = ks8851_net_open, |
| 1022 | .ndo_stop = ks8851_net_stop, |
| 1023 | .ndo_do_ioctl = ks8851_net_ioctl, |
| 1024 | .ndo_start_xmit = ks8851_start_xmit, |
| 1025 | .ndo_set_mac_address = ks8851_set_mac_address, |
| 1026 | .ndo_set_rx_mode = ks8851_set_rx_mode, |
| 1027 | .ndo_change_mtu = eth_change_mtu, |
| 1028 | .ndo_validate_addr = eth_validate_addr, |
| 1029 | }; |
| 1030 | |
| 1031 | /* ethtool support */ |
| 1032 | |
| 1033 | static void ks8851_get_drvinfo(struct net_device *dev, |
| 1034 | struct ethtool_drvinfo *di) |
| 1035 | { |
| 1036 | strlcpy(di->driver, "KS8851", sizeof(di->driver)); |
| 1037 | strlcpy(di->version, "1.00", sizeof(di->version)); |
| 1038 | strlcpy(di->bus_info, dev_name(dev->dev.parent), sizeof(di->bus_info)); |
| 1039 | } |
| 1040 | |
| 1041 | static u32 ks8851_get_msglevel(struct net_device *dev) |
| 1042 | { |
| 1043 | struct ks8851_net *ks = netdev_priv(dev); |
| 1044 | return ks->msg_enable; |
| 1045 | } |
| 1046 | |
| 1047 | static void ks8851_set_msglevel(struct net_device *dev, u32 to) |
| 1048 | { |
| 1049 | struct ks8851_net *ks = netdev_priv(dev); |
| 1050 | ks->msg_enable = to; |
| 1051 | } |
| 1052 | |
| 1053 | static int ks8851_get_settings(struct net_device *dev, struct ethtool_cmd *cmd) |
| 1054 | { |
| 1055 | struct ks8851_net *ks = netdev_priv(dev); |
| 1056 | return mii_ethtool_gset(&ks->mii, cmd); |
| 1057 | } |
| 1058 | |
| 1059 | static int ks8851_set_settings(struct net_device *dev, struct ethtool_cmd *cmd) |
| 1060 | { |
| 1061 | struct ks8851_net *ks = netdev_priv(dev); |
| 1062 | return mii_ethtool_sset(&ks->mii, cmd); |
| 1063 | } |
| 1064 | |
| 1065 | static u32 ks8851_get_link(struct net_device *dev) |
| 1066 | { |
| 1067 | struct ks8851_net *ks = netdev_priv(dev); |
| 1068 | return mii_link_ok(&ks->mii); |
| 1069 | } |
| 1070 | |
| 1071 | static int ks8851_nway_reset(struct net_device *dev) |
| 1072 | { |
| 1073 | struct ks8851_net *ks = netdev_priv(dev); |
| 1074 | return mii_nway_restart(&ks->mii); |
| 1075 | } |
| 1076 | |
| 1077 | static const struct ethtool_ops ks8851_ethtool_ops = { |
| 1078 | .get_drvinfo = ks8851_get_drvinfo, |
| 1079 | .get_msglevel = ks8851_get_msglevel, |
| 1080 | .set_msglevel = ks8851_set_msglevel, |
| 1081 | .get_settings = ks8851_get_settings, |
| 1082 | .set_settings = ks8851_set_settings, |
| 1083 | .get_link = ks8851_get_link, |
| 1084 | .nway_reset = ks8851_nway_reset, |
| 1085 | }; |
| 1086 | |
| 1087 | /* MII interface controls */ |
| 1088 | |
| 1089 | /** |
| 1090 | * ks8851_phy_reg - convert MII register into a KS8851 register |
| 1091 | * @reg: MII register number. |
| 1092 | * |
| 1093 | * Return the KS8851 register number for the corresponding MII PHY register |
| 1094 | * if possible. Return zero if the MII register has no direct mapping to the |
| 1095 | * KS8851 register set. |
| 1096 | */ |
| 1097 | static int ks8851_phy_reg(int reg) |
| 1098 | { |
| 1099 | switch (reg) { |
| 1100 | case MII_BMCR: |
| 1101 | return KS_P1MBCR; |
| 1102 | case MII_BMSR: |
| 1103 | return KS_P1MBSR; |
| 1104 | case MII_PHYSID1: |
| 1105 | return KS_PHY1ILR; |
| 1106 | case MII_PHYSID2: |
| 1107 | return KS_PHY1IHR; |
| 1108 | case MII_ADVERTISE: |
| 1109 | return KS_P1ANAR; |
| 1110 | case MII_LPA: |
| 1111 | return KS_P1ANLPR; |
| 1112 | } |
| 1113 | |
| 1114 | return 0x0; |
| 1115 | } |
| 1116 | |
| 1117 | /** |
| 1118 | * ks8851_phy_read - MII interface PHY register read. |
| 1119 | * @dev: The network device the PHY is on. |
| 1120 | * @phy_addr: Address of PHY (ignored as we only have one) |
| 1121 | * @reg: The register to read. |
| 1122 | * |
| 1123 | * This call reads data from the PHY register specified in @reg. Since the |
| 1124 | * device does not support all the MII registers, the non-existant values |
| 1125 | * are always returned as zero. |
| 1126 | * |
| 1127 | * We return zero for unsupported registers as the MII code does not check |
| 1128 | * the value returned for any error status, and simply returns it to the |
| 1129 | * caller. The mii-tool that the driver was tested with takes any -ve error |
| 1130 | * as real PHY capabilities, thus displaying incorrect data to the user. |
| 1131 | */ |
| 1132 | static int ks8851_phy_read(struct net_device *dev, int phy_addr, int reg) |
| 1133 | { |
| 1134 | struct ks8851_net *ks = netdev_priv(dev); |
| 1135 | int ksreg; |
| 1136 | int result; |
| 1137 | |
| 1138 | ksreg = ks8851_phy_reg(reg); |
| 1139 | if (!ksreg) |
| 1140 | return 0x0; /* no error return allowed, so use zero */ |
| 1141 | |
| 1142 | mutex_lock(&ks->lock); |
| 1143 | result = ks8851_rdreg16(ks, ksreg); |
| 1144 | mutex_unlock(&ks->lock); |
| 1145 | |
| 1146 | return result; |
| 1147 | } |
| 1148 | |
| 1149 | static void ks8851_phy_write(struct net_device *dev, |
| 1150 | int phy, int reg, int value) |
| 1151 | { |
| 1152 | struct ks8851_net *ks = netdev_priv(dev); |
| 1153 | int ksreg; |
| 1154 | |
| 1155 | ksreg = ks8851_phy_reg(reg); |
| 1156 | if (ksreg) { |
| 1157 | mutex_lock(&ks->lock); |
| 1158 | ks8851_wrreg16(ks, ksreg, value); |
| 1159 | mutex_unlock(&ks->lock); |
| 1160 | } |
| 1161 | } |
| 1162 | |
| 1163 | /** |
| 1164 | * ks8851_read_selftest - read the selftest memory info. |
| 1165 | * @ks: The device state |
| 1166 | * |
| 1167 | * Read and check the TX/RX memory selftest information. |
| 1168 | */ |
| 1169 | static int ks8851_read_selftest(struct ks8851_net *ks) |
| 1170 | { |
| 1171 | unsigned both_done = MBIR_TXMBF | MBIR_RXMBF; |
| 1172 | int ret = 0; |
| 1173 | unsigned rd; |
| 1174 | |
| 1175 | rd = ks8851_rdreg16(ks, KS_MBIR); |
| 1176 | |
| 1177 | if ((rd & both_done) != both_done) { |
Joe Perches | 0dc7d2b | 2010-02-27 14:43:51 +0000 | [diff] [blame] | 1178 | netdev_warn(ks->netdev, "Memory selftest not finished\n"); |
Ben Dooks | 3ba81f3 | 2009-07-16 05:24:08 +0000 | [diff] [blame] | 1179 | return 0; |
| 1180 | } |
| 1181 | |
| 1182 | if (rd & MBIR_TXMBFA) { |
Joe Perches | 0dc7d2b | 2010-02-27 14:43:51 +0000 | [diff] [blame] | 1183 | netdev_err(ks->netdev, "TX memory selftest fail\n"); |
Ben Dooks | 3ba81f3 | 2009-07-16 05:24:08 +0000 | [diff] [blame] | 1184 | ret |= 1; |
| 1185 | } |
| 1186 | |
| 1187 | if (rd & MBIR_RXMBFA) { |
Joe Perches | 0dc7d2b | 2010-02-27 14:43:51 +0000 | [diff] [blame] | 1188 | netdev_err(ks->netdev, "RX memory selftest fail\n"); |
Ben Dooks | 3ba81f3 | 2009-07-16 05:24:08 +0000 | [diff] [blame] | 1189 | ret |= 2; |
| 1190 | } |
| 1191 | |
| 1192 | return 0; |
| 1193 | } |
| 1194 | |
| 1195 | /* driver bus management functions */ |
| 1196 | |
| 1197 | static int __devinit ks8851_probe(struct spi_device *spi) |
| 1198 | { |
| 1199 | struct net_device *ndev; |
| 1200 | struct ks8851_net *ks; |
| 1201 | int ret; |
| 1202 | |
| 1203 | ndev = alloc_etherdev(sizeof(struct ks8851_net)); |
| 1204 | if (!ndev) { |
| 1205 | dev_err(&spi->dev, "failed to alloc ethernet device\n"); |
| 1206 | return -ENOMEM; |
| 1207 | } |
| 1208 | |
| 1209 | spi->bits_per_word = 8; |
| 1210 | |
| 1211 | ks = netdev_priv(ndev); |
| 1212 | |
| 1213 | ks->netdev = ndev; |
| 1214 | ks->spidev = spi; |
| 1215 | ks->tx_space = 6144; |
| 1216 | |
| 1217 | mutex_init(&ks->lock); |
| 1218 | spin_lock_init(&ks->statelock); |
| 1219 | |
| 1220 | INIT_WORK(&ks->tx_work, ks8851_tx_work); |
| 1221 | INIT_WORK(&ks->irq_work, ks8851_irq_work); |
| 1222 | INIT_WORK(&ks->rxctrl_work, ks8851_rxctrl_work); |
| 1223 | |
| 1224 | /* initialise pre-made spi transfer messages */ |
| 1225 | |
| 1226 | spi_message_init(&ks->spi_msg1); |
| 1227 | spi_message_add_tail(&ks->spi_xfer1, &ks->spi_msg1); |
| 1228 | |
| 1229 | spi_message_init(&ks->spi_msg2); |
| 1230 | spi_message_add_tail(&ks->spi_xfer2[0], &ks->spi_msg2); |
| 1231 | spi_message_add_tail(&ks->spi_xfer2[1], &ks->spi_msg2); |
| 1232 | |
| 1233 | /* setup mii state */ |
| 1234 | ks->mii.dev = ndev; |
| 1235 | ks->mii.phy_id = 1, |
| 1236 | ks->mii.phy_id_mask = 1; |
| 1237 | ks->mii.reg_num_mask = 0xf; |
| 1238 | ks->mii.mdio_read = ks8851_phy_read; |
| 1239 | ks->mii.mdio_write = ks8851_phy_write; |
| 1240 | |
| 1241 | dev_info(&spi->dev, "message enable is %d\n", msg_enable); |
| 1242 | |
| 1243 | /* set the default message enable */ |
| 1244 | ks->msg_enable = netif_msg_init(msg_enable, (NETIF_MSG_DRV | |
| 1245 | NETIF_MSG_PROBE | |
| 1246 | NETIF_MSG_LINK)); |
| 1247 | |
| 1248 | skb_queue_head_init(&ks->txq); |
| 1249 | |
| 1250 | SET_ETHTOOL_OPS(ndev, &ks8851_ethtool_ops); |
| 1251 | SET_NETDEV_DEV(ndev, &spi->dev); |
| 1252 | |
| 1253 | dev_set_drvdata(&spi->dev, ks); |
| 1254 | |
| 1255 | ndev->if_port = IF_PORT_100BASET; |
| 1256 | ndev->netdev_ops = &ks8851_netdev_ops; |
| 1257 | ndev->irq = spi->irq; |
| 1258 | |
Ben Dooks | 57dada6 | 2009-10-19 23:49:03 +0000 | [diff] [blame] | 1259 | /* issue a global soft reset to reset the device. */ |
| 1260 | ks8851_soft_reset(ks, GRR_GSR); |
| 1261 | |
Ben Dooks | 3ba81f3 | 2009-07-16 05:24:08 +0000 | [diff] [blame] | 1262 | /* simple check for a valid chip being connected to the bus */ |
| 1263 | |
| 1264 | if ((ks8851_rdreg16(ks, KS_CIDER) & ~CIDER_REV_MASK) != CIDER_ID) { |
| 1265 | dev_err(&spi->dev, "failed to read device ID\n"); |
| 1266 | ret = -ENODEV; |
| 1267 | goto err_id; |
| 1268 | } |
| 1269 | |
| 1270 | ks8851_read_selftest(ks); |
| 1271 | ks8851_init_mac(ks); |
| 1272 | |
| 1273 | ret = request_irq(spi->irq, ks8851_irq, IRQF_TRIGGER_LOW, |
| 1274 | ndev->name, ks); |
| 1275 | if (ret < 0) { |
| 1276 | dev_err(&spi->dev, "failed to get irq\n"); |
| 1277 | goto err_irq; |
| 1278 | } |
| 1279 | |
| 1280 | ret = register_netdev(ndev); |
| 1281 | if (ret) { |
| 1282 | dev_err(&spi->dev, "failed to register network device\n"); |
| 1283 | goto err_netdev; |
| 1284 | } |
| 1285 | |
Joe Perches | 0dc7d2b | 2010-02-27 14:43:51 +0000 | [diff] [blame] | 1286 | netdev_info(ndev, "revision %d, MAC %pM, IRQ %d\n", |
| 1287 | CIDER_REV_GET(ks8851_rdreg16(ks, KS_CIDER)), |
| 1288 | ndev->dev_addr, ndev->irq); |
Ben Dooks | 3ba81f3 | 2009-07-16 05:24:08 +0000 | [diff] [blame] | 1289 | |
| 1290 | return 0; |
| 1291 | |
| 1292 | |
| 1293 | err_netdev: |
| 1294 | free_irq(ndev->irq, ndev); |
| 1295 | |
| 1296 | err_id: |
| 1297 | err_irq: |
| 1298 | free_netdev(ndev); |
| 1299 | return ret; |
| 1300 | } |
| 1301 | |
| 1302 | static int __devexit ks8851_remove(struct spi_device *spi) |
| 1303 | { |
| 1304 | struct ks8851_net *priv = dev_get_drvdata(&spi->dev); |
| 1305 | |
| 1306 | if (netif_msg_drv(priv)) |
Joe Perches | 0dc7d2b | 2010-02-27 14:43:51 +0000 | [diff] [blame] | 1307 | dev_info(&spi->dev, "remove\n"); |
Ben Dooks | 3ba81f3 | 2009-07-16 05:24:08 +0000 | [diff] [blame] | 1308 | |
| 1309 | unregister_netdev(priv->netdev); |
| 1310 | free_irq(spi->irq, priv); |
| 1311 | free_netdev(priv->netdev); |
| 1312 | |
| 1313 | return 0; |
| 1314 | } |
| 1315 | |
| 1316 | static struct spi_driver ks8851_driver = { |
| 1317 | .driver = { |
| 1318 | .name = "ks8851", |
| 1319 | .owner = THIS_MODULE, |
| 1320 | }, |
| 1321 | .probe = ks8851_probe, |
| 1322 | .remove = __devexit_p(ks8851_remove), |
| 1323 | }; |
| 1324 | |
| 1325 | static int __init ks8851_init(void) |
| 1326 | { |
| 1327 | return spi_register_driver(&ks8851_driver); |
| 1328 | } |
| 1329 | |
| 1330 | static void __exit ks8851_exit(void) |
| 1331 | { |
| 1332 | spi_unregister_driver(&ks8851_driver); |
| 1333 | } |
| 1334 | |
| 1335 | module_init(ks8851_init); |
| 1336 | module_exit(ks8851_exit); |
| 1337 | |
| 1338 | MODULE_DESCRIPTION("KS8851 Network driver"); |
| 1339 | MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>"); |
| 1340 | MODULE_LICENSE("GPL"); |
| 1341 | |
| 1342 | module_param_named(message, msg_enable, int, 0); |
| 1343 | MODULE_PARM_DESC(message, "Message verbosity level (0=none, 31=all)"); |
Anton Vorontsov | e0626e3 | 2009-09-22 16:46:08 -0700 | [diff] [blame] | 1344 | MODULE_ALIAS("spi:ks8851"); |