Lines Matching +full:regulator +full:- +full:soft +full:- +full:start
1 // SPDX-License-Identifier: GPL-2.0
3 // flexcan.c - FLEXCAN CAN controller driver
5 // Copyright (c) 2005-2006 Varma Electronics Oy
7 // Copyright (c) 2010-2017 Pengutronix, Marc Kleine-Budde <kernel@pengutronix.de>
10 // Based on code originally by Andrey Volkov <avolkov@varma-el.com>
17 #include <linux/can/rx-offload.h>
31 #include <linux/regulator/consumer.h>
209 * SOC Version IP-Version Glitch- [TR]WRN_INT IRQ Err Memory err RTR rece- FD Mode
231 /* Disable non-correctable errors interrupt and freeze mode */
241 /* Support CAN-FD mode */
256 u32 ctrl; /* 0x04 - Not affected by Soft Reset */
259 u32 rxgmask; /* 0x10 - Not affected by Soft Reset */
260 u32 rx14mask; /* 0x14 - Not affected by Soft Reset */
261 u32 rx15mask; /* 0x18 - Not affected by Soft Reset */
270 u32 ctrl2; /* MX6, VF610 - Not affected by Soft Reset */
277 u32 rxfir; /* 0x4c - Not affected by Soft Reset */
278 u32 cbt; /* 0x50 - Not affected by Soft Reset */
283 u8 mb[2][512]; /* 0x80 - Not affected by Soft Reset */
284 /* FIFO-mode:
287 * 0x090...0x0df 1-5 reserved
288 * 0x0e0...0x0ff 6-7 8 entry ID table
290 * 0x0e0...0x2df 6-7..37 8..128 entry ID table
295 u32 rximr[64]; /* 0x880 - Not affected by Soft Reset */
297 u32 gfwr_mx6; /* 0x9e0 - MX6 */
317 u32 fdctrl; /* 0xc00 - Not affected by Soft Reset */
318 u32 fdcbt; /* 0xc04 - Not affected by Soft Reset */
358 struct regulator *reg_xceiver;
454 /* FlexCAN module is essentially modelled as a little-endian IP in most
456 * implemented in a little-endian fashion.
459 * module in a big-endian fashion (i.e the registers as well as the
460 * message buffer areas are implemented in a big-endian way).
493 if (WARN_ON(mb_index >= priv->mb_count)) in flexcan_get_mb()
496 bank_size = sizeof(priv->regs->mb[0]) / priv->mb_size; in flexcan_get_mb()
500 mb_index -= bank_size; in flexcan_get_mb()
503 (&priv->regs->mb[bank][priv->mb_size * mb_index]); in flexcan_get_mb()
508 struct flexcan_regs __iomem *regs = priv->regs; in flexcan_low_power_enter_ack()
511 while (timeout-- && !(priv->read(®s->mcr) & FLEXCAN_MCR_LPM_ACK)) in flexcan_low_power_enter_ack()
514 if (!(priv->read(®s->mcr) & FLEXCAN_MCR_LPM_ACK)) in flexcan_low_power_enter_ack()
515 return -ETIMEDOUT; in flexcan_low_power_enter_ack()
522 struct flexcan_regs __iomem *regs = priv->regs; in flexcan_low_power_exit_ack()
525 while (timeout-- && (priv->read(®s->mcr) & FLEXCAN_MCR_LPM_ACK)) in flexcan_low_power_exit_ack()
528 if (priv->read(®s->mcr) & FLEXCAN_MCR_LPM_ACK) in flexcan_low_power_exit_ack()
529 return -ETIMEDOUT; in flexcan_low_power_exit_ack()
536 struct flexcan_regs __iomem *regs = priv->regs; in flexcan_enable_wakeup_irq()
539 reg_mcr = priv->read(®s->mcr); in flexcan_enable_wakeup_irq()
546 priv->write(reg_mcr, ®s->mcr); in flexcan_enable_wakeup_irq()
551 struct flexcan_regs __iomem *regs = priv->regs; in flexcan_enter_stop_mode()
554 reg_mcr = priv->read(®s->mcr); in flexcan_enter_stop_mode()
556 priv->write(reg_mcr, ®s->mcr); in flexcan_enter_stop_mode()
559 regmap_update_bits(priv->stm.gpr, priv->stm.req_gpr, in flexcan_enter_stop_mode()
560 1 << priv->stm.req_bit, 1 << priv->stm.req_bit); in flexcan_enter_stop_mode()
567 struct flexcan_regs __iomem *regs = priv->regs; in flexcan_exit_stop_mode()
571 regmap_update_bits(priv->stm.gpr, priv->stm.req_gpr, in flexcan_exit_stop_mode()
572 1 << priv->stm.req_bit, 0); in flexcan_exit_stop_mode()
574 reg_mcr = priv->read(®s->mcr); in flexcan_exit_stop_mode()
576 priv->write(reg_mcr, ®s->mcr); in flexcan_exit_stop_mode()
583 struct flexcan_regs __iomem *regs = priv->regs; in flexcan_error_irq_enable()
584 u32 reg_ctrl = (priv->reg_ctrl_default | FLEXCAN_CTRL_ERR_MSK); in flexcan_error_irq_enable()
586 priv->write(reg_ctrl, ®s->ctrl); in flexcan_error_irq_enable()
591 struct flexcan_regs __iomem *regs = priv->regs; in flexcan_error_irq_disable()
592 u32 reg_ctrl = (priv->reg_ctrl_default & ~FLEXCAN_CTRL_ERR_MSK); in flexcan_error_irq_disable()
594 priv->write(reg_ctrl, ®s->ctrl); in flexcan_error_irq_disable()
601 err = clk_prepare_enable(priv->clk_ipg); in flexcan_clks_enable()
605 err = clk_prepare_enable(priv->clk_per); in flexcan_clks_enable()
607 clk_disable_unprepare(priv->clk_ipg); in flexcan_clks_enable()
614 clk_disable_unprepare(priv->clk_per); in flexcan_clks_disable()
615 clk_disable_unprepare(priv->clk_ipg); in flexcan_clks_disable()
620 if (!priv->reg_xceiver) in flexcan_transceiver_enable()
623 return regulator_enable(priv->reg_xceiver); in flexcan_transceiver_enable()
628 if (!priv->reg_xceiver) in flexcan_transceiver_disable()
631 return regulator_disable(priv->reg_xceiver); in flexcan_transceiver_disable()
636 struct flexcan_regs __iomem *regs = priv->regs; in flexcan_chip_enable()
639 reg = priv->read(®s->mcr); in flexcan_chip_enable()
641 priv->write(reg, ®s->mcr); in flexcan_chip_enable()
648 struct flexcan_regs __iomem *regs = priv->regs; in flexcan_chip_disable()
651 reg = priv->read(®s->mcr); in flexcan_chip_disable()
653 priv->write(reg, ®s->mcr); in flexcan_chip_disable()
660 struct flexcan_regs __iomem *regs = priv->regs; in flexcan_chip_freeze()
661 unsigned int timeout = 1000 * 1000 * 10 / priv->can.bittiming.bitrate; in flexcan_chip_freeze()
664 reg = priv->read(®s->mcr); in flexcan_chip_freeze()
666 priv->write(reg, ®s->mcr); in flexcan_chip_freeze()
668 while (timeout-- && !(priv->read(®s->mcr) & FLEXCAN_MCR_FRZ_ACK)) in flexcan_chip_freeze()
671 if (!(priv->read(®s->mcr) & FLEXCAN_MCR_FRZ_ACK)) in flexcan_chip_freeze()
672 return -ETIMEDOUT; in flexcan_chip_freeze()
679 struct flexcan_regs __iomem *regs = priv->regs; in flexcan_chip_unfreeze()
683 reg = priv->read(®s->mcr); in flexcan_chip_unfreeze()
685 priv->write(reg, ®s->mcr); in flexcan_chip_unfreeze()
687 while (timeout-- && (priv->read(®s->mcr) & FLEXCAN_MCR_FRZ_ACK)) in flexcan_chip_unfreeze()
690 if (priv->read(®s->mcr) & FLEXCAN_MCR_FRZ_ACK) in flexcan_chip_unfreeze()
691 return -ETIMEDOUT; in flexcan_chip_unfreeze()
698 struct flexcan_regs __iomem *regs = priv->regs; in flexcan_chip_softreset()
701 priv->write(FLEXCAN_MCR_SOFTRST, ®s->mcr); in flexcan_chip_softreset()
702 while (timeout-- && (priv->read(®s->mcr) & FLEXCAN_MCR_SOFTRST)) in flexcan_chip_softreset()
705 if (priv->read(®s->mcr) & FLEXCAN_MCR_SOFTRST) in flexcan_chip_softreset()
706 return -ETIMEDOUT; in flexcan_chip_softreset()
715 struct flexcan_regs __iomem *regs = priv->regs; in __flexcan_get_berr_counter()
716 u32 reg = priv->read(®s->ecr); in __flexcan_get_berr_counter()
718 bec->txerr = (reg >> 0) & 0xff; in __flexcan_get_berr_counter()
719 bec->rxerr = (reg >> 8) & 0xff; in __flexcan_get_berr_counter()
730 err = pm_runtime_get_sync(priv->dev); in flexcan_get_berr_counter()
732 pm_runtime_put_noidle(priv->dev); in flexcan_get_berr_counter()
738 pm_runtime_put(priv->dev); in flexcan_get_berr_counter()
746 struct canfd_frame *cfd = (struct canfd_frame *)skb->data; in flexcan_start_xmit()
749 u32 ctrl = FLEXCAN_MB_CODE_TX_DATA | ((can_len2dlc(cfd->len)) << 16); in flexcan_start_xmit()
757 if (cfd->can_id & CAN_EFF_FLAG) { in flexcan_start_xmit()
758 can_id = cfd->can_id & CAN_EFF_MASK; in flexcan_start_xmit()
761 can_id = (cfd->can_id & CAN_SFF_MASK) << 18; in flexcan_start_xmit()
764 if (cfd->can_id & CAN_RTR_FLAG) in flexcan_start_xmit()
770 if (cfd->flags & CANFD_BRS) in flexcan_start_xmit()
774 for (i = 0; i < cfd->len; i += sizeof(u32)) { in flexcan_start_xmit()
775 data = be32_to_cpup((__be32 *)&cfd->data[i]); in flexcan_start_xmit()
776 priv->write(data, &priv->tx_mb->data[i / sizeof(u32)]); in flexcan_start_xmit()
781 priv->write(can_id, &priv->tx_mb->can_id); in flexcan_start_xmit()
782 priv->write(ctrl, &priv->tx_mb->can_ctrl); in flexcan_start_xmit()
787 priv->write(FLEXCAN_MB_CODE_TX_INACTIVE, in flexcan_start_xmit()
788 &priv->tx_mb_reserved->can_ctrl); in flexcan_start_xmit()
789 priv->write(FLEXCAN_MB_CODE_TX_INACTIVE, in flexcan_start_xmit()
790 &priv->tx_mb_reserved->can_ctrl); in flexcan_start_xmit()
798 struct flexcan_regs __iomem *regs = priv->regs; in flexcan_irq_bus_err()
805 timestamp = priv->read(®s->timer) << 16; in flexcan_irq_bus_err()
811 cf->can_id |= CAN_ERR_PROT | CAN_ERR_BUSERROR; in flexcan_irq_bus_err()
815 cf->data[2] |= CAN_ERR_PROT_BIT1; in flexcan_irq_bus_err()
820 cf->data[2] |= CAN_ERR_PROT_BIT0; in flexcan_irq_bus_err()
825 cf->can_id |= CAN_ERR_ACK; in flexcan_irq_bus_err()
826 cf->data[3] = CAN_ERR_PROT_LOC_ACK; in flexcan_irq_bus_err()
831 cf->data[2] |= CAN_ERR_PROT_BIT; in flexcan_irq_bus_err()
832 cf->data[3] = CAN_ERR_PROT_LOC_CRC_SEQ; in flexcan_irq_bus_err()
837 cf->data[2] |= CAN_ERR_PROT_FORM; in flexcan_irq_bus_err()
842 cf->data[2] |= CAN_ERR_PROT_STUFF; in flexcan_irq_bus_err()
846 priv->can.can_stats.bus_error++; in flexcan_irq_bus_err()
848 dev->stats.rx_errors++; in flexcan_irq_bus_err()
850 dev->stats.tx_errors++; in flexcan_irq_bus_err()
852 err = can_rx_offload_queue_sorted(&priv->offload, skb, timestamp); in flexcan_irq_bus_err()
854 dev->stats.rx_fifo_errors++; in flexcan_irq_bus_err()
860 struct flexcan_regs __iomem *regs = priv->regs; in flexcan_irq_state()
885 if (likely(new_state == priv->can.state)) in flexcan_irq_state()
888 timestamp = priv->read(®s->timer) << 16; in flexcan_irq_state()
899 err = can_rx_offload_queue_sorted(&priv->offload, skb, timestamp); in flexcan_irq_state()
901 dev->stats.rx_fifo_errors++; in flexcan_irq_state()
909 reg = (u64)priv->read(addr - 4) << 32; in flexcan_read64_mask()
911 reg |= priv->read(addr); in flexcan_read64_mask()
919 priv->write(upper_32_bits(val), addr - 4); in flexcan_write64()
921 priv->write(lower_32_bits(val), addr); in flexcan_write64()
926 return flexcan_read64_mask(priv, &priv->regs->iflag1, priv->rx_mask); in flexcan_read_reg_iflag_rx()
931 return flexcan_read64_mask(priv, &priv->regs->iflag1, priv->tx_mask); in flexcan_read_reg_iflag_tx()
944 struct flexcan_regs __iomem *regs = priv->regs; in flexcan_mailbox_read()
952 skb = ERR_PTR(-ENOBUFS); in flexcan_mailbox_read()
958 if (priv->devtype_data->quirks & FLEXCAN_QUIRK_USE_OFF_TIMESTAMP) { in flexcan_mailbox_read()
962 reg_ctrl = priv->read(&mb->can_ctrl); in flexcan_mailbox_read()
973 offload->dev->stats.rx_over_errors++; in flexcan_mailbox_read()
974 offload->dev->stats.rx_errors++; in flexcan_mailbox_read()
977 reg_iflag1 = priv->read(®s->iflag1); in flexcan_mailbox_read()
981 reg_ctrl = priv->read(&mb->can_ctrl); in flexcan_mailbox_read()
985 skb = alloc_canfd_skb(offload->dev, &cfd); in flexcan_mailbox_read()
987 skb = alloc_can_skb(offload->dev, (struct can_frame **)&cfd); in flexcan_mailbox_read()
989 skb = ERR_PTR(-ENOMEM); in flexcan_mailbox_read()
996 reg_id = priv->read(&mb->can_id); in flexcan_mailbox_read()
998 cfd->can_id = ((reg_id >> 0) & CAN_EFF_MASK) | CAN_EFF_FLAG; in flexcan_mailbox_read()
1000 cfd->can_id = (reg_id >> 18) & CAN_SFF_MASK; in flexcan_mailbox_read()
1003 cfd->len = can_dlc2len(get_canfd_dlc((reg_ctrl >> 16) & 0xf)); in flexcan_mailbox_read()
1006 cfd->flags |= CANFD_BRS; in flexcan_mailbox_read()
1008 cfd->len = get_can_dlc((reg_ctrl >> 16) & 0xf); in flexcan_mailbox_read()
1011 cfd->can_id |= CAN_RTR_FLAG; in flexcan_mailbox_read()
1015 cfd->flags |= CANFD_ESI; in flexcan_mailbox_read()
1017 for (i = 0; i < cfd->len; i += sizeof(u32)) { in flexcan_mailbox_read()
1018 __be32 data = cpu_to_be32(priv->read(&mb->data[i / sizeof(u32)])); in flexcan_mailbox_read()
1019 *(__be32 *)(cfd->data + i) = data; in flexcan_mailbox_read()
1023 if (priv->devtype_data->quirks & FLEXCAN_QUIRK_USE_OFF_TIMESTAMP) in flexcan_mailbox_read()
1024 flexcan_write64(priv, FLEXCAN_IFLAG_MB(n), ®s->iflag1); in flexcan_mailbox_read()
1026 priv->write(FLEXCAN_IFLAG_RX_FIFO_AVAILABLE, ®s->iflag1); in flexcan_mailbox_read()
1032 priv->read(®s->timer); in flexcan_mailbox_read()
1040 struct net_device_stats *stats = &dev->stats; in flexcan_irq()
1042 struct flexcan_regs __iomem *regs = priv->regs; in flexcan_irq()
1046 enum can_state last_state = priv->can.state; in flexcan_irq()
1049 if (priv->devtype_data->quirks & FLEXCAN_QUIRK_USE_OFF_TIMESTAMP) { in flexcan_irq()
1055 ret = can_rx_offload_irq_offload_timestamp(&priv->offload, in flexcan_irq()
1063 reg_iflag1 = priv->read(®s->iflag1); in flexcan_irq()
1066 can_rx_offload_irq_offload_fifo(&priv->offload); in flexcan_irq()
1072 priv->write(FLEXCAN_IFLAG_RX_FIFO_OVERFLOW, in flexcan_irq()
1073 ®s->iflag1); in flexcan_irq()
1074 dev->stats.rx_over_errors++; in flexcan_irq()
1075 dev->stats.rx_errors++; in flexcan_irq()
1082 if (reg_iflag_tx & priv->tx_mask) { in flexcan_irq()
1083 u32 reg_ctrl = priv->read(&priv->tx_mb->can_ctrl); in flexcan_irq()
1086 stats->tx_bytes += can_rx_offload_get_echo_skb(&priv->offload, in flexcan_irq()
1088 stats->tx_packets++; in flexcan_irq()
1092 priv->write(FLEXCAN_MB_CODE_TX_INACTIVE, in flexcan_irq()
1093 &priv->tx_mb->can_ctrl); in flexcan_irq()
1094 flexcan_write64(priv, priv->tx_mask, ®s->iflag1); in flexcan_irq()
1098 reg_esr = priv->read(®s->esr); in flexcan_irq()
1103 priv->write(reg_esr & (FLEXCAN_ESR_ALL_INT | FLEXCAN_ESR_WAK_INT), ®s->esr); in flexcan_irq()
1108 (priv->devtype_data->quirks & (FLEXCAN_QUIRK_BROKEN_WERR_STATE | in flexcan_irq()
1112 /* bus error IRQ - handle if bus error reporting is activated */ in flexcan_irq()
1114 (priv->can.ctrlmode & CAN_CTRLMODE_BERR_REPORTING)) in flexcan_irq()
1118 * bus error reporting is de-activated and in flexcan_irq()
1120 * +--------------------------------------------------------------+ in flexcan_irq()
1121 * | +----------------------------------------------+ [stopped / | in flexcan_irq()
1122 * | | | sleeping] -+ in flexcan_irq()
1123 * +-+-> active <-> warning <-> passive -> bus off -+ in flexcan_irq()
1129 if ((last_state != priv->can.state) && in flexcan_irq()
1130 (priv->devtype_data->quirks & FLEXCAN_QUIRK_BROKEN_PERR_STATE) && in flexcan_irq()
1131 !(priv->can.ctrlmode & CAN_CTRLMODE_BERR_REPORTING)) { in flexcan_irq()
1132 switch (priv->can.state) { in flexcan_irq()
1134 if (priv->devtype_data->quirks & in flexcan_irq()
1161 const struct can_bittiming *bt = &priv->can.bittiming; in flexcan_set_bittiming_ctrl()
1162 struct flexcan_regs __iomem *regs = priv->regs; in flexcan_set_bittiming_ctrl()
1165 reg = priv->read(®s->ctrl); in flexcan_set_bittiming_ctrl()
1172 reg |= FLEXCAN_CTRL_PRESDIV(bt->brp - 1) | in flexcan_set_bittiming_ctrl()
1173 FLEXCAN_CTRL_PSEG1(bt->phase_seg1 - 1) | in flexcan_set_bittiming_ctrl()
1174 FLEXCAN_CTRL_PSEG2(bt->phase_seg2 - 1) | in flexcan_set_bittiming_ctrl()
1175 FLEXCAN_CTRL_RJW(bt->sjw - 1) | in flexcan_set_bittiming_ctrl()
1176 FLEXCAN_CTRL_PROPSEG(bt->prop_seg - 1); in flexcan_set_bittiming_ctrl()
1179 priv->write(reg, ®s->ctrl); in flexcan_set_bittiming_ctrl()
1183 priv->read(®s->mcr), priv->read(®s->ctrl)); in flexcan_set_bittiming_ctrl()
1189 struct can_bittiming *bt = &priv->can.bittiming; in flexcan_set_bittiming_cbt()
1190 struct can_bittiming *dbt = &priv->can.data_bittiming; in flexcan_set_bittiming_cbt()
1191 struct flexcan_regs __iomem *regs = priv->regs; in flexcan_set_bittiming_cbt()
1201 if (bt->phase_seg1 > 0x20) { in flexcan_set_bittiming_cbt()
1202 bt->prop_seg += (bt->phase_seg1 - 0x20); in flexcan_set_bittiming_cbt()
1203 bt->phase_seg1 = 0x20; in flexcan_set_bittiming_cbt()
1207 FIELD_PREP(FLEXCAN_CBT_EPRESDIV_MASK, bt->brp - 1) | in flexcan_set_bittiming_cbt()
1208 FIELD_PREP(FLEXCAN_CBT_ERJW_MASK, bt->sjw - 1) | in flexcan_set_bittiming_cbt()
1209 FIELD_PREP(FLEXCAN_CBT_EPROPSEG_MASK, bt->prop_seg - 1) | in flexcan_set_bittiming_cbt()
1210 FIELD_PREP(FLEXCAN_CBT_EPSEG1_MASK, bt->phase_seg1 - 1) | in flexcan_set_bittiming_cbt()
1211 FIELD_PREP(FLEXCAN_CBT_EPSEG2_MASK, bt->phase_seg2 - 1); in flexcan_set_bittiming_cbt()
1214 priv->write(reg_cbt, ®s->cbt); in flexcan_set_bittiming_cbt()
1216 if (priv->can.ctrlmode & CAN_CTRLMODE_FD) { in flexcan_set_bittiming_cbt()
1219 if (bt->brp != dbt->brp) in flexcan_set_bittiming_cbt()
1221 dbt->brp, bt->brp); in flexcan_set_bittiming_cbt()
1231 if (dbt->phase_seg1 > 0x8) { in flexcan_set_bittiming_cbt()
1232 dbt->prop_seg += (dbt->phase_seg1 - 0x8); in flexcan_set_bittiming_cbt()
1233 dbt->phase_seg1 = 0x8; in flexcan_set_bittiming_cbt()
1236 reg_fdcbt = priv->read(®s->fdcbt); in flexcan_set_bittiming_cbt()
1243 reg_fdcbt |= FIELD_PREP(FLEXCAN_FDCBT_FPRESDIV_MASK, dbt->brp - 1) | in flexcan_set_bittiming_cbt()
1244 FIELD_PREP(FLEXCAN_FDCBT_FRJW_MASK, dbt->sjw - 1) | in flexcan_set_bittiming_cbt()
1245 FIELD_PREP(FLEXCAN_FDCBT_FPROPSEG_MASK, dbt->prop_seg) | in flexcan_set_bittiming_cbt()
1246 FIELD_PREP(FLEXCAN_FDCBT_FPSEG1_MASK, dbt->phase_seg1 - 1) | in flexcan_set_bittiming_cbt()
1247 FIELD_PREP(FLEXCAN_FDCBT_FPSEG2_MASK, dbt->phase_seg2 - 1); in flexcan_set_bittiming_cbt()
1250 priv->write(reg_fdcbt, ®s->fdcbt); in flexcan_set_bittiming_cbt()
1253 reg_ctrl2 = priv->read(®s->ctrl2); in flexcan_set_bittiming_cbt()
1255 if (!(priv->can.ctrlmode & CAN_CTRLMODE_FD_NON_ISO)) in flexcan_set_bittiming_cbt()
1259 priv->write(reg_ctrl2, ®s->ctrl2); in flexcan_set_bittiming_cbt()
1263 reg_fdctrl = priv->read(®s->fdctrl); in flexcan_set_bittiming_cbt()
1267 if (priv->can.ctrlmode & CAN_CTRLMODE_FD) { in flexcan_set_bittiming_cbt()
1270 if (priv->can.ctrlmode & CAN_CTRLMODE_LOOPBACK) { in flexcan_set_bittiming_cbt()
1276 ((dbt->phase_seg1 - 1) + in flexcan_set_bittiming_cbt()
1277 dbt->prop_seg + 2) * in flexcan_set_bittiming_cbt()
1278 ((dbt->brp - 1 ) + 1)); in flexcan_set_bittiming_cbt()
1283 priv->write(reg_fdctrl, ®s->fdctrl); in flexcan_set_bittiming_cbt()
1287 priv->read(®s->mcr), priv->read(®s->ctrl), in flexcan_set_bittiming_cbt()
1288 priv->read(®s->ctrl2), priv->read(®s->fdctrl), in flexcan_set_bittiming_cbt()
1289 priv->read(®s->cbt), priv->read(®s->fdcbt)); in flexcan_set_bittiming_cbt()
1295 struct flexcan_regs __iomem *regs = priv->regs; in flexcan_set_bittiming()
1298 reg = priv->read(®s->ctrl); in flexcan_set_bittiming()
1302 if (priv->can.ctrlmode & CAN_CTRLMODE_LOOPBACK) in flexcan_set_bittiming()
1304 if (priv->can.ctrlmode & CAN_CTRLMODE_LISTENONLY) in flexcan_set_bittiming()
1306 if (priv->can.ctrlmode & CAN_CTRLMODE_3_SAMPLES) in flexcan_set_bittiming()
1310 priv->write(reg, ®s->ctrl); in flexcan_set_bittiming()
1312 if (priv->can.ctrlmode_supported & CAN_CTRLMODE_FD) in flexcan_set_bittiming()
1321 struct flexcan_regs __iomem *regs = priv->regs; in flexcan_ram_init()
1332 reg_ctrl2 = priv->read(®s->ctrl2); in flexcan_ram_init()
1334 priv->write(reg_ctrl2, ®s->ctrl2); in flexcan_ram_init()
1336 memset_io(®s->mb[0][0], 0, in flexcan_ram_init()
1337 offsetof(struct flexcan_regs, rx_smb1[3]) - in flexcan_ram_init()
1340 if (priv->can.ctrlmode & CAN_CTRLMODE_FD) in flexcan_ram_init()
1341 memset_io(®s->tx_smb_fd[0], 0, in flexcan_ram_init()
1342 offsetof(struct flexcan_regs, rx_smb1_fd[17]) - in flexcan_ram_init()
1346 priv->write(reg_ctrl2, ®s->ctrl2); in flexcan_ram_init()
1357 struct flexcan_regs __iomem *regs = priv->regs; in flexcan_chip_start()
1368 /* soft reset */ in flexcan_chip_start()
1373 if (priv->devtype_data->quirks & FLEXCAN_QUIRK_SUPPORT_ECC) in flexcan_chip_start()
1388 reg_mcr = priv->read(®s->mcr); in flexcan_chip_start()
1392 FLEXCAN_MCR_MAXMB(priv->tx_mb_idx); in flexcan_chip_start()
1397 * - disable for timestamp mode in flexcan_chip_start()
1398 * - enable for FIFO mode in flexcan_chip_start()
1400 if (priv->devtype_data->quirks & FLEXCAN_QUIRK_USE_OFF_TIMESTAMP) in flexcan_chip_start()
1413 * - enable Self Reception for loopback mode in flexcan_chip_start()
1415 * - disable for normal operation in flexcan_chip_start()
1417 if (priv->can.ctrlmode & CAN_CTRLMODE_LOOPBACK) in flexcan_chip_start()
1422 /* MCR - CAN-FD */ in flexcan_chip_start()
1423 if (priv->can.ctrlmode & CAN_CTRLMODE_FD) in flexcan_chip_start()
1429 priv->write(reg_mcr, ®s->mcr); in flexcan_chip_start()
1442 reg_ctrl = priv->read(®s->ctrl); in flexcan_chip_start()
1451 if (priv->devtype_data->quirks & FLEXCAN_QUIRK_BROKEN_WERR_STATE || in flexcan_chip_start()
1452 priv->can.ctrlmode & CAN_CTRLMODE_BERR_REPORTING) in flexcan_chip_start()
1458 priv->reg_ctrl_default = reg_ctrl; in flexcan_chip_start()
1462 priv->write(reg_ctrl, ®s->ctrl); in flexcan_chip_start()
1464 if ((priv->devtype_data->quirks & FLEXCAN_QUIRK_ENABLE_EACEN_RRS)) { in flexcan_chip_start()
1465 reg_ctrl2 = priv->read(®s->ctrl2); in flexcan_chip_start()
1467 priv->write(reg_ctrl2, ®s->ctrl2); in flexcan_chip_start()
1470 if (priv->can.ctrlmode_supported & CAN_CTRLMODE_FD) { in flexcan_chip_start()
1473 reg_fdctrl = priv->read(®s->fdctrl); in flexcan_chip_start()
1477 if (priv->can.ctrlmode & CAN_CTRLMODE_FD) { in flexcan_chip_start()
1493 priv->write(reg_fdctrl, ®s->fdctrl); in flexcan_chip_start()
1496 if (priv->devtype_data->quirks & FLEXCAN_QUIRK_USE_OFF_TIMESTAMP) { in flexcan_chip_start()
1497 for (i = priv->offload.mb_first; i <= priv->offload.mb_last; i++) { in flexcan_chip_start()
1499 priv->write(FLEXCAN_MB_CODE_RX_EMPTY, in flexcan_chip_start()
1500 &mb->can_ctrl); in flexcan_chip_start()
1504 for (i = FLEXCAN_TX_MB_RESERVED_OFF_FIFO; i < priv->mb_count; i++) { in flexcan_chip_start()
1506 priv->write(FLEXCAN_MB_CODE_RX_INACTIVE, in flexcan_chip_start()
1507 &mb->can_ctrl); in flexcan_chip_start()
1512 priv->write(FLEXCAN_MB_CODE_TX_INACTIVE, in flexcan_chip_start()
1513 &priv->tx_mb_reserved->can_ctrl); in flexcan_chip_start()
1516 priv->write(FLEXCAN_MB_CODE_TX_INACTIVE, in flexcan_chip_start()
1517 &priv->tx_mb->can_ctrl); in flexcan_chip_start()
1520 priv->write(0x0, ®s->rxgmask); in flexcan_chip_start()
1521 priv->write(0x0, ®s->rx14mask); in flexcan_chip_start()
1522 priv->write(0x0, ®s->rx15mask); in flexcan_chip_start()
1524 if (priv->devtype_data->quirks & FLEXCAN_QUIRK_DISABLE_RXFG) in flexcan_chip_start()
1525 priv->write(0x0, ®s->rxfgmask); in flexcan_chip_start()
1528 for (i = 0; i < priv->mb_count; i++) in flexcan_chip_start()
1529 priv->write(0, ®s->rximr[i]); in flexcan_chip_start()
1531 /* On Vybrid, disable non-correctable errors interrupt and in flexcan_chip_start()
1538 if (priv->devtype_data->quirks & FLEXCAN_QUIRK_DISABLE_MECR) { in flexcan_chip_start()
1541 * MECR register (step 1 - 5) in flexcan_chip_start()
1546 reg_ctrl2 = priv->read(®s->ctrl2); in flexcan_chip_start()
1548 priv->write(reg_ctrl2, ®s->ctrl2); in flexcan_chip_start()
1551 reg_mecr = priv->read(®s->mecr); in flexcan_chip_start()
1553 priv->write(reg_mecr, ®s->mecr); in flexcan_chip_start()
1558 priv->write(reg_mecr, ®s->mecr); in flexcan_chip_start()
1564 priv->write(reg_mecr, ®s->mecr); in flexcan_chip_start()
1567 priv->write(reg_ctrl2, ®s->ctrl2); in flexcan_chip_start()
1575 priv->can.state = CAN_STATE_ERROR_ACTIVE; in flexcan_chip_start()
1578 disable_irq(dev->irq); in flexcan_chip_start()
1579 priv->write(priv->reg_ctrl_default, ®s->ctrl); in flexcan_chip_start()
1580 reg_imask = priv->rx_mask | priv->tx_mask; in flexcan_chip_start()
1581 priv->write(upper_32_bits(reg_imask), ®s->imask2); in flexcan_chip_start()
1582 priv->write(lower_32_bits(reg_imask), ®s->imask1); in flexcan_chip_start()
1583 enable_irq(dev->irq); in flexcan_chip_start()
1587 priv->read(®s->mcr), priv->read(®s->ctrl)); in flexcan_chip_start()
1603 struct flexcan_regs __iomem *regs = priv->regs; in __flexcan_chip_stop()
1615 priv->write(0, ®s->imask2); in __flexcan_chip_stop()
1616 priv->write(0, ®s->imask1); in __flexcan_chip_stop()
1617 priv->write(priv->reg_ctrl_default & ~FLEXCAN_CTRL_ERR_ALL, in __flexcan_chip_stop()
1618 ®s->ctrl); in __flexcan_chip_stop()
1620 priv->can.state = CAN_STATE_STOPPED; in __flexcan_chip_stop()
1645 if ((priv->can.ctrlmode & CAN_CTRLMODE_3_SAMPLES) && in flexcan_open()
1646 (priv->can.ctrlmode & CAN_CTRLMODE_FD)) { in flexcan_open()
1647 netdev_err(dev, "Three Samples mode and CAN-FD mode can't be used together\n"); in flexcan_open()
1648 return -EINVAL; in flexcan_open()
1651 err = pm_runtime_get_sync(priv->dev); in flexcan_open()
1653 pm_runtime_put_noidle(priv->dev); in flexcan_open()
1665 err = request_irq(dev->irq, flexcan_irq, IRQF_SHARED, dev->name, dev); in flexcan_open()
1669 if (priv->can.ctrlmode & CAN_CTRLMODE_FD) in flexcan_open()
1670 priv->mb_size = sizeof(struct flexcan_mb) + CANFD_MAX_DLEN; in flexcan_open()
1672 priv->mb_size = sizeof(struct flexcan_mb) + CAN_MAX_DLEN; in flexcan_open()
1673 priv->mb_count = (sizeof(priv->regs->mb[0]) / priv->mb_size) + in flexcan_open()
1674 (sizeof(priv->regs->mb[1]) / priv->mb_size); in flexcan_open()
1676 if (priv->devtype_data->quirks & FLEXCAN_QUIRK_USE_OFF_TIMESTAMP) in flexcan_open()
1677 priv->tx_mb_reserved = in flexcan_open()
1680 priv->tx_mb_reserved = in flexcan_open()
1682 priv->tx_mb_idx = priv->mb_count - 1; in flexcan_open()
1683 priv->tx_mb = flexcan_get_mb(priv, priv->tx_mb_idx); in flexcan_open()
1684 priv->tx_mask = FLEXCAN_IFLAG_MB(priv->tx_mb_idx); in flexcan_open()
1686 priv->offload.mailbox_read = flexcan_mailbox_read; in flexcan_open()
1688 if (priv->devtype_data->quirks & FLEXCAN_QUIRK_USE_OFF_TIMESTAMP) { in flexcan_open()
1689 priv->offload.mb_first = FLEXCAN_RX_MB_OFF_TIMESTAMP_FIRST; in flexcan_open()
1690 priv->offload.mb_last = priv->mb_count - 2; in flexcan_open()
1692 priv->rx_mask = GENMASK_ULL(priv->offload.mb_last, in flexcan_open()
1693 priv->offload.mb_first); in flexcan_open()
1694 err = can_rx_offload_add_timestamp(dev, &priv->offload); in flexcan_open()
1696 priv->rx_mask = FLEXCAN_IFLAG_RX_FIFO_OVERFLOW | in flexcan_open()
1698 err = can_rx_offload_add_fifo(dev, &priv->offload, in flexcan_open()
1704 /* start chip and queuing */ in flexcan_open()
1711 can_rx_offload_enable(&priv->offload); in flexcan_open()
1717 can_rx_offload_del(&priv->offload); in flexcan_open()
1719 free_irq(dev->irq, dev); in flexcan_open()
1725 pm_runtime_put(priv->dev); in flexcan_open()
1735 can_rx_offload_disable(&priv->offload); in flexcan_close()
1738 can_rx_offload_del(&priv->offload); in flexcan_close()
1739 free_irq(dev->irq, dev); in flexcan_close()
1743 pm_runtime_put(priv->dev); in flexcan_close()
1764 return -EOPNOTSUPP; in flexcan_set_mode()
1780 struct flexcan_regs __iomem *regs = priv->regs; in register_flexcandev()
1792 reg = priv->read(®s->ctrl); in register_flexcandev()
1793 if (priv->clk_src) in register_flexcandev()
1797 priv->write(reg, ®s->ctrl); in register_flexcandev()
1804 reg = priv->read(®s->mcr); in register_flexcandev()
1807 priv->write(reg, ®s->mcr); in register_flexcandev()
1814 reg = priv->read(®s->mcr); in register_flexcandev()
1817 err = -ENODEV; in register_flexcandev()
1829 pm_runtime_put(priv->dev); in register_flexcandev()
1848 struct device_node *np = pdev->dev.of_node; in flexcan_setup_stop_mode()
1856 return -EINVAL; in flexcan_setup_stop_mode()
1861 ret = of_property_read_u32_array(np, "fsl,stop-mode", out_val, in flexcan_setup_stop_mode()
1864 dev_dbg(&pdev->dev, "no stop-mode property\n"); in flexcan_setup_stop_mode()
1871 dev_dbg(&pdev->dev, "could not find gpr node by phandle\n"); in flexcan_setup_stop_mode()
1872 return -ENODEV; in flexcan_setup_stop_mode()
1876 priv->stm.gpr = syscon_node_to_regmap(gpr_np); in flexcan_setup_stop_mode()
1877 if (IS_ERR(priv->stm.gpr)) { in flexcan_setup_stop_mode()
1878 dev_dbg(&pdev->dev, "could not find gpr regmap\n"); in flexcan_setup_stop_mode()
1879 ret = PTR_ERR(priv->stm.gpr); in flexcan_setup_stop_mode()
1883 priv->stm.req_gpr = out_val[1]; in flexcan_setup_stop_mode()
1884 priv->stm.req_bit = out_val[2]; in flexcan_setup_stop_mode()
1886 dev_dbg(&pdev->dev, in flexcan_setup_stop_mode()
1888 gpr_np->full_name, priv->stm.req_gpr, priv->stm.req_bit); in flexcan_setup_stop_mode()
1890 device_set_wakeup_capable(&pdev->dev, true); in flexcan_setup_stop_mode()
1892 if (of_property_read_bool(np, "wakeup-source")) in flexcan_setup_stop_mode()
1893 device_set_wakeup_enable(&pdev->dev, true); in flexcan_setup_stop_mode()
1903 { .compatible = "fsl,imx8qm-flexcan", .data = &fsl_imx8qm_devtype_data, },
1904 { .compatible = "fsl,imx8mp-flexcan", .data = &fsl_imx8mp_devtype_data, },
1905 { .compatible = "fsl,imx6q-flexcan", .data = &fsl_imx6q_devtype_data, },
1906 { .compatible = "fsl,imx28-flexcan", .data = &fsl_imx28_devtype_data, },
1907 { .compatible = "fsl,imx53-flexcan", .data = &fsl_imx25_devtype_data, },
1908 { .compatible = "fsl,imx35-flexcan", .data = &fsl_imx25_devtype_data, },
1909 { .compatible = "fsl,imx25-flexcan", .data = &fsl_imx25_devtype_data, },
1910 { .compatible = "fsl,p1010-flexcan", .data = &fsl_p1010_devtype_data, },
1911 { .compatible = "fsl,vf610-flexcan", .data = &fsl_vf610_devtype_data, },
1912 { .compatible = "fsl,ls1021ar2-flexcan", .data = &fsl_ls1021a_r2_devtype_data, },
1913 { .compatible = "fsl,lx2160ar1-flexcan", .data = &fsl_lx2160a_r1_devtype_data, },
1930 struct regulator *reg_xceiver; in flexcan_probe()
1937 reg_xceiver = devm_regulator_get_optional(&pdev->dev, "xceiver"); in flexcan_probe()
1938 if (PTR_ERR(reg_xceiver) == -EPROBE_DEFER) in flexcan_probe()
1939 return -EPROBE_DEFER; in flexcan_probe()
1940 else if (PTR_ERR(reg_xceiver) == -ENODEV) in flexcan_probe()
1945 if (pdev->dev.of_node) { in flexcan_probe()
1946 of_property_read_u32(pdev->dev.of_node, in flexcan_probe()
1947 "clock-frequency", &clock_freq); in flexcan_probe()
1948 of_property_read_u8(pdev->dev.of_node, in flexcan_probe()
1949 "fsl,clk-source", &clk_src); in flexcan_probe()
1953 clk_ipg = devm_clk_get(&pdev->dev, "ipg"); in flexcan_probe()
1955 dev_err(&pdev->dev, "no ipg clock defined\n"); in flexcan_probe()
1959 clk_per = devm_clk_get(&pdev->dev, "per"); in flexcan_probe()
1961 dev_err(&pdev->dev, "no per clock defined\n"); in flexcan_probe()
1969 return -ENODEV; in flexcan_probe()
1975 of_id = of_match_device(flexcan_of_match, &pdev->dev); in flexcan_probe()
1977 devtype_data = of_id->data; in flexcan_probe()
1978 } else if (platform_get_device_id(pdev)->driver_data) { in flexcan_probe()
1980 platform_get_device_id(pdev)->driver_data; in flexcan_probe()
1982 return -ENODEV; in flexcan_probe()
1985 if ((devtype_data->quirks & FLEXCAN_QUIRK_SUPPORT_FD) && in flexcan_probe()
1986 !(devtype_data->quirks & FLEXCAN_QUIRK_USE_OFF_TIMESTAMP)) { in flexcan_probe()
1987 dev_err(&pdev->dev, "CAN-FD mode doesn't work with FIFO mode!\n"); in flexcan_probe()
1988 return -EINVAL; in flexcan_probe()
1993 return -ENOMEM; in flexcan_probe()
1996 SET_NETDEV_DEV(dev, &pdev->dev); in flexcan_probe()
1998 dev->netdev_ops = &flexcan_netdev_ops; in flexcan_probe()
1999 dev->irq = irq; in flexcan_probe()
2000 dev->flags |= IFF_ECHO; in flexcan_probe()
2004 if (of_property_read_bool(pdev->dev.of_node, "big-endian") || in flexcan_probe()
2005 devtype_data->quirks & FLEXCAN_QUIRK_DEFAULT_BIG_ENDIAN) { in flexcan_probe()
2006 priv->read = flexcan_read_be; in flexcan_probe()
2007 priv->write = flexcan_write_be; in flexcan_probe()
2009 priv->read = flexcan_read_le; in flexcan_probe()
2010 priv->write = flexcan_write_le; in flexcan_probe()
2013 priv->dev = &pdev->dev; in flexcan_probe()
2014 priv->can.clock.freq = clock_freq; in flexcan_probe()
2015 priv->can.do_set_mode = flexcan_set_mode; in flexcan_probe()
2016 priv->can.do_get_berr_counter = flexcan_get_berr_counter; in flexcan_probe()
2017 priv->can.ctrlmode_supported = CAN_CTRLMODE_LOOPBACK | in flexcan_probe()
2020 priv->regs = regs; in flexcan_probe()
2021 priv->clk_ipg = clk_ipg; in flexcan_probe()
2022 priv->clk_per = clk_per; in flexcan_probe()
2023 priv->clk_src = clk_src; in flexcan_probe()
2024 priv->devtype_data = devtype_data; in flexcan_probe()
2025 priv->reg_xceiver = reg_xceiver; in flexcan_probe()
2027 if (priv->devtype_data->quirks & FLEXCAN_QUIRK_SUPPORT_FD) { in flexcan_probe()
2028 priv->can.ctrlmode_supported |= CAN_CTRLMODE_FD | in flexcan_probe()
2030 priv->can.bittiming_const = &flexcan_fd_bittiming_const; in flexcan_probe()
2031 priv->can.data_bittiming_const = in flexcan_probe()
2034 priv->can.bittiming_const = &flexcan_bittiming_const; in flexcan_probe()
2037 pm_runtime_get_noresume(&pdev->dev); in flexcan_probe()
2038 pm_runtime_set_active(&pdev->dev); in flexcan_probe()
2039 pm_runtime_enable(&pdev->dev); in flexcan_probe()
2043 dev_err(&pdev->dev, "registering netdev failed\n"); in flexcan_probe()
2050 if (priv->devtype_data->quirks & FLEXCAN_QUIRK_SETUP_STOP_MODE) { in flexcan_probe()
2053 dev_dbg(&pdev->dev, "failed to setup stop-mode\n"); in flexcan_probe()
2059 pm_runtime_put_noidle(&pdev->dev); in flexcan_probe()
2060 pm_runtime_disable(&pdev->dev); in flexcan_probe()
2069 device_set_wakeup_enable(&pdev->dev, false); in flexcan_remove()
2070 device_set_wakeup_capable(&pdev->dev, false); in flexcan_remove()
2072 pm_runtime_disable(&pdev->dev); in flexcan_remove()
2089 enable_irq_wake(dev->irq); in flexcan_suspend()
2105 priv->can.state = CAN_STATE_SLEEPING; in flexcan_suspend()
2116 priv->can.state = CAN_STATE_ERROR_ACTIVE; in flexcan_resume()
2121 disable_irq_wake(dev->irq); in flexcan_resume()
2215 "Marc Kleine-Budde <kernel@pengutronix.de>");