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