Lines Matching +full:tx +full:- +full:mailbox +full:- +full:count
1 // SPDX-License-Identifier: GPL-2.0-only
3 * at91_can.c - CAN network driver for AT91 SoC CAN controller
6 * (C) 2008, 2009, 2010, 2011, 2023 by Marc Kleine-Budde <kernel@pengutronix.de>
29 #include <linux/can/rx-offload.h>
31 #define AT91_MB_MASK(i) ((1 << (i)) - 1)
48 /* Mailbox registers (0 <= i <= 15) */
106 /* Mailbox Modes */
202 return priv->devtype_data.type == AT91_DEVTYPE_SAM##_model; \
210 return priv->devtype_data.rx_first; in get_mb_rx_first()
215 return priv->devtype_data.rx_last; in get_mb_rx_last()
220 return priv->devtype_data.tx_shift; in get_mb_tx_shift()
235 return get_mb_tx_first(priv) + get_mb_tx_num(priv) - 1; in get_mb_tx_last()
272 return (priv->tx_head & get_head_mb_mask(priv)) + get_mb_tx_first(priv); in get_tx_head_mb()
277 return (priv->tx_head >> get_head_prio_shift(priv)) & 0xf; in get_tx_head_prio()
282 return (priv->tx_tail & get_head_mb_mask(priv)) + get_mb_tx_first(priv); in get_tx_tail_mb()
287 return readl_relaxed(priv->reg_base + reg); in at91_read()
293 writel_relaxed(value, priv->reg_base + reg); in at91_write()
332 * mailbox is disabled. The next mailboxes are used as a in at91_setup_mailboxes()
337 reg_mid = at91_can_id_to_reg_mid(priv->mb0_id); in at91_setup_mailboxes()
358 /* Reset tx helper pointers */ in at91_setup_mailboxes()
359 priv->tx_head = priv->tx_tail = 0; in at91_setup_mailboxes()
365 const struct can_bittiming *bt = &priv->can.bittiming; in at91_set_bittiming()
368 if (priv->can.ctrlmode & CAN_CTRLMODE_3_SAMPLES) in at91_set_bittiming()
371 reg_br |= FIELD_PREP(AT91_BR_BRP_MASK, bt->brp - 1) | in at91_set_bittiming()
372 FIELD_PREP(AT91_BR_SJW_MASK, bt->sjw - 1) | in at91_set_bittiming()
373 FIELD_PREP(AT91_BR_PROPAG_MASK, bt->prop_seg - 1) | in at91_set_bittiming()
374 FIELD_PREP(AT91_BR_PHASE1_MASK, bt->phase_seg1 - 1) | in at91_set_bittiming()
375 FIELD_PREP(AT91_BR_PHASE2_MASK, bt->phase_seg2 - 1); in at91_set_bittiming()
390 bec->rxerr = FIELD_GET(AT91_ECR_REC_MASK, reg_ecr); in at91_get_berr_counter()
391 bec->txerr = FIELD_GET(AT91_ECR_TEC_MASK, reg_ecr); in at91_get_berr_counter()
412 if (priv->can.ctrlmode & CAN_CTRLMODE_LISTENONLY) in at91_chip_start()
418 priv->can.state = CAN_STATE_ERROR_ACTIVE; in at91_chip_start()
435 /* Abort any pending TX requests. However this doesn't seem to in at91_chip_stop()
436 * work in case of bus-off on sama5d3. in at91_chip_stop()
446 priv->can.state = state; in at91_chip_stop()
453 * message of the mailbox with the lowest number is sent first.
455 * We use the first TX mailbox (AT91_MB_TX_FIRST) with prio 0, then
456 * the next mailbox with prio 0, and so on, until all mailboxes are
457 * used. Then we start from the beginning with mailbox
458 * AT91_MB_TX_FIRST, but with prio 1, mailbox AT91_MB_TX_FIRST + 1
459 * prio 1. When we reach the last mailbox with prio 15, we have to
461 * again with mailbox AT91_MB_TX_FIRST prio 0.
463 * We use the priv->tx_head as counter for the next transmission
464 * mailbox, but without the offset AT91_MB_TX_FIRST. The lower bits
465 * encode the mailbox number, the upper 4 bits the mailbox priority:
467 * priv->tx_head = (prio << get_next_prio_shift(priv)) |
468 * (mb - get_mb_tx_first(priv));
474 struct can_frame *cf = (struct can_frame *)skb->data; in at91_start_xmit()
487 netdev_err(dev, "BUG! TX buffer full when queue awake!\n"); in at91_start_xmit()
490 reg_mid = at91_can_id_to_reg_mid(cf->can_id); in at91_start_xmit()
492 reg_mcr = FIELD_PREP(AT91_MCR_MDLC_MASK, cf->len) | in at91_start_xmit()
495 if (cf->can_id & CAN_RTR_FLAG) in at91_start_xmit()
503 at91_write(priv, AT91_MDL(mb), *(u32 *)(cf->data + 0)); in at91_start_xmit()
504 at91_write(priv, AT91_MDH(mb), *(u32 *)(cf->data + 4)); in at91_start_xmit()
510 can_put_echo_skb(skb, dev, mb - get_mb_tx_first(priv), 0); in at91_start_xmit()
514 * tx_head buffer prio and mailbox equals 0. in at91_start_xmit()
519 priv->tx_head++; in at91_start_xmit()
522 (priv->tx_head & get_head_mask(priv)) == 0) in at91_start_xmit()
525 /* Enable interrupt for this mailbox */ in at91_start_xmit()
548 * at91_rx_overflow_err - send error frame due to rx overflow
553 struct net_device_stats *stats = &dev->stats; in at91_rx_overflow_err()
561 stats->rx_over_errors++; in at91_rx_overflow_err()
562 stats->rx_errors++; in at91_rx_overflow_err()
568 cf->can_id |= CAN_ERR_CRTL; in at91_rx_overflow_err()
569 cf->data[1] = CAN_ERR_CRTL_RX_OVERFLOW; in at91_rx_overflow_err()
571 err = can_rx_offload_queue_timestamp(&priv->offload, skb, timestamp); in at91_rx_overflow_err()
573 stats->rx_fifo_errors++; in at91_rx_overflow_err()
577 * at91_mailbox_read - read CAN msg from mailbox
578 * @offload: rx-offload
579 * @mb: mailbox number to read from
581 * @drop: true indicated mailbox to mark as read and drop frame
583 * Reads a CAN message from the given mailbox if not empty.
599 skb = ERR_PTR(-ENOBUFS); in at91_mailbox_read()
603 skb = alloc_can_skb(offload->dev, &cf); in at91_mailbox_read()
605 skb = ERR_PTR(-ENOMEM); in at91_mailbox_read()
611 cf->can_id = FIELD_GET(AT91_MID_MIDVA_MASK | AT91_MID_MIDVB_MASK, reg_mid) | in at91_mailbox_read()
614 cf->can_id = FIELD_GET(AT91_MID_MIDVA_MASK, reg_mid); in at91_mailbox_read()
619 cf->len = can_cc_dlc2len(FIELD_GET(AT91_MSR_MDLC_MASK, reg_msr)); in at91_mailbox_read()
622 cf->can_id |= CAN_RTR_FLAG; in at91_mailbox_read()
624 *(u32 *)(cf->data + 0) = at91_read(priv, AT91_MDL(mb)); in at91_mailbox_read()
625 *(u32 *)(cf->data + 4) = at91_read(priv, AT91_MDH(mb)); in at91_mailbox_read()
632 at91_rx_overflow_err(offload->dev); in at91_mailbox_read()
642 * priv->tx_tail holds the number of the oldest can_frame put for
643 * transmission into the hardware, but not yet ACKed by the CAN tx
646 * We iterate from priv->tx_tail to priv->tx_head and check if the
657 for (/* nix */; (priv->tx_head - priv->tx_tail) > 0; priv->tx_tail++) { in at91_irq_tx()
660 /* no event in mailbox? */ in at91_irq_tx()
664 /* Disable irq for this TX mailbox */ in at91_irq_tx()
667 /* only echo if mailbox signals us a transfer in at91_irq_tx()
678 dev->stats.tx_bytes += in at91_irq_tx()
679 can_get_echo_skb(dev, mb - get_mb_tx_first(priv), NULL); in at91_irq_tx()
680 dev->stats.tx_packets++; in at91_irq_tx()
684 * we get a TX int for the last can frame directly before a in at91_irq_tx()
687 if ((priv->tx_head & get_head_mask(priv)) != 0 || in at91_irq_tx()
688 (priv->tx_tail & get_head_mask(priv)) == 0) in at91_irq_tx()
694 struct net_device_stats *stats = &dev->stats; in at91_irq_err_line()
706 /* The chip automatically recovers from bus-off after 128 in at91_irq_err_line()
709 * After an auto-recovered bus-off, the error counters no in at91_irq_err_line()
715 * Take any latched bus-off information from the SR register in at91_irq_err_line()
725 if (likely(new_state == priv->can.state)) in at91_irq_err_line()
743 cf->can_id |= CAN_ERR_CNT; in at91_irq_err_line()
744 cf->data[6] = bec.txerr; in at91_irq_err_line()
745 cf->data[7] = bec.rxerr; in at91_irq_err_line()
748 err = can_rx_offload_queue_timestamp(&priv->offload, skb, timestamp); in at91_irq_err_line()
750 stats->rx_fifo_errors++; in at91_irq_err_line()
755 struct net_device_stats *stats = &dev->stats; in at91_irq_err_frame()
762 priv->can.can_stats.bus_error++; in at91_irq_err_frame()
766 cf->can_id |= CAN_ERR_PROT | CAN_ERR_BUSERROR; in at91_irq_err_frame()
771 stats->rx_errors++; in at91_irq_err_frame()
773 cf->data[3] |= CAN_ERR_PROT_LOC_CRC_SEQ; in at91_irq_err_frame()
779 stats->rx_errors++; in at91_irq_err_frame()
781 cf->data[2] |= CAN_ERR_PROT_STUFF; in at91_irq_err_frame()
787 stats->tx_errors++; in at91_irq_err_frame()
789 cf->can_id |= CAN_ERR_ACK; in at91_irq_err_frame()
790 cf->data[2] |= CAN_ERR_PROT_TX; in at91_irq_err_frame()
797 stats->rx_errors++; in at91_irq_err_frame()
799 cf->data[2] |= CAN_ERR_PROT_FORM; in at91_irq_err_frame()
805 stats->tx_errors++; in at91_irq_err_frame()
807 cf->data[2] |= CAN_ERR_PROT_TX | CAN_ERR_PROT_BIT; in at91_irq_err_frame()
813 err = can_rx_offload_queue_timestamp(&priv->offload, skb, timestamp); in at91_irq_err_frame()
815 stats->rx_fifo_errors++; in at91_irq_err_frame()
839 ret = can_rx_offload_irq_offload_timestamp(&priv->offload, in at91_irq()
855 priv->can.state > CAN_STATE_ERROR_ACTIVE) { in at91_irq()
867 can_rx_offload_irq_finish(&priv->offload); in at91_irq()
877 err = phy_power_on(priv->transceiver); in at91_open()
886 err = clk_prepare_enable(priv->clk); in at91_open()
891 err = request_irq(dev->irq, at91_irq, IRQF_SHARED, in at91_open()
892 dev->name, dev); in at91_open()
898 can_rx_offload_enable(&priv->offload); in at91_open()
904 clk_disable_unprepare(priv->clk); in at91_open()
908 phy_power_off(priv->transceiver); in at91_open()
920 can_rx_offload_disable(&priv->offload); in at91_close()
923 free_irq(dev->irq, dev); in at91_close()
924 clk_disable_unprepare(priv->clk); in at91_close()
925 phy_power_off(priv->transceiver); in at91_close()
941 return -EOPNOTSUPP; in at91_set_mode()
963 if (priv->mb0_id & CAN_EFF_FLAG) in mb0_id_show()
964 return sysfs_emit(buf, "0x%08x\n", priv->mb0_id); in mb0_id_show()
966 return sysfs_emit(buf, "0x%03x\n", priv->mb0_id); in mb0_id_show()
971 const char *buf, size_t count) in mb0_id_store() argument
981 if (ndev->flags & IFF_UP) { in mb0_id_store()
982 ret = -EBUSY; in mb0_id_store()
997 priv->mb0_id = can_id; in mb0_id_store()
998 ret = count; in mb0_id_store()
1019 .compatible = "atmel,at91sam9x5-can",
1022 .compatible = "atmel,at91sam9263-can",
1033 if (pdev->dev.of_node) { in at91_can_get_driver_data()
1036 match = of_match_node(at91_can_dt_ids, pdev->dev.of_node); in at91_can_get_driver_data()
1038 dev_err(&pdev->dev, "no matching node found in dtb\n"); in at91_can_get_driver_data()
1041 return (const struct at91_devtype_data *)match->data; in at91_can_get_driver_data()
1044 platform_get_device_id(pdev)->driver_data; in at91_can_get_driver_data()
1060 dev_err(&pdev->dev, "no driver data\n"); in at91_can_probe()
1061 err = -ENODEV; in at91_can_probe()
1065 clk = clk_get(&pdev->dev, "can_clk"); in at91_can_probe()
1067 dev_err(&pdev->dev, "no clock defined\n"); in at91_can_probe()
1068 err = -ENODEV; in at91_can_probe()
1075 err = -ENODEV; in at91_can_probe()
1079 if (!request_mem_region(res->start, in at91_can_probe()
1081 pdev->name)) { in at91_can_probe()
1082 err = -EBUSY; in at91_can_probe()
1086 addr = ioremap(res->start, resource_size(res)); in at91_can_probe()
1088 err = -ENOMEM; in at91_can_probe()
1093 1 << devtype_data->tx_shift); in at91_can_probe()
1095 err = -ENOMEM; in at91_can_probe()
1099 transceiver = devm_phy_optional_get(&pdev->dev, NULL); in at91_can_probe()
1102 dev_err_probe(&pdev->dev, err, "failed to get phy\n"); in at91_can_probe()
1106 dev->netdev_ops = &at91_netdev_ops; in at91_can_probe()
1107 dev->ethtool_ops = &at91_ethtool_ops; in at91_can_probe()
1108 dev->irq = irq; in at91_can_probe()
1109 dev->flags |= IFF_ECHO; in at91_can_probe()
1112 priv->can.clock.freq = clk_get_rate(clk); in at91_can_probe()
1113 priv->can.bittiming_const = &at91_bittiming_const; in at91_can_probe()
1114 priv->can.do_set_mode = at91_set_mode; in at91_can_probe()
1115 priv->can.do_get_berr_counter = at91_get_berr_counter; in at91_can_probe()
1116 priv->can.ctrlmode_supported = CAN_CTRLMODE_3_SAMPLES | in at91_can_probe()
1118 priv->reg_base = addr; in at91_can_probe()
1119 priv->devtype_data = *devtype_data; in at91_can_probe()
1120 priv->clk = clk; in at91_can_probe()
1121 priv->pdata = dev_get_platdata(&pdev->dev); in at91_can_probe()
1122 priv->mb0_id = 0x7ff; in at91_can_probe()
1123 priv->offload.mailbox_read = at91_mailbox_read; in at91_can_probe()
1124 priv->offload.mb_first = devtype_data->rx_first; in at91_can_probe()
1125 priv->offload.mb_last = devtype_data->rx_last; in at91_can_probe()
1127 can_rx_offload_add_timestamp(dev, &priv->offload); in at91_can_probe()
1130 priv->can.bitrate_max = transceiver->attrs.max_link_rate; in at91_can_probe()
1133 dev->sysfs_groups[0] = &at91_sysfs_attr_group; in at91_can_probe()
1136 SET_NETDEV_DEV(dev, &pdev->dev); in at91_can_probe()
1140 dev_err(&pdev->dev, "registering netdev failed\n"); in at91_can_probe()
1144 dev_info(&pdev->dev, "device registered (reg_base=%p, irq=%d)\n", in at91_can_probe()
1145 priv->reg_base, dev->irq); in at91_can_probe()
1154 release_mem_region(res->start, resource_size(res)); in at91_can_probe()
1169 iounmap(priv->reg_base); in at91_can_remove()
1172 release_mem_region(res->start, resource_size(res)); in at91_can_remove()
1174 clk_put(priv->clk); in at91_can_remove()
1204 MODULE_AUTHOR("Marc Kleine-Budde <mkl@pengutronix.de>");