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