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