12 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
17 #include <linux/module.h>
18 #include <linux/kernel.h>
19 #include <linux/netdevice.h>
21 #include <linux/ethtool.h>
24 #include <linux/mii.h>
140 #define BYTE_EN(_x) ((_x) << 2)
143 #define MK_OP(_byteen, _reg) (BYTE_EN(_byteen) | (_reg) << (8+2) | (_reg) >> 6)
176 netdev_err(ks->
netdev,
"spi_sync() failed\n");
195 bit = 1 << (reg & 3);
206 netdev_err(ks->
netdev,
"spi_sync() failed\n");
223 static inline bool ks8851_rx_1msg(
struct ks8851_net *ks)
238 static void ks8851_rdreg(
struct ks8851_net *ks,
unsigned op,
239 u8 *rxb,
unsigned rxl)
249 if (ks8851_rx_1msg(ks)) {
272 netdev_err(ks->
netdev,
"read: spi_sync() failed\n");
273 else if (ks8851_rx_1msg(ks))
274 memcpy(rxb, trx + 2, rxl);
286 static unsigned ks8851_rdreg8(
struct ks8851_net *ks,
unsigned reg)
290 ks8851_rdreg(ks,
MK_OP(1 << (reg & 3), reg), rxb, 1);
301 static unsigned ks8851_rdreg16(
struct ks8851_net *ks,
unsigned reg)
305 ks8851_rdreg(ks,
MK_OP(reg & 2 ? 0xC : 0x3, reg), (
u8 *)&rx, 2);
318 static unsigned ks8851_rdreg32(
struct ks8851_net *ks,
unsigned reg)
324 ks8851_rdreg(ks,
MK_OP(0xf, reg), (
u8 *)&rx, 4);
341 static void ks8851_soft_reset(
struct ks8851_net *ks,
unsigned op)
343 ks8851_wrreg16(ks,
KS_GRR, op);
345 ks8851_wrreg16(ks,
KS_GRR, 0);
356 static void ks8851_set_powermode(
struct ks8851_net *ks,
unsigned pwrmode)
362 pmecr = ks8851_rdreg16(ks,
KS_PMECR);
366 ks8851_wrreg16(ks,
KS_PMECR, pmecr);
392 if (!netif_running(dev))
406 static void ks8851_read_mac_addr(
struct net_device *dev)
428 static void ks8851_init_mac(
struct ks8851_net *ks)
434 ks8851_read_mac_addr(dev);
435 if (is_valid_ether_addr(dev->
dev_addr))
438 netdev_err(ks->
netdev,
"invalid mac address read %pM\n",
442 eth_hw_addr_random(dev);
443 ks8851_write_mac_addr(dev);
472 static void ks8851_rdfifo(
struct ks8851_net *ks,
u8 *buff,
unsigned len)
480 "%s: %d@%p\n", __func__, len, buff);
496 netdev_err(ks->
netdev,
"%s: spi_sync() failed\n", __func__);
506 static void ks8851_dbg_dumpkkt(
struct ks8851_net *ks,
u8 *rxpkt)
509 "pkt %02x%02x%02x%02x %02x%02x%02x%02x %02x%02x%02x%02x\n",
510 rxpkt[4], rxpkt[5], rxpkt[6], rxpkt[7],
511 rxpkt[8], rxpkt[9], rxpkt[10], rxpkt[11],
512 rxpkt[12], rxpkt[13], rxpkt[14], rxpkt[15]);
523 static void ks8851_rx_pkts(
struct ks8851_net *ks)
532 rxfc = ks8851_rdreg8(ks,
KS_RXFC);
535 "%s: %d packets\n", __func__, rxfc);
547 for (; rxfc != 0; rxfc--) {
549 rxstat = rxh & 0xffff;
553 "rx: stat 0x%04x, len 0x%04x\n", rxstat, rxlen);
565 unsigned int rxalign;
568 rxalign =
ALIGN(rxlen, 4);
569 skb = netdev_alloc_skb_ip_align(ks->
netdev, rxalign);
578 rxpkt =
skb_put(skb, rxlen) - 8;
580 ks8851_rdfifo(ks, rxpkt, rxalign + 8);
583 ks8851_dbg_dumpkkt(ks, rxpkt);
588 ks->
netdev->stats.rx_packets++;
612 unsigned handled = 0;
616 status = ks8851_rdreg16(ks,
KS_ISR);
619 "%s: status 0x%04x\n", __func__, status);
645 "%s: txspace %d\n", __func__, ks->
tx_space);
656 ks8851_wrreg16(ks,
KS_ISR, handled);
658 if (status & IRQ_RXI) {
670 if (status & IRQ_RXPSI) {
685 if (status & IRQ_LCI)
688 if (status & IRQ_TXI)
689 netif_wake_queue(ks->
netdev);
701 static inline unsigned calc_txlen(
unsigned len)
703 return ALIGN(len + 4, 4);
725 __func__, txp, txp->
len, txp->
data, irq);
738 xfer->
tx_buf = &ks->txh.txb[1];
749 netdev_err(ks->
netdev,
"%s: spi_sync() failed\n", __func__);
762 dev->
stats.tx_packets++;
774 static void ks8851_tx_work(
struct work_struct *work)
778 bool last = skb_queue_empty(&ks->
txq);
784 last = skb_queue_empty(&ks->
txq);
788 ks8851_wrpkt(ks, txb, last);
792 ks8851_done_tx(ks, txb);
806 static int ks8851_net_open(
struct net_device *dev)
821 ks8851_soft_reset(ks,
GRR_QMU);
857 #define STD_IRQ (IRQ_LCI | \
865 ks8851_wrreg16(ks,
KS_ISR, STD_IRQ);
866 ks8851_wrreg16(ks,
KS_IER, STD_IRQ);
868 netif_start_queue(ks->
netdev);
884 static int ks8851_net_stop(
struct net_device *dev)
888 netif_info(ks, ifdown, dev,
"shutting down\n");
890 netif_stop_queue(dev);
894 ks8851_wrreg16(ks,
KS_IER, 0x0000);
895 ks8851_wrreg16(ks,
KS_ISR, 0xffff);
905 ks8851_wrreg16(ks,
KS_RXCR1, 0x0000);
908 ks8851_wrreg16(ks,
KS_TXCR, 0x0000);
915 while (!skb_queue_empty(&ks->
txq)) {
919 "%s: freeing txb %p\n", __func__, txb);
944 unsigned needed = calc_txlen(skb->
len);
948 "%s: skb %p, %d@%p\n", __func__, skb, skb->
len, skb->
data);
953 netif_stop_queue(dev);
979 static void ks8851_rxctrl_work(
struct work_struct *work)
991 static void ks8851_set_rx_mode(
struct net_device *dev)
996 memset(&rxctrl, 0,
sizeof(rxctrl));
1017 rxctrl.mchash[crc >> 4] |= (1 << (crc & 0xf));
1037 if (
memcmp(&rxctrl, &ks->
rxctrl,
sizeof(rxctrl)) != 0) {
1045 static int ks8851_set_mac_address(
struct net_device *dev,
void *
addr)
1049 if (netif_running(dev))
1052 if (!is_valid_ether_addr(sa->
sa_data))
1057 return ks8851_write_mac_addr(dev);
1064 if (!netif_running(dev))
1071 .ndo_open = ks8851_net_open,
1072 .ndo_stop = ks8851_net_stop,
1073 .ndo_do_ioctl = ks8851_net_ioctl,
1074 .ndo_start_xmit = ks8851_start_xmit,
1075 .ndo_set_mac_address = ks8851_set_mac_address,
1076 .ndo_set_rx_mode = ks8851_set_rx_mode,
1083 static void ks8851_get_drvinfo(
struct net_device *dev,
1094 return ks->msg_enable;
1097 static void ks8851_set_msglevel(
struct net_device *dev,
u32 to)
1100 ks->msg_enable = to;
1121 static int ks8851_nway_reset(
struct net_device *dev)
1129 static void ks8851_eeprom_regread(
struct eeprom_93cx6 *ee)
1134 val = ks8851_rdreg16(ks,
KS_EEPCR);
1141 static void ks8851_eeprom_regwrite(
struct eeprom_93cx6 *ee)
1165 static int ks8851_eeprom_claim(
struct ks8851_net *ks)
1183 static void ks8851_eeprom_release(
struct ks8851_net *ks)
1185 unsigned val = ks8851_rdreg16(ks,
KS_EEPCR);
1191 #define KS_EEPROM_MAGIC (0x00008851)
1193 static int ks8851_set_eeprom(
struct net_device *dev,
1208 if (ks8851_eeprom_claim(ks))
1229 ks8851_eeprom_release(ks);
1234 static int ks8851_get_eeprom(
struct net_device *dev,
1242 if (len & 1 || offset & 1)
1245 if (ks8851_eeprom_claim(ks))
1251 ks8851_eeprom_release(ks);
1256 static int ks8851_get_eeprom_len(
struct net_device *dev)
1264 static const struct ethtool_ops ks8851_ethtool_ops = {
1265 .get_drvinfo = ks8851_get_drvinfo,
1266 .get_msglevel = ks8851_get_msglevel,
1267 .set_msglevel = ks8851_set_msglevel,
1268 .get_settings = ks8851_get_settings,
1269 .set_settings = ks8851_set_settings,
1270 .get_link = ks8851_get_link,
1271 .nway_reset = ks8851_nway_reset,
1272 .get_eeprom_len = ks8851_get_eeprom_len,
1273 .get_eeprom = ks8851_get_eeprom,
1274 .set_eeprom = ks8851_set_eeprom,
1287 static int ks8851_phy_reg(
int reg)
1322 static int ks8851_phy_read(
struct net_device *dev,
int phy_addr,
int reg)
1328 ksreg = ks8851_phy_reg(reg);
1333 result = ks8851_rdreg16(ks, ksreg);
1339 static void ks8851_phy_write(
struct net_device *dev,
1345 ksreg = ks8851_phy_reg(reg);
1348 ks8851_wrreg16(ks, ksreg, value);
1359 static int ks8851_read_selftest(
struct ks8851_net *ks)
1365 rd = ks8851_rdreg16(ks,
KS_MBIR);
1367 if ((rd & both_done) != both_done) {
1368 netdev_warn(ks->
netdev,
"Memory selftest not finished\n");
1373 netdev_err(ks->
netdev,
"TX memory selftest fail\n");
1378 netdev_err(ks->
netdev,
"RX memory selftest fail\n");
1393 if (netif_running(dev)) {
1395 ks8851_net_stop(dev);
1406 if (netif_running(dev)) {
1407 ks8851_net_open(dev);
1414 #define ks8851_suspend NULL
1415 #define ks8851_resume NULL
1425 ndev = alloc_etherdev(
sizeof(
struct ks8851_net));
1431 ks = netdev_priv(ndev);
1457 ks->
eeprom.register_read = ks8851_eeprom_regread;
1458 ks->
eeprom.register_write = ks8851_eeprom_regwrite;
1463 ks->
mii.phy_id_mask = 1;
1464 ks->
mii.reg_num_mask = 0xf;
1465 ks->
mii.mdio_read = ks8851_phy_read;
1466 ks->
mii.mdio_write = ks8851_phy_write;
1475 skb_queue_head_init(&ks->
txq);
1487 ks8851_soft_reset(ks,
GRR_GSR);
1490 cider = ks8851_rdreg16(ks,
KS_CIDER);
1492 dev_err(&spi->
dev,
"failed to read device ID\n");
1505 ks8851_read_selftest(ks);
1506 ks8851_init_mac(ks);
1517 dev_err(&spi->
dev,
"failed to register network device\n");
1521 netdev_info(ndev,
"revision %d, MAC %pM, IRQ %d, %s EEPROM\n",
1556 .probe = ks8851_probe,
1562 static int __init ks8851_init(
void)
1567 static void __exit ks8851_exit(
void)
1569 spi_unregister_driver(&ks8851_driver);