1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Copyright (c) 2010 ASIX Electronics Corporation 4 * Copyright (c) 2020 Samsung Electronics Co., Ltd. 5 * 6 * ASIX AX88796C SPI Fast Ethernet Linux driver 7 */ 8 9 #define pr_fmt(fmt) "ax88796c: " fmt 10 11 #include "ax88796c_main.h" 12 #include "ax88796c_ioctl.h" 13 14 #include <linux/bitmap.h> 15 #include <linux/etherdevice.h> 16 #include <linux/iopoll.h> 17 #include <linux/lockdep.h> 18 #include <linux/mdio.h> 19 #include <linux/minmax.h> 20 #include <linux/module.h> 21 #include <linux/netdevice.h> 22 #include <linux/of.h> 23 #include <linux/phy.h> 24 #include <linux/skbuff.h> 25 #include <linux/spi/spi.h> 26 27 static int comp = IS_ENABLED(CONFIG_SPI_AX88796C_COMPRESSION); 28 static int msg_enable = NETIF_MSG_PROBE | 29 NETIF_MSG_LINK | 30 NETIF_MSG_RX_ERR | 31 NETIF_MSG_TX_ERR; 32 33 static const char *no_regs_list = "80018001,e1918001,8001a001,fc0d0000"; 34 unsigned long ax88796c_no_regs_mask[AX88796C_REGDUMP_LEN / (sizeof(unsigned long) * 8)]; 35 36 module_param(msg_enable, int, 0444); 37 MODULE_PARM_DESC(msg_enable, "Message mask (see linux/netdevice.h for bitmap)"); 38 39 static int ax88796c_soft_reset(struct ax88796c_device *ax_local) 40 { 41 u16 temp; 42 int ret; 43 44 lockdep_assert_held(&ax_local->spi_lock); 45 46 AX_WRITE(&ax_local->ax_spi, PSR_RESET, P0_PSR); 47 AX_WRITE(&ax_local->ax_spi, PSR_RESET_CLR, P0_PSR); 48 49 ret = read_poll_timeout(AX_READ, ret, 50 (ret & PSR_DEV_READY), 51 0, jiffies_to_usecs(160 * HZ / 1000), false, 52 &ax_local->ax_spi, P0_PSR); 53 if (ret) 54 return ret; 55 56 temp = AX_READ(&ax_local->ax_spi, P4_SPICR); 57 if (ax_local->priv_flags & AX_CAP_COMP) { 58 AX_WRITE(&ax_local->ax_spi, 59 (temp | SPICR_RCEN | SPICR_QCEN), P4_SPICR); 60 ax_local->ax_spi.comp = 1; 61 } else { 62 AX_WRITE(&ax_local->ax_spi, 63 (temp & ~(SPICR_RCEN | SPICR_QCEN)), P4_SPICR); 64 ax_local->ax_spi.comp = 0; 65 } 66 67 return 0; 68 } 69 70 static int ax88796c_reload_eeprom(struct ax88796c_device *ax_local) 71 { 72 int ret; 73 74 lockdep_assert_held(&ax_local->spi_lock); 75 76 AX_WRITE(&ax_local->ax_spi, EECR_RELOAD, P3_EECR); 77 78 ret = read_poll_timeout(AX_READ, ret, 79 (ret & PSR_DEV_READY), 80 0, jiffies_to_usecs(2 * HZ / 1000), false, 81 &ax_local->ax_spi, P0_PSR); 82 if (ret) { 83 dev_err(&ax_local->spi->dev, 84 "timeout waiting for reload eeprom\n"); 85 return ret; 86 } 87 88 return 0; 89 } 90 91 static void ax88796c_set_hw_multicast(struct net_device *ndev) 92 { 93 struct ax88796c_device *ax_local = to_ax88796c_device(ndev); 94 int mc_count = netdev_mc_count(ndev); 95 u16 rx_ctl = RXCR_AB; 96 97 lockdep_assert_held(&ax_local->spi_lock); 98 99 memset(ax_local->multi_filter, 0, AX_MCAST_FILTER_SIZE); 100 101 if (ndev->flags & IFF_PROMISC) { 102 rx_ctl |= RXCR_PRO; 103 104 } else if (ndev->flags & IFF_ALLMULTI || mc_count > AX_MAX_MCAST) { 105 rx_ctl |= RXCR_AMALL; 106 107 } else if (mc_count == 0) { 108 /* just broadcast and directed */ 109 } else { 110 u32 crc_bits; 111 int i; 112 struct netdev_hw_addr *ha; 113 114 netdev_for_each_mc_addr(ha, ndev) { 115 crc_bits = ether_crc(ETH_ALEN, ha->addr); 116 ax_local->multi_filter[crc_bits >> 29] |= 117 (1 << ((crc_bits >> 26) & 7)); 118 } 119 120 for (i = 0; i < 4; i++) { 121 AX_WRITE(&ax_local->ax_spi, 122 ((ax_local->multi_filter[i * 2 + 1] << 8) | 123 ax_local->multi_filter[i * 2]), P3_MFAR(i)); 124 } 125 } 126 127 AX_WRITE(&ax_local->ax_spi, rx_ctl, P2_RXCR); 128 } 129 130 static void ax88796c_set_mac_addr(struct net_device *ndev) 131 { 132 struct ax88796c_device *ax_local = to_ax88796c_device(ndev); 133 134 lockdep_assert_held(&ax_local->spi_lock); 135 136 AX_WRITE(&ax_local->ax_spi, ((u16)(ndev->dev_addr[4] << 8) | 137 (u16)ndev->dev_addr[5]), P3_MACASR0); 138 AX_WRITE(&ax_local->ax_spi, ((u16)(ndev->dev_addr[2] << 8) | 139 (u16)ndev->dev_addr[3]), P3_MACASR1); 140 AX_WRITE(&ax_local->ax_spi, ((u16)(ndev->dev_addr[0] << 8) | 141 (u16)ndev->dev_addr[1]), P3_MACASR2); 142 } 143 144 static void ax88796c_load_mac_addr(struct net_device *ndev) 145 { 146 struct ax88796c_device *ax_local = to_ax88796c_device(ndev); 147 u16 temp; 148 149 lockdep_assert_held(&ax_local->spi_lock); 150 151 /* Try the device tree first */ 152 if (!eth_platform_get_mac_address(&ax_local->spi->dev, ndev->dev_addr) && 153 is_valid_ether_addr(ndev->dev_addr)) { 154 if (netif_msg_probe(ax_local)) 155 dev_info(&ax_local->spi->dev, 156 "MAC address read from device tree\n"); 157 return; 158 } 159 160 /* Read the MAC address from AX88796C */ 161 temp = AX_READ(&ax_local->ax_spi, P3_MACASR0); 162 ndev->dev_addr[5] = (u8)temp; 163 ndev->dev_addr[4] = (u8)(temp >> 8); 164 165 temp = AX_READ(&ax_local->ax_spi, P3_MACASR1); 166 ndev->dev_addr[3] = (u8)temp; 167 ndev->dev_addr[2] = (u8)(temp >> 8); 168 169 temp = AX_READ(&ax_local->ax_spi, P3_MACASR2); 170 ndev->dev_addr[1] = (u8)temp; 171 ndev->dev_addr[0] = (u8)(temp >> 8); 172 173 if (is_valid_ether_addr(ndev->dev_addr)) { 174 if (netif_msg_probe(ax_local)) 175 dev_info(&ax_local->spi->dev, 176 "MAC address read from ASIX chip\n"); 177 return; 178 } 179 180 /* Use random address if none found */ 181 if (netif_msg_probe(ax_local)) 182 dev_info(&ax_local->spi->dev, "Use random MAC address\n"); 183 eth_hw_addr_random(ndev); 184 } 185 186 static void ax88796c_proc_tx_hdr(struct tx_pkt_info *info, u8 ip_summed) 187 { 188 u16 pkt_len_bar = (~info->pkt_len & TX_HDR_SOP_PKTLENBAR); 189 190 /* Prepare SOP header */ 191 info->sop.flags_len = info->pkt_len | 192 ((ip_summed == CHECKSUM_NONE) || 193 (ip_summed == CHECKSUM_UNNECESSARY) ? TX_HDR_SOP_DICF : 0); 194 195 info->sop.seq_lenbar = ((info->seq_num << 11) & TX_HDR_SOP_SEQNUM) 196 | pkt_len_bar; 197 cpu_to_be16s(&info->sop.flags_len); 198 cpu_to_be16s(&info->sop.seq_lenbar); 199 200 /* Prepare Segment header */ 201 info->seg.flags_seqnum_seglen = TX_HDR_SEG_FS | TX_HDR_SEG_LS 202 | info->pkt_len; 203 204 info->seg.eo_so_seglenbar = pkt_len_bar; 205 206 cpu_to_be16s(&info->seg.flags_seqnum_seglen); 207 cpu_to_be16s(&info->seg.eo_so_seglenbar); 208 209 /* Prepare EOP header */ 210 info->eop.seq_len = ((info->seq_num << 11) & 211 TX_HDR_EOP_SEQNUM) | info->pkt_len; 212 info->eop.seqbar_lenbar = ((~info->seq_num << 11) & 213 TX_HDR_EOP_SEQNUMBAR) | pkt_len_bar; 214 215 cpu_to_be16s(&info->eop.seq_len); 216 cpu_to_be16s(&info->eop.seqbar_lenbar); 217 } 218 219 static int 220 ax88796c_check_free_pages(struct ax88796c_device *ax_local, u8 need_pages) 221 { 222 u8 free_pages; 223 u16 tmp; 224 225 lockdep_assert_held(&ax_local->spi_lock); 226 227 free_pages = AX_READ(&ax_local->ax_spi, P0_TFBFCR) & TX_FREEBUF_MASK; 228 if (free_pages < need_pages) { 229 /* schedule free page interrupt */ 230 tmp = AX_READ(&ax_local->ax_spi, P0_TFBFCR) 231 & TFBFCR_SCHE_FREE_PAGE; 232 AX_WRITE(&ax_local->ax_spi, tmp | TFBFCR_TX_PAGE_SET | 233 TFBFCR_SET_FREE_PAGE(need_pages), 234 P0_TFBFCR); 235 return -ENOMEM; 236 } 237 238 return 0; 239 } 240 241 static struct sk_buff * 242 ax88796c_tx_fixup(struct net_device *ndev, struct sk_buff_head *q) 243 { 244 struct ax88796c_device *ax_local = to_ax88796c_device(ndev); 245 u8 spi_len = ax_local->ax_spi.comp ? 1 : 4; 246 struct sk_buff *skb; 247 struct tx_pkt_info info; 248 struct skb_data *entry; 249 u16 pkt_len; 250 u8 padlen, seq_num; 251 u8 need_pages; 252 int headroom; 253 int tailroom; 254 255 if (skb_queue_empty(q)) 256 return NULL; 257 258 skb = skb_peek(q); 259 pkt_len = skb->len; 260 need_pages = (pkt_len + TX_OVERHEAD + 127) >> 7; 261 if (ax88796c_check_free_pages(ax_local, need_pages) != 0) 262 return NULL; 263 264 headroom = skb_headroom(skb); 265 tailroom = skb_tailroom(skb); 266 padlen = round_up(pkt_len, 4) - pkt_len; 267 seq_num = ++ax_local->seq_num & 0x1F; 268 269 info.pkt_len = pkt_len; 270 271 if (skb_cloned(skb) || 272 (headroom < (TX_OVERHEAD + spi_len)) || 273 (tailroom < (padlen + TX_EOP_SIZE))) { 274 size_t h = max((TX_OVERHEAD + spi_len) - headroom, 0); 275 size_t t = max((padlen + TX_EOP_SIZE) - tailroom, 0); 276 277 if (pskb_expand_head(skb, h, t, GFP_KERNEL)) 278 return NULL; 279 } 280 281 info.seq_num = seq_num; 282 ax88796c_proc_tx_hdr(&info, skb->ip_summed); 283 284 /* SOP and SEG header */ 285 memcpy(skb_push(skb, TX_OVERHEAD), &info.sop, TX_OVERHEAD); 286 287 /* Write SPI TXQ header */ 288 memcpy(skb_push(skb, spi_len), ax88796c_tx_cmd_buf, spi_len); 289 290 /* Make 32-bit alignment */ 291 skb_put(skb, padlen); 292 293 /* EOP header */ 294 memcpy(skb_put(skb, TX_EOP_SIZE), &info.eop, TX_EOP_SIZE); 295 296 skb_unlink(skb, q); 297 298 entry = (struct skb_data *)skb->cb; 299 memset(entry, 0, sizeof(*entry)); 300 entry->len = pkt_len; 301 302 if (netif_msg_pktdata(ax_local)) { 303 char pfx[IFNAMSIZ + 7]; 304 305 snprintf(pfx, sizeof(pfx), "%s: ", ndev->name); 306 307 netdev_info(ndev, "TX packet len %d, total len %d, seq %d\n", 308 pkt_len, skb->len, seq_num); 309 310 netdev_info(ndev, " SPI Header:\n"); 311 print_hex_dump(KERN_INFO, pfx, DUMP_PREFIX_OFFSET, 16, 1, 312 skb->data, 4, 0); 313 314 netdev_info(ndev, " TX SOP:\n"); 315 print_hex_dump(KERN_INFO, pfx, DUMP_PREFIX_OFFSET, 16, 1, 316 skb->data + 4, TX_OVERHEAD, 0); 317 318 netdev_info(ndev, " TX packet:\n"); 319 print_hex_dump(KERN_INFO, pfx, DUMP_PREFIX_OFFSET, 16, 1, 320 skb->data + 4 + TX_OVERHEAD, 321 skb->len - TX_EOP_SIZE - 4 - TX_OVERHEAD, 0); 322 323 netdev_info(ndev, " TX EOP:\n"); 324 print_hex_dump(KERN_INFO, pfx, DUMP_PREFIX_OFFSET, 16, 1, 325 skb->data + skb->len - 4, 4, 0); 326 } 327 328 return skb; 329 } 330 331 static int ax88796c_hard_xmit(struct ax88796c_device *ax_local) 332 { 333 struct ax88796c_pcpu_stats *stats; 334 struct sk_buff *tx_skb; 335 struct skb_data *entry; 336 unsigned long flags; 337 338 lockdep_assert_held(&ax_local->spi_lock); 339 340 stats = this_cpu_ptr(ax_local->stats); 341 tx_skb = ax88796c_tx_fixup(ax_local->ndev, &ax_local->tx_wait_q); 342 343 if (!tx_skb) { 344 this_cpu_inc(ax_local->stats->tx_dropped); 345 return 0; 346 } 347 entry = (struct skb_data *)tx_skb->cb; 348 349 AX_WRITE(&ax_local->ax_spi, 350 (TSNR_TXB_START | TSNR_PKT_CNT(1)), P0_TSNR); 351 352 axspi_write_txq(&ax_local->ax_spi, tx_skb->data, tx_skb->len); 353 354 if (((AX_READ(&ax_local->ax_spi, P0_TSNR) & TXNR_TXB_IDLE) == 0) || 355 ((ISR_TXERR & AX_READ(&ax_local->ax_spi, P0_ISR)) != 0)) { 356 /* Ack tx error int */ 357 AX_WRITE(&ax_local->ax_spi, ISR_TXERR, P0_ISR); 358 359 this_cpu_inc(ax_local->stats->tx_dropped); 360 361 if (net_ratelimit()) 362 netif_err(ax_local, tx_err, ax_local->ndev, 363 "TX FIFO error, re-initialize the TX bridge\n"); 364 365 /* Reinitial tx bridge */ 366 AX_WRITE(&ax_local->ax_spi, TXNR_TXB_REINIT | 367 AX_READ(&ax_local->ax_spi, P0_TSNR), P0_TSNR); 368 ax_local->seq_num = 0; 369 } else { 370 flags = u64_stats_update_begin_irqsave(&stats->syncp); 371 u64_stats_inc(&stats->tx_packets); 372 u64_stats_add(&stats->tx_bytes, entry->len); 373 u64_stats_update_end_irqrestore(&stats->syncp, flags); 374 } 375 376 entry->state = tx_done; 377 dev_kfree_skb(tx_skb); 378 379 return 1; 380 } 381 382 static int 383 ax88796c_start_xmit(struct sk_buff *skb, struct net_device *ndev) 384 { 385 struct ax88796c_device *ax_local = to_ax88796c_device(ndev); 386 387 skb_queue_tail(&ax_local->tx_wait_q, skb); 388 if (skb_queue_len(&ax_local->tx_wait_q) > TX_QUEUE_HIGH_WATER) 389 netif_stop_queue(ndev); 390 391 set_bit(EVENT_TX, &ax_local->flags); 392 schedule_work(&ax_local->ax_work); 393 394 return NETDEV_TX_OK; 395 } 396 397 static void 398 ax88796c_skb_return(struct ax88796c_device *ax_local, 399 struct sk_buff *skb, struct rx_header *rxhdr) 400 { 401 struct net_device *ndev = ax_local->ndev; 402 struct ax88796c_pcpu_stats *stats; 403 unsigned long flags; 404 int status; 405 406 stats = this_cpu_ptr(ax_local->stats); 407 408 do { 409 if (!(ndev->features & NETIF_F_RXCSUM)) 410 break; 411 412 /* checksum error bit is set */ 413 if ((rxhdr->flags & RX_HDR3_L3_ERR) || 414 (rxhdr->flags & RX_HDR3_L4_ERR)) 415 break; 416 417 /* Other types may be indicated by more than one bit. */ 418 if ((rxhdr->flags & RX_HDR3_L4_TYPE_TCP) || 419 (rxhdr->flags & RX_HDR3_L4_TYPE_UDP)) 420 skb->ip_summed = CHECKSUM_UNNECESSARY; 421 } while (0); 422 423 flags = u64_stats_update_begin_irqsave(&stats->syncp); 424 u64_stats_inc(&stats->rx_packets); 425 u64_stats_add(&stats->rx_bytes, skb->len); 426 u64_stats_update_end_irqrestore(&stats->syncp, flags); 427 428 skb->dev = ndev; 429 skb->protocol = eth_type_trans(skb, ax_local->ndev); 430 431 netif_info(ax_local, rx_status, ndev, "< rx, len %zu, type 0x%x\n", 432 skb->len + sizeof(struct ethhdr), skb->protocol); 433 434 status = netif_rx_ni(skb); 435 if (status != NET_RX_SUCCESS && net_ratelimit()) 436 netif_info(ax_local, rx_err, ndev, 437 "netif_rx status %d\n", status); 438 } 439 440 static void 441 ax88796c_rx_fixup(struct ax88796c_device *ax_local, struct sk_buff *rx_skb) 442 { 443 struct rx_header *rxhdr = (struct rx_header *)rx_skb->data; 444 struct net_device *ndev = ax_local->ndev; 445 u16 len; 446 447 be16_to_cpus(&rxhdr->flags_len); 448 be16_to_cpus(&rxhdr->seq_lenbar); 449 be16_to_cpus(&rxhdr->flags); 450 451 if ((rxhdr->flags_len & RX_HDR1_PKT_LEN) != 452 (~rxhdr->seq_lenbar & 0x7FF)) { 453 netif_err(ax_local, rx_err, ndev, "Header error\n"); 454 455 this_cpu_inc(ax_local->stats->rx_frame_errors); 456 kfree_skb(rx_skb); 457 return; 458 } 459 460 if ((rxhdr->flags_len & RX_HDR1_MII_ERR) || 461 (rxhdr->flags_len & RX_HDR1_CRC_ERR)) { 462 netif_err(ax_local, rx_err, ndev, "CRC or MII error\n"); 463 464 this_cpu_inc(ax_local->stats->rx_crc_errors); 465 kfree_skb(rx_skb); 466 return; 467 } 468 469 len = rxhdr->flags_len & RX_HDR1_PKT_LEN; 470 if (netif_msg_pktdata(ax_local)) { 471 char pfx[IFNAMSIZ + 7]; 472 473 snprintf(pfx, sizeof(pfx), "%s: ", ndev->name); 474 netdev_info(ndev, "RX data, total len %d, packet len %d\n", 475 rx_skb->len, len); 476 477 netdev_info(ndev, " Dump RX packet header:"); 478 print_hex_dump(KERN_INFO, pfx, DUMP_PREFIX_OFFSET, 16, 1, 479 rx_skb->data, sizeof(*rxhdr), 0); 480 481 netdev_info(ndev, " Dump RX packet:"); 482 print_hex_dump(KERN_INFO, pfx, DUMP_PREFIX_OFFSET, 16, 1, 483 rx_skb->data + sizeof(*rxhdr), len, 0); 484 } 485 486 skb_pull(rx_skb, sizeof(*rxhdr)); 487 pskb_trim(rx_skb, len); 488 489 ax88796c_skb_return(ax_local, rx_skb, rxhdr); 490 } 491 492 static int ax88796c_receive(struct net_device *ndev) 493 { 494 struct ax88796c_device *ax_local = to_ax88796c_device(ndev); 495 struct skb_data *entry; 496 u16 w_count, pkt_len; 497 struct sk_buff *skb; 498 u8 pkt_cnt; 499 500 lockdep_assert_held(&ax_local->spi_lock); 501 502 /* check rx packet and total word count */ 503 AX_WRITE(&ax_local->ax_spi, AX_READ(&ax_local->ax_spi, P0_RTWCR) 504 | RTWCR_RX_LATCH, P0_RTWCR); 505 506 pkt_cnt = AX_READ(&ax_local->ax_spi, P0_RXBCR2) & RXBCR2_PKT_MASK; 507 if (!pkt_cnt) 508 return 0; 509 510 pkt_len = AX_READ(&ax_local->ax_spi, P0_RCPHR) & 0x7FF; 511 512 w_count = round_up(pkt_len + 6, 4) >> 1; 513 514 skb = netdev_alloc_skb(ndev, w_count * 2); 515 if (!skb) { 516 AX_WRITE(&ax_local->ax_spi, RXBCR1_RXB_DISCARD, P0_RXBCR1); 517 this_cpu_inc(ax_local->stats->rx_dropped); 518 return 0; 519 } 520 entry = (struct skb_data *)skb->cb; 521 522 AX_WRITE(&ax_local->ax_spi, RXBCR1_RXB_START | w_count, P0_RXBCR1); 523 524 axspi_read_rxq(&ax_local->ax_spi, 525 skb_put(skb, w_count * 2), skb->len); 526 527 /* Check if rx bridge is idle */ 528 if ((AX_READ(&ax_local->ax_spi, P0_RXBCR2) & RXBCR2_RXB_IDLE) == 0) { 529 if (net_ratelimit()) 530 netif_err(ax_local, rx_err, ndev, 531 "Rx Bridge is not idle\n"); 532 AX_WRITE(&ax_local->ax_spi, RXBCR2_RXB_REINIT, P0_RXBCR2); 533 534 entry->state = rx_err; 535 } else { 536 entry->state = rx_done; 537 } 538 539 AX_WRITE(&ax_local->ax_spi, ISR_RXPKT, P0_ISR); 540 541 ax88796c_rx_fixup(ax_local, skb); 542 543 return 1; 544 } 545 546 static int ax88796c_process_isr(struct ax88796c_device *ax_local) 547 { 548 struct net_device *ndev = ax_local->ndev; 549 int todo = 0; 550 u16 isr; 551 552 lockdep_assert_held(&ax_local->spi_lock); 553 554 isr = AX_READ(&ax_local->ax_spi, P0_ISR); 555 AX_WRITE(&ax_local->ax_spi, isr, P0_ISR); 556 557 netif_dbg(ax_local, intr, ndev, " ISR 0x%04x\n", isr); 558 559 if (isr & ISR_TXERR) { 560 netif_dbg(ax_local, intr, ndev, " TXERR interrupt\n"); 561 AX_WRITE(&ax_local->ax_spi, TXNR_TXB_REINIT, P0_TSNR); 562 ax_local->seq_num = 0x1f; 563 } 564 565 if (isr & ISR_TXPAGES) { 566 netif_dbg(ax_local, intr, ndev, " TXPAGES interrupt\n"); 567 set_bit(EVENT_TX, &ax_local->flags); 568 } 569 570 if (isr & ISR_LINK) { 571 netif_dbg(ax_local, intr, ndev, " Link change interrupt\n"); 572 phy_mac_interrupt(ax_local->ndev->phydev); 573 } 574 575 if (isr & ISR_RXPKT) { 576 netif_dbg(ax_local, intr, ndev, " RX interrupt\n"); 577 todo = ax88796c_receive(ax_local->ndev); 578 } 579 580 return todo; 581 } 582 583 static irqreturn_t ax88796c_interrupt(int irq, void *dev_instance) 584 { 585 struct ax88796c_device *ax_local; 586 struct net_device *ndev; 587 588 ndev = dev_instance; 589 if (!ndev) { 590 pr_err("irq %d for unknown device.\n", irq); 591 return IRQ_RETVAL(0); 592 } 593 ax_local = to_ax88796c_device(ndev); 594 595 disable_irq_nosync(irq); 596 597 netif_dbg(ax_local, intr, ndev, "Interrupt occurred\n"); 598 599 set_bit(EVENT_INTR, &ax_local->flags); 600 schedule_work(&ax_local->ax_work); 601 602 return IRQ_HANDLED; 603 } 604 605 static void ax88796c_work(struct work_struct *work) 606 { 607 struct ax88796c_device *ax_local = 608 container_of(work, struct ax88796c_device, ax_work); 609 610 mutex_lock(&ax_local->spi_lock); 611 612 if (test_bit(EVENT_SET_MULTI, &ax_local->flags)) { 613 ax88796c_set_hw_multicast(ax_local->ndev); 614 clear_bit(EVENT_SET_MULTI, &ax_local->flags); 615 } 616 617 if (test_bit(EVENT_INTR, &ax_local->flags)) { 618 AX_WRITE(&ax_local->ax_spi, IMR_MASKALL, P0_IMR); 619 620 while (ax88796c_process_isr(ax_local)) 621 /* nothing */; 622 623 clear_bit(EVENT_INTR, &ax_local->flags); 624 625 AX_WRITE(&ax_local->ax_spi, IMR_DEFAULT, P0_IMR); 626 627 enable_irq(ax_local->ndev->irq); 628 } 629 630 if (test_bit(EVENT_TX, &ax_local->flags)) { 631 while (skb_queue_len(&ax_local->tx_wait_q)) { 632 if (!ax88796c_hard_xmit(ax_local)) 633 break; 634 } 635 636 clear_bit(EVENT_TX, &ax_local->flags); 637 638 if (netif_queue_stopped(ax_local->ndev) && 639 (skb_queue_len(&ax_local->tx_wait_q) < TX_QUEUE_LOW_WATER)) 640 netif_wake_queue(ax_local->ndev); 641 } 642 643 mutex_unlock(&ax_local->spi_lock); 644 } 645 646 static void ax88796c_get_stats64(struct net_device *ndev, 647 struct rtnl_link_stats64 *stats) 648 { 649 struct ax88796c_device *ax_local = to_ax88796c_device(ndev); 650 u32 rx_frame_errors = 0, rx_crc_errors = 0; 651 u32 rx_dropped = 0, tx_dropped = 0; 652 unsigned int start; 653 int cpu; 654 655 for_each_possible_cpu(cpu) { 656 struct ax88796c_pcpu_stats *s; 657 u64 rx_packets, rx_bytes; 658 u64 tx_packets, tx_bytes; 659 660 s = per_cpu_ptr(ax_local->stats, cpu); 661 662 do { 663 start = u64_stats_fetch_begin_irq(&s->syncp); 664 rx_packets = u64_stats_read(&s->rx_packets); 665 rx_bytes = u64_stats_read(&s->rx_bytes); 666 tx_packets = u64_stats_read(&s->tx_packets); 667 tx_bytes = u64_stats_read(&s->tx_bytes); 668 } while (u64_stats_fetch_retry_irq(&s->syncp, start)); 669 670 stats->rx_packets += rx_packets; 671 stats->rx_bytes += rx_bytes; 672 stats->tx_packets += tx_packets; 673 stats->tx_bytes += tx_bytes; 674 675 rx_dropped += s->rx_dropped; 676 tx_dropped += s->tx_dropped; 677 rx_frame_errors += s->rx_frame_errors; 678 rx_crc_errors += s->rx_crc_errors; 679 } 680 681 stats->rx_dropped = rx_dropped; 682 stats->tx_dropped = tx_dropped; 683 stats->rx_frame_errors = rx_frame_errors; 684 stats->rx_crc_errors = rx_crc_errors; 685 } 686 687 static void ax88796c_set_mac(struct ax88796c_device *ax_local) 688 { 689 u16 maccr; 690 691 maccr = (ax_local->link) ? MACCR_RXEN : 0; 692 693 switch (ax_local->speed) { 694 case SPEED_100: 695 maccr |= MACCR_SPEED_100; 696 break; 697 case SPEED_10: 698 case SPEED_UNKNOWN: 699 break; 700 default: 701 return; 702 } 703 704 switch (ax_local->duplex) { 705 case DUPLEX_FULL: 706 maccr |= MACCR_SPEED_100; 707 break; 708 case DUPLEX_HALF: 709 case DUPLEX_UNKNOWN: 710 break; 711 default: 712 return; 713 } 714 715 if (ax_local->flowctrl & AX_FC_ANEG && 716 ax_local->phydev->autoneg) { 717 maccr |= ax_local->pause ? MACCR_RXFC_ENABLE : 0; 718 maccr |= !ax_local->pause != !ax_local->asym_pause ? 719 MACCR_TXFC_ENABLE : 0; 720 } else { 721 maccr |= (ax_local->flowctrl & AX_FC_RX) ? MACCR_RXFC_ENABLE : 0; 722 maccr |= (ax_local->flowctrl & AX_FC_TX) ? MACCR_TXFC_ENABLE : 0; 723 } 724 725 mutex_lock(&ax_local->spi_lock); 726 727 maccr |= AX_READ(&ax_local->ax_spi, P0_MACCR) & 728 ~(MACCR_DUPLEX_FULL | MACCR_SPEED_100 | 729 MACCR_TXFC_ENABLE | MACCR_RXFC_ENABLE); 730 AX_WRITE(&ax_local->ax_spi, maccr, P0_MACCR); 731 732 mutex_unlock(&ax_local->spi_lock); 733 } 734 735 static void ax88796c_handle_link_change(struct net_device *ndev) 736 { 737 struct ax88796c_device *ax_local = to_ax88796c_device(ndev); 738 struct phy_device *phydev = ndev->phydev; 739 bool update = false; 740 741 if (phydev->link && (ax_local->speed != phydev->speed || 742 ax_local->duplex != phydev->duplex || 743 ax_local->pause != phydev->pause || 744 ax_local->asym_pause != phydev->asym_pause)) { 745 ax_local->speed = phydev->speed; 746 ax_local->duplex = phydev->duplex; 747 ax_local->pause = phydev->pause; 748 ax_local->asym_pause = phydev->asym_pause; 749 update = true; 750 } 751 752 if (phydev->link != ax_local->link) { 753 if (!phydev->link) { 754 ax_local->speed = SPEED_UNKNOWN; 755 ax_local->duplex = DUPLEX_UNKNOWN; 756 } 757 758 ax_local->link = phydev->link; 759 update = true; 760 } 761 762 if (update) 763 ax88796c_set_mac(ax_local); 764 765 if (net_ratelimit()) 766 phy_print_status(ndev->phydev); 767 } 768 769 static void ax88796c_set_csums(struct ax88796c_device *ax_local) 770 { 771 struct net_device *ndev = ax_local->ndev; 772 773 lockdep_assert_held(&ax_local->spi_lock); 774 775 if (ndev->features & NETIF_F_RXCSUM) { 776 AX_WRITE(&ax_local->ax_spi, COERCR0_DEFAULT, P4_COERCR0); 777 AX_WRITE(&ax_local->ax_spi, COERCR1_DEFAULT, P4_COERCR1); 778 } else { 779 AX_WRITE(&ax_local->ax_spi, 0, P4_COERCR0); 780 AX_WRITE(&ax_local->ax_spi, 0, P4_COERCR1); 781 } 782 783 if (ndev->features & NETIF_F_HW_CSUM) { 784 AX_WRITE(&ax_local->ax_spi, COETCR0_DEFAULT, P4_COETCR0); 785 AX_WRITE(&ax_local->ax_spi, COETCR1_TXPPPE, P4_COETCR1); 786 } else { 787 AX_WRITE(&ax_local->ax_spi, 0, P4_COETCR0); 788 AX_WRITE(&ax_local->ax_spi, 0, P4_COETCR1); 789 } 790 } 791 792 static int 793 ax88796c_open(struct net_device *ndev) 794 { 795 struct ax88796c_device *ax_local = to_ax88796c_device(ndev); 796 unsigned long irq_flag = 0; 797 int fc = AX_FC_NONE; 798 int ret; 799 u16 t; 800 801 ret = request_irq(ndev->irq, ax88796c_interrupt, 802 irq_flag, ndev->name, ndev); 803 if (ret) { 804 netdev_err(ndev, "unable to get IRQ %d (errno=%d).\n", 805 ndev->irq, ret); 806 return ret; 807 } 808 809 mutex_lock(&ax_local->spi_lock); 810 811 ret = ax88796c_soft_reset(ax_local); 812 if (ret < 0) { 813 free_irq(ndev->irq, ndev); 814 mutex_unlock(&ax_local->spi_lock); 815 return ret; 816 } 817 ax_local->seq_num = 0x1f; 818 819 ax88796c_set_mac_addr(ndev); 820 ax88796c_set_csums(ax_local); 821 822 /* Disable stuffing packet */ 823 t = AX_READ(&ax_local->ax_spi, P1_RXBSPCR); 824 t &= ~RXBSPCR_STUF_ENABLE; 825 AX_WRITE(&ax_local->ax_spi, t, P1_RXBSPCR); 826 827 /* Enable RX packet process */ 828 AX_WRITE(&ax_local->ax_spi, RPPER_RXEN, P1_RPPER); 829 830 t = AX_READ(&ax_local->ax_spi, P0_FER); 831 t |= FER_RXEN | FER_TXEN | FER_BSWAP | FER_IRQ_PULL; 832 AX_WRITE(&ax_local->ax_spi, t, P0_FER); 833 834 /* Setup LED mode */ 835 AX_WRITE(&ax_local->ax_spi, 836 (LCR_LED0_EN | LCR_LED0_DUPLEX | LCR_LED1_EN | 837 LCR_LED1_100MODE), P2_LCR0); 838 AX_WRITE(&ax_local->ax_spi, 839 (AX_READ(&ax_local->ax_spi, P2_LCR1) & LCR_LED2_MASK) | 840 LCR_LED2_EN | LCR_LED2_LINK, P2_LCR1); 841 842 /* Disable PHY auto-polling */ 843 AX_WRITE(&ax_local->ax_spi, PCR_PHYID(AX88796C_PHY_ID), P2_PCR); 844 845 /* Enable MAC interrupts */ 846 AX_WRITE(&ax_local->ax_spi, IMR_DEFAULT, P0_IMR); 847 848 mutex_unlock(&ax_local->spi_lock); 849 850 /* Setup flow-control configuration */ 851 phy_support_asym_pause(ax_local->phydev); 852 853 if (linkmode_test_bit(ETHTOOL_LINK_MODE_Pause_BIT, 854 ax_local->phydev->advertising) || 855 linkmode_test_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT, 856 ax_local->phydev->advertising)) 857 fc |= AX_FC_ANEG; 858 859 fc |= linkmode_test_bit(ETHTOOL_LINK_MODE_Pause_BIT, 860 ax_local->phydev->advertising) ? AX_FC_RX : 0; 861 fc |= (linkmode_test_bit(ETHTOOL_LINK_MODE_Pause_BIT, 862 ax_local->phydev->advertising) != 863 linkmode_test_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT, 864 ax_local->phydev->advertising)) ? AX_FC_TX : 0; 865 ax_local->flowctrl = fc; 866 867 phy_start(ax_local->ndev->phydev); 868 869 netif_start_queue(ndev); 870 871 spi_message_init(&ax_local->ax_spi.rx_msg); 872 873 return 0; 874 } 875 876 static int 877 ax88796c_close(struct net_device *ndev) 878 { 879 struct ax88796c_device *ax_local = to_ax88796c_device(ndev); 880 881 phy_stop(ndev->phydev); 882 883 /* We lock the mutex early not only to protect the device 884 * against concurrent access, but also avoid waking up the 885 * queue in ax88796c_work(). phy_stop() needs to be called 886 * before because it locks the mutex to access SPI. 887 */ 888 mutex_lock(&ax_local->spi_lock); 889 890 netif_stop_queue(ndev); 891 892 /* No more work can be scheduled now. Make any pending work, 893 * including one already waiting for the mutex to be unlocked, 894 * NOP. 895 */ 896 netif_dbg(ax_local, ifdown, ndev, "clearing bits\n"); 897 clear_bit(EVENT_SET_MULTI, &ax_local->flags); 898 clear_bit(EVENT_INTR, &ax_local->flags); 899 clear_bit(EVENT_TX, &ax_local->flags); 900 901 /* Disable MAC interrupts */ 902 AX_WRITE(&ax_local->ax_spi, IMR_MASKALL, P0_IMR); 903 __skb_queue_purge(&ax_local->tx_wait_q); 904 ax88796c_soft_reset(ax_local); 905 906 mutex_unlock(&ax_local->spi_lock); 907 908 cancel_work_sync(&ax_local->ax_work); 909 910 free_irq(ndev->irq, ndev); 911 912 return 0; 913 } 914 915 static int 916 ax88796c_set_features(struct net_device *ndev, netdev_features_t features) 917 { 918 struct ax88796c_device *ax_local = to_ax88796c_device(ndev); 919 netdev_features_t changed = features ^ ndev->features; 920 921 if (!(changed & (NETIF_F_RXCSUM | NETIF_F_HW_CSUM))) 922 return 0; 923 924 ndev->features = features; 925 926 if (changed & (NETIF_F_RXCSUM | NETIF_F_HW_CSUM)) 927 ax88796c_set_csums(ax_local); 928 929 return 0; 930 } 931 932 static const struct net_device_ops ax88796c_netdev_ops = { 933 .ndo_open = ax88796c_open, 934 .ndo_stop = ax88796c_close, 935 .ndo_start_xmit = ax88796c_start_xmit, 936 .ndo_get_stats64 = ax88796c_get_stats64, 937 .ndo_eth_ioctl = ax88796c_ioctl, 938 .ndo_set_mac_address = eth_mac_addr, 939 .ndo_set_features = ax88796c_set_features, 940 }; 941 942 static int ax88796c_hard_reset(struct ax88796c_device *ax_local) 943 { 944 struct device *dev = (struct device *)&ax_local->spi->dev; 945 struct gpio_desc *reset_gpio; 946 947 /* reset info */ 948 reset_gpio = gpiod_get(dev, "reset", 0); 949 if (IS_ERR(reset_gpio)) { 950 dev_err(dev, "Could not get 'reset' GPIO: %ld", PTR_ERR(reset_gpio)); 951 return PTR_ERR(reset_gpio); 952 } 953 954 /* set reset */ 955 gpiod_direction_output(reset_gpio, 1); 956 msleep(100); 957 gpiod_direction_output(reset_gpio, 0); 958 gpiod_put(reset_gpio); 959 msleep(20); 960 961 return 0; 962 } 963 964 static int ax88796c_probe(struct spi_device *spi) 965 { 966 char phy_id[MII_BUS_ID_SIZE + 3]; 967 struct ax88796c_device *ax_local; 968 struct net_device *ndev; 969 u16 temp; 970 int ret; 971 972 ndev = devm_alloc_etherdev(&spi->dev, sizeof(*ax_local)); 973 if (!ndev) 974 return -ENOMEM; 975 976 SET_NETDEV_DEV(ndev, &spi->dev); 977 978 ax_local = to_ax88796c_device(ndev); 979 980 dev_set_drvdata(&spi->dev, ax_local); 981 ax_local->spi = spi; 982 ax_local->ax_spi.spi = spi; 983 984 ax_local->stats = 985 devm_netdev_alloc_pcpu_stats(&spi->dev, 986 struct ax88796c_pcpu_stats); 987 if (!ax_local->stats) 988 return -ENOMEM; 989 990 ax_local->ndev = ndev; 991 ax_local->priv_flags |= comp ? AX_CAP_COMP : 0; 992 ax_local->msg_enable = msg_enable; 993 mutex_init(&ax_local->spi_lock); 994 995 ax_local->mdiobus = devm_mdiobus_alloc(&spi->dev); 996 if (!ax_local->mdiobus) 997 return -ENOMEM; 998 999 ax_local->mdiobus->priv = ax_local; 1000 ax_local->mdiobus->read = ax88796c_mdio_read; 1001 ax_local->mdiobus->write = ax88796c_mdio_write; 1002 ax_local->mdiobus->name = "ax88976c-mdiobus"; 1003 ax_local->mdiobus->phy_mask = (u32)~BIT(AX88796C_PHY_ID); 1004 ax_local->mdiobus->parent = &spi->dev; 1005 1006 snprintf(ax_local->mdiobus->id, MII_BUS_ID_SIZE, 1007 "ax88796c-%s.%u", dev_name(&spi->dev), spi->chip_select); 1008 1009 ret = devm_mdiobus_register(&spi->dev, ax_local->mdiobus); 1010 if (ret < 0) { 1011 dev_err(&spi->dev, "Could not register MDIO bus\n"); 1012 return ret; 1013 } 1014 1015 if (netif_msg_probe(ax_local)) { 1016 dev_info(&spi->dev, "AX88796C-SPI Configuration:\n"); 1017 dev_info(&spi->dev, " Compression : %s\n", 1018 ax_local->priv_flags & AX_CAP_COMP ? "ON" : "OFF"); 1019 } 1020 1021 ndev->irq = spi->irq; 1022 ndev->netdev_ops = &ax88796c_netdev_ops; 1023 ndev->ethtool_ops = &ax88796c_ethtool_ops; 1024 ndev->hw_features |= NETIF_F_HW_CSUM | NETIF_F_RXCSUM; 1025 ndev->features |= NETIF_F_HW_CSUM | NETIF_F_RXCSUM; 1026 ndev->needed_headroom = TX_OVERHEAD; 1027 ndev->needed_tailroom = TX_EOP_SIZE; 1028 1029 mutex_lock(&ax_local->spi_lock); 1030 1031 /* ax88796c gpio reset */ 1032 ax88796c_hard_reset(ax_local); 1033 1034 /* Reset AX88796C */ 1035 ret = ax88796c_soft_reset(ax_local); 1036 if (ret < 0) { 1037 ret = -ENODEV; 1038 mutex_unlock(&ax_local->spi_lock); 1039 goto err; 1040 } 1041 /* Check board revision */ 1042 temp = AX_READ(&ax_local->ax_spi, P2_CRIR); 1043 if ((temp & 0xF) != 0x0) { 1044 dev_err(&spi->dev, "spi read failed: %d\n", temp); 1045 ret = -ENODEV; 1046 mutex_unlock(&ax_local->spi_lock); 1047 goto err; 1048 } 1049 1050 /*Reload EEPROM*/ 1051 ax88796c_reload_eeprom(ax_local); 1052 1053 ax88796c_load_mac_addr(ndev); 1054 1055 if (netif_msg_probe(ax_local)) 1056 dev_info(&spi->dev, 1057 "irq %d, MAC addr %02X:%02X:%02X:%02X:%02X:%02X\n", 1058 ndev->irq, 1059 ndev->dev_addr[0], ndev->dev_addr[1], 1060 ndev->dev_addr[2], ndev->dev_addr[3], 1061 ndev->dev_addr[4], ndev->dev_addr[5]); 1062 1063 /* Disable power saving */ 1064 AX_WRITE(&ax_local->ax_spi, (AX_READ(&ax_local->ax_spi, P0_PSCR) 1065 & PSCR_PS_MASK) | PSCR_PS_D0, P0_PSCR); 1066 1067 mutex_unlock(&ax_local->spi_lock); 1068 1069 INIT_WORK(&ax_local->ax_work, ax88796c_work); 1070 1071 skb_queue_head_init(&ax_local->tx_wait_q); 1072 1073 snprintf(phy_id, MII_BUS_ID_SIZE + 3, PHY_ID_FMT, 1074 ax_local->mdiobus->id, AX88796C_PHY_ID); 1075 ax_local->phydev = phy_connect(ax_local->ndev, phy_id, 1076 ax88796c_handle_link_change, 1077 PHY_INTERFACE_MODE_MII); 1078 if (IS_ERR(ax_local->phydev)) { 1079 ret = PTR_ERR(ax_local->phydev); 1080 goto err; 1081 } 1082 ax_local->phydev->irq = PHY_POLL; 1083 1084 ret = devm_register_netdev(&spi->dev, ndev); 1085 if (ret) { 1086 dev_err(&spi->dev, "failed to register a network device\n"); 1087 goto err_phy_dis; 1088 } 1089 1090 netif_info(ax_local, probe, ndev, "%s %s registered\n", 1091 dev_driver_string(&spi->dev), 1092 dev_name(&spi->dev)); 1093 phy_attached_info(ax_local->phydev); 1094 1095 return 0; 1096 1097 err_phy_dis: 1098 phy_disconnect(ax_local->phydev); 1099 err: 1100 return ret; 1101 } 1102 1103 static int ax88796c_remove(struct spi_device *spi) 1104 { 1105 struct ax88796c_device *ax_local = dev_get_drvdata(&spi->dev); 1106 struct net_device *ndev = ax_local->ndev; 1107 1108 phy_disconnect(ndev->phydev); 1109 1110 netif_info(ax_local, probe, ndev, "removing network device %s %s\n", 1111 dev_driver_string(&spi->dev), 1112 dev_name(&spi->dev)); 1113 1114 return 0; 1115 } 1116 1117 #ifdef CONFIG_OF 1118 static const struct of_device_id ax88796c_dt_ids[] = { 1119 { .compatible = "asix,ax88796c" }, 1120 {}, 1121 }; 1122 MODULE_DEVICE_TABLE(of, ax88796c_dt_ids); 1123 #endif 1124 1125 static const struct spi_device_id asix_id[] = { 1126 { "ax88796c", 0 }, 1127 { } 1128 }; 1129 MODULE_DEVICE_TABLE(spi, asix_id); 1130 1131 static struct spi_driver ax88796c_spi_driver = { 1132 .driver = { 1133 .name = DRV_NAME, 1134 .of_match_table = of_match_ptr(ax88796c_dt_ids), 1135 }, 1136 .probe = ax88796c_probe, 1137 .remove = ax88796c_remove, 1138 .id_table = asix_id, 1139 }; 1140 1141 static __init int ax88796c_spi_init(void) 1142 { 1143 int ret; 1144 1145 bitmap_zero(ax88796c_no_regs_mask, AX88796C_REGDUMP_LEN); 1146 ret = bitmap_parse(no_regs_list, 35, 1147 ax88796c_no_regs_mask, AX88796C_REGDUMP_LEN); 1148 if (ret) { 1149 bitmap_fill(ax88796c_no_regs_mask, AX88796C_REGDUMP_LEN); 1150 pr_err("Invalid bitmap description, masking all registers\n"); 1151 } 1152 1153 return spi_register_driver(&ax88796c_spi_driver); 1154 } 1155 1156 static __exit void ax88796c_spi_exit(void) 1157 { 1158 spi_unregister_driver(&ax88796c_spi_driver); 1159 } 1160 1161 module_init(ax88796c_spi_init); 1162 module_exit(ax88796c_spi_exit); 1163 1164 MODULE_AUTHOR("Łukasz Stelmach <l.stelmach@samsung.com>"); 1165 MODULE_DESCRIPTION("ASIX AX88796C SPI Ethernet driver"); 1166 MODULE_LICENSE("GPL"); 1167