Lines Matching +full:canfd +full:- +full:2

1 // SPDX-License-Identifier: GPL-2.0+
2 /* Renesas R-Car CAN FD device driver
7 /* The R-Car CAN FD controller can operate in either one of the below two modes
8 * - CAN FD only mode
9 * - Classical CAN (CAN 2.0) only mode
16 * "renesas,no-can-fd" optional property to the device tree node. A h/w reset is
62 #define RCANFD_GCTR_GSLPR BIT(2)
70 #define RCANFD_GSTS_GSLPSTS BIT(2)
73 /* Non-operational status */
74 #define RCANFD_GSTS_GNOPM (BIT(0) | BIT(1) | BIT(2) | BIT(3))
80 #define RCANFD_GERFL_THLES BIT(2)
88 ((gpriv)->fdmode ? RCANFD_GERFL_CMPOF : 0)))
95 (reg_gen4(gpriv, 16, 24) - ((n) & 1) * reg_gen4(gpriv, 16, 8)))
98 (((x) >> (reg_gen4(gpriv, 16, 24) - ((n) & 1) * reg_gen4(gpriv, 16, 8))) & \
113 /* RSCFDnCmCFG - Classical CAN only */
119 /* RSCFDnCFDCmNCFG - CAN FD only */
150 #define RCANFD_CCTR_CSLPR BIT(2)
162 #define RCANFD_CSTS_SLPSTS BIT(2)
182 #define RCANFD_CERFL_EPF BIT(2)
215 #define RCANFD_RFSTS_RFMLT BIT(2)
229 #define RCANFD_RFFDSTS_RFFDF BIT(2)
242 #define RCANFD_CFCC_CFTXIE BIT(2)
248 #define RCANFD_CFSTS_CFMLT BIT(2)
263 #define RCANFD_CFFDCSTS_CFFDF BIT(2)
301 #define RCANFD_GAFLCFG(ch) (0x009c + (0x04 * ((ch) / 2)))
396 /* RSCFDnRMXXXq -> RCANFD_C_RMXXX(q) */
402 /* RSCFDnRFXXx -> RCANFD_C_RFXX(x) */
409 /* RSCFDnCFXXk -> RCANFD_C_CFXX(ch, k) */
421 /* RSCFDnTMXXp -> RCANFD_C_TMXX(p) */
432 /* R-Car Gen4 Classical and CAN FD mode specific register map */
439 /* RSCFDnCFDCmXXX -> RCANFD_F_XXX(m) */
449 /* RSCFDnCFDRMXXXq -> RCANFD_F_RMXXX(q) */
455 /* RSCFDnCFDRFXXx -> RCANFD_F_RFXX(x) */
463 /* RSCFDnCFDCFXXk -> RCANFD_F_CFXX(ch, k) */
479 /* RSCFDnCFDTMXXp -> RCANFD_F_TMXX(p) */
495 #define RCANFD_CHANNELS_MASK BIT((RCANFD_NUM_CHANNELS) - 1)
543 bool extclk; /* CANFD or Ext clock */
553 .tseg1_min = 2,
555 .tseg2_min = 2,
566 .tseg1_min = 2,
568 .tseg2_min = 2,
581 .tseg2_min = 2,
590 .max_channels = 2,
591 .postdiv = 2,
597 .postdiv = 2,
602 .max_channels = 2,
610 return gpriv->info == &rcar_gen4_hw_info; in is_gen4()
659 lwords = DIV_ROUND_UP(cf->len, sizeof(u32)); in rcar_canfd_get_data()
661 *((u32 *)cf->data + i) = in rcar_canfd_get_data()
662 rcar_canfd_read(priv->base, off + i * sizeof(u32)); in rcar_canfd_get_data()
670 lwords = DIV_ROUND_UP(cf->len, sizeof(u32)); in rcar_canfd_put_data()
672 rcar_canfd_write(priv->base, off + i * sizeof(u32), in rcar_canfd_put_data()
673 *((u32 *)cf->data + i)); in rcar_canfd_put_data()
687 u32 ch, val = gpriv->fdmode ? RCANFD_GEN4_FDCFG_FDOE in rcar_canfd_set_mode()
690 for_each_set_bit(ch, &gpriv->channels_mask, in rcar_canfd_set_mode()
691 gpriv->info->max_channels) in rcar_canfd_set_mode()
692 rcar_canfd_set_bit(gpriv->base, RCANFD_GEN4_FDCFG(ch), in rcar_canfd_set_mode()
695 if (gpriv->fdmode) in rcar_canfd_set_mode()
696 rcar_canfd_set_bit(gpriv->base, RCANFD_GRMCFG, in rcar_canfd_set_mode()
699 rcar_canfd_clear_bit(gpriv->base, RCANFD_GRMCFG, in rcar_canfd_set_mode()
712 err = readl_poll_timeout((gpriv->base + RCANFD_GSTS), sts, in rcar_canfd_reset_controller()
713 !(sts & RCANFD_GSTS_GRAMINIT), 2, 500000); in rcar_canfd_reset_controller()
715 dev_dbg(&gpriv->pdev->dev, "global raminit failed\n"); in rcar_canfd_reset_controller()
720 rcar_canfd_clear_bit(gpriv->base, RCANFD_GCTR, RCANFD_GCTR_GSLPR); in rcar_canfd_reset_controller()
721 rcar_canfd_update_bit(gpriv->base, RCANFD_GCTR, in rcar_canfd_reset_controller()
725 err = readl_poll_timeout((gpriv->base + RCANFD_GSTS), sts, in rcar_canfd_reset_controller()
726 (sts & RCANFD_GSTS_GRSTSTS), 2, 500000); in rcar_canfd_reset_controller()
728 dev_dbg(&gpriv->pdev->dev, "global reset failed\n"); in rcar_canfd_reset_controller()
733 rcar_canfd_write(gpriv->base, RCANFD_GERFL, 0x0); in rcar_canfd_reset_controller()
739 for_each_set_bit(ch, &gpriv->channels_mask, gpriv->info->max_channels) { in rcar_canfd_reset_controller()
740 rcar_canfd_clear_bit(gpriv->base, in rcar_canfd_reset_controller()
743 rcar_canfd_update_bit(gpriv->base, RCANFD_CCTR(ch), in rcar_canfd_reset_controller()
748 err = readl_poll_timeout((gpriv->base + RCANFD_CSTS(ch)), sts, in rcar_canfd_reset_controller()
750 2, 500000); in rcar_canfd_reset_controller()
752 dev_dbg(&gpriv->pdev->dev, in rcar_canfd_reset_controller()
769 if (gpriv->fdmode) in rcar_canfd_configure_controller()
774 if (gpriv->extclk) in rcar_canfd_configure_controller()
777 rcar_canfd_set_bit(gpriv->base, RCANFD_GCFG, cfg); in rcar_canfd_configure_controller()
780 for_each_set_bit(ch, &gpriv->channels_mask, gpriv->info->max_channels) { in rcar_canfd_configure_controller()
781 rcar_canfd_set_bit(gpriv->base, RCANFD_CCTR(ch), in rcar_canfd_configure_controller()
783 rcar_canfd_update_bit(gpriv->base, RCANFD_CCTR(ch), in rcar_canfd_configure_controller()
800 cfg = rcar_canfd_read(gpriv->base, RCANFD_GAFLCFG(ch)); in rcar_canfd_configure_afl_rules()
806 rcar_canfd_set_bit(gpriv->base, RCANFD_GAFLECTR, in rcar_canfd_configure_afl_rules()
811 rcar_canfd_set_bit(gpriv->base, RCANFD_GAFLCFG(ch), in rcar_canfd_configure_afl_rules()
815 else if (gpriv->fdmode) in rcar_canfd_configure_afl_rules()
821 rcar_canfd_write(gpriv->base, RCANFD_GAFLID(offset, start), 0); in rcar_canfd_configure_afl_rules()
823 rcar_canfd_write(gpriv->base, RCANFD_GAFLM(offset, start), 0); in rcar_canfd_configure_afl_rules()
825 rcar_canfd_write(gpriv->base, RCANFD_GAFLP0(offset, start), 0); in rcar_canfd_configure_afl_rules()
827 rcar_canfd_set_bit(gpriv->base, RCANFD_GAFLP1(offset, start), in rcar_canfd_configure_afl_rules()
831 rcar_canfd_clear_bit(gpriv->base, in rcar_canfd_configure_afl_rules()
844 rfdc = 2; /* b010 - 8 messages Rx FIFO depth */ in rcar_canfd_configure_rx()
845 if (gpriv->fdmode) in rcar_canfd_configure_rx()
846 rfpls = 7; /* b111 - Max 64 bytes payload */ in rcar_canfd_configure_rx()
848 rfpls = 0; /* b000 - Max 8 bytes payload */ in rcar_canfd_configure_rx()
852 rcar_canfd_write(gpriv->base, RCANFD_RFCC(gpriv, ridx), cfg); in rcar_canfd_configure_rx()
867 cfm = 1; /* b01 - Transmit mode */ in rcar_canfd_configure_tx()
868 cfdc = 2; /* b010 - 8 messages Tx FIFO depth */ in rcar_canfd_configure_tx()
869 if (gpriv->fdmode) in rcar_canfd_configure_tx()
870 cfpls = 7; /* b111 - Max 64 bytes payload */ in rcar_canfd_configure_tx()
872 cfpls = 0; /* b000 - Max 8 bytes payload */ in rcar_canfd_configure_tx()
877 rcar_canfd_write(gpriv->base, RCANFD_CFCC(gpriv, ch, RCANFD_CFFIFO_IDX), cfg); in rcar_canfd_configure_tx()
879 if (gpriv->fdmode) in rcar_canfd_configure_tx()
881 rcar_canfd_write(gpriv->base, in rcar_canfd_configure_tx()
890 rcar_canfd_write(gpriv->base, RCANFD_GERFL, 0); in rcar_canfd_enable_global_interrupts()
894 if (gpriv->fdmode) in rcar_canfd_enable_global_interrupts()
897 rcar_canfd_set_bit(gpriv->base, RCANFD_GCTR, ctr); in rcar_canfd_enable_global_interrupts()
904 rcar_canfd_write(gpriv->base, RCANFD_GCTR, 0); in rcar_canfd_disable_global_interrupts()
907 rcar_canfd_write(gpriv->base, RCANFD_GERFL, 0); in rcar_canfd_disable_global_interrupts()
913 u32 ctr, ch = priv->channel; in rcar_canfd_enable_channel_interrupts()
916 rcar_canfd_write(priv->base, RCANFD_CERFL(ch), 0); in rcar_canfd_enable_channel_interrupts()
924 rcar_canfd_set_bit(priv->base, RCANFD_CCTR(ch), ctr); in rcar_canfd_enable_channel_interrupts()
930 u32 ctr, ch = priv->channel; in rcar_canfd_disable_channel_interrupts()
937 rcar_canfd_clear_bit(priv->base, RCANFD_CCTR(ch), ctr); in rcar_canfd_disable_channel_interrupts()
940 rcar_canfd_write(priv->base, RCANFD_CERFL(ch), 0); in rcar_canfd_disable_channel_interrupts()
946 struct rcar_canfd_global *gpriv = priv->gpriv; in rcar_canfd_global_error()
947 struct net_device_stats *stats = &ndev->stats; in rcar_canfd_global_error()
948 u32 ch = priv->channel; in rcar_canfd_global_error()
952 gerfl = rcar_canfd_read(priv->base, RCANFD_GERFL); in rcar_canfd_global_error()
955 stats->tx_dropped++; in rcar_canfd_global_error()
958 sts = rcar_canfd_read(priv->base, in rcar_canfd_global_error()
962 stats->tx_dropped++; in rcar_canfd_global_error()
963 rcar_canfd_write(priv->base, in rcar_canfd_global_error()
968 sts = rcar_canfd_read(priv->base, RCANFD_RFSTS(gpriv, ridx)); in rcar_canfd_global_error()
971 stats->rx_dropped++; in rcar_canfd_global_error()
972 rcar_canfd_write(priv->base, RCANFD_RFSTS(gpriv, ridx), in rcar_canfd_global_error()
976 if (gpriv->fdmode && gerfl & RCANFD_GERFL_CMPOF) { in rcar_canfd_global_error()
987 rcar_canfd_write(priv->base, RCANFD_GERFL, 0); in rcar_canfd_global_error()
994 struct net_device_stats *stats = &ndev->stats; in rcar_canfd_error()
997 u32 ch = priv->channel; in rcar_canfd_error()
1004 stats->rx_dropped++; in rcar_canfd_error()
1011 cf->can_id |= CAN_ERR_BUSERROR | CAN_ERR_PROT; in rcar_canfd_error()
1012 cf->data[2] = CAN_ERR_PROT_UNSPEC; in rcar_canfd_error()
1013 priv->can.can_stats.bus_error++; in rcar_canfd_error()
1017 stats->tx_errors++; in rcar_canfd_error()
1018 cf->data[3] |= CAN_ERR_PROT_LOC_ACK_DEL; in rcar_canfd_error()
1022 stats->tx_errors++; in rcar_canfd_error()
1023 cf->data[2] |= CAN_ERR_PROT_BIT0; in rcar_canfd_error()
1027 stats->tx_errors++; in rcar_canfd_error()
1028 cf->data[2] |= CAN_ERR_PROT_BIT1; in rcar_canfd_error()
1032 stats->rx_errors++; in rcar_canfd_error()
1033 cf->data[3] |= CAN_ERR_PROT_LOC_CRC_SEQ; in rcar_canfd_error()
1037 stats->tx_errors++; in rcar_canfd_error()
1038 cf->can_id |= CAN_ERR_ACK; in rcar_canfd_error()
1039 cf->data[3] |= CAN_ERR_PROT_LOC_ACK; in rcar_canfd_error()
1043 stats->rx_errors++; in rcar_canfd_error()
1044 cf->data[2] |= CAN_ERR_PROT_FORM; in rcar_canfd_error()
1048 stats->rx_errors++; in rcar_canfd_error()
1049 cf->data[2] |= CAN_ERR_PROT_STUFF; in rcar_canfd_error()
1053 priv->can.can_stats.arbitration_lost++; in rcar_canfd_error()
1054 cf->can_id |= CAN_ERR_LOSTARB; in rcar_canfd_error()
1055 cf->data[0] |= CAN_ERR_LOSTARB_UNSPEC; in rcar_canfd_error()
1059 stats->rx_errors++; in rcar_canfd_error()
1060 cf->can_id |= CAN_ERR_BUSERROR; in rcar_canfd_error()
1064 priv->can.state = CAN_STATE_ERROR_WARNING; in rcar_canfd_error()
1065 priv->can.can_stats.error_warning++; in rcar_canfd_error()
1066 cf->can_id |= CAN_ERR_CRTL | CAN_ERR_CNT; in rcar_canfd_error()
1067 cf->data[1] = txerr > rxerr ? CAN_ERR_CRTL_TX_WARNING : in rcar_canfd_error()
1069 cf->data[6] = txerr; in rcar_canfd_error()
1070 cf->data[7] = rxerr; in rcar_canfd_error()
1074 priv->can.state = CAN_STATE_ERROR_PASSIVE; in rcar_canfd_error()
1075 priv->can.can_stats.error_passive++; in rcar_canfd_error()
1076 cf->can_id |= CAN_ERR_CRTL | CAN_ERR_CNT; in rcar_canfd_error()
1077 cf->data[1] = txerr > rxerr ? CAN_ERR_CRTL_TX_PASSIVE : in rcar_canfd_error()
1079 cf->data[6] = txerr; in rcar_canfd_error()
1080 cf->data[7] = rxerr; in rcar_canfd_error()
1083 netdev_dbg(ndev, "Bus-off entry interrupt\n"); in rcar_canfd_error()
1085 priv->can.state = CAN_STATE_BUS_OFF; in rcar_canfd_error()
1086 priv->can.can_stats.bus_off++; in rcar_canfd_error()
1088 cf->can_id |= CAN_ERR_BUSOFF; in rcar_canfd_error()
1093 stats->tx_errors++; in rcar_canfd_error()
1094 cf->can_id |= CAN_ERR_PROT; in rcar_canfd_error()
1095 cf->data[2] |= CAN_ERR_PROT_OVERLOAD; in rcar_canfd_error()
1099 rcar_canfd_write(priv->base, RCANFD_CERFL(ch), in rcar_canfd_error()
1107 struct rcar_canfd_global *gpriv = priv->gpriv; in rcar_canfd_tx_done()
1108 struct net_device_stats *stats = &ndev->stats; in rcar_canfd_tx_done()
1111 u32 ch = priv->channel; in rcar_canfd_tx_done()
1116 sent = priv->tx_tail % RCANFD_FIFO_DEPTH; in rcar_canfd_tx_done()
1117 stats->tx_packets++; in rcar_canfd_tx_done()
1118 stats->tx_bytes += can_get_echo_skb(ndev, sent, NULL); in rcar_canfd_tx_done()
1120 spin_lock_irqsave(&priv->tx_lock, flags); in rcar_canfd_tx_done()
1121 priv->tx_tail++; in rcar_canfd_tx_done()
1122 sts = rcar_canfd_read(priv->base, in rcar_canfd_tx_done()
1130 if (priv->tx_head - priv->tx_tail <= unsent) { in rcar_canfd_tx_done()
1131 spin_unlock_irqrestore(&priv->tx_lock, flags); in rcar_canfd_tx_done()
1134 spin_unlock_irqrestore(&priv->tx_lock, flags); in rcar_canfd_tx_done()
1139 rcar_canfd_write(priv->base, RCANFD_CFSTS(gpriv, ch, RCANFD_CFFIFO_IDX), in rcar_canfd_tx_done()
1145 struct rcar_canfd_channel *priv = gpriv->ch[ch]; in rcar_canfd_handle_global_err()
1146 struct net_device *ndev = priv->ndev; in rcar_canfd_handle_global_err()
1150 gerfl = rcar_canfd_read(priv->base, RCANFD_GERFL); in rcar_canfd_handle_global_err()
1160 for_each_set_bit(ch, &gpriv->channels_mask, gpriv->info->max_channels) in rcar_canfd_global_err_interrupt()
1168 struct rcar_canfd_channel *priv = gpriv->ch[ch]; in rcar_canfd_handle_global_receive()
1173 sts = rcar_canfd_read(priv->base, RCANFD_RFSTS(gpriv, ridx)); in rcar_canfd_handle_global_receive()
1174 cc = rcar_canfd_read(priv->base, RCANFD_RFCC(gpriv, ridx)); in rcar_canfd_handle_global_receive()
1177 if (napi_schedule_prep(&priv->napi)) { in rcar_canfd_handle_global_receive()
1179 rcar_canfd_clear_bit(priv->base, in rcar_canfd_handle_global_receive()
1182 __napi_schedule(&priv->napi); in rcar_canfd_handle_global_receive()
1192 for_each_set_bit(ch, &gpriv->channels_mask, gpriv->info->max_channels) in rcar_canfd_global_receive_fifo_interrupt()
1206 for_each_set_bit(ch, &gpriv->channels_mask, gpriv->info->max_channels) { in rcar_canfd_global_interrupt()
1217 struct net_device_stats *stats = &ndev->stats; in rcar_canfd_state_change()
1218 enum can_state rx_state, tx_state, state = priv->can.state; in rcar_canfd_state_change()
1228 if (state != priv->can.state) { in rcar_canfd_state_change()
1230 state, priv->can.state, txerr, rxerr); in rcar_canfd_state_change()
1233 stats->rx_dropped++; in rcar_canfd_state_change()
1246 struct rcar_canfd_channel *priv = gpriv->ch[ch]; in rcar_canfd_handle_channel_tx()
1247 struct net_device *ndev = priv->ndev; in rcar_canfd_handle_channel_tx()
1251 sts = rcar_canfd_read(priv->base, in rcar_canfd_handle_channel_tx()
1261 rcar_canfd_handle_channel_tx(priv->gpriv, priv->channel); in rcar_canfd_channel_tx_interrupt()
1268 struct rcar_canfd_channel *priv = gpriv->ch[ch]; in rcar_canfd_handle_channel_err()
1269 struct net_device *ndev = priv->ndev; in rcar_canfd_handle_channel_err()
1274 cerfl = rcar_canfd_read(priv->base, RCANFD_CERFL(ch)); in rcar_canfd_handle_channel_err()
1275 sts = rcar_canfd_read(priv->base, RCANFD_CSTS(ch)); in rcar_canfd_handle_channel_err()
1282 if (unlikely(priv->can.state != CAN_STATE_ERROR_ACTIVE && in rcar_canfd_handle_channel_err()
1283 priv->can.state != CAN_STATE_BUS_OFF)) in rcar_canfd_handle_channel_err()
1291 rcar_canfd_handle_channel_err(priv->gpriv, priv->channel); in rcar_canfd_channel_err_interrupt()
1302 for_each_set_bit(ch, &gpriv->channels_mask, gpriv->info->max_channels) { in rcar_canfd_channel_interrupt()
1313 struct rcar_canfd_global *gpriv = priv->gpriv; in rcar_canfd_set_bittiming()
1314 const struct can_bittiming *bt = &priv->can.bittiming; in rcar_canfd_set_bittiming()
1315 const struct can_bittiming *dbt = &priv->can.data_bittiming; in rcar_canfd_set_bittiming()
1318 u32 ch = priv->channel; in rcar_canfd_set_bittiming()
1321 brp = bt->brp - 1; in rcar_canfd_set_bittiming()
1322 sjw = bt->sjw - 1; in rcar_canfd_set_bittiming()
1323 tseg1 = bt->prop_seg + bt->phase_seg1 - 1; in rcar_canfd_set_bittiming()
1324 tseg2 = bt->phase_seg2 - 1; in rcar_canfd_set_bittiming()
1326 if (priv->can.ctrlmode & CAN_CTRLMODE_FD) { in rcar_canfd_set_bittiming()
1331 rcar_canfd_write(priv->base, RCANFD_CCFG(ch), cfg); in rcar_canfd_set_bittiming()
1332 netdev_dbg(priv->ndev, "nrate: brp %u, sjw %u, tseg1 %u, tseg2 %u\n", in rcar_canfd_set_bittiming()
1336 brp = dbt->brp - 1; in rcar_canfd_set_bittiming()
1337 sjw = dbt->sjw - 1; in rcar_canfd_set_bittiming()
1338 tseg1 = dbt->prop_seg + dbt->phase_seg1 - 1; in rcar_canfd_set_bittiming()
1339 tseg2 = dbt->phase_seg2 - 1; in rcar_canfd_set_bittiming()
1344 rcar_canfd_write(priv->base, RCANFD_F_DCFG(gpriv, ch), cfg); in rcar_canfd_set_bittiming()
1345 netdev_dbg(priv->ndev, "drate: brp %u, sjw %u, tseg1 %u, tseg2 %u\n", in rcar_canfd_set_bittiming()
1361 rcar_canfd_write(priv->base, RCANFD_CCFG(ch), cfg); in rcar_canfd_set_bittiming()
1362 netdev_dbg(priv->ndev, in rcar_canfd_set_bittiming()
1371 struct rcar_canfd_global *gpriv = priv->gpriv; in rcar_canfd_start()
1372 int err = -EOPNOTSUPP; in rcar_canfd_start()
1373 u32 sts, ch = priv->channel; in rcar_canfd_start()
1381 rcar_canfd_update_bit(priv->base, RCANFD_CCTR(ch), in rcar_canfd_start()
1385 err = readl_poll_timeout((priv->base + RCANFD_CSTS(ch)), sts, in rcar_canfd_start()
1386 (sts & RCANFD_CSTS_COMSTS), 2, 500000); in rcar_canfd_start()
1393 rcar_canfd_set_bit(priv->base, RCANFD_CFCC(gpriv, ch, RCANFD_CFFIFO_IDX), in rcar_canfd_start()
1395 rcar_canfd_set_bit(priv->base, RCANFD_RFCC(gpriv, ridx), RCANFD_RFCC_RFE); in rcar_canfd_start()
1397 priv->can.state = CAN_STATE_ERROR_ACTIVE; in rcar_canfd_start()
1408 struct rcar_canfd_global *gpriv = priv->gpriv; in rcar_canfd_open()
1411 err = phy_power_on(priv->transceiver); in rcar_canfd_open()
1418 err = clk_prepare_enable(gpriv->can_clk); in rcar_canfd_open()
1430 napi_enable(&priv->napi); in rcar_canfd_open()
1437 napi_disable(&priv->napi); in rcar_canfd_open()
1440 clk_disable_unprepare(gpriv->can_clk); in rcar_canfd_open()
1442 phy_power_off(priv->transceiver); in rcar_canfd_open()
1449 struct rcar_canfd_global *gpriv = priv->gpriv; in rcar_canfd_stop()
1451 u32 sts, ch = priv->channel; in rcar_canfd_stop()
1455 rcar_canfd_update_bit(priv->base, RCANFD_CCTR(ch), in rcar_canfd_stop()
1459 err = readl_poll_timeout((priv->base + RCANFD_CSTS(ch)), sts, in rcar_canfd_stop()
1460 (sts & RCANFD_CSTS_CRSTSTS), 2, 500000); in rcar_canfd_stop()
1467 rcar_canfd_clear_bit(priv->base, RCANFD_CFCC(gpriv, ch, RCANFD_CFFIFO_IDX), in rcar_canfd_stop()
1469 rcar_canfd_clear_bit(priv->base, RCANFD_RFCC(gpriv, ridx), RCANFD_RFCC_RFE); in rcar_canfd_stop()
1472 priv->can.state = CAN_STATE_STOPPED; in rcar_canfd_stop()
1478 struct rcar_canfd_global *gpriv = priv->gpriv; in rcar_canfd_close()
1482 napi_disable(&priv->napi); in rcar_canfd_close()
1483 clk_disable_unprepare(gpriv->can_clk); in rcar_canfd_close()
1485 phy_power_off(priv->transceiver); in rcar_canfd_close()
1493 struct rcar_canfd_global *gpriv = priv->gpriv; in rcar_canfd_start_xmit()
1494 struct canfd_frame *cf = (struct canfd_frame *)skb->data; in rcar_canfd_start_xmit()
1497 u32 ch = priv->channel; in rcar_canfd_start_xmit()
1502 if (cf->can_id & CAN_EFF_FLAG) { in rcar_canfd_start_xmit()
1503 id = cf->can_id & CAN_EFF_MASK; in rcar_canfd_start_xmit()
1506 id = cf->can_id & CAN_SFF_MASK; in rcar_canfd_start_xmit()
1509 if (cf->can_id & CAN_RTR_FLAG) in rcar_canfd_start_xmit()
1512 dlc = RCANFD_CFPTR_CFDLC(can_fd_len2dlc(cf->len)); in rcar_canfd_start_xmit()
1514 if ((priv->can.ctrlmode & CAN_CTRLMODE_FD) || is_gen4(gpriv)) { in rcar_canfd_start_xmit()
1515 rcar_canfd_write(priv->base, in rcar_canfd_start_xmit()
1517 rcar_canfd_write(priv->base, in rcar_canfd_start_xmit()
1523 if (cf->flags & CANFD_BRS) in rcar_canfd_start_xmit()
1526 if (priv->can.state == CAN_STATE_ERROR_PASSIVE) in rcar_canfd_start_xmit()
1530 rcar_canfd_write(priv->base, in rcar_canfd_start_xmit()
1536 rcar_canfd_write(priv->base, in rcar_canfd_start_xmit()
1538 rcar_canfd_write(priv->base, in rcar_canfd_start_xmit()
1544 can_put_echo_skb(skb, ndev, priv->tx_head % RCANFD_FIFO_DEPTH, 0); in rcar_canfd_start_xmit()
1546 spin_lock_irqsave(&priv->tx_lock, flags); in rcar_canfd_start_xmit()
1547 priv->tx_head++; in rcar_canfd_start_xmit()
1550 if (priv->tx_head - priv->tx_tail >= RCANFD_FIFO_DEPTH) in rcar_canfd_start_xmit()
1553 /* Start Tx: Write 0xff to CFPC to increment the CPU-side in rcar_canfd_start_xmit()
1556 rcar_canfd_write(priv->base, in rcar_canfd_start_xmit()
1559 spin_unlock_irqrestore(&priv->tx_lock, flags); in rcar_canfd_start_xmit()
1565 struct net_device_stats *stats = &priv->ndev->stats; in rcar_canfd_rx_pkt()
1566 struct rcar_canfd_global *gpriv = priv->gpriv; in rcar_canfd_rx_pkt()
1570 u32 ch = priv->channel; in rcar_canfd_rx_pkt()
1573 if ((priv->can.ctrlmode & CAN_CTRLMODE_FD) || is_gen4(gpriv)) { in rcar_canfd_rx_pkt()
1574 id = rcar_canfd_read(priv->base, RCANFD_F_RFID(gpriv, ridx)); in rcar_canfd_rx_pkt()
1575 dlc = rcar_canfd_read(priv->base, RCANFD_F_RFPTR(gpriv, ridx)); in rcar_canfd_rx_pkt()
1577 sts = rcar_canfd_read(priv->base, RCANFD_F_RFFDSTS(gpriv, ridx)); in rcar_canfd_rx_pkt()
1579 if ((priv->can.ctrlmode & CAN_CTRLMODE_FD) && in rcar_canfd_rx_pkt()
1581 skb = alloc_canfd_skb(priv->ndev, &cf); in rcar_canfd_rx_pkt()
1583 skb = alloc_can_skb(priv->ndev, in rcar_canfd_rx_pkt()
1586 id = rcar_canfd_read(priv->base, RCANFD_C_RFID(ridx)); in rcar_canfd_rx_pkt()
1587 dlc = rcar_canfd_read(priv->base, RCANFD_C_RFPTR(ridx)); in rcar_canfd_rx_pkt()
1588 skb = alloc_can_skb(priv->ndev, (struct can_frame **)&cf); in rcar_canfd_rx_pkt()
1592 stats->rx_dropped++; in rcar_canfd_rx_pkt()
1597 cf->can_id = (id & CAN_EFF_MASK) | CAN_EFF_FLAG; in rcar_canfd_rx_pkt()
1599 cf->can_id = id & CAN_SFF_MASK; in rcar_canfd_rx_pkt()
1601 if (priv->can.ctrlmode & CAN_CTRLMODE_FD) { in rcar_canfd_rx_pkt()
1603 cf->len = can_fd_dlc2len(RCANFD_RFPTR_RFDLC(dlc)); in rcar_canfd_rx_pkt()
1605 cf->len = can_cc_dlc2len(RCANFD_RFPTR_RFDLC(dlc)); in rcar_canfd_rx_pkt()
1608 cf->flags |= CANFD_ESI; in rcar_canfd_rx_pkt()
1609 netdev_dbg(priv->ndev, "ESI Error\n"); in rcar_canfd_rx_pkt()
1613 cf->can_id |= CAN_RTR_FLAG; in rcar_canfd_rx_pkt()
1616 cf->flags |= CANFD_BRS; in rcar_canfd_rx_pkt()
1621 cf->len = can_cc_dlc2len(RCANFD_RFPTR_RFDLC(dlc)); in rcar_canfd_rx_pkt()
1623 cf->can_id |= CAN_RTR_FLAG; in rcar_canfd_rx_pkt()
1630 /* Write 0xff to RFPC to increment the CPU-side in rcar_canfd_rx_pkt()
1633 rcar_canfd_write(priv->base, RCANFD_RFPCTR(gpriv, ridx), 0xff); in rcar_canfd_rx_pkt()
1635 if (!(cf->can_id & CAN_RTR_FLAG)) in rcar_canfd_rx_pkt()
1636 stats->rx_bytes += cf->len; in rcar_canfd_rx_pkt()
1637 stats->rx_packets++; in rcar_canfd_rx_pkt()
1645 struct rcar_canfd_global *gpriv = priv->gpriv; in rcar_canfd_rx_poll()
1648 u32 ch = priv->channel; in rcar_canfd_rx_poll()
1652 sts = rcar_canfd_read(priv->base, RCANFD_RFSTS(gpriv, ridx)); in rcar_canfd_rx_poll()
1661 rcar_canfd_write(priv->base, RCANFD_RFSTS(gpriv, ridx), in rcar_canfd_rx_poll()
1669 rcar_canfd_set_bit(priv->base, RCANFD_RFCC(gpriv, ridx), in rcar_canfd_rx_poll()
1688 return -EOPNOTSUPP; in rcar_canfd_do_set_mode()
1696 u32 val, ch = priv->channel; in rcar_canfd_get_berr_counter()
1699 val = rcar_canfd_read(priv->base, RCANFD_CSTS(ch)); in rcar_canfd_get_berr_counter()
1700 bec->txerr = RCANFD_CSTS_TECCNT(val); in rcar_canfd_get_berr_counter()
1701 bec->rxerr = RCANFD_CSTS_RECCNT(val); in rcar_canfd_get_berr_counter()
1719 const struct rcar_canfd_hw_info *info = gpriv->info; in rcar_canfd_channel_probe()
1720 struct platform_device *pdev = gpriv->pdev; in rcar_canfd_channel_probe()
1721 struct device *dev = &pdev->dev; in rcar_canfd_channel_probe()
1724 int err = -ENODEV; in rcar_canfd_channel_probe()
1728 return -ENOMEM; in rcar_canfd_channel_probe()
1732 ndev->netdev_ops = &rcar_canfd_netdev_ops; in rcar_canfd_channel_probe()
1733 ndev->ethtool_ops = &rcar_canfd_ethtool_ops; in rcar_canfd_channel_probe()
1734 ndev->flags |= IFF_ECHO; in rcar_canfd_channel_probe()
1735 priv->ndev = ndev; in rcar_canfd_channel_probe()
1736 priv->base = gpriv->base; in rcar_canfd_channel_probe()
1737 priv->transceiver = transceiver; in rcar_canfd_channel_probe()
1738 priv->channel = ch; in rcar_canfd_channel_probe()
1739 priv->gpriv = gpriv; in rcar_canfd_channel_probe()
1741 priv->can.bitrate_max = transceiver->attrs.max_link_rate; in rcar_canfd_channel_probe()
1742 priv->can.clock.freq = fcan_freq; in rcar_canfd_channel_probe()
1743 dev_info(dev, "can_clk rate is %u\n", priv->can.clock.freq); in rcar_canfd_channel_probe()
1745 if (info->multi_channel_irqs) { in rcar_canfd_channel_probe()
1762 irq_name = devm_kasprintf(dev, GFP_KERNEL, "canfd.ch%d_err", in rcar_canfd_channel_probe()
1765 err = -ENOMEM; in rcar_canfd_channel_probe()
1776 irq_name = devm_kasprintf(dev, GFP_KERNEL, "canfd.ch%d_trx", in rcar_canfd_channel_probe()
1779 err = -ENOMEM; in rcar_canfd_channel_probe()
1792 if (gpriv->fdmode) { in rcar_canfd_channel_probe()
1793 priv->can.bittiming_const = &rcar_canfd_nom_bittiming_const; in rcar_canfd_channel_probe()
1794 priv->can.data_bittiming_const = in rcar_canfd_channel_probe()
1801 priv->can.ctrlmode_supported = CAN_CTRLMODE_BERR_REPORTING; in rcar_canfd_channel_probe()
1804 priv->can.bittiming_const = &rcar_canfd_bittiming_const; in rcar_canfd_channel_probe()
1805 priv->can.ctrlmode_supported = CAN_CTRLMODE_BERR_REPORTING; in rcar_canfd_channel_probe()
1808 priv->can.do_set_mode = rcar_canfd_do_set_mode; in rcar_canfd_channel_probe()
1809 priv->can.do_get_berr_counter = rcar_canfd_get_berr_counter; in rcar_canfd_channel_probe()
1812 netif_napi_add_weight(ndev, &priv->napi, rcar_canfd_rx_poll, in rcar_canfd_channel_probe()
1814 spin_lock_init(&priv->tx_lock); in rcar_canfd_channel_probe()
1815 gpriv->ch[priv->channel] = priv; in rcar_canfd_channel_probe()
1821 dev_info(dev, "device registered (channel %u)\n", priv->channel); in rcar_canfd_channel_probe()
1825 netif_napi_del(&priv->napi); in rcar_canfd_channel_probe()
1833 struct rcar_canfd_channel *priv = gpriv->ch[ch]; in rcar_canfd_channel_remove()
1836 unregister_candev(priv->ndev); in rcar_canfd_channel_remove()
1837 netif_napi_del(&priv->napi); in rcar_canfd_channel_remove()
1838 free_candev(priv->ndev); in rcar_canfd_channel_remove()
1846 struct device *dev = &pdev->dev; in rcar_canfd_probe()
1854 bool fdmode = true; /* CAN FD only mode - default */ in rcar_canfd_probe()
1860 if (of_property_read_bool(dev->of_node, "renesas,no-can-fd")) in rcar_canfd_probe()
1863 for (i = 0; i < info->max_channels; ++i) { in rcar_canfd_probe()
1865 of_child = of_get_child_by_name(dev->of_node, name); in rcar_canfd_probe()
1876 if (info->shared_global_irqs) { in rcar_canfd_probe()
1905 return -ENOMEM; in rcar_canfd_probe()
1907 gpriv->pdev = pdev; in rcar_canfd_probe()
1908 gpriv->channels_mask = channels_mask; in rcar_canfd_probe()
1909 gpriv->fdmode = fdmode; in rcar_canfd_probe()
1910 gpriv->info = info; in rcar_canfd_probe()
1912 gpriv->rstc1 = devm_reset_control_get_optional_exclusive(dev, "rstp_n"); in rcar_canfd_probe()
1913 if (IS_ERR(gpriv->rstc1)) in rcar_canfd_probe()
1914 return dev_err_probe(dev, PTR_ERR(gpriv->rstc1), in rcar_canfd_probe()
1917 gpriv->rstc2 = devm_reset_control_get_optional_exclusive(dev, "rstc_n"); in rcar_canfd_probe()
1918 if (IS_ERR(gpriv->rstc2)) in rcar_canfd_probe()
1919 return dev_err_probe(dev, PTR_ERR(gpriv->rstc2), in rcar_canfd_probe()
1923 gpriv->clkp = devm_clk_get(dev, "fck"); in rcar_canfd_probe()
1924 if (IS_ERR(gpriv->clkp)) in rcar_canfd_probe()
1925 return dev_err_probe(dev, PTR_ERR(gpriv->clkp), in rcar_canfd_probe()
1929 * CANFD clock in rcar_canfd_probe()
1931 gpriv->can_clk = devm_clk_get(dev, "can_clk"); in rcar_canfd_probe()
1932 if (IS_ERR(gpriv->can_clk) || (clk_get_rate(gpriv->can_clk) == 0)) { in rcar_canfd_probe()
1933 gpriv->can_clk = devm_clk_get(dev, "canfd"); in rcar_canfd_probe()
1934 if (IS_ERR(gpriv->can_clk)) in rcar_canfd_probe()
1935 return dev_err_probe(dev, PTR_ERR(gpriv->can_clk), in rcar_canfd_probe()
1936 "cannot get canfd clock\n"); in rcar_canfd_probe()
1938 /* CANFD clock may be further divided within the IP */ in rcar_canfd_probe()
1939 fcan_freq = clk_get_rate(gpriv->can_clk) / info->postdiv; in rcar_canfd_probe()
1941 fcan_freq = clk_get_rate(gpriv->can_clk); in rcar_canfd_probe()
1942 gpriv->extclk = true; in rcar_canfd_probe()
1950 gpriv->base = addr; in rcar_canfd_probe()
1953 if (info->shared_global_irqs) { in rcar_canfd_probe()
1956 "canfd.ch_int", gpriv); in rcar_canfd_probe()
1964 0, "canfd.g_int", gpriv); in rcar_canfd_probe()
1973 "canfd.g_recc", gpriv); in rcar_canfd_probe()
1983 "canfd.g_err", gpriv); in rcar_canfd_probe()
1991 err = reset_control_reset(gpriv->rstc1); in rcar_canfd_probe()
1994 err = reset_control_reset(gpriv->rstc2); in rcar_canfd_probe()
1996 reset_control_assert(gpriv->rstc1); in rcar_canfd_probe()
2001 err = clk_prepare_enable(gpriv->clkp); in rcar_canfd_probe()
2018 for_each_set_bit(ch, &gpriv->channels_mask, info->max_channels) { in rcar_canfd_probe()
2033 rcar_canfd_update_bit(gpriv->base, RCANFD_GCTR, RCANFD_GCTR_GMDC_MASK, in rcar_canfd_probe()
2037 err = readl_poll_timeout((gpriv->base + RCANFD_GSTS), sts, in rcar_canfd_probe()
2038 !(sts & RCANFD_GSTS_GNOPM), 2, 500000); in rcar_canfd_probe()
2044 for_each_set_bit(ch, &gpriv->channels_mask, info->max_channels) { in rcar_canfd_probe()
2053 gpriv->extclk ? "ext" : "canfd", in rcar_canfd_probe()
2054 gpriv->fdmode ? "fd" : "classical"); in rcar_canfd_probe()
2058 for_each_set_bit(ch, &gpriv->channels_mask, info->max_channels) in rcar_canfd_probe()
2063 clk_disable_unprepare(gpriv->clkp); in rcar_canfd_probe()
2065 reset_control_assert(gpriv->rstc1); in rcar_canfd_probe()
2066 reset_control_assert(gpriv->rstc2); in rcar_canfd_probe()
2079 for_each_set_bit(ch, &gpriv->channels_mask, gpriv->info->max_channels) { in rcar_canfd_remove()
2080 rcar_canfd_disable_channel_interrupts(gpriv->ch[ch]); in rcar_canfd_remove()
2085 rcar_canfd_set_bit(gpriv->base, RCANFD_GCTR, RCANFD_GCTR_GSLPR); in rcar_canfd_remove()
2086 clk_disable_unprepare(gpriv->clkp); in rcar_canfd_remove()
2087 reset_control_assert(gpriv->rstc1); in rcar_canfd_remove()
2088 reset_control_assert(gpriv->rstc2); in rcar_canfd_remove()
2105 { .compatible = "renesas,r8a779a0-canfd", .data = &rcar_gen4_hw_info },
2106 { .compatible = "renesas,rcar-gen3-canfd", .data = &rcar_gen3_hw_info },
2107 { .compatible = "renesas,rcar-gen4-canfd", .data = &rcar_gen4_hw_info },
2108 { .compatible = "renesas,rzg2l-canfd", .data = &rzg2l_hw_info },
2128 MODULE_DESCRIPTION("CAN FD driver for Renesas R-Car SoC");