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 by Marc Kleine-Budde <kernel@pengutronix.de>
28 #define AT91_MB_MASK(i) ((1 << (i)) - 1)
45 /* Mailbox registers (0 <= i <= 15) */
79 /* Mailbox Modes */
174 return priv->devtype_data.type == AT91_DEVTYPE_SAM##_model; \
182 return priv->devtype_data.rx_first; in get_mb_rx_first()
187 return priv->devtype_data.rx_last; in get_mb_rx_last()
192 return priv->devtype_data.rx_split; in get_mb_rx_split()
197 return get_mb_rx_last(priv) - get_mb_rx_first(priv) + 1; in get_mb_rx_num()
202 return get_mb_rx_split(priv) - 1; in get_mb_rx_low_last()
213 return priv->devtype_data.tx_shift; in get_mb_tx_shift()
228 return get_mb_tx_first(priv) + get_mb_tx_num(priv) - 1; in get_mb_tx_last()
265 return (priv->tx_next & get_next_mb_mask(priv)) + get_mb_tx_first(priv); in get_tx_next_mb()
270 return (priv->tx_next >> get_next_prio_shift(priv)) & 0xf; in get_tx_next_prio()
275 return (priv->tx_echo & get_next_mb_mask(priv)) + get_mb_tx_first(priv); in get_tx_echo_mb()
280 return readl_relaxed(priv->reg_base + reg); in at91_read()
286 writel_relaxed(value, priv->reg_base + reg); in at91_write()
321 * mailbox is disabled. The next 11 mailboxes are used as a in at91_setup_mailboxes()
322 * reception FIFO. The last mailbox is configured with in at91_setup_mailboxes()
326 reg_mid = at91_can_id_to_reg_mid(priv->mb0_id); in at91_setup_mailboxes()
347 /* Reset tx and rx helper pointers */ in at91_setup_mailboxes()
348 priv->tx_next = priv->tx_echo = 0; in at91_setup_mailboxes()
349 priv->rx_next = get_mb_rx_first(priv); in at91_setup_mailboxes()
355 const struct can_bittiming *bt = &priv->can.bittiming; in at91_set_bittiming()
358 reg_br = ((priv->can.ctrlmode & CAN_CTRLMODE_3_SAMPLES) ? 1 << 24 : 0) | in at91_set_bittiming()
359 ((bt->brp - 1) << 16) | ((bt->sjw - 1) << 12) | in at91_set_bittiming()
360 ((bt->prop_seg - 1) << 8) | ((bt->phase_seg1 - 1) << 4) | in at91_set_bittiming()
361 ((bt->phase_seg2 - 1) << 0); in at91_set_bittiming()
376 bec->rxerr = reg_ecr & 0xff; in at91_get_berr_counter()
377 bec->txerr = reg_ecr >> 16; in at91_get_berr_counter()
398 if (priv->can.ctrlmode & CAN_CTRLMODE_LISTENONLY) in at91_chip_start()
404 priv->can.state = CAN_STATE_ERROR_ACTIVE; in at91_chip_start()
423 priv->can.state = state; in at91_chip_stop()
430 * message of the mailbox with the lowest number is sent first.
432 * We use the first TX mailbox (AT91_MB_TX_FIRST) with prio 0, then
433 * the next mailbox with prio 0, and so on, until all mailboxes are
434 * used. Then we start from the beginning with mailbox
435 * AT91_MB_TX_FIRST, but with prio 1, mailbox AT91_MB_TX_FIRST + 1
436 * prio 1. When we reach the last mailbox with prio 15, we have to
438 * again with mailbox AT91_MB_TX_FIRST prio 0.
440 * We use the priv->tx_next as counter for the next transmission
441 * mailbox, but without the offset AT91_MB_TX_FIRST. The lower bits
442 * encode the mailbox number, the upper 4 bits the mailbox priority:
444 * priv->tx_next = (prio << get_next_prio_shift(priv)) |
445 * (mb - get_mb_tx_first(priv));
451 struct can_frame *cf = (struct can_frame *)skb->data; in at91_start_xmit()
464 netdev_err(dev, "BUG! TX buffer full when queue awake!\n"); in at91_start_xmit()
467 reg_mid = at91_can_id_to_reg_mid(cf->can_id); in at91_start_xmit()
468 reg_mcr = ((cf->can_id & CAN_RTR_FLAG) ? AT91_MCR_MRTR : 0) | in at91_start_xmit()
469 (cf->len << 16) | AT91_MCR_MTCR; in at91_start_xmit()
476 at91_write(priv, AT91_MDL(mb), *(u32 *)(cf->data + 0)); in at91_start_xmit()
477 at91_write(priv, AT91_MDH(mb), *(u32 *)(cf->data + 4)); in at91_start_xmit()
483 can_put_echo_skb(skb, dev, mb - get_mb_tx_first(priv), 0); in at91_start_xmit()
487 * tx_next buffer prio and mailbox equals 0. in at91_start_xmit()
492 priv->tx_next++; in at91_start_xmit()
495 (priv->tx_next & get_next_mask(priv)) == 0) in at91_start_xmit()
498 /* Enable interrupt for this mailbox */ in at91_start_xmit()
505 * at91_activate_rx_low - activate lower rx mailboxes
518 * at91_activate_rx_mb - reactive single rx mailbox
520 * @mb: mailbox to reactivate
522 * Reenables given mailbox for reception of new CAN messages
533 * at91_rx_overflow_err - send error frame due to rx overflow
538 struct net_device_stats *stats = &dev->stats; in at91_rx_overflow_err()
543 stats->rx_over_errors++; in at91_rx_overflow_err()
544 stats->rx_errors++; in at91_rx_overflow_err()
550 cf->can_id |= CAN_ERR_CRTL; in at91_rx_overflow_err()
551 cf->data[1] = CAN_ERR_CRTL_RX_OVERFLOW; in at91_rx_overflow_err()
557 * at91_read_mb - read CAN msg from mailbox (lowlevel impl)
559 * @mb: mailbox number to read from
562 * Reads a CAN message from the given mailbox and stores data into
573 cf->can_id = ((reg_mid >> 0) & CAN_EFF_MASK) | CAN_EFF_FLAG; in at91_read_mb()
575 cf->can_id = (reg_mid >> 18) & CAN_SFF_MASK; in at91_read_mb()
578 cf->len = can_cc_dlc2len((reg_msr >> 16) & 0xf); in at91_read_mb()
581 cf->can_id |= CAN_RTR_FLAG; in at91_read_mb()
583 *(u32 *)(cf->data + 0) = at91_read(priv, AT91_MDL(mb)); in at91_read_mb()
584 *(u32 *)(cf->data + 4) = at91_read(priv, AT91_MDH(mb)); in at91_read_mb()
595 * at91_read_msg - read CAN message from mailbox
599 * Reads a CAN message from given mailbox, and put into linux network
604 struct net_device_stats *stats = &dev->stats; in at91_read_msg()
610 stats->rx_dropped++; in at91_read_msg()
616 stats->rx_packets++; in at91_read_msg()
617 if (!(cf->can_id & CAN_RTR_FLAG)) in at91_read_msg()
618 stats->rx_bytes += cf->len; in at91_read_msg()
624 * at91_poll_rx - read multiple CAN messages from mailboxes
635 * into the first free mailbox it finds (starting with the
641 * scheduled. We read the mailbox, but do _not_ re-enable the mb (to
647 * +-+-+-+-+-+-+-+-++-+-+-+-+
649 * +-+-+-+-+-+-+-+-++-+-+-+-+
657 * The variable priv->rx_next points to the next mailbox to read a
659 * mailbox but not re-enable it.
661 * With completion of the last of the lower mailboxes, we re-enable the
665 * upper group we re-enable the mailbox right after reading it. Giving
680 if (priv->rx_next > get_mb_rx_low_last(priv) && in at91_poll_rx()
686 for (mb = find_next_bit(addr, get_mb_tx_first(priv), priv->rx_next); in at91_poll_rx()
689 mb = find_next_bit(addr, get_mb_tx_first(priv), ++priv->rx_next)) { in at91_poll_rx()
697 /* only the mailbox we read */ in at91_poll_rx()
701 quota--; in at91_poll_rx()
705 if (priv->rx_next > get_mb_rx_low_last(priv) && in at91_poll_rx()
707 priv->rx_next = get_mb_rx_first(priv); in at91_poll_rx()
723 dev->stats.rx_errors++; in at91_poll_err_frame()
724 priv->can.can_stats.bus_error++; in at91_poll_err_frame()
725 cf->can_id |= CAN_ERR_PROT | CAN_ERR_BUSERROR; in at91_poll_err_frame()
731 dev->stats.rx_errors++; in at91_poll_err_frame()
732 priv->can.can_stats.bus_error++; in at91_poll_err_frame()
733 cf->can_id |= CAN_ERR_PROT | CAN_ERR_BUSERROR; in at91_poll_err_frame()
734 cf->data[2] |= CAN_ERR_PROT_STUFF; in at91_poll_err_frame()
740 dev->stats.tx_errors++; in at91_poll_err_frame()
741 cf->can_id |= CAN_ERR_ACK; in at91_poll_err_frame()
747 dev->stats.rx_errors++; in at91_poll_err_frame()
748 priv->can.can_stats.bus_error++; in at91_poll_err_frame()
749 cf->can_id |= CAN_ERR_PROT | CAN_ERR_BUSERROR; in at91_poll_err_frame()
750 cf->data[2] |= CAN_ERR_PROT_FORM; in at91_poll_err_frame()
756 dev->stats.tx_errors++; in at91_poll_err_frame()
757 priv->can.can_stats.bus_error++; in at91_poll_err_frame()
758 cf->can_id |= CAN_ERR_PROT | CAN_ERR_BUSERROR; in at91_poll_err_frame()
759 cf->data[2] |= CAN_ERR_PROT_BIT; in at91_poll_err_frame()
784 struct net_device *dev = napi->dev; in at91_poll()
790 work_done += at91_poll_rx(dev, quota - work_done); in at91_poll()
795 reg_sr |= priv->reg_sr; in at91_poll()
797 work_done += at91_poll_err(dev, quota - work_done, reg_sr); in at91_poll()
803 reg_ier |= get_irq_mb_rx(priv) & ~AT91_MB_MASK(priv->rx_next); in at91_poll()
814 * priv->tx_echo holds the number of the oldest can_frame put for
815 * transmission into the hardware, but not yet ACKed by the CAN tx
818 * We iterate from priv->tx_echo to priv->tx_next and check if the
831 for (/* nix */; (priv->tx_next - priv->tx_echo) > 0; priv->tx_echo++) { in at91_irq_tx()
834 /* no event in mailbox? */ in at91_irq_tx()
838 /* Disable irq for this TX mailbox */ in at91_irq_tx()
841 /* only echo if mailbox signals us a transfer in at91_irq_tx()
850 dev->stats.tx_bytes += in at91_irq_tx()
852 mb - get_mb_tx_first(priv), in at91_irq_tx()
854 dev->stats.tx_packets++; in at91_irq_tx()
859 * we get a TX int for the last can frame directly before a in at91_irq_tx()
862 if ((priv->tx_next & get_next_mask(priv)) != 0 || in at91_irq_tx()
863 (priv->tx_echo & get_next_mask(priv)) == 0) in at91_irq_tx()
876 switch (priv->can.state) { in at91_irq_err_state()
885 priv->can.can_stats.error_warning++; in at91_irq_err_state()
887 cf->can_id |= CAN_ERR_CRTL; in at91_irq_err_state()
888 cf->data[1] = (bec.txerr > bec.rxerr) ? in at91_irq_err_state()
901 priv->can.can_stats.error_passive++; in at91_irq_err_state()
903 cf->can_id |= CAN_ERR_CRTL; in at91_irq_err_state()
904 cf->data[1] = (bec.txerr > bec.rxerr) ? in at91_irq_err_state()
914 cf->can_id |= CAN_ERR_RESTARTED; in at91_irq_err_state()
917 priv->can.can_stats.restarts++; in at91_irq_err_state()
936 cf->can_id |= CAN_ERR_PROT; in at91_irq_err_state()
937 cf->data[2] = CAN_ERR_PROT_ACTIVE; in at91_irq_err_state()
952 cf->can_id |= CAN_ERR_BUSOFF; in at91_irq_err_state()
954 netdev_dbg(dev, "bus-off\n"); in at91_irq_err_state()
956 priv->can.can_stats.bus_off++; in at91_irq_err_state()
959 if (!priv->can.restart_ms) { in at91_irq_err_state()
1026 if (likely(new_state == priv->can.state)) in at91_irq_err()
1037 priv->can.state = new_state; in at91_irq_err()
1059 /* Receive or error interrupt? -> napi */ in at91_irq()
1064 priv->reg_sr = reg_sr; in at91_irq()
1067 napi_schedule(&priv->napi); in at91_irq()
1085 err = clk_prepare_enable(priv->clk); in at91_open()
1095 if (request_irq(dev->irq, at91_irq, IRQF_SHARED, in at91_open()
1096 dev->name, dev)) { in at91_open()
1097 err = -EAGAIN; in at91_open()
1103 napi_enable(&priv->napi); in at91_open()
1111 clk_disable_unprepare(priv->clk); in at91_open()
1123 napi_disable(&priv->napi); in at91_close()
1126 free_irq(dev->irq, dev); in at91_close()
1127 clk_disable_unprepare(priv->clk); in at91_close()
1143 return -EOPNOTSUPP; in at91_set_mode()
1165 if (priv->mb0_id & CAN_EFF_FLAG) in mb0_id_show()
1166 return sysfs_emit(buf, "0x%08x\n", priv->mb0_id); in mb0_id_show()
1168 return sysfs_emit(buf, "0x%03x\n", priv->mb0_id); in mb0_id_show()
1173 const char *buf, size_t count) in mb0_id_store() argument
1183 if (ndev->flags & IFF_UP) { in mb0_id_store()
1184 ret = -EBUSY; in mb0_id_store()
1199 priv->mb0_id = can_id; in mb0_id_store()
1200 ret = count; in mb0_id_store()
1221 .compatible = "atmel,at91sam9x5-can",
1224 .compatible = "atmel,at91sam9263-can",
1235 if (pdev->dev.of_node) { in at91_can_get_driver_data()
1238 match = of_match_node(at91_can_dt_ids, pdev->dev.of_node); in at91_can_get_driver_data()
1240 dev_err(&pdev->dev, "no matching node found in dtb\n"); in at91_can_get_driver_data()
1243 return (const struct at91_devtype_data *)match->data; in at91_can_get_driver_data()
1246 platform_get_device_id(pdev)->driver_data; in at91_can_get_driver_data()
1261 dev_err(&pdev->dev, "no driver data\n"); in at91_can_probe()
1262 err = -ENODEV; in at91_can_probe()
1266 clk = clk_get(&pdev->dev, "can_clk"); in at91_can_probe()
1268 dev_err(&pdev->dev, "no clock defined\n"); in at91_can_probe()
1269 err = -ENODEV; in at91_can_probe()
1276 err = -ENODEV; in at91_can_probe()
1280 if (!request_mem_region(res->start, in at91_can_probe()
1282 pdev->name)) { in at91_can_probe()
1283 err = -EBUSY; in at91_can_probe()
1287 addr = ioremap(res->start, resource_size(res)); in at91_can_probe()
1289 err = -ENOMEM; in at91_can_probe()
1294 1 << devtype_data->tx_shift); in at91_can_probe()
1296 err = -ENOMEM; in at91_can_probe()
1300 dev->netdev_ops = &at91_netdev_ops; in at91_can_probe()
1301 dev->ethtool_ops = &at91_ethtool_ops; in at91_can_probe()
1302 dev->irq = irq; in at91_can_probe()
1303 dev->flags |= IFF_ECHO; in at91_can_probe()
1306 priv->can.clock.freq = clk_get_rate(clk); in at91_can_probe()
1307 priv->can.bittiming_const = &at91_bittiming_const; in at91_can_probe()
1308 priv->can.do_set_mode = at91_set_mode; in at91_can_probe()
1309 priv->can.do_get_berr_counter = at91_get_berr_counter; in at91_can_probe()
1310 priv->can.ctrlmode_supported = CAN_CTRLMODE_3_SAMPLES | in at91_can_probe()
1312 priv->reg_base = addr; in at91_can_probe()
1313 priv->devtype_data = *devtype_data; in at91_can_probe()
1314 priv->clk = clk; in at91_can_probe()
1315 priv->pdata = dev_get_platdata(&pdev->dev); in at91_can_probe()
1316 priv->mb0_id = 0x7ff; in at91_can_probe()
1318 netif_napi_add_weight(dev, &priv->napi, at91_poll, get_mb_rx_num(priv)); in at91_can_probe()
1321 dev->sysfs_groups[0] = &at91_sysfs_attr_group; in at91_can_probe()
1324 SET_NETDEV_DEV(dev, &pdev->dev); in at91_can_probe()
1328 dev_err(&pdev->dev, "registering netdev failed\n"); in at91_can_probe()
1332 dev_info(&pdev->dev, "device registered (reg_base=%p, irq=%d)\n", in at91_can_probe()
1333 priv->reg_base, dev->irq); in at91_can_probe()
1342 release_mem_region(res->start, resource_size(res)); in at91_can_probe()
1357 iounmap(priv->reg_base); in at91_can_remove()
1360 release_mem_region(res->start, resource_size(res)); in at91_can_remove()
1362 clk_put(priv->clk); in at91_can_remove()
1392 MODULE_AUTHOR("Marc Kleine-Budde <mkl@pengutronix.de>");