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