| 1 |
// SPDX-License-Identifier: GPL-2.0-only |
| 2 |
/* drivers/net/ethernet/micrel/ks8851.c |
| 3 |
* |
| 4 |
* Copyright 2009 Simtec Electronics |
| 5 |
* http://www.simtec.co.uk/ |
| 6 |
* Ben Dooks <[email protected]> |
| 7 |
*/ |
| 8 |
|
| 9 |
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 10 |
|
| 11 |
#include <linux/interrupt.h> |
| 12 |
#include <linux/module.h> |
| 13 |
#include <linux/kernel.h> |
| 14 |
#include <linux/netdevice.h> |
| 15 |
#include <linux/etherdevice.h> |
| 16 |
#include <linux/ethtool.h> |
| 17 |
#include <linux/cache.h> |
| 18 |
#include <linux/crc32.h> |
| 19 |
#include <linux/mii.h> |
| 20 |
#include <linux/gpio/consumer.h> |
| 21 |
#include <linux/regulator/consumer.h> |
| 22 |
|
| 23 |
#include <linux/of_mdio.h> |
| 24 |
#include <linux/of_net.h> |
| 25 |
|
| 26 |
#include "ks8851.h" |
| 27 |
|
| 28 |
/** |
| 29 |
* ks8851_lock - register access lock |
| 30 |
* @ks: The chip state |
| 31 |
* |
| 32 |
* Claim chip register access lock |
| 33 |
*/ |
| 34 |
static void ks8851_lock(struct ks8851_net *ks) |
| 35 |
{ |
| 36 |
ks->lock(ks); |
| 37 |
} |
| 38 |
|
| 39 |
/** |
| 40 |
* ks8851_unlock - register access unlock |
| 41 |
* @ks: The chip state |
| 42 |
* |
| 43 |
* Release chip register access lock |
| 44 |
*/ |
| 45 |
static void ks8851_unlock(struct ks8851_net *ks) |
| 46 |
{ |
| 47 |
ks->unlock(ks); |
| 48 |
} |
| 49 |
|
| 50 |
/** |
| 51 |
* ks8851_wrreg16 - write 16bit register value to chip |
| 52 |
* @ks: The chip state |
| 53 |
* @reg: The register address |
| 54 |
* @val: The value to write |
| 55 |
* |
| 56 |
* Issue a write to put the value @val into the register specified in @reg. |
| 57 |
*/ |
| 58 |
static void ks8851_wrreg16(struct ks8851_net *ks, unsigned int reg, |
| 59 |
unsigned int val) |
| 60 |
{ |
| 61 |
ks->wrreg16(ks, reg, val); |
| 62 |
} |
| 63 |
|
| 64 |
/** |
| 65 |
* ks8851_rdreg16 - read 16 bit register from device |
| 66 |
* @ks: The chip information |
| 67 |
* @reg: The register address |
| 68 |
* |
| 69 |
* Read a 16bit register from the chip, returning the result |
| 70 |
*/ |
| 71 |
static unsigned int ks8851_rdreg16(struct ks8851_net *ks, |
| 72 |
unsigned int reg) |
| 73 |
{ |
| 74 |
return ks->rdreg16(ks, reg); |
| 75 |
} |
| 76 |
|
| 77 |
/** |
| 78 |
* ks8851_soft_reset - issue one of the soft reset to the device |
| 79 |
* @ks: The device state. |
| 80 |
* @op: The bit(s) to set in the GRR |
| 81 |
* |
| 82 |
* Issue the relevant soft-reset command to the device's GRR register |
| 83 |
* specified by @op. |
| 84 |
* |
| 85 |
* Note, the delays are in there as a caution to ensure that the reset |
| 86 |
* has time to take effect and then complete. Since the datasheet does |
| 87 |
* not currently specify the exact sequence, we have chosen something |
| 88 |
* that seems to work with our device. |
| 89 |
*/ |
| 90 |
static void ks8851_soft_reset(struct ks8851_net *ks, unsigned op) |
| 91 |
{ |
| 92 |
ks8851_wrreg16(ks, KS_GRR, op); |
| 93 |
mdelay(1); /* wait a short time to effect reset */ |
| 94 |
ks8851_wrreg16(ks, KS_GRR, 0); |
| 95 |
mdelay(1); /* wait for condition to clear */ |
| 96 |
} |
| 97 |
|
| 98 |
/** |
| 99 |
* ks8851_set_powermode - set power mode of the device |
| 100 |
* @ks: The device state |
| 101 |
* @pwrmode: The power mode value to write to KS_PMECR. |
| 102 |
* |
| 103 |
* Change the power mode of the chip. |
| 104 |
*/ |
| 105 |
static void ks8851_set_powermode(struct ks8851_net *ks, unsigned pwrmode) |
| 106 |
{ |
| 107 |
unsigned pmecr; |
| 108 |
|
| 109 |
netif_dbg(ks, hw, ks->netdev, "setting power mode %d\n", pwrmode); |
| 110 |
|
| 111 |
pmecr = ks8851_rdreg16(ks, KS_PMECR); |
| 112 |
pmecr &= ~PMECR_PM_MASK; |
| 113 |
pmecr |= pwrmode; |
| 114 |
|
| 115 |
ks8851_wrreg16(ks, KS_PMECR, pmecr); |
| 116 |
} |
| 117 |
|
| 118 |
/** |
| 119 |
* ks8851_write_mac_addr - write mac address to device registers |
| 120 |
* @dev: The network device |
| 121 |
* |
| 122 |
* Update the KS8851 MAC address registers from the address in @dev. |
| 123 |
* |
| 124 |
* This call assumes that the chip is not running, so there is no need to |
| 125 |
* shutdown the RXQ process whilst setting this. |
| 126 |
*/ |
| 127 |
static int ks8851_write_mac_addr(struct net_device *dev) |
| 128 |
{ |
| 129 |
struct ks8851_net *ks = netdev_priv(dev); |
| 130 |
u16 val; |
| 131 |
int i; |
| 132 |
|
| 133 |
ks8851_lock(ks); |
| 134 |
|
| 135 |
/* |
| 136 |
* Wake up chip in case it was powered off when stopped; otherwise, |
| 137 |
* the first write to the MAC address does not take effect. |
| 138 |
*/ |
| 139 |
ks8851_set_powermode(ks, PMECR_PM_NORMAL); |
| 140 |
|
| 141 |
for (i = 0; i < ETH_ALEN; i += 2) { |
| 142 |
val = (dev->dev_addr[i] << 8) | dev->dev_addr[i + 1]; |
| 143 |
ks8851_wrreg16(ks, KS_MAR(i), val); |
| 144 |
} |
| 145 |
|
| 146 |
if (!netif_running(dev)) |
| 147 |
ks8851_set_powermode(ks, PMECR_PM_SOFTDOWN); |
| 148 |
|
| 149 |
ks8851_unlock(ks); |
| 150 |
|
| 151 |
return 0; |
| 152 |
} |
| 153 |
|
| 154 |
/** |
| 155 |
* ks8851_read_mac_addr - read mac address from device registers |
| 156 |
* @dev: The network device |
| 157 |
* |
| 158 |
* Update our copy of the KS8851 MAC address from the registers of @dev. |
| 159 |
*/ |
| 160 |
static void ks8851_read_mac_addr(struct net_device *dev) |
| 161 |
{ |
| 162 |
struct ks8851_net *ks = netdev_priv(dev); |
| 163 |
u8 addr[ETH_ALEN]; |
| 164 |
u16 reg; |
| 165 |
int i; |
| 166 |
|
| 167 |
ks8851_lock(ks); |
| 168 |
|
| 169 |
for (i = 0; i < ETH_ALEN; i += 2) { |
| 170 |
reg = ks8851_rdreg16(ks, KS_MAR(i)); |
| 171 |
addr[i] = reg >> 8; |
| 172 |
addr[i + 1] = reg & 0xff; |
| 173 |
} |
| 174 |
eth_hw_addr_set(dev, addr); |
| 175 |
|
| 176 |
ks8851_unlock(ks); |
| 177 |
} |
| 178 |
|
| 179 |
/** |
| 180 |
* ks8851_init_mac - initialise the mac address |
| 181 |
* @ks: The device structure |
| 182 |
* @np: The device node pointer |
| 183 |
* |
| 184 |
* Get or create the initial mac address for the device and then set that |
| 185 |
* into the station address register. A mac address supplied in the device |
| 186 |
* tree takes precedence. Otherwise, if there is an EEPROM present, then |
| 187 |
* we try that. If no valid mac address is found we use eth_random_addr() |
| 188 |
* to create a new one. |
| 189 |
*/ |
| 190 |
static void ks8851_init_mac(struct ks8851_net *ks, struct device_node *np) |
| 191 |
{ |
| 192 |
struct net_device *dev = ks->netdev; |
| 193 |
int ret; |
| 194 |
|
| 195 |
ret = of_get_ethdev_address(np, dev); |
| 196 |
if (!ret) { |
| 197 |
ks8851_write_mac_addr(dev); |
| 198 |
return; |
| 199 |
} |
| 200 |
|
| 201 |
if (ks->rc_ccr & CCR_EEPROM) { |
| 202 |
ks8851_read_mac_addr(dev); |
| 203 |
if (is_valid_ether_addr(dev->dev_addr)) |
| 204 |
return; |
| 205 |
|
| 206 |
netdev_err(ks->netdev, "invalid mac address read %pM\n", |
| 207 |
dev->dev_addr); |
| 208 |
} |
| 209 |
|
| 210 |
eth_hw_addr_random(dev); |
| 211 |
ks8851_write_mac_addr(dev); |
| 212 |
} |
| 213 |
|
| 214 |
/** |
| 215 |
* ks8851_rx_pkts - receive packets from the host |
| 216 |
* @ks: The device information. |
| 217 |
* @rxq: Queue of packets received in this function. |
| 218 |
* |
| 219 |
* This is called from the IRQ work queue when the system detects that there |
| 220 |
* are packets in the receive queue. Find out how many packets there are and |
| 221 |
* read them from the FIFO. |
| 222 |
*/ |
| 223 |
static void ks8851_rx_pkts(struct ks8851_net *ks, struct sk_buff_head *rxq) |
| 224 |
{ |
| 225 |
struct sk_buff *skb; |
| 226 |
unsigned rxfc; |
| 227 |
unsigned rxlen; |
| 228 |
unsigned rxstat; |
| 229 |
u8 *rxpkt; |
| 230 |
|
| 231 |
rxfc = (ks8851_rdreg16(ks, KS_RXFCTR) >> 8) & 0xff; |
| 232 |
|
| 233 |
netif_dbg(ks, rx_status, ks->netdev, |
| 234 |
"%s: %d packets\n", __func__, rxfc); |
| 235 |
|
| 236 |
/* Currently we're issuing a read per packet, but we could possibly |
| 237 |
* improve the code by issuing a single read, getting the receive |
| 238 |
* header, allocating the packet and then reading the packet data |
| 239 |
* out in one go. |
| 240 |
* |
| 241 |
* This form of operation would require us to hold the SPI bus' |
| 242 |
* chipselect low during the entie transaction to avoid any |
| 243 |
* reset to the data stream coming from the chip. |
| 244 |
*/ |
| 245 |
|
| 246 |
for (; rxfc != 0; rxfc--) { |
| 247 |
rxstat = ks8851_rdreg16(ks, KS_RXFHSR); |
| 248 |
rxlen = ks8851_rdreg16(ks, KS_RXFHBCR) & RXFHBCR_CNT_MASK; |
| 249 |
|
| 250 |
netif_dbg(ks, rx_status, ks->netdev, |
| 251 |
"rx: stat 0x%04x, len 0x%04x\n", rxstat, rxlen); |
| 252 |
|
| 253 |
/* the length of the packet includes the 32bit CRC */ |
| 254 |
|
| 255 |
/* set dma read address */ |
| 256 |
ks8851_wrreg16(ks, KS_RXFDPR, RXFDPR_RXFPAI | 0x00); |
| 257 |
|
| 258 |
/* start DMA access */ |
| 259 |
ks8851_wrreg16(ks, KS_RXQCR, ks->rc_rxqcr | RXQCR_SDA); |
| 260 |
|
| 261 |
if (rxlen > 4) { |
| 262 |
unsigned int rxalign; |
| 263 |
|
| 264 |
rxlen -= 4; |
| 265 |
rxalign = ALIGN(rxlen, 4); |
| 266 |
skb = netdev_alloc_skb_ip_align(ks->netdev, rxalign); |
| 267 |
if (skb) { |
| 268 |
|
| 269 |
/* 4 bytes of status header + 4 bytes of |
| 270 |
* garbage: we put them before ethernet |
| 271 |
* header, so that they are copied, |
| 272 |
* but ignored. |
| 273 |
*/ |
| 274 |
|
| 275 |
rxpkt = skb_put(skb, rxlen) - 8; |
| 276 |
|
| 277 |
ks->rdfifo(ks, rxpkt, rxalign + 8); |
| 278 |
|
| 279 |
netif_dbg(ks, pktdata, ks->netdev, |
| 280 |
"pkt %12ph\n", &rxpkt[4]); |
| 281 |
|
| 282 |
skb->protocol = eth_type_trans(skb, ks->netdev); |
| 283 |
__skb_queue_tail(rxq, skb); |
| 284 |
|
| 285 |
ks->netdev->stats.rx_packets++; |
| 286 |
ks->netdev->stats.rx_bytes += rxlen; |
| 287 |
} |
| 288 |
} |
| 289 |
|
| 290 |
/* end DMA access and dequeue packet */ |
| 291 |
ks8851_wrreg16(ks, KS_RXQCR, ks->rc_rxqcr | RXQCR_RRXEF); |
| 292 |
} |
| 293 |
} |
| 294 |
|
| 295 |
/** |
| 296 |
* ks8851_irq - IRQ handler for dealing with interrupt requests |
| 297 |
* @irq: IRQ number |
| 298 |
* @_ks: cookie |
| 299 |
* |
| 300 |
* This handler is invoked when the IRQ line asserts to find out what happened. |
| 301 |
* As we cannot allow ourselves to sleep in HARDIRQ context, this handler runs |
| 302 |
* in thread context. |
| 303 |
* |
| 304 |
* Read the interrupt status, work out what needs to be done and then clear |
| 305 |
* any of the interrupts that are not needed. |
| 306 |
*/ |
| 307 |
static irqreturn_t ks8851_irq(int irq, void *_ks) |
| 308 |
{ |
| 309 |
struct ks8851_net *ks = _ks; |
| 310 |
struct sk_buff_head rxq; |
| 311 |
unsigned int status; |
| 312 |
struct sk_buff *skb; |
| 313 |
|
| 314 |
ks8851_lock(ks); |
| 315 |
|
| 316 |
status = ks8851_rdreg16(ks, KS_ISR); |
| 317 |
ks8851_wrreg16(ks, KS_ISR, status); |
| 318 |
|
| 319 |
netif_dbg(ks, intr, ks->netdev, |
| 320 |
"%s: status 0x%04x\n", __func__, status); |
| 321 |
|
| 322 |
if (status & IRQ_LDI) { |
| 323 |
u16 pmecr = ks8851_rdreg16(ks, KS_PMECR); |
| 324 |
pmecr &= ~PMECR_WKEVT_MASK; |
| 325 |
ks8851_wrreg16(ks, KS_PMECR, pmecr | PMECR_WKEVT_LINK); |
| 326 |
} |
| 327 |
|
| 328 |
if (status & IRQ_TXI) { |
| 329 |
unsigned short tx_space = ks8851_rdreg16(ks, KS_TXMIR); |
| 330 |
|
| 331 |
netif_dbg(ks, intr, ks->netdev, |
| 332 |
"%s: txspace %d\n", __func__, tx_space); |
| 333 |
|
| 334 |
spin_lock_bh(&ks->statelock); |
| 335 |
ks->tx_space = tx_space; |
| 336 |
if (netif_queue_stopped(ks->netdev)) |
| 337 |
netif_wake_queue(ks->netdev); |
| 338 |
spin_unlock_bh(&ks->statelock); |
| 339 |
} |
| 340 |
|
| 341 |
if (status & IRQ_SPIBEI) { |
| 342 |
netdev_err(ks->netdev, "%s: spi bus error\n", __func__); |
| 343 |
} |
| 344 |
|
| 345 |
if (status & IRQ_RXI) { |
| 346 |
/* the datasheet says to disable the rx interrupt during |
| 347 |
* packet read-out, however we're masking the interrupt |
| 348 |
* from the device so do not bother masking just the RX |
| 349 |
* from the device. */ |
| 350 |
|
| 351 |
__skb_queue_head_init(&rxq); |
| 352 |
ks8851_rx_pkts(ks, &rxq); |
| 353 |
} |
| 354 |
|
| 355 |
/* if something stopped the rx process, probably due to wanting |
| 356 |
* to change the rx settings, then do something about restarting |
| 357 |
* it. */ |
| 358 |
if (status & IRQ_RXPSI) { |
| 359 |
struct ks8851_rxctrl *rxc = &ks->rxctrl; |
| 360 |
|
| 361 |
/* update the multicast hash table */ |
| 362 |
ks8851_wrreg16(ks, KS_MAHTR0, rxc->mchash[0]); |
| 363 |
ks8851_wrreg16(ks, KS_MAHTR1, rxc->mchash[1]); |
| 364 |
ks8851_wrreg16(ks, KS_MAHTR2, rxc->mchash[2]); |
| 365 |
ks8851_wrreg16(ks, KS_MAHTR3, rxc->mchash[3]); |
| 366 |
|
| 367 |
ks8851_wrreg16(ks, KS_RXCR2, rxc->rxcr2); |
| 368 |
ks8851_wrreg16(ks, KS_RXCR1, rxc->rxcr1); |
| 369 |
} |
| 370 |
|
| 371 |
ks8851_unlock(ks); |
| 372 |
|
| 373 |
if (status & IRQ_LCI) |
| 374 |
mii_check_link(&ks->mii); |
| 375 |
|
| 376 |
if (status & IRQ_RXI) { |
| 377 |
local_bh_disable(); |
| 378 |
while ((skb = __skb_dequeue(&rxq))) |
| 379 |
netif_rx(skb); |
| 380 |
local_bh_enable(); |
| 381 |
} |
| 382 |
|
| 383 |
return IRQ_HANDLED; |
| 384 |
} |
| 385 |
|
| 386 |
/** |
| 387 |
* ks8851_flush_tx_work - flush outstanding TX work |
| 388 |
* @ks: The device state |
| 389 |
*/ |
| 390 |
static void ks8851_flush_tx_work(struct ks8851_net *ks) |
| 391 |
{ |
| 392 |
if (ks->flush_tx_work) |
| 393 |
ks->flush_tx_work(ks); |
| 394 |
} |
| 395 |
|
| 396 |
/** |
| 397 |
* ks8851_net_open - open network device |
| 398 |
* @dev: The network device being opened. |
| 399 |
* |
| 400 |
* Called when the network device is marked active, such as a user executing |
| 401 |
* 'ifconfig up' on the device. |
| 402 |
*/ |
| 403 |
static int ks8851_net_open(struct net_device *dev) |
| 404 |
{ |
| 405 |
struct ks8851_net *ks = netdev_priv(dev); |
| 406 |
int ret; |
| 407 |
|
| 408 |
ret = request_threaded_irq(dev->irq, NULL, ks8851_irq, |
| 409 |
IRQF_TRIGGER_LOW | IRQF_ONESHOT, |
| 410 |
dev->name, ks); |
| 411 |
if (ret < 0) { |
| 412 |
netdev_err(dev, "failed to get irq\n"); |
| 413 |
return ret; |
| 414 |
} |
| 415 |
|
| 416 |
/* lock the card, even if we may not actually be doing anything |
| 417 |
* else at the moment */ |
| 418 |
ks8851_lock(ks); |
| 419 |
|
| 420 |
netif_dbg(ks, ifup, ks->netdev, "opening\n"); |
| 421 |
|
| 422 |
/* bring chip out of any power saving mode it was in */ |
| 423 |
ks8851_set_powermode(ks, PMECR_PM_NORMAL); |
| 424 |
|
| 425 |
/* issue a soft reset to the RX/TX QMU to put it into a known |
| 426 |
* state. */ |
| 427 |
ks8851_soft_reset(ks, GRR_QMU); |
| 428 |
|
| 429 |
/* setup transmission parameters */ |
| 430 |
|
| 431 |
ks8851_wrreg16(ks, KS_TXCR, (TXCR_TXE | /* enable transmit process */ |
| 432 |
TXCR_TXPE | /* pad to min length */ |
| 433 |
TXCR_TXCRC | /* add CRC */ |
| 434 |
TXCR_TXFCE)); /* enable flow control */ |
| 435 |
|
| 436 |
/* auto-increment tx data, reset tx pointer */ |
| 437 |
ks8851_wrreg16(ks, KS_TXFDPR, TXFDPR_TXFPAI); |
| 438 |
|
| 439 |
/* setup receiver control */ |
| 440 |
|
| 441 |
ks8851_wrreg16(ks, KS_RXCR1, (RXCR1_RXPAFMA | /* from mac filter */ |
| 442 |
RXCR1_RXFCE | /* enable flow control */ |
| 443 |
RXCR1_RXBE | /* broadcast enable */ |
| 444 |
RXCR1_RXUE | /* unicast enable */ |
| 445 |
RXCR1_RXE)); /* enable rx block */ |
| 446 |
|
| 447 |
/* transfer entire frames out in one go */ |
| 448 |
ks8851_wrreg16(ks, KS_RXCR2, RXCR2_SRDBL_FRAME); |
| 449 |
|
| 450 |
/* set receive counter timeouts */ |
| 451 |
ks8851_wrreg16(ks, KS_RXDTTR, 1000); /* 1ms after first frame to IRQ */ |
| 452 |
ks8851_wrreg16(ks, KS_RXDBCTR, 4096); /* >4Kbytes in buffer to IRQ */ |
| 453 |
ks8851_wrreg16(ks, KS_RXFCTR, 10); /* 10 frames to IRQ */ |
| 454 |
|
| 455 |
ks->rc_rxqcr = (RXQCR_RXFCTE | /* IRQ on frame count exceeded */ |
| 456 |
RXQCR_RXDBCTE | /* IRQ on byte count exceeded */ |
| 457 |
RXQCR_RXDTTE); /* IRQ on time exceeded */ |
| 458 |
|
| 459 |
ks8851_wrreg16(ks, KS_RXQCR, ks->rc_rxqcr); |
| 460 |
|
| 461 |
/* clear then enable interrupts */ |
| 462 |
ks8851_wrreg16(ks, KS_ISR, ks->rc_ier); |
| 463 |
ks8851_wrreg16(ks, KS_IER, ks->rc_ier); |
| 464 |
|
| 465 |
ks->queued_len = 0; |
| 466 |
ks->tx_space = ks8851_rdreg16(ks, KS_TXMIR); |
| 467 |
netif_start_queue(ks->netdev); |
| 468 |
|
| 469 |
netif_dbg(ks, ifup, ks->netdev, "network device up\n"); |
| 470 |
|
| 471 |
ks8851_unlock(ks); |
| 472 |
mii_check_link(&ks->mii); |
| 473 |
return 0; |
| 474 |
} |
| 475 |
|
| 476 |
/** |
| 477 |
* ks8851_net_stop - close network device |
| 478 |
* @dev: The device being closed. |
| 479 |
* |
| 480 |
* Called to close down a network device which has been active. Cancel any |
| 481 |
* work, shutdown the RX and TX process and then place the chip into a low |
| 482 |
* power state whilst it is not being used. |
| 483 |
*/ |
| 484 |
static int ks8851_net_stop(struct net_device *dev) |
| 485 |
{ |
| 486 |
struct ks8851_net *ks = netdev_priv(dev); |
| 487 |
|
| 488 |
netif_info(ks, ifdown, dev, "shutting down\n"); |
| 489 |
|
| 490 |
netif_stop_queue(dev); |
| 491 |
|
| 492 |
ks8851_lock(ks); |
| 493 |
/* turn off the IRQs and ack any outstanding */ |
| 494 |
ks8851_wrreg16(ks, KS_IER, 0x0000); |
| 495 |
ks8851_wrreg16(ks, KS_ISR, 0xffff); |
| 496 |
ks8851_unlock(ks); |
| 497 |
|
| 498 |
/* stop any outstanding work */ |
| 499 |
ks8851_flush_tx_work(ks); |
| 500 |
flush_work(&ks->rxctrl_work); |
| 501 |
|
| 502 |
ks8851_lock(ks); |
| 503 |
/* shutdown RX process */ |
| 504 |
ks8851_wrreg16(ks, KS_RXCR1, 0x0000); |
| 505 |
|
| 506 |
/* shutdown TX process */ |
| 507 |
ks8851_wrreg16(ks, KS_TXCR, 0x0000); |
| 508 |
|
| 509 |
/* set powermode to soft power down to save power */ |
| 510 |
ks8851_set_powermode(ks, PMECR_PM_SOFTDOWN); |
| 511 |
ks8851_unlock(ks); |
| 512 |
|
| 513 |
/* ensure any queued tx buffers are dumped */ |
| 514 |
while (!skb_queue_empty(&ks->txq)) { |
| 515 |
struct sk_buff *txb = skb_dequeue(&ks->txq); |
| 516 |
|
| 517 |
netif_dbg(ks, ifdown, ks->netdev, |
| 518 |
"%s: freeing txb %p\n", __func__, txb); |
| 519 |
|
| 520 |
dev_kfree_skb(txb); |
| 521 |
} |
| 522 |
|
| 523 |
free_irq(dev->irq, ks); |
| 524 |
|
| 525 |
return 0; |
| 526 |
} |
| 527 |
|
| 528 |
/** |
| 529 |
* ks8851_start_xmit - transmit packet |
| 530 |
* @skb: The buffer to transmit |
| 531 |
* @dev: The device used to transmit the packet. |
| 532 |
* |
| 533 |
* Called by the network layer to transmit the @skb. Queue the packet for |
| 534 |
* the device and schedule the necessary work to transmit the packet when |
| 535 |
* it is free. |
| 536 |
* |
| 537 |
* We do this to firstly avoid sleeping with the network device locked, |
| 538 |
* and secondly so we can round up more than one packet to transmit which |
| 539 |
* means we can try and avoid generating too many transmit done interrupts. |
| 540 |
*/ |
| 541 |
static netdev_tx_t ks8851_start_xmit(struct sk_buff *skb, |
| 542 |
struct net_device *dev) |
| 543 |
{ |
| 544 |
struct ks8851_net *ks = netdev_priv(dev); |
| 545 |
|
| 546 |
return ks->start_xmit(skb, dev); |
| 547 |
} |
| 548 |
|
| 549 |
/** |
| 550 |
* ks8851_rxctrl_work - work handler to change rx mode |
| 551 |
* @work: The work structure this belongs to. |
| 552 |
* |
| 553 |
* Lock the device and issue the necessary changes to the receive mode from |
| 554 |
* the network device layer. This is done so that we can do this without |
| 555 |
* having to sleep whilst holding the network device lock. |
| 556 |
* |
| 557 |
* Since the recommendation from Micrel is that the RXQ is shutdown whilst the |
| 558 |
* receive parameters are programmed, we issue a write to disable the RXQ and |
| 559 |
* then wait for the interrupt handler to be triggered once the RXQ shutdown is |
| 560 |
* complete. The interrupt handler then writes the new values into the chip. |
| 561 |
*/ |
| 562 |
static void ks8851_rxctrl_work(struct work_struct *work) |
| 563 |
{ |
| 564 |
struct ks8851_net *ks = container_of(work, struct ks8851_net, rxctrl_work); |
| 565 |
|
| 566 |
ks8851_lock(ks); |
| 567 |
|
| 568 |
/* need to shutdown RXQ before modifying filter parameters */ |
| 569 |
ks8851_wrreg16(ks, KS_RXCR1, 0x00); |
| 570 |
|
| 571 |
ks8851_unlock(ks); |
| 572 |
} |
| 573 |
|
| 574 |
static void ks8851_set_rx_mode(struct net_device *dev) |
| 575 |
{ |
| 576 |
struct ks8851_net *ks = netdev_priv(dev); |
| 577 |
struct ks8851_rxctrl rxctrl; |
| 578 |
|
| 579 |
memset(&rxctrl, 0, sizeof(rxctrl)); |
| 580 |
|
| 581 |
if (dev->flags & IFF_PROMISC) { |
| 582 |
/* interface to receive everything */ |
| 583 |
|
| 584 |
rxctrl.rxcr1 = RXCR1_RXAE | RXCR1_RXINVF; |
| 585 |
} else if (dev->flags & IFF_ALLMULTI) { |
| 586 |
/* accept all multicast packets */ |
| 587 |
|
| 588 |
rxctrl.rxcr1 = (RXCR1_RXME | RXCR1_RXAE | |
| 589 |
RXCR1_RXPAFMA | RXCR1_RXMAFMA); |
| 590 |
} else if (dev->flags & IFF_MULTICAST && !netdev_mc_empty(dev)) { |
| 591 |
struct netdev_hw_addr *ha; |
| 592 |
u32 crc; |
| 593 |
|
| 594 |
/* accept some multicast */ |
| 595 |
|
| 596 |
netdev_for_each_mc_addr(ha, dev) { |
| 597 |
crc = ether_crc(ETH_ALEN, ha->addr); |
| 598 |
crc >>= (32 - 6); /* get top six bits */ |
| 599 |
|
| 600 |
rxctrl.mchash[crc >> 4] |= (1 << (crc & 0xf)); |
| 601 |
} |
| 602 |
|
| 603 |
rxctrl.rxcr1 = RXCR1_RXME | RXCR1_RXPAFMA; |
| 604 |
} else { |
| 605 |
/* just accept broadcast / unicast */ |
| 606 |
rxctrl.rxcr1 = RXCR1_RXPAFMA; |
| 607 |
} |
| 608 |
|
| 609 |
rxctrl.rxcr1 |= (RXCR1_RXUE | /* unicast enable */ |
| 610 |
RXCR1_RXBE | /* broadcast enable */ |
| 611 |
RXCR1_RXE | /* RX process enable */ |
| 612 |
RXCR1_RXFCE); /* enable flow control */ |
| 613 |
|
| 614 |
rxctrl.rxcr2 |= RXCR2_SRDBL_FRAME; |
| 615 |
|
| 616 |
/* schedule work to do the actual set of the data if needed */ |
| 617 |
|
| 618 |
spin_lock_bh(&ks->statelock); |
| 619 |
|
| 620 |
if (memcmp(&rxctrl, &ks->rxctrl, sizeof(rxctrl)) != 0) { |
| 621 |
memcpy(&ks->rxctrl, &rxctrl, sizeof(ks->rxctrl)); |
| 622 |
schedule_work(&ks->rxctrl_work); |
| 623 |
} |
| 624 |
|
| 625 |
spin_unlock_bh(&ks->statelock); |
| 626 |
} |
| 627 |
|
| 628 |
static int ks8851_set_mac_address(struct net_device *dev, void *addr) |
| 629 |
{ |
| 630 |
struct sockaddr *sa = addr; |
| 631 |
|
| 632 |
if (netif_running(dev)) |
| 633 |
return -EBUSY; |
| 634 |
|
| 635 |
if (!is_valid_ether_addr(sa->sa_data)) |
| 636 |
return -EADDRNOTAVAIL; |
| 637 |
|
| 638 |
eth_hw_addr_set(dev, sa->sa_data); |
| 639 |
return ks8851_write_mac_addr(dev); |
| 640 |
} |
| 641 |
|
| 642 |
static int ks8851_net_ioctl(struct net_device *dev, struct ifreq *req, int cmd) |
| 643 |
{ |
| 644 |
struct ks8851_net *ks = netdev_priv(dev); |
| 645 |
|
| 646 |
if (!netif_running(dev)) |
| 647 |
return -EINVAL; |
| 648 |
|
| 649 |
return generic_mii_ioctl(&ks->mii, if_mii(req), cmd, NULL); |
| 650 |
} |
| 651 |
|
| 652 |
static const struct net_device_ops ks8851_netdev_ops = { |
| 653 |
.ndo_open = ks8851_net_open, |
| 654 |
.ndo_stop = ks8851_net_stop, |
| 655 |
.ndo_eth_ioctl = ks8851_net_ioctl, |
| 656 |
.ndo_start_xmit = ks8851_start_xmit, |
| 657 |
.ndo_set_mac_address = ks8851_set_mac_address, |
| 658 |
.ndo_set_rx_mode = ks8851_set_rx_mode, |
| 659 |
.ndo_validate_addr = eth_validate_addr, |
| 660 |
}; |
| 661 |
|
| 662 |
/* ethtool support */ |
| 663 |
|
| 664 |
static void ks8851_get_drvinfo(struct net_device *dev, |
| 665 |
struct ethtool_drvinfo *di) |
| 666 |
{ |
| 667 |
strscpy(di->driver, "KS8851", sizeof(di->driver)); |
| 668 |
strscpy(di->version, "1.00", sizeof(di->version)); |
| 669 |
strscpy(di->bus_info, dev_name(dev->dev.parent), sizeof(di->bus_info)); |
| 670 |
} |
| 671 |
|
| 672 |
static u32 ks8851_get_msglevel(struct net_device *dev) |
| 673 |
{ |
| 674 |
struct ks8851_net *ks = netdev_priv(dev); |
| 675 |
return ks->msg_enable; |
| 676 |
} |
| 677 |
|
| 678 |
static void ks8851_set_msglevel(struct net_device *dev, u32 to) |
| 679 |
{ |
| 680 |
struct ks8851_net *ks = netdev_priv(dev); |
| 681 |
ks->msg_enable = to; |
| 682 |
} |
| 683 |
|
| 684 |
static int ks8851_get_link_ksettings(struct net_device *dev, |
| 685 |
struct ethtool_link_ksettings *cmd) |
| 686 |
{ |
| 687 |
struct ks8851_net *ks = netdev_priv(dev); |
| 688 |
|
| 689 |
mii_ethtool_get_link_ksettings(&ks->mii, cmd); |
| 690 |
|
| 691 |
return 0; |
| 692 |
} |
| 693 |
|
| 694 |
static int ks8851_set_link_ksettings(struct net_device *dev, |
| 695 |
const struct ethtool_link_ksettings *cmd) |
| 696 |
{ |
| 697 |
struct ks8851_net *ks = netdev_priv(dev); |
| 698 |
return mii_ethtool_set_link_ksettings(&ks->mii, cmd); |
| 699 |
} |
| 700 |
|
| 701 |
static u32 ks8851_get_link(struct net_device *dev) |
| 702 |
{ |
| 703 |
struct ks8851_net *ks = netdev_priv(dev); |
| 704 |
return mii_link_ok(&ks->mii); |
| 705 |
} |
| 706 |
|
| 707 |
static int ks8851_nway_reset(struct net_device *dev) |
| 708 |
{ |
| 709 |
struct ks8851_net *ks = netdev_priv(dev); |
| 710 |
return mii_nway_restart(&ks->mii); |
| 711 |
} |
| 712 |
|
| 713 |
/* EEPROM support */ |
| 714 |
|
| 715 |
static void ks8851_eeprom_regread(struct eeprom_93cx6 *ee) |
| 716 |
{ |
| 717 |
struct ks8851_net *ks = ee->data; |
| 718 |
unsigned val; |
| 719 |
|
| 720 |
val = ks8851_rdreg16(ks, KS_EEPCR); |
| 721 |
|
| 722 |
ee->reg_data_out = (val & EEPCR_EESB) ? 1 : 0; |
| 723 |
ee->reg_data_clock = (val & EEPCR_EESCK) ? 1 : 0; |
| 724 |
ee->reg_chip_select = (val & EEPCR_EECS) ? 1 : 0; |
| 725 |
} |
| 726 |
|
| 727 |
static void ks8851_eeprom_regwrite(struct eeprom_93cx6 *ee) |
| 728 |
{ |
| 729 |
struct ks8851_net *ks = ee->data; |
| 730 |
unsigned val = EEPCR_EESA; /* default - eeprom access on */ |
| 731 |
|
| 732 |
if (ee->drive_data) |
| 733 |
val |= EEPCR_EESRWA; |
| 734 |
if (ee->reg_data_in) |
| 735 |
val |= EEPCR_EEDO; |
| 736 |
if (ee->reg_data_clock) |
| 737 |
val |= EEPCR_EESCK; |
| 738 |
if (ee->reg_chip_select) |
| 739 |
val |= EEPCR_EECS; |
| 740 |
|
| 741 |
ks8851_wrreg16(ks, KS_EEPCR, val); |
| 742 |
} |
| 743 |
|
| 744 |
/** |
| 745 |
* ks8851_eeprom_claim - claim device EEPROM and activate the interface |
| 746 |
* @ks: The network device state. |
| 747 |
* |
| 748 |
* Check for the presence of an EEPROM, and then activate software access |
| 749 |
* to the device. |
| 750 |
*/ |
| 751 |
static int ks8851_eeprom_claim(struct ks8851_net *ks) |
| 752 |
{ |
| 753 |
/* start with clock low, cs high */ |
| 754 |
ks8851_wrreg16(ks, KS_EEPCR, EEPCR_EESA | EEPCR_EECS); |
| 755 |
return 0; |
| 756 |
} |
| 757 |
|
| 758 |
/** |
| 759 |
* ks8851_eeprom_release - release the EEPROM interface |
| 760 |
* @ks: The device state |
| 761 |
* |
| 762 |
* Release the software access to the device EEPROM |
| 763 |
*/ |
| 764 |
static void ks8851_eeprom_release(struct ks8851_net *ks) |
| 765 |
{ |
| 766 |
unsigned val = ks8851_rdreg16(ks, KS_EEPCR); |
| 767 |
|
| 768 |
ks8851_wrreg16(ks, KS_EEPCR, val & ~EEPCR_EESA); |
| 769 |
} |
| 770 |
|
| 771 |
#define KS_EEPROM_MAGIC (0x00008851) |
| 772 |
|
| 773 |
static int ks8851_set_eeprom(struct net_device *dev, |
| 774 |
struct ethtool_eeprom *ee, u8 *data) |
| 775 |
{ |
| 776 |
struct ks8851_net *ks = netdev_priv(dev); |
| 777 |
int offset = ee->offset; |
| 778 |
int len = ee->len; |
| 779 |
u16 tmp; |
| 780 |
|
| 781 |
/* currently only support byte writing */ |
| 782 |
if (len != 1) |
| 783 |
return -EINVAL; |
| 784 |
|
| 785 |
if (ee->magic != KS_EEPROM_MAGIC) |
| 786 |
return -EINVAL; |
| 787 |
|
| 788 |
if (!(ks->rc_ccr & CCR_EEPROM)) |
| 789 |
return -ENOENT; |
| 790 |
|
| 791 |
ks8851_lock(ks); |
| 792 |
|
| 793 |
ks8851_eeprom_claim(ks); |
| 794 |
|
| 795 |
eeprom_93cx6_wren(&ks->eeprom, true); |
| 796 |
|
| 797 |
/* ethtool currently only supports writing bytes, which means |
| 798 |
* we have to read/modify/write our 16bit EEPROMs */ |
| 799 |
|
| 800 |
eeprom_93cx6_read(&ks->eeprom, offset/2, &tmp); |
| 801 |
|
| 802 |
if (offset & 1) { |
| 803 |
tmp &= 0xff; |
| 804 |
tmp |= *data << 8; |
| 805 |
} else { |
| 806 |
tmp &= 0xff00; |
| 807 |
tmp |= *data; |
| 808 |
} |
| 809 |
|
| 810 |
eeprom_93cx6_write(&ks->eeprom, offset/2, tmp); |
| 811 |
eeprom_93cx6_wren(&ks->eeprom, false); |
| 812 |
|
| 813 |
ks8851_eeprom_release(ks); |
| 814 |
ks8851_unlock(ks); |
| 815 |
|
| 816 |
return 0; |
| 817 |
} |
| 818 |
|
| 819 |
static int ks8851_get_eeprom(struct net_device *dev, |
| 820 |
struct ethtool_eeprom *ee, u8 *data) |
| 821 |
{ |
| 822 |
struct ks8851_net *ks = netdev_priv(dev); |
| 823 |
int offset = ee->offset; |
| 824 |
int len = ee->len; |
| 825 |
|
| 826 |
/* must be 2 byte aligned */ |
| 827 |
if (len & 1 || offset & 1) |
| 828 |
return -EINVAL; |
| 829 |
|
| 830 |
if (!(ks->rc_ccr & CCR_EEPROM)) |
| 831 |
return -ENOENT; |
| 832 |
|
| 833 |
ks8851_lock(ks); |
| 834 |
|
| 835 |
ks8851_eeprom_claim(ks); |
| 836 |
|
| 837 |
ee->magic = KS_EEPROM_MAGIC; |
| 838 |
|
| 839 |
eeprom_93cx6_multiread(&ks->eeprom, offset/2, (__le16 *)data, len/2); |
| 840 |
ks8851_eeprom_release(ks); |
| 841 |
ks8851_unlock(ks); |
| 842 |
|
| 843 |
return 0; |
| 844 |
} |
| 845 |
|
| 846 |
static int ks8851_get_eeprom_len(struct net_device *dev) |
| 847 |
{ |
| 848 |
struct ks8851_net *ks = netdev_priv(dev); |
| 849 |
|
| 850 |
/* currently, we assume it is an 93C46 attached, so return 128 */ |
| 851 |
return ks->rc_ccr & CCR_EEPROM ? 128 : 0; |
| 852 |
} |
| 853 |
|
| 854 |
static const struct ethtool_ops ks8851_ethtool_ops = { |
| 855 |
.get_drvinfo = ks8851_get_drvinfo, |
| 856 |
.get_msglevel = ks8851_get_msglevel, |
| 857 |
.set_msglevel = ks8851_set_msglevel, |
| 858 |
.get_link = ks8851_get_link, |
| 859 |
.nway_reset = ks8851_nway_reset, |
| 860 |
.get_eeprom_len = ks8851_get_eeprom_len, |
| 861 |
.get_eeprom = ks8851_get_eeprom, |
| 862 |
.set_eeprom = ks8851_set_eeprom, |
| 863 |
.get_link_ksettings = ks8851_get_link_ksettings, |
| 864 |
.set_link_ksettings = ks8851_set_link_ksettings, |
| 865 |
}; |
| 866 |
|
| 867 |
/* MII interface controls */ |
| 868 |
|
| 869 |
/** |
| 870 |
* ks8851_phy_reg - convert MII register into a KS8851 register |
| 871 |
* @reg: MII register number. |
| 872 |
* |
| 873 |
* Return the KS8851 register number for the corresponding MII PHY register |
| 874 |
* if possible. Return zero if the MII register has no direct mapping to the |
| 875 |
* KS8851 register set. |
| 876 |
*/ |
| 877 |
static int ks8851_phy_reg(int reg) |
| 878 |
{ |
| 879 |
switch (reg) { |
| 880 |
case MII_BMCR: |
| 881 |
return KS_P1MBCR; |
| 882 |
case MII_BMSR: |
| 883 |
return KS_P1MBSR; |
| 884 |
case MII_PHYSID1: |
| 885 |
return KS_PHY1ILR; |
| 886 |
case MII_PHYSID2: |
| 887 |
return KS_PHY1IHR; |
| 888 |
case MII_ADVERTISE: |
| 889 |
return KS_P1ANAR; |
| 890 |
case MII_LPA: |
| 891 |
return KS_P1ANLPR; |
| 892 |
} |
| 893 |
|
| 894 |
return -EOPNOTSUPP; |
| 895 |
} |
| 896 |
|
| 897 |
static int ks8851_phy_read_common(struct net_device *dev, int phy_addr, int reg) |
| 898 |
{ |
| 899 |
struct ks8851_net *ks = netdev_priv(dev); |
| 900 |
int result; |
| 901 |
int ksreg; |
| 902 |
|
| 903 |
ksreg = ks8851_phy_reg(reg); |
| 904 |
if (ksreg < 0) |
| 905 |
return ksreg; |
| 906 |
|
| 907 |
ks8851_lock(ks); |
| 908 |
result = ks8851_rdreg16(ks, ksreg); |
| 909 |
ks8851_unlock(ks); |
| 910 |
|
| 911 |
return result; |
| 912 |
} |
| 913 |
|
| 914 |
/** |
| 915 |
* ks8851_phy_read - MII interface PHY register read. |
| 916 |
* @dev: The network device the PHY is on. |
| 917 |
* @phy_addr: Address of PHY (ignored as we only have one) |
| 918 |
* @reg: The register to read. |
| 919 |
* |
| 920 |
* This call reads data from the PHY register specified in @reg. Since the |
| 921 |
* device does not support all the MII registers, the non-existent values |
| 922 |
* are always returned as zero. |
| 923 |
* |
| 924 |
* We return zero for unsupported registers as the MII code does not check |
| 925 |
* the value returned for any error status, and simply returns it to the |
| 926 |
* caller. The mii-tool that the driver was tested with takes any -ve error |
| 927 |
* as real PHY capabilities, thus displaying incorrect data to the user. |
| 928 |
*/ |
| 929 |
static int ks8851_phy_read(struct net_device *dev, int phy_addr, int reg) |
| 930 |
{ |
| 931 |
int ret; |
| 932 |
|
| 933 |
ret = ks8851_phy_read_common(dev, phy_addr, reg); |
| 934 |
if (ret < 0) |
| 935 |
return 0x0; /* no error return allowed, so use zero */ |
| 936 |
|
| 937 |
return ret; |
| 938 |
} |
| 939 |
|
| 940 |
static void ks8851_phy_write(struct net_device *dev, |
| 941 |
int phy, int reg, int value) |
| 942 |
{ |
| 943 |
struct ks8851_net *ks = netdev_priv(dev); |
| 944 |
int ksreg; |
| 945 |
|
| 946 |
ksreg = ks8851_phy_reg(reg); |
| 947 |
if (ksreg >= 0) { |
| 948 |
ks8851_lock(ks); |
| 949 |
ks8851_wrreg16(ks, ksreg, value); |
| 950 |
ks8851_unlock(ks); |
| 951 |
} |
| 952 |
} |
| 953 |
|
| 954 |
static int ks8851_mdio_read(struct mii_bus *bus, int phy_id, int reg) |
| 955 |
{ |
| 956 |
struct ks8851_net *ks = bus->priv; |
| 957 |
|
| 958 |
if (phy_id != 0) |
| 959 |
return -EOPNOTSUPP; |
| 960 |
|
| 961 |
/* KS8851 PHY ID registers are swapped in HW, swap them back. */ |
| 962 |
if (reg == MII_PHYSID1) |
| 963 |
reg = MII_PHYSID2; |
| 964 |
else if (reg == MII_PHYSID2) |
| 965 |
reg = MII_PHYSID1; |
| 966 |
|
| 967 |
return ks8851_phy_read_common(ks->netdev, phy_id, reg); |
| 968 |
} |
| 969 |
|
| 970 |
static int ks8851_mdio_write(struct mii_bus *bus, int phy_id, int reg, u16 val) |
| 971 |
{ |
| 972 |
struct ks8851_net *ks = bus->priv; |
| 973 |
|
| 974 |
ks8851_phy_write(ks->netdev, phy_id, reg, val); |
| 975 |
return 0; |
| 976 |
} |
| 977 |
|
| 978 |
/** |
| 979 |
* ks8851_read_selftest - read the selftest memory info. |
| 980 |
* @ks: The device state |
| 981 |
* |
| 982 |
* Read and check the TX/RX memory selftest information. |
| 983 |
*/ |
| 984 |
static void ks8851_read_selftest(struct ks8851_net *ks) |
| 985 |
{ |
| 986 |
unsigned both_done = MBIR_TXMBF | MBIR_RXMBF; |
| 987 |
unsigned rd; |
| 988 |
|
| 989 |
rd = ks8851_rdreg16(ks, KS_MBIR); |
| 990 |
|
| 991 |
if ((rd & both_done) != both_done) { |
| 992 |
netdev_warn(ks->netdev, "Memory selftest not finished\n"); |
| 993 |
return; |
| 994 |
} |
| 995 |
|
| 996 |
if (rd & MBIR_TXMBFA) |
| 997 |
netdev_err(ks->netdev, "TX memory selftest fail\n"); |
| 998 |
|
| 999 |
if (rd & MBIR_RXMBFA) |
| 1000 |
netdev_err(ks->netdev, "RX memory selftest fail\n"); |
| 1001 |
} |
| 1002 |
|
| 1003 |
/* driver bus management functions */ |
| 1004 |
|
| 1005 |
#ifdef CONFIG_PM_SLEEP |
| 1006 |
|
| 1007 |
int ks8851_suspend(struct device *dev) |
| 1008 |
{ |
| 1009 |
struct ks8851_net *ks = dev_get_drvdata(dev); |
| 1010 |
struct net_device *netdev = ks->netdev; |
| 1011 |
|
| 1012 |
if (netif_running(netdev)) { |
| 1013 |
netif_device_detach(netdev); |
| 1014 |
ks8851_net_stop(netdev); |
| 1015 |
} |
| 1016 |
|
| 1017 |
return 0; |
| 1018 |
} |
| 1019 |
EXPORT_SYMBOL_GPL(ks8851_suspend); |
| 1020 |
|
| 1021 |
int ks8851_resume(struct device *dev) |
| 1022 |
{ |
| 1023 |
struct ks8851_net *ks = dev_get_drvdata(dev); |
| 1024 |
struct net_device *netdev = ks->netdev; |
| 1025 |
|
| 1026 |
if (netif_running(netdev)) { |
| 1027 |
ks8851_net_open(netdev); |
| 1028 |
netif_device_attach(netdev); |
| 1029 |
} |
| 1030 |
|
| 1031 |
return 0; |
| 1032 |
} |
| 1033 |
EXPORT_SYMBOL_GPL(ks8851_resume); |
| 1034 |
#endif |
| 1035 |
|
| 1036 |
static int ks8851_register_mdiobus(struct ks8851_net *ks, struct device *dev) |
| 1037 |
{ |
| 1038 |
struct mii_bus *mii_bus; |
| 1039 |
int ret; |
| 1040 |
|
| 1041 |
mii_bus = mdiobus_alloc(); |
| 1042 |
if (!mii_bus) |
| 1043 |
return -ENOMEM; |
| 1044 |
|
| 1045 |
mii_bus->name = "ks8851_eth_mii"; |
| 1046 |
mii_bus->read = ks8851_mdio_read; |
| 1047 |
mii_bus->write = ks8851_mdio_write; |
| 1048 |
mii_bus->priv = ks; |
| 1049 |
mii_bus->parent = dev; |
| 1050 |
mii_bus->phy_mask = ~((u32)BIT(0)); |
| 1051 |
snprintf(mii_bus->id, MII_BUS_ID_SIZE, "%s", dev_name(dev)); |
| 1052 |
|
| 1053 |
ret = mdiobus_register(mii_bus); |
| 1054 |
if (ret) |
| 1055 |
goto err_mdiobus_register; |
| 1056 |
|
| 1057 |
ks->mii_bus = mii_bus; |
| 1058 |
|
| 1059 |
return 0; |
| 1060 |
|
| 1061 |
err_mdiobus_register: |
| 1062 |
mdiobus_free(mii_bus); |
| 1063 |
return ret; |
| 1064 |
} |
| 1065 |
|
| 1066 |
static void ks8851_unregister_mdiobus(struct ks8851_net *ks) |
| 1067 |
{ |
| 1068 |
mdiobus_unregister(ks->mii_bus); |
| 1069 |
mdiobus_free(ks->mii_bus); |
| 1070 |
} |
| 1071 |
|
| 1072 |
int ks8851_probe_common(struct net_device *netdev, struct device *dev, |
| 1073 |
int msg_en) |
| 1074 |
{ |
| 1075 |
struct ks8851_net *ks = netdev_priv(netdev); |
| 1076 |
unsigned cider; |
| 1077 |
int ret; |
| 1078 |
|
| 1079 |
ks->netdev = netdev; |
| 1080 |
|
| 1081 |
ks->gpio = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_HIGH); |
| 1082 |
ret = PTR_ERR_OR_ZERO(ks->gpio); |
| 1083 |
if (ret) { |
| 1084 |
if (ret != -EPROBE_DEFER) |
| 1085 |
dev_err(dev, "reset gpio request failed: %d\n", ret); |
| 1086 |
return ret; |
| 1087 |
} |
| 1088 |
|
| 1089 |
ret = gpiod_set_consumer_name(ks->gpio, "ks8851_rst_n"); |
| 1090 |
if (ret) { |
| 1091 |
dev_err(dev, "failed to set reset gpio name: %d\n", ret); |
| 1092 |
return ret; |
| 1093 |
} |
| 1094 |
|
| 1095 |
ks->vdd_io = devm_regulator_get(dev, "vdd-io"); |
| 1096 |
if (IS_ERR(ks->vdd_io)) { |
| 1097 |
ret = PTR_ERR(ks->vdd_io); |
| 1098 |
goto err_reg_io; |
| 1099 |
} |
| 1100 |
|
| 1101 |
ret = regulator_enable(ks->vdd_io); |
| 1102 |
if (ret) { |
| 1103 |
dev_err(dev, "regulator vdd_io enable fail: %d\n", ret); |
| 1104 |
goto err_reg_io; |
| 1105 |
} |
| 1106 |
|
| 1107 |
ks->vdd_reg = devm_regulator_get(dev, "vdd"); |
| 1108 |
if (IS_ERR(ks->vdd_reg)) { |
| 1109 |
ret = PTR_ERR(ks->vdd_reg); |
| 1110 |
goto err_reg; |
| 1111 |
} |
| 1112 |
|
| 1113 |
ret = regulator_enable(ks->vdd_reg); |
| 1114 |
if (ret) { |
| 1115 |
dev_err(dev, "regulator vdd enable fail: %d\n", ret); |
| 1116 |
goto err_reg; |
| 1117 |
} |
| 1118 |
|
| 1119 |
if (ks->gpio) { |
| 1120 |
usleep_range(10000, 11000); |
| 1121 |
gpiod_set_value_cansleep(ks->gpio, 0); |
| 1122 |
} |
| 1123 |
|
| 1124 |
spin_lock_init(&ks->statelock); |
| 1125 |
|
| 1126 |
INIT_WORK(&ks->rxctrl_work, ks8851_rxctrl_work); |
| 1127 |
|
| 1128 |
SET_NETDEV_DEV(netdev, dev); |
| 1129 |
|
| 1130 |
/* setup EEPROM state */ |
| 1131 |
ks->eeprom.data = ks; |
| 1132 |
ks->eeprom.width = PCI_EEPROM_WIDTH_93C46; |
| 1133 |
ks->eeprom.register_read = ks8851_eeprom_regread; |
| 1134 |
ks->eeprom.register_write = ks8851_eeprom_regwrite; |
| 1135 |
|
| 1136 |
/* setup mii state */ |
| 1137 |
ks->mii.dev = netdev; |
| 1138 |
ks->mii.phy_id = 1; |
| 1139 |
ks->mii.phy_id_mask = 1; |
| 1140 |
ks->mii.reg_num_mask = 0xf; |
| 1141 |
ks->mii.mdio_read = ks8851_phy_read; |
| 1142 |
ks->mii.mdio_write = ks8851_phy_write; |
| 1143 |
|
| 1144 |
dev_info(dev, "message enable is %d\n", msg_en); |
| 1145 |
|
| 1146 |
ret = ks8851_register_mdiobus(ks, dev); |
| 1147 |
if (ret) |
| 1148 |
goto err_mdio; |
| 1149 |
|
| 1150 |
/* set the default message enable */ |
| 1151 |
ks->msg_enable = netif_msg_init(msg_en, NETIF_MSG_DRV | |
| 1152 |
NETIF_MSG_PROBE | |
| 1153 |
NETIF_MSG_LINK); |
| 1154 |
|
| 1155 |
skb_queue_head_init(&ks->txq); |
| 1156 |
|
| 1157 |
netdev->ethtool_ops = &ks8851_ethtool_ops; |
| 1158 |
|
| 1159 |
dev_set_drvdata(dev, ks); |
| 1160 |
|
| 1161 |
netif_carrier_off(ks->netdev); |
| 1162 |
netdev->if_port = IF_PORT_100BASET; |
| 1163 |
netdev->netdev_ops = &ks8851_netdev_ops; |
| 1164 |
|
| 1165 |
/* issue a global soft reset to reset the device. */ |
| 1166 |
ks8851_soft_reset(ks, GRR_GSR); |
| 1167 |
|
| 1168 |
/* simple check for a valid chip being connected to the bus */ |
| 1169 |
cider = ks8851_rdreg16(ks, KS_CIDER); |
| 1170 |
if ((cider & ~CIDER_REV_MASK) != CIDER_ID) { |
| 1171 |
dev_err(dev, "failed to read device ID\n"); |
| 1172 |
ret = -ENODEV; |
| 1173 |
goto err_id; |
| 1174 |
} |
| 1175 |
|
| 1176 |
/* cache the contents of the CCR register for EEPROM, etc. */ |
| 1177 |
ks->rc_ccr = ks8851_rdreg16(ks, KS_CCR); |
| 1178 |
|
| 1179 |
ks8851_read_selftest(ks); |
| 1180 |
ks8851_init_mac(ks, dev->of_node); |
| 1181 |
|
| 1182 |
ret = register_netdev(netdev); |
| 1183 |
if (ret) { |
| 1184 |
dev_err(dev, "failed to register network device\n"); |
| 1185 |
goto err_id; |
| 1186 |
} |
| 1187 |
|
| 1188 |
netdev_info(netdev, "revision %d, MAC %pM, IRQ %d, %s EEPROM\n", |
| 1189 |
CIDER_REV_GET(cider), netdev->dev_addr, netdev->irq, |
| 1190 |
ks->rc_ccr & CCR_EEPROM ? "has" : "no"); |
| 1191 |
|
| 1192 |
return 0; |
| 1193 |
|
| 1194 |
err_id: |
| 1195 |
ks8851_unregister_mdiobus(ks); |
| 1196 |
err_mdio: |
| 1197 |
if (ks->gpio) |
| 1198 |
gpiod_set_value_cansleep(ks->gpio, 1); |
| 1199 |
regulator_disable(ks->vdd_reg); |
| 1200 |
err_reg: |
| 1201 |
regulator_disable(ks->vdd_io); |
| 1202 |
err_reg_io: |
| 1203 |
return ret; |
| 1204 |
} |
| 1205 |
EXPORT_SYMBOL_GPL(ks8851_probe_common); |
| 1206 |
|
| 1207 |
void ks8851_remove_common(struct device *dev) |
| 1208 |
{ |
| 1209 |
struct ks8851_net *priv = dev_get_drvdata(dev); |
| 1210 |
|
| 1211 |
ks8851_unregister_mdiobus(priv); |
| 1212 |
|
| 1213 |
if (netif_msg_drv(priv)) |
| 1214 |
dev_info(dev, "remove\n"); |
| 1215 |
|
| 1216 |
unregister_netdev(priv->netdev); |
| 1217 |
if (priv->gpio) |
| 1218 |
gpiod_set_value_cansleep(priv->gpio, 1); |
| 1219 |
regulator_disable(priv->vdd_reg); |
| 1220 |
regulator_disable(priv->vdd_io); |
| 1221 |
} |
| 1222 |
EXPORT_SYMBOL_GPL(ks8851_remove_common); |
| 1223 |
|
| 1224 |
MODULE_DESCRIPTION("KS8851 Network driver"); |
| 1225 |
MODULE_AUTHOR("Ben Dooks <[email protected]>"); |
| 1226 |
MODULE_LICENSE("GPL"); |