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