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