Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 1 | /* |
| 2 | * flexcan.c - FLEXCAN CAN controller driver |
| 3 | * |
| 4 | * Copyright (c) 2005-2006 Varma Electronics Oy |
| 5 | * Copyright (c) 2009 Sascha Hauer, Pengutronix |
Marc Kleine-Budde | b3cf53e | 2015-09-01 09:00:13 +0200 | [diff] [blame] | 6 | * Copyright (c) 2010-2017 Pengutronix, Marc Kleine-Budde <kernel@pengutronix.de> |
| 7 | * Copyright (c) 2014 David Jander, Protonic Holland |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 8 | * |
| 9 | * Based on code originally by Andrey Volkov <avolkov@varma-el.com> |
| 10 | * |
| 11 | * LICENCE: |
| 12 | * This program is free software; you can redistribute it and/or |
| 13 | * modify it under the terms of the GNU General Public License as |
| 14 | * published by the Free Software Foundation version 2. |
| 15 | * |
| 16 | * This program is distributed in the hope that it will be useful, |
| 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 19 | * GNU General Public License for more details. |
| 20 | * |
| 21 | */ |
| 22 | |
| 23 | #include <linux/netdevice.h> |
| 24 | #include <linux/can.h> |
| 25 | #include <linux/can/dev.h> |
| 26 | #include <linux/can/error.h> |
Fabio Baltieri | adccadb | 2012-12-18 18:50:58 +0100 | [diff] [blame] | 27 | #include <linux/can/led.h> |
Marc Kleine-Budde | 3016475 | 2015-05-10 15:26:58 +0200 | [diff] [blame] | 28 | #include <linux/can/rx-offload.h> |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 29 | #include <linux/clk.h> |
| 30 | #include <linux/delay.h> |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 31 | #include <linux/interrupt.h> |
| 32 | #include <linux/io.h> |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 33 | #include <linux/module.h> |
holt@sgi.com | 97efe9a | 2011-08-16 17:32:23 +0000 | [diff] [blame] | 34 | #include <linux/of.h> |
Hui Wang | 30c1e67 | 2012-06-28 16:21:35 +0800 | [diff] [blame] | 35 | #include <linux/of_device.h> |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 36 | #include <linux/platform_device.h> |
Fabio Estevam | b7c4114 | 2013-06-10 23:12:57 -0300 | [diff] [blame] | 37 | #include <linux/regulator/consumer.h> |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 38 | |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 39 | #define DRV_NAME "flexcan" |
| 40 | |
| 41 | /* 8 for RX fifo and 2 error handling */ |
| 42 | #define FLEXCAN_NAPI_WEIGHT (8 + 2) |
| 43 | |
| 44 | /* FLEXCAN module configuration register (CANMCR) bits */ |
| 45 | #define FLEXCAN_MCR_MDIS BIT(31) |
| 46 | #define FLEXCAN_MCR_FRZ BIT(30) |
| 47 | #define FLEXCAN_MCR_FEN BIT(29) |
| 48 | #define FLEXCAN_MCR_HALT BIT(28) |
| 49 | #define FLEXCAN_MCR_NOT_RDY BIT(27) |
| 50 | #define FLEXCAN_MCR_WAK_MSK BIT(26) |
| 51 | #define FLEXCAN_MCR_SOFTRST BIT(25) |
| 52 | #define FLEXCAN_MCR_FRZ_ACK BIT(24) |
| 53 | #define FLEXCAN_MCR_SUPV BIT(23) |
| 54 | #define FLEXCAN_MCR_SLF_WAK BIT(22) |
| 55 | #define FLEXCAN_MCR_WRN_EN BIT(21) |
| 56 | #define FLEXCAN_MCR_LPM_ACK BIT(20) |
| 57 | #define FLEXCAN_MCR_WAK_SRC BIT(19) |
| 58 | #define FLEXCAN_MCR_DOZE BIT(18) |
| 59 | #define FLEXCAN_MCR_SRX_DIS BIT(17) |
Marc Kleine-Budde | 62d1086 | 2015-08-27 16:01:27 +0200 | [diff] [blame] | 60 | #define FLEXCAN_MCR_IRMQ BIT(16) |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 61 | #define FLEXCAN_MCR_LPRIO_EN BIT(13) |
| 62 | #define FLEXCAN_MCR_AEN BIT(12) |
Marc Kleine-Budde | b3cf53e | 2015-09-01 09:00:13 +0200 | [diff] [blame] | 63 | /* MCR_MAXMB: maximum used MBs is MAXMB + 1 */ |
Marc Kleine-Budde | 4c728d8 | 2014-09-02 16:54:17 +0200 | [diff] [blame] | 64 | #define FLEXCAN_MCR_MAXMB(x) ((x) & 0x7f) |
Marc Kleine-Budde | 0012e5c | 2015-08-06 14:53:57 +0200 | [diff] [blame] | 65 | #define FLEXCAN_MCR_IDAM_A (0x0 << 8) |
| 66 | #define FLEXCAN_MCR_IDAM_B (0x1 << 8) |
| 67 | #define FLEXCAN_MCR_IDAM_C (0x2 << 8) |
| 68 | #define FLEXCAN_MCR_IDAM_D (0x3 << 8) |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 69 | |
| 70 | /* FLEXCAN control register (CANCTRL) bits */ |
| 71 | #define FLEXCAN_CTRL_PRESDIV(x) (((x) & 0xff) << 24) |
| 72 | #define FLEXCAN_CTRL_RJW(x) (((x) & 0x03) << 22) |
| 73 | #define FLEXCAN_CTRL_PSEG1(x) (((x) & 0x07) << 19) |
| 74 | #define FLEXCAN_CTRL_PSEG2(x) (((x) & 0x07) << 16) |
| 75 | #define FLEXCAN_CTRL_BOFF_MSK BIT(15) |
| 76 | #define FLEXCAN_CTRL_ERR_MSK BIT(14) |
| 77 | #define FLEXCAN_CTRL_CLK_SRC BIT(13) |
| 78 | #define FLEXCAN_CTRL_LPB BIT(12) |
| 79 | #define FLEXCAN_CTRL_TWRN_MSK BIT(11) |
| 80 | #define FLEXCAN_CTRL_RWRN_MSK BIT(10) |
| 81 | #define FLEXCAN_CTRL_SMP BIT(7) |
| 82 | #define FLEXCAN_CTRL_BOFF_REC BIT(6) |
| 83 | #define FLEXCAN_CTRL_TSYN BIT(5) |
| 84 | #define FLEXCAN_CTRL_LBUF BIT(4) |
| 85 | #define FLEXCAN_CTRL_LOM BIT(3) |
| 86 | #define FLEXCAN_CTRL_PROPSEG(x) ((x) & 0x07) |
| 87 | #define FLEXCAN_CTRL_ERR_BUS (FLEXCAN_CTRL_ERR_MSK) |
| 88 | #define FLEXCAN_CTRL_ERR_STATE \ |
| 89 | (FLEXCAN_CTRL_TWRN_MSK | FLEXCAN_CTRL_RWRN_MSK | \ |
| 90 | FLEXCAN_CTRL_BOFF_MSK) |
| 91 | #define FLEXCAN_CTRL_ERR_ALL \ |
| 92 | (FLEXCAN_CTRL_ERR_BUS | FLEXCAN_CTRL_ERR_STATE) |
| 93 | |
Stefan Agner | cdce844 | 2014-07-15 14:56:21 +0200 | [diff] [blame] | 94 | /* FLEXCAN control register 2 (CTRL2) bits */ |
Marc Kleine-Budde | 6f75fce | 2014-09-23 11:03:01 +0200 | [diff] [blame] | 95 | #define FLEXCAN_CTRL2_ECRWRE BIT(29) |
| 96 | #define FLEXCAN_CTRL2_WRMFRZ BIT(28) |
| 97 | #define FLEXCAN_CTRL2_RFFN(x) (((x) & 0x0f) << 24) |
| 98 | #define FLEXCAN_CTRL2_TASD(x) (((x) & 0x1f) << 19) |
| 99 | #define FLEXCAN_CTRL2_MRP BIT(18) |
| 100 | #define FLEXCAN_CTRL2_RRS BIT(17) |
| 101 | #define FLEXCAN_CTRL2_EACEN BIT(16) |
Stefan Agner | cdce844 | 2014-07-15 14:56:21 +0200 | [diff] [blame] | 102 | |
| 103 | /* FLEXCAN memory error control register (MECR) bits */ |
| 104 | #define FLEXCAN_MECR_ECRWRDIS BIT(31) |
| 105 | #define FLEXCAN_MECR_HANCEI_MSK BIT(19) |
| 106 | #define FLEXCAN_MECR_FANCEI_MSK BIT(18) |
| 107 | #define FLEXCAN_MECR_CEI_MSK BIT(16) |
| 108 | #define FLEXCAN_MECR_HAERRIE BIT(15) |
| 109 | #define FLEXCAN_MECR_FAERRIE BIT(14) |
| 110 | #define FLEXCAN_MECR_EXTERRIE BIT(13) |
| 111 | #define FLEXCAN_MECR_RERRDIS BIT(9) |
| 112 | #define FLEXCAN_MECR_ECCDIS BIT(8) |
| 113 | #define FLEXCAN_MECR_NCEFAFRZ BIT(7) |
| 114 | |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 115 | /* FLEXCAN error and status register (ESR) bits */ |
| 116 | #define FLEXCAN_ESR_TWRN_INT BIT(17) |
| 117 | #define FLEXCAN_ESR_RWRN_INT BIT(16) |
| 118 | #define FLEXCAN_ESR_BIT1_ERR BIT(15) |
| 119 | #define FLEXCAN_ESR_BIT0_ERR BIT(14) |
| 120 | #define FLEXCAN_ESR_ACK_ERR BIT(13) |
| 121 | #define FLEXCAN_ESR_CRC_ERR BIT(12) |
| 122 | #define FLEXCAN_ESR_FRM_ERR BIT(11) |
| 123 | #define FLEXCAN_ESR_STF_ERR BIT(10) |
| 124 | #define FLEXCAN_ESR_TX_WRN BIT(9) |
| 125 | #define FLEXCAN_ESR_RX_WRN BIT(8) |
| 126 | #define FLEXCAN_ESR_IDLE BIT(7) |
| 127 | #define FLEXCAN_ESR_TXRX BIT(6) |
| 128 | #define FLEXCAN_EST_FLT_CONF_SHIFT (4) |
| 129 | #define FLEXCAN_ESR_FLT_CONF_MASK (0x3 << FLEXCAN_EST_FLT_CONF_SHIFT) |
| 130 | #define FLEXCAN_ESR_FLT_CONF_ACTIVE (0x0 << FLEXCAN_EST_FLT_CONF_SHIFT) |
| 131 | #define FLEXCAN_ESR_FLT_CONF_PASSIVE (0x1 << FLEXCAN_EST_FLT_CONF_SHIFT) |
| 132 | #define FLEXCAN_ESR_BOFF_INT BIT(2) |
| 133 | #define FLEXCAN_ESR_ERR_INT BIT(1) |
| 134 | #define FLEXCAN_ESR_WAK_INT BIT(0) |
| 135 | #define FLEXCAN_ESR_ERR_BUS \ |
| 136 | (FLEXCAN_ESR_BIT1_ERR | FLEXCAN_ESR_BIT0_ERR | \ |
| 137 | FLEXCAN_ESR_ACK_ERR | FLEXCAN_ESR_CRC_ERR | \ |
| 138 | FLEXCAN_ESR_FRM_ERR | FLEXCAN_ESR_STF_ERR) |
| 139 | #define FLEXCAN_ESR_ERR_STATE \ |
| 140 | (FLEXCAN_ESR_TWRN_INT | FLEXCAN_ESR_RWRN_INT | FLEXCAN_ESR_BOFF_INT) |
| 141 | #define FLEXCAN_ESR_ERR_ALL \ |
| 142 | (FLEXCAN_ESR_ERR_BUS | FLEXCAN_ESR_ERR_STATE) |
Wolfgang Grandegger | 6e9d554 | 2011-12-12 16:09:28 +0100 | [diff] [blame] | 143 | #define FLEXCAN_ESR_ALL_INT \ |
| 144 | (FLEXCAN_ESR_TWRN_INT | FLEXCAN_ESR_RWRN_INT | \ |
| 145 | FLEXCAN_ESR_BOFF_INT | FLEXCAN_ESR_ERR_INT) |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 146 | |
| 147 | /* FLEXCAN interrupt flag register (IFLAG) bits */ |
David Jander | 25e9244 | 2014-09-03 16:47:22 +0200 | [diff] [blame] | 148 | /* Errata ERR005829 step7: Reserve first valid MB */ |
Marc Kleine-Budde | b93917c | 2015-07-12 00:47:47 +0200 | [diff] [blame] | 149 | #define FLEXCAN_TX_MB_RESERVED_OFF_FIFO 8 |
| 150 | #define FLEXCAN_TX_MB_OFF_FIFO 9 |
Marc Kleine-Budde | b3cf53e | 2015-09-01 09:00:13 +0200 | [diff] [blame] | 151 | #define FLEXCAN_TX_MB_RESERVED_OFF_TIMESTAMP 0 |
| 152 | #define FLEXCAN_TX_MB_OFF_TIMESTAMP 1 |
| 153 | #define FLEXCAN_RX_MB_OFF_TIMESTAMP_FIRST (FLEXCAN_TX_MB_OFF_TIMESTAMP + 1) |
| 154 | #define FLEXCAN_RX_MB_OFF_TIMESTAMP_LAST 63 |
Marc Kleine-Budde | b93917c | 2015-07-12 00:47:47 +0200 | [diff] [blame] | 155 | #define FLEXCAN_IFLAG_MB(x) BIT(x) |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 156 | #define FLEXCAN_IFLAG_RX_FIFO_OVERFLOW BIT(7) |
| 157 | #define FLEXCAN_IFLAG_RX_FIFO_WARN BIT(6) |
| 158 | #define FLEXCAN_IFLAG_RX_FIFO_AVAILABLE BIT(5) |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 159 | |
| 160 | /* FLEXCAN message buffers */ |
Marc Kleine-Budde | b3cf53e | 2015-09-01 09:00:13 +0200 | [diff] [blame] | 161 | #define FLEXCAN_MB_CODE_MASK (0xf << 24) |
| 162 | #define FLEXCAN_MB_CODE_RX_BUSY_BIT (0x1 << 24) |
Marc Kleine-Budde | c32fe4a | 2014-09-16 12:39:28 +0200 | [diff] [blame] | 163 | #define FLEXCAN_MB_CODE_RX_INACTIVE (0x0 << 24) |
| 164 | #define FLEXCAN_MB_CODE_RX_EMPTY (0x4 << 24) |
| 165 | #define FLEXCAN_MB_CODE_RX_FULL (0x2 << 24) |
Marc Kleine-Budde | 0012e5c | 2015-08-06 14:53:57 +0200 | [diff] [blame] | 166 | #define FLEXCAN_MB_CODE_RX_OVERRUN (0x6 << 24) |
Marc Kleine-Budde | c32fe4a | 2014-09-16 12:39:28 +0200 | [diff] [blame] | 167 | #define FLEXCAN_MB_CODE_RX_RANSWER (0xa << 24) |
| 168 | |
| 169 | #define FLEXCAN_MB_CODE_TX_INACTIVE (0x8 << 24) |
| 170 | #define FLEXCAN_MB_CODE_TX_ABORT (0x9 << 24) |
| 171 | #define FLEXCAN_MB_CODE_TX_DATA (0xc << 24) |
| 172 | #define FLEXCAN_MB_CODE_TX_TANSWER (0xe << 24) |
| 173 | |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 174 | #define FLEXCAN_MB_CNT_SRR BIT(22) |
| 175 | #define FLEXCAN_MB_CNT_IDE BIT(21) |
| 176 | #define FLEXCAN_MB_CNT_RTR BIT(20) |
| 177 | #define FLEXCAN_MB_CNT_LENGTH(x) (((x) & 0xf) << 16) |
| 178 | #define FLEXCAN_MB_CNT_TIMESTAMP(x) ((x) & 0xffff) |
| 179 | |
Marc Kleine-Budde | 0012e5c | 2015-08-06 14:53:57 +0200 | [diff] [blame] | 180 | #define FLEXCAN_TIMEOUT_US (50) |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 181 | |
Marc Kleine-Budde | 0012e5c | 2015-08-06 14:53:57 +0200 | [diff] [blame] | 182 | /* FLEXCAN hardware feature flags |
Wolfgang Grandegger | bb698ca | 2012-10-10 21:10:42 +0200 | [diff] [blame] | 183 | * |
| 184 | * Below is some version info we got: |
David Jander | 8a1ce7e | 2014-10-10 15:04:03 +0200 | [diff] [blame] | 185 | * SOC Version IP-Version Glitch- [TR]WRN_INT Memory err RTR re- |
| 186 | * Filter? connected? detection ception in MB |
| 187 | * MX25 FlexCAN2 03.00.00.00 no no no no |
| 188 | * MX28 FlexCAN2 03.00.04.00 yes yes no no |
| 189 | * MX35 FlexCAN2 03.00.00.00 no no no no |
| 190 | * MX53 FlexCAN2 03.00.00.00 yes no no no |
| 191 | * MX6s FlexCAN3 10.00.12.00 yes yes no yes |
| 192 | * VF610 FlexCAN3 ? no yes yes yes? |
Wolfgang Grandegger | bb698ca | 2012-10-10 21:10:42 +0200 | [diff] [blame] | 193 | * |
| 194 | * Some SOCs do not have the RX_WARN & TX_WARN interrupt line connected. |
| 195 | */ |
Marc Kleine-Budde | f377bff | 2015-05-08 15:22:36 +0200 | [diff] [blame] | 196 | #define FLEXCAN_QUIRK_BROKEN_ERR_STATE BIT(1) /* [TR]WRN_INT not connected */ |
| 197 | #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] | 198 | #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] | 199 | #define FLEXCAN_QUIRK_DISABLE_MECR BIT(4) /* Disable Memory error detection */ |
Marc Kleine-Budde | b3cf53e | 2015-09-01 09:00:13 +0200 | [diff] [blame] | 200 | #define FLEXCAN_QUIRK_USE_OFF_TIMESTAMP BIT(5) /* Use timestamp based offloading */ |
Wolfgang Grandegger | 4f72e5f | 2012-09-28 03:17:15 +0000 | [diff] [blame] | 201 | |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 202 | /* Structure of the message buffer */ |
| 203 | struct flexcan_mb { |
| 204 | u32 can_ctrl; |
| 205 | u32 can_id; |
| 206 | u32 data[2]; |
| 207 | }; |
| 208 | |
| 209 | /* Structure of the hardware registers */ |
| 210 | struct flexcan_regs { |
| 211 | u32 mcr; /* 0x00 */ |
| 212 | u32 ctrl; /* 0x04 */ |
| 213 | u32 timer; /* 0x08 */ |
| 214 | u32 _reserved1; /* 0x0c */ |
| 215 | u32 rxgmask; /* 0x10 */ |
| 216 | u32 rx14mask; /* 0x14 */ |
| 217 | u32 rx15mask; /* 0x18 */ |
| 218 | u32 ecr; /* 0x1c */ |
| 219 | u32 esr; /* 0x20 */ |
| 220 | u32 imask2; /* 0x24 */ |
| 221 | u32 imask1; /* 0x28 */ |
| 222 | u32 iflag2; /* 0x2c */ |
| 223 | u32 iflag1; /* 0x30 */ |
Marc Kleine-Budde | 62d1086 | 2015-08-27 16:01:27 +0200 | [diff] [blame] | 224 | union { /* 0x34 */ |
| 225 | u32 gfwr_mx28; /* MX28, MX53 */ |
| 226 | u32 ctrl2; /* MX6, VF610 */ |
| 227 | }; |
Hui Wang | 30c1e67 | 2012-06-28 16:21:35 +0800 | [diff] [blame] | 228 | u32 esr2; /* 0x38 */ |
| 229 | u32 imeur; /* 0x3c */ |
| 230 | u32 lrfr; /* 0x40 */ |
| 231 | u32 crcr; /* 0x44 */ |
| 232 | u32 rxfgmask; /* 0x48 */ |
| 233 | u32 rxfir; /* 0x4c */ |
Stefan Agner | cdce844 | 2014-07-15 14:56:21 +0200 | [diff] [blame] | 234 | u32 _reserved3[12]; /* 0x50 */ |
Marc Kleine-Budde | 1ba763d | 2015-08-25 10:39:19 +0200 | [diff] [blame] | 235 | struct flexcan_mb mb[64]; /* 0x80 */ |
Marc Kleine-Budde | 66a6ef0 | 2014-09-17 12:50:48 +0200 | [diff] [blame] | 236 | /* FIFO-mode: |
| 237 | * MB |
| 238 | * 0x080...0x08f 0 RX message buffer |
| 239 | * 0x090...0x0df 1-5 reserverd |
| 240 | * 0x0e0...0x0ff 6-7 8 entry ID table |
| 241 | * (mx25, mx28, mx35, mx53) |
| 242 | * 0x0e0...0x2df 6-7..37 8..128 entry ID table |
Marc Kleine-Budde | 0012e5c | 2015-08-06 14:53:57 +0200 | [diff] [blame] | 243 | * size conf'ed via ctrl2::RFFN |
Marc Kleine-Budde | 66a6ef0 | 2014-09-17 12:50:48 +0200 | [diff] [blame] | 244 | * (mx6, vf610) |
| 245 | */ |
Marc Kleine-Budde | 62d1086 | 2015-08-27 16:01:27 +0200 | [diff] [blame] | 246 | u32 _reserved4[256]; /* 0x480 */ |
| 247 | u32 rximr[64]; /* 0x880 */ |
| 248 | u32 _reserved5[24]; /* 0x980 */ |
| 249 | u32 gfwr_mx6; /* 0x9e0 - MX6 */ |
| 250 | u32 _reserved6[63]; /* 0x9e4 */ |
Stefan Agner | cdce844 | 2014-07-15 14:56:21 +0200 | [diff] [blame] | 251 | u32 mecr; /* 0xae0 */ |
| 252 | u32 erriar; /* 0xae4 */ |
| 253 | u32 erridpr; /* 0xae8 */ |
| 254 | u32 errippr; /* 0xaec */ |
| 255 | u32 rerrar; /* 0xaf0 */ |
| 256 | u32 rerrdr; /* 0xaf4 */ |
| 257 | u32 rerrsynr; /* 0xaf8 */ |
| 258 | u32 errsr; /* 0xafc */ |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 259 | }; |
| 260 | |
Hui Wang | 30c1e67 | 2012-06-28 16:21:35 +0800 | [diff] [blame] | 261 | struct flexcan_devtype_data { |
Marc Kleine-Budde | f377bff | 2015-05-08 15:22:36 +0200 | [diff] [blame] | 262 | u32 quirks; /* quirks needed for different IP cores */ |
Hui Wang | 30c1e67 | 2012-06-28 16:21:35 +0800 | [diff] [blame] | 263 | }; |
| 264 | |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 265 | struct flexcan_priv { |
| 266 | struct can_priv can; |
Marc Kleine-Budde | 3016475 | 2015-05-10 15:26:58 +0200 | [diff] [blame] | 267 | struct can_rx_offload offload; |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 268 | |
Marc Kleine-Budde | 89af874 | 2015-05-08 09:32:58 +0200 | [diff] [blame] | 269 | struct flexcan_regs __iomem *regs; |
Marc Kleine-Budde | b93917c | 2015-07-12 00:47:47 +0200 | [diff] [blame] | 270 | struct flexcan_mb __iomem *tx_mb; |
| 271 | struct flexcan_mb __iomem *tx_mb_reserved; |
| 272 | u8 tx_mb_idx; |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 273 | u32 reg_ctrl_default; |
Marc Kleine-Budde | 28ac7dc | 2015-08-04 13:46:10 +0200 | [diff] [blame] | 274 | u32 reg_imask1_default; |
Marc Kleine-Budde | b3cf53e | 2015-09-01 09:00:13 +0200 | [diff] [blame] | 275 | u32 reg_imask2_default; |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 276 | |
Steffen Trumtrar | 3d42a37 | 2012-07-17 16:14:34 +0200 | [diff] [blame] | 277 | struct clk *clk_ipg; |
| 278 | struct clk *clk_per; |
Marc Kleine-Budde | dda0b3b | 2012-07-13 14:52:48 +0200 | [diff] [blame] | 279 | const struct flexcan_devtype_data *devtype_data; |
Fabio Estevam | b7c4114 | 2013-06-10 23:12:57 -0300 | [diff] [blame] | 280 | struct regulator *reg_xceiver; |
Hui Wang | 30c1e67 | 2012-06-28 16:21:35 +0800 | [diff] [blame] | 281 | }; |
| 282 | |
Marc Kleine-Budde | a3c11a7 | 2016-07-04 14:45:44 +0200 | [diff] [blame] | 283 | static const struct flexcan_devtype_data fsl_p1010_devtype_data = { |
Marc Kleine-Budde | f377bff | 2015-05-08 15:22:36 +0200 | [diff] [blame] | 284 | .quirks = FLEXCAN_QUIRK_BROKEN_ERR_STATE, |
Hui Wang | 30c1e67 | 2012-06-28 16:21:35 +0800 | [diff] [blame] | 285 | }; |
Marc Kleine-Budde | 0012e5c | 2015-08-06 14:53:57 +0200 | [diff] [blame] | 286 | |
Marc Kleine-Budde | a3c11a7 | 2016-07-04 14:45:44 +0200 | [diff] [blame] | 287 | static const struct flexcan_devtype_data fsl_imx28_devtype_data; |
Marc Kleine-Budde | 0012e5c | 2015-08-06 14:53:57 +0200 | [diff] [blame] | 288 | |
Marc Kleine-Budde | a3c11a7 | 2016-07-04 14:45:44 +0200 | [diff] [blame] | 289 | static const struct flexcan_devtype_data fsl_imx6q_devtype_data = { |
Marc Kleine-Budde | 096de07 | 2015-09-01 10:28:46 +0200 | [diff] [blame] | 290 | .quirks = FLEXCAN_QUIRK_DISABLE_RXFG | FLEXCAN_QUIRK_ENABLE_EACEN_RRS | |
| 291 | FLEXCAN_QUIRK_USE_OFF_TIMESTAMP, |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 292 | }; |
Marc Kleine-Budde | 0012e5c | 2015-08-06 14:53:57 +0200 | [diff] [blame] | 293 | |
Marc Kleine-Budde | a3c11a7 | 2016-07-04 14:45:44 +0200 | [diff] [blame] | 294 | static const struct flexcan_devtype_data fsl_vf610_devtype_data = { |
Marc Kleine-Budde | 9eb7aa8 | 2015-09-01 08:57:55 +0200 | [diff] [blame] | 295 | .quirks = FLEXCAN_QUIRK_DISABLE_RXFG | FLEXCAN_QUIRK_ENABLE_EACEN_RRS | |
Marc Kleine-Budde | 096de07 | 2015-09-01 10:28:46 +0200 | [diff] [blame] | 296 | FLEXCAN_QUIRK_DISABLE_MECR | FLEXCAN_QUIRK_USE_OFF_TIMESTAMP, |
Stefan Agner | cdce844 | 2014-07-15 14:56:21 +0200 | [diff] [blame] | 297 | }; |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 298 | |
Marc Kleine-Budde | 194b9a4 | 2012-07-16 12:58:31 +0200 | [diff] [blame] | 299 | static const struct can_bittiming_const flexcan_bittiming_const = { |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 300 | .name = DRV_NAME, |
| 301 | .tseg1_min = 4, |
| 302 | .tseg1_max = 16, |
| 303 | .tseg2_min = 2, |
| 304 | .tseg2_max = 8, |
| 305 | .sjw_max = 4, |
| 306 | .brp_min = 1, |
| 307 | .brp_max = 256, |
| 308 | .brp_inc = 1, |
| 309 | }; |
| 310 | |
Marc Kleine-Budde | 0012e5c | 2015-08-06 14:53:57 +0200 | [diff] [blame] | 311 | /* Abstract off the read/write for arm versus ppc. This |
Arnd Bergmann | 0e4b949 | 2014-01-14 11:44:09 +0100 | [diff] [blame] | 312 | * assumes that PPC uses big-endian registers and everything |
| 313 | * else uses little-endian registers, independent of CPU |
Marc Kleine-Budde | 0012e5c | 2015-08-06 14:53:57 +0200 | [diff] [blame] | 314 | * endianness. |
holt@sgi.com | 61e271e | 2011-08-16 17:32:20 +0000 | [diff] [blame] | 315 | */ |
Arnd Bergmann | 0e4b949 | 2014-01-14 11:44:09 +0100 | [diff] [blame] | 316 | #if defined(CONFIG_PPC) |
holt@sgi.com | 61e271e | 2011-08-16 17:32:20 +0000 | [diff] [blame] | 317 | static inline u32 flexcan_read(void __iomem *addr) |
| 318 | { |
| 319 | return in_be32(addr); |
| 320 | } |
| 321 | |
| 322 | static inline void flexcan_write(u32 val, void __iomem *addr) |
| 323 | { |
| 324 | out_be32(addr, val); |
| 325 | } |
| 326 | #else |
| 327 | static inline u32 flexcan_read(void __iomem *addr) |
| 328 | { |
| 329 | return readl(addr); |
| 330 | } |
| 331 | |
| 332 | static inline void flexcan_write(u32 val, void __iomem *addr) |
| 333 | { |
| 334 | writel(val, addr); |
| 335 | } |
| 336 | #endif |
| 337 | |
Marc Kleine-Budde | f003698 | 2014-02-28 17:18:27 +0100 | [diff] [blame] | 338 | static inline int flexcan_transceiver_enable(const struct flexcan_priv *priv) |
| 339 | { |
| 340 | if (!priv->reg_xceiver) |
| 341 | return 0; |
| 342 | |
| 343 | return regulator_enable(priv->reg_xceiver); |
| 344 | } |
| 345 | |
| 346 | static inline int flexcan_transceiver_disable(const struct flexcan_priv *priv) |
| 347 | { |
| 348 | if (!priv->reg_xceiver) |
| 349 | return 0; |
| 350 | |
| 351 | return regulator_disable(priv->reg_xceiver); |
| 352 | } |
| 353 | |
Marc Kleine-Budde | 9b00b30 | 2014-02-28 15:30:18 +0100 | [diff] [blame] | 354 | static int flexcan_chip_enable(struct flexcan_priv *priv) |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 355 | { |
Marc Kleine-Budde | 89af874 | 2015-05-08 09:32:58 +0200 | [diff] [blame] | 356 | struct flexcan_regs __iomem *regs = priv->regs; |
Marc Kleine-Budde | 9b00b30 | 2014-02-28 15:30:18 +0100 | [diff] [blame] | 357 | unsigned int timeout = FLEXCAN_TIMEOUT_US / 10; |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 358 | u32 reg; |
| 359 | |
holt@sgi.com | 61e271e | 2011-08-16 17:32:20 +0000 | [diff] [blame] | 360 | reg = flexcan_read(®s->mcr); |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 361 | reg &= ~FLEXCAN_MCR_MDIS; |
holt@sgi.com | 61e271e | 2011-08-16 17:32:20 +0000 | [diff] [blame] | 362 | flexcan_write(reg, ®s->mcr); |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 363 | |
Marc Kleine-Budde | 9b00b30 | 2014-02-28 15:30:18 +0100 | [diff] [blame] | 364 | while (timeout-- && (flexcan_read(®s->mcr) & FLEXCAN_MCR_LPM_ACK)) |
David Jander | 8badd65 | 2014-08-27 12:02:16 +0200 | [diff] [blame] | 365 | udelay(10); |
Marc Kleine-Budde | 9b00b30 | 2014-02-28 15:30:18 +0100 | [diff] [blame] | 366 | |
| 367 | if (flexcan_read(®s->mcr) & FLEXCAN_MCR_LPM_ACK) |
| 368 | return -ETIMEDOUT; |
| 369 | |
| 370 | return 0; |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 371 | } |
| 372 | |
Marc Kleine-Budde | 9b00b30 | 2014-02-28 15:30:18 +0100 | [diff] [blame] | 373 | static int flexcan_chip_disable(struct flexcan_priv *priv) |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 374 | { |
Marc Kleine-Budde | 89af874 | 2015-05-08 09:32:58 +0200 | [diff] [blame] | 375 | struct flexcan_regs __iomem *regs = priv->regs; |
Marc Kleine-Budde | 9b00b30 | 2014-02-28 15:30:18 +0100 | [diff] [blame] | 376 | unsigned int timeout = FLEXCAN_TIMEOUT_US / 10; |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 377 | u32 reg; |
| 378 | |
holt@sgi.com | 61e271e | 2011-08-16 17:32:20 +0000 | [diff] [blame] | 379 | reg = flexcan_read(®s->mcr); |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 380 | reg |= FLEXCAN_MCR_MDIS; |
holt@sgi.com | 61e271e | 2011-08-16 17:32:20 +0000 | [diff] [blame] | 381 | flexcan_write(reg, ®s->mcr); |
Marc Kleine-Budde | 9b00b30 | 2014-02-28 15:30:18 +0100 | [diff] [blame] | 382 | |
| 383 | while (timeout-- && !(flexcan_read(®s->mcr) & FLEXCAN_MCR_LPM_ACK)) |
David Jander | 8badd65 | 2014-08-27 12:02:16 +0200 | [diff] [blame] | 384 | udelay(10); |
Marc Kleine-Budde | 9b00b30 | 2014-02-28 15:30:18 +0100 | [diff] [blame] | 385 | |
| 386 | if (!(flexcan_read(®s->mcr) & FLEXCAN_MCR_LPM_ACK)) |
| 387 | return -ETIMEDOUT; |
| 388 | |
| 389 | return 0; |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 390 | } |
| 391 | |
Marc Kleine-Budde | b1aa1c7 | 2014-02-28 17:08:21 +0100 | [diff] [blame] | 392 | static int flexcan_chip_freeze(struct flexcan_priv *priv) |
| 393 | { |
Marc Kleine-Budde | 89af874 | 2015-05-08 09:32:58 +0200 | [diff] [blame] | 394 | struct flexcan_regs __iomem *regs = priv->regs; |
Marc Kleine-Budde | b1aa1c7 | 2014-02-28 17:08:21 +0100 | [diff] [blame] | 395 | unsigned int timeout = 1000 * 1000 * 10 / priv->can.bittiming.bitrate; |
| 396 | u32 reg; |
| 397 | |
| 398 | reg = flexcan_read(®s->mcr); |
| 399 | reg |= FLEXCAN_MCR_HALT; |
| 400 | flexcan_write(reg, ®s->mcr); |
| 401 | |
| 402 | while (timeout-- && !(flexcan_read(®s->mcr) & FLEXCAN_MCR_FRZ_ACK)) |
David Jander | 8badd65 | 2014-08-27 12:02:16 +0200 | [diff] [blame] | 403 | udelay(100); |
Marc Kleine-Budde | b1aa1c7 | 2014-02-28 17:08:21 +0100 | [diff] [blame] | 404 | |
| 405 | if (!(flexcan_read(®s->mcr) & FLEXCAN_MCR_FRZ_ACK)) |
| 406 | return -ETIMEDOUT; |
| 407 | |
| 408 | return 0; |
| 409 | } |
| 410 | |
| 411 | static int flexcan_chip_unfreeze(struct flexcan_priv *priv) |
| 412 | { |
Marc Kleine-Budde | 89af874 | 2015-05-08 09:32:58 +0200 | [diff] [blame] | 413 | struct flexcan_regs __iomem *regs = priv->regs; |
Marc Kleine-Budde | b1aa1c7 | 2014-02-28 17:08:21 +0100 | [diff] [blame] | 414 | unsigned int timeout = FLEXCAN_TIMEOUT_US / 10; |
| 415 | u32 reg; |
| 416 | |
| 417 | reg = flexcan_read(®s->mcr); |
| 418 | reg &= ~FLEXCAN_MCR_HALT; |
| 419 | flexcan_write(reg, ®s->mcr); |
| 420 | |
| 421 | while (timeout-- && (flexcan_read(®s->mcr) & FLEXCAN_MCR_FRZ_ACK)) |
David Jander | 8badd65 | 2014-08-27 12:02:16 +0200 | [diff] [blame] | 422 | udelay(10); |
Marc Kleine-Budde | b1aa1c7 | 2014-02-28 17:08:21 +0100 | [diff] [blame] | 423 | |
| 424 | if (flexcan_read(®s->mcr) & FLEXCAN_MCR_FRZ_ACK) |
| 425 | return -ETIMEDOUT; |
| 426 | |
| 427 | return 0; |
| 428 | } |
| 429 | |
Marc Kleine-Budde | 4b5b822 | 2014-02-28 15:16:59 +0100 | [diff] [blame] | 430 | static int flexcan_chip_softreset(struct flexcan_priv *priv) |
| 431 | { |
Marc Kleine-Budde | 89af874 | 2015-05-08 09:32:58 +0200 | [diff] [blame] | 432 | struct flexcan_regs __iomem *regs = priv->regs; |
Marc Kleine-Budde | 4b5b822 | 2014-02-28 15:16:59 +0100 | [diff] [blame] | 433 | unsigned int timeout = FLEXCAN_TIMEOUT_US / 10; |
| 434 | |
| 435 | flexcan_write(FLEXCAN_MCR_SOFTRST, ®s->mcr); |
| 436 | while (timeout-- && (flexcan_read(®s->mcr) & FLEXCAN_MCR_SOFTRST)) |
David Jander | 8badd65 | 2014-08-27 12:02:16 +0200 | [diff] [blame] | 437 | udelay(10); |
Marc Kleine-Budde | 4b5b822 | 2014-02-28 15:16:59 +0100 | [diff] [blame] | 438 | |
| 439 | if (flexcan_read(®s->mcr) & FLEXCAN_MCR_SOFTRST) |
| 440 | return -ETIMEDOUT; |
| 441 | |
| 442 | return 0; |
| 443 | } |
| 444 | |
Stefan Agner | ec56acf | 2014-07-15 14:56:20 +0200 | [diff] [blame] | 445 | static int __flexcan_get_berr_counter(const struct net_device *dev, |
| 446 | struct can_berr_counter *bec) |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 447 | { |
| 448 | const struct flexcan_priv *priv = netdev_priv(dev); |
Marc Kleine-Budde | 89af874 | 2015-05-08 09:32:58 +0200 | [diff] [blame] | 449 | struct flexcan_regs __iomem *regs = priv->regs; |
holt@sgi.com | 61e271e | 2011-08-16 17:32:20 +0000 | [diff] [blame] | 450 | u32 reg = flexcan_read(®s->ecr); |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 451 | |
| 452 | bec->txerr = (reg >> 0) & 0xff; |
| 453 | bec->rxerr = (reg >> 8) & 0xff; |
| 454 | |
| 455 | return 0; |
| 456 | } |
| 457 | |
Stefan Agner | ec56acf | 2014-07-15 14:56:20 +0200 | [diff] [blame] | 458 | static int flexcan_get_berr_counter(const struct net_device *dev, |
| 459 | struct can_berr_counter *bec) |
| 460 | { |
| 461 | const struct flexcan_priv *priv = netdev_priv(dev); |
| 462 | int err; |
| 463 | |
| 464 | err = clk_prepare_enable(priv->clk_ipg); |
| 465 | if (err) |
| 466 | return err; |
| 467 | |
| 468 | err = clk_prepare_enable(priv->clk_per); |
| 469 | if (err) |
| 470 | goto out_disable_ipg; |
| 471 | |
| 472 | err = __flexcan_get_berr_counter(dev, bec); |
| 473 | |
| 474 | clk_disable_unprepare(priv->clk_per); |
| 475 | out_disable_ipg: |
| 476 | clk_disable_unprepare(priv->clk_ipg); |
| 477 | |
| 478 | return err; |
| 479 | } |
| 480 | |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 481 | static int flexcan_start_xmit(struct sk_buff *skb, struct net_device *dev) |
| 482 | { |
| 483 | const struct flexcan_priv *priv = netdev_priv(dev); |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 484 | struct can_frame *cf = (struct can_frame *)skb->data; |
| 485 | u32 can_id; |
Marc Kleine-Budde | 0012e5c | 2015-08-06 14:53:57 +0200 | [diff] [blame] | 486 | u32 data; |
Marc Kleine-Budde | 10d089b | 2014-09-23 11:18:11 +0200 | [diff] [blame] | 487 | u32 ctrl = FLEXCAN_MB_CODE_TX_DATA | (cf->can_dlc << 16); |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 488 | |
| 489 | if (can_dropped_invalid_skb(dev, skb)) |
| 490 | return NETDEV_TX_OK; |
| 491 | |
| 492 | netif_stop_queue(dev); |
| 493 | |
| 494 | if (cf->can_id & CAN_EFF_FLAG) { |
| 495 | can_id = cf->can_id & CAN_EFF_MASK; |
| 496 | ctrl |= FLEXCAN_MB_CNT_IDE | FLEXCAN_MB_CNT_SRR; |
| 497 | } else { |
| 498 | can_id = (cf->can_id & CAN_SFF_MASK) << 18; |
| 499 | } |
| 500 | |
| 501 | if (cf->can_id & CAN_RTR_FLAG) |
| 502 | ctrl |= FLEXCAN_MB_CNT_RTR; |
| 503 | |
| 504 | if (cf->can_dlc > 0) { |
Marc Kleine-Budde | 0012e5c | 2015-08-06 14:53:57 +0200 | [diff] [blame] | 505 | data = be32_to_cpup((__be32 *)&cf->data[0]); |
Marc Kleine-Budde | b93917c | 2015-07-12 00:47:47 +0200 | [diff] [blame] | 506 | flexcan_write(data, &priv->tx_mb->data[0]); |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 507 | } |
| 508 | if (cf->can_dlc > 3) { |
Marc Kleine-Budde | 0012e5c | 2015-08-06 14:53:57 +0200 | [diff] [blame] | 509 | data = be32_to_cpup((__be32 *)&cf->data[4]); |
Marc Kleine-Budde | b93917c | 2015-07-12 00:47:47 +0200 | [diff] [blame] | 510 | flexcan_write(data, &priv->tx_mb->data[1]); |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 511 | } |
| 512 | |
Reuben Dowle | 9a12349 | 2011-11-01 11:18:03 +1300 | [diff] [blame] | 513 | can_put_echo_skb(skb, dev, 0); |
| 514 | |
Marc Kleine-Budde | b93917c | 2015-07-12 00:47:47 +0200 | [diff] [blame] | 515 | flexcan_write(can_id, &priv->tx_mb->can_id); |
| 516 | flexcan_write(ctrl, &priv->tx_mb->can_ctrl); |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 517 | |
David Jander | 25e9244 | 2014-09-03 16:47:22 +0200 | [diff] [blame] | 518 | /* Errata ERR005829 step8: |
| 519 | * Write twice INACTIVE(0x8) code to first MB. |
| 520 | */ |
| 521 | flexcan_write(FLEXCAN_MB_CODE_TX_INACTIVE, |
Marc Kleine-Budde | b93917c | 2015-07-12 00:47:47 +0200 | [diff] [blame] | 522 | &priv->tx_mb_reserved->can_ctrl); |
David Jander | 25e9244 | 2014-09-03 16:47:22 +0200 | [diff] [blame] | 523 | flexcan_write(FLEXCAN_MB_CODE_TX_INACTIVE, |
Marc Kleine-Budde | b93917c | 2015-07-12 00:47:47 +0200 | [diff] [blame] | 524 | &priv->tx_mb_reserved->can_ctrl); |
David Jander | 25e9244 | 2014-09-03 16:47:22 +0200 | [diff] [blame] | 525 | |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 526 | return NETDEV_TX_OK; |
| 527 | } |
| 528 | |
Marc Kleine-Budde | 3016475 | 2015-05-10 15:26:58 +0200 | [diff] [blame] | 529 | 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] | 530 | { |
| 531 | struct flexcan_priv *priv = netdev_priv(dev); |
Marc Kleine-Budde | a5c02f66 | 2017-01-18 11:38:26 +0100 | [diff] [blame] | 532 | struct sk_buff *skb; |
| 533 | struct can_frame *cf; |
Marc Kleine-Budde | d166f56 | 2017-01-17 17:33:46 +0100 | [diff] [blame] | 534 | bool rx_errors = false, tx_errors = false; |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 535 | |
Marc Kleine-Budde | a5c02f66 | 2017-01-18 11:38:26 +0100 | [diff] [blame] | 536 | skb = alloc_can_err_skb(dev, &cf); |
| 537 | if (unlikely(!skb)) |
Marc Kleine-Budde | 3016475 | 2015-05-10 15:26:58 +0200 | [diff] [blame] | 538 | return; |
Marc Kleine-Budde | a5c02f66 | 2017-01-18 11:38:26 +0100 | [diff] [blame] | 539 | |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 540 | cf->can_id |= CAN_ERR_PROT | CAN_ERR_BUSERROR; |
| 541 | |
| 542 | if (reg_esr & FLEXCAN_ESR_BIT1_ERR) { |
Wolfgang Grandegger | aabdfd6 | 2012-02-01 11:02:05 +0100 | [diff] [blame] | 543 | netdev_dbg(dev, "BIT1_ERR irq\n"); |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 544 | cf->data[2] |= CAN_ERR_PROT_BIT1; |
Marc Kleine-Budde | d166f56 | 2017-01-17 17:33:46 +0100 | [diff] [blame] | 545 | tx_errors = true; |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 546 | } |
| 547 | if (reg_esr & FLEXCAN_ESR_BIT0_ERR) { |
Wolfgang Grandegger | aabdfd6 | 2012-02-01 11:02:05 +0100 | [diff] [blame] | 548 | netdev_dbg(dev, "BIT0_ERR irq\n"); |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 549 | cf->data[2] |= CAN_ERR_PROT_BIT0; |
Marc Kleine-Budde | d166f56 | 2017-01-17 17:33:46 +0100 | [diff] [blame] | 550 | tx_errors = true; |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 551 | } |
| 552 | if (reg_esr & FLEXCAN_ESR_ACK_ERR) { |
Wolfgang Grandegger | aabdfd6 | 2012-02-01 11:02:05 +0100 | [diff] [blame] | 553 | netdev_dbg(dev, "ACK_ERR irq\n"); |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 554 | cf->can_id |= CAN_ERR_ACK; |
Oliver Hartkopp | ffd461f | 2015-11-21 18:41:20 +0100 | [diff] [blame] | 555 | cf->data[3] = CAN_ERR_PROT_LOC_ACK; |
Marc Kleine-Budde | d166f56 | 2017-01-17 17:33:46 +0100 | [diff] [blame] | 556 | tx_errors = true; |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 557 | } |
| 558 | if (reg_esr & FLEXCAN_ESR_CRC_ERR) { |
Wolfgang Grandegger | aabdfd6 | 2012-02-01 11:02:05 +0100 | [diff] [blame] | 559 | netdev_dbg(dev, "CRC_ERR irq\n"); |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 560 | cf->data[2] |= CAN_ERR_PROT_BIT; |
Oliver Hartkopp | ffd461f | 2015-11-21 18:41:20 +0100 | [diff] [blame] | 561 | cf->data[3] = CAN_ERR_PROT_LOC_CRC_SEQ; |
Marc Kleine-Budde | d166f56 | 2017-01-17 17:33:46 +0100 | [diff] [blame] | 562 | rx_errors = true; |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 563 | } |
| 564 | if (reg_esr & FLEXCAN_ESR_FRM_ERR) { |
Wolfgang Grandegger | aabdfd6 | 2012-02-01 11:02:05 +0100 | [diff] [blame] | 565 | netdev_dbg(dev, "FRM_ERR irq\n"); |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 566 | cf->data[2] |= CAN_ERR_PROT_FORM; |
Marc Kleine-Budde | d166f56 | 2017-01-17 17:33:46 +0100 | [diff] [blame] | 567 | rx_errors = true; |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 568 | } |
| 569 | if (reg_esr & FLEXCAN_ESR_STF_ERR) { |
Wolfgang Grandegger | aabdfd6 | 2012-02-01 11:02:05 +0100 | [diff] [blame] | 570 | netdev_dbg(dev, "STF_ERR irq\n"); |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 571 | cf->data[2] |= CAN_ERR_PROT_STUFF; |
Marc Kleine-Budde | d166f56 | 2017-01-17 17:33:46 +0100 | [diff] [blame] | 572 | rx_errors = true; |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 573 | } |
| 574 | |
| 575 | priv->can.can_stats.bus_error++; |
| 576 | if (rx_errors) |
| 577 | dev->stats.rx_errors++; |
| 578 | if (tx_errors) |
| 579 | dev->stats.tx_errors++; |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 580 | |
Marc Kleine-Budde | 3016475 | 2015-05-10 15:26:58 +0200 | [diff] [blame] | 581 | can_rx_offload_irq_queue_err_skb(&priv->offload, skb); |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 582 | } |
| 583 | |
Marc Kleine-Budde | 3016475 | 2015-05-10 15:26:58 +0200 | [diff] [blame] | 584 | 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] | 585 | { |
| 586 | struct flexcan_priv *priv = netdev_priv(dev); |
| 587 | struct sk_buff *skb; |
| 588 | struct can_frame *cf; |
Marc Kleine-Budde | 238443d | 2017-01-18 11:25:41 +0100 | [diff] [blame] | 589 | enum can_state new_state, rx_state, tx_state; |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 590 | int flt; |
Andri Yngvason | 71a3aed | 2014-12-03 17:54:15 +0000 | [diff] [blame] | 591 | struct can_berr_counter bec; |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 592 | |
| 593 | flt = reg_esr & FLEXCAN_ESR_FLT_CONF_MASK; |
| 594 | if (likely(flt == FLEXCAN_ESR_FLT_CONF_ACTIVE)) { |
Andri Yngvason | 71a3aed | 2014-12-03 17:54:15 +0000 | [diff] [blame] | 595 | tx_state = unlikely(reg_esr & FLEXCAN_ESR_TX_WRN) ? |
Marc Kleine-Budde | 0012e5c | 2015-08-06 14:53:57 +0200 | [diff] [blame] | 596 | CAN_STATE_ERROR_WARNING : CAN_STATE_ERROR_ACTIVE; |
Andri Yngvason | 71a3aed | 2014-12-03 17:54:15 +0000 | [diff] [blame] | 597 | rx_state = unlikely(reg_esr & FLEXCAN_ESR_RX_WRN) ? |
Marc Kleine-Budde | 0012e5c | 2015-08-06 14:53:57 +0200 | [diff] [blame] | 598 | CAN_STATE_ERROR_WARNING : CAN_STATE_ERROR_ACTIVE; |
Andri Yngvason | 71a3aed | 2014-12-03 17:54:15 +0000 | [diff] [blame] | 599 | new_state = max(tx_state, rx_state); |
Andri Yngvason | 258ce80 | 2015-03-17 13:03:09 +0000 | [diff] [blame] | 600 | } else { |
Andri Yngvason | 71a3aed | 2014-12-03 17:54:15 +0000 | [diff] [blame] | 601 | __flexcan_get_berr_counter(dev, &bec); |
Andri Yngvason | 258ce80 | 2015-03-17 13:03:09 +0000 | [diff] [blame] | 602 | new_state = flt == FLEXCAN_ESR_FLT_CONF_PASSIVE ? |
Marc Kleine-Budde | 0012e5c | 2015-08-06 14:53:57 +0200 | [diff] [blame] | 603 | CAN_STATE_ERROR_PASSIVE : CAN_STATE_BUS_OFF; |
Andri Yngvason | 71a3aed | 2014-12-03 17:54:15 +0000 | [diff] [blame] | 604 | rx_state = bec.rxerr >= bec.txerr ? new_state : 0; |
| 605 | tx_state = bec.rxerr <= bec.txerr ? new_state : 0; |
Andri Yngvason | 71a3aed | 2014-12-03 17:54:15 +0000 | [diff] [blame] | 606 | } |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 607 | |
| 608 | /* state hasn't changed */ |
| 609 | if (likely(new_state == priv->can.state)) |
Marc Kleine-Budde | 3016475 | 2015-05-10 15:26:58 +0200 | [diff] [blame] | 610 | return; |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 611 | |
| 612 | skb = alloc_can_err_skb(dev, &cf); |
| 613 | if (unlikely(!skb)) |
Marc Kleine-Budde | 3016475 | 2015-05-10 15:26:58 +0200 | [diff] [blame] | 614 | return; |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 615 | |
Andri Yngvason | 71a3aed | 2014-12-03 17:54:15 +0000 | [diff] [blame] | 616 | can_change_state(dev, cf, tx_state, rx_state); |
| 617 | |
| 618 | if (unlikely(new_state == CAN_STATE_BUS_OFF)) |
| 619 | can_bus_off(dev); |
| 620 | |
Marc Kleine-Budde | 3016475 | 2015-05-10 15:26:58 +0200 | [diff] [blame] | 621 | can_rx_offload_irq_queue_err_skb(&priv->offload, skb); |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 622 | } |
| 623 | |
Marc Kleine-Budde | 3016475 | 2015-05-10 15:26:58 +0200 | [diff] [blame] | 624 | 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] | 625 | { |
Marc Kleine-Budde | 3016475 | 2015-05-10 15:26:58 +0200 | [diff] [blame] | 626 | return container_of(offload, struct flexcan_priv, offload); |
| 627 | } |
| 628 | |
| 629 | static unsigned int flexcan_mailbox_read(struct can_rx_offload *offload, |
| 630 | struct can_frame *cf, |
| 631 | u32 *timestamp, unsigned int n) |
| 632 | { |
| 633 | struct flexcan_priv *priv = rx_offload_to_priv(offload); |
Marc Kleine-Budde | 89af874 | 2015-05-08 09:32:58 +0200 | [diff] [blame] | 634 | struct flexcan_regs __iomem *regs = priv->regs; |
Marc Kleine-Budde | 3016475 | 2015-05-10 15:26:58 +0200 | [diff] [blame] | 635 | struct flexcan_mb __iomem *mb = ®s->mb[n]; |
| 636 | u32 reg_ctrl, reg_id, reg_iflag1; |
| 637 | |
Marc Kleine-Budde | b3cf53e | 2015-09-01 09:00:13 +0200 | [diff] [blame] | 638 | if (priv->devtype_data->quirks & FLEXCAN_QUIRK_USE_OFF_TIMESTAMP) { |
| 639 | u32 code; |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 640 | |
Marc Kleine-Budde | b3cf53e | 2015-09-01 09:00:13 +0200 | [diff] [blame] | 641 | do { |
| 642 | reg_ctrl = flexcan_read(&mb->can_ctrl); |
| 643 | } while (reg_ctrl & FLEXCAN_MB_CODE_RX_BUSY_BIT); |
| 644 | |
| 645 | /* is this MB empty? */ |
| 646 | code = reg_ctrl & FLEXCAN_MB_CODE_MASK; |
| 647 | if ((code != FLEXCAN_MB_CODE_RX_FULL) && |
| 648 | (code != FLEXCAN_MB_CODE_RX_OVERRUN)) |
| 649 | return 0; |
| 650 | |
| 651 | if (code == FLEXCAN_MB_CODE_RX_OVERRUN) { |
| 652 | /* This MB was overrun, we lost data */ |
| 653 | offload->dev->stats.rx_over_errors++; |
| 654 | offload->dev->stats.rx_errors++; |
| 655 | } |
| 656 | } else { |
| 657 | reg_iflag1 = flexcan_read(®s->iflag1); |
| 658 | if (!(reg_iflag1 & FLEXCAN_IFLAG_RX_FIFO_AVAILABLE)) |
| 659 | return 0; |
| 660 | |
| 661 | reg_ctrl = flexcan_read(&mb->can_ctrl); |
| 662 | } |
| 663 | |
Marc Kleine-Budde | 3016475 | 2015-05-10 15:26:58 +0200 | [diff] [blame] | 664 | /* increase timstamp to full 32 bit */ |
| 665 | *timestamp = reg_ctrl << 16; |
| 666 | |
holt@sgi.com | 61e271e | 2011-08-16 17:32:20 +0000 | [diff] [blame] | 667 | reg_id = flexcan_read(&mb->can_id); |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 668 | if (reg_ctrl & FLEXCAN_MB_CNT_IDE) |
| 669 | cf->can_id = ((reg_id >> 0) & CAN_EFF_MASK) | CAN_EFF_FLAG; |
| 670 | else |
| 671 | cf->can_id = (reg_id >> 18) & CAN_SFF_MASK; |
| 672 | |
| 673 | if (reg_ctrl & FLEXCAN_MB_CNT_RTR) |
| 674 | cf->can_id |= CAN_RTR_FLAG; |
| 675 | cf->can_dlc = get_can_dlc((reg_ctrl >> 16) & 0xf); |
| 676 | |
holt@sgi.com | 61e271e | 2011-08-16 17:32:20 +0000 | [diff] [blame] | 677 | *(__be32 *)(cf->data + 0) = cpu_to_be32(flexcan_read(&mb->data[0])); |
| 678 | *(__be32 *)(cf->data + 4) = cpu_to_be32(flexcan_read(&mb->data[1])); |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 679 | |
| 680 | /* mark as read */ |
Marc Kleine-Budde | b3cf53e | 2015-09-01 09:00:13 +0200 | [diff] [blame] | 681 | if (priv->devtype_data->quirks & FLEXCAN_QUIRK_USE_OFF_TIMESTAMP) { |
| 682 | /* Clear IRQ */ |
| 683 | if (n < 32) |
| 684 | flexcan_write(BIT(n), ®s->iflag1); |
| 685 | else |
| 686 | flexcan_write(BIT(n - 32), ®s->iflag2); |
| 687 | } else { |
| 688 | flexcan_write(FLEXCAN_IFLAG_RX_FIFO_AVAILABLE, ®s->iflag1); |
| 689 | flexcan_read(®s->timer); |
| 690 | } |
Fabio Baltieri | adccadb | 2012-12-18 18:50:58 +0100 | [diff] [blame] | 691 | |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 692 | return 1; |
| 693 | } |
| 694 | |
Marc Kleine-Budde | b3cf53e | 2015-09-01 09:00:13 +0200 | [diff] [blame] | 695 | |
| 696 | static inline u64 flexcan_read_reg_iflag_rx(struct flexcan_priv *priv) |
| 697 | { |
| 698 | struct flexcan_regs __iomem *regs = priv->regs; |
| 699 | u32 iflag1, iflag2; |
| 700 | |
| 701 | iflag2 = flexcan_read(®s->iflag2) & priv->reg_imask2_default; |
| 702 | iflag1 = flexcan_read(®s->iflag1) & priv->reg_imask1_default & |
| 703 | ~FLEXCAN_IFLAG_MB(priv->tx_mb_idx); |
| 704 | |
| 705 | return (u64)iflag2 << 32 | iflag1; |
| 706 | } |
| 707 | |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 708 | static irqreturn_t flexcan_irq(int irq, void *dev_id) |
| 709 | { |
| 710 | struct net_device *dev = dev_id; |
| 711 | struct net_device_stats *stats = &dev->stats; |
| 712 | struct flexcan_priv *priv = netdev_priv(dev); |
Marc Kleine-Budde | 89af874 | 2015-05-08 09:32:58 +0200 | [diff] [blame] | 713 | struct flexcan_regs __iomem *regs = priv->regs; |
Marc Kleine-Budde | dd2f122 | 2017-01-18 11:45:14 +0100 | [diff] [blame] | 714 | irqreturn_t handled = IRQ_NONE; |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 715 | u32 reg_iflag1, reg_esr; |
| 716 | |
holt@sgi.com | 61e271e | 2011-08-16 17:32:20 +0000 | [diff] [blame] | 717 | reg_iflag1 = flexcan_read(®s->iflag1); |
Marc Kleine-Budde | 0012e5c | 2015-08-06 14:53:57 +0200 | [diff] [blame] | 718 | |
Marc Kleine-Budde | 3016475 | 2015-05-10 15:26:58 +0200 | [diff] [blame] | 719 | /* reception interrupt */ |
Marc Kleine-Budde | b3cf53e | 2015-09-01 09:00:13 +0200 | [diff] [blame] | 720 | if (priv->devtype_data->quirks & FLEXCAN_QUIRK_USE_OFF_TIMESTAMP) { |
| 721 | u64 reg_iflag; |
| 722 | int ret; |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 723 | |
Marc Kleine-Budde | b3cf53e | 2015-09-01 09:00:13 +0200 | [diff] [blame] | 724 | while ((reg_iflag = flexcan_read_reg_iflag_rx(priv))) { |
| 725 | handled = IRQ_HANDLED; |
| 726 | ret = can_rx_offload_irq_offload_timestamp(&priv->offload, |
| 727 | reg_iflag); |
| 728 | if (!ret) |
| 729 | break; |
| 730 | } |
| 731 | } else { |
| 732 | if (reg_iflag1 & FLEXCAN_IFLAG_RX_FIFO_AVAILABLE) { |
| 733 | handled = IRQ_HANDLED; |
| 734 | can_rx_offload_irq_offload_fifo(&priv->offload); |
| 735 | } |
| 736 | |
| 737 | /* FIFO overflow interrupt */ |
| 738 | if (reg_iflag1 & FLEXCAN_IFLAG_RX_FIFO_OVERFLOW) { |
| 739 | handled = IRQ_HANDLED; |
| 740 | flexcan_write(FLEXCAN_IFLAG_RX_FIFO_OVERFLOW, ®s->iflag1); |
| 741 | dev->stats.rx_over_errors++; |
| 742 | dev->stats.rx_errors++; |
| 743 | } |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 744 | } |
| 745 | |
| 746 | /* transmission complete interrupt */ |
Marc Kleine-Budde | b93917c | 2015-07-12 00:47:47 +0200 | [diff] [blame] | 747 | if (reg_iflag1 & FLEXCAN_IFLAG_MB(priv->tx_mb_idx)) { |
Marc Kleine-Budde | dd2f122 | 2017-01-18 11:45:14 +0100 | [diff] [blame] | 748 | handled = IRQ_HANDLED; |
Reuben Dowle | 9a12349 | 2011-11-01 11:18:03 +1300 | [diff] [blame] | 749 | stats->tx_bytes += can_get_echo_skb(dev, 0); |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 750 | stats->tx_packets++; |
Fabio Baltieri | adccadb | 2012-12-18 18:50:58 +0100 | [diff] [blame] | 751 | can_led_event(dev, CAN_LED_EVENT_TX); |
Marc Kleine-Budde | 0012e5c | 2015-08-06 14:53:57 +0200 | [diff] [blame] | 752 | |
| 753 | /* after sending a RTR frame MB is in RX mode */ |
Marc Kleine-Budde | de59448 | 2014-09-16 15:31:27 +0200 | [diff] [blame] | 754 | flexcan_write(FLEXCAN_MB_CODE_TX_INACTIVE, |
Marc Kleine-Budde | b93917c | 2015-07-12 00:47:47 +0200 | [diff] [blame] | 755 | &priv->tx_mb->can_ctrl); |
| 756 | flexcan_write(FLEXCAN_IFLAG_MB(priv->tx_mb_idx), ®s->iflag1); |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 757 | netif_wake_queue(dev); |
| 758 | } |
| 759 | |
Marc Kleine-Budde | 3016475 | 2015-05-10 15:26:58 +0200 | [diff] [blame] | 760 | reg_esr = flexcan_read(®s->esr); |
| 761 | |
Marc Kleine-Budde | dd2f122 | 2017-01-18 11:45:14 +0100 | [diff] [blame] | 762 | /* ACK all bus error and state change IRQ sources */ |
| 763 | if (reg_esr & FLEXCAN_ESR_ALL_INT) { |
| 764 | handled = IRQ_HANDLED; |
| 765 | flexcan_write(reg_esr & FLEXCAN_ESR_ALL_INT, ®s->esr); |
| 766 | } |
| 767 | |
Marc Kleine-Budde | 3016475 | 2015-05-10 15:26:58 +0200 | [diff] [blame] | 768 | /* state change interrupt */ |
| 769 | if (reg_esr & FLEXCAN_ESR_ERR_STATE) |
| 770 | flexcan_irq_state(dev, reg_esr); |
| 771 | |
| 772 | /* bus error IRQ - handle if bus error reporting is activated */ |
| 773 | if ((reg_esr & FLEXCAN_ESR_ERR_BUS) && |
| 774 | (priv->can.ctrlmode & CAN_CTRLMODE_BERR_REPORTING)) |
| 775 | flexcan_irq_bus_err(dev, reg_esr); |
| 776 | |
Marc Kleine-Budde | dd2f122 | 2017-01-18 11:45:14 +0100 | [diff] [blame] | 777 | return handled; |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 778 | } |
| 779 | |
| 780 | static void flexcan_set_bittiming(struct net_device *dev) |
| 781 | { |
| 782 | const struct flexcan_priv *priv = netdev_priv(dev); |
| 783 | const struct can_bittiming *bt = &priv->can.bittiming; |
Marc Kleine-Budde | 89af874 | 2015-05-08 09:32:58 +0200 | [diff] [blame] | 784 | struct flexcan_regs __iomem *regs = priv->regs; |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 785 | u32 reg; |
| 786 | |
holt@sgi.com | 61e271e | 2011-08-16 17:32:20 +0000 | [diff] [blame] | 787 | reg = flexcan_read(®s->ctrl); |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 788 | reg &= ~(FLEXCAN_CTRL_PRESDIV(0xff) | |
| 789 | FLEXCAN_CTRL_RJW(0x3) | |
| 790 | FLEXCAN_CTRL_PSEG1(0x7) | |
| 791 | FLEXCAN_CTRL_PSEG2(0x7) | |
| 792 | FLEXCAN_CTRL_PROPSEG(0x7) | |
| 793 | FLEXCAN_CTRL_LPB | |
| 794 | FLEXCAN_CTRL_SMP | |
| 795 | FLEXCAN_CTRL_LOM); |
| 796 | |
| 797 | reg |= FLEXCAN_CTRL_PRESDIV(bt->brp - 1) | |
| 798 | FLEXCAN_CTRL_PSEG1(bt->phase_seg1 - 1) | |
| 799 | FLEXCAN_CTRL_PSEG2(bt->phase_seg2 - 1) | |
| 800 | FLEXCAN_CTRL_RJW(bt->sjw - 1) | |
| 801 | FLEXCAN_CTRL_PROPSEG(bt->prop_seg - 1); |
| 802 | |
| 803 | if (priv->can.ctrlmode & CAN_CTRLMODE_LOOPBACK) |
| 804 | reg |= FLEXCAN_CTRL_LPB; |
| 805 | if (priv->can.ctrlmode & CAN_CTRLMODE_LISTENONLY) |
| 806 | reg |= FLEXCAN_CTRL_LOM; |
| 807 | if (priv->can.ctrlmode & CAN_CTRLMODE_3_SAMPLES) |
| 808 | reg |= FLEXCAN_CTRL_SMP; |
| 809 | |
Lucas Stach | 7a4b6c8 | 2015-08-07 17:16:03 +0200 | [diff] [blame] | 810 | netdev_dbg(dev, "writing ctrl=0x%08x\n", reg); |
holt@sgi.com | 61e271e | 2011-08-16 17:32:20 +0000 | [diff] [blame] | 811 | flexcan_write(reg, ®s->ctrl); |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 812 | |
| 813 | /* print chip status */ |
Wolfgang Grandegger | aabdfd6 | 2012-02-01 11:02:05 +0100 | [diff] [blame] | 814 | netdev_dbg(dev, "%s: mcr=0x%08x ctrl=0x%08x\n", __func__, |
| 815 | flexcan_read(®s->mcr), flexcan_read(®s->ctrl)); |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 816 | } |
| 817 | |
Marc Kleine-Budde | 0012e5c | 2015-08-06 14:53:57 +0200 | [diff] [blame] | 818 | /* flexcan_chip_start |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 819 | * |
| 820 | * this functions is entered with clocks enabled |
| 821 | * |
| 822 | */ |
| 823 | static int flexcan_chip_start(struct net_device *dev) |
| 824 | { |
| 825 | struct flexcan_priv *priv = netdev_priv(dev); |
Marc Kleine-Budde | 89af874 | 2015-05-08 09:32:58 +0200 | [diff] [blame] | 826 | struct flexcan_regs __iomem *regs = priv->regs; |
Marc Kleine-Budde | 6f75fce | 2014-09-23 11:03:01 +0200 | [diff] [blame] | 827 | u32 reg_mcr, reg_ctrl, reg_ctrl2, reg_mecr; |
David S. Miller | 1f6d803 | 2014-09-23 12:09:27 -0400 | [diff] [blame] | 828 | int err, i; |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 829 | |
| 830 | /* enable module */ |
Marc Kleine-Budde | 9b00b30 | 2014-02-28 15:30:18 +0100 | [diff] [blame] | 831 | err = flexcan_chip_enable(priv); |
| 832 | if (err) |
| 833 | return err; |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 834 | |
| 835 | /* soft reset */ |
Marc Kleine-Budde | 4b5b822 | 2014-02-28 15:16:59 +0100 | [diff] [blame] | 836 | err = flexcan_chip_softreset(priv); |
| 837 | if (err) |
Marc Kleine-Budde | b1aa1c7 | 2014-02-28 17:08:21 +0100 | [diff] [blame] | 838 | goto out_chip_disable; |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 839 | |
| 840 | flexcan_set_bittiming(dev); |
| 841 | |
Marc Kleine-Budde | 0012e5c | 2015-08-06 14:53:57 +0200 | [diff] [blame] | 842 | /* MCR |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 843 | * |
| 844 | * enable freeze |
| 845 | * enable fifo |
| 846 | * halt now |
| 847 | * only supervisor access |
| 848 | * enable warning int |
Reuben Dowle | 9a12349 | 2011-11-01 11:18:03 +1300 | [diff] [blame] | 849 | * disable local echo |
Marc Kleine-Budde | 4bd888a | 2015-08-31 21:03:29 +0200 | [diff] [blame] | 850 | * enable individual RX masking |
Marc Kleine-Budde | 749de6f | 2015-08-31 21:32:34 +0200 | [diff] [blame] | 851 | * choose format C |
| 852 | * set max mailbox number |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 853 | */ |
holt@sgi.com | 61e271e | 2011-08-16 17:32:20 +0000 | [diff] [blame] | 854 | reg_mcr = flexcan_read(®s->mcr); |
Marc Kleine-Budde | d5a7b40 | 2013-10-04 10:52:36 +0200 | [diff] [blame] | 855 | reg_mcr &= ~FLEXCAN_MCR_MAXMB(0xff); |
Marc Kleine-Budde | b3cf53e | 2015-09-01 09:00:13 +0200 | [diff] [blame] | 856 | reg_mcr |= FLEXCAN_MCR_FRZ | FLEXCAN_MCR_HALT | FLEXCAN_MCR_SUPV | |
| 857 | FLEXCAN_MCR_WRN_EN | FLEXCAN_MCR_SRX_DIS | FLEXCAN_MCR_IRMQ | |
| 858 | FLEXCAN_MCR_IDAM_C; |
| 859 | |
| 860 | if (priv->devtype_data->quirks & FLEXCAN_QUIRK_USE_OFF_TIMESTAMP) { |
| 861 | reg_mcr &= ~FLEXCAN_MCR_FEN; |
| 862 | reg_mcr |= FLEXCAN_MCR_MAXMB(priv->offload.mb_last); |
| 863 | } else { |
| 864 | reg_mcr |= FLEXCAN_MCR_FEN | |
| 865 | FLEXCAN_MCR_MAXMB(priv->tx_mb_idx); |
| 866 | } |
Wolfgang Grandegger | aabdfd6 | 2012-02-01 11:02:05 +0100 | [diff] [blame] | 867 | netdev_dbg(dev, "%s: writing mcr=0x%08x", __func__, reg_mcr); |
holt@sgi.com | 61e271e | 2011-08-16 17:32:20 +0000 | [diff] [blame] | 868 | flexcan_write(reg_mcr, ®s->mcr); |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 869 | |
Marc Kleine-Budde | 0012e5c | 2015-08-06 14:53:57 +0200 | [diff] [blame] | 870 | /* CTRL |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 871 | * |
| 872 | * disable timer sync feature |
| 873 | * |
| 874 | * disable auto busoff recovery |
| 875 | * transmit lowest buffer first |
| 876 | * |
| 877 | * enable tx and rx warning interrupt |
| 878 | * enable bus off interrupt |
| 879 | * (== FLEXCAN_CTRL_ERR_STATE) |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 880 | */ |
holt@sgi.com | 61e271e | 2011-08-16 17:32:20 +0000 | [diff] [blame] | 881 | reg_ctrl = flexcan_read(®s->ctrl); |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 882 | reg_ctrl &= ~FLEXCAN_CTRL_TSYN; |
| 883 | reg_ctrl |= FLEXCAN_CTRL_BOFF_REC | FLEXCAN_CTRL_LBUF | |
Wolfgang Grandegger | 4f72e5f | 2012-09-28 03:17:15 +0000 | [diff] [blame] | 884 | FLEXCAN_CTRL_ERR_STATE; |
Marc Kleine-Budde | 0012e5c | 2015-08-06 14:53:57 +0200 | [diff] [blame] | 885 | |
| 886 | /* enable the "error interrupt" (FLEXCAN_CTRL_ERR_MSK), |
Wolfgang Grandegger | 4f72e5f | 2012-09-28 03:17:15 +0000 | [diff] [blame] | 887 | * on most Flexcan cores, too. Otherwise we don't get |
| 888 | * any error warning or passive interrupts. |
| 889 | */ |
Marc Kleine-Budde | f377bff | 2015-05-08 15:22:36 +0200 | [diff] [blame] | 890 | if (priv->devtype_data->quirks & FLEXCAN_QUIRK_BROKEN_ERR_STATE || |
Wolfgang Grandegger | 4f72e5f | 2012-09-28 03:17:15 +0000 | [diff] [blame] | 891 | priv->can.ctrlmode & CAN_CTRLMODE_BERR_REPORTING) |
| 892 | reg_ctrl |= FLEXCAN_CTRL_ERR_MSK; |
Alexander Stein | bc03a54 | 2014-08-12 10:47:21 +0200 | [diff] [blame] | 893 | else |
| 894 | reg_ctrl &= ~FLEXCAN_CTRL_ERR_MSK; |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 895 | |
| 896 | /* save for later use */ |
| 897 | priv->reg_ctrl_default = reg_ctrl; |
Marc Kleine-Budde | 6fa7da2 | 2015-08-27 14:24:48 +0200 | [diff] [blame] | 898 | /* leave interrupts disabled for now */ |
| 899 | reg_ctrl &= ~FLEXCAN_CTRL_ERR_ALL; |
Wolfgang Grandegger | aabdfd6 | 2012-02-01 11:02:05 +0100 | [diff] [blame] | 900 | netdev_dbg(dev, "%s: writing ctrl=0x%08x", __func__, reg_ctrl); |
holt@sgi.com | 61e271e | 2011-08-16 17:32:20 +0000 | [diff] [blame] | 901 | flexcan_write(reg_ctrl, ®s->ctrl); |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 902 | |
Marc Kleine-Budde | 9eb7aa8 | 2015-09-01 08:57:55 +0200 | [diff] [blame] | 903 | if ((priv->devtype_data->quirks & FLEXCAN_QUIRK_ENABLE_EACEN_RRS)) { |
| 904 | reg_ctrl2 = flexcan_read(®s->ctrl2); |
| 905 | reg_ctrl2 |= FLEXCAN_CTRL2_EACEN | FLEXCAN_CTRL2_RRS; |
| 906 | flexcan_write(reg_ctrl2, ®s->ctrl2); |
| 907 | } |
| 908 | |
David Jander | fc05b88 | 2014-08-27 11:58:05 +0200 | [diff] [blame] | 909 | /* clear and invalidate all mailboxes first */ |
Marc Kleine-Budde | b93917c | 2015-07-12 00:47:47 +0200 | [diff] [blame] | 910 | for (i = priv->tx_mb_idx; i < ARRAY_SIZE(regs->mb); i++) { |
David Jander | fc05b88 | 2014-08-27 11:58:05 +0200 | [diff] [blame] | 911 | flexcan_write(FLEXCAN_MB_CODE_RX_INACTIVE, |
Marc Kleine-Budde | 1ba763d | 2015-08-25 10:39:19 +0200 | [diff] [blame] | 912 | ®s->mb[i].can_ctrl); |
David Jander | fc05b88 | 2014-08-27 11:58:05 +0200 | [diff] [blame] | 913 | } |
| 914 | |
Marc Kleine-Budde | b3cf53e | 2015-09-01 09:00:13 +0200 | [diff] [blame] | 915 | if (priv->devtype_data->quirks & FLEXCAN_QUIRK_USE_OFF_TIMESTAMP) { |
| 916 | for (i = priv->offload.mb_first; i <= priv->offload.mb_last; i++) |
| 917 | flexcan_write(FLEXCAN_MB_CODE_RX_EMPTY, |
| 918 | ®s->mb[i].can_ctrl); |
| 919 | } |
| 920 | |
David Jander | 25e9244 | 2014-09-03 16:47:22 +0200 | [diff] [blame] | 921 | /* Errata ERR005829: mark first TX mailbox as INACTIVE */ |
| 922 | flexcan_write(FLEXCAN_MB_CODE_TX_INACTIVE, |
Marc Kleine-Budde | b93917c | 2015-07-12 00:47:47 +0200 | [diff] [blame] | 923 | &priv->tx_mb_reserved->can_ctrl); |
David Jander | 25e9244 | 2014-09-03 16:47:22 +0200 | [diff] [blame] | 924 | |
Marc Kleine-Budde | c32fe4a | 2014-09-16 12:39:28 +0200 | [diff] [blame] | 925 | /* mark TX mailbox as INACTIVE */ |
| 926 | flexcan_write(FLEXCAN_MB_CODE_TX_INACTIVE, |
Marc Kleine-Budde | b93917c | 2015-07-12 00:47:47 +0200 | [diff] [blame] | 927 | &priv->tx_mb->can_ctrl); |
Marc Kleine-Budde | d5a7b40 | 2013-10-04 10:52:36 +0200 | [diff] [blame] | 928 | |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 929 | /* acceptance mask/acceptance code (accept everything) */ |
holt@sgi.com | 61e271e | 2011-08-16 17:32:20 +0000 | [diff] [blame] | 930 | flexcan_write(0x0, ®s->rxgmask); |
| 931 | flexcan_write(0x0, ®s->rx14mask); |
| 932 | flexcan_write(0x0, ®s->rx15mask); |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 933 | |
Marc Kleine-Budde | f377bff | 2015-05-08 15:22:36 +0200 | [diff] [blame] | 934 | if (priv->devtype_data->quirks & FLEXCAN_QUIRK_DISABLE_RXFG) |
Hui Wang | 30c1e67 | 2012-06-28 16:21:35 +0800 | [diff] [blame] | 935 | flexcan_write(0x0, ®s->rxfgmask); |
| 936 | |
Marc Kleine-Budde | 4bd888a | 2015-08-31 21:03:29 +0200 | [diff] [blame] | 937 | /* clear acceptance filters */ |
| 938 | for (i = 0; i < ARRAY_SIZE(regs->mb); i++) |
| 939 | flexcan_write(0, ®s->rximr[i]); |
| 940 | |
Marc Kleine-Budde | 0012e5c | 2015-08-06 14:53:57 +0200 | [diff] [blame] | 941 | /* On Vybrid, disable memory error detection interrupts |
Stefan Agner | cdce844 | 2014-07-15 14:56:21 +0200 | [diff] [blame] | 942 | * and freeze mode. |
| 943 | * This also works around errata e5295 which generates |
| 944 | * false positive memory errors and put the device in |
| 945 | * freeze mode. |
| 946 | */ |
Marc Kleine-Budde | f377bff | 2015-05-08 15:22:36 +0200 | [diff] [blame] | 947 | if (priv->devtype_data->quirks & FLEXCAN_QUIRK_DISABLE_MECR) { |
Marc Kleine-Budde | 0012e5c | 2015-08-06 14:53:57 +0200 | [diff] [blame] | 948 | /* Follow the protocol as described in "Detection |
Stefan Agner | cdce844 | 2014-07-15 14:56:21 +0200 | [diff] [blame] | 949 | * and Correction of Memory Errors" to write to |
| 950 | * MECR register |
| 951 | */ |
Marc Kleine-Budde | 6f75fce | 2014-09-23 11:03:01 +0200 | [diff] [blame] | 952 | reg_ctrl2 = flexcan_read(®s->ctrl2); |
| 953 | reg_ctrl2 |= FLEXCAN_CTRL2_ECRWRE; |
| 954 | flexcan_write(reg_ctrl2, ®s->ctrl2); |
Stefan Agner | cdce844 | 2014-07-15 14:56:21 +0200 | [diff] [blame] | 955 | |
| 956 | reg_mecr = flexcan_read(®s->mecr); |
| 957 | reg_mecr &= ~FLEXCAN_MECR_ECRWRDIS; |
| 958 | flexcan_write(reg_mecr, ®s->mecr); |
| 959 | reg_mecr &= ~(FLEXCAN_MECR_NCEFAFRZ | FLEXCAN_MECR_HANCEI_MSK | |
Marc Kleine-Budde | 0012e5c | 2015-08-06 14:53:57 +0200 | [diff] [blame] | 960 | FLEXCAN_MECR_FANCEI_MSK); |
Stefan Agner | cdce844 | 2014-07-15 14:56:21 +0200 | [diff] [blame] | 961 | flexcan_write(reg_mecr, ®s->mecr); |
| 962 | } |
| 963 | |
Marc Kleine-Budde | f003698 | 2014-02-28 17:18:27 +0100 | [diff] [blame] | 964 | err = flexcan_transceiver_enable(priv); |
| 965 | if (err) |
Marc Kleine-Budde | b1aa1c7 | 2014-02-28 17:08:21 +0100 | [diff] [blame] | 966 | goto out_chip_disable; |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 967 | |
| 968 | /* synchronize with the can bus */ |
Marc Kleine-Budde | b1aa1c7 | 2014-02-28 17:08:21 +0100 | [diff] [blame] | 969 | err = flexcan_chip_unfreeze(priv); |
| 970 | if (err) |
| 971 | goto out_transceiver_disable; |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 972 | |
| 973 | priv->can.state = CAN_STATE_ERROR_ACTIVE; |
| 974 | |
Marc Kleine-Budde | 6fa7da2 | 2015-08-27 14:24:48 +0200 | [diff] [blame] | 975 | /* enable interrupts atomically */ |
| 976 | disable_irq(dev->irq); |
| 977 | flexcan_write(priv->reg_ctrl_default, ®s->ctrl); |
Marc Kleine-Budde | 28ac7dc | 2015-08-04 13:46:10 +0200 | [diff] [blame] | 978 | flexcan_write(priv->reg_imask1_default, ®s->imask1); |
Marc Kleine-Budde | b3cf53e | 2015-09-01 09:00:13 +0200 | [diff] [blame] | 979 | flexcan_write(priv->reg_imask2_default, ®s->imask2); |
Marc Kleine-Budde | 6fa7da2 | 2015-08-27 14:24:48 +0200 | [diff] [blame] | 980 | enable_irq(dev->irq); |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 981 | |
| 982 | /* print chip status */ |
Wolfgang Grandegger | aabdfd6 | 2012-02-01 11:02:05 +0100 | [diff] [blame] | 983 | netdev_dbg(dev, "%s: reading mcr=0x%08x ctrl=0x%08x\n", __func__, |
| 984 | flexcan_read(®s->mcr), flexcan_read(®s->ctrl)); |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 985 | |
| 986 | return 0; |
| 987 | |
Marc Kleine-Budde | b1aa1c7 | 2014-02-28 17:08:21 +0100 | [diff] [blame] | 988 | out_transceiver_disable: |
| 989 | flexcan_transceiver_disable(priv); |
| 990 | out_chip_disable: |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 991 | flexcan_chip_disable(priv); |
| 992 | return err; |
| 993 | } |
| 994 | |
Marc Kleine-Budde | 0012e5c | 2015-08-06 14:53:57 +0200 | [diff] [blame] | 995 | /* flexcan_chip_stop |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 996 | * |
| 997 | * this functions is entered with clocks enabled |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 998 | */ |
| 999 | static void flexcan_chip_stop(struct net_device *dev) |
| 1000 | { |
| 1001 | struct flexcan_priv *priv = netdev_priv(dev); |
Marc Kleine-Budde | 89af874 | 2015-05-08 09:32:58 +0200 | [diff] [blame] | 1002 | struct flexcan_regs __iomem *regs = priv->regs; |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 1003 | |
Marc Kleine-Budde | b1aa1c7 | 2014-02-28 17:08:21 +0100 | [diff] [blame] | 1004 | /* freeze + disable module */ |
| 1005 | flexcan_chip_freeze(priv); |
| 1006 | flexcan_chip_disable(priv); |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 1007 | |
Marc Kleine-Budde | 5be93bd | 2014-02-19 12:00:51 +0100 | [diff] [blame] | 1008 | /* Disable all interrupts */ |
Marc Kleine-Budde | b3cf53e | 2015-09-01 09:00:13 +0200 | [diff] [blame] | 1009 | flexcan_write(0, ®s->imask2); |
Marc Kleine-Budde | 5be93bd | 2014-02-19 12:00:51 +0100 | [diff] [blame] | 1010 | flexcan_write(0, ®s->imask1); |
| 1011 | flexcan_write(priv->reg_ctrl_default & ~FLEXCAN_CTRL_ERR_ALL, |
| 1012 | ®s->ctrl); |
| 1013 | |
Marc Kleine-Budde | f003698 | 2014-02-28 17:18:27 +0100 | [diff] [blame] | 1014 | flexcan_transceiver_disable(priv); |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 1015 | priv->can.state = CAN_STATE_STOPPED; |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 1016 | } |
| 1017 | |
| 1018 | static int flexcan_open(struct net_device *dev) |
| 1019 | { |
| 1020 | struct flexcan_priv *priv = netdev_priv(dev); |
| 1021 | int err; |
| 1022 | |
Fabio Estevam | aa10181 | 2013-07-22 12:41:40 -0300 | [diff] [blame] | 1023 | err = clk_prepare_enable(priv->clk_ipg); |
| 1024 | if (err) |
| 1025 | return err; |
| 1026 | |
| 1027 | err = clk_prepare_enable(priv->clk_per); |
| 1028 | if (err) |
| 1029 | goto out_disable_ipg; |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 1030 | |
| 1031 | err = open_candev(dev); |
| 1032 | if (err) |
Fabio Estevam | aa10181 | 2013-07-22 12:41:40 -0300 | [diff] [blame] | 1033 | goto out_disable_per; |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 1034 | |
| 1035 | err = request_irq(dev->irq, flexcan_irq, IRQF_SHARED, dev->name, dev); |
| 1036 | if (err) |
| 1037 | goto out_close; |
| 1038 | |
| 1039 | /* start chip and queuing */ |
| 1040 | err = flexcan_chip_start(dev); |
| 1041 | if (err) |
Marc Kleine-Budde | 7e9e148 | 2014-02-28 14:52:01 +0100 | [diff] [blame] | 1042 | goto out_free_irq; |
Fabio Baltieri | adccadb | 2012-12-18 18:50:58 +0100 | [diff] [blame] | 1043 | |
| 1044 | can_led_event(dev, CAN_LED_EVENT_OPEN); |
| 1045 | |
Marc Kleine-Budde | 3016475 | 2015-05-10 15:26:58 +0200 | [diff] [blame] | 1046 | can_rx_offload_enable(&priv->offload); |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 1047 | netif_start_queue(dev); |
| 1048 | |
| 1049 | return 0; |
| 1050 | |
Marc Kleine-Budde | 7e9e148 | 2014-02-28 14:52:01 +0100 | [diff] [blame] | 1051 | out_free_irq: |
| 1052 | free_irq(dev->irq, dev); |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 1053 | out_close: |
| 1054 | close_candev(dev); |
Fabio Estevam | aa10181 | 2013-07-22 12:41:40 -0300 | [diff] [blame] | 1055 | out_disable_per: |
Steffen Trumtrar | 3d42a37 | 2012-07-17 16:14:34 +0200 | [diff] [blame] | 1056 | clk_disable_unprepare(priv->clk_per); |
Fabio Estevam | aa10181 | 2013-07-22 12:41:40 -0300 | [diff] [blame] | 1057 | out_disable_ipg: |
Steffen Trumtrar | 3d42a37 | 2012-07-17 16:14:34 +0200 | [diff] [blame] | 1058 | clk_disable_unprepare(priv->clk_ipg); |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 1059 | |
| 1060 | return err; |
| 1061 | } |
| 1062 | |
| 1063 | static int flexcan_close(struct net_device *dev) |
| 1064 | { |
| 1065 | struct flexcan_priv *priv = netdev_priv(dev); |
| 1066 | |
| 1067 | netif_stop_queue(dev); |
Marc Kleine-Budde | 3016475 | 2015-05-10 15:26:58 +0200 | [diff] [blame] | 1068 | can_rx_offload_disable(&priv->offload); |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 1069 | flexcan_chip_stop(dev); |
| 1070 | |
| 1071 | free_irq(dev->irq, dev); |
Steffen Trumtrar | 3d42a37 | 2012-07-17 16:14:34 +0200 | [diff] [blame] | 1072 | clk_disable_unprepare(priv->clk_per); |
| 1073 | clk_disable_unprepare(priv->clk_ipg); |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 1074 | |
| 1075 | close_candev(dev); |
| 1076 | |
Fabio Baltieri | adccadb | 2012-12-18 18:50:58 +0100 | [diff] [blame] | 1077 | can_led_event(dev, CAN_LED_EVENT_STOP); |
| 1078 | |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 1079 | return 0; |
| 1080 | } |
| 1081 | |
| 1082 | static int flexcan_set_mode(struct net_device *dev, enum can_mode mode) |
| 1083 | { |
| 1084 | int err; |
| 1085 | |
| 1086 | switch (mode) { |
| 1087 | case CAN_MODE_START: |
| 1088 | err = flexcan_chip_start(dev); |
| 1089 | if (err) |
| 1090 | return err; |
| 1091 | |
| 1092 | netif_wake_queue(dev); |
| 1093 | break; |
| 1094 | |
| 1095 | default: |
| 1096 | return -EOPNOTSUPP; |
| 1097 | } |
| 1098 | |
| 1099 | return 0; |
| 1100 | } |
| 1101 | |
| 1102 | static const struct net_device_ops flexcan_netdev_ops = { |
| 1103 | .ndo_open = flexcan_open, |
| 1104 | .ndo_stop = flexcan_close, |
| 1105 | .ndo_start_xmit = flexcan_start_xmit, |
Oliver Hartkopp | c971fa2 | 2014-03-07 09:23:41 +0100 | [diff] [blame] | 1106 | .ndo_change_mtu = can_change_mtu, |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 1107 | }; |
| 1108 | |
Bill Pemberton | 3c8ac0f | 2012-12-03 09:22:44 -0500 | [diff] [blame] | 1109 | static int register_flexcandev(struct net_device *dev) |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 1110 | { |
| 1111 | struct flexcan_priv *priv = netdev_priv(dev); |
Marc Kleine-Budde | 89af874 | 2015-05-08 09:32:58 +0200 | [diff] [blame] | 1112 | struct flexcan_regs __iomem *regs = priv->regs; |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 1113 | u32 reg, err; |
| 1114 | |
Fabio Estevam | aa10181 | 2013-07-22 12:41:40 -0300 | [diff] [blame] | 1115 | err = clk_prepare_enable(priv->clk_ipg); |
| 1116 | if (err) |
| 1117 | return err; |
| 1118 | |
| 1119 | err = clk_prepare_enable(priv->clk_per); |
| 1120 | if (err) |
| 1121 | goto out_disable_ipg; |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 1122 | |
| 1123 | /* select "bus clock", chip must be disabled */ |
Marc Kleine-Budde | 9b00b30 | 2014-02-28 15:30:18 +0100 | [diff] [blame] | 1124 | err = flexcan_chip_disable(priv); |
| 1125 | if (err) |
| 1126 | goto out_disable_per; |
holt@sgi.com | 61e271e | 2011-08-16 17:32:20 +0000 | [diff] [blame] | 1127 | reg = flexcan_read(®s->ctrl); |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 1128 | reg |= FLEXCAN_CTRL_CLK_SRC; |
holt@sgi.com | 61e271e | 2011-08-16 17:32:20 +0000 | [diff] [blame] | 1129 | flexcan_write(reg, ®s->ctrl); |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 1130 | |
Marc Kleine-Budde | 9b00b30 | 2014-02-28 15:30:18 +0100 | [diff] [blame] | 1131 | err = flexcan_chip_enable(priv); |
| 1132 | if (err) |
| 1133 | goto out_chip_disable; |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 1134 | |
| 1135 | /* set freeze, halt and activate FIFO, restrict register access */ |
holt@sgi.com | 61e271e | 2011-08-16 17:32:20 +0000 | [diff] [blame] | 1136 | reg = flexcan_read(®s->mcr); |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 1137 | reg |= FLEXCAN_MCR_FRZ | FLEXCAN_MCR_HALT | |
| 1138 | FLEXCAN_MCR_FEN | FLEXCAN_MCR_SUPV; |
holt@sgi.com | 61e271e | 2011-08-16 17:32:20 +0000 | [diff] [blame] | 1139 | flexcan_write(reg, ®s->mcr); |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 1140 | |
Marc Kleine-Budde | 0012e5c | 2015-08-06 14:53:57 +0200 | [diff] [blame] | 1141 | /* Currently we only support newer versions of this core |
Marc Kleine-Budde | b3cf53e | 2015-09-01 09:00:13 +0200 | [diff] [blame] | 1142 | * featuring a RX hardware FIFO (although this driver doesn't |
| 1143 | * make use of it on some cores). Older cores, found on some |
| 1144 | * Coldfire derivates are not tested. |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 1145 | */ |
holt@sgi.com | 61e271e | 2011-08-16 17:32:20 +0000 | [diff] [blame] | 1146 | reg = flexcan_read(®s->mcr); |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 1147 | if (!(reg & FLEXCAN_MCR_FEN)) { |
Wolfgang Grandegger | aabdfd6 | 2012-02-01 11:02:05 +0100 | [diff] [blame] | 1148 | netdev_err(dev, "Could not enable RX FIFO, unsupported core\n"); |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 1149 | err = -ENODEV; |
Marc Kleine-Budde | 9b00b30 | 2014-02-28 15:30:18 +0100 | [diff] [blame] | 1150 | goto out_chip_disable; |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 1151 | } |
| 1152 | |
| 1153 | err = register_candev(dev); |
| 1154 | |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 1155 | /* disable core and turn off clocks */ |
Marc Kleine-Budde | 9b00b30 | 2014-02-28 15:30:18 +0100 | [diff] [blame] | 1156 | out_chip_disable: |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 1157 | flexcan_chip_disable(priv); |
Marc Kleine-Budde | 9b00b30 | 2014-02-28 15:30:18 +0100 | [diff] [blame] | 1158 | out_disable_per: |
Steffen Trumtrar | 3d42a37 | 2012-07-17 16:14:34 +0200 | [diff] [blame] | 1159 | clk_disable_unprepare(priv->clk_per); |
Fabio Estevam | aa10181 | 2013-07-22 12:41:40 -0300 | [diff] [blame] | 1160 | out_disable_ipg: |
Steffen Trumtrar | 3d42a37 | 2012-07-17 16:14:34 +0200 | [diff] [blame] | 1161 | clk_disable_unprepare(priv->clk_ipg); |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 1162 | |
| 1163 | return err; |
| 1164 | } |
| 1165 | |
Bill Pemberton | 3c8ac0f | 2012-12-03 09:22:44 -0500 | [diff] [blame] | 1166 | static void unregister_flexcandev(struct net_device *dev) |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 1167 | { |
| 1168 | unregister_candev(dev); |
| 1169 | } |
| 1170 | |
Hui Wang | 30c1e67 | 2012-06-28 16:21:35 +0800 | [diff] [blame] | 1171 | static const struct of_device_id flexcan_of_match[] = { |
Hui Wang | 30c1e67 | 2012-06-28 16:21:35 +0800 | [diff] [blame] | 1172 | { .compatible = "fsl,imx6q-flexcan", .data = &fsl_imx6q_devtype_data, }, |
Marc Kleine-Budde | e358784 | 2013-10-03 23:51:55 +0200 | [diff] [blame] | 1173 | { .compatible = "fsl,imx28-flexcan", .data = &fsl_imx28_devtype_data, }, |
| 1174 | { .compatible = "fsl,p1010-flexcan", .data = &fsl_p1010_devtype_data, }, |
Stefan Agner | cdce844 | 2014-07-15 14:56:21 +0200 | [diff] [blame] | 1175 | { .compatible = "fsl,vf610-flexcan", .data = &fsl_vf610_devtype_data, }, |
Hui Wang | 30c1e67 | 2012-06-28 16:21:35 +0800 | [diff] [blame] | 1176 | { /* sentinel */ }, |
| 1177 | }; |
Marc Kleine-Budde | 4358a9d | 2012-10-04 10:55:35 +0200 | [diff] [blame] | 1178 | MODULE_DEVICE_TABLE(of, flexcan_of_match); |
Hui Wang | 30c1e67 | 2012-06-28 16:21:35 +0800 | [diff] [blame] | 1179 | |
| 1180 | static const struct platform_device_id flexcan_id_table[] = { |
| 1181 | { .name = "flexcan", .driver_data = (kernel_ulong_t)&fsl_p1010_devtype_data, }, |
| 1182 | { /* sentinel */ }, |
| 1183 | }; |
Marc Kleine-Budde | 4358a9d | 2012-10-04 10:55:35 +0200 | [diff] [blame] | 1184 | MODULE_DEVICE_TABLE(platform, flexcan_id_table); |
Hui Wang | 30c1e67 | 2012-06-28 16:21:35 +0800 | [diff] [blame] | 1185 | |
Bill Pemberton | 3c8ac0f | 2012-12-03 09:22:44 -0500 | [diff] [blame] | 1186 | static int flexcan_probe(struct platform_device *pdev) |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 1187 | { |
Hui Wang | 30c1e67 | 2012-06-28 16:21:35 +0800 | [diff] [blame] | 1188 | const struct of_device_id *of_id; |
Marc Kleine-Budde | dda0b3b | 2012-07-13 14:52:48 +0200 | [diff] [blame] | 1189 | const struct flexcan_devtype_data *devtype_data; |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 1190 | struct net_device *dev; |
| 1191 | struct flexcan_priv *priv; |
Andreas Werner | 555828e | 2015-03-22 17:35:52 +0100 | [diff] [blame] | 1192 | struct regulator *reg_xceiver; |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 1193 | struct resource *mem; |
Steffen Trumtrar | 3d42a37 | 2012-07-17 16:14:34 +0200 | [diff] [blame] | 1194 | struct clk *clk_ipg = NULL, *clk_per = NULL; |
Marc Kleine-Budde | 89af874 | 2015-05-08 09:32:58 +0200 | [diff] [blame] | 1195 | struct flexcan_regs __iomem *regs; |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 1196 | int err, irq; |
holt@sgi.com | 97efe9a | 2011-08-16 17:32:23 +0000 | [diff] [blame] | 1197 | u32 clock_freq = 0; |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 1198 | |
Andreas Werner | 555828e | 2015-03-22 17:35:52 +0100 | [diff] [blame] | 1199 | reg_xceiver = devm_regulator_get(&pdev->dev, "xceiver"); |
| 1200 | if (PTR_ERR(reg_xceiver) == -EPROBE_DEFER) |
| 1201 | return -EPROBE_DEFER; |
| 1202 | else if (IS_ERR(reg_xceiver)) |
| 1203 | reg_xceiver = NULL; |
| 1204 | |
Hui Wang | afc016d | 2012-06-28 16:21:34 +0800 | [diff] [blame] | 1205 | if (pdev->dev.of_node) |
| 1206 | of_property_read_u32(pdev->dev.of_node, |
Marc Kleine-Budde | 0012e5c | 2015-08-06 14:53:57 +0200 | [diff] [blame] | 1207 | "clock-frequency", &clock_freq); |
holt@sgi.com | 97efe9a | 2011-08-16 17:32:23 +0000 | [diff] [blame] | 1208 | |
| 1209 | if (!clock_freq) { |
Steffen Trumtrar | 3d42a37 | 2012-07-17 16:14:34 +0200 | [diff] [blame] | 1210 | clk_ipg = devm_clk_get(&pdev->dev, "ipg"); |
| 1211 | if (IS_ERR(clk_ipg)) { |
| 1212 | dev_err(&pdev->dev, "no ipg clock defined\n"); |
Fabio Estevam | 933e4af | 2013-07-22 12:41:39 -0300 | [diff] [blame] | 1213 | return PTR_ERR(clk_ipg); |
holt@sgi.com | 97efe9a | 2011-08-16 17:32:23 +0000 | [diff] [blame] | 1214 | } |
Steffen Trumtrar | 3d42a37 | 2012-07-17 16:14:34 +0200 | [diff] [blame] | 1215 | |
| 1216 | clk_per = devm_clk_get(&pdev->dev, "per"); |
| 1217 | if (IS_ERR(clk_per)) { |
| 1218 | dev_err(&pdev->dev, "no per clock defined\n"); |
Fabio Estevam | 933e4af | 2013-07-22 12:41:39 -0300 | [diff] [blame] | 1219 | return PTR_ERR(clk_per); |
Steffen Trumtrar | 3d42a37 | 2012-07-17 16:14:34 +0200 | [diff] [blame] | 1220 | } |
Marc Kleine-Budde | 1a3e517 | 2013-11-25 22:15:20 +0100 | [diff] [blame] | 1221 | clock_freq = clk_get_rate(clk_per); |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 1222 | } |
| 1223 | |
| 1224 | mem = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 1225 | irq = platform_get_irq(pdev, 0); |
Fabio Estevam | 933e4af | 2013-07-22 12:41:39 -0300 | [diff] [blame] | 1226 | if (irq <= 0) |
| 1227 | return -ENODEV; |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 1228 | |
Marc Kleine-Budde | 89af874 | 2015-05-08 09:32:58 +0200 | [diff] [blame] | 1229 | regs = devm_ioremap_resource(&pdev->dev, mem); |
| 1230 | if (IS_ERR(regs)) |
| 1231 | return PTR_ERR(regs); |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 1232 | |
Hui Wang | 30c1e67 | 2012-06-28 16:21:35 +0800 | [diff] [blame] | 1233 | of_id = of_match_device(flexcan_of_match, &pdev->dev); |
| 1234 | if (of_id) { |
| 1235 | devtype_data = of_id->data; |
Marc Kleine-Budde | d0873e6 | 2014-03-04 22:04:22 +0100 | [diff] [blame] | 1236 | } else if (platform_get_device_id(pdev)->driver_data) { |
Hui Wang | 30c1e67 | 2012-06-28 16:21:35 +0800 | [diff] [blame] | 1237 | devtype_data = (struct flexcan_devtype_data *) |
Marc Kleine-Budde | d0873e6 | 2014-03-04 22:04:22 +0100 | [diff] [blame] | 1238 | platform_get_device_id(pdev)->driver_data; |
Hui Wang | 30c1e67 | 2012-06-28 16:21:35 +0800 | [diff] [blame] | 1239 | } else { |
Fabio Estevam | 933e4af | 2013-07-22 12:41:39 -0300 | [diff] [blame] | 1240 | return -ENODEV; |
Hui Wang | 30c1e67 | 2012-06-28 16:21:35 +0800 | [diff] [blame] | 1241 | } |
| 1242 | |
Fabio Estevam | 933e4af | 2013-07-22 12:41:39 -0300 | [diff] [blame] | 1243 | dev = alloc_candev(sizeof(struct flexcan_priv), 1); |
| 1244 | if (!dev) |
| 1245 | return -ENOMEM; |
| 1246 | |
Marc Kleine-Budde | 3016475 | 2015-05-10 15:26:58 +0200 | [diff] [blame] | 1247 | platform_set_drvdata(pdev, dev); |
| 1248 | SET_NETDEV_DEV(dev, &pdev->dev); |
| 1249 | |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 1250 | dev->netdev_ops = &flexcan_netdev_ops; |
| 1251 | dev->irq = irq; |
Reuben Dowle | 9a12349 | 2011-11-01 11:18:03 +1300 | [diff] [blame] | 1252 | dev->flags |= IFF_ECHO; |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 1253 | |
| 1254 | priv = netdev_priv(dev); |
holt@sgi.com | 97efe9a | 2011-08-16 17:32:23 +0000 | [diff] [blame] | 1255 | priv->can.clock.freq = clock_freq; |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 1256 | priv->can.bittiming_const = &flexcan_bittiming_const; |
| 1257 | priv->can.do_set_mode = flexcan_set_mode; |
| 1258 | priv->can.do_get_berr_counter = flexcan_get_berr_counter; |
| 1259 | priv->can.ctrlmode_supported = CAN_CTRLMODE_LOOPBACK | |
| 1260 | CAN_CTRLMODE_LISTENONLY | CAN_CTRLMODE_3_SAMPLES | |
| 1261 | CAN_CTRLMODE_BERR_REPORTING; |
Marc Kleine-Budde | 89af874 | 2015-05-08 09:32:58 +0200 | [diff] [blame] | 1262 | priv->regs = regs; |
Steffen Trumtrar | 3d42a37 | 2012-07-17 16:14:34 +0200 | [diff] [blame] | 1263 | priv->clk_ipg = clk_ipg; |
| 1264 | priv->clk_per = clk_per; |
Hui Wang | 30c1e67 | 2012-06-28 16:21:35 +0800 | [diff] [blame] | 1265 | priv->devtype_data = devtype_data; |
Andreas Werner | 555828e | 2015-03-22 17:35:52 +0100 | [diff] [blame] | 1266 | priv->reg_xceiver = reg_xceiver; |
Fabio Estevam | b7c4114 | 2013-06-10 23:12:57 -0300 | [diff] [blame] | 1267 | |
Marc Kleine-Budde | b3cf53e | 2015-09-01 09:00:13 +0200 | [diff] [blame] | 1268 | if (priv->devtype_data->quirks & FLEXCAN_QUIRK_USE_OFF_TIMESTAMP) { |
| 1269 | priv->tx_mb_idx = FLEXCAN_TX_MB_OFF_TIMESTAMP; |
| 1270 | priv->tx_mb_reserved = ®s->mb[FLEXCAN_TX_MB_RESERVED_OFF_TIMESTAMP]; |
| 1271 | } else { |
| 1272 | priv->tx_mb_idx = FLEXCAN_TX_MB_OFF_FIFO; |
| 1273 | priv->tx_mb_reserved = ®s->mb[FLEXCAN_TX_MB_RESERVED_OFF_FIFO]; |
| 1274 | } |
Marc Kleine-Budde | b93917c | 2015-07-12 00:47:47 +0200 | [diff] [blame] | 1275 | priv->tx_mb = ®s->mb[priv->tx_mb_idx]; |
| 1276 | |
Marc Kleine-Budde | b3cf53e | 2015-09-01 09:00:13 +0200 | [diff] [blame] | 1277 | priv->reg_imask1_default = FLEXCAN_IFLAG_MB(priv->tx_mb_idx); |
| 1278 | priv->reg_imask2_default = 0; |
Marc Kleine-Budde | 28ac7dc | 2015-08-04 13:46:10 +0200 | [diff] [blame] | 1279 | |
Marc Kleine-Budde | 3016475 | 2015-05-10 15:26:58 +0200 | [diff] [blame] | 1280 | priv->offload.mailbox_read = flexcan_mailbox_read; |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 1281 | |
Marc Kleine-Budde | b3cf53e | 2015-09-01 09:00:13 +0200 | [diff] [blame] | 1282 | if (priv->devtype_data->quirks & FLEXCAN_QUIRK_USE_OFF_TIMESTAMP) { |
| 1283 | u64 imask; |
| 1284 | |
| 1285 | priv->offload.mb_first = FLEXCAN_RX_MB_OFF_TIMESTAMP_FIRST; |
| 1286 | priv->offload.mb_last = FLEXCAN_RX_MB_OFF_TIMESTAMP_LAST; |
| 1287 | |
| 1288 | imask = GENMASK_ULL(priv->offload.mb_last, priv->offload.mb_first); |
| 1289 | priv->reg_imask1_default |= imask; |
| 1290 | priv->reg_imask2_default |= imask >> 32; |
| 1291 | |
| 1292 | err = can_rx_offload_add_timestamp(dev, &priv->offload); |
| 1293 | } else { |
| 1294 | priv->reg_imask1_default |= FLEXCAN_IFLAG_RX_FIFO_OVERFLOW | |
| 1295 | FLEXCAN_IFLAG_RX_FIFO_AVAILABLE; |
| 1296 | err = can_rx_offload_add_fifo(dev, &priv->offload, FLEXCAN_NAPI_WEIGHT); |
| 1297 | } |
Marc Kleine-Budde | 3016475 | 2015-05-10 15:26:58 +0200 | [diff] [blame] | 1298 | if (err) |
| 1299 | goto failed_offload; |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 1300 | |
| 1301 | err = register_flexcandev(dev); |
| 1302 | if (err) { |
| 1303 | dev_err(&pdev->dev, "registering netdev failed\n"); |
| 1304 | goto failed_register; |
| 1305 | } |
| 1306 | |
Fabio Baltieri | adccadb | 2012-12-18 18:50:58 +0100 | [diff] [blame] | 1307 | devm_can_led_init(dev); |
| 1308 | |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 1309 | 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] | 1310 | priv->regs, dev->irq); |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 1311 | |
| 1312 | return 0; |
| 1313 | |
Marc Kleine-Budde | 3016475 | 2015-05-10 15:26:58 +0200 | [diff] [blame] | 1314 | failed_offload: |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 1315 | failed_register: |
| 1316 | free_candev(dev); |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 1317 | return err; |
| 1318 | } |
| 1319 | |
Bill Pemberton | 3c8ac0f | 2012-12-03 09:22:44 -0500 | [diff] [blame] | 1320 | static int flexcan_remove(struct platform_device *pdev) |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 1321 | { |
| 1322 | struct net_device *dev = platform_get_drvdata(pdev); |
Marc Kleine-Budde | d96e43e | 2014-02-28 20:48:36 +0100 | [diff] [blame] | 1323 | struct flexcan_priv *priv = netdev_priv(dev); |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 1324 | |
| 1325 | unregister_flexcandev(dev); |
Marc Kleine-Budde | 3016475 | 2015-05-10 15:26:58 +0200 | [diff] [blame] | 1326 | can_rx_offload_del(&priv->offload); |
Marc Kleine-Budde | 9a27586 | 2010-10-21 05:07:58 +0000 | [diff] [blame] | 1327 | free_candev(dev); |
| 1328 | |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 1329 | return 0; |
| 1330 | } |
| 1331 | |
Marc Kleine-Budde | 08c6d35 | 2014-03-05 19:10:44 +0100 | [diff] [blame] | 1332 | static int __maybe_unused flexcan_suspend(struct device *device) |
Eric Bénard | 8b5e218 | 2012-05-08 17:12:17 +0200 | [diff] [blame] | 1333 | { |
Fabio Estevam | 588e7a8 | 2013-05-20 15:43:43 -0300 | [diff] [blame] | 1334 | struct net_device *dev = dev_get_drvdata(device); |
Eric Bénard | 8b5e218 | 2012-05-08 17:12:17 +0200 | [diff] [blame] | 1335 | struct flexcan_priv *priv = netdev_priv(dev); |
Marc Kleine-Budde | 9b00b30 | 2014-02-28 15:30:18 +0100 | [diff] [blame] | 1336 | int err; |
Eric Bénard | 8b5e218 | 2012-05-08 17:12:17 +0200 | [diff] [blame] | 1337 | |
Eric Bénard | 8b5e218 | 2012-05-08 17:12:17 +0200 | [diff] [blame] | 1338 | if (netif_running(dev)) { |
Fabio Estevam | 4de349e | 2016-08-17 12:41:08 -0300 | [diff] [blame] | 1339 | err = flexcan_chip_disable(priv); |
| 1340 | if (err) |
| 1341 | return err; |
Eric Bénard | 8b5e218 | 2012-05-08 17:12:17 +0200 | [diff] [blame] | 1342 | netif_stop_queue(dev); |
| 1343 | netif_device_detach(dev); |
| 1344 | } |
| 1345 | priv->can.state = CAN_STATE_SLEEPING; |
| 1346 | |
| 1347 | return 0; |
| 1348 | } |
| 1349 | |
Marc Kleine-Budde | 08c6d35 | 2014-03-05 19:10:44 +0100 | [diff] [blame] | 1350 | static int __maybe_unused flexcan_resume(struct device *device) |
Eric Bénard | 8b5e218 | 2012-05-08 17:12:17 +0200 | [diff] [blame] | 1351 | { |
Fabio Estevam | 588e7a8 | 2013-05-20 15:43:43 -0300 | [diff] [blame] | 1352 | struct net_device *dev = dev_get_drvdata(device); |
Eric Bénard | 8b5e218 | 2012-05-08 17:12:17 +0200 | [diff] [blame] | 1353 | struct flexcan_priv *priv = netdev_priv(dev); |
Fabio Estevam | 4de349e | 2016-08-17 12:41:08 -0300 | [diff] [blame] | 1354 | int err; |
Eric Bénard | 8b5e218 | 2012-05-08 17:12:17 +0200 | [diff] [blame] | 1355 | |
| 1356 | priv->can.state = CAN_STATE_ERROR_ACTIVE; |
| 1357 | if (netif_running(dev)) { |
| 1358 | netif_device_attach(dev); |
| 1359 | netif_start_queue(dev); |
Fabio Estevam | 4de349e | 2016-08-17 12:41:08 -0300 | [diff] [blame] | 1360 | err = flexcan_chip_enable(priv); |
| 1361 | if (err) |
| 1362 | return err; |
Eric Bénard | 8b5e218 | 2012-05-08 17:12:17 +0200 | [diff] [blame] | 1363 | } |
Fabio Estevam | 4de349e | 2016-08-17 12:41:08 -0300 | [diff] [blame] | 1364 | return 0; |
Eric Bénard | 8b5e218 | 2012-05-08 17:12:17 +0200 | [diff] [blame] | 1365 | } |
Fabio Estevam | 588e7a8 | 2013-05-20 15:43:43 -0300 | [diff] [blame] | 1366 | |
| 1367 | static SIMPLE_DEV_PM_OPS(flexcan_pm_ops, flexcan_suspend, flexcan_resume); |
Eric Bénard | 8b5e218 | 2012-05-08 17:12:17 +0200 | [diff] [blame] | 1368 | |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 1369 | static struct platform_driver flexcan_driver = { |
holt@sgi.com | c8aef4c | 2011-08-16 17:32:22 +0000 | [diff] [blame] | 1370 | .driver = { |
| 1371 | .name = DRV_NAME, |
Fabio Estevam | 588e7a8 | 2013-05-20 15:43:43 -0300 | [diff] [blame] | 1372 | .pm = &flexcan_pm_ops, |
holt@sgi.com | c8aef4c | 2011-08-16 17:32:22 +0000 | [diff] [blame] | 1373 | .of_match_table = flexcan_of_match, |
| 1374 | }, |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 1375 | .probe = flexcan_probe, |
Bill Pemberton | 3c8ac0f | 2012-12-03 09:22:44 -0500 | [diff] [blame] | 1376 | .remove = flexcan_remove, |
Hui Wang | 30c1e67 | 2012-06-28 16:21:35 +0800 | [diff] [blame] | 1377 | .id_table = flexcan_id_table, |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 1378 | }; |
| 1379 | |
Axel Lin | 871d337 | 2011-11-27 15:42:31 +0000 | [diff] [blame] | 1380 | module_platform_driver(flexcan_driver); |
Marc Kleine-Budde | e955cea | 2009-07-29 10:20:10 +0200 | [diff] [blame] | 1381 | |
| 1382 | MODULE_AUTHOR("Sascha Hauer <kernel@pengutronix.de>, " |
| 1383 | "Marc Kleine-Budde <kernel@pengutronix.de>"); |
| 1384 | MODULE_LICENSE("GPL v2"); |
| 1385 | MODULE_DESCRIPTION("CAN port driver for flexcan based chip"); |