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 | |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 12 | #include <linux/can.h> |
| 13 | #include <linux/can/dev.h> |
| 14 | #include <linux/can/error.h> |
Fabio Baltieri | adccadb | 2012-12-18 18:50:58 +0100 | [diff] [blame] | 15 | #include <linux/can/led.h> |
Marc Kleine-Budde | 3016475 | 2015-05-10 15:26:58 +0200 | [diff] [blame] | 16 | #include <linux/can/rx-offload.h> |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 17 | #include <linux/clk.h> |
| 18 | #include <linux/delay.h> |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 19 | #include <linux/interrupt.h> |
| 20 | #include <linux/io.h> |
Aisheng Dong | de3578c | 2018-11-23 08:35:33 +0000 | [diff] [blame] | 21 | #include <linux/mfd/syscon.h> |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 22 | #include <linux/module.h> |
Marc Kleine-Budde | 555f6e5 | 2020-09-22 16:44:10 +0200 | [diff] [blame] | 23 | #include <linux/netdevice.h> |
holt@sgi.com | 97efe9a | 2011-08-16 17:32:23 +0000 | [diff] [blame] | 24 | #include <linux/of.h> |
Hui Wang | 30c1e67 | 2012-06-28 16:21:35 +0800 | [diff] [blame] | 25 | #include <linux/of_device.h> |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 26 | #include <linux/platform_device.h> |
Aisheng Dong | ca10989 | 2018-11-30 08:53:26 +0000 | [diff] [blame] | 27 | #include <linux/pm_runtime.h> |
Aisheng Dong | de3578c | 2018-11-23 08:35:33 +0000 | [diff] [blame] | 28 | #include <linux/regmap.h> |
Marc Kleine-Budde | 555f6e5 | 2020-09-22 16:44:10 +0200 | [diff] [blame] | 29 | #include <linux/regulator/consumer.h> |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 30 | |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 31 | #define DRV_NAME "flexcan" |
| 32 | |
| 33 | /* 8 for RX fifo and 2 error handling */ |
| 34 | #define FLEXCAN_NAPI_WEIGHT (8 + 2) |
| 35 | |
| 36 | /* FLEXCAN module configuration register (CANMCR) bits */ |
| 37 | #define FLEXCAN_MCR_MDIS BIT(31) |
| 38 | #define FLEXCAN_MCR_FRZ BIT(30) |
| 39 | #define FLEXCAN_MCR_FEN BIT(29) |
| 40 | #define FLEXCAN_MCR_HALT BIT(28) |
| 41 | #define FLEXCAN_MCR_NOT_RDY BIT(27) |
| 42 | #define FLEXCAN_MCR_WAK_MSK BIT(26) |
| 43 | #define FLEXCAN_MCR_SOFTRST BIT(25) |
| 44 | #define FLEXCAN_MCR_FRZ_ACK BIT(24) |
| 45 | #define FLEXCAN_MCR_SUPV BIT(23) |
| 46 | #define FLEXCAN_MCR_SLF_WAK BIT(22) |
| 47 | #define FLEXCAN_MCR_WRN_EN BIT(21) |
| 48 | #define FLEXCAN_MCR_LPM_ACK BIT(20) |
| 49 | #define FLEXCAN_MCR_WAK_SRC BIT(19) |
| 50 | #define FLEXCAN_MCR_DOZE BIT(18) |
| 51 | #define FLEXCAN_MCR_SRX_DIS BIT(17) |
Marc Kleine-Budde | 62d1086 | 2015-08-27 16:01:27 +0200 | [diff] [blame] | 52 | #define FLEXCAN_MCR_IRMQ BIT(16) |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 53 | #define FLEXCAN_MCR_LPRIO_EN BIT(13) |
| 54 | #define FLEXCAN_MCR_AEN BIT(12) |
Marc Kleine-Budde | b3cf53e | 2015-09-01 09:00:13 +0200 | [diff] [blame] | 55 | /* MCR_MAXMB: maximum used MBs is MAXMB + 1 */ |
Marc Kleine-Budde | 4c728d8 | 2014-09-02 16:54:17 +0200 | [diff] [blame] | 56 | #define FLEXCAN_MCR_MAXMB(x) ((x) & 0x7f) |
Marc Kleine-Budde | 0012e5c | 2015-08-06 14:53:57 +0200 | [diff] [blame] | 57 | #define FLEXCAN_MCR_IDAM_A (0x0 << 8) |
| 58 | #define FLEXCAN_MCR_IDAM_B (0x1 << 8) |
| 59 | #define FLEXCAN_MCR_IDAM_C (0x2 << 8) |
| 60 | #define FLEXCAN_MCR_IDAM_D (0x3 << 8) |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 61 | |
| 62 | /* FLEXCAN control register (CANCTRL) bits */ |
| 63 | #define FLEXCAN_CTRL_PRESDIV(x) (((x) & 0xff) << 24) |
| 64 | #define FLEXCAN_CTRL_RJW(x) (((x) & 0x03) << 22) |
| 65 | #define FLEXCAN_CTRL_PSEG1(x) (((x) & 0x07) << 19) |
| 66 | #define FLEXCAN_CTRL_PSEG2(x) (((x) & 0x07) << 16) |
| 67 | #define FLEXCAN_CTRL_BOFF_MSK BIT(15) |
| 68 | #define FLEXCAN_CTRL_ERR_MSK BIT(14) |
| 69 | #define FLEXCAN_CTRL_CLK_SRC BIT(13) |
| 70 | #define FLEXCAN_CTRL_LPB BIT(12) |
| 71 | #define FLEXCAN_CTRL_TWRN_MSK BIT(11) |
| 72 | #define FLEXCAN_CTRL_RWRN_MSK BIT(10) |
| 73 | #define FLEXCAN_CTRL_SMP BIT(7) |
| 74 | #define FLEXCAN_CTRL_BOFF_REC BIT(6) |
| 75 | #define FLEXCAN_CTRL_TSYN BIT(5) |
| 76 | #define FLEXCAN_CTRL_LBUF BIT(4) |
| 77 | #define FLEXCAN_CTRL_LOM BIT(3) |
| 78 | #define FLEXCAN_CTRL_PROPSEG(x) ((x) & 0x07) |
| 79 | #define FLEXCAN_CTRL_ERR_BUS (FLEXCAN_CTRL_ERR_MSK) |
| 80 | #define FLEXCAN_CTRL_ERR_STATE \ |
| 81 | (FLEXCAN_CTRL_TWRN_MSK | FLEXCAN_CTRL_RWRN_MSK | \ |
| 82 | FLEXCAN_CTRL_BOFF_MSK) |
| 83 | #define FLEXCAN_CTRL_ERR_ALL \ |
| 84 | (FLEXCAN_CTRL_ERR_BUS | FLEXCAN_CTRL_ERR_STATE) |
| 85 | |
Stefan Agner | cdce844 | 2014-07-15 14:56:21 +0200 | [diff] [blame] | 86 | /* FLEXCAN control register 2 (CTRL2) bits */ |
Marc Kleine-Budde | 6f75fce | 2014-09-23 11:03:01 +0200 | [diff] [blame] | 87 | #define FLEXCAN_CTRL2_ECRWRE BIT(29) |
| 88 | #define FLEXCAN_CTRL2_WRMFRZ BIT(28) |
| 89 | #define FLEXCAN_CTRL2_RFFN(x) (((x) & 0x0f) << 24) |
| 90 | #define FLEXCAN_CTRL2_TASD(x) (((x) & 0x1f) << 19) |
| 91 | #define FLEXCAN_CTRL2_MRP BIT(18) |
| 92 | #define FLEXCAN_CTRL2_RRS BIT(17) |
| 93 | #define FLEXCAN_CTRL2_EACEN BIT(16) |
Stefan Agner | cdce844 | 2014-07-15 14:56:21 +0200 | [diff] [blame] | 94 | |
| 95 | /* FLEXCAN memory error control register (MECR) bits */ |
| 96 | #define FLEXCAN_MECR_ECRWRDIS BIT(31) |
| 97 | #define FLEXCAN_MECR_HANCEI_MSK BIT(19) |
| 98 | #define FLEXCAN_MECR_FANCEI_MSK BIT(18) |
| 99 | #define FLEXCAN_MECR_CEI_MSK BIT(16) |
| 100 | #define FLEXCAN_MECR_HAERRIE BIT(15) |
| 101 | #define FLEXCAN_MECR_FAERRIE BIT(14) |
| 102 | #define FLEXCAN_MECR_EXTERRIE BIT(13) |
| 103 | #define FLEXCAN_MECR_RERRDIS BIT(9) |
| 104 | #define FLEXCAN_MECR_ECCDIS BIT(8) |
| 105 | #define FLEXCAN_MECR_NCEFAFRZ BIT(7) |
| 106 | |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 107 | /* FLEXCAN error and status register (ESR) bits */ |
| 108 | #define FLEXCAN_ESR_TWRN_INT BIT(17) |
| 109 | #define FLEXCAN_ESR_RWRN_INT BIT(16) |
| 110 | #define FLEXCAN_ESR_BIT1_ERR BIT(15) |
| 111 | #define FLEXCAN_ESR_BIT0_ERR BIT(14) |
| 112 | #define FLEXCAN_ESR_ACK_ERR BIT(13) |
| 113 | #define FLEXCAN_ESR_CRC_ERR BIT(12) |
| 114 | #define FLEXCAN_ESR_FRM_ERR BIT(11) |
| 115 | #define FLEXCAN_ESR_STF_ERR BIT(10) |
| 116 | #define FLEXCAN_ESR_TX_WRN BIT(9) |
| 117 | #define FLEXCAN_ESR_RX_WRN BIT(8) |
| 118 | #define FLEXCAN_ESR_IDLE BIT(7) |
| 119 | #define FLEXCAN_ESR_TXRX BIT(6) |
| 120 | #define FLEXCAN_EST_FLT_CONF_SHIFT (4) |
| 121 | #define FLEXCAN_ESR_FLT_CONF_MASK (0x3 << FLEXCAN_EST_FLT_CONF_SHIFT) |
| 122 | #define FLEXCAN_ESR_FLT_CONF_ACTIVE (0x0 << FLEXCAN_EST_FLT_CONF_SHIFT) |
| 123 | #define FLEXCAN_ESR_FLT_CONF_PASSIVE (0x1 << FLEXCAN_EST_FLT_CONF_SHIFT) |
| 124 | #define FLEXCAN_ESR_BOFF_INT BIT(2) |
| 125 | #define FLEXCAN_ESR_ERR_INT BIT(1) |
| 126 | #define FLEXCAN_ESR_WAK_INT BIT(0) |
| 127 | #define FLEXCAN_ESR_ERR_BUS \ |
| 128 | (FLEXCAN_ESR_BIT1_ERR | FLEXCAN_ESR_BIT0_ERR | \ |
| 129 | FLEXCAN_ESR_ACK_ERR | FLEXCAN_ESR_CRC_ERR | \ |
| 130 | FLEXCAN_ESR_FRM_ERR | FLEXCAN_ESR_STF_ERR) |
| 131 | #define FLEXCAN_ESR_ERR_STATE \ |
| 132 | (FLEXCAN_ESR_TWRN_INT | FLEXCAN_ESR_RWRN_INT | FLEXCAN_ESR_BOFF_INT) |
| 133 | #define FLEXCAN_ESR_ERR_ALL \ |
| 134 | (FLEXCAN_ESR_ERR_BUS | FLEXCAN_ESR_ERR_STATE) |
Wolfgang Grandegger | 6e9d554 | 2011-12-12 16:09:28 +0100 | [diff] [blame] | 135 | #define FLEXCAN_ESR_ALL_INT \ |
| 136 | (FLEXCAN_ESR_TWRN_INT | FLEXCAN_ESR_RWRN_INT | \ |
Joakim Zhang | ab60523 | 2019-12-04 11:36:08 +0000 | [diff] [blame] | 137 | FLEXCAN_ESR_BOFF_INT | FLEXCAN_ESR_ERR_INT) |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 138 | |
| 139 | /* FLEXCAN interrupt flag register (IFLAG) bits */ |
David Jander | 25e9244 | 2014-09-03 16:47:22 +0200 | [diff] [blame] | 140 | /* Errata ERR005829 step7: Reserve first valid MB */ |
Alexander Stein | cbffaf7 | 2018-10-11 17:01:25 +0200 | [diff] [blame] | 141 | #define FLEXCAN_TX_MB_RESERVED_OFF_FIFO 8 |
Marc Kleine-Budde | b3cf53e | 2015-09-01 09:00:13 +0200 | [diff] [blame] | 142 | #define FLEXCAN_TX_MB_RESERVED_OFF_TIMESTAMP 0 |
Alexander Stein | cbffaf7 | 2018-10-11 17:01:25 +0200 | [diff] [blame] | 143 | #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] | 144 | #define FLEXCAN_IFLAG_MB(x) BIT_ULL(x) |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 145 | #define FLEXCAN_IFLAG_RX_FIFO_OVERFLOW BIT(7) |
| 146 | #define FLEXCAN_IFLAG_RX_FIFO_WARN BIT(6) |
| 147 | #define FLEXCAN_IFLAG_RX_FIFO_AVAILABLE BIT(5) |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 148 | |
| 149 | /* FLEXCAN message buffers */ |
Marc Kleine-Budde | b3cf53e | 2015-09-01 09:00:13 +0200 | [diff] [blame] | 150 | #define FLEXCAN_MB_CODE_MASK (0xf << 24) |
| 151 | #define FLEXCAN_MB_CODE_RX_BUSY_BIT (0x1 << 24) |
Marc Kleine-Budde | c32fe4a | 2014-09-16 12:39:28 +0200 | [diff] [blame] | 152 | #define FLEXCAN_MB_CODE_RX_INACTIVE (0x0 << 24) |
| 153 | #define FLEXCAN_MB_CODE_RX_EMPTY (0x4 << 24) |
| 154 | #define FLEXCAN_MB_CODE_RX_FULL (0x2 << 24) |
Marc Kleine-Budde | 0012e5c | 2015-08-06 14:53:57 +0200 | [diff] [blame] | 155 | #define FLEXCAN_MB_CODE_RX_OVERRUN (0x6 << 24) |
Marc Kleine-Budde | c32fe4a | 2014-09-16 12:39:28 +0200 | [diff] [blame] | 156 | #define FLEXCAN_MB_CODE_RX_RANSWER (0xa << 24) |
| 157 | |
| 158 | #define FLEXCAN_MB_CODE_TX_INACTIVE (0x8 << 24) |
| 159 | #define FLEXCAN_MB_CODE_TX_ABORT (0x9 << 24) |
| 160 | #define FLEXCAN_MB_CODE_TX_DATA (0xc << 24) |
| 161 | #define FLEXCAN_MB_CODE_TX_TANSWER (0xe << 24) |
| 162 | |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 163 | #define FLEXCAN_MB_CNT_SRR BIT(22) |
| 164 | #define FLEXCAN_MB_CNT_IDE BIT(21) |
| 165 | #define FLEXCAN_MB_CNT_RTR BIT(20) |
| 166 | #define FLEXCAN_MB_CNT_LENGTH(x) (((x) & 0xf) << 16) |
| 167 | #define FLEXCAN_MB_CNT_TIMESTAMP(x) ((x) & 0xffff) |
| 168 | |
Joakim Zhang | 247e535 | 2019-01-31 09:37:22 +0000 | [diff] [blame] | 169 | #define FLEXCAN_TIMEOUT_US (250) |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 170 | |
Marc Kleine-Budde | 0012e5c | 2015-08-06 14:53:57 +0200 | [diff] [blame] | 171 | /* FLEXCAN hardware feature flags |
Wolfgang Grandegger | bb698ca | 2012-10-10 21:10:42 +0200 | [diff] [blame] | 172 | * |
| 173 | * Below is some version info we got: |
ZHU Yi (ST-FIR/ENG1-Zhu) | da49a80 | 2017-09-15 07:03:58 +0000 | [diff] [blame] | 174 | * SOC Version IP-Version Glitch- [TR]WRN_INT IRQ Err Memory err RTR re- |
| 175 | * Filter? connected? Passive detection ception in MB |
Marc Kleine-Budde | 658f534 | 2017-11-22 13:01:08 +0100 | [diff] [blame] | 176 | * MX25 FlexCAN2 03.00.00.00 no no no no no |
ZHU Yi (ST-FIR/ENG1-Zhu) | da49a80 | 2017-09-15 07:03:58 +0000 | [diff] [blame] | 177 | * MX28 FlexCAN2 03.00.04.00 yes yes no no no |
Marc Kleine-Budde | 658f534 | 2017-11-22 13:01:08 +0100 | [diff] [blame] | 178 | * MX35 FlexCAN2 03.00.00.00 no no no no no |
ZHU Yi (ST-FIR/ENG1-Zhu) | da49a80 | 2017-09-15 07:03:58 +0000 | [diff] [blame] | 179 | * MX53 FlexCAN2 03.00.00.00 yes no no no no |
| 180 | * MX6s FlexCAN3 10.00.12.00 yes yes no no yes |
Marc Kleine-Budde | 29c64b1 | 2017-11-27 09:18:21 +0100 | [diff] [blame] | 181 | * VF610 FlexCAN3 ? no yes no yes yes? |
Pankaj Bansal | 99b7668 | 2017-11-24 18:52:09 +0530 | [diff] [blame] | 182 | * LS1021A FlexCAN2 03.00.04.00 no yes no no yes |
Wolfgang Grandegger | bb698ca | 2012-10-10 21:10:42 +0200 | [diff] [blame] | 183 | * |
| 184 | * Some SOCs do not have the RX_WARN & TX_WARN interrupt line connected. |
| 185 | */ |
Marc Kleine-Budde | ef4b623 | 2020-09-22 16:44:14 +0200 | [diff] [blame] | 186 | |
| 187 | /* [TR]WRN_INT not connected */ |
| 188 | #define FLEXCAN_QUIRK_BROKEN_WERR_STATE BIT(1) |
| 189 | /* Disable RX FIFO Global mask */ |
| 190 | #define FLEXCAN_QUIRK_DISABLE_RXFG BIT(2) |
| 191 | /* Enable EACEN and RRS bit in ctrl2 */ |
| 192 | #define FLEXCAN_QUIRK_ENABLE_EACEN_RRS BIT(3) |
Joakim Zhang | 15ef207 | 2020-04-16 17:31:25 +0800 | [diff] [blame^] | 193 | /* Disable non-correctable errors interrupt and freeze mode */ |
Marc Kleine-Budde | ef4b623 | 2020-09-22 16:44:14 +0200 | [diff] [blame] | 194 | #define FLEXCAN_QUIRK_DISABLE_MECR BIT(4) |
| 195 | /* Use timestamp based offloading */ |
| 196 | #define FLEXCAN_QUIRK_USE_OFF_TIMESTAMP BIT(5) |
| 197 | /* No interrupt for error passive */ |
| 198 | #define FLEXCAN_QUIRK_BROKEN_PERR_STATE BIT(6) |
| 199 | /* default to BE register access */ |
| 200 | #define FLEXCAN_QUIRK_DEFAULT_BIG_ENDIAN BIT(7) |
| 201 | /* Setup stop mode to support wakeup */ |
| 202 | #define FLEXCAN_QUIRK_SETUP_STOP_MODE BIT(8) |
Wolfgang Grandegger | 4f72e5f | 2012-09-28 03:17:15 +0000 | [diff] [blame] | 203 | |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 204 | /* Structure of the message buffer */ |
| 205 | struct flexcan_mb { |
| 206 | u32 can_ctrl; |
| 207 | u32 can_id; |
Pankaj Bansal | 0517961 | 2018-11-23 22:18:44 +0100 | [diff] [blame] | 208 | u32 data[]; |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 209 | }; |
| 210 | |
| 211 | /* Structure of the hardware registers */ |
| 212 | struct flexcan_regs { |
| 213 | u32 mcr; /* 0x00 */ |
Marc Kleine-Budde | fe63a06 | 2020-09-22 16:44:13 +0200 | [diff] [blame] | 214 | u32 ctrl; /* 0x04 - Not affected by Soft Reset */ |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 215 | u32 timer; /* 0x08 */ |
Marc Kleine-Budde | 4b70287 | 2020-09-22 16:44:12 +0200 | [diff] [blame] | 216 | u32 tcr; /* 0x0c */ |
Marc Kleine-Budde | fe63a06 | 2020-09-22 16:44:13 +0200 | [diff] [blame] | 217 | u32 rxgmask; /* 0x10 - Not affected by Soft Reset */ |
| 218 | u32 rx14mask; /* 0x14 - Not affected by Soft Reset */ |
| 219 | u32 rx15mask; /* 0x18 - Not affected by Soft Reset */ |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 220 | u32 ecr; /* 0x1c */ |
| 221 | u32 esr; /* 0x20 */ |
| 222 | u32 imask2; /* 0x24 */ |
| 223 | u32 imask1; /* 0x28 */ |
| 224 | u32 iflag2; /* 0x2c */ |
| 225 | u32 iflag1; /* 0x30 */ |
Marc Kleine-Budde | 62d1086 | 2015-08-27 16:01:27 +0200 | [diff] [blame] | 226 | union { /* 0x34 */ |
| 227 | u32 gfwr_mx28; /* MX28, MX53 */ |
Marc Kleine-Budde | fe63a06 | 2020-09-22 16:44:13 +0200 | [diff] [blame] | 228 | u32 ctrl2; /* MX6, VF610 - Not affected by Soft Reset */ |
Marc Kleine-Budde | 62d1086 | 2015-08-27 16:01:27 +0200 | [diff] [blame] | 229 | }; |
Hui Wang | 30c1e67 | 2012-06-28 16:21:35 +0800 | [diff] [blame] | 230 | u32 esr2; /* 0x38 */ |
| 231 | u32 imeur; /* 0x3c */ |
| 232 | u32 lrfr; /* 0x40 */ |
| 233 | u32 crcr; /* 0x44 */ |
| 234 | u32 rxfgmask; /* 0x48 */ |
Marc Kleine-Budde | fe63a06 | 2020-09-22 16:44:13 +0200 | [diff] [blame] | 235 | u32 rxfir; /* 0x4c - Not affected by Soft Reset */ |
| 236 | u32 cbt; /* 0x50 - Not affected by Soft Reset */ |
Marc Kleine-Budde | 4b70287 | 2020-09-22 16:44:12 +0200 | [diff] [blame] | 237 | u32 _reserved2; /* 0x54 */ |
| 238 | u32 dbg1; /* 0x58 */ |
| 239 | u32 dbg2; /* 0x5c */ |
| 240 | u32 _reserved3[8]; /* 0x60 */ |
Marc Kleine-Budde | fe63a06 | 2020-09-22 16:44:13 +0200 | [diff] [blame] | 241 | u8 mb[2][512]; /* 0x80 - Not affected by Soft Reset */ |
Marc Kleine-Budde | 66a6ef0 | 2014-09-17 12:50:48 +0200 | [diff] [blame] | 242 | /* FIFO-mode: |
| 243 | * MB |
| 244 | * 0x080...0x08f 0 RX message buffer |
Alexandre Belloni | 6850863 | 2020-02-14 15:17:51 +0100 | [diff] [blame] | 245 | * 0x090...0x0df 1-5 reserved |
Marc Kleine-Budde | 66a6ef0 | 2014-09-17 12:50:48 +0200 | [diff] [blame] | 246 | * 0x0e0...0x0ff 6-7 8 entry ID table |
| 247 | * (mx25, mx28, mx35, mx53) |
| 248 | * 0x0e0...0x2df 6-7..37 8..128 entry ID table |
Marc Kleine-Budde | 0012e5c | 2015-08-06 14:53:57 +0200 | [diff] [blame] | 249 | * size conf'ed via ctrl2::RFFN |
Marc Kleine-Budde | 66a6ef0 | 2014-09-17 12:50:48 +0200 | [diff] [blame] | 250 | * (mx6, vf610) |
| 251 | */ |
Marc Kleine-Budde | 62d1086 | 2015-08-27 16:01:27 +0200 | [diff] [blame] | 252 | u32 _reserved4[256]; /* 0x480 */ |
Marc Kleine-Budde | fe63a06 | 2020-09-22 16:44:13 +0200 | [diff] [blame] | 253 | u32 rximr[64]; /* 0x880 - Not affected by Soft Reset */ |
Marc Kleine-Budde | 62d1086 | 2015-08-27 16:01:27 +0200 | [diff] [blame] | 254 | u32 _reserved5[24]; /* 0x980 */ |
| 255 | u32 gfwr_mx6; /* 0x9e0 - MX6 */ |
| 256 | u32 _reserved6[63]; /* 0x9e4 */ |
Stefan Agner | cdce844 | 2014-07-15 14:56:21 +0200 | [diff] [blame] | 257 | u32 mecr; /* 0xae0 */ |
| 258 | u32 erriar; /* 0xae4 */ |
| 259 | u32 erridpr; /* 0xae8 */ |
| 260 | u32 errippr; /* 0xaec */ |
| 261 | u32 rerrar; /* 0xaf0 */ |
| 262 | u32 rerrdr; /* 0xaf4 */ |
| 263 | u32 rerrsynr; /* 0xaf8 */ |
| 264 | u32 errsr; /* 0xafc */ |
Marc Kleine-Budde | 4b70287 | 2020-09-22 16:44:12 +0200 | [diff] [blame] | 265 | u32 _reserved7[64]; /* 0xb00 */ |
Marc Kleine-Budde | fe63a06 | 2020-09-22 16:44:13 +0200 | [diff] [blame] | 266 | u32 fdctrl; /* 0xc00 - Not affected by Soft Reset */ |
| 267 | u32 fdcbt; /* 0xc04 - Not affected by Soft Reset */ |
Marc Kleine-Budde | 4b70287 | 2020-09-22 16:44:12 +0200 | [diff] [blame] | 268 | u32 fdcrc; /* 0xc08 */ |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 269 | }; |
| 270 | |
Marc Kleine-Budde | 4b70287 | 2020-09-22 16:44:12 +0200 | [diff] [blame] | 271 | static_assert(sizeof(struct flexcan_regs) == 0x4 + 0xc08); |
| 272 | |
Hui Wang | 30c1e67 | 2012-06-28 16:21:35 +0800 | [diff] [blame] | 273 | struct flexcan_devtype_data { |
Marc Kleine-Budde | f377bff | 2015-05-08 15:22:36 +0200 | [diff] [blame] | 274 | u32 quirks; /* quirks needed for different IP cores */ |
Hui Wang | 30c1e67 | 2012-06-28 16:21:35 +0800 | [diff] [blame] | 275 | }; |
| 276 | |
Aisheng Dong | de3578c | 2018-11-23 08:35:33 +0000 | [diff] [blame] | 277 | struct flexcan_stop_mode { |
| 278 | struct regmap *gpr; |
| 279 | u8 req_gpr; |
| 280 | u8 req_bit; |
| 281 | u8 ack_gpr; |
| 282 | u8 ack_bit; |
| 283 | }; |
| 284 | |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 285 | struct flexcan_priv { |
| 286 | struct can_priv can; |
Marc Kleine-Budde | 3016475 | 2015-05-10 15:26:58 +0200 | [diff] [blame] | 287 | struct can_rx_offload offload; |
Aisheng Dong | ca10989 | 2018-11-30 08:53:26 +0000 | [diff] [blame] | 288 | struct device *dev; |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 289 | |
Marc Kleine-Budde | 89af874 | 2015-05-08 09:32:58 +0200 | [diff] [blame] | 290 | struct flexcan_regs __iomem *regs; |
Pankaj Bansal | 0517961 | 2018-11-23 22:18:44 +0100 | [diff] [blame] | 291 | struct flexcan_mb __iomem *tx_mb; |
Marc Kleine-Budde | b93917c | 2015-07-12 00:47:47 +0200 | [diff] [blame] | 292 | struct flexcan_mb __iomem *tx_mb_reserved; |
Pankaj Bansal | 0517961 | 2018-11-23 22:18:44 +0100 | [diff] [blame] | 293 | u8 tx_mb_idx; |
| 294 | u8 mb_count; |
| 295 | u8 mb_size; |
Dong Aisheng | 8c306be | 2018-12-13 07:08:00 +0000 | [diff] [blame] | 296 | u8 clk_src; /* clock source of CAN Protocol Engine */ |
| 297 | |
Marc Kleine-Budde | 8ce5139 | 2019-03-01 12:17:30 +0100 | [diff] [blame] | 298 | u64 rx_mask; |
Marc Kleine-Budde | 0ca64f02 | 2019-03-01 13:54:19 +0100 | [diff] [blame] | 299 | u64 tx_mask; |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 300 | u32 reg_ctrl_default; |
| 301 | |
Steffen Trumtrar | 3d42a37 | 2012-07-17 16:14:34 +0200 | [diff] [blame] | 302 | struct clk *clk_ipg; |
| 303 | struct clk *clk_per; |
Marc Kleine-Budde | dda0b3b | 2012-07-13 14:52:48 +0200 | [diff] [blame] | 304 | const struct flexcan_devtype_data *devtype_data; |
Fabio Estevam | b7c4114 | 2013-06-10 23:12:57 -0300 | [diff] [blame] | 305 | struct regulator *reg_xceiver; |
Aisheng Dong | de3578c | 2018-11-23 08:35:33 +0000 | [diff] [blame] | 306 | struct flexcan_stop_mode stm; |
Pankaj Bansal | 88462d2 | 2017-11-24 18:52:08 +0530 | [diff] [blame] | 307 | |
| 308 | /* Read and Write APIs */ |
| 309 | u32 (*read)(void __iomem *addr); |
| 310 | void (*write)(u32 val, void __iomem *addr); |
Hui Wang | 30c1e67 | 2012-06-28 16:21:35 +0800 | [diff] [blame] | 311 | }; |
| 312 | |
Marc Kleine-Budde | a3c11a7 | 2016-07-04 14:45:44 +0200 | [diff] [blame] | 313 | 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] | 314 | .quirks = FLEXCAN_QUIRK_BROKEN_WERR_STATE | |
Uwe Kleine-König | 0e030a3 | 2018-04-25 16:50:39 +0200 | [diff] [blame] | 315 | FLEXCAN_QUIRK_BROKEN_PERR_STATE | |
| 316 | FLEXCAN_QUIRK_DEFAULT_BIG_ENDIAN, |
| 317 | }; |
| 318 | |
| 319 | static const struct flexcan_devtype_data fsl_imx25_devtype_data = { |
| 320 | .quirks = FLEXCAN_QUIRK_BROKEN_WERR_STATE | |
ZHU Yi (ST-FIR/ENG1-Zhu) | fb5b91d6 | 2017-09-15 07:09:37 +0000 | [diff] [blame] | 321 | FLEXCAN_QUIRK_BROKEN_PERR_STATE, |
Hui Wang | 30c1e67 | 2012-06-28 16:21:35 +0800 | [diff] [blame] | 322 | }; |
Marc Kleine-Budde | 0012e5c | 2015-08-06 14:53:57 +0200 | [diff] [blame] | 323 | |
ZHU Yi (ST-FIR/ENG1-Zhu) | 083c557 | 2017-09-15 07:08:23 +0000 | [diff] [blame] | 324 | static const struct flexcan_devtype_data fsl_imx28_devtype_data = { |
| 325 | .quirks = FLEXCAN_QUIRK_BROKEN_PERR_STATE, |
| 326 | }; |
Marc Kleine-Budde | 0012e5c | 2015-08-06 14:53:57 +0200 | [diff] [blame] | 327 | |
Marc Kleine-Budde | a3c11a7 | 2016-07-04 14:45:44 +0200 | [diff] [blame] | 328 | static const struct flexcan_devtype_data fsl_imx6q_devtype_data = { |
Marc Kleine-Budde | 096de07 | 2015-09-01 10:28:46 +0200 | [diff] [blame] | 329 | .quirks = FLEXCAN_QUIRK_DISABLE_RXFG | FLEXCAN_QUIRK_ENABLE_EACEN_RRS | |
Aisheng Dong | de3578c | 2018-11-23 08:35:33 +0000 | [diff] [blame] | 330 | FLEXCAN_QUIRK_USE_OFF_TIMESTAMP | FLEXCAN_QUIRK_BROKEN_PERR_STATE | |
| 331 | FLEXCAN_QUIRK_SETUP_STOP_MODE, |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 332 | }; |
Marc Kleine-Budde | 0012e5c | 2015-08-06 14:53:57 +0200 | [diff] [blame] | 333 | |
Marc Kleine-Budde | a3c11a7 | 2016-07-04 14:45:44 +0200 | [diff] [blame] | 334 | static const struct flexcan_devtype_data fsl_vf610_devtype_data = { |
Marc Kleine-Budde | 9eb7aa8 | 2015-09-01 08:57:55 +0200 | [diff] [blame] | 335 | .quirks = FLEXCAN_QUIRK_DISABLE_RXFG | FLEXCAN_QUIRK_ENABLE_EACEN_RRS | |
Marc Kleine-Budde | 29c64b1 | 2017-11-27 09:18:21 +0100 | [diff] [blame] | 336 | FLEXCAN_QUIRK_DISABLE_MECR | FLEXCAN_QUIRK_USE_OFF_TIMESTAMP | |
| 337 | FLEXCAN_QUIRK_BROKEN_PERR_STATE, |
Stefan Agner | cdce844 | 2014-07-15 14:56:21 +0200 | [diff] [blame] | 338 | }; |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 339 | |
Pankaj Bansal | 99b7668 | 2017-11-24 18:52:09 +0530 | [diff] [blame] | 340 | static const struct flexcan_devtype_data fsl_ls1021a_r2_devtype_data = { |
| 341 | .quirks = FLEXCAN_QUIRK_DISABLE_RXFG | FLEXCAN_QUIRK_ENABLE_EACEN_RRS | |
| 342 | FLEXCAN_QUIRK_DISABLE_MECR | FLEXCAN_QUIRK_BROKEN_PERR_STATE | |
| 343 | FLEXCAN_QUIRK_USE_OFF_TIMESTAMP, |
| 344 | }; |
| 345 | |
Marc Kleine-Budde | 194b9a4 | 2012-07-16 12:58:31 +0200 | [diff] [blame] | 346 | static const struct can_bittiming_const flexcan_bittiming_const = { |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 347 | .name = DRV_NAME, |
| 348 | .tseg1_min = 4, |
| 349 | .tseg1_max = 16, |
| 350 | .tseg2_min = 2, |
| 351 | .tseg2_max = 8, |
| 352 | .sjw_max = 4, |
| 353 | .brp_min = 1, |
| 354 | .brp_max = 256, |
| 355 | .brp_inc = 1, |
| 356 | }; |
| 357 | |
Pankaj Bansal | 88462d2 | 2017-11-24 18:52:08 +0530 | [diff] [blame] | 358 | /* FlexCAN module is essentially modelled as a little-endian IP in most |
| 359 | * SoCs, i.e the registers as well as the message buffer areas are |
| 360 | * implemented in a little-endian fashion. |
| 361 | * |
| 362 | * However there are some SoCs (e.g. LS1021A) which implement the FlexCAN |
| 363 | * module in a big-endian fashion (i.e the registers as well as the |
| 364 | * message buffer areas are implemented in a big-endian way). |
| 365 | * |
| 366 | * In addition, the FlexCAN module can be found on SoCs having ARM or |
| 367 | * PPC cores. So, we need to abstract off the register read/write |
| 368 | * functions, ensuring that these cater to all the combinations of module |
| 369 | * endianness and underlying CPU endianness. |
holt@sgi.com | 61e271e | 2011-08-16 17:32:20 +0000 | [diff] [blame] | 370 | */ |
Pankaj Bansal | 88462d2 | 2017-11-24 18:52:08 +0530 | [diff] [blame] | 371 | static inline u32 flexcan_read_be(void __iomem *addr) |
holt@sgi.com | 61e271e | 2011-08-16 17:32:20 +0000 | [diff] [blame] | 372 | { |
Pankaj Bansal | 88462d2 | 2017-11-24 18:52:08 +0530 | [diff] [blame] | 373 | return ioread32be(addr); |
holt@sgi.com | 61e271e | 2011-08-16 17:32:20 +0000 | [diff] [blame] | 374 | } |
| 375 | |
Pankaj Bansal | 88462d2 | 2017-11-24 18:52:08 +0530 | [diff] [blame] | 376 | static inline void flexcan_write_be(u32 val, void __iomem *addr) |
holt@sgi.com | 61e271e | 2011-08-16 17:32:20 +0000 | [diff] [blame] | 377 | { |
Pankaj Bansal | 88462d2 | 2017-11-24 18:52:08 +0530 | [diff] [blame] | 378 | iowrite32be(val, addr); |
holt@sgi.com | 61e271e | 2011-08-16 17:32:20 +0000 | [diff] [blame] | 379 | } |
| 380 | |
Pankaj Bansal | 88462d2 | 2017-11-24 18:52:08 +0530 | [diff] [blame] | 381 | static inline u32 flexcan_read_le(void __iomem *addr) |
holt@sgi.com | 61e271e | 2011-08-16 17:32:20 +0000 | [diff] [blame] | 382 | { |
Pankaj Bansal | 88462d2 | 2017-11-24 18:52:08 +0530 | [diff] [blame] | 383 | return ioread32(addr); |
holt@sgi.com | 61e271e | 2011-08-16 17:32:20 +0000 | [diff] [blame] | 384 | } |
Pankaj Bansal | 88462d2 | 2017-11-24 18:52:08 +0530 | [diff] [blame] | 385 | |
| 386 | static inline void flexcan_write_le(u32 val, void __iomem *addr) |
| 387 | { |
| 388 | iowrite32(val, addr); |
| 389 | } |
holt@sgi.com | 61e271e | 2011-08-16 17:32:20 +0000 | [diff] [blame] | 390 | |
Pankaj Bansal | 0517961 | 2018-11-23 22:18:44 +0100 | [diff] [blame] | 391 | static struct flexcan_mb __iomem *flexcan_get_mb(const struct flexcan_priv *priv, |
| 392 | u8 mb_index) |
| 393 | { |
Pankaj Bansal | 6cbf760 | 2018-08-28 23:19:12 +0530 | [diff] [blame] | 394 | u8 bank_size; |
| 395 | bool bank; |
| 396 | |
Pankaj Bansal | 0517961 | 2018-11-23 22:18:44 +0100 | [diff] [blame] | 397 | if (WARN_ON(mb_index >= priv->mb_count)) |
| 398 | return NULL; |
| 399 | |
Pankaj Bansal | 6cbf760 | 2018-08-28 23:19:12 +0530 | [diff] [blame] | 400 | bank_size = sizeof(priv->regs->mb[0]) / priv->mb_size; |
| 401 | |
| 402 | bank = mb_index >= bank_size; |
| 403 | if (bank) |
| 404 | mb_index -= bank_size; |
| 405 | |
Pankaj Bansal | 0517961 | 2018-11-23 22:18:44 +0100 | [diff] [blame] | 406 | return (struct flexcan_mb __iomem *) |
Pankaj Bansal | 6cbf760 | 2018-08-28 23:19:12 +0530 | [diff] [blame] | 407 | (&priv->regs->mb[bank][priv->mb_size * mb_index]); |
Pankaj Bansal | 0517961 | 2018-11-23 22:18:44 +0100 | [diff] [blame] | 408 | } |
| 409 | |
Joakim Zhang | b7603d0 | 2019-12-04 11:36:11 +0000 | [diff] [blame] | 410 | static int flexcan_low_power_enter_ack(struct flexcan_priv *priv) |
| 411 | { |
| 412 | struct flexcan_regs __iomem *regs = priv->regs; |
| 413 | unsigned int timeout = FLEXCAN_TIMEOUT_US / 10; |
| 414 | |
| 415 | while (timeout-- && !(priv->read(®s->mcr) & FLEXCAN_MCR_LPM_ACK)) |
| 416 | udelay(10); |
| 417 | |
| 418 | if (!(priv->read(®s->mcr) & FLEXCAN_MCR_LPM_ACK)) |
| 419 | return -ETIMEDOUT; |
| 420 | |
| 421 | return 0; |
| 422 | } |
| 423 | |
| 424 | static int flexcan_low_power_exit_ack(struct flexcan_priv *priv) |
| 425 | { |
| 426 | struct flexcan_regs __iomem *regs = priv->regs; |
| 427 | unsigned int timeout = FLEXCAN_TIMEOUT_US / 10; |
| 428 | |
| 429 | while (timeout-- && (priv->read(®s->mcr) & FLEXCAN_MCR_LPM_ACK)) |
| 430 | udelay(10); |
| 431 | |
| 432 | if (priv->read(®s->mcr) & FLEXCAN_MCR_LPM_ACK) |
| 433 | return -ETIMEDOUT; |
| 434 | |
| 435 | return 0; |
| 436 | } |
| 437 | |
Aisheng Dong | de3578c | 2018-11-23 08:35:33 +0000 | [diff] [blame] | 438 | static void flexcan_enable_wakeup_irq(struct flexcan_priv *priv, bool enable) |
| 439 | { |
| 440 | struct flexcan_regs __iomem *regs = priv->regs; |
| 441 | u32 reg_mcr; |
| 442 | |
| 443 | reg_mcr = priv->read(®s->mcr); |
| 444 | |
| 445 | if (enable) |
| 446 | reg_mcr |= FLEXCAN_MCR_WAK_MSK; |
| 447 | else |
| 448 | reg_mcr &= ~FLEXCAN_MCR_WAK_MSK; |
| 449 | |
| 450 | priv->write(reg_mcr, ®s->mcr); |
| 451 | } |
| 452 | |
Joakim Zhang | 5f186c2 | 2019-07-02 01:45:41 +0000 | [diff] [blame] | 453 | static inline int flexcan_enter_stop_mode(struct flexcan_priv *priv) |
Aisheng Dong | de3578c | 2018-11-23 08:35:33 +0000 | [diff] [blame] | 454 | { |
| 455 | struct flexcan_regs __iomem *regs = priv->regs; |
| 456 | u32 reg_mcr; |
| 457 | |
| 458 | reg_mcr = priv->read(®s->mcr); |
| 459 | reg_mcr |= FLEXCAN_MCR_SLF_WAK; |
| 460 | priv->write(reg_mcr, ®s->mcr); |
| 461 | |
| 462 | /* enable stop request */ |
| 463 | regmap_update_bits(priv->stm.gpr, priv->stm.req_gpr, |
| 464 | 1 << priv->stm.req_bit, 1 << priv->stm.req_bit); |
Joakim Zhang | 5f186c2 | 2019-07-02 01:45:41 +0000 | [diff] [blame] | 465 | |
Joakim Zhang | 048e3a34 | 2019-12-04 11:36:14 +0000 | [diff] [blame] | 466 | return flexcan_low_power_enter_ack(priv); |
Aisheng Dong | de3578c | 2018-11-23 08:35:33 +0000 | [diff] [blame] | 467 | } |
| 468 | |
Joakim Zhang | 5f186c2 | 2019-07-02 01:45:41 +0000 | [diff] [blame] | 469 | static inline int flexcan_exit_stop_mode(struct flexcan_priv *priv) |
Aisheng Dong | de3578c | 2018-11-23 08:35:33 +0000 | [diff] [blame] | 470 | { |
| 471 | struct flexcan_regs __iomem *regs = priv->regs; |
| 472 | u32 reg_mcr; |
| 473 | |
| 474 | /* remove stop request */ |
| 475 | regmap_update_bits(priv->stm.gpr, priv->stm.req_gpr, |
| 476 | 1 << priv->stm.req_bit, 0); |
| 477 | |
| 478 | reg_mcr = priv->read(®s->mcr); |
| 479 | reg_mcr &= ~FLEXCAN_MCR_SLF_WAK; |
| 480 | priv->write(reg_mcr, ®s->mcr); |
Joakim Zhang | 5f186c2 | 2019-07-02 01:45:41 +0000 | [diff] [blame] | 481 | |
Joakim Zhang | 048e3a34 | 2019-12-04 11:36:14 +0000 | [diff] [blame] | 482 | return flexcan_low_power_exit_ack(priv); |
Aisheng Dong | de3578c | 2018-11-23 08:35:33 +0000 | [diff] [blame] | 483 | } |
| 484 | |
ZHU Yi (ST-FIR/ENG1-Zhu) | da49a80 | 2017-09-15 07:03:58 +0000 | [diff] [blame] | 485 | static inline void flexcan_error_irq_enable(const struct flexcan_priv *priv) |
| 486 | { |
| 487 | struct flexcan_regs __iomem *regs = priv->regs; |
| 488 | u32 reg_ctrl = (priv->reg_ctrl_default | FLEXCAN_CTRL_ERR_MSK); |
| 489 | |
Pankaj Bansal | 88462d2 | 2017-11-24 18:52:08 +0530 | [diff] [blame] | 490 | priv->write(reg_ctrl, ®s->ctrl); |
ZHU Yi (ST-FIR/ENG1-Zhu) | da49a80 | 2017-09-15 07:03:58 +0000 | [diff] [blame] | 491 | } |
| 492 | |
| 493 | static inline void flexcan_error_irq_disable(const struct flexcan_priv *priv) |
| 494 | { |
| 495 | struct flexcan_regs __iomem *regs = priv->regs; |
| 496 | u32 reg_ctrl = (priv->reg_ctrl_default & ~FLEXCAN_CTRL_ERR_MSK); |
| 497 | |
Pankaj Bansal | 88462d2 | 2017-11-24 18:52:08 +0530 | [diff] [blame] | 498 | priv->write(reg_ctrl, ®s->ctrl); |
ZHU Yi (ST-FIR/ENG1-Zhu) | da49a80 | 2017-09-15 07:03:58 +0000 | [diff] [blame] | 499 | } |
| 500 | |
Aisheng Dong | ca10989 | 2018-11-30 08:53:26 +0000 | [diff] [blame] | 501 | static int flexcan_clks_enable(const struct flexcan_priv *priv) |
| 502 | { |
| 503 | int err; |
| 504 | |
| 505 | err = clk_prepare_enable(priv->clk_ipg); |
| 506 | if (err) |
| 507 | return err; |
| 508 | |
| 509 | err = clk_prepare_enable(priv->clk_per); |
| 510 | if (err) |
| 511 | clk_disable_unprepare(priv->clk_ipg); |
| 512 | |
| 513 | return err; |
| 514 | } |
| 515 | |
| 516 | static void flexcan_clks_disable(const struct flexcan_priv *priv) |
| 517 | { |
| 518 | clk_disable_unprepare(priv->clk_per); |
| 519 | clk_disable_unprepare(priv->clk_ipg); |
| 520 | } |
| 521 | |
Marc Kleine-Budde | f003698 | 2014-02-28 17:18:27 +0100 | [diff] [blame] | 522 | static inline int flexcan_transceiver_enable(const struct flexcan_priv *priv) |
| 523 | { |
| 524 | if (!priv->reg_xceiver) |
| 525 | return 0; |
| 526 | |
| 527 | return regulator_enable(priv->reg_xceiver); |
| 528 | } |
| 529 | |
| 530 | static inline int flexcan_transceiver_disable(const struct flexcan_priv *priv) |
| 531 | { |
| 532 | if (!priv->reg_xceiver) |
| 533 | return 0; |
| 534 | |
| 535 | return regulator_disable(priv->reg_xceiver); |
| 536 | } |
| 537 | |
Marc Kleine-Budde | 9b00b30 | 2014-02-28 15:30:18 +0100 | [diff] [blame] | 538 | static int flexcan_chip_enable(struct flexcan_priv *priv) |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 539 | { |
Marc Kleine-Budde | 89af874 | 2015-05-08 09:32:58 +0200 | [diff] [blame] | 540 | struct flexcan_regs __iomem *regs = priv->regs; |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 541 | u32 reg; |
| 542 | |
Pankaj Bansal | 88462d2 | 2017-11-24 18:52:08 +0530 | [diff] [blame] | 543 | reg = priv->read(®s->mcr); |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 544 | reg &= ~FLEXCAN_MCR_MDIS; |
Pankaj Bansal | 88462d2 | 2017-11-24 18:52:08 +0530 | [diff] [blame] | 545 | priv->write(reg, ®s->mcr); |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 546 | |
Joakim Zhang | b7603d0 | 2019-12-04 11:36:11 +0000 | [diff] [blame] | 547 | return flexcan_low_power_exit_ack(priv); |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 548 | } |
| 549 | |
Marc Kleine-Budde | 9b00b30 | 2014-02-28 15:30:18 +0100 | [diff] [blame] | 550 | static int flexcan_chip_disable(struct flexcan_priv *priv) |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 551 | { |
Marc Kleine-Budde | 89af874 | 2015-05-08 09:32:58 +0200 | [diff] [blame] | 552 | struct flexcan_regs __iomem *regs = priv->regs; |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 553 | u32 reg; |
| 554 | |
Pankaj Bansal | 88462d2 | 2017-11-24 18:52:08 +0530 | [diff] [blame] | 555 | reg = priv->read(®s->mcr); |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 556 | reg |= FLEXCAN_MCR_MDIS; |
Pankaj Bansal | 88462d2 | 2017-11-24 18:52:08 +0530 | [diff] [blame] | 557 | priv->write(reg, ®s->mcr); |
Marc Kleine-Budde | 9b00b30 | 2014-02-28 15:30:18 +0100 | [diff] [blame] | 558 | |
Joakim Zhang | b7603d0 | 2019-12-04 11:36:11 +0000 | [diff] [blame] | 559 | return flexcan_low_power_enter_ack(priv); |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 560 | } |
| 561 | |
Marc Kleine-Budde | b1aa1c7 | 2014-02-28 17:08:21 +0100 | [diff] [blame] | 562 | static int flexcan_chip_freeze(struct flexcan_priv *priv) |
| 563 | { |
Marc Kleine-Budde | 89af874 | 2015-05-08 09:32:58 +0200 | [diff] [blame] | 564 | struct flexcan_regs __iomem *regs = priv->regs; |
Marc Kleine-Budde | b1aa1c7 | 2014-02-28 17:08:21 +0100 | [diff] [blame] | 565 | unsigned int timeout = 1000 * 1000 * 10 / priv->can.bittiming.bitrate; |
| 566 | u32 reg; |
| 567 | |
Pankaj Bansal | 88462d2 | 2017-11-24 18:52:08 +0530 | [diff] [blame] | 568 | reg = priv->read(®s->mcr); |
Marc Kleine-Budde | b1aa1c7 | 2014-02-28 17:08:21 +0100 | [diff] [blame] | 569 | reg |= FLEXCAN_MCR_HALT; |
Pankaj Bansal | 88462d2 | 2017-11-24 18:52:08 +0530 | [diff] [blame] | 570 | priv->write(reg, ®s->mcr); |
Marc Kleine-Budde | b1aa1c7 | 2014-02-28 17:08:21 +0100 | [diff] [blame] | 571 | |
Pankaj Bansal | 88462d2 | 2017-11-24 18:52:08 +0530 | [diff] [blame] | 572 | while (timeout-- && !(priv->read(®s->mcr) & FLEXCAN_MCR_FRZ_ACK)) |
David Jander | 8badd65 | 2014-08-27 12:02:16 +0200 | [diff] [blame] | 573 | udelay(100); |
Marc Kleine-Budde | b1aa1c7 | 2014-02-28 17:08:21 +0100 | [diff] [blame] | 574 | |
Pankaj Bansal | 88462d2 | 2017-11-24 18:52:08 +0530 | [diff] [blame] | 575 | if (!(priv->read(®s->mcr) & FLEXCAN_MCR_FRZ_ACK)) |
Marc Kleine-Budde | b1aa1c7 | 2014-02-28 17:08:21 +0100 | [diff] [blame] | 576 | return -ETIMEDOUT; |
| 577 | |
| 578 | return 0; |
| 579 | } |
| 580 | |
| 581 | static int flexcan_chip_unfreeze(struct flexcan_priv *priv) |
| 582 | { |
Marc Kleine-Budde | 89af874 | 2015-05-08 09:32:58 +0200 | [diff] [blame] | 583 | struct flexcan_regs __iomem *regs = priv->regs; |
Marc Kleine-Budde | b1aa1c7 | 2014-02-28 17:08:21 +0100 | [diff] [blame] | 584 | unsigned int timeout = FLEXCAN_TIMEOUT_US / 10; |
| 585 | u32 reg; |
| 586 | |
Pankaj Bansal | 88462d2 | 2017-11-24 18:52:08 +0530 | [diff] [blame] | 587 | reg = priv->read(®s->mcr); |
Marc Kleine-Budde | b1aa1c7 | 2014-02-28 17:08:21 +0100 | [diff] [blame] | 588 | reg &= ~FLEXCAN_MCR_HALT; |
Pankaj Bansal | 88462d2 | 2017-11-24 18:52:08 +0530 | [diff] [blame] | 589 | priv->write(reg, ®s->mcr); |
Marc Kleine-Budde | b1aa1c7 | 2014-02-28 17:08:21 +0100 | [diff] [blame] | 590 | |
Pankaj Bansal | 88462d2 | 2017-11-24 18:52:08 +0530 | [diff] [blame] | 591 | while (timeout-- && (priv->read(®s->mcr) & FLEXCAN_MCR_FRZ_ACK)) |
David Jander | 8badd65 | 2014-08-27 12:02:16 +0200 | [diff] [blame] | 592 | udelay(10); |
Marc Kleine-Budde | b1aa1c7 | 2014-02-28 17:08:21 +0100 | [diff] [blame] | 593 | |
Pankaj Bansal | 88462d2 | 2017-11-24 18:52:08 +0530 | [diff] [blame] | 594 | if (priv->read(®s->mcr) & FLEXCAN_MCR_FRZ_ACK) |
Marc Kleine-Budde | b1aa1c7 | 2014-02-28 17:08:21 +0100 | [diff] [blame] | 595 | return -ETIMEDOUT; |
| 596 | |
| 597 | return 0; |
| 598 | } |
| 599 | |
Marc Kleine-Budde | 4b5b822 | 2014-02-28 15:16:59 +0100 | [diff] [blame] | 600 | static int flexcan_chip_softreset(struct flexcan_priv *priv) |
| 601 | { |
Marc Kleine-Budde | 89af874 | 2015-05-08 09:32:58 +0200 | [diff] [blame] | 602 | struct flexcan_regs __iomem *regs = priv->regs; |
Marc Kleine-Budde | 4b5b822 | 2014-02-28 15:16:59 +0100 | [diff] [blame] | 603 | unsigned int timeout = FLEXCAN_TIMEOUT_US / 10; |
| 604 | |
Pankaj Bansal | 88462d2 | 2017-11-24 18:52:08 +0530 | [diff] [blame] | 605 | priv->write(FLEXCAN_MCR_SOFTRST, ®s->mcr); |
| 606 | while (timeout-- && (priv->read(®s->mcr) & FLEXCAN_MCR_SOFTRST)) |
David Jander | 8badd65 | 2014-08-27 12:02:16 +0200 | [diff] [blame] | 607 | udelay(10); |
Marc Kleine-Budde | 4b5b822 | 2014-02-28 15:16:59 +0100 | [diff] [blame] | 608 | |
Pankaj Bansal | 88462d2 | 2017-11-24 18:52:08 +0530 | [diff] [blame] | 609 | if (priv->read(®s->mcr) & FLEXCAN_MCR_SOFTRST) |
Marc Kleine-Budde | 4b5b822 | 2014-02-28 15:16:59 +0100 | [diff] [blame] | 610 | return -ETIMEDOUT; |
| 611 | |
| 612 | return 0; |
| 613 | } |
| 614 | |
Stefan Agner | ec56acf | 2014-07-15 14:56:20 +0200 | [diff] [blame] | 615 | static int __flexcan_get_berr_counter(const struct net_device *dev, |
| 616 | struct can_berr_counter *bec) |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 617 | { |
| 618 | const struct flexcan_priv *priv = netdev_priv(dev); |
Marc Kleine-Budde | 89af874 | 2015-05-08 09:32:58 +0200 | [diff] [blame] | 619 | struct flexcan_regs __iomem *regs = priv->regs; |
Pankaj Bansal | 88462d2 | 2017-11-24 18:52:08 +0530 | [diff] [blame] | 620 | u32 reg = priv->read(®s->ecr); |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 621 | |
| 622 | bec->txerr = (reg >> 0) & 0xff; |
| 623 | bec->rxerr = (reg >> 8) & 0xff; |
| 624 | |
| 625 | return 0; |
| 626 | } |
| 627 | |
Stefan Agner | ec56acf | 2014-07-15 14:56:20 +0200 | [diff] [blame] | 628 | static int flexcan_get_berr_counter(const struct net_device *dev, |
| 629 | struct can_berr_counter *bec) |
| 630 | { |
| 631 | const struct flexcan_priv *priv = netdev_priv(dev); |
| 632 | int err; |
| 633 | |
Aisheng Dong | ca10989 | 2018-11-30 08:53:26 +0000 | [diff] [blame] | 634 | err = pm_runtime_get_sync(priv->dev); |
| 635 | if (err < 0) |
Stefan Agner | ec56acf | 2014-07-15 14:56:20 +0200 | [diff] [blame] | 636 | return err; |
| 637 | |
Stefan Agner | ec56acf | 2014-07-15 14:56:20 +0200 | [diff] [blame] | 638 | err = __flexcan_get_berr_counter(dev, bec); |
| 639 | |
Aisheng Dong | ca10989 | 2018-11-30 08:53:26 +0000 | [diff] [blame] | 640 | pm_runtime_put(priv->dev); |
Stefan Agner | ec56acf | 2014-07-15 14:56:20 +0200 | [diff] [blame] | 641 | |
| 642 | return err; |
| 643 | } |
| 644 | |
Marc Kleine-Budde | fb1e13e6 | 2018-04-26 23:13:38 +0200 | [diff] [blame] | 645 | 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] | 646 | { |
| 647 | const struct flexcan_priv *priv = netdev_priv(dev); |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 648 | struct can_frame *cf = (struct can_frame *)skb->data; |
| 649 | u32 can_id; |
Marc Kleine-Budde | 0012e5c | 2015-08-06 14:53:57 +0200 | [diff] [blame] | 650 | u32 data; |
Marc Kleine-Budde | 10d089b | 2014-09-23 11:18:11 +0200 | [diff] [blame] | 651 | u32 ctrl = FLEXCAN_MB_CODE_TX_DATA | (cf->can_dlc << 16); |
Pankaj Bansal | 0517961 | 2018-11-23 22:18:44 +0100 | [diff] [blame] | 652 | int i; |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 653 | |
| 654 | if (can_dropped_invalid_skb(dev, skb)) |
| 655 | return NETDEV_TX_OK; |
| 656 | |
| 657 | netif_stop_queue(dev); |
| 658 | |
| 659 | if (cf->can_id & CAN_EFF_FLAG) { |
| 660 | can_id = cf->can_id & CAN_EFF_MASK; |
| 661 | ctrl |= FLEXCAN_MB_CNT_IDE | FLEXCAN_MB_CNT_SRR; |
| 662 | } else { |
| 663 | can_id = (cf->can_id & CAN_SFF_MASK) << 18; |
| 664 | } |
| 665 | |
| 666 | if (cf->can_id & CAN_RTR_FLAG) |
| 667 | ctrl |= FLEXCAN_MB_CNT_RTR; |
| 668 | |
Pankaj Bansal | 0517961 | 2018-11-23 22:18:44 +0100 | [diff] [blame] | 669 | for (i = 0; i < cf->can_dlc; i += sizeof(u32)) { |
| 670 | data = be32_to_cpup((__be32 *)&cf->data[i]); |
| 671 | priv->write(data, &priv->tx_mb->data[i / sizeof(u32)]); |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 672 | } |
| 673 | |
Reuben Dowle | 9a12349 | 2011-11-01 11:18:03 +1300 | [diff] [blame] | 674 | can_put_echo_skb(skb, dev, 0); |
| 675 | |
Pankaj Bansal | 0517961 | 2018-11-23 22:18:44 +0100 | [diff] [blame] | 676 | priv->write(can_id, &priv->tx_mb->can_id); |
| 677 | priv->write(ctrl, &priv->tx_mb->can_ctrl); |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 678 | |
David Jander | 25e9244 | 2014-09-03 16:47:22 +0200 | [diff] [blame] | 679 | /* Errata ERR005829 step8: |
| 680 | * Write twice INACTIVE(0x8) code to first MB. |
| 681 | */ |
Pankaj Bansal | 88462d2 | 2017-11-24 18:52:08 +0530 | [diff] [blame] | 682 | priv->write(FLEXCAN_MB_CODE_TX_INACTIVE, |
Marc Kleine-Budde | 9dc1ee1 | 2018-11-12 15:33:57 +0100 | [diff] [blame] | 683 | &priv->tx_mb_reserved->can_ctrl); |
Pankaj Bansal | 88462d2 | 2017-11-24 18:52:08 +0530 | [diff] [blame] | 684 | priv->write(FLEXCAN_MB_CODE_TX_INACTIVE, |
Marc Kleine-Budde | 9dc1ee1 | 2018-11-12 15:33:57 +0100 | [diff] [blame] | 685 | &priv->tx_mb_reserved->can_ctrl); |
David Jander | 25e9244 | 2014-09-03 16:47:22 +0200 | [diff] [blame] | 686 | |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 687 | return NETDEV_TX_OK; |
| 688 | } |
| 689 | |
Marc Kleine-Budde | 3016475 | 2015-05-10 15:26:58 +0200 | [diff] [blame] | 690 | 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] | 691 | { |
| 692 | struct flexcan_priv *priv = netdev_priv(dev); |
Oleksij Rempel | d788905f | 2018-09-18 11:40:41 +0200 | [diff] [blame] | 693 | struct flexcan_regs __iomem *regs = priv->regs; |
Marc Kleine-Budde | a5c02f66 | 2017-01-18 11:38:26 +0100 | [diff] [blame] | 694 | struct sk_buff *skb; |
| 695 | struct can_frame *cf; |
Marc Kleine-Budde | d166f56 | 2017-01-17 17:33:46 +0100 | [diff] [blame] | 696 | bool rx_errors = false, tx_errors = false; |
Oleksij Rempel | d788905f | 2018-09-18 11:40:41 +0200 | [diff] [blame] | 697 | u32 timestamp; |
Marc Kleine-Budde | 7581243 | 2019-07-15 20:53:08 +0200 | [diff] [blame] | 698 | int err; |
Oleksij Rempel | d788905f | 2018-09-18 11:40:41 +0200 | [diff] [blame] | 699 | |
| 700 | timestamp = priv->read(®s->timer) << 16; |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 701 | |
Marc Kleine-Budde | a5c02f66 | 2017-01-18 11:38:26 +0100 | [diff] [blame] | 702 | skb = alloc_can_err_skb(dev, &cf); |
| 703 | if (unlikely(!skb)) |
Marc Kleine-Budde | 3016475 | 2015-05-10 15:26:58 +0200 | [diff] [blame] | 704 | return; |
Marc Kleine-Budde | a5c02f66 | 2017-01-18 11:38:26 +0100 | [diff] [blame] | 705 | |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 706 | cf->can_id |= CAN_ERR_PROT | CAN_ERR_BUSERROR; |
| 707 | |
| 708 | if (reg_esr & FLEXCAN_ESR_BIT1_ERR) { |
Wolfgang Grandegger | aabdfd6 | 2012-02-01 11:02:05 +0100 | [diff] [blame] | 709 | netdev_dbg(dev, "BIT1_ERR irq\n"); |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 710 | cf->data[2] |= CAN_ERR_PROT_BIT1; |
Marc Kleine-Budde | d166f56 | 2017-01-17 17:33:46 +0100 | [diff] [blame] | 711 | tx_errors = true; |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 712 | } |
| 713 | if (reg_esr & FLEXCAN_ESR_BIT0_ERR) { |
Wolfgang Grandegger | aabdfd6 | 2012-02-01 11:02:05 +0100 | [diff] [blame] | 714 | netdev_dbg(dev, "BIT0_ERR irq\n"); |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 715 | cf->data[2] |= CAN_ERR_PROT_BIT0; |
Marc Kleine-Budde | d166f56 | 2017-01-17 17:33:46 +0100 | [diff] [blame] | 716 | tx_errors = true; |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 717 | } |
| 718 | if (reg_esr & FLEXCAN_ESR_ACK_ERR) { |
Wolfgang Grandegger | aabdfd6 | 2012-02-01 11:02:05 +0100 | [diff] [blame] | 719 | netdev_dbg(dev, "ACK_ERR irq\n"); |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 720 | cf->can_id |= CAN_ERR_ACK; |
Oliver Hartkopp | ffd461f | 2015-11-21 18:41:20 +0100 | [diff] [blame] | 721 | cf->data[3] = CAN_ERR_PROT_LOC_ACK; |
Marc Kleine-Budde | d166f56 | 2017-01-17 17:33:46 +0100 | [diff] [blame] | 722 | tx_errors = true; |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 723 | } |
| 724 | if (reg_esr & FLEXCAN_ESR_CRC_ERR) { |
Wolfgang Grandegger | aabdfd6 | 2012-02-01 11:02:05 +0100 | [diff] [blame] | 725 | netdev_dbg(dev, "CRC_ERR irq\n"); |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 726 | cf->data[2] |= CAN_ERR_PROT_BIT; |
Oliver Hartkopp | ffd461f | 2015-11-21 18:41:20 +0100 | [diff] [blame] | 727 | cf->data[3] = CAN_ERR_PROT_LOC_CRC_SEQ; |
Marc Kleine-Budde | d166f56 | 2017-01-17 17:33:46 +0100 | [diff] [blame] | 728 | rx_errors = true; |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 729 | } |
| 730 | if (reg_esr & FLEXCAN_ESR_FRM_ERR) { |
Wolfgang Grandegger | aabdfd6 | 2012-02-01 11:02:05 +0100 | [diff] [blame] | 731 | netdev_dbg(dev, "FRM_ERR irq\n"); |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 732 | cf->data[2] |= CAN_ERR_PROT_FORM; |
Marc Kleine-Budde | d166f56 | 2017-01-17 17:33:46 +0100 | [diff] [blame] | 733 | rx_errors = true; |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 734 | } |
| 735 | if (reg_esr & FLEXCAN_ESR_STF_ERR) { |
Wolfgang Grandegger | aabdfd6 | 2012-02-01 11:02:05 +0100 | [diff] [blame] | 736 | netdev_dbg(dev, "STF_ERR irq\n"); |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 737 | cf->data[2] |= CAN_ERR_PROT_STUFF; |
Marc Kleine-Budde | d166f56 | 2017-01-17 17:33:46 +0100 | [diff] [blame] | 738 | rx_errors = true; |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 739 | } |
| 740 | |
| 741 | priv->can.can_stats.bus_error++; |
| 742 | if (rx_errors) |
| 743 | dev->stats.rx_errors++; |
| 744 | if (tx_errors) |
| 745 | dev->stats.tx_errors++; |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 746 | |
Marc Kleine-Budde | 7581243 | 2019-07-15 20:53:08 +0200 | [diff] [blame] | 747 | err = can_rx_offload_queue_sorted(&priv->offload, skb, timestamp); |
| 748 | if (err) |
| 749 | dev->stats.rx_fifo_errors++; |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 750 | } |
| 751 | |
Marc Kleine-Budde | 3016475 | 2015-05-10 15:26:58 +0200 | [diff] [blame] | 752 | 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] | 753 | { |
| 754 | struct flexcan_priv *priv = netdev_priv(dev); |
Oleksij Rempel | d788905f | 2018-09-18 11:40:41 +0200 | [diff] [blame] | 755 | struct flexcan_regs __iomem *regs = priv->regs; |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 756 | struct sk_buff *skb; |
| 757 | struct can_frame *cf; |
Marc Kleine-Budde | 238443d | 2017-01-18 11:25:41 +0100 | [diff] [blame] | 758 | enum can_state new_state, rx_state, tx_state; |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 759 | int flt; |
Andri Yngvason | 71a3aed | 2014-12-03 17:54:15 +0000 | [diff] [blame] | 760 | struct can_berr_counter bec; |
Oleksij Rempel | d788905f | 2018-09-18 11:40:41 +0200 | [diff] [blame] | 761 | u32 timestamp; |
Marc Kleine-Budde | 7581243 | 2019-07-15 20:53:08 +0200 | [diff] [blame] | 762 | int err; |
Oleksij Rempel | d788905f | 2018-09-18 11:40:41 +0200 | [diff] [blame] | 763 | |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 764 | flt = reg_esr & FLEXCAN_ESR_FLT_CONF_MASK; |
| 765 | if (likely(flt == FLEXCAN_ESR_FLT_CONF_ACTIVE)) { |
Andri Yngvason | 71a3aed | 2014-12-03 17:54:15 +0000 | [diff] [blame] | 766 | tx_state = unlikely(reg_esr & FLEXCAN_ESR_TX_WRN) ? |
Marc Kleine-Budde | 0012e5c | 2015-08-06 14:53:57 +0200 | [diff] [blame] | 767 | CAN_STATE_ERROR_WARNING : CAN_STATE_ERROR_ACTIVE; |
Andri Yngvason | 71a3aed | 2014-12-03 17:54:15 +0000 | [diff] [blame] | 768 | rx_state = unlikely(reg_esr & FLEXCAN_ESR_RX_WRN) ? |
Marc Kleine-Budde | 0012e5c | 2015-08-06 14:53:57 +0200 | [diff] [blame] | 769 | CAN_STATE_ERROR_WARNING : CAN_STATE_ERROR_ACTIVE; |
Andri Yngvason | 71a3aed | 2014-12-03 17:54:15 +0000 | [diff] [blame] | 770 | new_state = max(tx_state, rx_state); |
Andri Yngvason | 258ce80 | 2015-03-17 13:03:09 +0000 | [diff] [blame] | 771 | } else { |
Andri Yngvason | 71a3aed | 2014-12-03 17:54:15 +0000 | [diff] [blame] | 772 | __flexcan_get_berr_counter(dev, &bec); |
Andri Yngvason | 258ce80 | 2015-03-17 13:03:09 +0000 | [diff] [blame] | 773 | new_state = flt == FLEXCAN_ESR_FLT_CONF_PASSIVE ? |
Marc Kleine-Budde | 0012e5c | 2015-08-06 14:53:57 +0200 | [diff] [blame] | 774 | CAN_STATE_ERROR_PASSIVE : CAN_STATE_BUS_OFF; |
Andri Yngvason | 71a3aed | 2014-12-03 17:54:15 +0000 | [diff] [blame] | 775 | rx_state = bec.rxerr >= bec.txerr ? new_state : 0; |
| 776 | tx_state = bec.rxerr <= bec.txerr ? new_state : 0; |
Andri Yngvason | 71a3aed | 2014-12-03 17:54:15 +0000 | [diff] [blame] | 777 | } |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 778 | |
| 779 | /* state hasn't changed */ |
| 780 | if (likely(new_state == priv->can.state)) |
Marc Kleine-Budde | 3016475 | 2015-05-10 15:26:58 +0200 | [diff] [blame] | 781 | return; |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 782 | |
Marc Kleine-Budde | 58ed8e7 | 2019-10-09 15:15:37 +0200 | [diff] [blame] | 783 | timestamp = priv->read(®s->timer) << 16; |
| 784 | |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 785 | skb = alloc_can_err_skb(dev, &cf); |
| 786 | if (unlikely(!skb)) |
Marc Kleine-Budde | 3016475 | 2015-05-10 15:26:58 +0200 | [diff] [blame] | 787 | return; |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 788 | |
Andri Yngvason | 71a3aed | 2014-12-03 17:54:15 +0000 | [diff] [blame] | 789 | can_change_state(dev, cf, tx_state, rx_state); |
| 790 | |
| 791 | if (unlikely(new_state == CAN_STATE_BUS_OFF)) |
| 792 | can_bus_off(dev); |
| 793 | |
Marc Kleine-Budde | 7581243 | 2019-07-15 20:53:08 +0200 | [diff] [blame] | 794 | err = can_rx_offload_queue_sorted(&priv->offload, skb, timestamp); |
| 795 | if (err) |
| 796 | dev->stats.rx_fifo_errors++; |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 797 | } |
| 798 | |
Marc Kleine-Budde | d3a5150 | 2019-03-01 15:38:05 +0100 | [diff] [blame] | 799 | static inline u64 flexcan_read64_mask(struct flexcan_priv *priv, void __iomem *addr, u64 mask) |
| 800 | { |
| 801 | u64 reg = 0; |
| 802 | |
| 803 | if (upper_32_bits(mask)) |
| 804 | reg = (u64)priv->read(addr - 4) << 32; |
| 805 | if (lower_32_bits(mask)) |
| 806 | reg |= priv->read(addr); |
| 807 | |
| 808 | return reg & mask; |
| 809 | } |
| 810 | |
Marc Kleine-Budde | b87c28b7 | 2019-03-01 15:38:05 +0100 | [diff] [blame] | 811 | static inline void flexcan_write64(struct flexcan_priv *priv, u64 val, void __iomem *addr) |
| 812 | { |
| 813 | if (upper_32_bits(val)) |
| 814 | priv->write(upper_32_bits(val), addr - 4); |
| 815 | if (lower_32_bits(val)) |
| 816 | priv->write(lower_32_bits(val), addr); |
| 817 | } |
| 818 | |
Marc Kleine-Budde | d3a5150 | 2019-03-01 15:38:05 +0100 | [diff] [blame] | 819 | static inline u64 flexcan_read_reg_iflag_rx(struct flexcan_priv *priv) |
| 820 | { |
| 821 | return flexcan_read64_mask(priv, &priv->regs->iflag1, priv->rx_mask); |
| 822 | } |
| 823 | |
Marc Kleine-Budde | b87c28b7 | 2019-03-01 15:38:05 +0100 | [diff] [blame] | 824 | static inline u64 flexcan_read_reg_iflag_tx(struct flexcan_priv *priv) |
| 825 | { |
| 826 | return flexcan_read64_mask(priv, &priv->regs->iflag1, priv->tx_mask); |
| 827 | } |
| 828 | |
Marc Kleine-Budde | 3016475 | 2015-05-10 15:26:58 +0200 | [diff] [blame] | 829 | 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] | 830 | { |
Marc Kleine-Budde | 3016475 | 2015-05-10 15:26:58 +0200 | [diff] [blame] | 831 | return container_of(offload, struct flexcan_priv, offload); |
| 832 | } |
| 833 | |
Joakim Zhang | 4e9c948 | 2019-07-12 08:02:38 +0000 | [diff] [blame] | 834 | static struct sk_buff *flexcan_mailbox_read(struct can_rx_offload *offload, |
| 835 | unsigned int n, u32 *timestamp, |
| 836 | bool drop) |
Marc Kleine-Budde | 3016475 | 2015-05-10 15:26:58 +0200 | [diff] [blame] | 837 | { |
| 838 | struct flexcan_priv *priv = rx_offload_to_priv(offload); |
Marc Kleine-Budde | 89af874 | 2015-05-08 09:32:58 +0200 | [diff] [blame] | 839 | struct flexcan_regs __iomem *regs = priv->regs; |
Pankaj Bansal | 0517961 | 2018-11-23 22:18:44 +0100 | [diff] [blame] | 840 | struct flexcan_mb __iomem *mb; |
Joakim Zhang | 4e9c948 | 2019-07-12 08:02:38 +0000 | [diff] [blame] | 841 | struct sk_buff *skb; |
| 842 | struct can_frame *cf; |
Marc Kleine-Budde | 3016475 | 2015-05-10 15:26:58 +0200 | [diff] [blame] | 843 | u32 reg_ctrl, reg_id, reg_iflag1; |
Pankaj Bansal | 0517961 | 2018-11-23 22:18:44 +0100 | [diff] [blame] | 844 | int i; |
| 845 | |
Joakim Zhang | 4e9c948 | 2019-07-12 08:02:38 +0000 | [diff] [blame] | 846 | if (unlikely(drop)) { |
| 847 | skb = ERR_PTR(-ENOBUFS); |
| 848 | goto mark_as_read; |
| 849 | } |
| 850 | |
Pankaj Bansal | 0517961 | 2018-11-23 22:18:44 +0100 | [diff] [blame] | 851 | mb = flexcan_get_mb(priv, n); |
Marc Kleine-Budde | 3016475 | 2015-05-10 15:26:58 +0200 | [diff] [blame] | 852 | |
Marc Kleine-Budde | b3cf53e | 2015-09-01 09:00:13 +0200 | [diff] [blame] | 853 | if (priv->devtype_data->quirks & FLEXCAN_QUIRK_USE_OFF_TIMESTAMP) { |
| 854 | u32 code; |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 855 | |
Marc Kleine-Budde | b3cf53e | 2015-09-01 09:00:13 +0200 | [diff] [blame] | 856 | do { |
Pankaj Bansal | 88462d2 | 2017-11-24 18:52:08 +0530 | [diff] [blame] | 857 | reg_ctrl = priv->read(&mb->can_ctrl); |
Marc Kleine-Budde | b3cf53e | 2015-09-01 09:00:13 +0200 | [diff] [blame] | 858 | } while (reg_ctrl & FLEXCAN_MB_CODE_RX_BUSY_BIT); |
| 859 | |
| 860 | /* is this MB empty? */ |
| 861 | code = reg_ctrl & FLEXCAN_MB_CODE_MASK; |
| 862 | if ((code != FLEXCAN_MB_CODE_RX_FULL) && |
| 863 | (code != FLEXCAN_MB_CODE_RX_OVERRUN)) |
Joakim Zhang | 4e9c948 | 2019-07-12 08:02:38 +0000 | [diff] [blame] | 864 | return NULL; |
Marc Kleine-Budde | b3cf53e | 2015-09-01 09:00:13 +0200 | [diff] [blame] | 865 | |
| 866 | if (code == FLEXCAN_MB_CODE_RX_OVERRUN) { |
| 867 | /* This MB was overrun, we lost data */ |
| 868 | offload->dev->stats.rx_over_errors++; |
| 869 | offload->dev->stats.rx_errors++; |
| 870 | } |
| 871 | } else { |
Pankaj Bansal | 88462d2 | 2017-11-24 18:52:08 +0530 | [diff] [blame] | 872 | reg_iflag1 = priv->read(®s->iflag1); |
Marc Kleine-Budde | b3cf53e | 2015-09-01 09:00:13 +0200 | [diff] [blame] | 873 | if (!(reg_iflag1 & FLEXCAN_IFLAG_RX_FIFO_AVAILABLE)) |
Joakim Zhang | 4e9c948 | 2019-07-12 08:02:38 +0000 | [diff] [blame] | 874 | return NULL; |
Marc Kleine-Budde | b3cf53e | 2015-09-01 09:00:13 +0200 | [diff] [blame] | 875 | |
Pankaj Bansal | 88462d2 | 2017-11-24 18:52:08 +0530 | [diff] [blame] | 876 | reg_ctrl = priv->read(&mb->can_ctrl); |
Marc Kleine-Budde | b3cf53e | 2015-09-01 09:00:13 +0200 | [diff] [blame] | 877 | } |
| 878 | |
Joakim Zhang | 4e9c948 | 2019-07-12 08:02:38 +0000 | [diff] [blame] | 879 | skb = alloc_can_skb(offload->dev, &cf); |
| 880 | if (!skb) { |
| 881 | skb = ERR_PTR(-ENOMEM); |
| 882 | goto mark_as_read; |
| 883 | } |
| 884 | |
Marc Kleine-Budde | 3016475 | 2015-05-10 15:26:58 +0200 | [diff] [blame] | 885 | /* increase timstamp to full 32 bit */ |
| 886 | *timestamp = reg_ctrl << 16; |
| 887 | |
Pankaj Bansal | 88462d2 | 2017-11-24 18:52:08 +0530 | [diff] [blame] | 888 | reg_id = priv->read(&mb->can_id); |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 889 | if (reg_ctrl & FLEXCAN_MB_CNT_IDE) |
| 890 | cf->can_id = ((reg_id >> 0) & CAN_EFF_MASK) | CAN_EFF_FLAG; |
| 891 | else |
| 892 | cf->can_id = (reg_id >> 18) & CAN_SFF_MASK; |
| 893 | |
| 894 | if (reg_ctrl & FLEXCAN_MB_CNT_RTR) |
| 895 | cf->can_id |= CAN_RTR_FLAG; |
| 896 | cf->can_dlc = get_can_dlc((reg_ctrl >> 16) & 0xf); |
| 897 | |
Pankaj Bansal | 0517961 | 2018-11-23 22:18:44 +0100 | [diff] [blame] | 898 | for (i = 0; i < cf->can_dlc; i += sizeof(u32)) { |
| 899 | __be32 data = cpu_to_be32(priv->read(&mb->data[i / sizeof(u32)])); |
| 900 | *(__be32 *)(cf->data + i) = data; |
| 901 | } |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 902 | |
Joakim Zhang | 4e9c948 | 2019-07-12 08:02:38 +0000 | [diff] [blame] | 903 | mark_as_read: |
Marc Kleine-Budde | b9468ad | 2019-03-01 16:27:59 +0100 | [diff] [blame] | 904 | if (priv->devtype_data->quirks & FLEXCAN_QUIRK_USE_OFF_TIMESTAMP) |
| 905 | flexcan_write64(priv, FLEXCAN_IFLAG_MB(n), ®s->iflag1); |
| 906 | else |
Pankaj Bansal | 88462d2 | 2017-11-24 18:52:08 +0530 | [diff] [blame] | 907 | priv->write(FLEXCAN_IFLAG_RX_FIFO_AVAILABLE, ®s->iflag1); |
Fabio Baltieri | adccadb | 2012-12-18 18:50:58 +0100 | [diff] [blame] | 908 | |
Pankaj Bansal | 5178b7c | 2018-08-01 19:36:46 +0530 | [diff] [blame] | 909 | /* Read the Free Running Timer. It is optional but recommended |
| 910 | * to unlock Mailbox as soon as possible and make it available |
| 911 | * for reception. |
| 912 | */ |
| 913 | priv->read(®s->timer); |
| 914 | |
Joakim Zhang | 4e9c948 | 2019-07-12 08:02:38 +0000 | [diff] [blame] | 915 | return skb; |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 916 | } |
| 917 | |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 918 | static irqreturn_t flexcan_irq(int irq, void *dev_id) |
| 919 | { |
| 920 | struct net_device *dev = dev_id; |
| 921 | struct net_device_stats *stats = &dev->stats; |
| 922 | struct flexcan_priv *priv = netdev_priv(dev); |
Marc Kleine-Budde | 89af874 | 2015-05-08 09:32:58 +0200 | [diff] [blame] | 923 | struct flexcan_regs __iomem *regs = priv->regs; |
Marc Kleine-Budde | dd2f122 | 2017-01-18 11:45:14 +0100 | [diff] [blame] | 924 | irqreturn_t handled = IRQ_NONE; |
Marc Kleine-Budde | 0ca64f02 | 2019-03-01 13:54:19 +0100 | [diff] [blame] | 925 | u64 reg_iflag_tx; |
| 926 | u32 reg_esr; |
ZHU Yi (ST-FIR/ENG1-Zhu) | da49a80 | 2017-09-15 07:03:58 +0000 | [diff] [blame] | 927 | enum can_state last_state = priv->can.state; |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 928 | |
Marc Kleine-Budde | 3016475 | 2015-05-10 15:26:58 +0200 | [diff] [blame] | 929 | /* reception interrupt */ |
Marc Kleine-Budde | b3cf53e | 2015-09-01 09:00:13 +0200 | [diff] [blame] | 930 | if (priv->devtype_data->quirks & FLEXCAN_QUIRK_USE_OFF_TIMESTAMP) { |
Marc Kleine-Budde | 4e26598 | 2019-03-01 16:29:47 +0100 | [diff] [blame] | 931 | u64 reg_iflag_rx; |
Marc Kleine-Budde | b3cf53e | 2015-09-01 09:00:13 +0200 | [diff] [blame] | 932 | int ret; |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 933 | |
Marc Kleine-Budde | 4e26598 | 2019-03-01 16:29:47 +0100 | [diff] [blame] | 934 | while ((reg_iflag_rx = flexcan_read_reg_iflag_rx(priv))) { |
Marc Kleine-Budde | b3cf53e | 2015-09-01 09:00:13 +0200 | [diff] [blame] | 935 | handled = IRQ_HANDLED; |
| 936 | ret = can_rx_offload_irq_offload_timestamp(&priv->offload, |
Marc Kleine-Budde | 4e26598 | 2019-03-01 16:29:47 +0100 | [diff] [blame] | 937 | reg_iflag_rx); |
Marc Kleine-Budde | b3cf53e | 2015-09-01 09:00:13 +0200 | [diff] [blame] | 938 | if (!ret) |
| 939 | break; |
| 940 | } |
| 941 | } else { |
Alexander Stein | cbffaf7 | 2018-10-11 17:01:25 +0200 | [diff] [blame] | 942 | u32 reg_iflag1; |
| 943 | |
| 944 | reg_iflag1 = priv->read(®s->iflag1); |
Marc Kleine-Budde | b3cf53e | 2015-09-01 09:00:13 +0200 | [diff] [blame] | 945 | if (reg_iflag1 & FLEXCAN_IFLAG_RX_FIFO_AVAILABLE) { |
| 946 | handled = IRQ_HANDLED; |
| 947 | can_rx_offload_irq_offload_fifo(&priv->offload); |
| 948 | } |
| 949 | |
| 950 | /* FIFO overflow interrupt */ |
| 951 | if (reg_iflag1 & FLEXCAN_IFLAG_RX_FIFO_OVERFLOW) { |
| 952 | handled = IRQ_HANDLED; |
Pankaj Bansal | 88462d2 | 2017-11-24 18:52:08 +0530 | [diff] [blame] | 953 | priv->write(FLEXCAN_IFLAG_RX_FIFO_OVERFLOW, |
| 954 | ®s->iflag1); |
Marc Kleine-Budde | b3cf53e | 2015-09-01 09:00:13 +0200 | [diff] [blame] | 955 | dev->stats.rx_over_errors++; |
| 956 | dev->stats.rx_errors++; |
| 957 | } |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 958 | } |
| 959 | |
Marc Kleine-Budde | b87c28b7 | 2019-03-01 15:38:05 +0100 | [diff] [blame] | 960 | reg_iflag_tx = flexcan_read_reg_iflag_tx(priv); |
Alexander Stein | cbffaf7 | 2018-10-11 17:01:25 +0200 | [diff] [blame] | 961 | |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 962 | /* transmission complete interrupt */ |
Marc Kleine-Budde | 0ca64f02 | 2019-03-01 13:54:19 +0100 | [diff] [blame] | 963 | if (reg_iflag_tx & priv->tx_mask) { |
Pankaj Bansal | 0517961 | 2018-11-23 22:18:44 +0100 | [diff] [blame] | 964 | u32 reg_ctrl = priv->read(&priv->tx_mb->can_ctrl); |
Oleksij Rempel | ed72bc8 | 2018-09-18 11:40:39 +0200 | [diff] [blame] | 965 | |
Marc Kleine-Budde | dd2f122 | 2017-01-18 11:45:14 +0100 | [diff] [blame] | 966 | handled = IRQ_HANDLED; |
Oleksij Rempel | ed72bc8 | 2018-09-18 11:40:39 +0200 | [diff] [blame] | 967 | stats->tx_bytes += can_rx_offload_get_echo_skb(&priv->offload, |
| 968 | 0, reg_ctrl << 16); |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 969 | stats->tx_packets++; |
Fabio Baltieri | adccadb | 2012-12-18 18:50:58 +0100 | [diff] [blame] | 970 | can_led_event(dev, CAN_LED_EVENT_TX); |
Marc Kleine-Budde | 0012e5c | 2015-08-06 14:53:57 +0200 | [diff] [blame] | 971 | |
| 972 | /* after sending a RTR frame MB is in RX mode */ |
Pankaj Bansal | 88462d2 | 2017-11-24 18:52:08 +0530 | [diff] [blame] | 973 | priv->write(FLEXCAN_MB_CODE_TX_INACTIVE, |
Pankaj Bansal | 0517961 | 2018-11-23 22:18:44 +0100 | [diff] [blame] | 974 | &priv->tx_mb->can_ctrl); |
Marc Kleine-Budde | b87c28b7 | 2019-03-01 15:38:05 +0100 | [diff] [blame] | 975 | flexcan_write64(priv, priv->tx_mask, ®s->iflag1); |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 976 | netif_wake_queue(dev); |
| 977 | } |
| 978 | |
Pankaj Bansal | 88462d2 | 2017-11-24 18:52:08 +0530 | [diff] [blame] | 979 | reg_esr = priv->read(®s->esr); |
Marc Kleine-Budde | 3016475 | 2015-05-10 15:26:58 +0200 | [diff] [blame] | 980 | |
Joakim Zhang | ab60523 | 2019-12-04 11:36:08 +0000 | [diff] [blame] | 981 | /* ACK all bus error, state change and wake IRQ sources */ |
| 982 | if (reg_esr & (FLEXCAN_ESR_ALL_INT | FLEXCAN_ESR_WAK_INT)) { |
Marc Kleine-Budde | dd2f122 | 2017-01-18 11:45:14 +0100 | [diff] [blame] | 983 | handled = IRQ_HANDLED; |
Joakim Zhang | ab60523 | 2019-12-04 11:36:08 +0000 | [diff] [blame] | 984 | 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] | 985 | } |
| 986 | |
ZHU Yi (ST-FIR/ENG1-Zhu) | ad23023 | 2017-09-15 06:59:15 +0000 | [diff] [blame] | 987 | /* state change interrupt or broken error state quirk fix is enabled */ |
| 988 | if ((reg_esr & FLEXCAN_ESR_ERR_STATE) || |
ZHU Yi (ST-FIR/ENG1-Zhu) | da49a80 | 2017-09-15 07:03:58 +0000 | [diff] [blame] | 989 | (priv->devtype_data->quirks & (FLEXCAN_QUIRK_BROKEN_WERR_STATE | |
Marc Kleine-Budde | bc8ad65 | 2018-11-28 15:45:27 +0100 | [diff] [blame] | 990 | FLEXCAN_QUIRK_BROKEN_PERR_STATE))) |
Marc Kleine-Budde | 3016475 | 2015-05-10 15:26:58 +0200 | [diff] [blame] | 991 | flexcan_irq_state(dev, reg_esr); |
| 992 | |
| 993 | /* bus error IRQ - handle if bus error reporting is activated */ |
| 994 | if ((reg_esr & FLEXCAN_ESR_ERR_BUS) && |
| 995 | (priv->can.ctrlmode & CAN_CTRLMODE_BERR_REPORTING)) |
| 996 | flexcan_irq_bus_err(dev, reg_esr); |
| 997 | |
ZHU Yi (ST-FIR/ENG1-Zhu) | da49a80 | 2017-09-15 07:03:58 +0000 | [diff] [blame] | 998 | /* availability of error interrupt among state transitions in case |
| 999 | * bus error reporting is de-activated and |
| 1000 | * FLEXCAN_QUIRK_BROKEN_PERR_STATE is enabled: |
| 1001 | * +--------------------------------------------------------------+ |
| 1002 | * | +----------------------------------------------+ [stopped / | |
| 1003 | * | | | sleeping] -+ |
| 1004 | * +-+-> active <-> warning <-> passive -> bus off -+ |
| 1005 | * ___________^^^^^^^^^^^^_______________________________ |
| 1006 | * disabled(1) enabled disabled |
| 1007 | * |
| 1008 | * (1): enabled if FLEXCAN_QUIRK_BROKEN_WERR_STATE is enabled |
| 1009 | */ |
| 1010 | if ((last_state != priv->can.state) && |
| 1011 | (priv->devtype_data->quirks & FLEXCAN_QUIRK_BROKEN_PERR_STATE) && |
| 1012 | !(priv->can.ctrlmode & CAN_CTRLMODE_BERR_REPORTING)) { |
| 1013 | switch (priv->can.state) { |
| 1014 | case CAN_STATE_ERROR_ACTIVE: |
| 1015 | if (priv->devtype_data->quirks & |
| 1016 | FLEXCAN_QUIRK_BROKEN_WERR_STATE) |
| 1017 | flexcan_error_irq_enable(priv); |
| 1018 | else |
| 1019 | flexcan_error_irq_disable(priv); |
| 1020 | break; |
| 1021 | |
| 1022 | case CAN_STATE_ERROR_WARNING: |
| 1023 | flexcan_error_irq_enable(priv); |
| 1024 | break; |
| 1025 | |
| 1026 | case CAN_STATE_ERROR_PASSIVE: |
| 1027 | case CAN_STATE_BUS_OFF: |
| 1028 | flexcan_error_irq_disable(priv); |
| 1029 | break; |
| 1030 | |
| 1031 | default: |
| 1032 | break; |
| 1033 | } |
| 1034 | } |
| 1035 | |
Marc Kleine-Budde | dd2f122 | 2017-01-18 11:45:14 +0100 | [diff] [blame] | 1036 | return handled; |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 1037 | } |
| 1038 | |
| 1039 | static void flexcan_set_bittiming(struct net_device *dev) |
| 1040 | { |
| 1041 | const struct flexcan_priv *priv = netdev_priv(dev); |
| 1042 | const struct can_bittiming *bt = &priv->can.bittiming; |
Marc Kleine-Budde | 89af874 | 2015-05-08 09:32:58 +0200 | [diff] [blame] | 1043 | struct flexcan_regs __iomem *regs = priv->regs; |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 1044 | u32 reg; |
| 1045 | |
Pankaj Bansal | 88462d2 | 2017-11-24 18:52:08 +0530 | [diff] [blame] | 1046 | reg = priv->read(®s->ctrl); |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 1047 | reg &= ~(FLEXCAN_CTRL_PRESDIV(0xff) | |
| 1048 | FLEXCAN_CTRL_RJW(0x3) | |
| 1049 | FLEXCAN_CTRL_PSEG1(0x7) | |
| 1050 | FLEXCAN_CTRL_PSEG2(0x7) | |
| 1051 | FLEXCAN_CTRL_PROPSEG(0x7) | |
| 1052 | FLEXCAN_CTRL_LPB | |
| 1053 | FLEXCAN_CTRL_SMP | |
| 1054 | FLEXCAN_CTRL_LOM); |
| 1055 | |
| 1056 | reg |= FLEXCAN_CTRL_PRESDIV(bt->brp - 1) | |
| 1057 | FLEXCAN_CTRL_PSEG1(bt->phase_seg1 - 1) | |
| 1058 | FLEXCAN_CTRL_PSEG2(bt->phase_seg2 - 1) | |
| 1059 | FLEXCAN_CTRL_RJW(bt->sjw - 1) | |
| 1060 | FLEXCAN_CTRL_PROPSEG(bt->prop_seg - 1); |
| 1061 | |
| 1062 | if (priv->can.ctrlmode & CAN_CTRLMODE_LOOPBACK) |
| 1063 | reg |= FLEXCAN_CTRL_LPB; |
| 1064 | if (priv->can.ctrlmode & CAN_CTRLMODE_LISTENONLY) |
| 1065 | reg |= FLEXCAN_CTRL_LOM; |
| 1066 | if (priv->can.ctrlmode & CAN_CTRLMODE_3_SAMPLES) |
| 1067 | reg |= FLEXCAN_CTRL_SMP; |
| 1068 | |
Lucas Stach | 7a4b6c8 | 2015-08-07 17:16:03 +0200 | [diff] [blame] | 1069 | netdev_dbg(dev, "writing ctrl=0x%08x\n", reg); |
Pankaj Bansal | 88462d2 | 2017-11-24 18:52:08 +0530 | [diff] [blame] | 1070 | priv->write(reg, ®s->ctrl); |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 1071 | |
| 1072 | /* print chip status */ |
Wolfgang Grandegger | aabdfd6 | 2012-02-01 11:02:05 +0100 | [diff] [blame] | 1073 | netdev_dbg(dev, "%s: mcr=0x%08x ctrl=0x%08x\n", __func__, |
Pankaj Bansal | 88462d2 | 2017-11-24 18:52:08 +0530 | [diff] [blame] | 1074 | priv->read(®s->mcr), priv->read(®s->ctrl)); |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 1075 | } |
| 1076 | |
Marc Kleine-Budde | 0012e5c | 2015-08-06 14:53:57 +0200 | [diff] [blame] | 1077 | /* flexcan_chip_start |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 1078 | * |
| 1079 | * this functions is entered with clocks enabled |
| 1080 | * |
| 1081 | */ |
| 1082 | static int flexcan_chip_start(struct net_device *dev) |
| 1083 | { |
| 1084 | struct flexcan_priv *priv = netdev_priv(dev); |
Marc Kleine-Budde | 89af874 | 2015-05-08 09:32:58 +0200 | [diff] [blame] | 1085 | struct flexcan_regs __iomem *regs = priv->regs; |
Marc Kleine-Budde | 6f75fce | 2014-09-23 11:03:01 +0200 | [diff] [blame] | 1086 | u32 reg_mcr, reg_ctrl, reg_ctrl2, reg_mecr; |
Marc Kleine-Budde | 8ce5139 | 2019-03-01 12:17:30 +0100 | [diff] [blame] | 1087 | u64 reg_imask; |
David S. Miller | 1f6d803 | 2014-09-23 12:09:27 -0400 | [diff] [blame] | 1088 | int err, i; |
Pankaj Bansal | 0517961 | 2018-11-23 22:18:44 +0100 | [diff] [blame] | 1089 | struct flexcan_mb __iomem *mb; |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 1090 | |
| 1091 | /* enable module */ |
Marc Kleine-Budde | 9b00b30 | 2014-02-28 15:30:18 +0100 | [diff] [blame] | 1092 | err = flexcan_chip_enable(priv); |
| 1093 | if (err) |
| 1094 | return err; |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 1095 | |
| 1096 | /* soft reset */ |
Marc Kleine-Budde | 4b5b822 | 2014-02-28 15:16:59 +0100 | [diff] [blame] | 1097 | err = flexcan_chip_softreset(priv); |
| 1098 | if (err) |
Marc Kleine-Budde | b1aa1c7 | 2014-02-28 17:08:21 +0100 | [diff] [blame] | 1099 | goto out_chip_disable; |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 1100 | |
| 1101 | flexcan_set_bittiming(dev); |
| 1102 | |
Marc Kleine-Budde | 0012e5c | 2015-08-06 14:53:57 +0200 | [diff] [blame] | 1103 | /* MCR |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 1104 | * |
| 1105 | * enable freeze |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 1106 | * halt now |
| 1107 | * only supervisor access |
| 1108 | * enable warning int |
Marc Kleine-Budde | 4bd888a | 2015-08-31 21:03:29 +0200 | [diff] [blame] | 1109 | * enable individual RX masking |
Marc Kleine-Budde | 749de6f | 2015-08-31 21:32:34 +0200 | [diff] [blame] | 1110 | * choose format C |
| 1111 | * set max mailbox number |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 1112 | */ |
Pankaj Bansal | 88462d2 | 2017-11-24 18:52:08 +0530 | [diff] [blame] | 1113 | reg_mcr = priv->read(®s->mcr); |
Marc Kleine-Budde | d5a7b40 | 2013-10-04 10:52:36 +0200 | [diff] [blame] | 1114 | reg_mcr &= ~FLEXCAN_MCR_MAXMB(0xff); |
Marc Kleine-Budde | b3cf53e | 2015-09-01 09:00:13 +0200 | [diff] [blame] | 1115 | reg_mcr |= FLEXCAN_MCR_FRZ | FLEXCAN_MCR_HALT | FLEXCAN_MCR_SUPV | |
Pankaj Bansal | 7ad0f53 | 2018-08-13 23:50:48 +0530 | [diff] [blame] | 1116 | FLEXCAN_MCR_WRN_EN | FLEXCAN_MCR_IRMQ | FLEXCAN_MCR_IDAM_C | |
Pankaj Bansal | 0517961 | 2018-11-23 22:18:44 +0100 | [diff] [blame] | 1117 | FLEXCAN_MCR_MAXMB(priv->tx_mb_idx); |
Marc Kleine-Budde | b3cf53e | 2015-09-01 09:00:13 +0200 | [diff] [blame] | 1118 | |
Marc Kleine-Budde | c982a3ca | 2018-08-17 14:52:58 +0200 | [diff] [blame] | 1119 | /* MCR |
| 1120 | * |
| 1121 | * FIFO: |
| 1122 | * - disable for timestamp mode |
| 1123 | * - enable for FIFO mode |
| 1124 | */ |
Alexander Stein | cbffaf7 | 2018-10-11 17:01:25 +0200 | [diff] [blame] | 1125 | if (priv->devtype_data->quirks & FLEXCAN_QUIRK_USE_OFF_TIMESTAMP) |
Marc Kleine-Budde | b3cf53e | 2015-09-01 09:00:13 +0200 | [diff] [blame] | 1126 | reg_mcr &= ~FLEXCAN_MCR_FEN; |
Alexander Stein | cbffaf7 | 2018-10-11 17:01:25 +0200 | [diff] [blame] | 1127 | else |
| 1128 | reg_mcr |= FLEXCAN_MCR_FEN; |
| 1129 | |
Pankaj Bansal | 7ad0f53 | 2018-08-13 23:50:48 +0530 | [diff] [blame] | 1130 | /* MCR |
| 1131 | * |
| 1132 | * NOTE: In loopback mode, the CAN_MCR[SRXDIS] cannot be |
| 1133 | * asserted because this will impede the self reception |
| 1134 | * of a transmitted message. This is not documented in |
| 1135 | * earlier versions of flexcan block guide. |
| 1136 | * |
| 1137 | * Self Reception: |
| 1138 | * - enable Self Reception for loopback mode |
| 1139 | * (by clearing "Self Reception Disable" bit) |
| 1140 | * - disable for normal operation |
| 1141 | */ |
| 1142 | if (priv->can.ctrlmode & CAN_CTRLMODE_LOOPBACK) |
| 1143 | reg_mcr &= ~FLEXCAN_MCR_SRX_DIS; |
| 1144 | else |
| 1145 | reg_mcr |= FLEXCAN_MCR_SRX_DIS; |
| 1146 | |
Wolfgang Grandegger | aabdfd6 | 2012-02-01 11:02:05 +0100 | [diff] [blame] | 1147 | netdev_dbg(dev, "%s: writing mcr=0x%08x", __func__, reg_mcr); |
Pankaj Bansal | 88462d2 | 2017-11-24 18:52:08 +0530 | [diff] [blame] | 1148 | priv->write(reg_mcr, ®s->mcr); |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 1149 | |
Marc Kleine-Budde | 0012e5c | 2015-08-06 14:53:57 +0200 | [diff] [blame] | 1150 | /* CTRL |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 1151 | * |
| 1152 | * disable timer sync feature |
| 1153 | * |
| 1154 | * disable auto busoff recovery |
| 1155 | * transmit lowest buffer first |
| 1156 | * |
| 1157 | * enable tx and rx warning interrupt |
| 1158 | * enable bus off interrupt |
| 1159 | * (== FLEXCAN_CTRL_ERR_STATE) |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 1160 | */ |
Pankaj Bansal | 88462d2 | 2017-11-24 18:52:08 +0530 | [diff] [blame] | 1161 | reg_ctrl = priv->read(®s->ctrl); |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 1162 | reg_ctrl &= ~FLEXCAN_CTRL_TSYN; |
| 1163 | reg_ctrl |= FLEXCAN_CTRL_BOFF_REC | FLEXCAN_CTRL_LBUF | |
Wolfgang Grandegger | 4f72e5f | 2012-09-28 03:17:15 +0000 | [diff] [blame] | 1164 | FLEXCAN_CTRL_ERR_STATE; |
Marc Kleine-Budde | 0012e5c | 2015-08-06 14:53:57 +0200 | [diff] [blame] | 1165 | |
| 1166 | /* enable the "error interrupt" (FLEXCAN_CTRL_ERR_MSK), |
Wolfgang Grandegger | 4f72e5f | 2012-09-28 03:17:15 +0000 | [diff] [blame] | 1167 | * on most Flexcan cores, too. Otherwise we don't get |
| 1168 | * any error warning or passive interrupts. |
| 1169 | */ |
ZHU Yi (ST-FIR/ENG1-Zhu) | 2f8639b | 2017-09-15 07:01:23 +0000 | [diff] [blame] | 1170 | if (priv->devtype_data->quirks & FLEXCAN_QUIRK_BROKEN_WERR_STATE || |
Wolfgang Grandegger | 4f72e5f | 2012-09-28 03:17:15 +0000 | [diff] [blame] | 1171 | priv->can.ctrlmode & CAN_CTRLMODE_BERR_REPORTING) |
| 1172 | reg_ctrl |= FLEXCAN_CTRL_ERR_MSK; |
Alexander Stein | bc03a54 | 2014-08-12 10:47:21 +0200 | [diff] [blame] | 1173 | else |
| 1174 | reg_ctrl &= ~FLEXCAN_CTRL_ERR_MSK; |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 1175 | |
| 1176 | /* save for later use */ |
| 1177 | priv->reg_ctrl_default = reg_ctrl; |
Marc Kleine-Budde | 6fa7da2 | 2015-08-27 14:24:48 +0200 | [diff] [blame] | 1178 | /* leave interrupts disabled for now */ |
| 1179 | reg_ctrl &= ~FLEXCAN_CTRL_ERR_ALL; |
Wolfgang Grandegger | aabdfd6 | 2012-02-01 11:02:05 +0100 | [diff] [blame] | 1180 | netdev_dbg(dev, "%s: writing ctrl=0x%08x", __func__, reg_ctrl); |
Pankaj Bansal | 88462d2 | 2017-11-24 18:52:08 +0530 | [diff] [blame] | 1181 | priv->write(reg_ctrl, ®s->ctrl); |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 1182 | |
Marc Kleine-Budde | 9eb7aa8 | 2015-09-01 08:57:55 +0200 | [diff] [blame] | 1183 | if ((priv->devtype_data->quirks & FLEXCAN_QUIRK_ENABLE_EACEN_RRS)) { |
Pankaj Bansal | 88462d2 | 2017-11-24 18:52:08 +0530 | [diff] [blame] | 1184 | reg_ctrl2 = priv->read(®s->ctrl2); |
Marc Kleine-Budde | 9eb7aa8 | 2015-09-01 08:57:55 +0200 | [diff] [blame] | 1185 | reg_ctrl2 |= FLEXCAN_CTRL2_EACEN | FLEXCAN_CTRL2_RRS; |
Pankaj Bansal | 88462d2 | 2017-11-24 18:52:08 +0530 | [diff] [blame] | 1186 | priv->write(reg_ctrl2, ®s->ctrl2); |
Marc Kleine-Budde | 9eb7aa8 | 2015-09-01 08:57:55 +0200 | [diff] [blame] | 1187 | } |
| 1188 | |
Marc Kleine-Budde | b3cf53e | 2015-09-01 09:00:13 +0200 | [diff] [blame] | 1189 | if (priv->devtype_data->quirks & FLEXCAN_QUIRK_USE_OFF_TIMESTAMP) { |
Alexander Stein | cbffaf7 | 2018-10-11 17:01:25 +0200 | [diff] [blame] | 1190 | for (i = priv->offload.mb_first; i <= priv->offload.mb_last; i++) { |
Pankaj Bansal | 0517961 | 2018-11-23 22:18:44 +0100 | [diff] [blame] | 1191 | mb = flexcan_get_mb(priv, i); |
Pankaj Bansal | 88462d2 | 2017-11-24 18:52:08 +0530 | [diff] [blame] | 1192 | priv->write(FLEXCAN_MB_CODE_RX_EMPTY, |
Pankaj Bansal | 0517961 | 2018-11-23 22:18:44 +0100 | [diff] [blame] | 1193 | &mb->can_ctrl); |
Alexander Stein | cbffaf7 | 2018-10-11 17:01:25 +0200 | [diff] [blame] | 1194 | } |
| 1195 | } else { |
| 1196 | /* clear and invalidate unused mailboxes first */ |
Uwe Kleine-König | a55234d | 2019-01-11 12:20:41 +0100 | [diff] [blame] | 1197 | 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] | 1198 | mb = flexcan_get_mb(priv, i); |
Alexander Stein | cbffaf7 | 2018-10-11 17:01:25 +0200 | [diff] [blame] | 1199 | priv->write(FLEXCAN_MB_CODE_RX_INACTIVE, |
Pankaj Bansal | 0517961 | 2018-11-23 22:18:44 +0100 | [diff] [blame] | 1200 | &mb->can_ctrl); |
Alexander Stein | cbffaf7 | 2018-10-11 17:01:25 +0200 | [diff] [blame] | 1201 | } |
Marc Kleine-Budde | b3cf53e | 2015-09-01 09:00:13 +0200 | [diff] [blame] | 1202 | } |
| 1203 | |
David Jander | 25e9244 | 2014-09-03 16:47:22 +0200 | [diff] [blame] | 1204 | /* Errata ERR005829: mark first TX mailbox as INACTIVE */ |
Pankaj Bansal | 88462d2 | 2017-11-24 18:52:08 +0530 | [diff] [blame] | 1205 | priv->write(FLEXCAN_MB_CODE_TX_INACTIVE, |
| 1206 | &priv->tx_mb_reserved->can_ctrl); |
David Jander | 25e9244 | 2014-09-03 16:47:22 +0200 | [diff] [blame] | 1207 | |
Marc Kleine-Budde | c32fe4a | 2014-09-16 12:39:28 +0200 | [diff] [blame] | 1208 | /* mark TX mailbox as INACTIVE */ |
Pankaj Bansal | 88462d2 | 2017-11-24 18:52:08 +0530 | [diff] [blame] | 1209 | priv->write(FLEXCAN_MB_CODE_TX_INACTIVE, |
Pankaj Bansal | 0517961 | 2018-11-23 22:18:44 +0100 | [diff] [blame] | 1210 | &priv->tx_mb->can_ctrl); |
Marc Kleine-Budde | d5a7b40 | 2013-10-04 10:52:36 +0200 | [diff] [blame] | 1211 | |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 1212 | /* acceptance mask/acceptance code (accept everything) */ |
Pankaj Bansal | 88462d2 | 2017-11-24 18:52:08 +0530 | [diff] [blame] | 1213 | priv->write(0x0, ®s->rxgmask); |
| 1214 | priv->write(0x0, ®s->rx14mask); |
| 1215 | priv->write(0x0, ®s->rx15mask); |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 1216 | |
Marc Kleine-Budde | f377bff | 2015-05-08 15:22:36 +0200 | [diff] [blame] | 1217 | if (priv->devtype_data->quirks & FLEXCAN_QUIRK_DISABLE_RXFG) |
Pankaj Bansal | 88462d2 | 2017-11-24 18:52:08 +0530 | [diff] [blame] | 1218 | priv->write(0x0, ®s->rxfgmask); |
Hui Wang | 30c1e67 | 2012-06-28 16:21:35 +0800 | [diff] [blame] | 1219 | |
Marc Kleine-Budde | 4bd888a | 2015-08-31 21:03:29 +0200 | [diff] [blame] | 1220 | /* clear acceptance filters */ |
Pankaj Bansal | 0517961 | 2018-11-23 22:18:44 +0100 | [diff] [blame] | 1221 | for (i = 0; i < priv->mb_count; i++) |
Pankaj Bansal | 88462d2 | 2017-11-24 18:52:08 +0530 | [diff] [blame] | 1222 | priv->write(0, ®s->rximr[i]); |
Marc Kleine-Budde | 4bd888a | 2015-08-31 21:03:29 +0200 | [diff] [blame] | 1223 | |
Joakim Zhang | 15ef207 | 2020-04-16 17:31:25 +0800 | [diff] [blame^] | 1224 | /* On Vybrid, disable non-correctable errors interrupt and |
| 1225 | * freeze mode. It still can correct the correctable errors |
| 1226 | * when HW supports ECC. |
| 1227 | * |
| 1228 | * This also works around errata e5295 which generates false |
| 1229 | * positive memory errors and put the device in freeze mode. |
Stefan Agner | cdce844 | 2014-07-15 14:56:21 +0200 | [diff] [blame] | 1230 | */ |
Marc Kleine-Budde | f377bff | 2015-05-08 15:22:36 +0200 | [diff] [blame] | 1231 | if (priv->devtype_data->quirks & FLEXCAN_QUIRK_DISABLE_MECR) { |
Marc Kleine-Budde | 0012e5c | 2015-08-06 14:53:57 +0200 | [diff] [blame] | 1232 | /* Follow the protocol as described in "Detection |
Stefan Agner | cdce844 | 2014-07-15 14:56:21 +0200 | [diff] [blame] | 1233 | * and Correction of Memory Errors" to write to |
Joakim Zhang | 15ef207 | 2020-04-16 17:31:25 +0800 | [diff] [blame^] | 1234 | * MECR register (step 1 - 5) |
| 1235 | * |
| 1236 | * 1. By default, CTRL2[ECRWRE] = 0, MECR[ECRWRDIS] = 1 |
| 1237 | * 2. set CTRL2[ECRWRE] |
Stefan Agner | cdce844 | 2014-07-15 14:56:21 +0200 | [diff] [blame] | 1238 | */ |
Pankaj Bansal | 88462d2 | 2017-11-24 18:52:08 +0530 | [diff] [blame] | 1239 | reg_ctrl2 = priv->read(®s->ctrl2); |
Marc Kleine-Budde | 6f75fce | 2014-09-23 11:03:01 +0200 | [diff] [blame] | 1240 | reg_ctrl2 |= FLEXCAN_CTRL2_ECRWRE; |
Pankaj Bansal | 88462d2 | 2017-11-24 18:52:08 +0530 | [diff] [blame] | 1241 | priv->write(reg_ctrl2, ®s->ctrl2); |
Stefan Agner | cdce844 | 2014-07-15 14:56:21 +0200 | [diff] [blame] | 1242 | |
Joakim Zhang | 15ef207 | 2020-04-16 17:31:25 +0800 | [diff] [blame^] | 1243 | /* 3. clear MECR[ECRWRDIS] */ |
Pankaj Bansal | 88462d2 | 2017-11-24 18:52:08 +0530 | [diff] [blame] | 1244 | reg_mecr = priv->read(®s->mecr); |
Stefan Agner | cdce844 | 2014-07-15 14:56:21 +0200 | [diff] [blame] | 1245 | reg_mecr &= ~FLEXCAN_MECR_ECRWRDIS; |
Pankaj Bansal | 88462d2 | 2017-11-24 18:52:08 +0530 | [diff] [blame] | 1246 | priv->write(reg_mecr, ®s->mecr); |
Joakim Zhang | 15ef207 | 2020-04-16 17:31:25 +0800 | [diff] [blame^] | 1247 | |
| 1248 | /* 4. all writes to MECR must keep MECR[ECRWRDIS] cleared */ |
Stefan Agner | cdce844 | 2014-07-15 14:56:21 +0200 | [diff] [blame] | 1249 | reg_mecr &= ~(FLEXCAN_MECR_NCEFAFRZ | FLEXCAN_MECR_HANCEI_MSK | |
Marc Kleine-Budde | 0012e5c | 2015-08-06 14:53:57 +0200 | [diff] [blame] | 1250 | FLEXCAN_MECR_FANCEI_MSK); |
Pankaj Bansal | 88462d2 | 2017-11-24 18:52:08 +0530 | [diff] [blame] | 1251 | priv->write(reg_mecr, ®s->mecr); |
Joakim Zhang | 15ef207 | 2020-04-16 17:31:25 +0800 | [diff] [blame^] | 1252 | |
| 1253 | /* 5. after configuration done, lock MECR by either |
| 1254 | * setting MECR[ECRWRDIS] or clearing CTRL2[ECRWRE] |
| 1255 | */ |
| 1256 | reg_mecr |= FLEXCAN_MECR_ECRWRDIS; |
| 1257 | priv->write(reg_mecr, ®s->mecr); |
| 1258 | |
| 1259 | reg_ctrl2 &= ~FLEXCAN_CTRL2_ECRWRE; |
| 1260 | priv->write(reg_ctrl2, ®s->ctrl2); |
Stefan Agner | cdce844 | 2014-07-15 14:56:21 +0200 | [diff] [blame] | 1261 | } |
| 1262 | |
Marc Kleine-Budde | f003698 | 2014-02-28 17:18:27 +0100 | [diff] [blame] | 1263 | err = flexcan_transceiver_enable(priv); |
| 1264 | if (err) |
Marc Kleine-Budde | b1aa1c7 | 2014-02-28 17:08:21 +0100 | [diff] [blame] | 1265 | goto out_chip_disable; |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 1266 | |
| 1267 | /* synchronize with the can bus */ |
Marc Kleine-Budde | b1aa1c7 | 2014-02-28 17:08:21 +0100 | [diff] [blame] | 1268 | err = flexcan_chip_unfreeze(priv); |
| 1269 | if (err) |
| 1270 | goto out_transceiver_disable; |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 1271 | |
| 1272 | priv->can.state = CAN_STATE_ERROR_ACTIVE; |
| 1273 | |
Marc Kleine-Budde | 6fa7da2 | 2015-08-27 14:24:48 +0200 | [diff] [blame] | 1274 | /* enable interrupts atomically */ |
| 1275 | disable_irq(dev->irq); |
Pankaj Bansal | 88462d2 | 2017-11-24 18:52:08 +0530 | [diff] [blame] | 1276 | priv->write(priv->reg_ctrl_default, ®s->ctrl); |
Marc Kleine-Budde | 0ca64f02 | 2019-03-01 13:54:19 +0100 | [diff] [blame] | 1277 | reg_imask = priv->rx_mask | priv->tx_mask; |
Marc Kleine-Budde | 8ce5139 | 2019-03-01 12:17:30 +0100 | [diff] [blame] | 1278 | priv->write(upper_32_bits(reg_imask), ®s->imask2); |
| 1279 | priv->write(lower_32_bits(reg_imask), ®s->imask1); |
Marc Kleine-Budde | 6fa7da2 | 2015-08-27 14:24:48 +0200 | [diff] [blame] | 1280 | enable_irq(dev->irq); |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 1281 | |
| 1282 | /* print chip status */ |
Wolfgang Grandegger | aabdfd6 | 2012-02-01 11:02:05 +0100 | [diff] [blame] | 1283 | 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] | 1284 | priv->read(®s->mcr), priv->read(®s->ctrl)); |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 1285 | |
| 1286 | return 0; |
| 1287 | |
Marc Kleine-Budde | b1aa1c7 | 2014-02-28 17:08:21 +0100 | [diff] [blame] | 1288 | out_transceiver_disable: |
| 1289 | flexcan_transceiver_disable(priv); |
| 1290 | out_chip_disable: |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 1291 | flexcan_chip_disable(priv); |
| 1292 | return err; |
| 1293 | } |
| 1294 | |
Marc Kleine-Budde | 0012e5c | 2015-08-06 14:53:57 +0200 | [diff] [blame] | 1295 | /* flexcan_chip_stop |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 1296 | * |
| 1297 | * this functions is entered with clocks enabled |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 1298 | */ |
| 1299 | static void flexcan_chip_stop(struct net_device *dev) |
| 1300 | { |
| 1301 | struct flexcan_priv *priv = netdev_priv(dev); |
Marc Kleine-Budde | 89af874 | 2015-05-08 09:32:58 +0200 | [diff] [blame] | 1302 | struct flexcan_regs __iomem *regs = priv->regs; |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 1303 | |
Marc Kleine-Budde | b1aa1c7 | 2014-02-28 17:08:21 +0100 | [diff] [blame] | 1304 | /* freeze + disable module */ |
| 1305 | flexcan_chip_freeze(priv); |
| 1306 | flexcan_chip_disable(priv); |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 1307 | |
Marc Kleine-Budde | 5be93bd | 2014-02-19 12:00:51 +0100 | [diff] [blame] | 1308 | /* Disable all interrupts */ |
Pankaj Bansal | 88462d2 | 2017-11-24 18:52:08 +0530 | [diff] [blame] | 1309 | priv->write(0, ®s->imask2); |
| 1310 | priv->write(0, ®s->imask1); |
| 1311 | priv->write(priv->reg_ctrl_default & ~FLEXCAN_CTRL_ERR_ALL, |
| 1312 | ®s->ctrl); |
Marc Kleine-Budde | 5be93bd | 2014-02-19 12:00:51 +0100 | [diff] [blame] | 1313 | |
Marc Kleine-Budde | f003698 | 2014-02-28 17:18:27 +0100 | [diff] [blame] | 1314 | flexcan_transceiver_disable(priv); |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 1315 | priv->can.state = CAN_STATE_STOPPED; |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 1316 | } |
| 1317 | |
| 1318 | static int flexcan_open(struct net_device *dev) |
| 1319 | { |
| 1320 | struct flexcan_priv *priv = netdev_priv(dev); |
| 1321 | int err; |
| 1322 | |
Aisheng Dong | ca10989 | 2018-11-30 08:53:26 +0000 | [diff] [blame] | 1323 | err = pm_runtime_get_sync(priv->dev); |
| 1324 | if (err < 0) |
Fabio Estevam | aa10181 | 2013-07-22 12:41:40 -0300 | [diff] [blame] | 1325 | return err; |
| 1326 | |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 1327 | err = open_candev(dev); |
| 1328 | if (err) |
Aisheng Dong | ca10989 | 2018-11-30 08:53:26 +0000 | [diff] [blame] | 1329 | goto out_runtime_put; |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 1330 | |
| 1331 | err = request_irq(dev->irq, flexcan_irq, IRQF_SHARED, dev->name, dev); |
| 1332 | if (err) |
| 1333 | goto out_close; |
| 1334 | |
Pankaj Bansal | 0517961 | 2018-11-23 22:18:44 +0100 | [diff] [blame] | 1335 | priv->mb_size = sizeof(struct flexcan_mb) + CAN_MAX_DLEN; |
Pankaj Bansal | 6cbf760 | 2018-08-28 23:19:12 +0530 | [diff] [blame] | 1336 | priv->mb_count = (sizeof(priv->regs->mb[0]) / priv->mb_size) + |
| 1337 | (sizeof(priv->regs->mb[1]) / priv->mb_size); |
Pankaj Bansal | 0517961 | 2018-11-23 22:18:44 +0100 | [diff] [blame] | 1338 | |
Pankaj Bansal | 5156c7b | 2018-08-13 23:50:49 +0530 | [diff] [blame] | 1339 | if (priv->devtype_data->quirks & FLEXCAN_QUIRK_USE_OFF_TIMESTAMP) |
Pankaj Bansal | 0517961 | 2018-11-23 22:18:44 +0100 | [diff] [blame] | 1340 | priv->tx_mb_reserved = |
| 1341 | flexcan_get_mb(priv, FLEXCAN_TX_MB_RESERVED_OFF_TIMESTAMP); |
Pankaj Bansal | 5156c7b | 2018-08-13 23:50:49 +0530 | [diff] [blame] | 1342 | else |
Pankaj Bansal | 0517961 | 2018-11-23 22:18:44 +0100 | [diff] [blame] | 1343 | priv->tx_mb_reserved = |
| 1344 | flexcan_get_mb(priv, FLEXCAN_TX_MB_RESERVED_OFF_FIFO); |
| 1345 | priv->tx_mb_idx = priv->mb_count - 1; |
| 1346 | priv->tx_mb = flexcan_get_mb(priv, priv->tx_mb_idx); |
Marc Kleine-Budde | 0ca64f02 | 2019-03-01 13:54:19 +0100 | [diff] [blame] | 1347 | priv->tx_mask = FLEXCAN_IFLAG_MB(priv->tx_mb_idx); |
Pankaj Bansal | 5156c7b | 2018-08-13 23:50:49 +0530 | [diff] [blame] | 1348 | |
Pankaj Bansal | 5156c7b | 2018-08-13 23:50:49 +0530 | [diff] [blame] | 1349 | priv->offload.mailbox_read = flexcan_mailbox_read; |
| 1350 | |
| 1351 | if (priv->devtype_data->quirks & FLEXCAN_QUIRK_USE_OFF_TIMESTAMP) { |
Pankaj Bansal | 5156c7b | 2018-08-13 23:50:49 +0530 | [diff] [blame] | 1352 | priv->offload.mb_first = FLEXCAN_RX_MB_OFF_TIMESTAMP_FIRST; |
Pankaj Bansal | 0517961 | 2018-11-23 22:18:44 +0100 | [diff] [blame] | 1353 | priv->offload.mb_last = priv->mb_count - 2; |
Pankaj Bansal | 5156c7b | 2018-08-13 23:50:49 +0530 | [diff] [blame] | 1354 | |
Marc Kleine-Budde | 8ce5139 | 2019-03-01 12:17:30 +0100 | [diff] [blame] | 1355 | priv->rx_mask = GENMASK_ULL(priv->offload.mb_last, |
| 1356 | priv->offload.mb_first); |
Pankaj Bansal | 5156c7b | 2018-08-13 23:50:49 +0530 | [diff] [blame] | 1357 | err = can_rx_offload_add_timestamp(dev, &priv->offload); |
| 1358 | } else { |
Marc Kleine-Budde | 8ce5139 | 2019-03-01 12:17:30 +0100 | [diff] [blame] | 1359 | priv->rx_mask = FLEXCAN_IFLAG_RX_FIFO_OVERFLOW | |
Pankaj Bansal | 5156c7b | 2018-08-13 23:50:49 +0530 | [diff] [blame] | 1360 | FLEXCAN_IFLAG_RX_FIFO_AVAILABLE; |
| 1361 | err = can_rx_offload_add_fifo(dev, &priv->offload, |
| 1362 | FLEXCAN_NAPI_WEIGHT); |
| 1363 | } |
| 1364 | if (err) |
| 1365 | goto out_free_irq; |
| 1366 | |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 1367 | /* start chip and queuing */ |
| 1368 | err = flexcan_chip_start(dev); |
| 1369 | if (err) |
Pankaj Bansal | 5156c7b | 2018-08-13 23:50:49 +0530 | [diff] [blame] | 1370 | goto out_offload_del; |
Fabio Baltieri | adccadb | 2012-12-18 18:50:58 +0100 | [diff] [blame] | 1371 | |
| 1372 | can_led_event(dev, CAN_LED_EVENT_OPEN); |
| 1373 | |
Marc Kleine-Budde | 3016475 | 2015-05-10 15:26:58 +0200 | [diff] [blame] | 1374 | can_rx_offload_enable(&priv->offload); |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 1375 | netif_start_queue(dev); |
| 1376 | |
| 1377 | return 0; |
| 1378 | |
Pankaj Bansal | 5156c7b | 2018-08-13 23:50:49 +0530 | [diff] [blame] | 1379 | out_offload_del: |
| 1380 | can_rx_offload_del(&priv->offload); |
Marc Kleine-Budde | 7e9e148 | 2014-02-28 14:52:01 +0100 | [diff] [blame] | 1381 | out_free_irq: |
| 1382 | free_irq(dev->irq, dev); |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 1383 | out_close: |
| 1384 | close_candev(dev); |
Aisheng Dong | ca10989 | 2018-11-30 08:53:26 +0000 | [diff] [blame] | 1385 | out_runtime_put: |
| 1386 | pm_runtime_put(priv->dev); |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 1387 | |
| 1388 | return err; |
| 1389 | } |
| 1390 | |
| 1391 | static int flexcan_close(struct net_device *dev) |
| 1392 | { |
| 1393 | struct flexcan_priv *priv = netdev_priv(dev); |
| 1394 | |
| 1395 | netif_stop_queue(dev); |
Marc Kleine-Budde | 3016475 | 2015-05-10 15:26:58 +0200 | [diff] [blame] | 1396 | can_rx_offload_disable(&priv->offload); |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 1397 | flexcan_chip_stop(dev); |
| 1398 | |
Pankaj Bansal | 5156c7b | 2018-08-13 23:50:49 +0530 | [diff] [blame] | 1399 | can_rx_offload_del(&priv->offload); |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 1400 | free_irq(dev->irq, dev); |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 1401 | |
| 1402 | close_candev(dev); |
Aisheng Dong | ca10989 | 2018-11-30 08:53:26 +0000 | [diff] [blame] | 1403 | pm_runtime_put(priv->dev); |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 1404 | |
Fabio Baltieri | adccadb | 2012-12-18 18:50:58 +0100 | [diff] [blame] | 1405 | can_led_event(dev, CAN_LED_EVENT_STOP); |
| 1406 | |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 1407 | return 0; |
| 1408 | } |
| 1409 | |
| 1410 | static int flexcan_set_mode(struct net_device *dev, enum can_mode mode) |
| 1411 | { |
| 1412 | int err; |
| 1413 | |
| 1414 | switch (mode) { |
| 1415 | case CAN_MODE_START: |
| 1416 | err = flexcan_chip_start(dev); |
| 1417 | if (err) |
| 1418 | return err; |
| 1419 | |
| 1420 | netif_wake_queue(dev); |
| 1421 | break; |
| 1422 | |
| 1423 | default: |
| 1424 | return -EOPNOTSUPP; |
| 1425 | } |
| 1426 | |
| 1427 | return 0; |
| 1428 | } |
| 1429 | |
| 1430 | static const struct net_device_ops flexcan_netdev_ops = { |
| 1431 | .ndo_open = flexcan_open, |
| 1432 | .ndo_stop = flexcan_close, |
| 1433 | .ndo_start_xmit = flexcan_start_xmit, |
Oliver Hartkopp | c971fa2 | 2014-03-07 09:23:41 +0100 | [diff] [blame] | 1434 | .ndo_change_mtu = can_change_mtu, |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 1435 | }; |
| 1436 | |
Bill Pemberton | 3c8ac0f | 2012-12-03 09:22:44 -0500 | [diff] [blame] | 1437 | static int register_flexcandev(struct net_device *dev) |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 1438 | { |
| 1439 | struct flexcan_priv *priv = netdev_priv(dev); |
Marc Kleine-Budde | 89af874 | 2015-05-08 09:32:58 +0200 | [diff] [blame] | 1440 | struct flexcan_regs __iomem *regs = priv->regs; |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 1441 | u32 reg, err; |
| 1442 | |
Aisheng Dong | ca10989 | 2018-11-30 08:53:26 +0000 | [diff] [blame] | 1443 | err = flexcan_clks_enable(priv); |
Fabio Estevam | aa10181 | 2013-07-22 12:41:40 -0300 | [diff] [blame] | 1444 | if (err) |
| 1445 | return err; |
| 1446 | |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 1447 | /* select "bus clock", chip must be disabled */ |
Marc Kleine-Budde | 9b00b30 | 2014-02-28 15:30:18 +0100 | [diff] [blame] | 1448 | err = flexcan_chip_disable(priv); |
| 1449 | if (err) |
Aisheng Dong | ca10989 | 2018-11-30 08:53:26 +0000 | [diff] [blame] | 1450 | goto out_clks_disable; |
| 1451 | |
Pankaj Bansal | 88462d2 | 2017-11-24 18:52:08 +0530 | [diff] [blame] | 1452 | reg = priv->read(®s->ctrl); |
Dong Aisheng | 8c306be | 2018-12-13 07:08:00 +0000 | [diff] [blame] | 1453 | if (priv->clk_src) |
| 1454 | reg |= FLEXCAN_CTRL_CLK_SRC; |
| 1455 | else |
| 1456 | reg &= ~FLEXCAN_CTRL_CLK_SRC; |
Pankaj Bansal | 88462d2 | 2017-11-24 18:52:08 +0530 | [diff] [blame] | 1457 | priv->write(reg, ®s->ctrl); |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 1458 | |
Marc Kleine-Budde | 9b00b30 | 2014-02-28 15:30:18 +0100 | [diff] [blame] | 1459 | err = flexcan_chip_enable(priv); |
| 1460 | if (err) |
| 1461 | goto out_chip_disable; |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 1462 | |
| 1463 | /* set freeze, halt and activate FIFO, restrict register access */ |
Pankaj Bansal | 88462d2 | 2017-11-24 18:52:08 +0530 | [diff] [blame] | 1464 | reg = priv->read(®s->mcr); |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 1465 | reg |= FLEXCAN_MCR_FRZ | FLEXCAN_MCR_HALT | |
| 1466 | FLEXCAN_MCR_FEN | FLEXCAN_MCR_SUPV; |
Pankaj Bansal | 88462d2 | 2017-11-24 18:52:08 +0530 | [diff] [blame] | 1467 | priv->write(reg, ®s->mcr); |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 1468 | |
Marc Kleine-Budde | 0012e5c | 2015-08-06 14:53:57 +0200 | [diff] [blame] | 1469 | /* Currently we only support newer versions of this core |
Marc Kleine-Budde | b3cf53e | 2015-09-01 09:00:13 +0200 | [diff] [blame] | 1470 | * featuring a RX hardware FIFO (although this driver doesn't |
| 1471 | * make use of it on some cores). Older cores, found on some |
| 1472 | * Coldfire derivates are not tested. |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 1473 | */ |
Pankaj Bansal | 88462d2 | 2017-11-24 18:52:08 +0530 | [diff] [blame] | 1474 | reg = priv->read(®s->mcr); |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 1475 | if (!(reg & FLEXCAN_MCR_FEN)) { |
Wolfgang Grandegger | aabdfd6 | 2012-02-01 11:02:05 +0100 | [diff] [blame] | 1476 | netdev_err(dev, "Could not enable RX FIFO, unsupported core\n"); |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 1477 | err = -ENODEV; |
Marc Kleine-Budde | 9b00b30 | 2014-02-28 15:30:18 +0100 | [diff] [blame] | 1478 | goto out_chip_disable; |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 1479 | } |
| 1480 | |
| 1481 | err = register_candev(dev); |
Aisheng Dong | ca10989 | 2018-11-30 08:53:26 +0000 | [diff] [blame] | 1482 | if (err) |
| 1483 | goto out_chip_disable; |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 1484 | |
Aisheng Dong | ca10989 | 2018-11-30 08:53:26 +0000 | [diff] [blame] | 1485 | /* Disable core and let pm_runtime_put() disable the clocks. |
| 1486 | * If CONFIG_PM is not enabled, the clocks will stay powered. |
| 1487 | */ |
| 1488 | flexcan_chip_disable(priv); |
| 1489 | pm_runtime_put(priv->dev); |
| 1490 | |
| 1491 | return 0; |
| 1492 | |
Marc Kleine-Budde | 9b00b30 | 2014-02-28 15:30:18 +0100 | [diff] [blame] | 1493 | out_chip_disable: |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 1494 | flexcan_chip_disable(priv); |
Aisheng Dong | ca10989 | 2018-11-30 08:53:26 +0000 | [diff] [blame] | 1495 | out_clks_disable: |
| 1496 | flexcan_clks_disable(priv); |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 1497 | return err; |
| 1498 | } |
| 1499 | |
Bill Pemberton | 3c8ac0f | 2012-12-03 09:22:44 -0500 | [diff] [blame] | 1500 | static void unregister_flexcandev(struct net_device *dev) |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 1501 | { |
| 1502 | unregister_candev(dev); |
| 1503 | } |
| 1504 | |
Aisheng Dong | de3578c | 2018-11-23 08:35:33 +0000 | [diff] [blame] | 1505 | static int flexcan_setup_stop_mode(struct platform_device *pdev) |
| 1506 | { |
| 1507 | struct net_device *dev = platform_get_drvdata(pdev); |
| 1508 | struct device_node *np = pdev->dev.of_node; |
| 1509 | struct device_node *gpr_np; |
| 1510 | struct flexcan_priv *priv; |
| 1511 | phandle phandle; |
| 1512 | u32 out_val[5]; |
| 1513 | int ret; |
| 1514 | |
| 1515 | if (!np) |
| 1516 | return -EINVAL; |
| 1517 | |
| 1518 | /* stop mode property format is: |
| 1519 | * <&gpr req_gpr req_bit ack_gpr ack_bit>. |
| 1520 | */ |
| 1521 | ret = of_property_read_u32_array(np, "fsl,stop-mode", out_val, |
| 1522 | ARRAY_SIZE(out_val)); |
| 1523 | if (ret) { |
| 1524 | dev_dbg(&pdev->dev, "no stop-mode property\n"); |
| 1525 | return ret; |
| 1526 | } |
| 1527 | phandle = *out_val; |
| 1528 | |
| 1529 | gpr_np = of_find_node_by_phandle(phandle); |
| 1530 | if (!gpr_np) { |
| 1531 | dev_dbg(&pdev->dev, "could not find gpr node by phandle\n"); |
YueHaibing | 7873e98 | 2018-12-12 17:24:01 +0800 | [diff] [blame] | 1532 | return -ENODEV; |
Aisheng Dong | de3578c | 2018-11-23 08:35:33 +0000 | [diff] [blame] | 1533 | } |
| 1534 | |
| 1535 | priv = netdev_priv(dev); |
| 1536 | priv->stm.gpr = syscon_node_to_regmap(gpr_np); |
Aisheng Dong | de3578c | 2018-11-23 08:35:33 +0000 | [diff] [blame] | 1537 | if (IS_ERR(priv->stm.gpr)) { |
| 1538 | dev_dbg(&pdev->dev, "could not find gpr regmap\n"); |
Wen Yang | e9f2a85 | 2019-07-06 11:37:20 +0800 | [diff] [blame] | 1539 | ret = PTR_ERR(priv->stm.gpr); |
| 1540 | goto out_put_node; |
Aisheng Dong | de3578c | 2018-11-23 08:35:33 +0000 | [diff] [blame] | 1541 | } |
| 1542 | |
| 1543 | priv->stm.req_gpr = out_val[1]; |
| 1544 | priv->stm.req_bit = out_val[2]; |
| 1545 | priv->stm.ack_gpr = out_val[3]; |
| 1546 | priv->stm.ack_bit = out_val[4]; |
| 1547 | |
| 1548 | dev_dbg(&pdev->dev, |
| 1549 | "gpr %s req_gpr=0x02%x req_bit=%u ack_gpr=0x02%x ack_bit=%u\n", |
| 1550 | gpr_np->full_name, priv->stm.req_gpr, priv->stm.req_bit, |
| 1551 | priv->stm.ack_gpr, priv->stm.ack_bit); |
| 1552 | |
| 1553 | device_set_wakeup_capable(&pdev->dev, true); |
| 1554 | |
Sean Nyekjaer | 915f966 | 2019-04-09 10:39:48 +0200 | [diff] [blame] | 1555 | if (of_property_read_bool(np, "wakeup-source")) |
| 1556 | device_set_wakeup_enable(&pdev->dev, true); |
| 1557 | |
Aisheng Dong | de3578c | 2018-11-23 08:35:33 +0000 | [diff] [blame] | 1558 | return 0; |
David S. Miller | 13dfb3f | 2019-08-06 18:44:57 -0700 | [diff] [blame] | 1559 | |
Wen Yang | e9f2a85 | 2019-07-06 11:37:20 +0800 | [diff] [blame] | 1560 | out_put_node: |
| 1561 | of_node_put(gpr_np); |
| 1562 | return ret; |
Aisheng Dong | de3578c | 2018-11-23 08:35:33 +0000 | [diff] [blame] | 1563 | } |
| 1564 | |
Hui Wang | 30c1e67 | 2012-06-28 16:21:35 +0800 | [diff] [blame] | 1565 | static const struct of_device_id flexcan_of_match[] = { |
Hui Wang | 30c1e67 | 2012-06-28 16:21:35 +0800 | [diff] [blame] | 1566 | { .compatible = "fsl,imx6q-flexcan", .data = &fsl_imx6q_devtype_data, }, |
Marc Kleine-Budde | e358784 | 2013-10-03 23:51:55 +0200 | [diff] [blame] | 1567 | { .compatible = "fsl,imx28-flexcan", .data = &fsl_imx28_devtype_data, }, |
Uwe Kleine-König | 0e030a3 | 2018-04-25 16:50:39 +0200 | [diff] [blame] | 1568 | { .compatible = "fsl,imx53-flexcan", .data = &fsl_imx25_devtype_data, }, |
| 1569 | { .compatible = "fsl,imx35-flexcan", .data = &fsl_imx25_devtype_data, }, |
| 1570 | { .compatible = "fsl,imx25-flexcan", .data = &fsl_imx25_devtype_data, }, |
Marc Kleine-Budde | e358784 | 2013-10-03 23:51:55 +0200 | [diff] [blame] | 1571 | { .compatible = "fsl,p1010-flexcan", .data = &fsl_p1010_devtype_data, }, |
Stefan Agner | cdce844 | 2014-07-15 14:56:21 +0200 | [diff] [blame] | 1572 | { .compatible = "fsl,vf610-flexcan", .data = &fsl_vf610_devtype_data, }, |
Pankaj Bansal | 99b7668 | 2017-11-24 18:52:09 +0530 | [diff] [blame] | 1573 | { .compatible = "fsl,ls1021ar2-flexcan", .data = &fsl_ls1021a_r2_devtype_data, }, |
Hui Wang | 30c1e67 | 2012-06-28 16:21:35 +0800 | [diff] [blame] | 1574 | { /* sentinel */ }, |
| 1575 | }; |
Marc Kleine-Budde | 4358a9d | 2012-10-04 10:55:35 +0200 | [diff] [blame] | 1576 | MODULE_DEVICE_TABLE(of, flexcan_of_match); |
Hui Wang | 30c1e67 | 2012-06-28 16:21:35 +0800 | [diff] [blame] | 1577 | |
| 1578 | static const struct platform_device_id flexcan_id_table[] = { |
| 1579 | { .name = "flexcan", .driver_data = (kernel_ulong_t)&fsl_p1010_devtype_data, }, |
| 1580 | { /* sentinel */ }, |
| 1581 | }; |
Marc Kleine-Budde | 4358a9d | 2012-10-04 10:55:35 +0200 | [diff] [blame] | 1582 | MODULE_DEVICE_TABLE(platform, flexcan_id_table); |
Hui Wang | 30c1e67 | 2012-06-28 16:21:35 +0800 | [diff] [blame] | 1583 | |
Bill Pemberton | 3c8ac0f | 2012-12-03 09:22:44 -0500 | [diff] [blame] | 1584 | static int flexcan_probe(struct platform_device *pdev) |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 1585 | { |
Hui Wang | 30c1e67 | 2012-06-28 16:21:35 +0800 | [diff] [blame] | 1586 | const struct of_device_id *of_id; |
Marc Kleine-Budde | dda0b3b | 2012-07-13 14:52:48 +0200 | [diff] [blame] | 1587 | const struct flexcan_devtype_data *devtype_data; |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 1588 | struct net_device *dev; |
| 1589 | struct flexcan_priv *priv; |
Andreas Werner | 555828e | 2015-03-22 17:35:52 +0100 | [diff] [blame] | 1590 | struct regulator *reg_xceiver; |
Steffen Trumtrar | 3d42a37 | 2012-07-17 16:14:34 +0200 | [diff] [blame] | 1591 | struct clk *clk_ipg = NULL, *clk_per = NULL; |
Marc Kleine-Budde | 89af874 | 2015-05-08 09:32:58 +0200 | [diff] [blame] | 1592 | struct flexcan_regs __iomem *regs; |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 1593 | int err, irq; |
Dong Aisheng | 8c306be | 2018-12-13 07:08:00 +0000 | [diff] [blame] | 1594 | u8 clk_src = 1; |
holt@sgi.com | 97efe9a | 2011-08-16 17:32:23 +0000 | [diff] [blame] | 1595 | u32 clock_freq = 0; |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 1596 | |
Marc Kleine-Budde | 3d60f33 | 2020-09-22 16:44:16 +0200 | [diff] [blame] | 1597 | reg_xceiver = devm_regulator_get_optional(&pdev->dev, "xceiver"); |
Andreas Werner | 555828e | 2015-03-22 17:35:52 +0100 | [diff] [blame] | 1598 | if (PTR_ERR(reg_xceiver) == -EPROBE_DEFER) |
| 1599 | return -EPROBE_DEFER; |
Marc Kleine-Budde | 3d60f33 | 2020-09-22 16:44:16 +0200 | [diff] [blame] | 1600 | else if (PTR_ERR(reg_xceiver) == -ENODEV) |
Andreas Werner | 555828e | 2015-03-22 17:35:52 +0100 | [diff] [blame] | 1601 | reg_xceiver = NULL; |
Marc Kleine-Budde | 3d60f33 | 2020-09-22 16:44:16 +0200 | [diff] [blame] | 1602 | else if (IS_ERR(reg_xceiver)) |
| 1603 | return PTR_ERR(reg_xceiver); |
Andreas Werner | 555828e | 2015-03-22 17:35:52 +0100 | [diff] [blame] | 1604 | |
Dong Aisheng | 8c306be | 2018-12-13 07:08:00 +0000 | [diff] [blame] | 1605 | if (pdev->dev.of_node) { |
Hui Wang | afc016d | 2012-06-28 16:21:34 +0800 | [diff] [blame] | 1606 | of_property_read_u32(pdev->dev.of_node, |
Marc Kleine-Budde | 0012e5c | 2015-08-06 14:53:57 +0200 | [diff] [blame] | 1607 | "clock-frequency", &clock_freq); |
Dong Aisheng | 8c306be | 2018-12-13 07:08:00 +0000 | [diff] [blame] | 1608 | of_property_read_u8(pdev->dev.of_node, |
| 1609 | "fsl,clk-source", &clk_src); |
| 1610 | } |
holt@sgi.com | 97efe9a | 2011-08-16 17:32:23 +0000 | [diff] [blame] | 1611 | |
| 1612 | if (!clock_freq) { |
Steffen Trumtrar | 3d42a37 | 2012-07-17 16:14:34 +0200 | [diff] [blame] | 1613 | clk_ipg = devm_clk_get(&pdev->dev, "ipg"); |
| 1614 | if (IS_ERR(clk_ipg)) { |
| 1615 | dev_err(&pdev->dev, "no ipg clock defined\n"); |
Fabio Estevam | 933e4af | 2013-07-22 12:41:39 -0300 | [diff] [blame] | 1616 | return PTR_ERR(clk_ipg); |
holt@sgi.com | 97efe9a | 2011-08-16 17:32:23 +0000 | [diff] [blame] | 1617 | } |
Steffen Trumtrar | 3d42a37 | 2012-07-17 16:14:34 +0200 | [diff] [blame] | 1618 | |
| 1619 | clk_per = devm_clk_get(&pdev->dev, "per"); |
| 1620 | if (IS_ERR(clk_per)) { |
| 1621 | dev_err(&pdev->dev, "no per clock defined\n"); |
Fabio Estevam | 933e4af | 2013-07-22 12:41:39 -0300 | [diff] [blame] | 1622 | return PTR_ERR(clk_per); |
Steffen Trumtrar | 3d42a37 | 2012-07-17 16:14:34 +0200 | [diff] [blame] | 1623 | } |
Marc Kleine-Budde | 1a3e517 | 2013-11-25 22:15:20 +0100 | [diff] [blame] | 1624 | clock_freq = clk_get_rate(clk_per); |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 1625 | } |
| 1626 | |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 1627 | irq = platform_get_irq(pdev, 0); |
Fabio Estevam | 933e4af | 2013-07-22 12:41:39 -0300 | [diff] [blame] | 1628 | if (irq <= 0) |
| 1629 | return -ENODEV; |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 1630 | |
Joakim Zhang | a4721f2 | 2019-09-29 08:32:09 +0000 | [diff] [blame] | 1631 | regs = devm_platform_ioremap_resource(pdev, 0); |
Marc Kleine-Budde | 89af874 | 2015-05-08 09:32:58 +0200 | [diff] [blame] | 1632 | if (IS_ERR(regs)) |
| 1633 | return PTR_ERR(regs); |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 1634 | |
Hui Wang | 30c1e67 | 2012-06-28 16:21:35 +0800 | [diff] [blame] | 1635 | of_id = of_match_device(flexcan_of_match, &pdev->dev); |
| 1636 | if (of_id) { |
| 1637 | devtype_data = of_id->data; |
Marc Kleine-Budde | d0873e6 | 2014-03-04 22:04:22 +0100 | [diff] [blame] | 1638 | } else if (platform_get_device_id(pdev)->driver_data) { |
Hui Wang | 30c1e67 | 2012-06-28 16:21:35 +0800 | [diff] [blame] | 1639 | devtype_data = (struct flexcan_devtype_data *) |
Marc Kleine-Budde | d0873e6 | 2014-03-04 22:04:22 +0100 | [diff] [blame] | 1640 | platform_get_device_id(pdev)->driver_data; |
Hui Wang | 30c1e67 | 2012-06-28 16:21:35 +0800 | [diff] [blame] | 1641 | } else { |
Fabio Estevam | 933e4af | 2013-07-22 12:41:39 -0300 | [diff] [blame] | 1642 | return -ENODEV; |
Hui Wang | 30c1e67 | 2012-06-28 16:21:35 +0800 | [diff] [blame] | 1643 | } |
| 1644 | |
Fabio Estevam | 933e4af | 2013-07-22 12:41:39 -0300 | [diff] [blame] | 1645 | dev = alloc_candev(sizeof(struct flexcan_priv), 1); |
| 1646 | if (!dev) |
| 1647 | return -ENOMEM; |
| 1648 | |
Marc Kleine-Budde | 3016475 | 2015-05-10 15:26:58 +0200 | [diff] [blame] | 1649 | platform_set_drvdata(pdev, dev); |
| 1650 | SET_NETDEV_DEV(dev, &pdev->dev); |
| 1651 | |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 1652 | dev->netdev_ops = &flexcan_netdev_ops; |
| 1653 | dev->irq = irq; |
Reuben Dowle | 9a12349 | 2011-11-01 11:18:03 +1300 | [diff] [blame] | 1654 | dev->flags |= IFF_ECHO; |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 1655 | |
| 1656 | priv = netdev_priv(dev); |
Pankaj Bansal | 88462d2 | 2017-11-24 18:52:08 +0530 | [diff] [blame] | 1657 | |
Uwe Kleine-König | 0e030a3 | 2018-04-25 16:50:39 +0200 | [diff] [blame] | 1658 | if (of_property_read_bool(pdev->dev.of_node, "big-endian") || |
| 1659 | devtype_data->quirks & FLEXCAN_QUIRK_DEFAULT_BIG_ENDIAN) { |
Pankaj Bansal | 88462d2 | 2017-11-24 18:52:08 +0530 | [diff] [blame] | 1660 | priv->read = flexcan_read_be; |
| 1661 | priv->write = flexcan_write_be; |
| 1662 | } else { |
Uwe Kleine-König | 0e030a3 | 2018-04-25 16:50:39 +0200 | [diff] [blame] | 1663 | priv->read = flexcan_read_le; |
| 1664 | priv->write = flexcan_write_le; |
Pankaj Bansal | 88462d2 | 2017-11-24 18:52:08 +0530 | [diff] [blame] | 1665 | } |
| 1666 | |
Aisheng Dong | ca10989 | 2018-11-30 08:53:26 +0000 | [diff] [blame] | 1667 | priv->dev = &pdev->dev; |
holt@sgi.com | 97efe9a | 2011-08-16 17:32:23 +0000 | [diff] [blame] | 1668 | priv->can.clock.freq = clock_freq; |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 1669 | priv->can.bittiming_const = &flexcan_bittiming_const; |
| 1670 | priv->can.do_set_mode = flexcan_set_mode; |
| 1671 | priv->can.do_get_berr_counter = flexcan_get_berr_counter; |
| 1672 | priv->can.ctrlmode_supported = CAN_CTRLMODE_LOOPBACK | |
| 1673 | CAN_CTRLMODE_LISTENONLY | CAN_CTRLMODE_3_SAMPLES | |
| 1674 | CAN_CTRLMODE_BERR_REPORTING; |
Marc Kleine-Budde | 89af874 | 2015-05-08 09:32:58 +0200 | [diff] [blame] | 1675 | priv->regs = regs; |
Steffen Trumtrar | 3d42a37 | 2012-07-17 16:14:34 +0200 | [diff] [blame] | 1676 | priv->clk_ipg = clk_ipg; |
| 1677 | priv->clk_per = clk_per; |
Dong Aisheng | 8c306be | 2018-12-13 07:08:00 +0000 | [diff] [blame] | 1678 | priv->clk_src = clk_src; |
Hui Wang | 30c1e67 | 2012-06-28 16:21:35 +0800 | [diff] [blame] | 1679 | priv->devtype_data = devtype_data; |
Andreas Werner | 555828e | 2015-03-22 17:35:52 +0100 | [diff] [blame] | 1680 | priv->reg_xceiver = reg_xceiver; |
Fabio Estevam | b7c4114 | 2013-06-10 23:12:57 -0300 | [diff] [blame] | 1681 | |
Aisheng Dong | ca10989 | 2018-11-30 08:53:26 +0000 | [diff] [blame] | 1682 | pm_runtime_get_noresume(&pdev->dev); |
| 1683 | pm_runtime_set_active(&pdev->dev); |
| 1684 | pm_runtime_enable(&pdev->dev); |
| 1685 | |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 1686 | err = register_flexcandev(dev); |
| 1687 | if (err) { |
| 1688 | dev_err(&pdev->dev, "registering netdev failed\n"); |
| 1689 | goto failed_register; |
| 1690 | } |
| 1691 | |
Joakim Zhang | ee97302 | 2019-10-30 06:45:57 +0000 | [diff] [blame] | 1692 | of_can_transceiver(dev); |
Fabio Baltieri | adccadb | 2012-12-18 18:50:58 +0100 | [diff] [blame] | 1693 | devm_can_led_init(dev); |
| 1694 | |
Aisheng Dong | de3578c | 2018-11-23 08:35:33 +0000 | [diff] [blame] | 1695 | if (priv->devtype_data->quirks & FLEXCAN_QUIRK_SETUP_STOP_MODE) { |
| 1696 | err = flexcan_setup_stop_mode(pdev); |
| 1697 | if (err) |
| 1698 | dev_dbg(&pdev->dev, "failed to setup stop-mode\n"); |
| 1699 | } |
| 1700 | |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 1701 | return 0; |
| 1702 | |
| 1703 | failed_register: |
| 1704 | free_candev(dev); |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 1705 | return err; |
| 1706 | } |
| 1707 | |
Bill Pemberton | 3c8ac0f | 2012-12-03 09:22:44 -0500 | [diff] [blame] | 1708 | static int flexcan_remove(struct platform_device *pdev) |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 1709 | { |
| 1710 | struct net_device *dev = platform_get_drvdata(pdev); |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 1711 | |
| 1712 | unregister_flexcandev(dev); |
Aisheng Dong | ca10989 | 2018-11-30 08:53:26 +0000 | [diff] [blame] | 1713 | pm_runtime_disable(&pdev->dev); |
Marc Kleine-Budde | 9a27586 | 2010-10-21 05:07:58 +0000 | [diff] [blame] | 1714 | free_candev(dev); |
| 1715 | |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 1716 | return 0; |
| 1717 | } |
| 1718 | |
Marc Kleine-Budde | 08c6d35 | 2014-03-05 19:10:44 +0100 | [diff] [blame] | 1719 | static int __maybe_unused flexcan_suspend(struct device *device) |
Eric Bénard | 8b5e218 | 2012-05-08 17:12:17 +0200 | [diff] [blame] | 1720 | { |
Fabio Estevam | 588e7a8 | 2013-05-20 15:43:43 -0300 | [diff] [blame] | 1721 | struct net_device *dev = dev_get_drvdata(device); |
Eric Bénard | 8b5e218 | 2012-05-08 17:12:17 +0200 | [diff] [blame] | 1722 | struct flexcan_priv *priv = netdev_priv(dev); |
Aisheng Dong | ca10989 | 2018-11-30 08:53:26 +0000 | [diff] [blame] | 1723 | int err = 0; |
Eric Bénard | 8b5e218 | 2012-05-08 17:12:17 +0200 | [diff] [blame] | 1724 | |
Eric Bénard | 8b5e218 | 2012-05-08 17:12:17 +0200 | [diff] [blame] | 1725 | if (netif_running(dev)) { |
Aisheng Dong | de3578c | 2018-11-23 08:35:33 +0000 | [diff] [blame] | 1726 | /* if wakeup is enabled, enter stop mode |
| 1727 | * else enter disabled mode. |
| 1728 | */ |
| 1729 | if (device_may_wakeup(device)) { |
| 1730 | enable_irq_wake(dev->irq); |
Joakim Zhang | 5f186c2 | 2019-07-02 01:45:41 +0000 | [diff] [blame] | 1731 | err = flexcan_enter_stop_mode(priv); |
| 1732 | if (err) |
| 1733 | return err; |
Aisheng Dong | de3578c | 2018-11-23 08:35:33 +0000 | [diff] [blame] | 1734 | } else { |
| 1735 | err = flexcan_chip_disable(priv); |
| 1736 | if (err) |
| 1737 | return err; |
Aisheng Dong | ca10989 | 2018-11-30 08:53:26 +0000 | [diff] [blame] | 1738 | |
| 1739 | err = pm_runtime_force_suspend(device); |
Aisheng Dong | de3578c | 2018-11-23 08:35:33 +0000 | [diff] [blame] | 1740 | } |
Eric Bénard | 8b5e218 | 2012-05-08 17:12:17 +0200 | [diff] [blame] | 1741 | netif_stop_queue(dev); |
| 1742 | netif_device_detach(dev); |
| 1743 | } |
| 1744 | priv->can.state = CAN_STATE_SLEEPING; |
| 1745 | |
Aisheng Dong | ca10989 | 2018-11-30 08:53:26 +0000 | [diff] [blame] | 1746 | return err; |
Eric Bénard | 8b5e218 | 2012-05-08 17:12:17 +0200 | [diff] [blame] | 1747 | } |
| 1748 | |
Marc Kleine-Budde | 08c6d35 | 2014-03-05 19:10:44 +0100 | [diff] [blame] | 1749 | static int __maybe_unused flexcan_resume(struct device *device) |
Eric Bénard | 8b5e218 | 2012-05-08 17:12:17 +0200 | [diff] [blame] | 1750 | { |
Fabio Estevam | 588e7a8 | 2013-05-20 15:43:43 -0300 | [diff] [blame] | 1751 | struct net_device *dev = dev_get_drvdata(device); |
Eric Bénard | 8b5e218 | 2012-05-08 17:12:17 +0200 | [diff] [blame] | 1752 | struct flexcan_priv *priv = netdev_priv(dev); |
Aisheng Dong | ca10989 | 2018-11-30 08:53:26 +0000 | [diff] [blame] | 1753 | int err = 0; |
Eric Bénard | 8b5e218 | 2012-05-08 17:12:17 +0200 | [diff] [blame] | 1754 | |
| 1755 | priv->can.state = CAN_STATE_ERROR_ACTIVE; |
| 1756 | if (netif_running(dev)) { |
| 1757 | netif_device_attach(dev); |
| 1758 | netif_start_queue(dev); |
Aisheng Dong | de3578c | 2018-11-23 08:35:33 +0000 | [diff] [blame] | 1759 | if (device_may_wakeup(device)) { |
| 1760 | disable_irq_wake(dev->irq); |
Sean Nyekjaer | e707180 | 2019-12-04 11:36:06 +0000 | [diff] [blame] | 1761 | err = flexcan_exit_stop_mode(priv); |
| 1762 | if (err) |
| 1763 | return err; |
Aisheng Dong | de3578c | 2018-11-23 08:35:33 +0000 | [diff] [blame] | 1764 | } else { |
Aisheng Dong | ca10989 | 2018-11-30 08:53:26 +0000 | [diff] [blame] | 1765 | err = pm_runtime_force_resume(device); |
Aisheng Dong | de3578c | 2018-11-23 08:35:33 +0000 | [diff] [blame] | 1766 | if (err) |
| 1767 | return err; |
Aisheng Dong | ca10989 | 2018-11-30 08:53:26 +0000 | [diff] [blame] | 1768 | |
| 1769 | err = flexcan_chip_enable(priv); |
Aisheng Dong | de3578c | 2018-11-23 08:35:33 +0000 | [diff] [blame] | 1770 | } |
Eric Bénard | 8b5e218 | 2012-05-08 17:12:17 +0200 | [diff] [blame] | 1771 | } |
Aisheng Dong | ca10989 | 2018-11-30 08:53:26 +0000 | [diff] [blame] | 1772 | |
| 1773 | return err; |
| 1774 | } |
| 1775 | |
| 1776 | static int __maybe_unused flexcan_runtime_suspend(struct device *device) |
| 1777 | { |
| 1778 | struct net_device *dev = dev_get_drvdata(device); |
| 1779 | struct flexcan_priv *priv = netdev_priv(dev); |
| 1780 | |
| 1781 | flexcan_clks_disable(priv); |
| 1782 | |
Fabio Estevam | 4de349e | 2016-08-17 12:41:08 -0300 | [diff] [blame] | 1783 | return 0; |
Eric Bénard | 8b5e218 | 2012-05-08 17:12:17 +0200 | [diff] [blame] | 1784 | } |
Fabio Estevam | 588e7a8 | 2013-05-20 15:43:43 -0300 | [diff] [blame] | 1785 | |
Aisheng Dong | ca10989 | 2018-11-30 08:53:26 +0000 | [diff] [blame] | 1786 | static int __maybe_unused flexcan_runtime_resume(struct device *device) |
| 1787 | { |
| 1788 | struct net_device *dev = dev_get_drvdata(device); |
| 1789 | struct flexcan_priv *priv = netdev_priv(dev); |
| 1790 | |
| 1791 | return flexcan_clks_enable(priv); |
| 1792 | } |
| 1793 | |
Aisheng Dong | de3578c | 2018-11-23 08:35:33 +0000 | [diff] [blame] | 1794 | static int __maybe_unused flexcan_noirq_suspend(struct device *device) |
| 1795 | { |
| 1796 | struct net_device *dev = dev_get_drvdata(device); |
| 1797 | struct flexcan_priv *priv = netdev_priv(dev); |
| 1798 | |
| 1799 | if (netif_running(dev) && device_may_wakeup(device)) |
| 1800 | flexcan_enable_wakeup_irq(priv, true); |
| 1801 | |
| 1802 | return 0; |
| 1803 | } |
| 1804 | |
| 1805 | static int __maybe_unused flexcan_noirq_resume(struct device *device) |
| 1806 | { |
| 1807 | struct net_device *dev = dev_get_drvdata(device); |
| 1808 | struct flexcan_priv *priv = netdev_priv(dev); |
| 1809 | |
Sean Nyekjaer | e707180 | 2019-12-04 11:36:06 +0000 | [diff] [blame] | 1810 | if (netif_running(dev) && device_may_wakeup(device)) |
Aisheng Dong | de3578c | 2018-11-23 08:35:33 +0000 | [diff] [blame] | 1811 | flexcan_enable_wakeup_irq(priv, false); |
Aisheng Dong | de3578c | 2018-11-23 08:35:33 +0000 | [diff] [blame] | 1812 | |
| 1813 | return 0; |
| 1814 | } |
| 1815 | |
| 1816 | static const struct dev_pm_ops flexcan_pm_ops = { |
| 1817 | SET_SYSTEM_SLEEP_PM_OPS(flexcan_suspend, flexcan_resume) |
Aisheng Dong | ca10989 | 2018-11-30 08:53:26 +0000 | [diff] [blame] | 1818 | SET_RUNTIME_PM_OPS(flexcan_runtime_suspend, flexcan_runtime_resume, NULL) |
Aisheng Dong | de3578c | 2018-11-23 08:35:33 +0000 | [diff] [blame] | 1819 | SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(flexcan_noirq_suspend, flexcan_noirq_resume) |
| 1820 | }; |
Eric Bénard | 8b5e218 | 2012-05-08 17:12:17 +0200 | [diff] [blame] | 1821 | |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 1822 | static struct platform_driver flexcan_driver = { |
holt@sgi.com | c8aef4c | 2011-08-16 17:32:22 +0000 | [diff] [blame] | 1823 | .driver = { |
| 1824 | .name = DRV_NAME, |
Fabio Estevam | 588e7a8 | 2013-05-20 15:43:43 -0300 | [diff] [blame] | 1825 | .pm = &flexcan_pm_ops, |
holt@sgi.com | c8aef4c | 2011-08-16 17:32:22 +0000 | [diff] [blame] | 1826 | .of_match_table = flexcan_of_match, |
| 1827 | }, |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 1828 | .probe = flexcan_probe, |
Bill Pemberton | 3c8ac0f | 2012-12-03 09:22:44 -0500 | [diff] [blame] | 1829 | .remove = flexcan_remove, |
Hui Wang | 30c1e67 | 2012-06-28 16:21:35 +0800 | [diff] [blame] | 1830 | .id_table = flexcan_id_table, |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 1831 | }; |
| 1832 | |
Axel Lin | 871d337 | 2011-11-27 15:42:31 +0000 | [diff] [blame] | 1833 | module_platform_driver(flexcan_driver); |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 1834 | |
| 1835 | MODULE_AUTHOR("Sascha Hauer <kernel@pengutronix.de>, " |
| 1836 | "Marc Kleine-Budde <kernel@pengutronix.de>"); |
| 1837 | MODULE_LICENSE("GPL v2"); |
| 1838 | MODULE_DESCRIPTION("CAN port driver for flexcan based chip"); |