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