Fabio Estevam | 5b749be | 2018-07-06 14:35:12 -0300 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
| 2 | // |
| 3 | // flexcan.c - FLEXCAN CAN controller driver |
| 4 | // |
| 5 | // Copyright (c) 2005-2006 Varma Electronics Oy |
| 6 | // Copyright (c) 2009 Sascha Hauer, Pengutronix |
| 7 | // Copyright (c) 2010-2017 Pengutronix, Marc Kleine-Budde <kernel@pengutronix.de> |
| 8 | // Copyright (c) 2014 David Jander, Protonic Holland |
| 9 | // |
| 10 | // Based on code originally by Andrey Volkov <avolkov@varma-el.com> |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 11 | |
Joakim Zhang | 812f011 | 2020-11-06 18:56:27 +0800 | [diff] [blame] | 12 | #include <dt-bindings/firmware/imx/rsrc.h> |
Joakim Zhang | eadf6ca | 2019-07-12 08:02:44 +0000 | [diff] [blame] | 13 | #include <linux/bitfield.h> |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 14 | #include <linux/can.h> |
| 15 | #include <linux/can/dev.h> |
| 16 | #include <linux/can/error.h> |
Fabio Baltieri | adccadb | 2012-12-18 18:50:58 +0100 | [diff] [blame] | 17 | #include <linux/can/led.h> |
Marc Kleine-Budde | 3016475 | 2015-05-10 15:26:58 +0200 | [diff] [blame] | 18 | #include <linux/can/rx-offload.h> |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 19 | #include <linux/clk.h> |
| 20 | #include <linux/delay.h> |
Joakim Zhang | 812f011 | 2020-11-06 18:56:27 +0800 | [diff] [blame] | 21 | #include <linux/firmware/imx/sci.h> |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 22 | #include <linux/interrupt.h> |
| 23 | #include <linux/io.h> |
Aisheng Dong | de3578c | 2018-11-23 08:35:33 +0000 | [diff] [blame] | 24 | #include <linux/mfd/syscon.h> |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 25 | #include <linux/module.h> |
Marc Kleine-Budde | 555f6e5 | 2020-09-22 16:44:10 +0200 | [diff] [blame] | 26 | #include <linux/netdevice.h> |
holt@sgi.com | 97efe9a | 2011-08-16 17:32:23 +0000 | [diff] [blame] | 27 | #include <linux/of.h> |
Hui Wang | 30c1e67 | 2012-06-28 16:21:35 +0800 | [diff] [blame] | 28 | #include <linux/of_device.h> |
Joakim Zhang | 1434d04 | 2019-12-04 11:36:19 +0000 | [diff] [blame] | 29 | #include <linux/pinctrl/consumer.h> |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 30 | #include <linux/platform_device.h> |
Angelo Dureghello | d9cead7 | 2021-07-02 11:48:41 +0200 | [diff] [blame] | 31 | #include <linux/can/platform/flexcan.h> |
Aisheng Dong | ca10989 | 2018-11-30 08:53:26 +0000 | [diff] [blame] | 32 | #include <linux/pm_runtime.h> |
Aisheng Dong | de3578c | 2018-11-23 08:35:33 +0000 | [diff] [blame] | 33 | #include <linux/regmap.h> |
Marc Kleine-Budde | 555f6e5 | 2020-09-22 16:44:10 +0200 | [diff] [blame] | 34 | #include <linux/regulator/consumer.h> |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 35 | |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 36 | #define DRV_NAME "flexcan" |
| 37 | |
| 38 | /* 8 for RX fifo and 2 error handling */ |
| 39 | #define FLEXCAN_NAPI_WEIGHT (8 + 2) |
| 40 | |
| 41 | /* FLEXCAN module configuration register (CANMCR) bits */ |
| 42 | #define FLEXCAN_MCR_MDIS BIT(31) |
| 43 | #define FLEXCAN_MCR_FRZ BIT(30) |
| 44 | #define FLEXCAN_MCR_FEN BIT(29) |
| 45 | #define FLEXCAN_MCR_HALT BIT(28) |
| 46 | #define FLEXCAN_MCR_NOT_RDY BIT(27) |
| 47 | #define FLEXCAN_MCR_WAK_MSK BIT(26) |
| 48 | #define FLEXCAN_MCR_SOFTRST BIT(25) |
| 49 | #define FLEXCAN_MCR_FRZ_ACK BIT(24) |
| 50 | #define FLEXCAN_MCR_SUPV BIT(23) |
| 51 | #define FLEXCAN_MCR_SLF_WAK BIT(22) |
| 52 | #define FLEXCAN_MCR_WRN_EN BIT(21) |
| 53 | #define FLEXCAN_MCR_LPM_ACK BIT(20) |
| 54 | #define FLEXCAN_MCR_WAK_SRC BIT(19) |
| 55 | #define FLEXCAN_MCR_DOZE BIT(18) |
| 56 | #define FLEXCAN_MCR_SRX_DIS BIT(17) |
Marc Kleine-Budde | 62d1086 | 2015-08-27 16:01:27 +0200 | [diff] [blame] | 57 | #define FLEXCAN_MCR_IRMQ BIT(16) |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 58 | #define FLEXCAN_MCR_LPRIO_EN BIT(13) |
| 59 | #define FLEXCAN_MCR_AEN BIT(12) |
Joakim Zhang | eadf6ca | 2019-07-12 08:02:44 +0000 | [diff] [blame] | 60 | #define FLEXCAN_MCR_FDEN BIT(11) |
Marc Kleine-Budde | b3cf53e | 2015-09-01 09:00:13 +0200 | [diff] [blame] | 61 | /* MCR_MAXMB: maximum used MBs is MAXMB + 1 */ |
Marc Kleine-Budde | 4c728d8 | 2014-09-02 16:54:17 +0200 | [diff] [blame] | 62 | #define FLEXCAN_MCR_MAXMB(x) ((x) & 0x7f) |
Marc Kleine-Budde | 0012e5c | 2015-08-06 14:53:57 +0200 | [diff] [blame] | 63 | #define FLEXCAN_MCR_IDAM_A (0x0 << 8) |
| 64 | #define FLEXCAN_MCR_IDAM_B (0x1 << 8) |
| 65 | #define FLEXCAN_MCR_IDAM_C (0x2 << 8) |
| 66 | #define FLEXCAN_MCR_IDAM_D (0x3 << 8) |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 67 | |
| 68 | /* FLEXCAN control register (CANCTRL) bits */ |
| 69 | #define FLEXCAN_CTRL_PRESDIV(x) (((x) & 0xff) << 24) |
| 70 | #define FLEXCAN_CTRL_RJW(x) (((x) & 0x03) << 22) |
| 71 | #define FLEXCAN_CTRL_PSEG1(x) (((x) & 0x07) << 19) |
| 72 | #define FLEXCAN_CTRL_PSEG2(x) (((x) & 0x07) << 16) |
| 73 | #define FLEXCAN_CTRL_BOFF_MSK BIT(15) |
| 74 | #define FLEXCAN_CTRL_ERR_MSK BIT(14) |
| 75 | #define FLEXCAN_CTRL_CLK_SRC BIT(13) |
| 76 | #define FLEXCAN_CTRL_LPB BIT(12) |
| 77 | #define FLEXCAN_CTRL_TWRN_MSK BIT(11) |
| 78 | #define FLEXCAN_CTRL_RWRN_MSK BIT(10) |
| 79 | #define FLEXCAN_CTRL_SMP BIT(7) |
| 80 | #define FLEXCAN_CTRL_BOFF_REC BIT(6) |
| 81 | #define FLEXCAN_CTRL_TSYN BIT(5) |
| 82 | #define FLEXCAN_CTRL_LBUF BIT(4) |
| 83 | #define FLEXCAN_CTRL_LOM BIT(3) |
| 84 | #define FLEXCAN_CTRL_PROPSEG(x) ((x) & 0x07) |
| 85 | #define FLEXCAN_CTRL_ERR_BUS (FLEXCAN_CTRL_ERR_MSK) |
| 86 | #define FLEXCAN_CTRL_ERR_STATE \ |
| 87 | (FLEXCAN_CTRL_TWRN_MSK | FLEXCAN_CTRL_RWRN_MSK | \ |
| 88 | FLEXCAN_CTRL_BOFF_MSK) |
| 89 | #define FLEXCAN_CTRL_ERR_ALL \ |
| 90 | (FLEXCAN_CTRL_ERR_BUS | FLEXCAN_CTRL_ERR_STATE) |
| 91 | |
Stefan Agner | cdce844 | 2014-07-15 14:56:21 +0200 | [diff] [blame] | 92 | /* FLEXCAN control register 2 (CTRL2) bits */ |
Marc Kleine-Budde | 6f75fce | 2014-09-23 11:03:01 +0200 | [diff] [blame] | 93 | #define FLEXCAN_CTRL2_ECRWRE BIT(29) |
| 94 | #define FLEXCAN_CTRL2_WRMFRZ BIT(28) |
| 95 | #define FLEXCAN_CTRL2_RFFN(x) (((x) & 0x0f) << 24) |
| 96 | #define FLEXCAN_CTRL2_TASD(x) (((x) & 0x1f) << 19) |
| 97 | #define FLEXCAN_CTRL2_MRP BIT(18) |
| 98 | #define FLEXCAN_CTRL2_RRS BIT(17) |
| 99 | #define FLEXCAN_CTRL2_EACEN BIT(16) |
Joakim Zhang | ce885a1 | 2019-07-12 08:02:51 +0000 | [diff] [blame] | 100 | #define FLEXCAN_CTRL2_ISOCANFDEN BIT(12) |
Stefan Agner | cdce844 | 2014-07-15 14:56:21 +0200 | [diff] [blame] | 101 | |
| 102 | /* FLEXCAN memory error control register (MECR) bits */ |
| 103 | #define FLEXCAN_MECR_ECRWRDIS BIT(31) |
| 104 | #define FLEXCAN_MECR_HANCEI_MSK BIT(19) |
| 105 | #define FLEXCAN_MECR_FANCEI_MSK BIT(18) |
| 106 | #define FLEXCAN_MECR_CEI_MSK BIT(16) |
| 107 | #define FLEXCAN_MECR_HAERRIE BIT(15) |
| 108 | #define FLEXCAN_MECR_FAERRIE BIT(14) |
| 109 | #define FLEXCAN_MECR_EXTERRIE BIT(13) |
| 110 | #define FLEXCAN_MECR_RERRDIS BIT(9) |
| 111 | #define FLEXCAN_MECR_ECCDIS BIT(8) |
| 112 | #define FLEXCAN_MECR_NCEFAFRZ BIT(7) |
| 113 | |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 114 | /* FLEXCAN error and status register (ESR) bits */ |
| 115 | #define FLEXCAN_ESR_TWRN_INT BIT(17) |
| 116 | #define FLEXCAN_ESR_RWRN_INT BIT(16) |
| 117 | #define FLEXCAN_ESR_BIT1_ERR BIT(15) |
| 118 | #define FLEXCAN_ESR_BIT0_ERR BIT(14) |
| 119 | #define FLEXCAN_ESR_ACK_ERR BIT(13) |
| 120 | #define FLEXCAN_ESR_CRC_ERR BIT(12) |
| 121 | #define FLEXCAN_ESR_FRM_ERR BIT(11) |
| 122 | #define FLEXCAN_ESR_STF_ERR BIT(10) |
| 123 | #define FLEXCAN_ESR_TX_WRN BIT(9) |
| 124 | #define FLEXCAN_ESR_RX_WRN BIT(8) |
| 125 | #define FLEXCAN_ESR_IDLE BIT(7) |
| 126 | #define FLEXCAN_ESR_TXRX BIT(6) |
| 127 | #define FLEXCAN_EST_FLT_CONF_SHIFT (4) |
| 128 | #define FLEXCAN_ESR_FLT_CONF_MASK (0x3 << FLEXCAN_EST_FLT_CONF_SHIFT) |
| 129 | #define FLEXCAN_ESR_FLT_CONF_ACTIVE (0x0 << FLEXCAN_EST_FLT_CONF_SHIFT) |
| 130 | #define FLEXCAN_ESR_FLT_CONF_PASSIVE (0x1 << FLEXCAN_EST_FLT_CONF_SHIFT) |
| 131 | #define FLEXCAN_ESR_BOFF_INT BIT(2) |
| 132 | #define FLEXCAN_ESR_ERR_INT BIT(1) |
| 133 | #define FLEXCAN_ESR_WAK_INT BIT(0) |
| 134 | #define FLEXCAN_ESR_ERR_BUS \ |
| 135 | (FLEXCAN_ESR_BIT1_ERR | FLEXCAN_ESR_BIT0_ERR | \ |
| 136 | FLEXCAN_ESR_ACK_ERR | FLEXCAN_ESR_CRC_ERR | \ |
| 137 | FLEXCAN_ESR_FRM_ERR | FLEXCAN_ESR_STF_ERR) |
| 138 | #define FLEXCAN_ESR_ERR_STATE \ |
| 139 | (FLEXCAN_ESR_TWRN_INT | FLEXCAN_ESR_RWRN_INT | FLEXCAN_ESR_BOFF_INT) |
| 140 | #define FLEXCAN_ESR_ERR_ALL \ |
| 141 | (FLEXCAN_ESR_ERR_BUS | FLEXCAN_ESR_ERR_STATE) |
Wolfgang Grandegger | 6e9d554 | 2011-12-12 16:09:28 +0100 | [diff] [blame] | 142 | #define FLEXCAN_ESR_ALL_INT \ |
| 143 | (FLEXCAN_ESR_TWRN_INT | FLEXCAN_ESR_RWRN_INT | \ |
Joakim Zhang | ab60523 | 2019-12-04 11:36:08 +0000 | [diff] [blame] | 144 | FLEXCAN_ESR_BOFF_INT | FLEXCAN_ESR_ERR_INT) |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 145 | |
Joakim Zhang | eadf6ca | 2019-07-12 08:02:44 +0000 | [diff] [blame] | 146 | /* FLEXCAN Bit Timing register (CBT) bits */ |
| 147 | #define FLEXCAN_CBT_BTF BIT(31) |
| 148 | #define FLEXCAN_CBT_EPRESDIV_MASK GENMASK(30, 21) |
| 149 | #define FLEXCAN_CBT_ERJW_MASK GENMASK(20, 16) |
| 150 | #define FLEXCAN_CBT_EPROPSEG_MASK GENMASK(15, 10) |
| 151 | #define FLEXCAN_CBT_EPSEG1_MASK GENMASK(9, 5) |
| 152 | #define FLEXCAN_CBT_EPSEG2_MASK GENMASK(4, 0) |
| 153 | |
| 154 | /* FLEXCAN FD control register (FDCTRL) bits */ |
| 155 | #define FLEXCAN_FDCTRL_FDRATE BIT(31) |
| 156 | #define FLEXCAN_FDCTRL_MBDSR1 GENMASK(20, 19) |
| 157 | #define FLEXCAN_FDCTRL_MBDSR0 GENMASK(17, 16) |
| 158 | #define FLEXCAN_FDCTRL_MBDSR_8 0x0 |
| 159 | #define FLEXCAN_FDCTRL_MBDSR_12 0x1 |
| 160 | #define FLEXCAN_FDCTRL_MBDSR_32 0x2 |
| 161 | #define FLEXCAN_FDCTRL_MBDSR_64 0x3 |
Joakim Zhang | ef5f631 | 2019-07-12 08:02:56 +0000 | [diff] [blame] | 162 | #define FLEXCAN_FDCTRL_TDCEN BIT(15) |
| 163 | #define FLEXCAN_FDCTRL_TDCFAIL BIT(14) |
| 164 | #define FLEXCAN_FDCTRL_TDCOFF GENMASK(12, 8) |
| 165 | #define FLEXCAN_FDCTRL_TDCVAL GENMASK(5, 0) |
Joakim Zhang | eadf6ca | 2019-07-12 08:02:44 +0000 | [diff] [blame] | 166 | |
| 167 | /* FLEXCAN FD Bit Timing register (FDCBT) bits */ |
| 168 | #define FLEXCAN_FDCBT_FPRESDIV_MASK GENMASK(29, 20) |
| 169 | #define FLEXCAN_FDCBT_FRJW_MASK GENMASK(18, 16) |
| 170 | #define FLEXCAN_FDCBT_FPROPSEG_MASK GENMASK(14, 10) |
| 171 | #define FLEXCAN_FDCBT_FPSEG1_MASK GENMASK(7, 5) |
| 172 | #define FLEXCAN_FDCBT_FPSEG2_MASK GENMASK(2, 0) |
| 173 | |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 174 | /* FLEXCAN interrupt flag register (IFLAG) bits */ |
David Jander | 25e9244 | 2014-09-03 16:47:22 +0200 | [diff] [blame] | 175 | /* Errata ERR005829 step7: Reserve first valid MB */ |
Alexander Stein | cbffaf7 | 2018-10-11 17:01:25 +0200 | [diff] [blame] | 176 | #define FLEXCAN_TX_MB_RESERVED_OFF_FIFO 8 |
Marc Kleine-Budde | b3cf53e | 2015-09-01 09:00:13 +0200 | [diff] [blame] | 177 | #define FLEXCAN_TX_MB_RESERVED_OFF_TIMESTAMP 0 |
Alexander Stein | cbffaf7 | 2018-10-11 17:01:25 +0200 | [diff] [blame] | 178 | #define FLEXCAN_RX_MB_OFF_TIMESTAMP_FIRST (FLEXCAN_TX_MB_RESERVED_OFF_TIMESTAMP + 1) |
Marc Kleine-Budde | 8ce5139 | 2019-03-01 12:17:30 +0100 | [diff] [blame] | 179 | #define FLEXCAN_IFLAG_MB(x) BIT_ULL(x) |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 180 | #define FLEXCAN_IFLAG_RX_FIFO_OVERFLOW BIT(7) |
| 181 | #define FLEXCAN_IFLAG_RX_FIFO_WARN BIT(6) |
| 182 | #define FLEXCAN_IFLAG_RX_FIFO_AVAILABLE BIT(5) |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 183 | |
| 184 | /* FLEXCAN message buffers */ |
Marc Kleine-Budde | b3cf53e | 2015-09-01 09:00:13 +0200 | [diff] [blame] | 185 | #define FLEXCAN_MB_CODE_MASK (0xf << 24) |
| 186 | #define FLEXCAN_MB_CODE_RX_BUSY_BIT (0x1 << 24) |
Marc Kleine-Budde | c32fe4a | 2014-09-16 12:39:28 +0200 | [diff] [blame] | 187 | #define FLEXCAN_MB_CODE_RX_INACTIVE (0x0 << 24) |
| 188 | #define FLEXCAN_MB_CODE_RX_EMPTY (0x4 << 24) |
| 189 | #define FLEXCAN_MB_CODE_RX_FULL (0x2 << 24) |
Marc Kleine-Budde | 0012e5c | 2015-08-06 14:53:57 +0200 | [diff] [blame] | 190 | #define FLEXCAN_MB_CODE_RX_OVERRUN (0x6 << 24) |
Marc Kleine-Budde | c32fe4a | 2014-09-16 12:39:28 +0200 | [diff] [blame] | 191 | #define FLEXCAN_MB_CODE_RX_RANSWER (0xa << 24) |
| 192 | |
| 193 | #define FLEXCAN_MB_CODE_TX_INACTIVE (0x8 << 24) |
| 194 | #define FLEXCAN_MB_CODE_TX_ABORT (0x9 << 24) |
| 195 | #define FLEXCAN_MB_CODE_TX_DATA (0xc << 24) |
| 196 | #define FLEXCAN_MB_CODE_TX_TANSWER (0xe << 24) |
| 197 | |
Joakim Zhang | eadf6ca | 2019-07-12 08:02:44 +0000 | [diff] [blame] | 198 | #define FLEXCAN_MB_CNT_EDL BIT(31) |
| 199 | #define FLEXCAN_MB_CNT_BRS BIT(30) |
| 200 | #define FLEXCAN_MB_CNT_ESI BIT(29) |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 201 | #define FLEXCAN_MB_CNT_SRR BIT(22) |
| 202 | #define FLEXCAN_MB_CNT_IDE BIT(21) |
| 203 | #define FLEXCAN_MB_CNT_RTR BIT(20) |
| 204 | #define FLEXCAN_MB_CNT_LENGTH(x) (((x) & 0xf) << 16) |
| 205 | #define FLEXCAN_MB_CNT_TIMESTAMP(x) ((x) & 0xffff) |
| 206 | |
Joakim Zhang | 247e535 | 2019-01-31 09:37:22 +0000 | [diff] [blame] | 207 | #define FLEXCAN_TIMEOUT_US (250) |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 208 | |
Marc Kleine-Budde | 0012e5c | 2015-08-06 14:53:57 +0200 | [diff] [blame] | 209 | /* FLEXCAN hardware feature flags |
Wolfgang Grandegger | bb698ca | 2012-10-10 21:10:42 +0200 | [diff] [blame] | 210 | * |
| 211 | * Below is some version info we got: |
Angelo Dureghello | d9cead7 | 2021-07-02 11:48:41 +0200 | [diff] [blame] | 212 | * SOC Version IP-Version Glitch- [TR]WRN_INT IRQ Err Memory err RTR rece- FD Mode MB |
Joakim Zhang | 2a1993e | 2019-07-12 08:02:59 +0000 | [diff] [blame] | 213 | * Filter? connected? Passive detection ption in MB Supported? |
Angelo Dureghello | d9cead7 | 2021-07-02 11:48:41 +0200 | [diff] [blame] | 214 | * MCF5441X FlexCAN2 ? no yes no no yes no 16 |
| 215 | * MX25 FlexCAN2 03.00.00.00 no no no no no no 64 |
| 216 | * MX28 FlexCAN2 03.00.04.00 yes yes no no no no 64 |
| 217 | * MX35 FlexCAN2 03.00.00.00 no no no no no no 64 |
| 218 | * MX53 FlexCAN2 03.00.00.00 yes no no no no no 64 |
| 219 | * MX6s FlexCAN3 10.00.12.00 yes yes no no yes no 64 |
| 220 | * MX8QM FlexCAN3 03.00.23.00 yes yes no no yes yes 64 |
| 221 | * MX8MP FlexCAN3 03.00.17.01 yes yes no yes yes yes 64 |
| 222 | * VF610 FlexCAN3 ? no yes no yes yes? no 64 |
| 223 | * LS1021A FlexCAN2 03.00.04.00 no yes no no yes no 64 |
| 224 | * LX2160A FlexCAN3 03.00.23.00 no yes no yes yes yes 64 |
Wolfgang Grandegger | bb698ca | 2012-10-10 21:10:42 +0200 | [diff] [blame] | 225 | * |
| 226 | * Some SOCs do not have the RX_WARN & TX_WARN interrupt line connected. |
| 227 | */ |
Marc Kleine-Budde | ef4b623 | 2020-09-22 16:44:14 +0200 | [diff] [blame] | 228 | |
| 229 | /* [TR]WRN_INT not connected */ |
| 230 | #define FLEXCAN_QUIRK_BROKEN_WERR_STATE BIT(1) |
| 231 | /* Disable RX FIFO Global mask */ |
| 232 | #define FLEXCAN_QUIRK_DISABLE_RXFG BIT(2) |
| 233 | /* Enable EACEN and RRS bit in ctrl2 */ |
| 234 | #define FLEXCAN_QUIRK_ENABLE_EACEN_RRS BIT(3) |
Joakim Zhang | 15ef207 | 2020-04-16 17:31:25 +0800 | [diff] [blame] | 235 | /* Disable non-correctable errors interrupt and freeze mode */ |
Marc Kleine-Budde | ef4b623 | 2020-09-22 16:44:14 +0200 | [diff] [blame] | 236 | #define FLEXCAN_QUIRK_DISABLE_MECR BIT(4) |
| 237 | /* Use timestamp based offloading */ |
| 238 | #define FLEXCAN_QUIRK_USE_OFF_TIMESTAMP BIT(5) |
| 239 | /* No interrupt for error passive */ |
| 240 | #define FLEXCAN_QUIRK_BROKEN_PERR_STATE BIT(6) |
| 241 | /* default to BE register access */ |
| 242 | #define FLEXCAN_QUIRK_DEFAULT_BIG_ENDIAN BIT(7) |
Joakim Zhang | 1457998 | 2020-11-06 18:56:25 +0800 | [diff] [blame] | 243 | /* Setup stop mode with GPR to support wakeup */ |
| 244 | #define FLEXCAN_QUIRK_SETUP_STOP_MODE_GPR BIT(8) |
Joakim Zhang | eadf6ca | 2019-07-12 08:02:44 +0000 | [diff] [blame] | 245 | /* Support CAN-FD mode */ |
| 246 | #define FLEXCAN_QUIRK_SUPPORT_FD BIT(9) |
Joakim Zhang | a659712 | 2020-09-30 05:15:55 +0800 | [diff] [blame] | 247 | /* support memory detection and correction */ |
| 248 | #define FLEXCAN_QUIRK_SUPPORT_ECC BIT(10) |
Joakim Zhang | 812f011 | 2020-11-06 18:56:27 +0800 | [diff] [blame] | 249 | /* Setup stop mode with SCU firmware to support wakeup */ |
| 250 | #define FLEXCAN_QUIRK_SETUP_STOP_MODE_SCFW BIT(11) |
Angelo Dureghello | d9cead7 | 2021-07-02 11:48:41 +0200 | [diff] [blame] | 251 | /* Setup 3 separate interrupts, main, boff and err */ |
| 252 | #define FLEXCAN_QUIRK_NR_IRQ_3 BIT(12) |
| 253 | /* Setup 16 mailboxes */ |
| 254 | #define FLEXCAN_QUIRK_NR_MB_16 BIT(13) |
Wolfgang Grandegger | 4f72e5f | 2012-09-28 03:17:15 +0000 | [diff] [blame] | 255 | |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 256 | /* Structure of the message buffer */ |
| 257 | struct flexcan_mb { |
| 258 | u32 can_ctrl; |
| 259 | u32 can_id; |
Pankaj Bansal | 0517961 | 2018-11-23 22:18:44 +0100 | [diff] [blame] | 260 | u32 data[]; |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 261 | }; |
| 262 | |
| 263 | /* Structure of the hardware registers */ |
| 264 | struct flexcan_regs { |
| 265 | u32 mcr; /* 0x00 */ |
Marc Kleine-Budde | fe63a06 | 2020-09-22 16:44:13 +0200 | [diff] [blame] | 266 | u32 ctrl; /* 0x04 - Not affected by Soft Reset */ |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 267 | u32 timer; /* 0x08 */ |
Marc Kleine-Budde | 4b70287 | 2020-09-22 16:44:12 +0200 | [diff] [blame] | 268 | u32 tcr; /* 0x0c */ |
Marc Kleine-Budde | fe63a06 | 2020-09-22 16:44:13 +0200 | [diff] [blame] | 269 | u32 rxgmask; /* 0x10 - Not affected by Soft Reset */ |
| 270 | u32 rx14mask; /* 0x14 - Not affected by Soft Reset */ |
| 271 | u32 rx15mask; /* 0x18 - Not affected by Soft Reset */ |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 272 | u32 ecr; /* 0x1c */ |
| 273 | u32 esr; /* 0x20 */ |
| 274 | u32 imask2; /* 0x24 */ |
| 275 | u32 imask1; /* 0x28 */ |
| 276 | u32 iflag2; /* 0x2c */ |
| 277 | u32 iflag1; /* 0x30 */ |
Marc Kleine-Budde | 62d1086 | 2015-08-27 16:01:27 +0200 | [diff] [blame] | 278 | union { /* 0x34 */ |
| 279 | u32 gfwr_mx28; /* MX28, MX53 */ |
Marc Kleine-Budde | fe63a06 | 2020-09-22 16:44:13 +0200 | [diff] [blame] | 280 | u32 ctrl2; /* MX6, VF610 - Not affected by Soft Reset */ |
Marc Kleine-Budde | 62d1086 | 2015-08-27 16:01:27 +0200 | [diff] [blame] | 281 | }; |
Hui Wang | 30c1e67 | 2012-06-28 16:21:35 +0800 | [diff] [blame] | 282 | u32 esr2; /* 0x38 */ |
| 283 | u32 imeur; /* 0x3c */ |
| 284 | u32 lrfr; /* 0x40 */ |
| 285 | u32 crcr; /* 0x44 */ |
| 286 | u32 rxfgmask; /* 0x48 */ |
Marc Kleine-Budde | fe63a06 | 2020-09-22 16:44:13 +0200 | [diff] [blame] | 287 | u32 rxfir; /* 0x4c - Not affected by Soft Reset */ |
| 288 | u32 cbt; /* 0x50 - Not affected by Soft Reset */ |
Marc Kleine-Budde | 4b70287 | 2020-09-22 16:44:12 +0200 | [diff] [blame] | 289 | u32 _reserved2; /* 0x54 */ |
| 290 | u32 dbg1; /* 0x58 */ |
| 291 | u32 dbg2; /* 0x5c */ |
| 292 | u32 _reserved3[8]; /* 0x60 */ |
Kees Cook | c92a08c | 2021-07-31 17:50:58 -0700 | [diff] [blame] | 293 | struct_group(init, |
| 294 | u8 mb[2][512]; /* 0x80 - Not affected by Soft Reset */ |
| 295 | /* FIFO-mode: |
| 296 | * MB |
| 297 | * 0x080...0x08f 0 RX message buffer |
| 298 | * 0x090...0x0df 1-5 reserved |
| 299 | * 0x0e0...0x0ff 6-7 8 entry ID table |
| 300 | * (mx25, mx28, mx35, mx53) |
| 301 | * 0x0e0...0x2df 6-7..37 8..128 entry ID table |
| 302 | * size conf'ed via ctrl2::RFFN |
| 303 | * (mx6, vf610) |
| 304 | */ |
| 305 | u32 _reserved4[256]; /* 0x480 */ |
| 306 | u32 rximr[64]; /* 0x880 - Not affected by Soft Reset */ |
| 307 | u32 _reserved5[24]; /* 0x980 */ |
| 308 | u32 gfwr_mx6; /* 0x9e0 - MX6 */ |
| 309 | u32 _reserved6[39]; /* 0x9e4 */ |
| 310 | u32 _rxfir[6]; /* 0xa80 */ |
| 311 | u32 _reserved8[2]; /* 0xa98 */ |
| 312 | u32 _rxmgmask; /* 0xaa0 */ |
| 313 | u32 _rxfgmask; /* 0xaa4 */ |
| 314 | u32 _rx14mask; /* 0xaa8 */ |
| 315 | u32 _rx15mask; /* 0xaac */ |
| 316 | u32 tx_smb[4]; /* 0xab0 */ |
| 317 | u32 rx_smb0[4]; /* 0xac0 */ |
| 318 | u32 rx_smb1[4]; /* 0xad0 */ |
| 319 | ); |
Stefan Agner | cdce844 | 2014-07-15 14:56:21 +0200 | [diff] [blame] | 320 | u32 mecr; /* 0xae0 */ |
| 321 | u32 erriar; /* 0xae4 */ |
| 322 | u32 erridpr; /* 0xae8 */ |
| 323 | u32 errippr; /* 0xaec */ |
| 324 | u32 rerrar; /* 0xaf0 */ |
| 325 | u32 rerrdr; /* 0xaf4 */ |
| 326 | u32 rerrsynr; /* 0xaf8 */ |
| 327 | u32 errsr; /* 0xafc */ |
Marc Kleine-Budde | 4b70287 | 2020-09-22 16:44:12 +0200 | [diff] [blame] | 328 | u32 _reserved7[64]; /* 0xb00 */ |
Marc Kleine-Budde | fe63a06 | 2020-09-22 16:44:13 +0200 | [diff] [blame] | 329 | u32 fdctrl; /* 0xc00 - Not affected by Soft Reset */ |
| 330 | u32 fdcbt; /* 0xc04 - Not affected by Soft Reset */ |
Marc Kleine-Budde | 4b70287 | 2020-09-22 16:44:12 +0200 | [diff] [blame] | 331 | u32 fdcrc; /* 0xc08 */ |
Joakim Zhang | a659712 | 2020-09-30 05:15:55 +0800 | [diff] [blame] | 332 | u32 _reserved9[199]; /* 0xc0c */ |
Kees Cook | c92a08c | 2021-07-31 17:50:58 -0700 | [diff] [blame] | 333 | struct_group(init_fd, |
| 334 | u32 tx_smb_fd[18]; /* 0xf28 */ |
| 335 | u32 rx_smb0_fd[18]; /* 0xf70 */ |
| 336 | u32 rx_smb1_fd[18]; /* 0xfb8 */ |
| 337 | ); |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 338 | }; |
| 339 | |
Joakim Zhang | a659712 | 2020-09-30 05:15:55 +0800 | [diff] [blame] | 340 | static_assert(sizeof(struct flexcan_regs) == 0x4 * 18 + 0xfb8); |
Marc Kleine-Budde | 4b70287 | 2020-09-22 16:44:12 +0200 | [diff] [blame] | 341 | |
Hui Wang | 30c1e67 | 2012-06-28 16:21:35 +0800 | [diff] [blame] | 342 | struct flexcan_devtype_data { |
Marc Kleine-Budde | f377bff | 2015-05-08 15:22:36 +0200 | [diff] [blame] | 343 | u32 quirks; /* quirks needed for different IP cores */ |
Hui Wang | 30c1e67 | 2012-06-28 16:21:35 +0800 | [diff] [blame] | 344 | }; |
| 345 | |
Aisheng Dong | de3578c | 2018-11-23 08:35:33 +0000 | [diff] [blame] | 346 | struct flexcan_stop_mode { |
| 347 | struct regmap *gpr; |
| 348 | u8 req_gpr; |
| 349 | u8 req_bit; |
Aisheng Dong | de3578c | 2018-11-23 08:35:33 +0000 | [diff] [blame] | 350 | }; |
| 351 | |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 352 | struct flexcan_priv { |
| 353 | struct can_priv can; |
Marc Kleine-Budde | 3016475 | 2015-05-10 15:26:58 +0200 | [diff] [blame] | 354 | struct can_rx_offload offload; |
Aisheng Dong | ca10989 | 2018-11-30 08:53:26 +0000 | [diff] [blame] | 355 | struct device *dev; |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 356 | |
Marc Kleine-Budde | 89af874 | 2015-05-08 09:32:58 +0200 | [diff] [blame] | 357 | struct flexcan_regs __iomem *regs; |
Pankaj Bansal | 0517961 | 2018-11-23 22:18:44 +0100 | [diff] [blame] | 358 | struct flexcan_mb __iomem *tx_mb; |
Marc Kleine-Budde | b93917c | 2015-07-12 00:47:47 +0200 | [diff] [blame] | 359 | struct flexcan_mb __iomem *tx_mb_reserved; |
Pankaj Bansal | 0517961 | 2018-11-23 22:18:44 +0100 | [diff] [blame] | 360 | u8 tx_mb_idx; |
| 361 | u8 mb_count; |
| 362 | u8 mb_size; |
Dong Aisheng | 8c306be | 2018-12-13 07:08:00 +0000 | [diff] [blame] | 363 | u8 clk_src; /* clock source of CAN Protocol Engine */ |
Joakim Zhang | 812f011 | 2020-11-06 18:56:27 +0800 | [diff] [blame] | 364 | u8 scu_idx; |
Dong Aisheng | 8c306be | 2018-12-13 07:08:00 +0000 | [diff] [blame] | 365 | |
Marc Kleine-Budde | 8ce5139 | 2019-03-01 12:17:30 +0100 | [diff] [blame] | 366 | u64 rx_mask; |
Marc Kleine-Budde | 0ca64f02 | 2019-03-01 13:54:19 +0100 | [diff] [blame] | 367 | u64 tx_mask; |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 368 | u32 reg_ctrl_default; |
| 369 | |
Steffen Trumtrar | 3d42a37 | 2012-07-17 16:14:34 +0200 | [diff] [blame] | 370 | struct clk *clk_ipg; |
| 371 | struct clk *clk_per; |
Marc Kleine-Budde | dda0b3b | 2012-07-13 14:52:48 +0200 | [diff] [blame] | 372 | const struct flexcan_devtype_data *devtype_data; |
Fabio Estevam | b7c4114 | 2013-06-10 23:12:57 -0300 | [diff] [blame] | 373 | struct regulator *reg_xceiver; |
Aisheng Dong | de3578c | 2018-11-23 08:35:33 +0000 | [diff] [blame] | 374 | struct flexcan_stop_mode stm; |
Pankaj Bansal | 88462d2 | 2017-11-24 18:52:08 +0530 | [diff] [blame] | 375 | |
Angelo Dureghello | d9cead7 | 2021-07-02 11:48:41 +0200 | [diff] [blame] | 376 | int irq_boff; |
| 377 | int irq_err; |
| 378 | |
Joakim Zhang | 812f011 | 2020-11-06 18:56:27 +0800 | [diff] [blame] | 379 | /* IPC handle when setup stop mode by System Controller firmware(scfw) */ |
| 380 | struct imx_sc_ipc *sc_ipc_handle; |
| 381 | |
Pankaj Bansal | 88462d2 | 2017-11-24 18:52:08 +0530 | [diff] [blame] | 382 | /* Read and Write APIs */ |
| 383 | u32 (*read)(void __iomem *addr); |
| 384 | void (*write)(u32 val, void __iomem *addr); |
Hui Wang | 30c1e67 | 2012-06-28 16:21:35 +0800 | [diff] [blame] | 385 | }; |
| 386 | |
Angelo Dureghello | d9cead7 | 2021-07-02 11:48:41 +0200 | [diff] [blame] | 387 | static const struct flexcan_devtype_data fsl_mcf5441x_devtype_data = { |
| 388 | .quirks = FLEXCAN_QUIRK_BROKEN_PERR_STATE | |
| 389 | FLEXCAN_QUIRK_NR_IRQ_3 | FLEXCAN_QUIRK_NR_MB_16, |
| 390 | }; |
| 391 | |
Marc Kleine-Budde | a3c11a7 | 2016-07-04 14:45:44 +0200 | [diff] [blame] | 392 | static const struct flexcan_devtype_data fsl_p1010_devtype_data = { |
ZHU Yi (ST-FIR/ENG1-Zhu) | fb5b91d6 | 2017-09-15 07:09:37 +0000 | [diff] [blame] | 393 | .quirks = FLEXCAN_QUIRK_BROKEN_WERR_STATE | |
Uwe Kleine-König | 0e030a3 | 2018-04-25 16:50:39 +0200 | [diff] [blame] | 394 | FLEXCAN_QUIRK_BROKEN_PERR_STATE | |
| 395 | FLEXCAN_QUIRK_DEFAULT_BIG_ENDIAN, |
| 396 | }; |
| 397 | |
| 398 | static const struct flexcan_devtype_data fsl_imx25_devtype_data = { |
| 399 | .quirks = FLEXCAN_QUIRK_BROKEN_WERR_STATE | |
ZHU Yi (ST-FIR/ENG1-Zhu) | fb5b91d6 | 2017-09-15 07:09:37 +0000 | [diff] [blame] | 400 | FLEXCAN_QUIRK_BROKEN_PERR_STATE, |
Hui Wang | 30c1e67 | 2012-06-28 16:21:35 +0800 | [diff] [blame] | 401 | }; |
Marc Kleine-Budde | 0012e5c | 2015-08-06 14:53:57 +0200 | [diff] [blame] | 402 | |
ZHU Yi (ST-FIR/ENG1-Zhu) | 083c557 | 2017-09-15 07:08:23 +0000 | [diff] [blame] | 403 | static const struct flexcan_devtype_data fsl_imx28_devtype_data = { |
| 404 | .quirks = FLEXCAN_QUIRK_BROKEN_PERR_STATE, |
| 405 | }; |
Marc Kleine-Budde | 0012e5c | 2015-08-06 14:53:57 +0200 | [diff] [blame] | 406 | |
Marc Kleine-Budde | a3c11a7 | 2016-07-04 14:45:44 +0200 | [diff] [blame] | 407 | static const struct flexcan_devtype_data fsl_imx6q_devtype_data = { |
Marc Kleine-Budde | 096de07 | 2015-09-01 10:28:46 +0200 | [diff] [blame] | 408 | .quirks = FLEXCAN_QUIRK_DISABLE_RXFG | FLEXCAN_QUIRK_ENABLE_EACEN_RRS | |
Aisheng Dong | de3578c | 2018-11-23 08:35:33 +0000 | [diff] [blame] | 409 | FLEXCAN_QUIRK_USE_OFF_TIMESTAMP | FLEXCAN_QUIRK_BROKEN_PERR_STATE | |
Joakim Zhang | 1457998 | 2020-11-06 18:56:25 +0800 | [diff] [blame] | 410 | FLEXCAN_QUIRK_SETUP_STOP_MODE_GPR, |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 411 | }; |
Marc Kleine-Budde | 0012e5c | 2015-08-06 14:53:57 +0200 | [diff] [blame] | 412 | |
Joakim Zhang | 2a1993e | 2019-07-12 08:02:59 +0000 | [diff] [blame] | 413 | static const struct flexcan_devtype_data fsl_imx8qm_devtype_data = { |
| 414 | .quirks = FLEXCAN_QUIRK_DISABLE_RXFG | FLEXCAN_QUIRK_ENABLE_EACEN_RRS | |
| 415 | FLEXCAN_QUIRK_USE_OFF_TIMESTAMP | FLEXCAN_QUIRK_BROKEN_PERR_STATE | |
Joakim Zhang | 812f011 | 2020-11-06 18:56:27 +0800 | [diff] [blame] | 416 | FLEXCAN_QUIRK_SUPPORT_FD | FLEXCAN_QUIRK_SETUP_STOP_MODE_SCFW, |
Joakim Zhang | 2a1993e | 2019-07-12 08:02:59 +0000 | [diff] [blame] | 417 | }; |
| 418 | |
Joakim Zhang | 3aa2539 | 2020-09-30 05:15:56 +0800 | [diff] [blame] | 419 | static struct flexcan_devtype_data fsl_imx8mp_devtype_data = { |
| 420 | .quirks = FLEXCAN_QUIRK_DISABLE_RXFG | FLEXCAN_QUIRK_ENABLE_EACEN_RRS | |
| 421 | FLEXCAN_QUIRK_DISABLE_MECR | FLEXCAN_QUIRK_USE_OFF_TIMESTAMP | |
Joakim Zhang | 1457998 | 2020-11-06 18:56:25 +0800 | [diff] [blame] | 422 | FLEXCAN_QUIRK_BROKEN_PERR_STATE | FLEXCAN_QUIRK_SETUP_STOP_MODE_GPR | |
Joakim Zhang | 3aa2539 | 2020-09-30 05:15:56 +0800 | [diff] [blame] | 423 | FLEXCAN_QUIRK_SUPPORT_FD | FLEXCAN_QUIRK_SUPPORT_ECC, |
| 424 | }; |
| 425 | |
Marc Kleine-Budde | a3c11a7 | 2016-07-04 14:45:44 +0200 | [diff] [blame] | 426 | static const struct flexcan_devtype_data fsl_vf610_devtype_data = { |
Marc Kleine-Budde | 9eb7aa8 | 2015-09-01 08:57:55 +0200 | [diff] [blame] | 427 | .quirks = FLEXCAN_QUIRK_DISABLE_RXFG | FLEXCAN_QUIRK_ENABLE_EACEN_RRS | |
Marc Kleine-Budde | 29c64b1 | 2017-11-27 09:18:21 +0100 | [diff] [blame] | 428 | FLEXCAN_QUIRK_DISABLE_MECR | FLEXCAN_QUIRK_USE_OFF_TIMESTAMP | |
Joakim Zhang | 5fdf04a | 2020-10-20 23:53:57 +0800 | [diff] [blame] | 429 | FLEXCAN_QUIRK_BROKEN_PERR_STATE | FLEXCAN_QUIRK_SUPPORT_ECC, |
Stefan Agner | cdce844 | 2014-07-15 14:56:21 +0200 | [diff] [blame] | 430 | }; |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 431 | |
Pankaj Bansal | 99b7668 | 2017-11-24 18:52:09 +0530 | [diff] [blame] | 432 | static const struct flexcan_devtype_data fsl_ls1021a_r2_devtype_data = { |
| 433 | .quirks = FLEXCAN_QUIRK_DISABLE_RXFG | FLEXCAN_QUIRK_ENABLE_EACEN_RRS | |
Joakim Zhang | 0187996 | 2020-10-20 23:53:55 +0800 | [diff] [blame] | 434 | FLEXCAN_QUIRK_BROKEN_PERR_STATE | FLEXCAN_QUIRK_USE_OFF_TIMESTAMP, |
Pankaj Bansal | 99b7668 | 2017-11-24 18:52:09 +0530 | [diff] [blame] | 435 | }; |
| 436 | |
Joakim Zhang | 2c19bb4 | 2019-07-12 08:03:01 +0000 | [diff] [blame] | 437 | static const struct flexcan_devtype_data fsl_lx2160a_r1_devtype_data = { |
| 438 | .quirks = FLEXCAN_QUIRK_DISABLE_RXFG | FLEXCAN_QUIRK_ENABLE_EACEN_RRS | |
| 439 | FLEXCAN_QUIRK_DISABLE_MECR | FLEXCAN_QUIRK_BROKEN_PERR_STATE | |
Joakim Zhang | 91a22bf | 2020-10-20 23:53:56 +0800 | [diff] [blame] | 440 | FLEXCAN_QUIRK_USE_OFF_TIMESTAMP | FLEXCAN_QUIRK_SUPPORT_FD | |
| 441 | FLEXCAN_QUIRK_SUPPORT_ECC, |
Joakim Zhang | 2c19bb4 | 2019-07-12 08:03:01 +0000 | [diff] [blame] | 442 | }; |
| 443 | |
Marc Kleine-Budde | 194b9a4 | 2012-07-16 12:58:31 +0200 | [diff] [blame] | 444 | static const struct can_bittiming_const flexcan_bittiming_const = { |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 445 | .name = DRV_NAME, |
| 446 | .tseg1_min = 4, |
| 447 | .tseg1_max = 16, |
| 448 | .tseg2_min = 2, |
| 449 | .tseg2_max = 8, |
| 450 | .sjw_max = 4, |
| 451 | .brp_min = 1, |
| 452 | .brp_max = 256, |
| 453 | .brp_inc = 1, |
| 454 | }; |
| 455 | |
Joakim Zhang | eadf6ca | 2019-07-12 08:02:44 +0000 | [diff] [blame] | 456 | static const struct can_bittiming_const flexcan_fd_bittiming_const = { |
| 457 | .name = DRV_NAME, |
| 458 | .tseg1_min = 2, |
| 459 | .tseg1_max = 96, |
| 460 | .tseg2_min = 2, |
| 461 | .tseg2_max = 32, |
| 462 | .sjw_max = 16, |
| 463 | .brp_min = 1, |
| 464 | .brp_max = 1024, |
| 465 | .brp_inc = 1, |
| 466 | }; |
| 467 | |
| 468 | static const struct can_bittiming_const flexcan_fd_data_bittiming_const = { |
| 469 | .name = DRV_NAME, |
| 470 | .tseg1_min = 2, |
| 471 | .tseg1_max = 39, |
| 472 | .tseg2_min = 2, |
| 473 | .tseg2_max = 8, |
| 474 | .sjw_max = 4, |
| 475 | .brp_min = 1, |
| 476 | .brp_max = 1024, |
| 477 | .brp_inc = 1, |
| 478 | }; |
| 479 | |
Pankaj Bansal | 88462d2 | 2017-11-24 18:52:08 +0530 | [diff] [blame] | 480 | /* FlexCAN module is essentially modelled as a little-endian IP in most |
| 481 | * SoCs, i.e the registers as well as the message buffer areas are |
| 482 | * implemented in a little-endian fashion. |
| 483 | * |
| 484 | * However there are some SoCs (e.g. LS1021A) which implement the FlexCAN |
| 485 | * module in a big-endian fashion (i.e the registers as well as the |
| 486 | * message buffer areas are implemented in a big-endian way). |
| 487 | * |
| 488 | * In addition, the FlexCAN module can be found on SoCs having ARM or |
| 489 | * PPC cores. So, we need to abstract off the register read/write |
| 490 | * functions, ensuring that these cater to all the combinations of module |
| 491 | * endianness and underlying CPU endianness. |
holt@sgi.com | 61e271e | 2011-08-16 17:32:20 +0000 | [diff] [blame] | 492 | */ |
Pankaj Bansal | 88462d2 | 2017-11-24 18:52:08 +0530 | [diff] [blame] | 493 | static inline u32 flexcan_read_be(void __iomem *addr) |
holt@sgi.com | 61e271e | 2011-08-16 17:32:20 +0000 | [diff] [blame] | 494 | { |
Pankaj Bansal | 88462d2 | 2017-11-24 18:52:08 +0530 | [diff] [blame] | 495 | return ioread32be(addr); |
holt@sgi.com | 61e271e | 2011-08-16 17:32:20 +0000 | [diff] [blame] | 496 | } |
| 497 | |
Pankaj Bansal | 88462d2 | 2017-11-24 18:52:08 +0530 | [diff] [blame] | 498 | static inline void flexcan_write_be(u32 val, void __iomem *addr) |
holt@sgi.com | 61e271e | 2011-08-16 17:32:20 +0000 | [diff] [blame] | 499 | { |
Pankaj Bansal | 88462d2 | 2017-11-24 18:52:08 +0530 | [diff] [blame] | 500 | iowrite32be(val, addr); |
holt@sgi.com | 61e271e | 2011-08-16 17:32:20 +0000 | [diff] [blame] | 501 | } |
| 502 | |
Pankaj Bansal | 88462d2 | 2017-11-24 18:52:08 +0530 | [diff] [blame] | 503 | static inline u32 flexcan_read_le(void __iomem *addr) |
holt@sgi.com | 61e271e | 2011-08-16 17:32:20 +0000 | [diff] [blame] | 504 | { |
Pankaj Bansal | 88462d2 | 2017-11-24 18:52:08 +0530 | [diff] [blame] | 505 | return ioread32(addr); |
holt@sgi.com | 61e271e | 2011-08-16 17:32:20 +0000 | [diff] [blame] | 506 | } |
Pankaj Bansal | 88462d2 | 2017-11-24 18:52:08 +0530 | [diff] [blame] | 507 | |
| 508 | static inline void flexcan_write_le(u32 val, void __iomem *addr) |
| 509 | { |
| 510 | iowrite32(val, addr); |
| 511 | } |
holt@sgi.com | 61e271e | 2011-08-16 17:32:20 +0000 | [diff] [blame] | 512 | |
Pankaj Bansal | 0517961 | 2018-11-23 22:18:44 +0100 | [diff] [blame] | 513 | static struct flexcan_mb __iomem *flexcan_get_mb(const struct flexcan_priv *priv, |
| 514 | u8 mb_index) |
| 515 | { |
Pankaj Bansal | 6cbf760 | 2018-08-28 23:19:12 +0530 | [diff] [blame] | 516 | u8 bank_size; |
| 517 | bool bank; |
| 518 | |
Pankaj Bansal | 0517961 | 2018-11-23 22:18:44 +0100 | [diff] [blame] | 519 | if (WARN_ON(mb_index >= priv->mb_count)) |
| 520 | return NULL; |
| 521 | |
Pankaj Bansal | 6cbf760 | 2018-08-28 23:19:12 +0530 | [diff] [blame] | 522 | bank_size = sizeof(priv->regs->mb[0]) / priv->mb_size; |
| 523 | |
| 524 | bank = mb_index >= bank_size; |
| 525 | if (bank) |
| 526 | mb_index -= bank_size; |
| 527 | |
Pankaj Bansal | 0517961 | 2018-11-23 22:18:44 +0100 | [diff] [blame] | 528 | return (struct flexcan_mb __iomem *) |
Pankaj Bansal | 6cbf760 | 2018-08-28 23:19:12 +0530 | [diff] [blame] | 529 | (&priv->regs->mb[bank][priv->mb_size * mb_index]); |
Pankaj Bansal | 0517961 | 2018-11-23 22:18:44 +0100 | [diff] [blame] | 530 | } |
| 531 | |
Joakim Zhang | b7603d0 | 2019-12-04 11:36:11 +0000 | [diff] [blame] | 532 | static int flexcan_low_power_enter_ack(struct flexcan_priv *priv) |
| 533 | { |
| 534 | struct flexcan_regs __iomem *regs = priv->regs; |
| 535 | unsigned int timeout = FLEXCAN_TIMEOUT_US / 10; |
| 536 | |
| 537 | while (timeout-- && !(priv->read(®s->mcr) & FLEXCAN_MCR_LPM_ACK)) |
| 538 | udelay(10); |
| 539 | |
| 540 | if (!(priv->read(®s->mcr) & FLEXCAN_MCR_LPM_ACK)) |
| 541 | return -ETIMEDOUT; |
| 542 | |
| 543 | return 0; |
| 544 | } |
| 545 | |
| 546 | static int flexcan_low_power_exit_ack(struct flexcan_priv *priv) |
| 547 | { |
| 548 | struct flexcan_regs __iomem *regs = priv->regs; |
| 549 | unsigned int timeout = FLEXCAN_TIMEOUT_US / 10; |
| 550 | |
| 551 | while (timeout-- && (priv->read(®s->mcr) & FLEXCAN_MCR_LPM_ACK)) |
| 552 | udelay(10); |
| 553 | |
| 554 | if (priv->read(®s->mcr) & FLEXCAN_MCR_LPM_ACK) |
| 555 | return -ETIMEDOUT; |
| 556 | |
| 557 | return 0; |
| 558 | } |
| 559 | |
Aisheng Dong | de3578c | 2018-11-23 08:35:33 +0000 | [diff] [blame] | 560 | static void flexcan_enable_wakeup_irq(struct flexcan_priv *priv, bool enable) |
| 561 | { |
| 562 | struct flexcan_regs __iomem *regs = priv->regs; |
| 563 | u32 reg_mcr; |
| 564 | |
| 565 | reg_mcr = priv->read(®s->mcr); |
| 566 | |
| 567 | if (enable) |
| 568 | reg_mcr |= FLEXCAN_MCR_WAK_MSK; |
| 569 | else |
| 570 | reg_mcr &= ~FLEXCAN_MCR_WAK_MSK; |
| 571 | |
| 572 | priv->write(reg_mcr, ®s->mcr); |
| 573 | } |
| 574 | |
Joakim Zhang | 812f011 | 2020-11-06 18:56:27 +0800 | [diff] [blame] | 575 | static int flexcan_stop_mode_enable_scfw(struct flexcan_priv *priv, bool enabled) |
| 576 | { |
| 577 | u8 idx = priv->scu_idx; |
| 578 | u32 rsrc_id, val; |
| 579 | |
| 580 | rsrc_id = IMX_SC_R_CAN(idx); |
| 581 | |
| 582 | if (enabled) |
| 583 | val = 1; |
| 584 | else |
| 585 | val = 0; |
| 586 | |
| 587 | /* stop mode request via scu firmware */ |
| 588 | return imx_sc_misc_set_control(priv->sc_ipc_handle, rsrc_id, |
| 589 | IMX_SC_C_IPG_STOP, val); |
| 590 | } |
| 591 | |
Joakim Zhang | 5f186c2 | 2019-07-02 01:45:41 +0000 | [diff] [blame] | 592 | static inline int flexcan_enter_stop_mode(struct flexcan_priv *priv) |
Aisheng Dong | de3578c | 2018-11-23 08:35:33 +0000 | [diff] [blame] | 593 | { |
| 594 | struct flexcan_regs __iomem *regs = priv->regs; |
| 595 | u32 reg_mcr; |
Joakim Zhang | 812f011 | 2020-11-06 18:56:27 +0800 | [diff] [blame] | 596 | int ret; |
Aisheng Dong | de3578c | 2018-11-23 08:35:33 +0000 | [diff] [blame] | 597 | |
| 598 | reg_mcr = priv->read(®s->mcr); |
| 599 | reg_mcr |= FLEXCAN_MCR_SLF_WAK; |
| 600 | priv->write(reg_mcr, ®s->mcr); |
| 601 | |
| 602 | /* enable stop request */ |
Joakim Zhang | 812f011 | 2020-11-06 18:56:27 +0800 | [diff] [blame] | 603 | if (priv->devtype_data->quirks & FLEXCAN_QUIRK_SETUP_STOP_MODE_SCFW) { |
| 604 | ret = flexcan_stop_mode_enable_scfw(priv, true); |
| 605 | if (ret < 0) |
| 606 | return ret; |
| 607 | } else { |
| 608 | regmap_update_bits(priv->stm.gpr, priv->stm.req_gpr, |
| 609 | 1 << priv->stm.req_bit, 1 << priv->stm.req_bit); |
| 610 | } |
Joakim Zhang | 5f186c2 | 2019-07-02 01:45:41 +0000 | [diff] [blame] | 611 | |
Joakim Zhang | 048e3a34 | 2019-12-04 11:36:14 +0000 | [diff] [blame] | 612 | return flexcan_low_power_enter_ack(priv); |
Aisheng Dong | de3578c | 2018-11-23 08:35:33 +0000 | [diff] [blame] | 613 | } |
| 614 | |
Joakim Zhang | 5f186c2 | 2019-07-02 01:45:41 +0000 | [diff] [blame] | 615 | static inline int flexcan_exit_stop_mode(struct flexcan_priv *priv) |
Aisheng Dong | de3578c | 2018-11-23 08:35:33 +0000 | [diff] [blame] | 616 | { |
| 617 | struct flexcan_regs __iomem *regs = priv->regs; |
| 618 | u32 reg_mcr; |
Joakim Zhang | 812f011 | 2020-11-06 18:56:27 +0800 | [diff] [blame] | 619 | int ret; |
Aisheng Dong | de3578c | 2018-11-23 08:35:33 +0000 | [diff] [blame] | 620 | |
| 621 | /* remove stop request */ |
Joakim Zhang | 812f011 | 2020-11-06 18:56:27 +0800 | [diff] [blame] | 622 | if (priv->devtype_data->quirks & FLEXCAN_QUIRK_SETUP_STOP_MODE_SCFW) { |
| 623 | ret = flexcan_stop_mode_enable_scfw(priv, false); |
| 624 | if (ret < 0) |
| 625 | return ret; |
| 626 | } else { |
| 627 | regmap_update_bits(priv->stm.gpr, priv->stm.req_gpr, |
| 628 | 1 << priv->stm.req_bit, 0); |
| 629 | } |
Aisheng Dong | de3578c | 2018-11-23 08:35:33 +0000 | [diff] [blame] | 630 | |
| 631 | reg_mcr = priv->read(®s->mcr); |
| 632 | reg_mcr &= ~FLEXCAN_MCR_SLF_WAK; |
| 633 | priv->write(reg_mcr, ®s->mcr); |
Joakim Zhang | 5f186c2 | 2019-07-02 01:45:41 +0000 | [diff] [blame] | 634 | |
Joakim Zhang | 048e3a34 | 2019-12-04 11:36:14 +0000 | [diff] [blame] | 635 | return flexcan_low_power_exit_ack(priv); |
Aisheng Dong | de3578c | 2018-11-23 08:35:33 +0000 | [diff] [blame] | 636 | } |
| 637 | |
ZHU Yi (ST-FIR/ENG1-Zhu) | da49a80 | 2017-09-15 07:03:58 +0000 | [diff] [blame] | 638 | static inline void flexcan_error_irq_enable(const struct flexcan_priv *priv) |
| 639 | { |
| 640 | struct flexcan_regs __iomem *regs = priv->regs; |
| 641 | u32 reg_ctrl = (priv->reg_ctrl_default | FLEXCAN_CTRL_ERR_MSK); |
| 642 | |
Pankaj Bansal | 88462d2 | 2017-11-24 18:52:08 +0530 | [diff] [blame] | 643 | priv->write(reg_ctrl, ®s->ctrl); |
ZHU Yi (ST-FIR/ENG1-Zhu) | da49a80 | 2017-09-15 07:03:58 +0000 | [diff] [blame] | 644 | } |
| 645 | |
| 646 | static inline void flexcan_error_irq_disable(const struct flexcan_priv *priv) |
| 647 | { |
| 648 | struct flexcan_regs __iomem *regs = priv->regs; |
| 649 | u32 reg_ctrl = (priv->reg_ctrl_default & ~FLEXCAN_CTRL_ERR_MSK); |
| 650 | |
Pankaj Bansal | 88462d2 | 2017-11-24 18:52:08 +0530 | [diff] [blame] | 651 | priv->write(reg_ctrl, ®s->ctrl); |
ZHU Yi (ST-FIR/ENG1-Zhu) | da49a80 | 2017-09-15 07:03:58 +0000 | [diff] [blame] | 652 | } |
| 653 | |
Aisheng Dong | ca10989 | 2018-11-30 08:53:26 +0000 | [diff] [blame] | 654 | static int flexcan_clks_enable(const struct flexcan_priv *priv) |
| 655 | { |
Marc Kleine-Budde | 3362666 | 2021-07-28 09:51:42 +0200 | [diff] [blame] | 656 | int err = 0; |
Aisheng Dong | ca10989 | 2018-11-30 08:53:26 +0000 | [diff] [blame] | 657 | |
Angelo Dureghello | d9cead7 | 2021-07-02 11:48:41 +0200 | [diff] [blame] | 658 | if (priv->clk_ipg) { |
| 659 | err = clk_prepare_enable(priv->clk_ipg); |
| 660 | if (err) |
| 661 | return err; |
| 662 | } |
Aisheng Dong | ca10989 | 2018-11-30 08:53:26 +0000 | [diff] [blame] | 663 | |
Angelo Dureghello | d9cead7 | 2021-07-02 11:48:41 +0200 | [diff] [blame] | 664 | if (priv->clk_per) { |
| 665 | err = clk_prepare_enable(priv->clk_per); |
| 666 | if (err) |
| 667 | clk_disable_unprepare(priv->clk_ipg); |
| 668 | } |
Aisheng Dong | ca10989 | 2018-11-30 08:53:26 +0000 | [diff] [blame] | 669 | |
| 670 | return err; |
| 671 | } |
| 672 | |
| 673 | static void flexcan_clks_disable(const struct flexcan_priv *priv) |
| 674 | { |
| 675 | clk_disable_unprepare(priv->clk_per); |
| 676 | clk_disable_unprepare(priv->clk_ipg); |
| 677 | } |
| 678 | |
Marc Kleine-Budde | f003698 | 2014-02-28 17:18:27 +0100 | [diff] [blame] | 679 | static inline int flexcan_transceiver_enable(const struct flexcan_priv *priv) |
| 680 | { |
| 681 | if (!priv->reg_xceiver) |
| 682 | return 0; |
| 683 | |
| 684 | return regulator_enable(priv->reg_xceiver); |
| 685 | } |
| 686 | |
| 687 | static inline int flexcan_transceiver_disable(const struct flexcan_priv *priv) |
| 688 | { |
| 689 | if (!priv->reg_xceiver) |
| 690 | return 0; |
| 691 | |
| 692 | return regulator_disable(priv->reg_xceiver); |
| 693 | } |
| 694 | |
Marc Kleine-Budde | 9b00b30 | 2014-02-28 15:30:18 +0100 | [diff] [blame] | 695 | static int flexcan_chip_enable(struct flexcan_priv *priv) |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 696 | { |
Marc Kleine-Budde | 89af874 | 2015-05-08 09:32:58 +0200 | [diff] [blame] | 697 | struct flexcan_regs __iomem *regs = priv->regs; |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 698 | u32 reg; |
| 699 | |
Pankaj Bansal | 88462d2 | 2017-11-24 18:52:08 +0530 | [diff] [blame] | 700 | reg = priv->read(®s->mcr); |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 701 | reg &= ~FLEXCAN_MCR_MDIS; |
Pankaj Bansal | 88462d2 | 2017-11-24 18:52:08 +0530 | [diff] [blame] | 702 | priv->write(reg, ®s->mcr); |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 703 | |
Joakim Zhang | b7603d0 | 2019-12-04 11:36:11 +0000 | [diff] [blame] | 704 | return flexcan_low_power_exit_ack(priv); |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 705 | } |
| 706 | |
Marc Kleine-Budde | 9b00b30 | 2014-02-28 15:30:18 +0100 | [diff] [blame] | 707 | static int flexcan_chip_disable(struct flexcan_priv *priv) |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 708 | { |
Marc Kleine-Budde | 89af874 | 2015-05-08 09:32:58 +0200 | [diff] [blame] | 709 | struct flexcan_regs __iomem *regs = priv->regs; |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 710 | u32 reg; |
| 711 | |
Pankaj Bansal | 88462d2 | 2017-11-24 18:52:08 +0530 | [diff] [blame] | 712 | reg = priv->read(®s->mcr); |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 713 | reg |= FLEXCAN_MCR_MDIS; |
Pankaj Bansal | 88462d2 | 2017-11-24 18:52:08 +0530 | [diff] [blame] | 714 | priv->write(reg, ®s->mcr); |
Marc Kleine-Budde | 9b00b30 | 2014-02-28 15:30:18 +0100 | [diff] [blame] | 715 | |
Joakim Zhang | b7603d0 | 2019-12-04 11:36:11 +0000 | [diff] [blame] | 716 | return flexcan_low_power_enter_ack(priv); |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 717 | } |
| 718 | |
Marc Kleine-Budde | b1aa1c7 | 2014-02-28 17:08:21 +0100 | [diff] [blame] | 719 | static int flexcan_chip_freeze(struct flexcan_priv *priv) |
| 720 | { |
Marc Kleine-Budde | 89af874 | 2015-05-08 09:32:58 +0200 | [diff] [blame] | 721 | struct flexcan_regs __iomem *regs = priv->regs; |
Angelo Dureghello | 47c5e47 | 2021-03-16 00:15:10 +0100 | [diff] [blame] | 722 | unsigned int timeout; |
| 723 | u32 bitrate = priv->can.bittiming.bitrate; |
Marc Kleine-Budde | b1aa1c7 | 2014-02-28 17:08:21 +0100 | [diff] [blame] | 724 | u32 reg; |
| 725 | |
Angelo Dureghello | 47c5e47 | 2021-03-16 00:15:10 +0100 | [diff] [blame] | 726 | if (bitrate) |
| 727 | timeout = 1000 * 1000 * 10 / bitrate; |
| 728 | else |
| 729 | timeout = FLEXCAN_TIMEOUT_US / 10; |
| 730 | |
Pankaj Bansal | 88462d2 | 2017-11-24 18:52:08 +0530 | [diff] [blame] | 731 | reg = priv->read(®s->mcr); |
Joakim Zhang | 449052c | 2021-02-18 19:00:35 +0800 | [diff] [blame] | 732 | reg |= FLEXCAN_MCR_FRZ | FLEXCAN_MCR_HALT; |
Pankaj Bansal | 88462d2 | 2017-11-24 18:52:08 +0530 | [diff] [blame] | 733 | priv->write(reg, ®s->mcr); |
Marc Kleine-Budde | b1aa1c7 | 2014-02-28 17:08:21 +0100 | [diff] [blame] | 734 | |
Pankaj Bansal | 88462d2 | 2017-11-24 18:52:08 +0530 | [diff] [blame] | 735 | while (timeout-- && !(priv->read(®s->mcr) & FLEXCAN_MCR_FRZ_ACK)) |
David Jander | 8badd65 | 2014-08-27 12:02:16 +0200 | [diff] [blame] | 736 | udelay(100); |
Marc Kleine-Budde | b1aa1c7 | 2014-02-28 17:08:21 +0100 | [diff] [blame] | 737 | |
Pankaj Bansal | 88462d2 | 2017-11-24 18:52:08 +0530 | [diff] [blame] | 738 | if (!(priv->read(®s->mcr) & FLEXCAN_MCR_FRZ_ACK)) |
Marc Kleine-Budde | b1aa1c7 | 2014-02-28 17:08:21 +0100 | [diff] [blame] | 739 | return -ETIMEDOUT; |
| 740 | |
| 741 | return 0; |
| 742 | } |
| 743 | |
| 744 | static int flexcan_chip_unfreeze(struct flexcan_priv *priv) |
| 745 | { |
Marc Kleine-Budde | 89af874 | 2015-05-08 09:32:58 +0200 | [diff] [blame] | 746 | struct flexcan_regs __iomem *regs = priv->regs; |
Marc Kleine-Budde | b1aa1c7 | 2014-02-28 17:08:21 +0100 | [diff] [blame] | 747 | unsigned int timeout = FLEXCAN_TIMEOUT_US / 10; |
| 748 | u32 reg; |
| 749 | |
Pankaj Bansal | 88462d2 | 2017-11-24 18:52:08 +0530 | [diff] [blame] | 750 | reg = priv->read(®s->mcr); |
Marc Kleine-Budde | b1aa1c7 | 2014-02-28 17:08:21 +0100 | [diff] [blame] | 751 | reg &= ~FLEXCAN_MCR_HALT; |
Pankaj Bansal | 88462d2 | 2017-11-24 18:52:08 +0530 | [diff] [blame] | 752 | priv->write(reg, ®s->mcr); |
Marc Kleine-Budde | b1aa1c7 | 2014-02-28 17:08:21 +0100 | [diff] [blame] | 753 | |
Pankaj Bansal | 88462d2 | 2017-11-24 18:52:08 +0530 | [diff] [blame] | 754 | while (timeout-- && (priv->read(®s->mcr) & FLEXCAN_MCR_FRZ_ACK)) |
David Jander | 8badd65 | 2014-08-27 12:02:16 +0200 | [diff] [blame] | 755 | udelay(10); |
Marc Kleine-Budde | b1aa1c7 | 2014-02-28 17:08:21 +0100 | [diff] [blame] | 756 | |
Pankaj Bansal | 88462d2 | 2017-11-24 18:52:08 +0530 | [diff] [blame] | 757 | if (priv->read(®s->mcr) & FLEXCAN_MCR_FRZ_ACK) |
Marc Kleine-Budde | b1aa1c7 | 2014-02-28 17:08:21 +0100 | [diff] [blame] | 758 | return -ETIMEDOUT; |
| 759 | |
| 760 | return 0; |
| 761 | } |
| 762 | |
Marc Kleine-Budde | 4b5b822 | 2014-02-28 15:16:59 +0100 | [diff] [blame] | 763 | static int flexcan_chip_softreset(struct flexcan_priv *priv) |
| 764 | { |
Marc Kleine-Budde | 89af874 | 2015-05-08 09:32:58 +0200 | [diff] [blame] | 765 | struct flexcan_regs __iomem *regs = priv->regs; |
Marc Kleine-Budde | 4b5b822 | 2014-02-28 15:16:59 +0100 | [diff] [blame] | 766 | unsigned int timeout = FLEXCAN_TIMEOUT_US / 10; |
| 767 | |
Pankaj Bansal | 88462d2 | 2017-11-24 18:52:08 +0530 | [diff] [blame] | 768 | priv->write(FLEXCAN_MCR_SOFTRST, ®s->mcr); |
| 769 | while (timeout-- && (priv->read(®s->mcr) & FLEXCAN_MCR_SOFTRST)) |
David Jander | 8badd65 | 2014-08-27 12:02:16 +0200 | [diff] [blame] | 770 | udelay(10); |
Marc Kleine-Budde | 4b5b822 | 2014-02-28 15:16:59 +0100 | [diff] [blame] | 771 | |
Pankaj Bansal | 88462d2 | 2017-11-24 18:52:08 +0530 | [diff] [blame] | 772 | if (priv->read(®s->mcr) & FLEXCAN_MCR_SOFTRST) |
Marc Kleine-Budde | 4b5b822 | 2014-02-28 15:16:59 +0100 | [diff] [blame] | 773 | return -ETIMEDOUT; |
| 774 | |
| 775 | return 0; |
| 776 | } |
| 777 | |
Stefan Agner | ec56acf | 2014-07-15 14:56:20 +0200 | [diff] [blame] | 778 | static int __flexcan_get_berr_counter(const struct net_device *dev, |
| 779 | struct can_berr_counter *bec) |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 780 | { |
| 781 | const struct flexcan_priv *priv = netdev_priv(dev); |
Marc Kleine-Budde | 89af874 | 2015-05-08 09:32:58 +0200 | [diff] [blame] | 782 | struct flexcan_regs __iomem *regs = priv->regs; |
Pankaj Bansal | 88462d2 | 2017-11-24 18:52:08 +0530 | [diff] [blame] | 783 | u32 reg = priv->read(®s->ecr); |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 784 | |
| 785 | bec->txerr = (reg >> 0) & 0xff; |
| 786 | bec->rxerr = (reg >> 8) & 0xff; |
| 787 | |
| 788 | return 0; |
| 789 | } |
| 790 | |
Stefan Agner | ec56acf | 2014-07-15 14:56:20 +0200 | [diff] [blame] | 791 | static int flexcan_get_berr_counter(const struct net_device *dev, |
| 792 | struct can_berr_counter *bec) |
| 793 | { |
| 794 | const struct flexcan_priv *priv = netdev_priv(dev); |
| 795 | int err; |
| 796 | |
Aisheng Dong | ca10989 | 2018-11-30 08:53:26 +0000 | [diff] [blame] | 797 | err = pm_runtime_get_sync(priv->dev); |
Zhang Qilong | b7ee5bc | 2020-11-08 16:30:00 +0800 | [diff] [blame] | 798 | if (err < 0) { |
| 799 | pm_runtime_put_noidle(priv->dev); |
Stefan Agner | ec56acf | 2014-07-15 14:56:20 +0200 | [diff] [blame] | 800 | return err; |
Zhang Qilong | b7ee5bc | 2020-11-08 16:30:00 +0800 | [diff] [blame] | 801 | } |
Stefan Agner | ec56acf | 2014-07-15 14:56:20 +0200 | [diff] [blame] | 802 | |
Stefan Agner | ec56acf | 2014-07-15 14:56:20 +0200 | [diff] [blame] | 803 | err = __flexcan_get_berr_counter(dev, bec); |
| 804 | |
Aisheng Dong | ca10989 | 2018-11-30 08:53:26 +0000 | [diff] [blame] | 805 | pm_runtime_put(priv->dev); |
Stefan Agner | ec56acf | 2014-07-15 14:56:20 +0200 | [diff] [blame] | 806 | |
| 807 | return err; |
| 808 | } |
| 809 | |
Marc Kleine-Budde | fb1e13e6 | 2018-04-26 23:13:38 +0200 | [diff] [blame] | 810 | static netdev_tx_t flexcan_start_xmit(struct sk_buff *skb, struct net_device *dev) |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 811 | { |
| 812 | const struct flexcan_priv *priv = netdev_priv(dev); |
Joakim Zhang | d9b90b0 | 2019-07-12 08:02:41 +0000 | [diff] [blame] | 813 | struct canfd_frame *cfd = (struct canfd_frame *)skb->data; |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 814 | u32 can_id; |
Marc Kleine-Budde | 0012e5c | 2015-08-06 14:53:57 +0200 | [diff] [blame] | 815 | u32 data; |
Oliver Hartkopp | 3ab4ce0 | 2020-11-10 11:18:49 +0100 | [diff] [blame] | 816 | u32 ctrl = FLEXCAN_MB_CODE_TX_DATA | ((can_fd_len2dlc(cfd->len)) << 16); |
Pankaj Bansal | 0517961 | 2018-11-23 22:18:44 +0100 | [diff] [blame] | 817 | int i; |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 818 | |
| 819 | if (can_dropped_invalid_skb(dev, skb)) |
| 820 | return NETDEV_TX_OK; |
| 821 | |
| 822 | netif_stop_queue(dev); |
| 823 | |
Joakim Zhang | d9b90b0 | 2019-07-12 08:02:41 +0000 | [diff] [blame] | 824 | if (cfd->can_id & CAN_EFF_FLAG) { |
| 825 | can_id = cfd->can_id & CAN_EFF_MASK; |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 826 | ctrl |= FLEXCAN_MB_CNT_IDE | FLEXCAN_MB_CNT_SRR; |
| 827 | } else { |
Joakim Zhang | d9b90b0 | 2019-07-12 08:02:41 +0000 | [diff] [blame] | 828 | can_id = (cfd->can_id & CAN_SFF_MASK) << 18; |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 829 | } |
| 830 | |
Joakim Zhang | d9b90b0 | 2019-07-12 08:02:41 +0000 | [diff] [blame] | 831 | if (cfd->can_id & CAN_RTR_FLAG) |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 832 | ctrl |= FLEXCAN_MB_CNT_RTR; |
| 833 | |
Joakim Zhang | 0542920 | 2019-07-12 08:02:47 +0000 | [diff] [blame] | 834 | if (can_is_canfd_skb(skb)) { |
Joakim Zhang | eadf6ca | 2019-07-12 08:02:44 +0000 | [diff] [blame] | 835 | ctrl |= FLEXCAN_MB_CNT_EDL; |
| 836 | |
Joakim Zhang | 0542920 | 2019-07-12 08:02:47 +0000 | [diff] [blame] | 837 | if (cfd->flags & CANFD_BRS) |
| 838 | ctrl |= FLEXCAN_MB_CNT_BRS; |
| 839 | } |
| 840 | |
Joakim Zhang | d9b90b0 | 2019-07-12 08:02:41 +0000 | [diff] [blame] | 841 | for (i = 0; i < cfd->len; i += sizeof(u32)) { |
| 842 | data = be32_to_cpup((__be32 *)&cfd->data[i]); |
Pankaj Bansal | 0517961 | 2018-11-23 22:18:44 +0100 | [diff] [blame] | 843 | priv->write(data, &priv->tx_mb->data[i / sizeof(u32)]); |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 844 | } |
| 845 | |
Vincent Mailhol | 1dcb6e5 | 2021-01-11 15:19:27 +0100 | [diff] [blame] | 846 | can_put_echo_skb(skb, dev, 0, 0); |
Reuben Dowle | 9a12349 | 2011-11-01 11:18:03 +1300 | [diff] [blame] | 847 | |
Pankaj Bansal | 0517961 | 2018-11-23 22:18:44 +0100 | [diff] [blame] | 848 | priv->write(can_id, &priv->tx_mb->can_id); |
| 849 | priv->write(ctrl, &priv->tx_mb->can_ctrl); |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 850 | |
David Jander | 25e9244 | 2014-09-03 16:47:22 +0200 | [diff] [blame] | 851 | /* Errata ERR005829 step8: |
| 852 | * Write twice INACTIVE(0x8) code to first MB. |
| 853 | */ |
Pankaj Bansal | 88462d2 | 2017-11-24 18:52:08 +0530 | [diff] [blame] | 854 | priv->write(FLEXCAN_MB_CODE_TX_INACTIVE, |
Marc Kleine-Budde | 9dc1ee1 | 2018-11-12 15:33:57 +0100 | [diff] [blame] | 855 | &priv->tx_mb_reserved->can_ctrl); |
Pankaj Bansal | 88462d2 | 2017-11-24 18:52:08 +0530 | [diff] [blame] | 856 | priv->write(FLEXCAN_MB_CODE_TX_INACTIVE, |
Marc Kleine-Budde | 9dc1ee1 | 2018-11-12 15:33:57 +0100 | [diff] [blame] | 857 | &priv->tx_mb_reserved->can_ctrl); |
David Jander | 25e9244 | 2014-09-03 16:47:22 +0200 | [diff] [blame] | 858 | |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 859 | return NETDEV_TX_OK; |
| 860 | } |
| 861 | |
Marc Kleine-Budde | 3016475 | 2015-05-10 15:26:58 +0200 | [diff] [blame] | 862 | static void flexcan_irq_bus_err(struct net_device *dev, u32 reg_esr) |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 863 | { |
| 864 | struct flexcan_priv *priv = netdev_priv(dev); |
Oleksij Rempel | d788905f | 2018-09-18 11:40:41 +0200 | [diff] [blame] | 865 | struct flexcan_regs __iomem *regs = priv->regs; |
Marc Kleine-Budde | a5c02f66 | 2017-01-18 11:38:26 +0100 | [diff] [blame] | 866 | struct sk_buff *skb; |
| 867 | struct can_frame *cf; |
Marc Kleine-Budde | d166f56 | 2017-01-17 17:33:46 +0100 | [diff] [blame] | 868 | bool rx_errors = false, tx_errors = false; |
Oleksij Rempel | d788905f | 2018-09-18 11:40:41 +0200 | [diff] [blame] | 869 | u32 timestamp; |
Marc Kleine-Budde | 7581243 | 2019-07-15 20:53:08 +0200 | [diff] [blame] | 870 | int err; |
Oleksij Rempel | d788905f | 2018-09-18 11:40:41 +0200 | [diff] [blame] | 871 | |
| 872 | timestamp = priv->read(®s->timer) << 16; |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 873 | |
Marc Kleine-Budde | a5c02f66 | 2017-01-18 11:38:26 +0100 | [diff] [blame] | 874 | skb = alloc_can_err_skb(dev, &cf); |
| 875 | if (unlikely(!skb)) |
Marc Kleine-Budde | 3016475 | 2015-05-10 15:26:58 +0200 | [diff] [blame] | 876 | return; |
Marc Kleine-Budde | a5c02f66 | 2017-01-18 11:38:26 +0100 | [diff] [blame] | 877 | |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 878 | cf->can_id |= CAN_ERR_PROT | CAN_ERR_BUSERROR; |
| 879 | |
| 880 | if (reg_esr & FLEXCAN_ESR_BIT1_ERR) { |
Wolfgang Grandegger | aabdfd6 | 2012-02-01 11:02:05 +0100 | [diff] [blame] | 881 | netdev_dbg(dev, "BIT1_ERR irq\n"); |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 882 | cf->data[2] |= CAN_ERR_PROT_BIT1; |
Marc Kleine-Budde | d166f56 | 2017-01-17 17:33:46 +0100 | [diff] [blame] | 883 | tx_errors = true; |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 884 | } |
| 885 | if (reg_esr & FLEXCAN_ESR_BIT0_ERR) { |
Wolfgang Grandegger | aabdfd6 | 2012-02-01 11:02:05 +0100 | [diff] [blame] | 886 | netdev_dbg(dev, "BIT0_ERR irq\n"); |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 887 | cf->data[2] |= CAN_ERR_PROT_BIT0; |
Marc Kleine-Budde | d166f56 | 2017-01-17 17:33:46 +0100 | [diff] [blame] | 888 | tx_errors = true; |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 889 | } |
| 890 | if (reg_esr & FLEXCAN_ESR_ACK_ERR) { |
Wolfgang Grandegger | aabdfd6 | 2012-02-01 11:02:05 +0100 | [diff] [blame] | 891 | netdev_dbg(dev, "ACK_ERR irq\n"); |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 892 | cf->can_id |= CAN_ERR_ACK; |
Oliver Hartkopp | ffd461f | 2015-11-21 18:41:20 +0100 | [diff] [blame] | 893 | cf->data[3] = CAN_ERR_PROT_LOC_ACK; |
Marc Kleine-Budde | d166f56 | 2017-01-17 17:33:46 +0100 | [diff] [blame] | 894 | tx_errors = true; |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 895 | } |
| 896 | if (reg_esr & FLEXCAN_ESR_CRC_ERR) { |
Wolfgang Grandegger | aabdfd6 | 2012-02-01 11:02:05 +0100 | [diff] [blame] | 897 | netdev_dbg(dev, "CRC_ERR irq\n"); |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 898 | cf->data[2] |= CAN_ERR_PROT_BIT; |
Oliver Hartkopp | ffd461f | 2015-11-21 18:41:20 +0100 | [diff] [blame] | 899 | cf->data[3] = CAN_ERR_PROT_LOC_CRC_SEQ; |
Marc Kleine-Budde | d166f56 | 2017-01-17 17:33:46 +0100 | [diff] [blame] | 900 | rx_errors = true; |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 901 | } |
| 902 | if (reg_esr & FLEXCAN_ESR_FRM_ERR) { |
Wolfgang Grandegger | aabdfd6 | 2012-02-01 11:02:05 +0100 | [diff] [blame] | 903 | netdev_dbg(dev, "FRM_ERR irq\n"); |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 904 | cf->data[2] |= CAN_ERR_PROT_FORM; |
Marc Kleine-Budde | d166f56 | 2017-01-17 17:33:46 +0100 | [diff] [blame] | 905 | rx_errors = true; |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 906 | } |
| 907 | if (reg_esr & FLEXCAN_ESR_STF_ERR) { |
Wolfgang Grandegger | aabdfd6 | 2012-02-01 11:02:05 +0100 | [diff] [blame] | 908 | netdev_dbg(dev, "STF_ERR irq\n"); |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 909 | cf->data[2] |= CAN_ERR_PROT_STUFF; |
Marc Kleine-Budde | d166f56 | 2017-01-17 17:33:46 +0100 | [diff] [blame] | 910 | rx_errors = true; |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 911 | } |
| 912 | |
| 913 | priv->can.can_stats.bus_error++; |
| 914 | if (rx_errors) |
| 915 | dev->stats.rx_errors++; |
| 916 | if (tx_errors) |
| 917 | dev->stats.tx_errors++; |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 918 | |
Marc Kleine-Budde | 7581243 | 2019-07-15 20:53:08 +0200 | [diff] [blame] | 919 | err = can_rx_offload_queue_sorted(&priv->offload, skb, timestamp); |
| 920 | if (err) |
| 921 | dev->stats.rx_fifo_errors++; |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 922 | } |
| 923 | |
Marc Kleine-Budde | 3016475 | 2015-05-10 15:26:58 +0200 | [diff] [blame] | 924 | static void flexcan_irq_state(struct net_device *dev, u32 reg_esr) |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 925 | { |
| 926 | struct flexcan_priv *priv = netdev_priv(dev); |
Oleksij Rempel | d788905f | 2018-09-18 11:40:41 +0200 | [diff] [blame] | 927 | struct flexcan_regs __iomem *regs = priv->regs; |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 928 | struct sk_buff *skb; |
| 929 | struct can_frame *cf; |
Marc Kleine-Budde | 238443d | 2017-01-18 11:25:41 +0100 | [diff] [blame] | 930 | enum can_state new_state, rx_state, tx_state; |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 931 | int flt; |
Andri Yngvason | 71a3aed | 2014-12-03 17:54:15 +0000 | [diff] [blame] | 932 | struct can_berr_counter bec; |
Oleksij Rempel | d788905f | 2018-09-18 11:40:41 +0200 | [diff] [blame] | 933 | u32 timestamp; |
Marc Kleine-Budde | 7581243 | 2019-07-15 20:53:08 +0200 | [diff] [blame] | 934 | int err; |
Oleksij Rempel | d788905f | 2018-09-18 11:40:41 +0200 | [diff] [blame] | 935 | |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 936 | flt = reg_esr & FLEXCAN_ESR_FLT_CONF_MASK; |
| 937 | if (likely(flt == FLEXCAN_ESR_FLT_CONF_ACTIVE)) { |
Andri Yngvason | 71a3aed | 2014-12-03 17:54:15 +0000 | [diff] [blame] | 938 | tx_state = unlikely(reg_esr & FLEXCAN_ESR_TX_WRN) ? |
Marc Kleine-Budde | 0012e5c | 2015-08-06 14:53:57 +0200 | [diff] [blame] | 939 | CAN_STATE_ERROR_WARNING : CAN_STATE_ERROR_ACTIVE; |
Andri Yngvason | 71a3aed | 2014-12-03 17:54:15 +0000 | [diff] [blame] | 940 | rx_state = unlikely(reg_esr & FLEXCAN_ESR_RX_WRN) ? |
Marc Kleine-Budde | 0012e5c | 2015-08-06 14:53:57 +0200 | [diff] [blame] | 941 | CAN_STATE_ERROR_WARNING : CAN_STATE_ERROR_ACTIVE; |
Andri Yngvason | 71a3aed | 2014-12-03 17:54:15 +0000 | [diff] [blame] | 942 | new_state = max(tx_state, rx_state); |
Andri Yngvason | 258ce80 | 2015-03-17 13:03:09 +0000 | [diff] [blame] | 943 | } else { |
Andri Yngvason | 71a3aed | 2014-12-03 17:54:15 +0000 | [diff] [blame] | 944 | __flexcan_get_berr_counter(dev, &bec); |
Andri Yngvason | 258ce80 | 2015-03-17 13:03:09 +0000 | [diff] [blame] | 945 | new_state = flt == FLEXCAN_ESR_FLT_CONF_PASSIVE ? |
Marc Kleine-Budde | 0012e5c | 2015-08-06 14:53:57 +0200 | [diff] [blame] | 946 | CAN_STATE_ERROR_PASSIVE : CAN_STATE_BUS_OFF; |
Andri Yngvason | 71a3aed | 2014-12-03 17:54:15 +0000 | [diff] [blame] | 947 | rx_state = bec.rxerr >= bec.txerr ? new_state : 0; |
| 948 | tx_state = bec.rxerr <= bec.txerr ? new_state : 0; |
Andri Yngvason | 71a3aed | 2014-12-03 17:54:15 +0000 | [diff] [blame] | 949 | } |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 950 | |
| 951 | /* state hasn't changed */ |
| 952 | if (likely(new_state == priv->can.state)) |
Marc Kleine-Budde | 3016475 | 2015-05-10 15:26:58 +0200 | [diff] [blame] | 953 | return; |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 954 | |
Marc Kleine-Budde | 58ed8e7 | 2019-10-09 15:15:37 +0200 | [diff] [blame] | 955 | timestamp = priv->read(®s->timer) << 16; |
| 956 | |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 957 | skb = alloc_can_err_skb(dev, &cf); |
| 958 | if (unlikely(!skb)) |
Marc Kleine-Budde | 3016475 | 2015-05-10 15:26:58 +0200 | [diff] [blame] | 959 | return; |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 960 | |
Andri Yngvason | 71a3aed | 2014-12-03 17:54:15 +0000 | [diff] [blame] | 961 | can_change_state(dev, cf, tx_state, rx_state); |
| 962 | |
| 963 | if (unlikely(new_state == CAN_STATE_BUS_OFF)) |
| 964 | can_bus_off(dev); |
| 965 | |
Marc Kleine-Budde | 7581243 | 2019-07-15 20:53:08 +0200 | [diff] [blame] | 966 | err = can_rx_offload_queue_sorted(&priv->offload, skb, timestamp); |
| 967 | if (err) |
| 968 | dev->stats.rx_fifo_errors++; |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 969 | } |
| 970 | |
Marc Kleine-Budde | d3a5150 | 2019-03-01 15:38:05 +0100 | [diff] [blame] | 971 | static inline u64 flexcan_read64_mask(struct flexcan_priv *priv, void __iomem *addr, u64 mask) |
| 972 | { |
| 973 | u64 reg = 0; |
| 974 | |
| 975 | if (upper_32_bits(mask)) |
| 976 | reg = (u64)priv->read(addr - 4) << 32; |
| 977 | if (lower_32_bits(mask)) |
| 978 | reg |= priv->read(addr); |
| 979 | |
| 980 | return reg & mask; |
| 981 | } |
| 982 | |
Marc Kleine-Budde | b87c28b7 | 2019-03-01 15:38:05 +0100 | [diff] [blame] | 983 | static inline void flexcan_write64(struct flexcan_priv *priv, u64 val, void __iomem *addr) |
| 984 | { |
| 985 | if (upper_32_bits(val)) |
| 986 | priv->write(upper_32_bits(val), addr - 4); |
| 987 | if (lower_32_bits(val)) |
| 988 | priv->write(lower_32_bits(val), addr); |
| 989 | } |
| 990 | |
Marc Kleine-Budde | d3a5150 | 2019-03-01 15:38:05 +0100 | [diff] [blame] | 991 | static inline u64 flexcan_read_reg_iflag_rx(struct flexcan_priv *priv) |
| 992 | { |
| 993 | return flexcan_read64_mask(priv, &priv->regs->iflag1, priv->rx_mask); |
| 994 | } |
| 995 | |
Marc Kleine-Budde | b87c28b7 | 2019-03-01 15:38:05 +0100 | [diff] [blame] | 996 | static inline u64 flexcan_read_reg_iflag_tx(struct flexcan_priv *priv) |
| 997 | { |
| 998 | return flexcan_read64_mask(priv, &priv->regs->iflag1, priv->tx_mask); |
| 999 | } |
| 1000 | |
Marc Kleine-Budde | 3016475 | 2015-05-10 15:26:58 +0200 | [diff] [blame] | 1001 | static inline struct flexcan_priv *rx_offload_to_priv(struct can_rx_offload *offload) |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 1002 | { |
Marc Kleine-Budde | 3016475 | 2015-05-10 15:26:58 +0200 | [diff] [blame] | 1003 | return container_of(offload, struct flexcan_priv, offload); |
| 1004 | } |
| 1005 | |
Joakim Zhang | 4e9c948 | 2019-07-12 08:02:38 +0000 | [diff] [blame] | 1006 | static struct sk_buff *flexcan_mailbox_read(struct can_rx_offload *offload, |
| 1007 | unsigned int n, u32 *timestamp, |
| 1008 | bool drop) |
Marc Kleine-Budde | 3016475 | 2015-05-10 15:26:58 +0200 | [diff] [blame] | 1009 | { |
| 1010 | struct flexcan_priv *priv = rx_offload_to_priv(offload); |
Marc Kleine-Budde | 89af874 | 2015-05-08 09:32:58 +0200 | [diff] [blame] | 1011 | struct flexcan_regs __iomem *regs = priv->regs; |
Pankaj Bansal | 0517961 | 2018-11-23 22:18:44 +0100 | [diff] [blame] | 1012 | struct flexcan_mb __iomem *mb; |
Joakim Zhang | 4e9c948 | 2019-07-12 08:02:38 +0000 | [diff] [blame] | 1013 | struct sk_buff *skb; |
Joakim Zhang | d9b90b0 | 2019-07-12 08:02:41 +0000 | [diff] [blame] | 1014 | struct canfd_frame *cfd; |
Marc Kleine-Budde | 3016475 | 2015-05-10 15:26:58 +0200 | [diff] [blame] | 1015 | u32 reg_ctrl, reg_id, reg_iflag1; |
Pankaj Bansal | 0517961 | 2018-11-23 22:18:44 +0100 | [diff] [blame] | 1016 | int i; |
| 1017 | |
Joakim Zhang | 4e9c948 | 2019-07-12 08:02:38 +0000 | [diff] [blame] | 1018 | if (unlikely(drop)) { |
| 1019 | skb = ERR_PTR(-ENOBUFS); |
| 1020 | goto mark_as_read; |
| 1021 | } |
| 1022 | |
Pankaj Bansal | 0517961 | 2018-11-23 22:18:44 +0100 | [diff] [blame] | 1023 | mb = flexcan_get_mb(priv, n); |
Marc Kleine-Budde | 3016475 | 2015-05-10 15:26:58 +0200 | [diff] [blame] | 1024 | |
Marc Kleine-Budde | b3cf53e | 2015-09-01 09:00:13 +0200 | [diff] [blame] | 1025 | if (priv->devtype_data->quirks & FLEXCAN_QUIRK_USE_OFF_TIMESTAMP) { |
| 1026 | u32 code; |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 1027 | |
Marc Kleine-Budde | b3cf53e | 2015-09-01 09:00:13 +0200 | [diff] [blame] | 1028 | do { |
Pankaj Bansal | 88462d2 | 2017-11-24 18:52:08 +0530 | [diff] [blame] | 1029 | reg_ctrl = priv->read(&mb->can_ctrl); |
Marc Kleine-Budde | b3cf53e | 2015-09-01 09:00:13 +0200 | [diff] [blame] | 1030 | } while (reg_ctrl & FLEXCAN_MB_CODE_RX_BUSY_BIT); |
| 1031 | |
| 1032 | /* is this MB empty? */ |
| 1033 | code = reg_ctrl & FLEXCAN_MB_CODE_MASK; |
| 1034 | if ((code != FLEXCAN_MB_CODE_RX_FULL) && |
| 1035 | (code != FLEXCAN_MB_CODE_RX_OVERRUN)) |
Joakim Zhang | 4e9c948 | 2019-07-12 08:02:38 +0000 | [diff] [blame] | 1036 | return NULL; |
Marc Kleine-Budde | b3cf53e | 2015-09-01 09:00:13 +0200 | [diff] [blame] | 1037 | |
| 1038 | if (code == FLEXCAN_MB_CODE_RX_OVERRUN) { |
| 1039 | /* This MB was overrun, we lost data */ |
| 1040 | offload->dev->stats.rx_over_errors++; |
| 1041 | offload->dev->stats.rx_errors++; |
| 1042 | } |
| 1043 | } else { |
Pankaj Bansal | 88462d2 | 2017-11-24 18:52:08 +0530 | [diff] [blame] | 1044 | reg_iflag1 = priv->read(®s->iflag1); |
Marc Kleine-Budde | b3cf53e | 2015-09-01 09:00:13 +0200 | [diff] [blame] | 1045 | if (!(reg_iflag1 & FLEXCAN_IFLAG_RX_FIFO_AVAILABLE)) |
Joakim Zhang | 4e9c948 | 2019-07-12 08:02:38 +0000 | [diff] [blame] | 1046 | return NULL; |
Marc Kleine-Budde | b3cf53e | 2015-09-01 09:00:13 +0200 | [diff] [blame] | 1047 | |
Pankaj Bansal | 88462d2 | 2017-11-24 18:52:08 +0530 | [diff] [blame] | 1048 | reg_ctrl = priv->read(&mb->can_ctrl); |
Marc Kleine-Budde | b3cf53e | 2015-09-01 09:00:13 +0200 | [diff] [blame] | 1049 | } |
| 1050 | |
Joakim Zhang | eadf6ca | 2019-07-12 08:02:44 +0000 | [diff] [blame] | 1051 | if (reg_ctrl & FLEXCAN_MB_CNT_EDL) |
| 1052 | skb = alloc_canfd_skb(offload->dev, &cfd); |
| 1053 | else |
| 1054 | skb = alloc_can_skb(offload->dev, (struct can_frame **)&cfd); |
Joakim Zhang | d9b90b0 | 2019-07-12 08:02:41 +0000 | [diff] [blame] | 1055 | if (unlikely(!skb)) { |
Joakim Zhang | 4e9c948 | 2019-07-12 08:02:38 +0000 | [diff] [blame] | 1056 | skb = ERR_PTR(-ENOMEM); |
| 1057 | goto mark_as_read; |
| 1058 | } |
| 1059 | |
Marc Kleine-Budde | 3016475 | 2015-05-10 15:26:58 +0200 | [diff] [blame] | 1060 | /* increase timstamp to full 32 bit */ |
| 1061 | *timestamp = reg_ctrl << 16; |
| 1062 | |
Pankaj Bansal | 88462d2 | 2017-11-24 18:52:08 +0530 | [diff] [blame] | 1063 | reg_id = priv->read(&mb->can_id); |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 1064 | if (reg_ctrl & FLEXCAN_MB_CNT_IDE) |
Joakim Zhang | d9b90b0 | 2019-07-12 08:02:41 +0000 | [diff] [blame] | 1065 | cfd->can_id = ((reg_id >> 0) & CAN_EFF_MASK) | CAN_EFF_FLAG; |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 1066 | else |
Joakim Zhang | d9b90b0 | 2019-07-12 08:02:41 +0000 | [diff] [blame] | 1067 | cfd->can_id = (reg_id >> 18) & CAN_SFF_MASK; |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 1068 | |
Joakim Zhang | eadf6ca | 2019-07-12 08:02:44 +0000 | [diff] [blame] | 1069 | if (reg_ctrl & FLEXCAN_MB_CNT_EDL) { |
Oliver Hartkopp | 3ab4ce0 | 2020-11-10 11:18:49 +0100 | [diff] [blame] | 1070 | cfd->len = can_fd_dlc2len((reg_ctrl >> 16) & 0xf); |
Joakim Zhang | 0542920 | 2019-07-12 08:02:47 +0000 | [diff] [blame] | 1071 | |
| 1072 | if (reg_ctrl & FLEXCAN_MB_CNT_BRS) |
| 1073 | cfd->flags |= CANFD_BRS; |
Joakim Zhang | eadf6ca | 2019-07-12 08:02:44 +0000 | [diff] [blame] | 1074 | } else { |
Oliver Hartkopp | 69d98969 | 2020-11-10 11:18:46 +0100 | [diff] [blame] | 1075 | cfd->len = can_cc_dlc2len((reg_ctrl >> 16) & 0xf); |
Joakim Zhang | eadf6ca | 2019-07-12 08:02:44 +0000 | [diff] [blame] | 1076 | |
| 1077 | if (reg_ctrl & FLEXCAN_MB_CNT_RTR) |
| 1078 | cfd->can_id |= CAN_RTR_FLAG; |
| 1079 | } |
| 1080 | |
| 1081 | if (reg_ctrl & FLEXCAN_MB_CNT_ESI) |
| 1082 | cfd->flags |= CANFD_ESI; |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 1083 | |
Joakim Zhang | d9b90b0 | 2019-07-12 08:02:41 +0000 | [diff] [blame] | 1084 | for (i = 0; i < cfd->len; i += sizeof(u32)) { |
Pankaj Bansal | 0517961 | 2018-11-23 22:18:44 +0100 | [diff] [blame] | 1085 | __be32 data = cpu_to_be32(priv->read(&mb->data[i / sizeof(u32)])); |
Joakim Zhang | d9b90b0 | 2019-07-12 08:02:41 +0000 | [diff] [blame] | 1086 | *(__be32 *)(cfd->data + i) = data; |
Pankaj Bansal | 0517961 | 2018-11-23 22:18:44 +0100 | [diff] [blame] | 1087 | } |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 1088 | |
Joakim Zhang | 4e9c948 | 2019-07-12 08:02:38 +0000 | [diff] [blame] | 1089 | mark_as_read: |
Marc Kleine-Budde | b9468ad | 2019-03-01 16:27:59 +0100 | [diff] [blame] | 1090 | if (priv->devtype_data->quirks & FLEXCAN_QUIRK_USE_OFF_TIMESTAMP) |
| 1091 | flexcan_write64(priv, FLEXCAN_IFLAG_MB(n), ®s->iflag1); |
| 1092 | else |
Pankaj Bansal | 88462d2 | 2017-11-24 18:52:08 +0530 | [diff] [blame] | 1093 | priv->write(FLEXCAN_IFLAG_RX_FIFO_AVAILABLE, ®s->iflag1); |
Fabio Baltieri | adccadb | 2012-12-18 18:50:58 +0100 | [diff] [blame] | 1094 | |
Pankaj Bansal | 5178b7c | 2018-08-01 19:36:46 +0530 | [diff] [blame] | 1095 | /* Read the Free Running Timer. It is optional but recommended |
| 1096 | * to unlock Mailbox as soon as possible and make it available |
| 1097 | * for reception. |
| 1098 | */ |
| 1099 | priv->read(®s->timer); |
| 1100 | |
Joakim Zhang | 4e9c948 | 2019-07-12 08:02:38 +0000 | [diff] [blame] | 1101 | return skb; |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 1102 | } |
| 1103 | |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 1104 | static irqreturn_t flexcan_irq(int irq, void *dev_id) |
| 1105 | { |
| 1106 | struct net_device *dev = dev_id; |
| 1107 | struct net_device_stats *stats = &dev->stats; |
| 1108 | struct flexcan_priv *priv = netdev_priv(dev); |
Marc Kleine-Budde | 89af874 | 2015-05-08 09:32:58 +0200 | [diff] [blame] | 1109 | struct flexcan_regs __iomem *regs = priv->regs; |
Marc Kleine-Budde | dd2f122 | 2017-01-18 11:45:14 +0100 | [diff] [blame] | 1110 | irqreturn_t handled = IRQ_NONE; |
Marc Kleine-Budde | 0ca64f02 | 2019-03-01 13:54:19 +0100 | [diff] [blame] | 1111 | u64 reg_iflag_tx; |
| 1112 | u32 reg_esr; |
ZHU Yi (ST-FIR/ENG1-Zhu) | da49a80 | 2017-09-15 07:03:58 +0000 | [diff] [blame] | 1113 | enum can_state last_state = priv->can.state; |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 1114 | |
Marc Kleine-Budde | 3016475 | 2015-05-10 15:26:58 +0200 | [diff] [blame] | 1115 | /* reception interrupt */ |
Marc Kleine-Budde | b3cf53e | 2015-09-01 09:00:13 +0200 | [diff] [blame] | 1116 | if (priv->devtype_data->quirks & FLEXCAN_QUIRK_USE_OFF_TIMESTAMP) { |
Marc Kleine-Budde | 4e26598 | 2019-03-01 16:29:47 +0100 | [diff] [blame] | 1117 | u64 reg_iflag_rx; |
Marc Kleine-Budde | b3cf53e | 2015-09-01 09:00:13 +0200 | [diff] [blame] | 1118 | int ret; |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 1119 | |
Marc Kleine-Budde | 4e26598 | 2019-03-01 16:29:47 +0100 | [diff] [blame] | 1120 | while ((reg_iflag_rx = flexcan_read_reg_iflag_rx(priv))) { |
Marc Kleine-Budde | b3cf53e | 2015-09-01 09:00:13 +0200 | [diff] [blame] | 1121 | handled = IRQ_HANDLED; |
| 1122 | ret = can_rx_offload_irq_offload_timestamp(&priv->offload, |
Marc Kleine-Budde | 4e26598 | 2019-03-01 16:29:47 +0100 | [diff] [blame] | 1123 | reg_iflag_rx); |
Marc Kleine-Budde | b3cf53e | 2015-09-01 09:00:13 +0200 | [diff] [blame] | 1124 | if (!ret) |
| 1125 | break; |
| 1126 | } |
| 1127 | } else { |
Alexander Stein | cbffaf7 | 2018-10-11 17:01:25 +0200 | [diff] [blame] | 1128 | u32 reg_iflag1; |
| 1129 | |
| 1130 | reg_iflag1 = priv->read(®s->iflag1); |
Marc Kleine-Budde | b3cf53e | 2015-09-01 09:00:13 +0200 | [diff] [blame] | 1131 | if (reg_iflag1 & FLEXCAN_IFLAG_RX_FIFO_AVAILABLE) { |
| 1132 | handled = IRQ_HANDLED; |
| 1133 | can_rx_offload_irq_offload_fifo(&priv->offload); |
| 1134 | } |
| 1135 | |
| 1136 | /* FIFO overflow interrupt */ |
| 1137 | if (reg_iflag1 & FLEXCAN_IFLAG_RX_FIFO_OVERFLOW) { |
| 1138 | handled = IRQ_HANDLED; |
Pankaj Bansal | 88462d2 | 2017-11-24 18:52:08 +0530 | [diff] [blame] | 1139 | priv->write(FLEXCAN_IFLAG_RX_FIFO_OVERFLOW, |
| 1140 | ®s->iflag1); |
Marc Kleine-Budde | b3cf53e | 2015-09-01 09:00:13 +0200 | [diff] [blame] | 1141 | dev->stats.rx_over_errors++; |
| 1142 | dev->stats.rx_errors++; |
| 1143 | } |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 1144 | } |
| 1145 | |
Marc Kleine-Budde | b87c28b7 | 2019-03-01 15:38:05 +0100 | [diff] [blame] | 1146 | reg_iflag_tx = flexcan_read_reg_iflag_tx(priv); |
Alexander Stein | cbffaf7 | 2018-10-11 17:01:25 +0200 | [diff] [blame] | 1147 | |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 1148 | /* transmission complete interrupt */ |
Marc Kleine-Budde | 0ca64f02 | 2019-03-01 13:54:19 +0100 | [diff] [blame] | 1149 | if (reg_iflag_tx & priv->tx_mask) { |
Pankaj Bansal | 0517961 | 2018-11-23 22:18:44 +0100 | [diff] [blame] | 1150 | u32 reg_ctrl = priv->read(&priv->tx_mb->can_ctrl); |
Oleksij Rempel | ed72bc8 | 2018-09-18 11:40:39 +0200 | [diff] [blame] | 1151 | |
Marc Kleine-Budde | dd2f122 | 2017-01-18 11:45:14 +0100 | [diff] [blame] | 1152 | handled = IRQ_HANDLED; |
Marc Kleine-Budde | 99842c9 | 2021-01-11 15:19:29 +0100 | [diff] [blame] | 1153 | stats->tx_bytes += |
| 1154 | can_rx_offload_get_echo_skb(&priv->offload, 0, |
| 1155 | reg_ctrl << 16, NULL); |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 1156 | stats->tx_packets++; |
Fabio Baltieri | adccadb | 2012-12-18 18:50:58 +0100 | [diff] [blame] | 1157 | can_led_event(dev, CAN_LED_EVENT_TX); |
Marc Kleine-Budde | 0012e5c | 2015-08-06 14:53:57 +0200 | [diff] [blame] | 1158 | |
| 1159 | /* after sending a RTR frame MB is in RX mode */ |
Pankaj Bansal | 88462d2 | 2017-11-24 18:52:08 +0530 | [diff] [blame] | 1160 | priv->write(FLEXCAN_MB_CODE_TX_INACTIVE, |
Pankaj Bansal | 0517961 | 2018-11-23 22:18:44 +0100 | [diff] [blame] | 1161 | &priv->tx_mb->can_ctrl); |
Marc Kleine-Budde | b87c28b7 | 2019-03-01 15:38:05 +0100 | [diff] [blame] | 1162 | flexcan_write64(priv, priv->tx_mask, ®s->iflag1); |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 1163 | netif_wake_queue(dev); |
| 1164 | } |
| 1165 | |
Pankaj Bansal | 88462d2 | 2017-11-24 18:52:08 +0530 | [diff] [blame] | 1166 | reg_esr = priv->read(®s->esr); |
Marc Kleine-Budde | 3016475 | 2015-05-10 15:26:58 +0200 | [diff] [blame] | 1167 | |
Joakim Zhang | ab60523 | 2019-12-04 11:36:08 +0000 | [diff] [blame] | 1168 | /* ACK all bus error, state change and wake IRQ sources */ |
| 1169 | if (reg_esr & (FLEXCAN_ESR_ALL_INT | FLEXCAN_ESR_WAK_INT)) { |
Marc Kleine-Budde | dd2f122 | 2017-01-18 11:45:14 +0100 | [diff] [blame] | 1170 | handled = IRQ_HANDLED; |
Joakim Zhang | ab60523 | 2019-12-04 11:36:08 +0000 | [diff] [blame] | 1171 | priv->write(reg_esr & (FLEXCAN_ESR_ALL_INT | FLEXCAN_ESR_WAK_INT), ®s->esr); |
Marc Kleine-Budde | dd2f122 | 2017-01-18 11:45:14 +0100 | [diff] [blame] | 1172 | } |
| 1173 | |
ZHU Yi (ST-FIR/ENG1-Zhu) | ad23023 | 2017-09-15 06:59:15 +0000 | [diff] [blame] | 1174 | /* state change interrupt or broken error state quirk fix is enabled */ |
| 1175 | if ((reg_esr & FLEXCAN_ESR_ERR_STATE) || |
ZHU Yi (ST-FIR/ENG1-Zhu) | da49a80 | 2017-09-15 07:03:58 +0000 | [diff] [blame] | 1176 | (priv->devtype_data->quirks & (FLEXCAN_QUIRK_BROKEN_WERR_STATE | |
Marc Kleine-Budde | bc8ad65 | 2018-11-28 15:45:27 +0100 | [diff] [blame] | 1177 | FLEXCAN_QUIRK_BROKEN_PERR_STATE))) |
Marc Kleine-Budde | 3016475 | 2015-05-10 15:26:58 +0200 | [diff] [blame] | 1178 | flexcan_irq_state(dev, reg_esr); |
| 1179 | |
| 1180 | /* bus error IRQ - handle if bus error reporting is activated */ |
| 1181 | if ((reg_esr & FLEXCAN_ESR_ERR_BUS) && |
| 1182 | (priv->can.ctrlmode & CAN_CTRLMODE_BERR_REPORTING)) |
| 1183 | flexcan_irq_bus_err(dev, reg_esr); |
| 1184 | |
ZHU Yi (ST-FIR/ENG1-Zhu) | da49a80 | 2017-09-15 07:03:58 +0000 | [diff] [blame] | 1185 | /* availability of error interrupt among state transitions in case |
| 1186 | * bus error reporting is de-activated and |
| 1187 | * FLEXCAN_QUIRK_BROKEN_PERR_STATE is enabled: |
| 1188 | * +--------------------------------------------------------------+ |
| 1189 | * | +----------------------------------------------+ [stopped / | |
| 1190 | * | | | sleeping] -+ |
| 1191 | * +-+-> active <-> warning <-> passive -> bus off -+ |
| 1192 | * ___________^^^^^^^^^^^^_______________________________ |
| 1193 | * disabled(1) enabled disabled |
| 1194 | * |
| 1195 | * (1): enabled if FLEXCAN_QUIRK_BROKEN_WERR_STATE is enabled |
| 1196 | */ |
| 1197 | if ((last_state != priv->can.state) && |
| 1198 | (priv->devtype_data->quirks & FLEXCAN_QUIRK_BROKEN_PERR_STATE) && |
| 1199 | !(priv->can.ctrlmode & CAN_CTRLMODE_BERR_REPORTING)) { |
| 1200 | switch (priv->can.state) { |
| 1201 | case CAN_STATE_ERROR_ACTIVE: |
| 1202 | if (priv->devtype_data->quirks & |
| 1203 | FLEXCAN_QUIRK_BROKEN_WERR_STATE) |
| 1204 | flexcan_error_irq_enable(priv); |
| 1205 | else |
| 1206 | flexcan_error_irq_disable(priv); |
| 1207 | break; |
| 1208 | |
| 1209 | case CAN_STATE_ERROR_WARNING: |
| 1210 | flexcan_error_irq_enable(priv); |
| 1211 | break; |
| 1212 | |
| 1213 | case CAN_STATE_ERROR_PASSIVE: |
| 1214 | case CAN_STATE_BUS_OFF: |
| 1215 | flexcan_error_irq_disable(priv); |
| 1216 | break; |
| 1217 | |
| 1218 | default: |
| 1219 | break; |
| 1220 | } |
| 1221 | } |
| 1222 | |
Marc Kleine-Budde | c757096 | 2019-10-09 06:41:08 +0200 | [diff] [blame] | 1223 | if (handled) |
| 1224 | can_rx_offload_irq_finish(&priv->offload); |
| 1225 | |
Marc Kleine-Budde | dd2f122 | 2017-01-18 11:45:14 +0100 | [diff] [blame] | 1226 | return handled; |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 1227 | } |
| 1228 | |
Marc Kleine-Budde | 890599b | 2020-09-22 16:44:22 +0200 | [diff] [blame] | 1229 | static void flexcan_set_bittiming_ctrl(const struct net_device *dev) |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 1230 | { |
| 1231 | const struct flexcan_priv *priv = netdev_priv(dev); |
| 1232 | const struct can_bittiming *bt = &priv->can.bittiming; |
Marc Kleine-Budde | 89af874 | 2015-05-08 09:32:58 +0200 | [diff] [blame] | 1233 | struct flexcan_regs __iomem *regs = priv->regs; |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 1234 | u32 reg; |
| 1235 | |
Pankaj Bansal | 88462d2 | 2017-11-24 18:52:08 +0530 | [diff] [blame] | 1236 | reg = priv->read(®s->ctrl); |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 1237 | reg &= ~(FLEXCAN_CTRL_PRESDIV(0xff) | |
| 1238 | FLEXCAN_CTRL_RJW(0x3) | |
| 1239 | FLEXCAN_CTRL_PSEG1(0x7) | |
| 1240 | FLEXCAN_CTRL_PSEG2(0x7) | |
Marc Kleine-Budde | 890599b | 2020-09-22 16:44:22 +0200 | [diff] [blame] | 1241 | FLEXCAN_CTRL_PROPSEG(0x7)); |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 1242 | |
| 1243 | reg |= FLEXCAN_CTRL_PRESDIV(bt->brp - 1) | |
| 1244 | FLEXCAN_CTRL_PSEG1(bt->phase_seg1 - 1) | |
| 1245 | FLEXCAN_CTRL_PSEG2(bt->phase_seg2 - 1) | |
| 1246 | FLEXCAN_CTRL_RJW(bt->sjw - 1) | |
| 1247 | FLEXCAN_CTRL_PROPSEG(bt->prop_seg - 1); |
| 1248 | |
Marc Kleine-Budde | 890599b | 2020-09-22 16:44:22 +0200 | [diff] [blame] | 1249 | netdev_dbg(dev, "writing ctrl=0x%08x\n", reg); |
| 1250 | priv->write(reg, ®s->ctrl); |
| 1251 | |
| 1252 | /* print chip status */ |
| 1253 | netdev_dbg(dev, "%s: mcr=0x%08x ctrl=0x%08x\n", __func__, |
| 1254 | priv->read(®s->mcr), priv->read(®s->ctrl)); |
| 1255 | } |
| 1256 | |
Joakim Zhang | eadf6ca | 2019-07-12 08:02:44 +0000 | [diff] [blame] | 1257 | static void flexcan_set_bittiming_cbt(const struct net_device *dev) |
| 1258 | { |
| 1259 | struct flexcan_priv *priv = netdev_priv(dev); |
| 1260 | struct can_bittiming *bt = &priv->can.bittiming; |
| 1261 | struct can_bittiming *dbt = &priv->can.data_bittiming; |
| 1262 | struct flexcan_regs __iomem *regs = priv->regs; |
| 1263 | u32 reg_cbt, reg_fdctrl; |
| 1264 | |
| 1265 | /* CBT */ |
| 1266 | /* CBT[EPSEG1] is 5 bit long and CBT[EPROPSEG] is 6 bit |
| 1267 | * long. The can_calc_bittiming() tries to divide the tseg1 |
| 1268 | * equally between phase_seg1 and prop_seg, which may not fit |
| 1269 | * in CBT register. Therefore, if phase_seg1 is more than |
| 1270 | * possible value, increase prop_seg and decrease phase_seg1. |
| 1271 | */ |
| 1272 | if (bt->phase_seg1 > 0x20) { |
| 1273 | bt->prop_seg += (bt->phase_seg1 - 0x20); |
| 1274 | bt->phase_seg1 = 0x20; |
| 1275 | } |
| 1276 | |
| 1277 | reg_cbt = FLEXCAN_CBT_BTF | |
| 1278 | FIELD_PREP(FLEXCAN_CBT_EPRESDIV_MASK, bt->brp - 1) | |
| 1279 | FIELD_PREP(FLEXCAN_CBT_ERJW_MASK, bt->sjw - 1) | |
| 1280 | FIELD_PREP(FLEXCAN_CBT_EPROPSEG_MASK, bt->prop_seg - 1) | |
| 1281 | FIELD_PREP(FLEXCAN_CBT_EPSEG1_MASK, bt->phase_seg1 - 1) | |
| 1282 | FIELD_PREP(FLEXCAN_CBT_EPSEG2_MASK, bt->phase_seg2 - 1); |
| 1283 | |
| 1284 | netdev_dbg(dev, "writing cbt=0x%08x\n", reg_cbt); |
| 1285 | priv->write(reg_cbt, ®s->cbt); |
| 1286 | |
| 1287 | if (priv->can.ctrlmode & CAN_CTRLMODE_FD) { |
Joakim Zhang | ce885a1 | 2019-07-12 08:02:51 +0000 | [diff] [blame] | 1288 | u32 reg_fdcbt, reg_ctrl2; |
Joakim Zhang | eadf6ca | 2019-07-12 08:02:44 +0000 | [diff] [blame] | 1289 | |
| 1290 | if (bt->brp != dbt->brp) |
| 1291 | netdev_warn(dev, "Data brp=%d and brp=%d don't match, this may result in a phase error. Consider using different bitrate and/or data bitrate.\n", |
| 1292 | dbt->brp, bt->brp); |
| 1293 | |
| 1294 | /* FDCBT */ |
| 1295 | /* FDCBT[FPSEG1] is 3 bit long and FDCBT[FPROPSEG] is |
| 1296 | * 5 bit long. The can_calc_bittiming tries to divide |
| 1297 | * the tseg1 equally between phase_seg1 and prop_seg, |
| 1298 | * which may not fit in FDCBT register. Therefore, if |
| 1299 | * phase_seg1 is more than possible value, increase |
| 1300 | * prop_seg and decrease phase_seg1 |
| 1301 | */ |
| 1302 | if (dbt->phase_seg1 > 0x8) { |
| 1303 | dbt->prop_seg += (dbt->phase_seg1 - 0x8); |
| 1304 | dbt->phase_seg1 = 0x8; |
| 1305 | } |
| 1306 | |
Joakim Zhang | ce885a1 | 2019-07-12 08:02:51 +0000 | [diff] [blame] | 1307 | reg_fdcbt = priv->read(®s->fdcbt); |
| 1308 | reg_fdcbt &= ~(FIELD_PREP(FLEXCAN_FDCBT_FPRESDIV_MASK, 0x3ff) | |
| 1309 | FIELD_PREP(FLEXCAN_FDCBT_FRJW_MASK, 0x7) | |
| 1310 | FIELD_PREP(FLEXCAN_FDCBT_FPROPSEG_MASK, 0x1f) | |
| 1311 | FIELD_PREP(FLEXCAN_FDCBT_FPSEG1_MASK, 0x7) | |
| 1312 | FIELD_PREP(FLEXCAN_FDCBT_FPSEG2_MASK, 0x7)); |
| 1313 | |
| 1314 | reg_fdcbt |= FIELD_PREP(FLEXCAN_FDCBT_FPRESDIV_MASK, dbt->brp - 1) | |
Joakim Zhang | eadf6ca | 2019-07-12 08:02:44 +0000 | [diff] [blame] | 1315 | FIELD_PREP(FLEXCAN_FDCBT_FRJW_MASK, dbt->sjw - 1) | |
| 1316 | FIELD_PREP(FLEXCAN_FDCBT_FPROPSEG_MASK, dbt->prop_seg) | |
| 1317 | FIELD_PREP(FLEXCAN_FDCBT_FPSEG1_MASK, dbt->phase_seg1 - 1) | |
| 1318 | FIELD_PREP(FLEXCAN_FDCBT_FPSEG2_MASK, dbt->phase_seg2 - 1); |
| 1319 | |
| 1320 | netdev_dbg(dev, "writing fdcbt=0x%08x\n", reg_fdcbt); |
| 1321 | priv->write(reg_fdcbt, ®s->fdcbt); |
Joakim Zhang | ce885a1 | 2019-07-12 08:02:51 +0000 | [diff] [blame] | 1322 | |
| 1323 | /* CTRL2 */ |
| 1324 | reg_ctrl2 = priv->read(®s->ctrl2); |
| 1325 | reg_ctrl2 &= ~FLEXCAN_CTRL2_ISOCANFDEN; |
| 1326 | if (!(priv->can.ctrlmode & CAN_CTRLMODE_FD_NON_ISO)) |
| 1327 | reg_ctrl2 |= FLEXCAN_CTRL2_ISOCANFDEN; |
| 1328 | |
| 1329 | netdev_dbg(dev, "writing ctrl2=0x%08x\n", reg_ctrl2); |
| 1330 | priv->write(reg_ctrl2, ®s->ctrl2); |
Joakim Zhang | eadf6ca | 2019-07-12 08:02:44 +0000 | [diff] [blame] | 1331 | } |
| 1332 | |
| 1333 | /* FDCTRL */ |
| 1334 | reg_fdctrl = priv->read(®s->fdctrl); |
Joakim Zhang | ef5f631 | 2019-07-12 08:02:56 +0000 | [diff] [blame] | 1335 | reg_fdctrl &= ~(FLEXCAN_FDCTRL_FDRATE | |
| 1336 | FIELD_PREP(FLEXCAN_FDCTRL_TDCOFF, 0x1f)); |
Joakim Zhang | eadf6ca | 2019-07-12 08:02:44 +0000 | [diff] [blame] | 1337 | |
Joakim Zhang | ef5f631 | 2019-07-12 08:02:56 +0000 | [diff] [blame] | 1338 | if (priv->can.ctrlmode & CAN_CTRLMODE_FD) { |
Joakim Zhang | eadf6ca | 2019-07-12 08:02:44 +0000 | [diff] [blame] | 1339 | reg_fdctrl |= FLEXCAN_FDCTRL_FDRATE; |
| 1340 | |
Joakim Zhang | ef5f631 | 2019-07-12 08:02:56 +0000 | [diff] [blame] | 1341 | if (priv->can.ctrlmode & CAN_CTRLMODE_LOOPBACK) { |
| 1342 | /* TDC must be disabled for Loop Back mode */ |
| 1343 | reg_fdctrl &= ~FLEXCAN_FDCTRL_TDCEN; |
| 1344 | } else { |
| 1345 | reg_fdctrl |= FLEXCAN_FDCTRL_TDCEN | |
| 1346 | FIELD_PREP(FLEXCAN_FDCTRL_TDCOFF, |
| 1347 | ((dbt->phase_seg1 - 1) + |
| 1348 | dbt->prop_seg + 2) * |
| 1349 | ((dbt->brp - 1 ) + 1)); |
| 1350 | } |
| 1351 | } |
| 1352 | |
Joakim Zhang | eadf6ca | 2019-07-12 08:02:44 +0000 | [diff] [blame] | 1353 | netdev_dbg(dev, "writing fdctrl=0x%08x\n", reg_fdctrl); |
| 1354 | priv->write(reg_fdctrl, ®s->fdctrl); |
| 1355 | |
Joakim Zhang | ce885a1 | 2019-07-12 08:02:51 +0000 | [diff] [blame] | 1356 | netdev_dbg(dev, "%s: mcr=0x%08x ctrl=0x%08x ctrl2=0x%08x fdctrl=0x%08x cbt=0x%08x fdcbt=0x%08x\n", |
Joakim Zhang | eadf6ca | 2019-07-12 08:02:44 +0000 | [diff] [blame] | 1357 | __func__, |
| 1358 | priv->read(®s->mcr), priv->read(®s->ctrl), |
Joakim Zhang | ce885a1 | 2019-07-12 08:02:51 +0000 | [diff] [blame] | 1359 | priv->read(®s->ctrl2), priv->read(®s->fdctrl), |
| 1360 | priv->read(®s->cbt), priv->read(®s->fdcbt)); |
Joakim Zhang | eadf6ca | 2019-07-12 08:02:44 +0000 | [diff] [blame] | 1361 | } |
| 1362 | |
Marc Kleine-Budde | 890599b | 2020-09-22 16:44:22 +0200 | [diff] [blame] | 1363 | static void flexcan_set_bittiming(struct net_device *dev) |
| 1364 | { |
| 1365 | const struct flexcan_priv *priv = netdev_priv(dev); |
| 1366 | struct flexcan_regs __iomem *regs = priv->regs; |
| 1367 | u32 reg; |
| 1368 | |
| 1369 | reg = priv->read(®s->ctrl); |
| 1370 | reg &= ~(FLEXCAN_CTRL_LPB | FLEXCAN_CTRL_SMP | |
| 1371 | FLEXCAN_CTRL_LOM); |
| 1372 | |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 1373 | if (priv->can.ctrlmode & CAN_CTRLMODE_LOOPBACK) |
| 1374 | reg |= FLEXCAN_CTRL_LPB; |
| 1375 | if (priv->can.ctrlmode & CAN_CTRLMODE_LISTENONLY) |
| 1376 | reg |= FLEXCAN_CTRL_LOM; |
| 1377 | if (priv->can.ctrlmode & CAN_CTRLMODE_3_SAMPLES) |
| 1378 | reg |= FLEXCAN_CTRL_SMP; |
| 1379 | |
Lucas Stach | 7a4b6c8 | 2015-08-07 17:16:03 +0200 | [diff] [blame] | 1380 | netdev_dbg(dev, "writing ctrl=0x%08x\n", reg); |
Pankaj Bansal | 88462d2 | 2017-11-24 18:52:08 +0530 | [diff] [blame] | 1381 | priv->write(reg, ®s->ctrl); |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 1382 | |
Joakim Zhang | eadf6ca | 2019-07-12 08:02:44 +0000 | [diff] [blame] | 1383 | if (priv->can.ctrlmode_supported & CAN_CTRLMODE_FD) |
| 1384 | return flexcan_set_bittiming_cbt(dev); |
| 1385 | else |
| 1386 | return flexcan_set_bittiming_ctrl(dev); |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 1387 | } |
| 1388 | |
Joakim Zhang | a659712 | 2020-09-30 05:15:55 +0800 | [diff] [blame] | 1389 | static void flexcan_ram_init(struct net_device *dev) |
| 1390 | { |
| 1391 | struct flexcan_priv *priv = netdev_priv(dev); |
| 1392 | struct flexcan_regs __iomem *regs = priv->regs; |
| 1393 | u32 reg_ctrl2; |
| 1394 | |
| 1395 | /* 11.8.3.13 Detection and correction of memory errors: |
| 1396 | * CTRL2[WRMFRZ] grants write access to all memory positions |
| 1397 | * that require initialization, ranging from 0x080 to 0xADF |
| 1398 | * and from 0xF28 to 0xFFF when the CAN FD feature is enabled. |
| 1399 | * The RXMGMASK, RX14MASK, RX15MASK, and RXFGMASK registers |
| 1400 | * need to be initialized as well. MCR[RFEN] must not be set |
| 1401 | * during memory initialization. |
| 1402 | */ |
| 1403 | reg_ctrl2 = priv->read(®s->ctrl2); |
| 1404 | reg_ctrl2 |= FLEXCAN_CTRL2_WRMFRZ; |
| 1405 | priv->write(reg_ctrl2, ®s->ctrl2); |
| 1406 | |
Kees Cook | c92a08c | 2021-07-31 17:50:58 -0700 | [diff] [blame] | 1407 | memset_io(®s->init, 0, sizeof(regs->init)); |
Joakim Zhang | a659712 | 2020-09-30 05:15:55 +0800 | [diff] [blame] | 1408 | |
| 1409 | if (priv->can.ctrlmode & CAN_CTRLMODE_FD) |
Kees Cook | c92a08c | 2021-07-31 17:50:58 -0700 | [diff] [blame] | 1410 | memset_io(®s->init_fd, 0, sizeof(regs->init_fd)); |
Joakim Zhang | a659712 | 2020-09-30 05:15:55 +0800 | [diff] [blame] | 1411 | |
| 1412 | reg_ctrl2 &= ~FLEXCAN_CTRL2_WRMFRZ; |
| 1413 | priv->write(reg_ctrl2, ®s->ctrl2); |
| 1414 | } |
| 1415 | |
Marc Kleine-Budde | f3f2a54 | 2020-11-19 11:09:15 +0100 | [diff] [blame] | 1416 | static int flexcan_rx_offload_setup(struct net_device *dev) |
| 1417 | { |
| 1418 | struct flexcan_priv *priv = netdev_priv(dev); |
| 1419 | int err; |
| 1420 | |
| 1421 | if (priv->can.ctrlmode & CAN_CTRLMODE_FD) |
| 1422 | priv->mb_size = sizeof(struct flexcan_mb) + CANFD_MAX_DLEN; |
| 1423 | else |
| 1424 | priv->mb_size = sizeof(struct flexcan_mb) + CAN_MAX_DLEN; |
Angelo Dureghello | d9cead7 | 2021-07-02 11:48:41 +0200 | [diff] [blame] | 1425 | |
| 1426 | if (priv->devtype_data->quirks & FLEXCAN_QUIRK_NR_MB_16) |
| 1427 | priv->mb_count = 16; |
| 1428 | else |
| 1429 | priv->mb_count = (sizeof(priv->regs->mb[0]) / priv->mb_size) + |
| 1430 | (sizeof(priv->regs->mb[1]) / priv->mb_size); |
Marc Kleine-Budde | f3f2a54 | 2020-11-19 11:09:15 +0100 | [diff] [blame] | 1431 | |
| 1432 | if (priv->devtype_data->quirks & FLEXCAN_QUIRK_USE_OFF_TIMESTAMP) |
| 1433 | priv->tx_mb_reserved = |
| 1434 | flexcan_get_mb(priv, FLEXCAN_TX_MB_RESERVED_OFF_TIMESTAMP); |
| 1435 | else |
| 1436 | priv->tx_mb_reserved = |
| 1437 | flexcan_get_mb(priv, FLEXCAN_TX_MB_RESERVED_OFF_FIFO); |
| 1438 | priv->tx_mb_idx = priv->mb_count - 1; |
| 1439 | priv->tx_mb = flexcan_get_mb(priv, priv->tx_mb_idx); |
| 1440 | priv->tx_mask = FLEXCAN_IFLAG_MB(priv->tx_mb_idx); |
| 1441 | |
| 1442 | priv->offload.mailbox_read = flexcan_mailbox_read; |
| 1443 | |
| 1444 | if (priv->devtype_data->quirks & FLEXCAN_QUIRK_USE_OFF_TIMESTAMP) { |
| 1445 | priv->offload.mb_first = FLEXCAN_RX_MB_OFF_TIMESTAMP_FIRST; |
| 1446 | priv->offload.mb_last = priv->mb_count - 2; |
| 1447 | |
| 1448 | priv->rx_mask = GENMASK_ULL(priv->offload.mb_last, |
| 1449 | priv->offload.mb_first); |
| 1450 | err = can_rx_offload_add_timestamp(dev, &priv->offload); |
| 1451 | } else { |
| 1452 | priv->rx_mask = FLEXCAN_IFLAG_RX_FIFO_OVERFLOW | |
| 1453 | FLEXCAN_IFLAG_RX_FIFO_AVAILABLE; |
| 1454 | err = can_rx_offload_add_fifo(dev, &priv->offload, |
| 1455 | FLEXCAN_NAPI_WEIGHT); |
| 1456 | } |
| 1457 | |
| 1458 | return err; |
| 1459 | } |
| 1460 | |
Marc Kleine-Budde | 1c5e6db | 2020-11-19 11:09:13 +0100 | [diff] [blame] | 1461 | static void flexcan_chip_interrupts_enable(const struct net_device *dev) |
| 1462 | { |
| 1463 | const struct flexcan_priv *priv = netdev_priv(dev); |
| 1464 | struct flexcan_regs __iomem *regs = priv->regs; |
| 1465 | u64 reg_imask; |
| 1466 | |
| 1467 | disable_irq(dev->irq); |
| 1468 | priv->write(priv->reg_ctrl_default, ®s->ctrl); |
| 1469 | reg_imask = priv->rx_mask | priv->tx_mask; |
| 1470 | priv->write(upper_32_bits(reg_imask), ®s->imask2); |
| 1471 | priv->write(lower_32_bits(reg_imask), ®s->imask1); |
| 1472 | enable_irq(dev->irq); |
| 1473 | } |
| 1474 | |
| 1475 | static void flexcan_chip_interrupts_disable(const struct net_device *dev) |
| 1476 | { |
| 1477 | const struct flexcan_priv *priv = netdev_priv(dev); |
| 1478 | struct flexcan_regs __iomem *regs = priv->regs; |
| 1479 | |
| 1480 | priv->write(0, ®s->imask2); |
| 1481 | priv->write(0, ®s->imask1); |
| 1482 | priv->write(priv->reg_ctrl_default & ~FLEXCAN_CTRL_ERR_ALL, |
| 1483 | ®s->ctrl); |
| 1484 | } |
| 1485 | |
Marc Kleine-Budde | 0012e5c | 2015-08-06 14:53:57 +0200 | [diff] [blame] | 1486 | /* flexcan_chip_start |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 1487 | * |
| 1488 | * this functions is entered with clocks enabled |
| 1489 | * |
| 1490 | */ |
| 1491 | static int flexcan_chip_start(struct net_device *dev) |
| 1492 | { |
| 1493 | struct flexcan_priv *priv = netdev_priv(dev); |
Marc Kleine-Budde | 89af874 | 2015-05-08 09:32:58 +0200 | [diff] [blame] | 1494 | struct flexcan_regs __iomem *regs = priv->regs; |
Marc Kleine-Budde | 6f75fce | 2014-09-23 11:03:01 +0200 | [diff] [blame] | 1495 | u32 reg_mcr, reg_ctrl, reg_ctrl2, reg_mecr; |
David S. Miller | 1f6d803 | 2014-09-23 12:09:27 -0400 | [diff] [blame] | 1496 | int err, i; |
Pankaj Bansal | 0517961 | 2018-11-23 22:18:44 +0100 | [diff] [blame] | 1497 | struct flexcan_mb __iomem *mb; |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 1498 | |
| 1499 | /* enable module */ |
Marc Kleine-Budde | 9b00b30 | 2014-02-28 15:30:18 +0100 | [diff] [blame] | 1500 | err = flexcan_chip_enable(priv); |
| 1501 | if (err) |
| 1502 | return err; |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 1503 | |
| 1504 | /* soft reset */ |
Marc Kleine-Budde | 4b5b822 | 2014-02-28 15:16:59 +0100 | [diff] [blame] | 1505 | err = flexcan_chip_softreset(priv); |
| 1506 | if (err) |
Marc Kleine-Budde | b1aa1c7 | 2014-02-28 17:08:21 +0100 | [diff] [blame] | 1507 | goto out_chip_disable; |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 1508 | |
Joakim Zhang | a659712 | 2020-09-30 05:15:55 +0800 | [diff] [blame] | 1509 | if (priv->devtype_data->quirks & FLEXCAN_QUIRK_SUPPORT_ECC) |
| 1510 | flexcan_ram_init(dev); |
| 1511 | |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 1512 | flexcan_set_bittiming(dev); |
| 1513 | |
Joakim Zhang | c638200 | 2021-02-18 19:00:37 +0800 | [diff] [blame] | 1514 | /* set freeze, halt */ |
| 1515 | err = flexcan_chip_freeze(priv); |
| 1516 | if (err) |
| 1517 | goto out_chip_disable; |
| 1518 | |
Marc Kleine-Budde | 0012e5c | 2015-08-06 14:53:57 +0200 | [diff] [blame] | 1519 | /* MCR |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 1520 | * |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 1521 | * only supervisor access |
| 1522 | * enable warning int |
Marc Kleine-Budde | 4bd888a | 2015-08-31 21:03:29 +0200 | [diff] [blame] | 1523 | * enable individual RX masking |
Marc Kleine-Budde | 749de6f | 2015-08-31 21:32:34 +0200 | [diff] [blame] | 1524 | * choose format C |
| 1525 | * set max mailbox number |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 1526 | */ |
Pankaj Bansal | 88462d2 | 2017-11-24 18:52:08 +0530 | [diff] [blame] | 1527 | reg_mcr = priv->read(®s->mcr); |
Marc Kleine-Budde | d5a7b40 | 2013-10-04 10:52:36 +0200 | [diff] [blame] | 1528 | reg_mcr &= ~FLEXCAN_MCR_MAXMB(0xff); |
Joakim Zhang | c638200 | 2021-02-18 19:00:37 +0800 | [diff] [blame] | 1529 | reg_mcr |= FLEXCAN_MCR_SUPV | FLEXCAN_MCR_WRN_EN | FLEXCAN_MCR_IRMQ | |
| 1530 | FLEXCAN_MCR_IDAM_C | FLEXCAN_MCR_MAXMB(priv->tx_mb_idx); |
Marc Kleine-Budde | b3cf53e | 2015-09-01 09:00:13 +0200 | [diff] [blame] | 1531 | |
Marc Kleine-Budde | c982a3ca | 2018-08-17 14:52:58 +0200 | [diff] [blame] | 1532 | /* MCR |
| 1533 | * |
| 1534 | * FIFO: |
| 1535 | * - disable for timestamp mode |
| 1536 | * - enable for FIFO mode |
| 1537 | */ |
Alexander Stein | cbffaf7 | 2018-10-11 17:01:25 +0200 | [diff] [blame] | 1538 | if (priv->devtype_data->quirks & FLEXCAN_QUIRK_USE_OFF_TIMESTAMP) |
Marc Kleine-Budde | b3cf53e | 2015-09-01 09:00:13 +0200 | [diff] [blame] | 1539 | reg_mcr &= ~FLEXCAN_MCR_FEN; |
Alexander Stein | cbffaf7 | 2018-10-11 17:01:25 +0200 | [diff] [blame] | 1540 | else |
| 1541 | reg_mcr |= FLEXCAN_MCR_FEN; |
| 1542 | |
Pankaj Bansal | 7ad0f53 | 2018-08-13 23:50:48 +0530 | [diff] [blame] | 1543 | /* MCR |
| 1544 | * |
| 1545 | * NOTE: In loopback mode, the CAN_MCR[SRXDIS] cannot be |
| 1546 | * asserted because this will impede the self reception |
| 1547 | * of a transmitted message. This is not documented in |
| 1548 | * earlier versions of flexcan block guide. |
| 1549 | * |
| 1550 | * Self Reception: |
| 1551 | * - enable Self Reception for loopback mode |
| 1552 | * (by clearing "Self Reception Disable" bit) |
| 1553 | * - disable for normal operation |
| 1554 | */ |
| 1555 | if (priv->can.ctrlmode & CAN_CTRLMODE_LOOPBACK) |
| 1556 | reg_mcr &= ~FLEXCAN_MCR_SRX_DIS; |
| 1557 | else |
| 1558 | reg_mcr |= FLEXCAN_MCR_SRX_DIS; |
| 1559 | |
Joakim Zhang | eadf6ca | 2019-07-12 08:02:44 +0000 | [diff] [blame] | 1560 | /* MCR - CAN-FD */ |
| 1561 | if (priv->can.ctrlmode & CAN_CTRLMODE_FD) |
| 1562 | reg_mcr |= FLEXCAN_MCR_FDEN; |
| 1563 | else |
| 1564 | reg_mcr &= ~FLEXCAN_MCR_FDEN; |
| 1565 | |
Wolfgang Grandegger | aabdfd6 | 2012-02-01 11:02:05 +0100 | [diff] [blame] | 1566 | netdev_dbg(dev, "%s: writing mcr=0x%08x", __func__, reg_mcr); |
Pankaj Bansal | 88462d2 | 2017-11-24 18:52:08 +0530 | [diff] [blame] | 1567 | priv->write(reg_mcr, ®s->mcr); |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 1568 | |
Marc Kleine-Budde | 0012e5c | 2015-08-06 14:53:57 +0200 | [diff] [blame] | 1569 | /* CTRL |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 1570 | * |
| 1571 | * disable timer sync feature |
| 1572 | * |
| 1573 | * disable auto busoff recovery |
| 1574 | * transmit lowest buffer first |
| 1575 | * |
| 1576 | * enable tx and rx warning interrupt |
| 1577 | * enable bus off interrupt |
| 1578 | * (== FLEXCAN_CTRL_ERR_STATE) |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 1579 | */ |
Pankaj Bansal | 88462d2 | 2017-11-24 18:52:08 +0530 | [diff] [blame] | 1580 | reg_ctrl = priv->read(®s->ctrl); |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 1581 | reg_ctrl &= ~FLEXCAN_CTRL_TSYN; |
| 1582 | reg_ctrl |= FLEXCAN_CTRL_BOFF_REC | FLEXCAN_CTRL_LBUF | |
Wolfgang Grandegger | 4f72e5f | 2012-09-28 03:17:15 +0000 | [diff] [blame] | 1583 | FLEXCAN_CTRL_ERR_STATE; |
Marc Kleine-Budde | 0012e5c | 2015-08-06 14:53:57 +0200 | [diff] [blame] | 1584 | |
| 1585 | /* enable the "error interrupt" (FLEXCAN_CTRL_ERR_MSK), |
Wolfgang Grandegger | 4f72e5f | 2012-09-28 03:17:15 +0000 | [diff] [blame] | 1586 | * on most Flexcan cores, too. Otherwise we don't get |
| 1587 | * any error warning or passive interrupts. |
| 1588 | */ |
ZHU Yi (ST-FIR/ENG1-Zhu) | 2f8639b | 2017-09-15 07:01:23 +0000 | [diff] [blame] | 1589 | if (priv->devtype_data->quirks & FLEXCAN_QUIRK_BROKEN_WERR_STATE || |
Wolfgang Grandegger | 4f72e5f | 2012-09-28 03:17:15 +0000 | [diff] [blame] | 1590 | priv->can.ctrlmode & CAN_CTRLMODE_BERR_REPORTING) |
| 1591 | reg_ctrl |= FLEXCAN_CTRL_ERR_MSK; |
Alexander Stein | bc03a54 | 2014-08-12 10:47:21 +0200 | [diff] [blame] | 1592 | else |
| 1593 | reg_ctrl &= ~FLEXCAN_CTRL_ERR_MSK; |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 1594 | |
| 1595 | /* save for later use */ |
| 1596 | priv->reg_ctrl_default = reg_ctrl; |
Marc Kleine-Budde | 6fa7da2 | 2015-08-27 14:24:48 +0200 | [diff] [blame] | 1597 | /* leave interrupts disabled for now */ |
| 1598 | reg_ctrl &= ~FLEXCAN_CTRL_ERR_ALL; |
Wolfgang Grandegger | aabdfd6 | 2012-02-01 11:02:05 +0100 | [diff] [blame] | 1599 | netdev_dbg(dev, "%s: writing ctrl=0x%08x", __func__, reg_ctrl); |
Pankaj Bansal | 88462d2 | 2017-11-24 18:52:08 +0530 | [diff] [blame] | 1600 | priv->write(reg_ctrl, ®s->ctrl); |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 1601 | |
Marc Kleine-Budde | 9eb7aa8 | 2015-09-01 08:57:55 +0200 | [diff] [blame] | 1602 | if ((priv->devtype_data->quirks & FLEXCAN_QUIRK_ENABLE_EACEN_RRS)) { |
Pankaj Bansal | 88462d2 | 2017-11-24 18:52:08 +0530 | [diff] [blame] | 1603 | reg_ctrl2 = priv->read(®s->ctrl2); |
Marc Kleine-Budde | 9eb7aa8 | 2015-09-01 08:57:55 +0200 | [diff] [blame] | 1604 | reg_ctrl2 |= FLEXCAN_CTRL2_EACEN | FLEXCAN_CTRL2_RRS; |
Pankaj Bansal | 88462d2 | 2017-11-24 18:52:08 +0530 | [diff] [blame] | 1605 | priv->write(reg_ctrl2, ®s->ctrl2); |
Marc Kleine-Budde | 9eb7aa8 | 2015-09-01 08:57:55 +0200 | [diff] [blame] | 1606 | } |
| 1607 | |
Joakim Zhang | eadf6ca | 2019-07-12 08:02:44 +0000 | [diff] [blame] | 1608 | if (priv->can.ctrlmode_supported & CAN_CTRLMODE_FD) { |
| 1609 | u32 reg_fdctrl; |
| 1610 | |
| 1611 | reg_fdctrl = priv->read(®s->fdctrl); |
| 1612 | reg_fdctrl &= ~(FIELD_PREP(FLEXCAN_FDCTRL_MBDSR1, 0x3) | |
| 1613 | FIELD_PREP(FLEXCAN_FDCTRL_MBDSR0, 0x3)); |
| 1614 | |
| 1615 | if (priv->can.ctrlmode & CAN_CTRLMODE_FD) { |
| 1616 | reg_fdctrl |= |
| 1617 | FIELD_PREP(FLEXCAN_FDCTRL_MBDSR1, |
| 1618 | FLEXCAN_FDCTRL_MBDSR_64) | |
| 1619 | FIELD_PREP(FLEXCAN_FDCTRL_MBDSR0, |
| 1620 | FLEXCAN_FDCTRL_MBDSR_64); |
| 1621 | } else { |
| 1622 | reg_fdctrl |= |
| 1623 | FIELD_PREP(FLEXCAN_FDCTRL_MBDSR1, |
| 1624 | FLEXCAN_FDCTRL_MBDSR_8) | |
| 1625 | FIELD_PREP(FLEXCAN_FDCTRL_MBDSR0, |
| 1626 | FLEXCAN_FDCTRL_MBDSR_8); |
| 1627 | } |
| 1628 | |
| 1629 | netdev_dbg(dev, "%s: writing fdctrl=0x%08x", |
| 1630 | __func__, reg_fdctrl); |
| 1631 | priv->write(reg_fdctrl, ®s->fdctrl); |
| 1632 | } |
| 1633 | |
Marc Kleine-Budde | b3cf53e | 2015-09-01 09:00:13 +0200 | [diff] [blame] | 1634 | if (priv->devtype_data->quirks & FLEXCAN_QUIRK_USE_OFF_TIMESTAMP) { |
Alexander Stein | cbffaf7 | 2018-10-11 17:01:25 +0200 | [diff] [blame] | 1635 | for (i = priv->offload.mb_first; i <= priv->offload.mb_last; i++) { |
Pankaj Bansal | 0517961 | 2018-11-23 22:18:44 +0100 | [diff] [blame] | 1636 | mb = flexcan_get_mb(priv, i); |
Pankaj Bansal | 88462d2 | 2017-11-24 18:52:08 +0530 | [diff] [blame] | 1637 | priv->write(FLEXCAN_MB_CODE_RX_EMPTY, |
Pankaj Bansal | 0517961 | 2018-11-23 22:18:44 +0100 | [diff] [blame] | 1638 | &mb->can_ctrl); |
Alexander Stein | cbffaf7 | 2018-10-11 17:01:25 +0200 | [diff] [blame] | 1639 | } |
| 1640 | } else { |
| 1641 | /* clear and invalidate unused mailboxes first */ |
Uwe Kleine-König | a55234d | 2019-01-11 12:20:41 +0100 | [diff] [blame] | 1642 | for (i = FLEXCAN_TX_MB_RESERVED_OFF_FIFO; i < priv->mb_count; i++) { |
Pankaj Bansal | 0517961 | 2018-11-23 22:18:44 +0100 | [diff] [blame] | 1643 | mb = flexcan_get_mb(priv, i); |
Alexander Stein | cbffaf7 | 2018-10-11 17:01:25 +0200 | [diff] [blame] | 1644 | priv->write(FLEXCAN_MB_CODE_RX_INACTIVE, |
Pankaj Bansal | 0517961 | 2018-11-23 22:18:44 +0100 | [diff] [blame] | 1645 | &mb->can_ctrl); |
Alexander Stein | cbffaf7 | 2018-10-11 17:01:25 +0200 | [diff] [blame] | 1646 | } |
Marc Kleine-Budde | b3cf53e | 2015-09-01 09:00:13 +0200 | [diff] [blame] | 1647 | } |
| 1648 | |
David Jander | 25e9244 | 2014-09-03 16:47:22 +0200 | [diff] [blame] | 1649 | /* Errata ERR005829: mark first TX mailbox as INACTIVE */ |
Pankaj Bansal | 88462d2 | 2017-11-24 18:52:08 +0530 | [diff] [blame] | 1650 | priv->write(FLEXCAN_MB_CODE_TX_INACTIVE, |
| 1651 | &priv->tx_mb_reserved->can_ctrl); |
David Jander | 25e9244 | 2014-09-03 16:47:22 +0200 | [diff] [blame] | 1652 | |
Marc Kleine-Budde | c32fe4a | 2014-09-16 12:39:28 +0200 | [diff] [blame] | 1653 | /* mark TX mailbox as INACTIVE */ |
Pankaj Bansal | 88462d2 | 2017-11-24 18:52:08 +0530 | [diff] [blame] | 1654 | priv->write(FLEXCAN_MB_CODE_TX_INACTIVE, |
Pankaj Bansal | 0517961 | 2018-11-23 22:18:44 +0100 | [diff] [blame] | 1655 | &priv->tx_mb->can_ctrl); |
Marc Kleine-Budde | d5a7b40 | 2013-10-04 10:52:36 +0200 | [diff] [blame] | 1656 | |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 1657 | /* acceptance mask/acceptance code (accept everything) */ |
Pankaj Bansal | 88462d2 | 2017-11-24 18:52:08 +0530 | [diff] [blame] | 1658 | priv->write(0x0, ®s->rxgmask); |
| 1659 | priv->write(0x0, ®s->rx14mask); |
| 1660 | priv->write(0x0, ®s->rx15mask); |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 1661 | |
Marc Kleine-Budde | f377bff | 2015-05-08 15:22:36 +0200 | [diff] [blame] | 1662 | if (priv->devtype_data->quirks & FLEXCAN_QUIRK_DISABLE_RXFG) |
Pankaj Bansal | 88462d2 | 2017-11-24 18:52:08 +0530 | [diff] [blame] | 1663 | priv->write(0x0, ®s->rxfgmask); |
Hui Wang | 30c1e67 | 2012-06-28 16:21:35 +0800 | [diff] [blame] | 1664 | |
Marc Kleine-Budde | 4bd888a | 2015-08-31 21:03:29 +0200 | [diff] [blame] | 1665 | /* clear acceptance filters */ |
Pankaj Bansal | 0517961 | 2018-11-23 22:18:44 +0100 | [diff] [blame] | 1666 | for (i = 0; i < priv->mb_count; i++) |
Pankaj Bansal | 88462d2 | 2017-11-24 18:52:08 +0530 | [diff] [blame] | 1667 | priv->write(0, ®s->rximr[i]); |
Marc Kleine-Budde | 4bd888a | 2015-08-31 21:03:29 +0200 | [diff] [blame] | 1668 | |
Joakim Zhang | 15ef207 | 2020-04-16 17:31:25 +0800 | [diff] [blame] | 1669 | /* On Vybrid, disable non-correctable errors interrupt and |
| 1670 | * freeze mode. It still can correct the correctable errors |
| 1671 | * when HW supports ECC. |
| 1672 | * |
| 1673 | * This also works around errata e5295 which generates false |
| 1674 | * positive memory errors and put the device in freeze mode. |
Stefan Agner | cdce844 | 2014-07-15 14:56:21 +0200 | [diff] [blame] | 1675 | */ |
Marc Kleine-Budde | f377bff | 2015-05-08 15:22:36 +0200 | [diff] [blame] | 1676 | if (priv->devtype_data->quirks & FLEXCAN_QUIRK_DISABLE_MECR) { |
Marc Kleine-Budde | 0012e5c | 2015-08-06 14:53:57 +0200 | [diff] [blame] | 1677 | /* Follow the protocol as described in "Detection |
Stefan Agner | cdce844 | 2014-07-15 14:56:21 +0200 | [diff] [blame] | 1678 | * and Correction of Memory Errors" to write to |
Joakim Zhang | 15ef207 | 2020-04-16 17:31:25 +0800 | [diff] [blame] | 1679 | * MECR register (step 1 - 5) |
| 1680 | * |
| 1681 | * 1. By default, CTRL2[ECRWRE] = 0, MECR[ECRWRDIS] = 1 |
| 1682 | * 2. set CTRL2[ECRWRE] |
Stefan Agner | cdce844 | 2014-07-15 14:56:21 +0200 | [diff] [blame] | 1683 | */ |
Pankaj Bansal | 88462d2 | 2017-11-24 18:52:08 +0530 | [diff] [blame] | 1684 | reg_ctrl2 = priv->read(®s->ctrl2); |
Marc Kleine-Budde | 6f75fce | 2014-09-23 11:03:01 +0200 | [diff] [blame] | 1685 | reg_ctrl2 |= FLEXCAN_CTRL2_ECRWRE; |
Pankaj Bansal | 88462d2 | 2017-11-24 18:52:08 +0530 | [diff] [blame] | 1686 | priv->write(reg_ctrl2, ®s->ctrl2); |
Stefan Agner | cdce844 | 2014-07-15 14:56:21 +0200 | [diff] [blame] | 1687 | |
Joakim Zhang | 15ef207 | 2020-04-16 17:31:25 +0800 | [diff] [blame] | 1688 | /* 3. clear MECR[ECRWRDIS] */ |
Pankaj Bansal | 88462d2 | 2017-11-24 18:52:08 +0530 | [diff] [blame] | 1689 | reg_mecr = priv->read(®s->mecr); |
Stefan Agner | cdce844 | 2014-07-15 14:56:21 +0200 | [diff] [blame] | 1690 | reg_mecr &= ~FLEXCAN_MECR_ECRWRDIS; |
Pankaj Bansal | 88462d2 | 2017-11-24 18:52:08 +0530 | [diff] [blame] | 1691 | priv->write(reg_mecr, ®s->mecr); |
Joakim Zhang | 15ef207 | 2020-04-16 17:31:25 +0800 | [diff] [blame] | 1692 | |
| 1693 | /* 4. all writes to MECR must keep MECR[ECRWRDIS] cleared */ |
Stefan Agner | cdce844 | 2014-07-15 14:56:21 +0200 | [diff] [blame] | 1694 | reg_mecr &= ~(FLEXCAN_MECR_NCEFAFRZ | FLEXCAN_MECR_HANCEI_MSK | |
Marc Kleine-Budde | 0012e5c | 2015-08-06 14:53:57 +0200 | [diff] [blame] | 1695 | FLEXCAN_MECR_FANCEI_MSK); |
Pankaj Bansal | 88462d2 | 2017-11-24 18:52:08 +0530 | [diff] [blame] | 1696 | priv->write(reg_mecr, ®s->mecr); |
Joakim Zhang | 15ef207 | 2020-04-16 17:31:25 +0800 | [diff] [blame] | 1697 | |
| 1698 | /* 5. after configuration done, lock MECR by either |
| 1699 | * setting MECR[ECRWRDIS] or clearing CTRL2[ECRWRE] |
| 1700 | */ |
| 1701 | reg_mecr |= FLEXCAN_MECR_ECRWRDIS; |
| 1702 | priv->write(reg_mecr, ®s->mecr); |
| 1703 | |
| 1704 | reg_ctrl2 &= ~FLEXCAN_CTRL2_ECRWRE; |
| 1705 | priv->write(reg_ctrl2, ®s->ctrl2); |
Stefan Agner | cdce844 | 2014-07-15 14:56:21 +0200 | [diff] [blame] | 1706 | } |
| 1707 | |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 1708 | /* synchronize with the can bus */ |
Marc Kleine-Budde | b1aa1c7 | 2014-02-28 17:08:21 +0100 | [diff] [blame] | 1709 | err = flexcan_chip_unfreeze(priv); |
| 1710 | if (err) |
Marc Kleine-Budde | cd9f13c | 2020-11-18 16:01:48 +0100 | [diff] [blame] | 1711 | goto out_chip_disable; |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 1712 | |
| 1713 | priv->can.state = CAN_STATE_ERROR_ACTIVE; |
| 1714 | |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 1715 | /* print chip status */ |
Wolfgang Grandegger | aabdfd6 | 2012-02-01 11:02:05 +0100 | [diff] [blame] | 1716 | netdev_dbg(dev, "%s: reading mcr=0x%08x ctrl=0x%08x\n", __func__, |
Pankaj Bansal | 88462d2 | 2017-11-24 18:52:08 +0530 | [diff] [blame] | 1717 | priv->read(®s->mcr), priv->read(®s->ctrl)); |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 1718 | |
| 1719 | return 0; |
| 1720 | |
Marc Kleine-Budde | b1aa1c7 | 2014-02-28 17:08:21 +0100 | [diff] [blame] | 1721 | out_chip_disable: |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 1722 | flexcan_chip_disable(priv); |
| 1723 | return err; |
| 1724 | } |
| 1725 | |
Joakim Zhang | 9ad02c7 | 2020-09-22 16:44:19 +0200 | [diff] [blame] | 1726 | /* __flexcan_chip_stop |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 1727 | * |
Joakim Zhang | 9ad02c7 | 2020-09-22 16:44:19 +0200 | [diff] [blame] | 1728 | * this function is entered with clocks enabled |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 1729 | */ |
Joakim Zhang | 9ad02c7 | 2020-09-22 16:44:19 +0200 | [diff] [blame] | 1730 | static int __flexcan_chip_stop(struct net_device *dev, bool disable_on_error) |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 1731 | { |
| 1732 | struct flexcan_priv *priv = netdev_priv(dev); |
Joakim Zhang | 9ad02c7 | 2020-09-22 16:44:19 +0200 | [diff] [blame] | 1733 | int err; |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 1734 | |
Marc Kleine-Budde | b1aa1c7 | 2014-02-28 17:08:21 +0100 | [diff] [blame] | 1735 | /* freeze + disable module */ |
Joakim Zhang | 9ad02c7 | 2020-09-22 16:44:19 +0200 | [diff] [blame] | 1736 | err = flexcan_chip_freeze(priv); |
| 1737 | if (err && !disable_on_error) |
| 1738 | return err; |
| 1739 | err = flexcan_chip_disable(priv); |
| 1740 | if (err && !disable_on_error) |
| 1741 | goto out_chip_unfreeze; |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 1742 | |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 1743 | priv->can.state = CAN_STATE_STOPPED; |
Joakim Zhang | 9ad02c7 | 2020-09-22 16:44:19 +0200 | [diff] [blame] | 1744 | |
| 1745 | return 0; |
| 1746 | |
| 1747 | out_chip_unfreeze: |
| 1748 | flexcan_chip_unfreeze(priv); |
| 1749 | |
| 1750 | return err; |
| 1751 | } |
| 1752 | |
| 1753 | static inline int flexcan_chip_stop_disable_on_error(struct net_device *dev) |
| 1754 | { |
| 1755 | return __flexcan_chip_stop(dev, true); |
| 1756 | } |
| 1757 | |
| 1758 | static inline int flexcan_chip_stop(struct net_device *dev) |
| 1759 | { |
| 1760 | return __flexcan_chip_stop(dev, false); |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 1761 | } |
| 1762 | |
| 1763 | static int flexcan_open(struct net_device *dev) |
| 1764 | { |
| 1765 | struct flexcan_priv *priv = netdev_priv(dev); |
| 1766 | int err; |
| 1767 | |
Joakim Zhang | eadf6ca | 2019-07-12 08:02:44 +0000 | [diff] [blame] | 1768 | if ((priv->can.ctrlmode & CAN_CTRLMODE_3_SAMPLES) && |
| 1769 | (priv->can.ctrlmode & CAN_CTRLMODE_FD)) { |
| 1770 | netdev_err(dev, "Three Samples mode and CAN-FD mode can't be used together\n"); |
| 1771 | return -EINVAL; |
| 1772 | } |
| 1773 | |
Aisheng Dong | ca10989 | 2018-11-30 08:53:26 +0000 | [diff] [blame] | 1774 | err = pm_runtime_get_sync(priv->dev); |
Zhang Qilong | b7ee5bc | 2020-11-08 16:30:00 +0800 | [diff] [blame] | 1775 | if (err < 0) { |
| 1776 | pm_runtime_put_noidle(priv->dev); |
Fabio Estevam | aa10181 | 2013-07-22 12:41:40 -0300 | [diff] [blame] | 1777 | return err; |
Zhang Qilong | b7ee5bc | 2020-11-08 16:30:00 +0800 | [diff] [blame] | 1778 | } |
Fabio Estevam | aa10181 | 2013-07-22 12:41:40 -0300 | [diff] [blame] | 1779 | |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 1780 | err = open_candev(dev); |
| 1781 | if (err) |
Aisheng Dong | ca10989 | 2018-11-30 08:53:26 +0000 | [diff] [blame] | 1782 | goto out_runtime_put; |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 1783 | |
Marc Kleine-Budde | cd9f13c | 2020-11-18 16:01:48 +0100 | [diff] [blame] | 1784 | err = flexcan_transceiver_enable(priv); |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 1785 | if (err) |
| 1786 | goto out_close; |
| 1787 | |
Marc Kleine-Budde | 648a34b | 2020-11-19 11:09:16 +0100 | [diff] [blame] | 1788 | err = flexcan_rx_offload_setup(dev); |
Marc Kleine-Budde | cd9f13c | 2020-11-18 16:01:48 +0100 | [diff] [blame] | 1789 | if (err) |
| 1790 | goto out_transceiver_disable; |
| 1791 | |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 1792 | err = flexcan_chip_start(dev); |
| 1793 | if (err) |
Marc Kleine-Budde | 648a34b | 2020-11-19 11:09:16 +0100 | [diff] [blame] | 1794 | goto out_can_rx_offload_del; |
| 1795 | |
| 1796 | can_rx_offload_enable(&priv->offload); |
| 1797 | |
| 1798 | err = request_irq(dev->irq, flexcan_irq, IRQF_SHARED, dev->name, dev); |
| 1799 | if (err) |
| 1800 | goto out_can_rx_offload_disable; |
Fabio Baltieri | adccadb | 2012-12-18 18:50:58 +0100 | [diff] [blame] | 1801 | |
Angelo Dureghello | d9cead7 | 2021-07-02 11:48:41 +0200 | [diff] [blame] | 1802 | if (priv->devtype_data->quirks & FLEXCAN_QUIRK_NR_IRQ_3) { |
| 1803 | err = request_irq(priv->irq_boff, |
| 1804 | flexcan_irq, IRQF_SHARED, dev->name, dev); |
| 1805 | if (err) |
| 1806 | goto out_free_irq; |
| 1807 | |
| 1808 | err = request_irq(priv->irq_err, |
| 1809 | flexcan_irq, IRQF_SHARED, dev->name, dev); |
| 1810 | if (err) |
| 1811 | goto out_free_irq_boff; |
| 1812 | } |
| 1813 | |
Marc Kleine-Budde | 49dea04 | 2020-11-19 11:09:14 +0100 | [diff] [blame] | 1814 | flexcan_chip_interrupts_enable(dev); |
| 1815 | |
Fabio Baltieri | adccadb | 2012-12-18 18:50:58 +0100 | [diff] [blame] | 1816 | can_led_event(dev, CAN_LED_EVENT_OPEN); |
| 1817 | |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 1818 | netif_start_queue(dev); |
| 1819 | |
| 1820 | return 0; |
| 1821 | |
Angelo Dureghello | d9cead7 | 2021-07-02 11:48:41 +0200 | [diff] [blame] | 1822 | out_free_irq_boff: |
| 1823 | free_irq(priv->irq_boff, dev); |
| 1824 | out_free_irq: |
| 1825 | free_irq(dev->irq, dev); |
Marc Kleine-Budde | 648a34b | 2020-11-19 11:09:16 +0100 | [diff] [blame] | 1826 | out_can_rx_offload_disable: |
| 1827 | can_rx_offload_disable(&priv->offload); |
| 1828 | flexcan_chip_stop(dev); |
| 1829 | out_can_rx_offload_del: |
Pankaj Bansal | 5156c7b | 2018-08-13 23:50:49 +0530 | [diff] [blame] | 1830 | can_rx_offload_del(&priv->offload); |
Marc Kleine-Budde | cd9f13c | 2020-11-18 16:01:48 +0100 | [diff] [blame] | 1831 | out_transceiver_disable: |
| 1832 | flexcan_transceiver_disable(priv); |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 1833 | out_close: |
| 1834 | close_candev(dev); |
Aisheng Dong | ca10989 | 2018-11-30 08:53:26 +0000 | [diff] [blame] | 1835 | out_runtime_put: |
| 1836 | pm_runtime_put(priv->dev); |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 1837 | |
| 1838 | return err; |
| 1839 | } |
| 1840 | |
| 1841 | static int flexcan_close(struct net_device *dev) |
| 1842 | { |
| 1843 | struct flexcan_priv *priv = netdev_priv(dev); |
| 1844 | |
| 1845 | netif_stop_queue(dev); |
Marc Kleine-Budde | 6b6e986 | 2020-11-19 11:09:17 +0100 | [diff] [blame] | 1846 | flexcan_chip_interrupts_disable(dev); |
Angelo Dureghello | d9cead7 | 2021-07-02 11:48:41 +0200 | [diff] [blame] | 1847 | |
| 1848 | if (priv->devtype_data->quirks & FLEXCAN_QUIRK_NR_IRQ_3) { |
| 1849 | free_irq(priv->irq_err, dev); |
| 1850 | free_irq(priv->irq_boff, dev); |
| 1851 | } |
| 1852 | |
Marc Kleine-Budde | 6b6e986 | 2020-11-19 11:09:17 +0100 | [diff] [blame] | 1853 | free_irq(dev->irq, dev); |
Marc Kleine-Budde | 3016475 | 2015-05-10 15:26:58 +0200 | [diff] [blame] | 1854 | can_rx_offload_disable(&priv->offload); |
Joakim Zhang | 9ad02c7 | 2020-09-22 16:44:19 +0200 | [diff] [blame] | 1855 | flexcan_chip_stop_disable_on_error(dev); |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 1856 | |
Pankaj Bansal | 5156c7b | 2018-08-13 23:50:49 +0530 | [diff] [blame] | 1857 | can_rx_offload_del(&priv->offload); |
Marc Kleine-Budde | cd9f13c | 2020-11-18 16:01:48 +0100 | [diff] [blame] | 1858 | flexcan_transceiver_disable(priv); |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 1859 | close_candev(dev); |
Marc Kleine-Budde | 6b6e986 | 2020-11-19 11:09:17 +0100 | [diff] [blame] | 1860 | |
Aisheng Dong | ca10989 | 2018-11-30 08:53:26 +0000 | [diff] [blame] | 1861 | pm_runtime_put(priv->dev); |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 1862 | |
Fabio Baltieri | adccadb | 2012-12-18 18:50:58 +0100 | [diff] [blame] | 1863 | can_led_event(dev, CAN_LED_EVENT_STOP); |
| 1864 | |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 1865 | return 0; |
| 1866 | } |
| 1867 | |
| 1868 | static int flexcan_set_mode(struct net_device *dev, enum can_mode mode) |
| 1869 | { |
| 1870 | int err; |
| 1871 | |
| 1872 | switch (mode) { |
| 1873 | case CAN_MODE_START: |
| 1874 | err = flexcan_chip_start(dev); |
| 1875 | if (err) |
| 1876 | return err; |
| 1877 | |
Marc Kleine-Budde | 49dea04 | 2020-11-19 11:09:14 +0100 | [diff] [blame] | 1878 | flexcan_chip_interrupts_enable(dev); |
| 1879 | |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 1880 | netif_wake_queue(dev); |
| 1881 | break; |
| 1882 | |
| 1883 | default: |
| 1884 | return -EOPNOTSUPP; |
| 1885 | } |
| 1886 | |
| 1887 | return 0; |
| 1888 | } |
| 1889 | |
| 1890 | static const struct net_device_ops flexcan_netdev_ops = { |
| 1891 | .ndo_open = flexcan_open, |
| 1892 | .ndo_stop = flexcan_close, |
| 1893 | .ndo_start_xmit = flexcan_start_xmit, |
Oliver Hartkopp | c971fa2 | 2014-03-07 09:23:41 +0100 | [diff] [blame] | 1894 | .ndo_change_mtu = can_change_mtu, |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 1895 | }; |
| 1896 | |
Bill Pemberton | 3c8ac0f | 2012-12-03 09:22:44 -0500 | [diff] [blame] | 1897 | static int register_flexcandev(struct net_device *dev) |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 1898 | { |
| 1899 | struct flexcan_priv *priv = netdev_priv(dev); |
Marc Kleine-Budde | 89af874 | 2015-05-08 09:32:58 +0200 | [diff] [blame] | 1900 | struct flexcan_regs __iomem *regs = priv->regs; |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 1901 | u32 reg, err; |
| 1902 | |
Aisheng Dong | ca10989 | 2018-11-30 08:53:26 +0000 | [diff] [blame] | 1903 | err = flexcan_clks_enable(priv); |
Fabio Estevam | aa10181 | 2013-07-22 12:41:40 -0300 | [diff] [blame] | 1904 | if (err) |
| 1905 | return err; |
| 1906 | |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 1907 | /* select "bus clock", chip must be disabled */ |
Marc Kleine-Budde | 9b00b30 | 2014-02-28 15:30:18 +0100 | [diff] [blame] | 1908 | err = flexcan_chip_disable(priv); |
| 1909 | if (err) |
Aisheng Dong | ca10989 | 2018-11-30 08:53:26 +0000 | [diff] [blame] | 1910 | goto out_clks_disable; |
| 1911 | |
Pankaj Bansal | 88462d2 | 2017-11-24 18:52:08 +0530 | [diff] [blame] | 1912 | reg = priv->read(®s->ctrl); |
Dong Aisheng | 8c306be | 2018-12-13 07:08:00 +0000 | [diff] [blame] | 1913 | if (priv->clk_src) |
| 1914 | reg |= FLEXCAN_CTRL_CLK_SRC; |
| 1915 | else |
| 1916 | reg &= ~FLEXCAN_CTRL_CLK_SRC; |
Pankaj Bansal | 88462d2 | 2017-11-24 18:52:08 +0530 | [diff] [blame] | 1917 | priv->write(reg, ®s->ctrl); |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 1918 | |
Marc Kleine-Budde | 9b00b30 | 2014-02-28 15:30:18 +0100 | [diff] [blame] | 1919 | err = flexcan_chip_enable(priv); |
| 1920 | if (err) |
| 1921 | goto out_chip_disable; |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 1922 | |
Joakim Zhang | ec15e27 | 2021-02-18 19:00:36 +0800 | [diff] [blame] | 1923 | /* set freeze, halt */ |
| 1924 | err = flexcan_chip_freeze(priv); |
| 1925 | if (err) |
| 1926 | goto out_chip_disable; |
| 1927 | |
| 1928 | /* activate FIFO, restrict register access */ |
Pankaj Bansal | 88462d2 | 2017-11-24 18:52:08 +0530 | [diff] [blame] | 1929 | reg = priv->read(®s->mcr); |
Joakim Zhang | ec15e27 | 2021-02-18 19:00:36 +0800 | [diff] [blame] | 1930 | reg |= FLEXCAN_MCR_FEN | FLEXCAN_MCR_SUPV; |
Pankaj Bansal | 88462d2 | 2017-11-24 18:52:08 +0530 | [diff] [blame] | 1931 | priv->write(reg, ®s->mcr); |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 1932 | |
Marc Kleine-Budde | 0012e5c | 2015-08-06 14:53:57 +0200 | [diff] [blame] | 1933 | /* Currently we only support newer versions of this core |
Marc Kleine-Budde | b3cf53e | 2015-09-01 09:00:13 +0200 | [diff] [blame] | 1934 | * featuring a RX hardware FIFO (although this driver doesn't |
| 1935 | * make use of it on some cores). Older cores, found on some |
| 1936 | * Coldfire derivates are not tested. |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 1937 | */ |
Pankaj Bansal | 88462d2 | 2017-11-24 18:52:08 +0530 | [diff] [blame] | 1938 | reg = priv->read(®s->mcr); |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 1939 | if (!(reg & FLEXCAN_MCR_FEN)) { |
Wolfgang Grandegger | aabdfd6 | 2012-02-01 11:02:05 +0100 | [diff] [blame] | 1940 | netdev_err(dev, "Could not enable RX FIFO, unsupported core\n"); |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 1941 | err = -ENODEV; |
Marc Kleine-Budde | 9b00b30 | 2014-02-28 15:30:18 +0100 | [diff] [blame] | 1942 | goto out_chip_disable; |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 1943 | } |
| 1944 | |
| 1945 | err = register_candev(dev); |
Aisheng Dong | ca10989 | 2018-11-30 08:53:26 +0000 | [diff] [blame] | 1946 | if (err) |
| 1947 | goto out_chip_disable; |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 1948 | |
Aisheng Dong | ca10989 | 2018-11-30 08:53:26 +0000 | [diff] [blame] | 1949 | /* Disable core and let pm_runtime_put() disable the clocks. |
| 1950 | * If CONFIG_PM is not enabled, the clocks will stay powered. |
| 1951 | */ |
| 1952 | flexcan_chip_disable(priv); |
| 1953 | pm_runtime_put(priv->dev); |
| 1954 | |
| 1955 | return 0; |
| 1956 | |
Marc Kleine-Budde | 9b00b30 | 2014-02-28 15:30:18 +0100 | [diff] [blame] | 1957 | out_chip_disable: |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 1958 | flexcan_chip_disable(priv); |
Aisheng Dong | ca10989 | 2018-11-30 08:53:26 +0000 | [diff] [blame] | 1959 | out_clks_disable: |
| 1960 | flexcan_clks_disable(priv); |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 1961 | return err; |
| 1962 | } |
| 1963 | |
Bill Pemberton | 3c8ac0f | 2012-12-03 09:22:44 -0500 | [diff] [blame] | 1964 | static void unregister_flexcandev(struct net_device *dev) |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 1965 | { |
| 1966 | unregister_candev(dev); |
| 1967 | } |
| 1968 | |
Joakim Zhang | 812f011 | 2020-11-06 18:56:27 +0800 | [diff] [blame] | 1969 | static int flexcan_setup_stop_mode_gpr(struct platform_device *pdev) |
Aisheng Dong | de3578c | 2018-11-23 08:35:33 +0000 | [diff] [blame] | 1970 | { |
| 1971 | struct net_device *dev = platform_get_drvdata(pdev); |
| 1972 | struct device_node *np = pdev->dev.of_node; |
| 1973 | struct device_node *gpr_np; |
| 1974 | struct flexcan_priv *priv; |
| 1975 | phandle phandle; |
Marc Kleine-Budde | d9b081e | 2020-06-14 21:09:20 +0200 | [diff] [blame] | 1976 | u32 out_val[3]; |
Aisheng Dong | de3578c | 2018-11-23 08:35:33 +0000 | [diff] [blame] | 1977 | int ret; |
| 1978 | |
| 1979 | if (!np) |
| 1980 | return -EINVAL; |
| 1981 | |
| 1982 | /* stop mode property format is: |
Marc Kleine-Budde | 499aa92 | 2020-10-14 13:41:36 +0200 | [diff] [blame] | 1983 | * <&gpr req_gpr req_bit>. |
Aisheng Dong | de3578c | 2018-11-23 08:35:33 +0000 | [diff] [blame] | 1984 | */ |
| 1985 | ret = of_property_read_u32_array(np, "fsl,stop-mode", out_val, |
| 1986 | ARRAY_SIZE(out_val)); |
| 1987 | if (ret) { |
| 1988 | dev_dbg(&pdev->dev, "no stop-mode property\n"); |
| 1989 | return ret; |
| 1990 | } |
| 1991 | phandle = *out_val; |
| 1992 | |
| 1993 | gpr_np = of_find_node_by_phandle(phandle); |
| 1994 | if (!gpr_np) { |
| 1995 | dev_dbg(&pdev->dev, "could not find gpr node by phandle\n"); |
YueHaibing | 7873e98 | 2018-12-12 17:24:01 +0800 | [diff] [blame] | 1996 | return -ENODEV; |
Aisheng Dong | de3578c | 2018-11-23 08:35:33 +0000 | [diff] [blame] | 1997 | } |
| 1998 | |
| 1999 | priv = netdev_priv(dev); |
| 2000 | priv->stm.gpr = syscon_node_to_regmap(gpr_np); |
Aisheng Dong | de3578c | 2018-11-23 08:35:33 +0000 | [diff] [blame] | 2001 | if (IS_ERR(priv->stm.gpr)) { |
| 2002 | dev_dbg(&pdev->dev, "could not find gpr regmap\n"); |
Wen Yang | e9f2a85 | 2019-07-06 11:37:20 +0800 | [diff] [blame] | 2003 | ret = PTR_ERR(priv->stm.gpr); |
| 2004 | goto out_put_node; |
Aisheng Dong | de3578c | 2018-11-23 08:35:33 +0000 | [diff] [blame] | 2005 | } |
| 2006 | |
| 2007 | priv->stm.req_gpr = out_val[1]; |
| 2008 | priv->stm.req_bit = out_val[2]; |
Aisheng Dong | de3578c | 2018-11-23 08:35:33 +0000 | [diff] [blame] | 2009 | |
| 2010 | dev_dbg(&pdev->dev, |
Marc Kleine-Budde | d9b081e | 2020-06-14 21:09:20 +0200 | [diff] [blame] | 2011 | "gpr %s req_gpr=0x02%x req_bit=%u\n", |
| 2012 | gpr_np->full_name, priv->stm.req_gpr, priv->stm.req_bit); |
Aisheng Dong | de3578c | 2018-11-23 08:35:33 +0000 | [diff] [blame] | 2013 | |
Aisheng Dong | de3578c | 2018-11-23 08:35:33 +0000 | [diff] [blame] | 2014 | return 0; |
David S. Miller | 13dfb3f | 2019-08-06 18:44:57 -0700 | [diff] [blame] | 2015 | |
Wen Yang | e9f2a85 | 2019-07-06 11:37:20 +0800 | [diff] [blame] | 2016 | out_put_node: |
| 2017 | of_node_put(gpr_np); |
| 2018 | return ret; |
Aisheng Dong | de3578c | 2018-11-23 08:35:33 +0000 | [diff] [blame] | 2019 | } |
| 2020 | |
Joakim Zhang | 812f011 | 2020-11-06 18:56:27 +0800 | [diff] [blame] | 2021 | static int flexcan_setup_stop_mode_scfw(struct platform_device *pdev) |
| 2022 | { |
| 2023 | struct net_device *dev = platform_get_drvdata(pdev); |
| 2024 | struct flexcan_priv *priv; |
| 2025 | u8 scu_idx; |
| 2026 | int ret; |
| 2027 | |
| 2028 | ret = of_property_read_u8(pdev->dev.of_node, "fsl,scu-index", &scu_idx); |
| 2029 | if (ret < 0) { |
| 2030 | dev_dbg(&pdev->dev, "failed to get scu index\n"); |
| 2031 | return ret; |
| 2032 | } |
| 2033 | |
| 2034 | priv = netdev_priv(dev); |
| 2035 | priv->scu_idx = scu_idx; |
| 2036 | |
Marc Kleine-Budde | 02ee680 | 2021-01-27 09:13:03 +0100 | [diff] [blame] | 2037 | /* this function could be deferred probe, return -EPROBE_DEFER */ |
Joakim Zhang | 812f011 | 2020-11-06 18:56:27 +0800 | [diff] [blame] | 2038 | return imx_scu_get_handle(&priv->sc_ipc_handle); |
| 2039 | } |
| 2040 | |
| 2041 | /* flexcan_setup_stop_mode - Setup stop mode for wakeup |
| 2042 | * |
| 2043 | * Return: = 0 setup stop mode successfully or doesn't support this feature |
Marc Kleine-Budde | 02ee680 | 2021-01-27 09:13:03 +0100 | [diff] [blame] | 2044 | * < 0 fail to setup stop mode (could be deferred probe) |
Joakim Zhang | 812f011 | 2020-11-06 18:56:27 +0800 | [diff] [blame] | 2045 | */ |
| 2046 | static int flexcan_setup_stop_mode(struct platform_device *pdev) |
| 2047 | { |
| 2048 | struct net_device *dev = platform_get_drvdata(pdev); |
| 2049 | struct flexcan_priv *priv; |
| 2050 | int ret; |
| 2051 | |
| 2052 | priv = netdev_priv(dev); |
| 2053 | |
| 2054 | if (priv->devtype_data->quirks & FLEXCAN_QUIRK_SETUP_STOP_MODE_SCFW) |
| 2055 | ret = flexcan_setup_stop_mode_scfw(pdev); |
| 2056 | else if (priv->devtype_data->quirks & FLEXCAN_QUIRK_SETUP_STOP_MODE_GPR) |
| 2057 | ret = flexcan_setup_stop_mode_gpr(pdev); |
| 2058 | else |
| 2059 | /* return 0 directly if doesn't support stop mode feature */ |
| 2060 | return 0; |
| 2061 | |
| 2062 | if (ret) |
| 2063 | return ret; |
| 2064 | |
| 2065 | device_set_wakeup_capable(&pdev->dev, true); |
| 2066 | |
| 2067 | if (of_property_read_bool(pdev->dev.of_node, "wakeup-source")) |
| 2068 | device_set_wakeup_enable(&pdev->dev, true); |
| 2069 | |
| 2070 | return 0; |
| 2071 | } |
| 2072 | |
Hui Wang | 30c1e67 | 2012-06-28 16:21:35 +0800 | [diff] [blame] | 2073 | static const struct of_device_id flexcan_of_match[] = { |
Joakim Zhang | 2a1993e | 2019-07-12 08:02:59 +0000 | [diff] [blame] | 2074 | { .compatible = "fsl,imx8qm-flexcan", .data = &fsl_imx8qm_devtype_data, }, |
Joakim Zhang | 3aa2539 | 2020-09-30 05:15:56 +0800 | [diff] [blame] | 2075 | { .compatible = "fsl,imx8mp-flexcan", .data = &fsl_imx8mp_devtype_data, }, |
Hui Wang | 30c1e67 | 2012-06-28 16:21:35 +0800 | [diff] [blame] | 2076 | { .compatible = "fsl,imx6q-flexcan", .data = &fsl_imx6q_devtype_data, }, |
Marc Kleine-Budde | e358784 | 2013-10-03 23:51:55 +0200 | [diff] [blame] | 2077 | { .compatible = "fsl,imx28-flexcan", .data = &fsl_imx28_devtype_data, }, |
Uwe Kleine-König | 0e030a3 | 2018-04-25 16:50:39 +0200 | [diff] [blame] | 2078 | { .compatible = "fsl,imx53-flexcan", .data = &fsl_imx25_devtype_data, }, |
| 2079 | { .compatible = "fsl,imx35-flexcan", .data = &fsl_imx25_devtype_data, }, |
| 2080 | { .compatible = "fsl,imx25-flexcan", .data = &fsl_imx25_devtype_data, }, |
Marc Kleine-Budde | e358784 | 2013-10-03 23:51:55 +0200 | [diff] [blame] | 2081 | { .compatible = "fsl,p1010-flexcan", .data = &fsl_p1010_devtype_data, }, |
Stefan Agner | cdce844 | 2014-07-15 14:56:21 +0200 | [diff] [blame] | 2082 | { .compatible = "fsl,vf610-flexcan", .data = &fsl_vf610_devtype_data, }, |
Pankaj Bansal | 99b7668 | 2017-11-24 18:52:09 +0530 | [diff] [blame] | 2083 | { .compatible = "fsl,ls1021ar2-flexcan", .data = &fsl_ls1021a_r2_devtype_data, }, |
Joakim Zhang | 2c19bb4 | 2019-07-12 08:03:01 +0000 | [diff] [blame] | 2084 | { .compatible = "fsl,lx2160ar1-flexcan", .data = &fsl_lx2160a_r1_devtype_data, }, |
Hui Wang | 30c1e67 | 2012-06-28 16:21:35 +0800 | [diff] [blame] | 2085 | { /* sentinel */ }, |
| 2086 | }; |
Marc Kleine-Budde | 4358a9d | 2012-10-04 10:55:35 +0200 | [diff] [blame] | 2087 | MODULE_DEVICE_TABLE(of, flexcan_of_match); |
Hui Wang | 30c1e67 | 2012-06-28 16:21:35 +0800 | [diff] [blame] | 2088 | |
Angelo Dureghello | d9cead7 | 2021-07-02 11:48:41 +0200 | [diff] [blame] | 2089 | static const struct platform_device_id flexcan_id_table[] = { |
| 2090 | { |
| 2091 | .name = "flexcan-mcf5441x", |
| 2092 | .driver_data = (kernel_ulong_t)&fsl_mcf5441x_devtype_data, |
| 2093 | }, { |
| 2094 | /* sentinel */ |
| 2095 | }, |
| 2096 | }; |
| 2097 | MODULE_DEVICE_TABLE(platform, flexcan_id_table); |
| 2098 | |
Bill Pemberton | 3c8ac0f | 2012-12-03 09:22:44 -0500 | [diff] [blame] | 2099 | static int flexcan_probe(struct platform_device *pdev) |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 2100 | { |
Angelo Dureghello | d9cead7 | 2021-07-02 11:48:41 +0200 | [diff] [blame] | 2101 | const struct of_device_id *of_id; |
Marc Kleine-Budde | dda0b3b | 2012-07-13 14:52:48 +0200 | [diff] [blame] | 2102 | const struct flexcan_devtype_data *devtype_data; |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 2103 | struct net_device *dev; |
| 2104 | struct flexcan_priv *priv; |
Andreas Werner | 555828e | 2015-03-22 17:35:52 +0100 | [diff] [blame] | 2105 | struct regulator *reg_xceiver; |
Steffen Trumtrar | 3d42a37 | 2012-07-17 16:14:34 +0200 | [diff] [blame] | 2106 | struct clk *clk_ipg = NULL, *clk_per = NULL; |
Marc Kleine-Budde | 89af874 | 2015-05-08 09:32:58 +0200 | [diff] [blame] | 2107 | struct flexcan_regs __iomem *regs; |
Angelo Dureghello | d9cead7 | 2021-07-02 11:48:41 +0200 | [diff] [blame] | 2108 | struct flexcan_platform_data *pdata; |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 2109 | int err, irq; |
Dong Aisheng | 8c306be | 2018-12-13 07:08:00 +0000 | [diff] [blame] | 2110 | u8 clk_src = 1; |
holt@sgi.com | 97efe9a | 2011-08-16 17:32:23 +0000 | [diff] [blame] | 2111 | u32 clock_freq = 0; |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 2112 | |
Marc Kleine-Budde | 3d60f33 | 2020-09-22 16:44:16 +0200 | [diff] [blame] | 2113 | reg_xceiver = devm_regulator_get_optional(&pdev->dev, "xceiver"); |
Andreas Werner | 555828e | 2015-03-22 17:35:52 +0100 | [diff] [blame] | 2114 | if (PTR_ERR(reg_xceiver) == -EPROBE_DEFER) |
| 2115 | return -EPROBE_DEFER; |
Marc Kleine-Budde | 3d60f33 | 2020-09-22 16:44:16 +0200 | [diff] [blame] | 2116 | else if (PTR_ERR(reg_xceiver) == -ENODEV) |
Andreas Werner | 555828e | 2015-03-22 17:35:52 +0100 | [diff] [blame] | 2117 | reg_xceiver = NULL; |
Marc Kleine-Budde | 3d60f33 | 2020-09-22 16:44:16 +0200 | [diff] [blame] | 2118 | else if (IS_ERR(reg_xceiver)) |
| 2119 | return PTR_ERR(reg_xceiver); |
Andreas Werner | 555828e | 2015-03-22 17:35:52 +0100 | [diff] [blame] | 2120 | |
Dong Aisheng | 8c306be | 2018-12-13 07:08:00 +0000 | [diff] [blame] | 2121 | if (pdev->dev.of_node) { |
Hui Wang | afc016d | 2012-06-28 16:21:34 +0800 | [diff] [blame] | 2122 | of_property_read_u32(pdev->dev.of_node, |
Marc Kleine-Budde | 0012e5c | 2015-08-06 14:53:57 +0200 | [diff] [blame] | 2123 | "clock-frequency", &clock_freq); |
Dong Aisheng | 8c306be | 2018-12-13 07:08:00 +0000 | [diff] [blame] | 2124 | of_property_read_u8(pdev->dev.of_node, |
| 2125 | "fsl,clk-source", &clk_src); |
Angelo Dureghello | d9cead7 | 2021-07-02 11:48:41 +0200 | [diff] [blame] | 2126 | } else { |
| 2127 | pdata = dev_get_platdata(&pdev->dev); |
| 2128 | if (pdata) { |
| 2129 | clock_freq = pdata->clock_frequency; |
| 2130 | clk_src = pdata->clk_src; |
| 2131 | } |
Dong Aisheng | 8c306be | 2018-12-13 07:08:00 +0000 | [diff] [blame] | 2132 | } |
holt@sgi.com | 97efe9a | 2011-08-16 17:32:23 +0000 | [diff] [blame] | 2133 | |
| 2134 | if (!clock_freq) { |
Steffen Trumtrar | 3d42a37 | 2012-07-17 16:14:34 +0200 | [diff] [blame] | 2135 | clk_ipg = devm_clk_get(&pdev->dev, "ipg"); |
| 2136 | if (IS_ERR(clk_ipg)) { |
| 2137 | dev_err(&pdev->dev, "no ipg clock defined\n"); |
Fabio Estevam | 933e4af | 2013-07-22 12:41:39 -0300 | [diff] [blame] | 2138 | return PTR_ERR(clk_ipg); |
holt@sgi.com | 97efe9a | 2011-08-16 17:32:23 +0000 | [diff] [blame] | 2139 | } |
Steffen Trumtrar | 3d42a37 | 2012-07-17 16:14:34 +0200 | [diff] [blame] | 2140 | |
| 2141 | clk_per = devm_clk_get(&pdev->dev, "per"); |
| 2142 | if (IS_ERR(clk_per)) { |
| 2143 | dev_err(&pdev->dev, "no per clock defined\n"); |
Fabio Estevam | 933e4af | 2013-07-22 12:41:39 -0300 | [diff] [blame] | 2144 | return PTR_ERR(clk_per); |
Steffen Trumtrar | 3d42a37 | 2012-07-17 16:14:34 +0200 | [diff] [blame] | 2145 | } |
Marc Kleine-Budde | 1a3e517 | 2013-11-25 22:15:20 +0100 | [diff] [blame] | 2146 | clock_freq = clk_get_rate(clk_per); |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 2147 | } |
| 2148 | |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 2149 | irq = platform_get_irq(pdev, 0); |
Fabio Estevam | 933e4af | 2013-07-22 12:41:39 -0300 | [diff] [blame] | 2150 | if (irq <= 0) |
| 2151 | return -ENODEV; |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 2152 | |
Joakim Zhang | a4721f2 | 2019-09-29 08:32:09 +0000 | [diff] [blame] | 2153 | regs = devm_platform_ioremap_resource(pdev, 0); |
Marc Kleine-Budde | 89af874 | 2015-05-08 09:32:58 +0200 | [diff] [blame] | 2154 | if (IS_ERR(regs)) |
| 2155 | return PTR_ERR(regs); |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 2156 | |
Angelo Dureghello | d9cead7 | 2021-07-02 11:48:41 +0200 | [diff] [blame] | 2157 | of_id = of_match_device(flexcan_of_match, &pdev->dev); |
| 2158 | if (of_id) |
| 2159 | devtype_data = of_id->data; |
| 2160 | else if (platform_get_device_id(pdev)->driver_data) |
| 2161 | devtype_data = (struct flexcan_devtype_data *) |
| 2162 | platform_get_device_id(pdev)->driver_data; |
| 2163 | else |
| 2164 | return -ENODEV; |
Hui Wang | 30c1e67 | 2012-06-28 16:21:35 +0800 | [diff] [blame] | 2165 | |
Joakim Zhang | eadf6ca | 2019-07-12 08:02:44 +0000 | [diff] [blame] | 2166 | if ((devtype_data->quirks & FLEXCAN_QUIRK_SUPPORT_FD) && |
| 2167 | !(devtype_data->quirks & FLEXCAN_QUIRK_USE_OFF_TIMESTAMP)) { |
| 2168 | dev_err(&pdev->dev, "CAN-FD mode doesn't work with FIFO mode!\n"); |
| 2169 | return -EINVAL; |
| 2170 | } |
| 2171 | |
Fabio Estevam | 933e4af | 2013-07-22 12:41:39 -0300 | [diff] [blame] | 2172 | dev = alloc_candev(sizeof(struct flexcan_priv), 1); |
| 2173 | if (!dev) |
| 2174 | return -ENOMEM; |
| 2175 | |
Marc Kleine-Budde | 3016475 | 2015-05-10 15:26:58 +0200 | [diff] [blame] | 2176 | platform_set_drvdata(pdev, dev); |
| 2177 | SET_NETDEV_DEV(dev, &pdev->dev); |
| 2178 | |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 2179 | dev->netdev_ops = &flexcan_netdev_ops; |
| 2180 | dev->irq = irq; |
Reuben Dowle | 9a12349 | 2011-11-01 11:18:03 +1300 | [diff] [blame] | 2181 | dev->flags |= IFF_ECHO; |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 2182 | |
| 2183 | priv = netdev_priv(dev); |
Pankaj Bansal | 88462d2 | 2017-11-24 18:52:08 +0530 | [diff] [blame] | 2184 | |
Uwe Kleine-König | 0e030a3 | 2018-04-25 16:50:39 +0200 | [diff] [blame] | 2185 | if (of_property_read_bool(pdev->dev.of_node, "big-endian") || |
| 2186 | devtype_data->quirks & FLEXCAN_QUIRK_DEFAULT_BIG_ENDIAN) { |
Pankaj Bansal | 88462d2 | 2017-11-24 18:52:08 +0530 | [diff] [blame] | 2187 | priv->read = flexcan_read_be; |
| 2188 | priv->write = flexcan_write_be; |
| 2189 | } else { |
Uwe Kleine-König | 0e030a3 | 2018-04-25 16:50:39 +0200 | [diff] [blame] | 2190 | priv->read = flexcan_read_le; |
| 2191 | priv->write = flexcan_write_le; |
Pankaj Bansal | 88462d2 | 2017-11-24 18:52:08 +0530 | [diff] [blame] | 2192 | } |
| 2193 | |
Aisheng Dong | ca10989 | 2018-11-30 08:53:26 +0000 | [diff] [blame] | 2194 | priv->dev = &pdev->dev; |
holt@sgi.com | 97efe9a | 2011-08-16 17:32:23 +0000 | [diff] [blame] | 2195 | priv->can.clock.freq = clock_freq; |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 2196 | priv->can.do_set_mode = flexcan_set_mode; |
| 2197 | priv->can.do_get_berr_counter = flexcan_get_berr_counter; |
| 2198 | priv->can.ctrlmode_supported = CAN_CTRLMODE_LOOPBACK | |
| 2199 | CAN_CTRLMODE_LISTENONLY | CAN_CTRLMODE_3_SAMPLES | |
| 2200 | CAN_CTRLMODE_BERR_REPORTING; |
Marc Kleine-Budde | 89af874 | 2015-05-08 09:32:58 +0200 | [diff] [blame] | 2201 | priv->regs = regs; |
Steffen Trumtrar | 3d42a37 | 2012-07-17 16:14:34 +0200 | [diff] [blame] | 2202 | priv->clk_ipg = clk_ipg; |
| 2203 | priv->clk_per = clk_per; |
Dong Aisheng | 8c306be | 2018-12-13 07:08:00 +0000 | [diff] [blame] | 2204 | priv->clk_src = clk_src; |
Hui Wang | 30c1e67 | 2012-06-28 16:21:35 +0800 | [diff] [blame] | 2205 | priv->devtype_data = devtype_data; |
Andreas Werner | 555828e | 2015-03-22 17:35:52 +0100 | [diff] [blame] | 2206 | priv->reg_xceiver = reg_xceiver; |
Fabio Estevam | b7c4114 | 2013-06-10 23:12:57 -0300 | [diff] [blame] | 2207 | |
Angelo Dureghello | d9cead7 | 2021-07-02 11:48:41 +0200 | [diff] [blame] | 2208 | if (devtype_data->quirks & FLEXCAN_QUIRK_NR_IRQ_3) { |
| 2209 | priv->irq_boff = platform_get_irq(pdev, 1); |
| 2210 | if (priv->irq_boff <= 0) { |
| 2211 | err = -ENODEV; |
| 2212 | goto failed_platform_get_irq; |
| 2213 | } |
| 2214 | priv->irq_err = platform_get_irq(pdev, 2); |
| 2215 | if (priv->irq_err <= 0) { |
| 2216 | err = -ENODEV; |
| 2217 | goto failed_platform_get_irq; |
| 2218 | } |
| 2219 | } |
| 2220 | |
Joakim Zhang | eadf6ca | 2019-07-12 08:02:44 +0000 | [diff] [blame] | 2221 | if (priv->devtype_data->quirks & FLEXCAN_QUIRK_SUPPORT_FD) { |
Joakim Zhang | ce885a1 | 2019-07-12 08:02:51 +0000 | [diff] [blame] | 2222 | priv->can.ctrlmode_supported |= CAN_CTRLMODE_FD | |
| 2223 | CAN_CTRLMODE_FD_NON_ISO; |
Joakim Zhang | eadf6ca | 2019-07-12 08:02:44 +0000 | [diff] [blame] | 2224 | priv->can.bittiming_const = &flexcan_fd_bittiming_const; |
| 2225 | priv->can.data_bittiming_const = |
| 2226 | &flexcan_fd_data_bittiming_const; |
| 2227 | } else { |
| 2228 | priv->can.bittiming_const = &flexcan_bittiming_const; |
| 2229 | } |
| 2230 | |
Aisheng Dong | ca10989 | 2018-11-30 08:53:26 +0000 | [diff] [blame] | 2231 | pm_runtime_get_noresume(&pdev->dev); |
| 2232 | pm_runtime_set_active(&pdev->dev); |
| 2233 | pm_runtime_enable(&pdev->dev); |
| 2234 | |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 2235 | err = register_flexcandev(dev); |
| 2236 | if (err) { |
| 2237 | dev_err(&pdev->dev, "registering netdev failed\n"); |
| 2238 | goto failed_register; |
| 2239 | } |
| 2240 | |
Joakim Zhang | 812f011 | 2020-11-06 18:56:27 +0800 | [diff] [blame] | 2241 | err = flexcan_setup_stop_mode(pdev); |
| 2242 | if (err < 0) { |
| 2243 | if (err != -EPROBE_DEFER) |
| 2244 | dev_err(&pdev->dev, "setup stop mode failed\n"); |
| 2245 | goto failed_setup_stop_mode; |
| 2246 | } |
| 2247 | |
Joakim Zhang | ee97302 | 2019-10-30 06:45:57 +0000 | [diff] [blame] | 2248 | of_can_transceiver(dev); |
Fabio Baltieri | adccadb | 2012-12-18 18:50:58 +0100 | [diff] [blame] | 2249 | devm_can_led_init(dev); |
| 2250 | |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 2251 | return 0; |
| 2252 | |
Joakim Zhang | 812f011 | 2020-11-06 18:56:27 +0800 | [diff] [blame] | 2253 | failed_setup_stop_mode: |
| 2254 | unregister_flexcandev(dev); |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 2255 | failed_register: |
Joakim Zhang | 5a9323f | 2020-09-30 05:15:57 +0800 | [diff] [blame] | 2256 | pm_runtime_put_noidle(&pdev->dev); |
| 2257 | pm_runtime_disable(&pdev->dev); |
Angelo Dureghello | d9cead7 | 2021-07-02 11:48:41 +0200 | [diff] [blame] | 2258 | failed_platform_get_irq: |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 2259 | free_candev(dev); |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 2260 | return err; |
| 2261 | } |
| 2262 | |
Bill Pemberton | 3c8ac0f | 2012-12-03 09:22:44 -0500 | [diff] [blame] | 2263 | static int flexcan_remove(struct platform_device *pdev) |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 2264 | { |
| 2265 | struct net_device *dev = platform_get_drvdata(pdev); |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 2266 | |
Joakim Zhang | ab07ff1 | 2020-10-21 02:45:27 +0800 | [diff] [blame] | 2267 | device_set_wakeup_enable(&pdev->dev, false); |
| 2268 | device_set_wakeup_capable(&pdev->dev, false); |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 2269 | unregister_flexcandev(dev); |
Aisheng Dong | ca10989 | 2018-11-30 08:53:26 +0000 | [diff] [blame] | 2270 | pm_runtime_disable(&pdev->dev); |
Marc Kleine-Budde | 9a27586 | 2010-10-21 05:07:58 +0000 | [diff] [blame] | 2271 | free_candev(dev); |
| 2272 | |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 2273 | return 0; |
| 2274 | } |
| 2275 | |
Marc Kleine-Budde | 08c6d35 | 2014-03-05 19:10:44 +0100 | [diff] [blame] | 2276 | static int __maybe_unused flexcan_suspend(struct device *device) |
Eric Bénard | 8b5e218 | 2012-05-08 17:12:17 +0200 | [diff] [blame] | 2277 | { |
Fabio Estevam | 588e7a8 | 2013-05-20 15:43:43 -0300 | [diff] [blame] | 2278 | struct net_device *dev = dev_get_drvdata(device); |
Eric Bénard | 8b5e218 | 2012-05-08 17:12:17 +0200 | [diff] [blame] | 2279 | struct flexcan_priv *priv = netdev_priv(dev); |
Joakim Zhang | 1434d04 | 2019-12-04 11:36:19 +0000 | [diff] [blame] | 2280 | int err; |
Eric Bénard | 8b5e218 | 2012-05-08 17:12:17 +0200 | [diff] [blame] | 2281 | |
Eric Bénard | 8b5e218 | 2012-05-08 17:12:17 +0200 | [diff] [blame] | 2282 | if (netif_running(dev)) { |
Aisheng Dong | de3578c | 2018-11-23 08:35:33 +0000 | [diff] [blame] | 2283 | /* if wakeup is enabled, enter stop mode |
| 2284 | * else enter disabled mode. |
| 2285 | */ |
| 2286 | if (device_may_wakeup(device)) { |
| 2287 | enable_irq_wake(dev->irq); |
Joakim Zhang | 5f186c2 | 2019-07-02 01:45:41 +0000 | [diff] [blame] | 2288 | err = flexcan_enter_stop_mode(priv); |
| 2289 | if (err) |
| 2290 | return err; |
Aisheng Dong | de3578c | 2018-11-23 08:35:33 +0000 | [diff] [blame] | 2291 | } else { |
Joakim Zhang | 1434d04 | 2019-12-04 11:36:19 +0000 | [diff] [blame] | 2292 | err = flexcan_chip_stop(dev); |
| 2293 | if (err) |
| 2294 | return err; |
| 2295 | |
Marc Kleine-Budde | 49dea04 | 2020-11-19 11:09:14 +0100 | [diff] [blame] | 2296 | flexcan_chip_interrupts_disable(dev); |
| 2297 | |
Joakim Zhang | 1434d04 | 2019-12-04 11:36:19 +0000 | [diff] [blame] | 2298 | err = pinctrl_pm_select_sleep_state(device); |
Aisheng Dong | de3578c | 2018-11-23 08:35:33 +0000 | [diff] [blame] | 2299 | if (err) |
| 2300 | return err; |
| 2301 | } |
Eric Bénard | 8b5e218 | 2012-05-08 17:12:17 +0200 | [diff] [blame] | 2302 | netif_stop_queue(dev); |
| 2303 | netif_device_detach(dev); |
| 2304 | } |
| 2305 | priv->can.state = CAN_STATE_SLEEPING; |
| 2306 | |
Joakim Zhang | 1434d04 | 2019-12-04 11:36:19 +0000 | [diff] [blame] | 2307 | return 0; |
Eric Bénard | 8b5e218 | 2012-05-08 17:12:17 +0200 | [diff] [blame] | 2308 | } |
| 2309 | |
Marc Kleine-Budde | 08c6d35 | 2014-03-05 19:10:44 +0100 | [diff] [blame] | 2310 | static int __maybe_unused flexcan_resume(struct device *device) |
Eric Bénard | 8b5e218 | 2012-05-08 17:12:17 +0200 | [diff] [blame] | 2311 | { |
Fabio Estevam | 588e7a8 | 2013-05-20 15:43:43 -0300 | [diff] [blame] | 2312 | struct net_device *dev = dev_get_drvdata(device); |
Eric Bénard | 8b5e218 | 2012-05-08 17:12:17 +0200 | [diff] [blame] | 2313 | struct flexcan_priv *priv = netdev_priv(dev); |
Joakim Zhang | 1434d04 | 2019-12-04 11:36:19 +0000 | [diff] [blame] | 2314 | int err; |
Eric Bénard | 8b5e218 | 2012-05-08 17:12:17 +0200 | [diff] [blame] | 2315 | |
| 2316 | priv->can.state = CAN_STATE_ERROR_ACTIVE; |
| 2317 | if (netif_running(dev)) { |
| 2318 | netif_device_attach(dev); |
| 2319 | netif_start_queue(dev); |
Aisheng Dong | de3578c | 2018-11-23 08:35:33 +0000 | [diff] [blame] | 2320 | if (device_may_wakeup(device)) { |
| 2321 | disable_irq_wake(dev->irq); |
Sean Nyekjaer | e707180 | 2019-12-04 11:36:06 +0000 | [diff] [blame] | 2322 | err = flexcan_exit_stop_mode(priv); |
| 2323 | if (err) |
| 2324 | return err; |
Aisheng Dong | de3578c | 2018-11-23 08:35:33 +0000 | [diff] [blame] | 2325 | } else { |
Joakim Zhang | 1434d04 | 2019-12-04 11:36:19 +0000 | [diff] [blame] | 2326 | err = pinctrl_pm_select_default_state(device); |
| 2327 | if (err) |
| 2328 | return err; |
| 2329 | |
| 2330 | err = flexcan_chip_start(dev); |
| 2331 | if (err) |
| 2332 | return err; |
Marc Kleine-Budde | 49dea04 | 2020-11-19 11:09:14 +0100 | [diff] [blame] | 2333 | |
| 2334 | flexcan_chip_interrupts_enable(dev); |
Aisheng Dong | de3578c | 2018-11-23 08:35:33 +0000 | [diff] [blame] | 2335 | } |
Eric Bénard | 8b5e218 | 2012-05-08 17:12:17 +0200 | [diff] [blame] | 2336 | } |
Aisheng Dong | ca10989 | 2018-11-30 08:53:26 +0000 | [diff] [blame] | 2337 | |
Joakim Zhang | 1434d04 | 2019-12-04 11:36:19 +0000 | [diff] [blame] | 2338 | return 0; |
Aisheng Dong | ca10989 | 2018-11-30 08:53:26 +0000 | [diff] [blame] | 2339 | } |
| 2340 | |
| 2341 | static int __maybe_unused flexcan_runtime_suspend(struct device *device) |
| 2342 | { |
| 2343 | struct net_device *dev = dev_get_drvdata(device); |
| 2344 | struct flexcan_priv *priv = netdev_priv(dev); |
| 2345 | |
| 2346 | flexcan_clks_disable(priv); |
| 2347 | |
Fabio Estevam | 4de349e | 2016-08-17 12:41:08 -0300 | [diff] [blame] | 2348 | return 0; |
Eric Bénard | 8b5e218 | 2012-05-08 17:12:17 +0200 | [diff] [blame] | 2349 | } |
Fabio Estevam | 588e7a8 | 2013-05-20 15:43:43 -0300 | [diff] [blame] | 2350 | |
Aisheng Dong | ca10989 | 2018-11-30 08:53:26 +0000 | [diff] [blame] | 2351 | static int __maybe_unused flexcan_runtime_resume(struct device *device) |
| 2352 | { |
| 2353 | struct net_device *dev = dev_get_drvdata(device); |
| 2354 | struct flexcan_priv *priv = netdev_priv(dev); |
| 2355 | |
| 2356 | return flexcan_clks_enable(priv); |
| 2357 | } |
| 2358 | |
Aisheng Dong | de3578c | 2018-11-23 08:35:33 +0000 | [diff] [blame] | 2359 | static int __maybe_unused flexcan_noirq_suspend(struct device *device) |
| 2360 | { |
| 2361 | struct net_device *dev = dev_get_drvdata(device); |
| 2362 | struct flexcan_priv *priv = netdev_priv(dev); |
| 2363 | |
Joakim Zhang | 02f71c6 | 2019-12-10 09:00:13 +0000 | [diff] [blame] | 2364 | if (netif_running(dev)) { |
| 2365 | int err; |
| 2366 | |
| 2367 | if (device_may_wakeup(device)) |
| 2368 | flexcan_enable_wakeup_irq(priv, true); |
| 2369 | |
| 2370 | err = pm_runtime_force_suspend(device); |
| 2371 | if (err) |
| 2372 | return err; |
| 2373 | } |
Aisheng Dong | de3578c | 2018-11-23 08:35:33 +0000 | [diff] [blame] | 2374 | |
| 2375 | return 0; |
| 2376 | } |
| 2377 | |
| 2378 | static int __maybe_unused flexcan_noirq_resume(struct device *device) |
| 2379 | { |
| 2380 | struct net_device *dev = dev_get_drvdata(device); |
| 2381 | struct flexcan_priv *priv = netdev_priv(dev); |
| 2382 | |
Joakim Zhang | 02f71c6 | 2019-12-10 09:00:13 +0000 | [diff] [blame] | 2383 | if (netif_running(dev)) { |
| 2384 | int err; |
| 2385 | |
| 2386 | err = pm_runtime_force_resume(device); |
| 2387 | if (err) |
| 2388 | return err; |
| 2389 | |
| 2390 | if (device_may_wakeup(device)) |
| 2391 | flexcan_enable_wakeup_irq(priv, false); |
| 2392 | } |
Aisheng Dong | de3578c | 2018-11-23 08:35:33 +0000 | [diff] [blame] | 2393 | |
| 2394 | return 0; |
| 2395 | } |
| 2396 | |
| 2397 | static const struct dev_pm_ops flexcan_pm_ops = { |
| 2398 | SET_SYSTEM_SLEEP_PM_OPS(flexcan_suspend, flexcan_resume) |
Aisheng Dong | ca10989 | 2018-11-30 08:53:26 +0000 | [diff] [blame] | 2399 | SET_RUNTIME_PM_OPS(flexcan_runtime_suspend, flexcan_runtime_resume, NULL) |
Aisheng Dong | de3578c | 2018-11-23 08:35:33 +0000 | [diff] [blame] | 2400 | SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(flexcan_noirq_suspend, flexcan_noirq_resume) |
| 2401 | }; |
Eric Bénard | 8b5e218 | 2012-05-08 17:12:17 +0200 | [diff] [blame] | 2402 | |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 2403 | static struct platform_driver flexcan_driver = { |
holt@sgi.com | c8aef4c | 2011-08-16 17:32:22 +0000 | [diff] [blame] | 2404 | .driver = { |
| 2405 | .name = DRV_NAME, |
Fabio Estevam | 588e7a8 | 2013-05-20 15:43:43 -0300 | [diff] [blame] | 2406 | .pm = &flexcan_pm_ops, |
holt@sgi.com | c8aef4c | 2011-08-16 17:32:22 +0000 | [diff] [blame] | 2407 | .of_match_table = flexcan_of_match, |
| 2408 | }, |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 2409 | .probe = flexcan_probe, |
Bill Pemberton | 3c8ac0f | 2012-12-03 09:22:44 -0500 | [diff] [blame] | 2410 | .remove = flexcan_remove, |
Angelo Dureghello | d9cead7 | 2021-07-02 11:48:41 +0200 | [diff] [blame] | 2411 | .id_table = flexcan_id_table, |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 2412 | }; |
| 2413 | |
Axel Lin | 871d337 | 2011-11-27 15:42:31 +0000 | [diff] [blame] | 2414 | module_platform_driver(flexcan_driver); |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 2415 | |
| 2416 | MODULE_AUTHOR("Sascha Hauer <kernel@pengutronix.de>, " |
| 2417 | "Marc Kleine-Budde <kernel@pengutronix.de>"); |
| 2418 | MODULE_LICENSE("GPL v2"); |
| 2419 | MODULE_DESCRIPTION("CAN port driver for flexcan based chip"); |