Bhupesh Sharma | 881ff67 | 2011-02-13 22:51:44 -0800 | [diff] [blame] | 1 | /* |
| 2 | * CAN bus driver for Bosch C_CAN controller |
| 3 | * |
| 4 | * Copyright (C) 2010 ST Microelectronics |
| 5 | * Bhupesh Sharma <bhupesh.sharma@st.com> |
| 6 | * |
| 7 | * Borrowed heavily from the C_CAN driver originally written by: |
| 8 | * Copyright (C) 2007 |
| 9 | * - Sascha Hauer, Marc Kleine-Budde, Pengutronix <s.hauer@pengutronix.de> |
| 10 | * - Simon Kallweit, intefo AG <simon.kallweit@intefo.ch> |
| 11 | * |
| 12 | * TX and RX NAPI implementation has been borrowed from at91 CAN driver |
| 13 | * written by: |
| 14 | * Copyright |
| 15 | * (C) 2007 by Hans J. Koch <hjk@hansjkoch.de> |
| 16 | * (C) 2008, 2009 by Marc Kleine-Budde <kernel@pengutronix.de> |
| 17 | * |
| 18 | * Bosch C_CAN controller is compliant to CAN protocol version 2.0 part A and B. |
| 19 | * Bosch C_CAN user manual can be obtained from: |
| 20 | * http://www.semiconductors.bosch.de/media/en/pdf/ipmodules_1/c_can/ |
| 21 | * users_manual_c_can.pdf |
| 22 | * |
| 23 | * This file is licensed under the terms of the GNU General Public |
| 24 | * License version 2. This program is licensed "as is" without any |
| 25 | * warranty of any kind, whether express or implied. |
| 26 | */ |
| 27 | |
| 28 | #include <linux/kernel.h> |
Bhupesh Sharma | 881ff67 | 2011-02-13 22:51:44 -0800 | [diff] [blame] | 29 | #include <linux/module.h> |
| 30 | #include <linux/interrupt.h> |
| 31 | #include <linux/delay.h> |
| 32 | #include <linux/netdevice.h> |
| 33 | #include <linux/if_arp.h> |
| 34 | #include <linux/if_ether.h> |
| 35 | #include <linux/list.h> |
Bhupesh Sharma | 881ff67 | 2011-02-13 22:51:44 -0800 | [diff] [blame] | 36 | #include <linux/io.h> |
AnilKumar Ch | 4cdd34b | 2012-08-20 16:50:54 +0530 | [diff] [blame] | 37 | #include <linux/pm_runtime.h> |
Bhupesh Sharma | 881ff67 | 2011-02-13 22:51:44 -0800 | [diff] [blame] | 38 | |
| 39 | #include <linux/can.h> |
| 40 | #include <linux/can/dev.h> |
| 41 | #include <linux/can/error.h> |
Fabio Baltieri | 5090f80 | 2012-12-18 18:51:01 +0100 | [diff] [blame] | 42 | #include <linux/can/led.h> |
Bhupesh Sharma | 881ff67 | 2011-02-13 22:51:44 -0800 | [diff] [blame] | 43 | |
| 44 | #include "c_can.h" |
| 45 | |
AnilKumar Ch | 33f8100 | 2012-05-29 11:13:15 +0530 | [diff] [blame] | 46 | /* Number of interface registers */ |
| 47 | #define IF_ENUM_REG_LEN 11 |
| 48 | #define C_CAN_IFACE(reg, iface) (C_CAN_IF1_##reg + (iface) * IF_ENUM_REG_LEN) |
| 49 | |
AnilKumar Ch | 8212003 | 2012-09-21 15:29:01 +0530 | [diff] [blame] | 50 | /* control extension register D_CAN specific */ |
| 51 | #define CONTROL_EX_PDR BIT(8) |
| 52 | |
Bhupesh Sharma | 881ff67 | 2011-02-13 22:51:44 -0800 | [diff] [blame] | 53 | /* control register */ |
| 54 | #define CONTROL_TEST BIT(7) |
| 55 | #define CONTROL_CCE BIT(6) |
| 56 | #define CONTROL_DISABLE_AR BIT(5) |
| 57 | #define CONTROL_ENABLE_AR (0 << 5) |
| 58 | #define CONTROL_EIE BIT(3) |
| 59 | #define CONTROL_SIE BIT(2) |
| 60 | #define CONTROL_IE BIT(1) |
| 61 | #define CONTROL_INIT BIT(0) |
| 62 | |
Thomas Gleixner | 2d5f4f8 | 2014-04-11 08:13:17 +0000 | [diff] [blame] | 63 | #define CONTROL_IRQMSK (CONTROL_EIE | CONTROL_IE | CONTROL_SIE) |
| 64 | |
Bhupesh Sharma | 881ff67 | 2011-02-13 22:51:44 -0800 | [diff] [blame] | 65 | /* test register */ |
| 66 | #define TEST_RX BIT(7) |
| 67 | #define TEST_TX1 BIT(6) |
| 68 | #define TEST_TX2 BIT(5) |
| 69 | #define TEST_LBACK BIT(4) |
| 70 | #define TEST_SILENT BIT(3) |
| 71 | #define TEST_BASIC BIT(2) |
| 72 | |
| 73 | /* status register */ |
AnilKumar Ch | 8212003 | 2012-09-21 15:29:01 +0530 | [diff] [blame] | 74 | #define STATUS_PDA BIT(10) |
Bhupesh Sharma | 881ff67 | 2011-02-13 22:51:44 -0800 | [diff] [blame] | 75 | #define STATUS_BOFF BIT(7) |
| 76 | #define STATUS_EWARN BIT(6) |
| 77 | #define STATUS_EPASS BIT(5) |
| 78 | #define STATUS_RXOK BIT(4) |
| 79 | #define STATUS_TXOK BIT(3) |
| 80 | |
| 81 | /* error counter register */ |
| 82 | #define ERR_CNT_TEC_MASK 0xff |
| 83 | #define ERR_CNT_TEC_SHIFT 0 |
| 84 | #define ERR_CNT_REC_SHIFT 8 |
| 85 | #define ERR_CNT_REC_MASK (0x7f << ERR_CNT_REC_SHIFT) |
| 86 | #define ERR_CNT_RP_SHIFT 15 |
| 87 | #define ERR_CNT_RP_MASK (0x1 << ERR_CNT_RP_SHIFT) |
| 88 | |
| 89 | /* bit-timing register */ |
| 90 | #define BTR_BRP_MASK 0x3f |
| 91 | #define BTR_BRP_SHIFT 0 |
| 92 | #define BTR_SJW_SHIFT 6 |
| 93 | #define BTR_SJW_MASK (0x3 << BTR_SJW_SHIFT) |
| 94 | #define BTR_TSEG1_SHIFT 8 |
| 95 | #define BTR_TSEG1_MASK (0xf << BTR_TSEG1_SHIFT) |
| 96 | #define BTR_TSEG2_SHIFT 12 |
| 97 | #define BTR_TSEG2_MASK (0x7 << BTR_TSEG2_SHIFT) |
| 98 | |
| 99 | /* brp extension register */ |
| 100 | #define BRP_EXT_BRPE_MASK 0x0f |
| 101 | #define BRP_EXT_BRPE_SHIFT 0 |
| 102 | |
| 103 | /* IFx command request */ |
| 104 | #define IF_COMR_BUSY BIT(15) |
| 105 | |
| 106 | /* IFx command mask */ |
| 107 | #define IF_COMM_WR BIT(7) |
| 108 | #define IF_COMM_MASK BIT(6) |
| 109 | #define IF_COMM_ARB BIT(5) |
| 110 | #define IF_COMM_CONTROL BIT(4) |
| 111 | #define IF_COMM_CLR_INT_PND BIT(3) |
| 112 | #define IF_COMM_TXRQST BIT(2) |
Thomas Gleixner | 6b48ff8 | 2014-04-11 08:13:14 +0000 | [diff] [blame] | 113 | #define IF_COMM_CLR_NEWDAT IF_COMM_TXRQST |
Bhupesh Sharma | 881ff67 | 2011-02-13 22:51:44 -0800 | [diff] [blame] | 114 | #define IF_COMM_DATAA BIT(1) |
| 115 | #define IF_COMM_DATAB BIT(0) |
Thomas Gleixner | 23ef0a8 | 2014-04-11 08:13:21 +0000 | [diff] [blame] | 116 | |
| 117 | /* TX buffer setup */ |
| 118 | #define IF_COMM_TX (IF_COMM_ARB | IF_COMM_CONTROL | \ |
| 119 | IF_COMM_TXRQST | \ |
| 120 | IF_COMM_DATAA | IF_COMM_DATAB) |
Bhupesh Sharma | 881ff67 | 2011-02-13 22:51:44 -0800 | [diff] [blame] | 121 | |
Thomas Gleixner | c0a9f4d | 2014-03-18 17:19:13 +0000 | [diff] [blame] | 122 | /* For the low buffers we clear the interrupt bit, but keep newdat */ |
| 123 | #define IF_COMM_RCV_LOW (IF_COMM_MASK | IF_COMM_ARB | \ |
| 124 | IF_COMM_CONTROL | IF_COMM_CLR_INT_PND | \ |
| 125 | IF_COMM_DATAA | IF_COMM_DATAB) |
| 126 | |
| 127 | /* For the high buffers we clear the interrupt bit and newdat */ |
Thomas Gleixner | 6b48ff8 | 2014-04-11 08:13:14 +0000 | [diff] [blame] | 128 | #define IF_COMM_RCV_HIGH (IF_COMM_RCV_LOW | IF_COMM_CLR_NEWDAT) |
Thomas Gleixner | c0a9f4d | 2014-03-18 17:19:13 +0000 | [diff] [blame] | 129 | |
Thomas Gleixner | 8ff2de0 | 2014-04-11 08:13:18 +0000 | [diff] [blame] | 130 | |
| 131 | /* Receive setup of message objects */ |
| 132 | #define IF_COMM_RCV_SETUP (IF_COMM_MASK | IF_COMM_ARB | IF_COMM_CONTROL) |
| 133 | |
Thomas Gleixner | b07faaa | 2014-04-11 08:13:19 +0000 | [diff] [blame] | 134 | /* Invalidation of message objects */ |
| 135 | #define IF_COMM_INVAL (IF_COMM_ARB | IF_COMM_CONTROL) |
| 136 | |
Bhupesh Sharma | 881ff67 | 2011-02-13 22:51:44 -0800 | [diff] [blame] | 137 | /* IFx arbitration */ |
Thomas Gleixner | d48071b | 2014-04-11 08:13:21 +0000 | [diff] [blame] | 138 | #define IF_ARB_MSGVAL BIT(31) |
| 139 | #define IF_ARB_MSGXTD BIT(30) |
| 140 | #define IF_ARB_TRANSMIT BIT(29) |
Bhupesh Sharma | 881ff67 | 2011-02-13 22:51:44 -0800 | [diff] [blame] | 141 | |
| 142 | /* IFx message control */ |
| 143 | #define IF_MCONT_NEWDAT BIT(15) |
| 144 | #define IF_MCONT_MSGLST BIT(14) |
Bhupesh Sharma | 881ff67 | 2011-02-13 22:51:44 -0800 | [diff] [blame] | 145 | #define IF_MCONT_INTPND BIT(13) |
| 146 | #define IF_MCONT_UMASK BIT(12) |
| 147 | #define IF_MCONT_TXIE BIT(11) |
| 148 | #define IF_MCONT_RXIE BIT(10) |
| 149 | #define IF_MCONT_RMTEN BIT(9) |
| 150 | #define IF_MCONT_TXRQST BIT(8) |
| 151 | #define IF_MCONT_EOB BIT(7) |
| 152 | #define IF_MCONT_DLC_MASK 0xf |
| 153 | |
Thomas Gleixner | 8ff2de0 | 2014-04-11 08:13:18 +0000 | [diff] [blame] | 154 | #define IF_MCONT_RCV (IF_MCONT_RXIE | IF_MCONT_UMASK) |
| 155 | #define IF_MCONT_RCV_EOB (IF_MCONT_RCV | IF_MCONT_EOB) |
| 156 | |
Thomas Gleixner | 23ef0a8 | 2014-04-11 08:13:21 +0000 | [diff] [blame] | 157 | #define IF_MCONT_TX (IF_MCONT_TXIE | IF_MCONT_EOB) |
| 158 | |
Bhupesh Sharma | 881ff67 | 2011-02-13 22:51:44 -0800 | [diff] [blame] | 159 | /* |
Thomas Gleixner | 640916d | 2014-03-18 17:19:09 +0000 | [diff] [blame] | 160 | * Use IF1 for RX and IF2 for TX |
Bhupesh Sharma | 881ff67 | 2011-02-13 22:51:44 -0800 | [diff] [blame] | 161 | */ |
Thomas Gleixner | 640916d | 2014-03-18 17:19:09 +0000 | [diff] [blame] | 162 | #define IF_RX 0 |
| 163 | #define IF_TX 1 |
Bhupesh Sharma | 881ff67 | 2011-02-13 22:51:44 -0800 | [diff] [blame] | 164 | |
Bhupesh Sharma | 881ff67 | 2011-02-13 22:51:44 -0800 | [diff] [blame] | 165 | /* minimum timeout for checking BUSY status */ |
| 166 | #define MIN_TIMEOUT_VALUE 6 |
| 167 | |
AnilKumar Ch | 8212003 | 2012-09-21 15:29:01 +0530 | [diff] [blame] | 168 | /* Wait for ~1 sec for INIT bit */ |
| 169 | #define INIT_WAIT_MS 1000 |
| 170 | |
Bhupesh Sharma | 881ff67 | 2011-02-13 22:51:44 -0800 | [diff] [blame] | 171 | /* napi related */ |
| 172 | #define C_CAN_NAPI_WEIGHT C_CAN_MSG_OBJ_RX_NUM |
| 173 | |
| 174 | /* c_can lec values */ |
| 175 | enum c_can_lec_type { |
| 176 | LEC_NO_ERROR = 0, |
| 177 | LEC_STUFF_ERROR, |
| 178 | LEC_FORM_ERROR, |
| 179 | LEC_ACK_ERROR, |
| 180 | LEC_BIT1_ERROR, |
| 181 | LEC_BIT0_ERROR, |
| 182 | LEC_CRC_ERROR, |
| 183 | LEC_UNUSED, |
Thomas Gleixner | 097aec1 | 2014-04-11 08:13:13 +0000 | [diff] [blame] | 184 | LEC_MASK = LEC_UNUSED, |
Bhupesh Sharma | 881ff67 | 2011-02-13 22:51:44 -0800 | [diff] [blame] | 185 | }; |
| 186 | |
| 187 | /* |
| 188 | * c_can error types: |
| 189 | * Bus errors (BUS_OFF, ERROR_WARNING, ERROR_PASSIVE) are supported |
| 190 | */ |
| 191 | enum c_can_bus_error_types { |
| 192 | C_CAN_NO_ERROR = 0, |
| 193 | C_CAN_BUS_OFF, |
| 194 | C_CAN_ERROR_WARNING, |
| 195 | C_CAN_ERROR_PASSIVE, |
| 196 | }; |
| 197 | |
Marc Kleine-Budde | 194b9a4 | 2012-07-16 12:58:31 +0200 | [diff] [blame] | 198 | static const struct can_bittiming_const c_can_bittiming_const = { |
Bhupesh Sharma | 881ff67 | 2011-02-13 22:51:44 -0800 | [diff] [blame] | 199 | .name = KBUILD_MODNAME, |
| 200 | .tseg1_min = 2, /* Time segment 1 = prop_seg + phase_seg1 */ |
| 201 | .tseg1_max = 16, |
| 202 | .tseg2_min = 1, /* Time segment 2 = phase_seg2 */ |
| 203 | .tseg2_max = 8, |
| 204 | .sjw_max = 4, |
| 205 | .brp_min = 1, |
| 206 | .brp_max = 1024, /* 6-bit BRP field + 4-bit BRPE field*/ |
| 207 | .brp_inc = 1, |
| 208 | }; |
| 209 | |
AnilKumar Ch | 4cdd34b | 2012-08-20 16:50:54 +0530 | [diff] [blame] | 210 | static inline void c_can_pm_runtime_enable(const struct c_can_priv *priv) |
| 211 | { |
| 212 | if (priv->device) |
| 213 | pm_runtime_enable(priv->device); |
| 214 | } |
| 215 | |
| 216 | static inline void c_can_pm_runtime_disable(const struct c_can_priv *priv) |
| 217 | { |
| 218 | if (priv->device) |
| 219 | pm_runtime_disable(priv->device); |
| 220 | } |
| 221 | |
| 222 | static inline void c_can_pm_runtime_get_sync(const struct c_can_priv *priv) |
| 223 | { |
| 224 | if (priv->device) |
| 225 | pm_runtime_get_sync(priv->device); |
| 226 | } |
| 227 | |
| 228 | static inline void c_can_pm_runtime_put_sync(const struct c_can_priv *priv) |
| 229 | { |
| 230 | if (priv->device) |
| 231 | pm_runtime_put_sync(priv->device); |
| 232 | } |
| 233 | |
AnilKumar Ch | 52cde85 | 2012-11-21 11:14:10 +0530 | [diff] [blame] | 234 | static inline void c_can_reset_ram(const struct c_can_priv *priv, bool enable) |
| 235 | { |
| 236 | if (priv->raminit) |
| 237 | priv->raminit(priv, enable); |
| 238 | } |
| 239 | |
Thomas Gleixner | 2d5f4f8 | 2014-04-11 08:13:17 +0000 | [diff] [blame] | 240 | static void c_can_irq_control(struct c_can_priv *priv, bool enable) |
Bhupesh Sharma | 881ff67 | 2011-02-13 22:51:44 -0800 | [diff] [blame] | 241 | { |
Thomas Gleixner | 2d5f4f8 | 2014-04-11 08:13:17 +0000 | [diff] [blame] | 242 | u32 ctrl = priv->read_reg(priv, C_CAN_CTRL_REG) & ~CONTROL_IRQMSK; |
Bhupesh Sharma | 881ff67 | 2011-02-13 22:51:44 -0800 | [diff] [blame] | 243 | |
| 244 | if (enable) |
Thomas Gleixner | 2d5f4f8 | 2014-04-11 08:13:17 +0000 | [diff] [blame] | 245 | ctrl |= CONTROL_IRQMSK; |
Bhupesh Sharma | 881ff67 | 2011-02-13 22:51:44 -0800 | [diff] [blame] | 246 | |
Thomas Gleixner | 2d5f4f8 | 2014-04-11 08:13:17 +0000 | [diff] [blame] | 247 | priv->write_reg(priv, C_CAN_CTRL_REG, ctrl); |
Bhupesh Sharma | 881ff67 | 2011-02-13 22:51:44 -0800 | [diff] [blame] | 248 | } |
| 249 | |
Thomas Gleixner | 7af2863 | 2014-04-11 08:13:20 +0000 | [diff] [blame] | 250 | static void c_can_obj_update(struct net_device *dev, int iface, u32 cmd, u32 obj) |
Bhupesh Sharma | 881ff67 | 2011-02-13 22:51:44 -0800 | [diff] [blame] | 251 | { |
Thomas Gleixner | 7af2863 | 2014-04-11 08:13:20 +0000 | [diff] [blame] | 252 | struct c_can_priv *priv = netdev_priv(dev); |
| 253 | int cnt, reg = C_CAN_IFACE(COMREQ_REG, iface); |
Bhupesh Sharma | 881ff67 | 2011-02-13 22:51:44 -0800 | [diff] [blame] | 254 | |
Thomas Gleixner | 7af2863 | 2014-04-11 08:13:20 +0000 | [diff] [blame] | 255 | priv->write_reg(priv, reg + 1, cmd); |
| 256 | priv->write_reg(priv, reg, obj); |
| 257 | |
| 258 | for (cnt = MIN_TIMEOUT_VALUE; cnt; cnt--) { |
| 259 | if (!(priv->read_reg(priv, reg) & IF_COMR_BUSY)) |
| 260 | return; |
Bhupesh Sharma | 881ff67 | 2011-02-13 22:51:44 -0800 | [diff] [blame] | 261 | udelay(1); |
| 262 | } |
Thomas Gleixner | 7af2863 | 2014-04-11 08:13:20 +0000 | [diff] [blame] | 263 | netdev_err(dev, "Updating object timed out\n"); |
Bhupesh Sharma | 881ff67 | 2011-02-13 22:51:44 -0800 | [diff] [blame] | 264 | |
Bhupesh Sharma | 881ff67 | 2011-02-13 22:51:44 -0800 | [diff] [blame] | 265 | } |
| 266 | |
Thomas Gleixner | 7af2863 | 2014-04-11 08:13:20 +0000 | [diff] [blame] | 267 | static inline void c_can_object_get(struct net_device *dev, int iface, |
| 268 | u32 obj, u32 cmd) |
Bhupesh Sharma | 881ff67 | 2011-02-13 22:51:44 -0800 | [diff] [blame] | 269 | { |
Thomas Gleixner | 7af2863 | 2014-04-11 08:13:20 +0000 | [diff] [blame] | 270 | c_can_obj_update(dev, iface, cmd, obj); |
Bhupesh Sharma | 881ff67 | 2011-02-13 22:51:44 -0800 | [diff] [blame] | 271 | } |
| 272 | |
Thomas Gleixner | 7af2863 | 2014-04-11 08:13:20 +0000 | [diff] [blame] | 273 | static inline void c_can_object_put(struct net_device *dev, int iface, |
| 274 | u32 obj, u32 cmd) |
Bhupesh Sharma | 881ff67 | 2011-02-13 22:51:44 -0800 | [diff] [blame] | 275 | { |
Thomas Gleixner | 7af2863 | 2014-04-11 08:13:20 +0000 | [diff] [blame] | 276 | c_can_obj_update(dev, iface, cmd | IF_COMM_WR, obj); |
Bhupesh Sharma | 881ff67 | 2011-02-13 22:51:44 -0800 | [diff] [blame] | 277 | } |
| 278 | |
Thomas Gleixner | 9394159 | 2014-04-11 08:13:22 +0000 | [diff] [blame] | 279 | /* |
| 280 | * Note: According to documentation clearing TXIE while MSGVAL is set |
| 281 | * is not allowed, but works nicely on C/DCAN. And that lowers the I/O |
| 282 | * load significantly. |
| 283 | */ |
| 284 | static void c_can_inval_tx_object(struct net_device *dev, int iface, int obj) |
| 285 | { |
| 286 | struct c_can_priv *priv = netdev_priv(dev); |
| 287 | |
| 288 | priv->write_reg(priv, C_CAN_IFACE(MSGCTRL_REG, iface), 0); |
| 289 | c_can_object_put(dev, iface, obj, IF_COMM_INVAL); |
| 290 | } |
| 291 | |
| 292 | static void c_can_inval_msg_object(struct net_device *dev, int iface, int obj) |
| 293 | { |
| 294 | struct c_can_priv *priv = netdev_priv(dev); |
| 295 | |
| 296 | priv->write_reg(priv, C_CAN_IFACE(ARB1_REG, iface), 0); |
| 297 | priv->write_reg(priv, C_CAN_IFACE(ARB2_REG, iface), 0); |
| 298 | c_can_inval_tx_object(dev, iface, obj); |
| 299 | } |
| 300 | |
Thomas Gleixner | 35bdafb5 | 2014-04-11 08:13:22 +0000 | [diff] [blame] | 301 | static void c_can_setup_tx_object(struct net_device *dev, int iface, |
Thomas Gleixner | 9394159 | 2014-04-11 08:13:22 +0000 | [diff] [blame] | 302 | struct can_frame *frame, int idx) |
Bhupesh Sharma | 881ff67 | 2011-02-13 22:51:44 -0800 | [diff] [blame] | 303 | { |
Bhupesh Sharma | 881ff67 | 2011-02-13 22:51:44 -0800 | [diff] [blame] | 304 | struct c_can_priv *priv = netdev_priv(dev); |
Thomas Gleixner | 23ef0a8 | 2014-04-11 08:13:21 +0000 | [diff] [blame] | 305 | u16 ctrl = IF_MCONT_TX | frame->can_dlc; |
Thomas Gleixner | 9394159 | 2014-04-11 08:13:22 +0000 | [diff] [blame] | 306 | bool rtr = frame->can_id & CAN_RTR_FLAG; |
Thomas Gleixner | d48071b | 2014-04-11 08:13:21 +0000 | [diff] [blame] | 307 | u32 arb = IF_ARB_MSGVAL; |
Thomas Gleixner | 23ef0a8 | 2014-04-11 08:13:21 +0000 | [diff] [blame] | 308 | int i; |
Bhupesh Sharma | 881ff67 | 2011-02-13 22:51:44 -0800 | [diff] [blame] | 309 | |
| 310 | if (frame->can_id & CAN_EFF_FLAG) { |
Thomas Gleixner | 23ef0a8 | 2014-04-11 08:13:21 +0000 | [diff] [blame] | 311 | arb |= frame->can_id & CAN_EFF_MASK; |
Thomas Gleixner | d48071b | 2014-04-11 08:13:21 +0000 | [diff] [blame] | 312 | arb |= IF_ARB_MSGXTD; |
Thomas Gleixner | 23ef0a8 | 2014-04-11 08:13:21 +0000 | [diff] [blame] | 313 | } else { |
| 314 | arb |= (frame->can_id & CAN_SFF_MASK) << 18; |
| 315 | } |
Bhupesh Sharma | 881ff67 | 2011-02-13 22:51:44 -0800 | [diff] [blame] | 316 | |
Thomas Gleixner | 9394159 | 2014-04-11 08:13:22 +0000 | [diff] [blame] | 317 | if (!rtr) |
Thomas Gleixner | d48071b | 2014-04-11 08:13:21 +0000 | [diff] [blame] | 318 | arb |= IF_ARB_TRANSMIT; |
Bhupesh Sharma | 881ff67 | 2011-02-13 22:51:44 -0800 | [diff] [blame] | 319 | |
Thomas Gleixner | 9394159 | 2014-04-11 08:13:22 +0000 | [diff] [blame] | 320 | /* |
| 321 | * If we change the DIR bit, we need to invalidate the buffer |
| 322 | * first, i.e. clear the MSGVAL flag in the arbiter. |
| 323 | */ |
| 324 | if (rtr != (bool)test_bit(idx, &priv->tx_dir)) { |
| 325 | u32 obj = idx + C_CAN_MSG_OBJ_TX_FIRST; |
| 326 | |
| 327 | c_can_inval_msg_object(dev, iface, obj); |
| 328 | change_bit(idx, &priv->tx_dir); |
| 329 | } |
| 330 | |
Thomas Gleixner | 23ef0a8 | 2014-04-11 08:13:21 +0000 | [diff] [blame] | 331 | priv->write_reg(priv, C_CAN_IFACE(ARB1_REG, iface), arb); |
| 332 | priv->write_reg(priv, C_CAN_IFACE(ARB2_REG, iface), arb >> 16); |
| 333 | |
| 334 | priv->write_reg(priv, C_CAN_IFACE(MSGCTRL_REG, iface), ctrl); |
Bhupesh Sharma | 881ff67 | 2011-02-13 22:51:44 -0800 | [diff] [blame] | 335 | |
| 336 | for (i = 0; i < frame->can_dlc; i += 2) { |
AnilKumar Ch | 33f8100 | 2012-05-29 11:13:15 +0530 | [diff] [blame] | 337 | priv->write_reg(priv, C_CAN_IFACE(DATA1_REG, iface) + i / 2, |
Bhupesh Sharma | 881ff67 | 2011-02-13 22:51:44 -0800 | [diff] [blame] | 338 | frame->data[i] | (frame->data[i + 1] << 8)); |
| 339 | } |
Bhupesh Sharma | 881ff67 | 2011-02-13 22:51:44 -0800 | [diff] [blame] | 340 | } |
| 341 | |
Bhupesh Sharma | 881ff67 | 2011-02-13 22:51:44 -0800 | [diff] [blame] | 342 | static inline void c_can_activate_all_lower_rx_msg_obj(struct net_device *dev, |
Thomas Gleixner | 6b48ff8 | 2014-04-11 08:13:14 +0000 | [diff] [blame] | 343 | int iface) |
Bhupesh Sharma | 881ff67 | 2011-02-13 22:51:44 -0800 | [diff] [blame] | 344 | { |
| 345 | int i; |
Bhupesh Sharma | 881ff67 | 2011-02-13 22:51:44 -0800 | [diff] [blame] | 346 | |
Thomas Gleixner | 6b48ff8 | 2014-04-11 08:13:14 +0000 | [diff] [blame] | 347 | for (i = C_CAN_MSG_OBJ_RX_FIRST; i <= C_CAN_MSG_RX_LOW_LAST; i++) |
| 348 | c_can_object_get(dev, iface, i, IF_COMM_CLR_NEWDAT); |
Bhupesh Sharma | 881ff67 | 2011-02-13 22:51:44 -0800 | [diff] [blame] | 349 | } |
| 350 | |
Thomas Gleixner | 07c7b6f | 2014-03-18 17:19:10 +0000 | [diff] [blame] | 351 | static int c_can_handle_lost_msg_obj(struct net_device *dev, |
| 352 | int iface, int objno, u32 ctrl) |
Bhupesh Sharma | 881ff67 | 2011-02-13 22:51:44 -0800 | [diff] [blame] | 353 | { |
Bhupesh Sharma | 881ff67 | 2011-02-13 22:51:44 -0800 | [diff] [blame] | 354 | struct net_device_stats *stats = &dev->stats; |
Thomas Gleixner | 07c7b6f | 2014-03-18 17:19:10 +0000 | [diff] [blame] | 355 | struct c_can_priv *priv = netdev_priv(dev); |
Bhupesh Sharma | 881ff67 | 2011-02-13 22:51:44 -0800 | [diff] [blame] | 356 | struct can_frame *frame; |
Thomas Gleixner | 07c7b6f | 2014-03-18 17:19:10 +0000 | [diff] [blame] | 357 | struct sk_buff *skb; |
Bhupesh Sharma | 881ff67 | 2011-02-13 22:51:44 -0800 | [diff] [blame] | 358 | |
Thomas Gleixner | 07c7b6f | 2014-03-18 17:19:10 +0000 | [diff] [blame] | 359 | ctrl &= ~(IF_MCONT_MSGLST | IF_MCONT_INTPND | IF_MCONT_NEWDAT); |
| 360 | priv->write_reg(priv, C_CAN_IFACE(MSGCTRL_REG, iface), ctrl); |
Thomas Gleixner | 640916d | 2014-03-18 17:19:09 +0000 | [diff] [blame] | 361 | c_can_object_put(dev, iface, objno, IF_COMM_CONTROL); |
Bhupesh Sharma | 881ff67 | 2011-02-13 22:51:44 -0800 | [diff] [blame] | 362 | |
Thomas Gleixner | 1da394d | 2014-04-11 08:13:13 +0000 | [diff] [blame] | 363 | stats->rx_errors++; |
| 364 | stats->rx_over_errors++; |
| 365 | |
Bhupesh Sharma | 881ff67 | 2011-02-13 22:51:44 -0800 | [diff] [blame] | 366 | /* create an error msg */ |
| 367 | skb = alloc_can_err_skb(dev, &frame); |
| 368 | if (unlikely(!skb)) |
Thomas Gleixner | 07c7b6f | 2014-03-18 17:19:10 +0000 | [diff] [blame] | 369 | return 0; |
Bhupesh Sharma | 881ff67 | 2011-02-13 22:51:44 -0800 | [diff] [blame] | 370 | |
| 371 | frame->can_id |= CAN_ERR_CRTL; |
| 372 | frame->data[1] = CAN_ERR_CRTL_RX_OVERFLOW; |
Bhupesh Sharma | 881ff67 | 2011-02-13 22:51:44 -0800 | [diff] [blame] | 373 | |
| 374 | netif_receive_skb(skb); |
Thomas Gleixner | 07c7b6f | 2014-03-18 17:19:10 +0000 | [diff] [blame] | 375 | return 1; |
Bhupesh Sharma | 881ff67 | 2011-02-13 22:51:44 -0800 | [diff] [blame] | 376 | } |
| 377 | |
Thomas Gleixner | 4fb6dcc | 2014-04-11 08:13:18 +0000 | [diff] [blame] | 378 | static int c_can_read_msg_object(struct net_device *dev, int iface, u32 ctrl) |
Bhupesh Sharma | 881ff67 | 2011-02-13 22:51:44 -0800 | [diff] [blame] | 379 | { |
Bhupesh Sharma | 881ff67 | 2011-02-13 22:51:44 -0800 | [diff] [blame] | 380 | struct net_device_stats *stats = &dev->stats; |
Thomas Gleixner | 4fb6dcc | 2014-04-11 08:13:18 +0000 | [diff] [blame] | 381 | struct c_can_priv *priv = netdev_priv(dev); |
Bhupesh Sharma | 881ff67 | 2011-02-13 22:51:44 -0800 | [diff] [blame] | 382 | struct can_frame *frame; |
Thomas Gleixner | 4fb6dcc | 2014-04-11 08:13:18 +0000 | [diff] [blame] | 383 | struct sk_buff *skb; |
| 384 | u32 arb, data; |
Bhupesh Sharma | 881ff67 | 2011-02-13 22:51:44 -0800 | [diff] [blame] | 385 | |
| 386 | skb = alloc_can_skb(dev, &frame); |
| 387 | if (!skb) { |
| 388 | stats->rx_dropped++; |
| 389 | return -ENOMEM; |
| 390 | } |
| 391 | |
| 392 | frame->can_dlc = get_can_dlc(ctrl & 0x0F); |
| 393 | |
Thomas Gleixner | 4fb6dcc | 2014-04-11 08:13:18 +0000 | [diff] [blame] | 394 | arb = priv->read_reg(priv, C_CAN_IFACE(ARB1_REG, iface)); |
| 395 | arb |= priv->read_reg(priv, C_CAN_IFACE(ARB2_REG, iface)) << 16; |
Bhupesh Sharma | 881ff67 | 2011-02-13 22:51:44 -0800 | [diff] [blame] | 396 | |
Thomas Gleixner | d48071b | 2014-04-11 08:13:21 +0000 | [diff] [blame] | 397 | if (arb & IF_ARB_MSGXTD) |
Thomas Gleixner | 4fb6dcc | 2014-04-11 08:13:18 +0000 | [diff] [blame] | 398 | frame->can_id = (arb & CAN_EFF_MASK) | CAN_EFF_FLAG; |
Bhupesh Sharma | 881ff67 | 2011-02-13 22:51:44 -0800 | [diff] [blame] | 399 | else |
Thomas Gleixner | 4fb6dcc | 2014-04-11 08:13:18 +0000 | [diff] [blame] | 400 | frame->can_id = (arb >> 18) & CAN_SFF_MASK; |
Bhupesh Sharma | 881ff67 | 2011-02-13 22:51:44 -0800 | [diff] [blame] | 401 | |
Thomas Gleixner | d48071b | 2014-04-11 08:13:21 +0000 | [diff] [blame] | 402 | if (arb & IF_ARB_TRANSMIT) { |
Bhupesh Sharma | 881ff67 | 2011-02-13 22:51:44 -0800 | [diff] [blame] | 403 | frame->can_id |= CAN_RTR_FLAG; |
Thomas Gleixner | 4fb6dcc | 2014-04-11 08:13:18 +0000 | [diff] [blame] | 404 | } else { |
| 405 | int i, dreg = C_CAN_IFACE(DATA1_REG, iface); |
| 406 | |
| 407 | for (i = 0; i < frame->can_dlc; i += 2, dreg ++) { |
| 408 | data = priv->read_reg(priv, dreg); |
Bhupesh Sharma | 881ff67 | 2011-02-13 22:51:44 -0800 | [diff] [blame] | 409 | frame->data[i] = data; |
| 410 | frame->data[i + 1] = data >> 8; |
| 411 | } |
| 412 | } |
| 413 | |
Bhupesh Sharma | 881ff67 | 2011-02-13 22:51:44 -0800 | [diff] [blame] | 414 | stats->rx_packets++; |
| 415 | stats->rx_bytes += frame->can_dlc; |
Thomas Gleixner | 9c64863 | 2014-04-11 08:13:12 +0000 | [diff] [blame] | 416 | |
| 417 | netif_receive_skb(skb); |
Bhupesh Sharma | 881ff67 | 2011-02-13 22:51:44 -0800 | [diff] [blame] | 418 | return 0; |
| 419 | } |
| 420 | |
| 421 | static void c_can_setup_receive_object(struct net_device *dev, int iface, |
Thomas Gleixner | 8ff2de0 | 2014-04-11 08:13:18 +0000 | [diff] [blame] | 422 | u32 obj, u32 mask, u32 id, u32 mcont) |
Bhupesh Sharma | 881ff67 | 2011-02-13 22:51:44 -0800 | [diff] [blame] | 423 | { |
| 424 | struct c_can_priv *priv = netdev_priv(dev); |
| 425 | |
Thomas Gleixner | 8ff2de0 | 2014-04-11 08:13:18 +0000 | [diff] [blame] | 426 | mask |= BIT(29); |
| 427 | priv->write_reg(priv, C_CAN_IFACE(MASK1_REG, iface), mask); |
| 428 | priv->write_reg(priv, C_CAN_IFACE(MASK2_REG, iface), mask >> 16); |
Alexander Stein | 2bd3bc4 | 2012-12-13 10:06:10 +0100 | [diff] [blame] | 429 | |
Thomas Gleixner | d48071b | 2014-04-11 08:13:21 +0000 | [diff] [blame] | 430 | id |= IF_ARB_MSGVAL; |
Thomas Gleixner | 8ff2de0 | 2014-04-11 08:13:18 +0000 | [diff] [blame] | 431 | priv->write_reg(priv, C_CAN_IFACE(ARB1_REG, iface), id); |
| 432 | priv->write_reg(priv, C_CAN_IFACE(ARB2_REG, iface), id >> 16); |
Bhupesh Sharma | 881ff67 | 2011-02-13 22:51:44 -0800 | [diff] [blame] | 433 | |
AnilKumar Ch | 33f8100 | 2012-05-29 11:13:15 +0530 | [diff] [blame] | 434 | priv->write_reg(priv, C_CAN_IFACE(MSGCTRL_REG, iface), mcont); |
Thomas Gleixner | 8ff2de0 | 2014-04-11 08:13:18 +0000 | [diff] [blame] | 435 | c_can_object_put(dev, iface, obj, IF_COMM_RCV_SETUP); |
Bhupesh Sharma | 881ff67 | 2011-02-13 22:51:44 -0800 | [diff] [blame] | 436 | } |
| 437 | |
Bhupesh Sharma | 881ff67 | 2011-02-13 22:51:44 -0800 | [diff] [blame] | 438 | static netdev_tx_t c_can_start_xmit(struct sk_buff *skb, |
Thomas Gleixner | 35bdafb5 | 2014-04-11 08:13:22 +0000 | [diff] [blame] | 439 | struct net_device *dev) |
Bhupesh Sharma | 881ff67 | 2011-02-13 22:51:44 -0800 | [diff] [blame] | 440 | { |
Bhupesh Sharma | 881ff67 | 2011-02-13 22:51:44 -0800 | [diff] [blame] | 441 | struct can_frame *frame = (struct can_frame *)skb->data; |
Thomas Gleixner | 35bdafb5 | 2014-04-11 08:13:22 +0000 | [diff] [blame] | 442 | struct c_can_priv *priv = netdev_priv(dev); |
| 443 | u32 idx, obj; |
Bhupesh Sharma | 881ff67 | 2011-02-13 22:51:44 -0800 | [diff] [blame] | 444 | |
| 445 | if (can_dropped_invalid_skb(dev, skb)) |
| 446 | return NETDEV_TX_OK; |
Bhupesh Sharma | 881ff67 | 2011-02-13 22:51:44 -0800 | [diff] [blame] | 447 | /* |
Thomas Gleixner | 35bdafb5 | 2014-04-11 08:13:22 +0000 | [diff] [blame] | 448 | * This is not a FIFO. C/D_CAN sends out the buffers |
| 449 | * prioritized. The lowest buffer number wins. |
Bhupesh Sharma | 881ff67 | 2011-02-13 22:51:44 -0800 | [diff] [blame] | 450 | */ |
Thomas Gleixner | 35bdafb5 | 2014-04-11 08:13:22 +0000 | [diff] [blame] | 451 | idx = fls(atomic_read(&priv->tx_active)); |
| 452 | obj = idx + C_CAN_MSG_OBJ_TX_FIRST; |
| 453 | |
| 454 | /* If this is the last buffer, stop the xmit queue */ |
| 455 | if (idx == C_CAN_MSG_OBJ_TX_NUM - 1) |
Bhupesh Sharma | 881ff67 | 2011-02-13 22:51:44 -0800 | [diff] [blame] | 456 | netif_stop_queue(dev); |
Thomas Gleixner | 35bdafb5 | 2014-04-11 08:13:22 +0000 | [diff] [blame] | 457 | /* |
| 458 | * Store the message in the interface so we can call |
| 459 | * can_put_echo_skb(). We must do this before we enable |
| 460 | * transmit as we might race against do_tx(). |
| 461 | */ |
Thomas Gleixner | 9394159 | 2014-04-11 08:13:22 +0000 | [diff] [blame] | 462 | c_can_setup_tx_object(dev, IF_TX, frame, idx); |
Thomas Gleixner | 35bdafb5 | 2014-04-11 08:13:22 +0000 | [diff] [blame] | 463 | priv->dlc[idx] = frame->can_dlc; |
| 464 | can_put_echo_skb(skb, dev, idx); |
| 465 | |
| 466 | /* Update the active bits */ |
| 467 | atomic_add((1 << idx), &priv->tx_active); |
| 468 | /* Start transmission */ |
| 469 | c_can_object_put(dev, IF_TX, obj, IF_COMM_TX); |
Bhupesh Sharma | 881ff67 | 2011-02-13 22:51:44 -0800 | [diff] [blame] | 470 | |
| 471 | return NETDEV_TX_OK; |
| 472 | } |
| 473 | |
Thomas Gleixner | 9fac1d1 | 2014-03-18 17:19:08 +0000 | [diff] [blame] | 474 | static int c_can_wait_for_ctrl_init(struct net_device *dev, |
| 475 | struct c_can_priv *priv, u32 init) |
| 476 | { |
| 477 | int retry = 0; |
| 478 | |
| 479 | while (init != (priv->read_reg(priv, C_CAN_CTRL_REG) & CONTROL_INIT)) { |
| 480 | udelay(10); |
| 481 | if (retry++ > 1000) { |
| 482 | netdev_err(dev, "CCTRL: set CONTROL_INIT failed\n"); |
| 483 | return -EIO; |
| 484 | } |
| 485 | } |
| 486 | return 0; |
| 487 | } |
| 488 | |
Bhupesh Sharma | 881ff67 | 2011-02-13 22:51:44 -0800 | [diff] [blame] | 489 | static int c_can_set_bittiming(struct net_device *dev) |
| 490 | { |
| 491 | unsigned int reg_btr, reg_brpe, ctrl_save; |
| 492 | u8 brp, brpe, sjw, tseg1, tseg2; |
| 493 | u32 ten_bit_brp; |
| 494 | struct c_can_priv *priv = netdev_priv(dev); |
| 495 | const struct can_bittiming *bt = &priv->can.bittiming; |
Thomas Gleixner | 9fac1d1 | 2014-03-18 17:19:08 +0000 | [diff] [blame] | 496 | int res; |
Bhupesh Sharma | 881ff67 | 2011-02-13 22:51:44 -0800 | [diff] [blame] | 497 | |
| 498 | /* c_can provides a 6-bit brp and 4-bit brpe fields */ |
| 499 | ten_bit_brp = bt->brp - 1; |
| 500 | brp = ten_bit_brp & BTR_BRP_MASK; |
| 501 | brpe = ten_bit_brp >> 6; |
| 502 | |
| 503 | sjw = bt->sjw - 1; |
| 504 | tseg1 = bt->prop_seg + bt->phase_seg1 - 1; |
| 505 | tseg2 = bt->phase_seg2 - 1; |
| 506 | reg_btr = brp | (sjw << BTR_SJW_SHIFT) | (tseg1 << BTR_TSEG1_SHIFT) | |
| 507 | (tseg2 << BTR_TSEG2_SHIFT); |
| 508 | reg_brpe = brpe & BRP_EXT_BRPE_MASK; |
| 509 | |
| 510 | netdev_info(dev, |
| 511 | "setting BTR=%04x BRPE=%04x\n", reg_btr, reg_brpe); |
| 512 | |
AnilKumar Ch | 33f8100 | 2012-05-29 11:13:15 +0530 | [diff] [blame] | 513 | ctrl_save = priv->read_reg(priv, C_CAN_CTRL_REG); |
Thomas Gleixner | 9fac1d1 | 2014-03-18 17:19:08 +0000 | [diff] [blame] | 514 | ctrl_save &= ~CONTROL_INIT; |
| 515 | priv->write_reg(priv, C_CAN_CTRL_REG, CONTROL_CCE | CONTROL_INIT); |
| 516 | res = c_can_wait_for_ctrl_init(dev, priv, CONTROL_INIT); |
| 517 | if (res) |
| 518 | return res; |
| 519 | |
AnilKumar Ch | 33f8100 | 2012-05-29 11:13:15 +0530 | [diff] [blame] | 520 | priv->write_reg(priv, C_CAN_BTR_REG, reg_btr); |
| 521 | priv->write_reg(priv, C_CAN_BRPEXT_REG, reg_brpe); |
| 522 | priv->write_reg(priv, C_CAN_CTRL_REG, ctrl_save); |
Bhupesh Sharma | 881ff67 | 2011-02-13 22:51:44 -0800 | [diff] [blame] | 523 | |
Thomas Gleixner | 9fac1d1 | 2014-03-18 17:19:08 +0000 | [diff] [blame] | 524 | return c_can_wait_for_ctrl_init(dev, priv, 0); |
Bhupesh Sharma | 881ff67 | 2011-02-13 22:51:44 -0800 | [diff] [blame] | 525 | } |
| 526 | |
| 527 | /* |
| 528 | * Configure C_CAN message objects for Tx and Rx purposes: |
| 529 | * C_CAN provides a total of 32 message objects that can be configured |
| 530 | * either for Tx or Rx purposes. Here the first 16 message objects are used as |
| 531 | * a reception FIFO. The end of reception FIFO is signified by the EoB bit |
| 532 | * being SET. The remaining 16 message objects are kept aside for Tx purposes. |
| 533 | * See user guide document for further details on configuring message |
| 534 | * objects. |
| 535 | */ |
| 536 | static void c_can_configure_msg_objects(struct net_device *dev) |
| 537 | { |
| 538 | int i; |
| 539 | |
| 540 | /* first invalidate all message objects */ |
| 541 | for (i = C_CAN_MSG_OBJ_RX_FIRST; i <= C_CAN_NO_OF_OBJECTS; i++) |
Thomas Gleixner | 640916d | 2014-03-18 17:19:09 +0000 | [diff] [blame] | 542 | c_can_inval_msg_object(dev, IF_RX, i); |
Bhupesh Sharma | 881ff67 | 2011-02-13 22:51:44 -0800 | [diff] [blame] | 543 | |
| 544 | /* setup receive message objects */ |
| 545 | for (i = C_CAN_MSG_OBJ_RX_FIRST; i < C_CAN_MSG_OBJ_RX_LAST; i++) |
Thomas Gleixner | 8ff2de0 | 2014-04-11 08:13:18 +0000 | [diff] [blame] | 546 | c_can_setup_receive_object(dev, IF_RX, i, 0, 0, IF_MCONT_RCV); |
Bhupesh Sharma | 881ff67 | 2011-02-13 22:51:44 -0800 | [diff] [blame] | 547 | |
Thomas Gleixner | 640916d | 2014-03-18 17:19:09 +0000 | [diff] [blame] | 548 | c_can_setup_receive_object(dev, IF_RX, C_CAN_MSG_OBJ_RX_LAST, 0, 0, |
Thomas Gleixner | 8ff2de0 | 2014-04-11 08:13:18 +0000 | [diff] [blame] | 549 | IF_MCONT_RCV_EOB); |
Bhupesh Sharma | 881ff67 | 2011-02-13 22:51:44 -0800 | [diff] [blame] | 550 | } |
| 551 | |
| 552 | /* |
| 553 | * Configure C_CAN chip: |
| 554 | * - enable/disable auto-retransmission |
| 555 | * - set operating mode |
| 556 | * - configure message objects |
| 557 | */ |
Marc Kleine-Budde | 130a517 | 2014-03-18 19:06:01 +0100 | [diff] [blame] | 558 | static int c_can_chip_config(struct net_device *dev) |
Bhupesh Sharma | 881ff67 | 2011-02-13 22:51:44 -0800 | [diff] [blame] | 559 | { |
| 560 | struct c_can_priv *priv = netdev_priv(dev); |
| 561 | |
Marc Kleine-Budde | ee6f098 | 2011-03-24 02:34:32 +0000 | [diff] [blame] | 562 | /* enable automatic retransmission */ |
Thomas Gleixner | bed11db | 2014-04-11 08:13:10 +0000 | [diff] [blame] | 563 | priv->write_reg(priv, C_CAN_CTRL_REG, CONTROL_ENABLE_AR); |
Bhupesh Sharma | 881ff67 | 2011-02-13 22:51:44 -0800 | [diff] [blame] | 564 | |
Dan Carpenter | d9cb9bd | 2012-06-15 00:20:44 +0000 | [diff] [blame] | 565 | if ((priv->can.ctrlmode & CAN_CTRLMODE_LISTENONLY) && |
| 566 | (priv->can.ctrlmode & CAN_CTRLMODE_LOOPBACK)) { |
Bhupesh Sharma | 881ff67 | 2011-02-13 22:51:44 -0800 | [diff] [blame] | 567 | /* loopback + silent mode : useful for hot self-test */ |
Thomas Gleixner | bed11db | 2014-04-11 08:13:10 +0000 | [diff] [blame] | 568 | priv->write_reg(priv, C_CAN_CTRL_REG, CONTROL_TEST); |
| 569 | priv->write_reg(priv, C_CAN_TEST_REG, TEST_LBACK | TEST_SILENT); |
Bhupesh Sharma | 881ff67 | 2011-02-13 22:51:44 -0800 | [diff] [blame] | 570 | } else if (priv->can.ctrlmode & CAN_CTRLMODE_LOOPBACK) { |
| 571 | /* loopback mode : useful for self-test function */ |
Thomas Gleixner | bed11db | 2014-04-11 08:13:10 +0000 | [diff] [blame] | 572 | priv->write_reg(priv, C_CAN_CTRL_REG, CONTROL_TEST); |
AnilKumar Ch | 33f8100 | 2012-05-29 11:13:15 +0530 | [diff] [blame] | 573 | priv->write_reg(priv, C_CAN_TEST_REG, TEST_LBACK); |
Bhupesh Sharma | 881ff67 | 2011-02-13 22:51:44 -0800 | [diff] [blame] | 574 | } else if (priv->can.ctrlmode & CAN_CTRLMODE_LISTENONLY) { |
| 575 | /* silent mode : bus-monitoring mode */ |
Thomas Gleixner | bed11db | 2014-04-11 08:13:10 +0000 | [diff] [blame] | 576 | priv->write_reg(priv, C_CAN_CTRL_REG, CONTROL_TEST); |
AnilKumar Ch | 33f8100 | 2012-05-29 11:13:15 +0530 | [diff] [blame] | 577 | priv->write_reg(priv, C_CAN_TEST_REG, TEST_SILENT); |
Thomas Gleixner | bed11db | 2014-04-11 08:13:10 +0000 | [diff] [blame] | 578 | } |
Bhupesh Sharma | 881ff67 | 2011-02-13 22:51:44 -0800 | [diff] [blame] | 579 | |
| 580 | /* configure message objects */ |
| 581 | c_can_configure_msg_objects(dev); |
| 582 | |
| 583 | /* set a `lec` value so that we can check for updates later */ |
AnilKumar Ch | 33f8100 | 2012-05-29 11:13:15 +0530 | [diff] [blame] | 584 | priv->write_reg(priv, C_CAN_STS_REG, LEC_UNUSED); |
Bhupesh Sharma | 881ff67 | 2011-02-13 22:51:44 -0800 | [diff] [blame] | 585 | |
Thomas Gleixner | 35bdafb5 | 2014-04-11 08:13:22 +0000 | [diff] [blame] | 586 | /* Clear all internal status */ |
| 587 | atomic_set(&priv->tx_active, 0); |
| 588 | priv->rxmasked = 0; |
Thomas Gleixner | 9394159 | 2014-04-11 08:13:22 +0000 | [diff] [blame] | 589 | priv->tx_dir = 0; |
Thomas Gleixner | 35bdafb5 | 2014-04-11 08:13:22 +0000 | [diff] [blame] | 590 | |
Bhupesh Sharma | 881ff67 | 2011-02-13 22:51:44 -0800 | [diff] [blame] | 591 | /* set bittiming params */ |
Marc Kleine-Budde | 130a517 | 2014-03-18 19:06:01 +0100 | [diff] [blame] | 592 | return c_can_set_bittiming(dev); |
Bhupesh Sharma | 881ff67 | 2011-02-13 22:51:44 -0800 | [diff] [blame] | 593 | } |
| 594 | |
Marc Kleine-Budde | 130a517 | 2014-03-18 19:06:01 +0100 | [diff] [blame] | 595 | static int c_can_start(struct net_device *dev) |
Bhupesh Sharma | 881ff67 | 2011-02-13 22:51:44 -0800 | [diff] [blame] | 596 | { |
| 597 | struct c_can_priv *priv = netdev_priv(dev); |
Marc Kleine-Budde | 130a517 | 2014-03-18 19:06:01 +0100 | [diff] [blame] | 598 | int err; |
Bhupesh Sharma | 881ff67 | 2011-02-13 22:51:44 -0800 | [diff] [blame] | 599 | |
Bhupesh Sharma | 881ff67 | 2011-02-13 22:51:44 -0800 | [diff] [blame] | 600 | /* basic c_can configuration */ |
Marc Kleine-Budde | 130a517 | 2014-03-18 19:06:01 +0100 | [diff] [blame] | 601 | err = c_can_chip_config(dev); |
| 602 | if (err) |
| 603 | return err; |
Bhupesh Sharma | 881ff67 | 2011-02-13 22:51:44 -0800 | [diff] [blame] | 604 | |
Thomas Gleixner | d61d09d | 2014-04-11 08:13:17 +0000 | [diff] [blame] | 605 | /* Setup the command for new messages */ |
| 606 | priv->comm_rcv_high = priv->type != BOSCH_D_CAN ? |
| 607 | IF_COMM_RCV_LOW : IF_COMM_RCV_HIGH; |
| 608 | |
Bhupesh Sharma | 881ff67 | 2011-02-13 22:51:44 -0800 | [diff] [blame] | 609 | priv->can.state = CAN_STATE_ERROR_ACTIVE; |
| 610 | |
Marc Kleine-Budde | 130a517 | 2014-03-18 19:06:01 +0100 | [diff] [blame] | 611 | return 0; |
Bhupesh Sharma | 881ff67 | 2011-02-13 22:51:44 -0800 | [diff] [blame] | 612 | } |
| 613 | |
| 614 | static void c_can_stop(struct net_device *dev) |
| 615 | { |
| 616 | struct c_can_priv *priv = netdev_priv(dev); |
| 617 | |
Thomas Gleixner | 2d5f4f8 | 2014-04-11 08:13:17 +0000 | [diff] [blame] | 618 | c_can_irq_control(priv, false); |
Bhupesh Sharma | 881ff67 | 2011-02-13 22:51:44 -0800 | [diff] [blame] | 619 | priv->can.state = CAN_STATE_STOPPED; |
| 620 | } |
| 621 | |
| 622 | static int c_can_set_mode(struct net_device *dev, enum can_mode mode) |
| 623 | { |
Thomas Gleixner | bed11db | 2014-04-11 08:13:10 +0000 | [diff] [blame] | 624 | struct c_can_priv *priv = netdev_priv(dev); |
Marc Kleine-Budde | 130a517 | 2014-03-18 19:06:01 +0100 | [diff] [blame] | 625 | int err; |
| 626 | |
Bhupesh Sharma | 881ff67 | 2011-02-13 22:51:44 -0800 | [diff] [blame] | 627 | switch (mode) { |
| 628 | case CAN_MODE_START: |
Marc Kleine-Budde | 130a517 | 2014-03-18 19:06:01 +0100 | [diff] [blame] | 629 | err = c_can_start(dev); |
| 630 | if (err) |
| 631 | return err; |
Bhupesh Sharma | 881ff67 | 2011-02-13 22:51:44 -0800 | [diff] [blame] | 632 | netif_wake_queue(dev); |
Thomas Gleixner | 2d5f4f8 | 2014-04-11 08:13:17 +0000 | [diff] [blame] | 633 | c_can_irq_control(priv, true); |
Bhupesh Sharma | 881ff67 | 2011-02-13 22:51:44 -0800 | [diff] [blame] | 634 | break; |
| 635 | default: |
| 636 | return -EOPNOTSUPP; |
| 637 | } |
| 638 | |
| 639 | return 0; |
| 640 | } |
| 641 | |
Marc Kleine-Budde | e35d46a | 2013-11-24 23:31:24 +0100 | [diff] [blame] | 642 | static int __c_can_get_berr_counter(const struct net_device *dev, |
| 643 | struct can_berr_counter *bec) |
Bhupesh Sharma | 881ff67 | 2011-02-13 22:51:44 -0800 | [diff] [blame] | 644 | { |
| 645 | unsigned int reg_err_counter; |
| 646 | struct c_can_priv *priv = netdev_priv(dev); |
| 647 | |
AnilKumar Ch | 33f8100 | 2012-05-29 11:13:15 +0530 | [diff] [blame] | 648 | reg_err_counter = priv->read_reg(priv, C_CAN_ERR_CNT_REG); |
Bhupesh Sharma | 881ff67 | 2011-02-13 22:51:44 -0800 | [diff] [blame] | 649 | bec->rxerr = (reg_err_counter & ERR_CNT_REC_MASK) >> |
| 650 | ERR_CNT_REC_SHIFT; |
| 651 | bec->txerr = reg_err_counter & ERR_CNT_TEC_MASK; |
| 652 | |
Marc Kleine-Budde | e35d46a | 2013-11-24 23:31:24 +0100 | [diff] [blame] | 653 | return 0; |
| 654 | } |
| 655 | |
| 656 | static int c_can_get_berr_counter(const struct net_device *dev, |
| 657 | struct can_berr_counter *bec) |
| 658 | { |
| 659 | struct c_can_priv *priv = netdev_priv(dev); |
| 660 | int err; |
| 661 | |
| 662 | c_can_pm_runtime_get_sync(priv); |
| 663 | err = __c_can_get_berr_counter(dev, bec); |
AnilKumar Ch | 4cdd34b | 2012-08-20 16:50:54 +0530 | [diff] [blame] | 664 | c_can_pm_runtime_put_sync(priv); |
| 665 | |
Marc Kleine-Budde | e35d46a | 2013-11-24 23:31:24 +0100 | [diff] [blame] | 666 | return err; |
Bhupesh Sharma | 881ff67 | 2011-02-13 22:51:44 -0800 | [diff] [blame] | 667 | } |
| 668 | |
Bhupesh Sharma | 881ff67 | 2011-02-13 22:51:44 -0800 | [diff] [blame] | 669 | static void c_can_do_tx(struct net_device *dev) |
| 670 | { |
Bhupesh Sharma | 881ff67 | 2011-02-13 22:51:44 -0800 | [diff] [blame] | 671 | struct c_can_priv *priv = netdev_priv(dev); |
| 672 | struct net_device_stats *stats = &dev->stats; |
Thomas Gleixner | 35bdafb5 | 2014-04-11 08:13:22 +0000 | [diff] [blame] | 673 | u32 idx, obj, pkts = 0, bytes = 0, pend, clr; |
Bhupesh Sharma | 881ff67 | 2011-02-13 22:51:44 -0800 | [diff] [blame] | 674 | |
Thomas Gleixner | 35bdafb5 | 2014-04-11 08:13:22 +0000 | [diff] [blame] | 675 | clr = pend = priv->read_reg(priv, C_CAN_INTPND2_REG); |
Thomas Gleixner | bf88a206 | 2014-03-18 17:19:12 +0000 | [diff] [blame] | 676 | |
Thomas Gleixner | 35bdafb5 | 2014-04-11 08:13:22 +0000 | [diff] [blame] | 677 | while ((idx = ffs(pend))) { |
| 678 | idx--; |
| 679 | pend &= ~(1 << idx); |
| 680 | obj = idx + C_CAN_MSG_OBJ_TX_FIRST; |
Thomas Gleixner | 9394159 | 2014-04-11 08:13:22 +0000 | [diff] [blame] | 681 | c_can_inval_tx_object(dev, IF_RX, obj); |
Thomas Gleixner | 35bdafb5 | 2014-04-11 08:13:22 +0000 | [diff] [blame] | 682 | can_get_echo_skb(dev, idx); |
| 683 | bytes += priv->dlc[idx]; |
Thomas Gleixner | 5a7513a | 2014-03-18 17:19:14 +0000 | [diff] [blame] | 684 | pkts++; |
Bhupesh Sharma | 881ff67 | 2011-02-13 22:51:44 -0800 | [diff] [blame] | 685 | } |
| 686 | |
Thomas Gleixner | 35bdafb5 | 2014-04-11 08:13:22 +0000 | [diff] [blame] | 687 | /* Clear the bits in the tx_active mask */ |
| 688 | atomic_sub(clr, &priv->tx_active); |
Thomas Gleixner | bf88a206 | 2014-03-18 17:19:12 +0000 | [diff] [blame] | 689 | |
Thomas Gleixner | 35bdafb5 | 2014-04-11 08:13:22 +0000 | [diff] [blame] | 690 | if (clr & (1 << (C_CAN_MSG_OBJ_TX_NUM - 1))) |
| 691 | netif_wake_queue(dev); |
Thomas Gleixner | 5a7513a | 2014-03-18 17:19:14 +0000 | [diff] [blame] | 692 | |
| 693 | if (pkts) { |
| 694 | stats->tx_bytes += bytes; |
| 695 | stats->tx_packets += pkts; |
| 696 | can_led_event(dev, CAN_LED_EVENT_TX); |
| 697 | } |
Bhupesh Sharma | 881ff67 | 2011-02-13 22:51:44 -0800 | [diff] [blame] | 698 | } |
| 699 | |
| 700 | /* |
Thomas Gleixner | 64f08f2 | 2014-03-18 17:19:10 +0000 | [diff] [blame] | 701 | * If we have a gap in the pending bits, that means we either |
| 702 | * raced with the hardware or failed to readout all upper |
| 703 | * objects in the last run due to quota limit. |
| 704 | */ |
| 705 | static u32 c_can_adjust_pending(u32 pend) |
| 706 | { |
| 707 | u32 weight, lasts; |
| 708 | |
| 709 | if (pend == RECEIVE_OBJECT_BITS) |
| 710 | return pend; |
| 711 | |
| 712 | /* |
| 713 | * If the last set bit is larger than the number of pending |
| 714 | * bits we have a gap. |
| 715 | */ |
| 716 | weight = hweight32(pend); |
| 717 | lasts = fls(pend); |
| 718 | |
| 719 | /* If the bits are linear, nothing to do */ |
| 720 | if (lasts == weight) |
| 721 | return pend; |
| 722 | |
| 723 | /* |
| 724 | * Find the first set bit after the gap. We walk backwards |
| 725 | * from the last set bit. |
| 726 | */ |
| 727 | for (lasts--; pend & (1 << (lasts - 1)); lasts--); |
| 728 | |
| 729 | return pend & ~((1 << lasts) - 1); |
| 730 | } |
| 731 | |
Thomas Gleixner | d61d09d | 2014-04-11 08:13:17 +0000 | [diff] [blame] | 732 | static inline void c_can_rx_object_get(struct net_device *dev, |
| 733 | struct c_can_priv *priv, u32 obj) |
Thomas Gleixner | 2b9aecd | 2014-04-11 08:13:16 +0000 | [diff] [blame] | 734 | { |
Thomas Gleixner | d61d09d | 2014-04-11 08:13:17 +0000 | [diff] [blame] | 735 | c_can_object_get(dev, IF_RX, obj, priv->comm_rcv_high); |
Thomas Gleixner | 2b9aecd | 2014-04-11 08:13:16 +0000 | [diff] [blame] | 736 | } |
| 737 | |
| 738 | static inline void c_can_rx_finalize(struct net_device *dev, |
| 739 | struct c_can_priv *priv, u32 obj) |
| 740 | { |
Thomas Gleixner | d61d09d | 2014-04-11 08:13:17 +0000 | [diff] [blame] | 741 | if (priv->type != BOSCH_D_CAN) |
| 742 | c_can_object_get(dev, IF_RX, obj, IF_COMM_CLR_NEWDAT); |
Thomas Gleixner | 2b9aecd | 2014-04-11 08:13:16 +0000 | [diff] [blame] | 743 | } |
| 744 | |
Thomas Gleixner | 520f570 | 2014-03-18 19:27:42 +0100 | [diff] [blame] | 745 | static int c_can_read_objects(struct net_device *dev, struct c_can_priv *priv, |
| 746 | u32 pend, int quota) |
| 747 | { |
Thomas Gleixner | 2b9aecd | 2014-04-11 08:13:16 +0000 | [diff] [blame] | 748 | u32 pkts = 0, ctrl, obj; |
Thomas Gleixner | 520f570 | 2014-03-18 19:27:42 +0100 | [diff] [blame] | 749 | |
| 750 | while ((obj = ffs(pend)) && quota > 0) { |
| 751 | pend &= ~BIT(obj - 1); |
| 752 | |
Thomas Gleixner | d61d09d | 2014-04-11 08:13:17 +0000 | [diff] [blame] | 753 | c_can_rx_object_get(dev, priv, obj); |
Thomas Gleixner | 520f570 | 2014-03-18 19:27:42 +0100 | [diff] [blame] | 754 | ctrl = priv->read_reg(priv, C_CAN_IFACE(MSGCTRL_REG, IF_RX)); |
| 755 | |
| 756 | if (ctrl & IF_MCONT_MSGLST) { |
| 757 | int n = c_can_handle_lost_msg_obj(dev, IF_RX, obj, ctrl); |
| 758 | |
| 759 | pkts += n; |
| 760 | quota -= n; |
| 761 | continue; |
| 762 | } |
| 763 | |
| 764 | /* |
| 765 | * This really should not happen, but this covers some |
| 766 | * odd HW behaviour. Do not remove that unless you |
| 767 | * want to brick your machine. |
| 768 | */ |
| 769 | if (!(ctrl & IF_MCONT_NEWDAT)) |
| 770 | continue; |
| 771 | |
| 772 | /* read the data from the message object */ |
| 773 | c_can_read_msg_object(dev, IF_RX, ctrl); |
| 774 | |
Thomas Gleixner | 2b9aecd | 2014-04-11 08:13:16 +0000 | [diff] [blame] | 775 | c_can_rx_finalize(dev, priv, obj); |
Thomas Gleixner | 520f570 | 2014-03-18 19:27:42 +0100 | [diff] [blame] | 776 | |
| 777 | pkts++; |
| 778 | quota--; |
| 779 | } |
| 780 | |
| 781 | return pkts; |
Bhupesh Sharma | 881ff67 | 2011-02-13 22:51:44 -0800 | [diff] [blame] | 782 | } |
| 783 | |
Thomas Gleixner | 2b9aecd | 2014-04-11 08:13:16 +0000 | [diff] [blame] | 784 | static inline u32 c_can_get_pending(struct c_can_priv *priv) |
| 785 | { |
| 786 | u32 pend = priv->read_reg(priv, C_CAN_NEWDAT1_REG); |
| 787 | |
Thomas Gleixner | 2b9aecd | 2014-04-11 08:13:16 +0000 | [diff] [blame] | 788 | return pend; |
| 789 | } |
| 790 | |
Bhupesh Sharma | 881ff67 | 2011-02-13 22:51:44 -0800 | [diff] [blame] | 791 | /* |
| 792 | * theory of operation: |
| 793 | * |
| 794 | * c_can core saves a received CAN message into the first free message |
| 795 | * object it finds free (starting with the lowest). Bits NEWDAT and |
| 796 | * INTPND are set for this message object indicating that a new message |
| 797 | * has arrived. To work-around this issue, we keep two groups of message |
| 798 | * objects whose partitioning is defined by C_CAN_MSG_OBJ_RX_SPLIT. |
| 799 | * |
Thomas Gleixner | 2b9aecd | 2014-04-11 08:13:16 +0000 | [diff] [blame] | 800 | * We clear the newdat bit right away. |
| 801 | * |
| 802 | * This can result in packet reordering when the readout is slow. |
Bhupesh Sharma | 881ff67 | 2011-02-13 22:51:44 -0800 | [diff] [blame] | 803 | */ |
| 804 | static int c_can_do_rx_poll(struct net_device *dev, int quota) |
| 805 | { |
Bhupesh Sharma | 881ff67 | 2011-02-13 22:51:44 -0800 | [diff] [blame] | 806 | struct c_can_priv *priv = netdev_priv(dev); |
Thomas Gleixner | 520f570 | 2014-03-18 19:27:42 +0100 | [diff] [blame] | 807 | u32 pkts = 0, pend = 0, toread, n; |
Bhupesh Sharma | 881ff67 | 2011-02-13 22:51:44 -0800 | [diff] [blame] | 808 | |
Markus Pargmann | 4ce78a8 | 2013-11-01 10:36:36 +0100 | [diff] [blame] | 809 | /* |
| 810 | * It is faster to read only one 16bit register. This is only possible |
| 811 | * for a maximum number of 16 objects. |
| 812 | */ |
| 813 | BUILD_BUG_ON_MSG(C_CAN_MSG_OBJ_RX_LAST > 16, |
| 814 | "Implementation does not support more message objects than 16"); |
| 815 | |
Thomas Gleixner | 64f08f2 | 2014-03-18 17:19:10 +0000 | [diff] [blame] | 816 | while (quota > 0) { |
Thomas Gleixner | 64f08f2 | 2014-03-18 17:19:10 +0000 | [diff] [blame] | 817 | if (!pend) { |
Thomas Gleixner | 2b9aecd | 2014-04-11 08:13:16 +0000 | [diff] [blame] | 818 | pend = c_can_get_pending(priv); |
Thomas Gleixner | 64f08f2 | 2014-03-18 17:19:10 +0000 | [diff] [blame] | 819 | if (!pend) |
Thomas Gleixner | 520f570 | 2014-03-18 19:27:42 +0100 | [diff] [blame] | 820 | break; |
Thomas Gleixner | 64f08f2 | 2014-03-18 17:19:10 +0000 | [diff] [blame] | 821 | /* |
| 822 | * If the pending field has a gap, handle the |
| 823 | * bits above the gap first. |
| 824 | */ |
Thomas Gleixner | 520f570 | 2014-03-18 19:27:42 +0100 | [diff] [blame] | 825 | toread = c_can_adjust_pending(pend); |
Thomas Gleixner | 64f08f2 | 2014-03-18 17:19:10 +0000 | [diff] [blame] | 826 | } else { |
Thomas Gleixner | 520f570 | 2014-03-18 19:27:42 +0100 | [diff] [blame] | 827 | toread = pend; |
Bhupesh Sharma | 881ff67 | 2011-02-13 22:51:44 -0800 | [diff] [blame] | 828 | } |
Thomas Gleixner | 64f08f2 | 2014-03-18 17:19:10 +0000 | [diff] [blame] | 829 | /* Remove the bits from pend */ |
Thomas Gleixner | 520f570 | 2014-03-18 19:27:42 +0100 | [diff] [blame] | 830 | pend &= ~toread; |
| 831 | /* Read the objects */ |
| 832 | n = c_can_read_objects(dev, priv, toread, quota); |
| 833 | pkts += n; |
| 834 | quota -= n; |
Bhupesh Sharma | 881ff67 | 2011-02-13 22:51:44 -0800 | [diff] [blame] | 835 | } |
| 836 | |
Thomas Gleixner | b1d8e43 | 2014-03-18 17:19:15 +0000 | [diff] [blame] | 837 | if (pkts) |
| 838 | can_led_event(dev, CAN_LED_EVENT_RX); |
| 839 | |
Thomas Gleixner | 520f570 | 2014-03-18 19:27:42 +0100 | [diff] [blame] | 840 | return pkts; |
Bhupesh Sharma | 881ff67 | 2011-02-13 22:51:44 -0800 | [diff] [blame] | 841 | } |
| 842 | |
Bhupesh Sharma | 881ff67 | 2011-02-13 22:51:44 -0800 | [diff] [blame] | 843 | static int c_can_handle_state_change(struct net_device *dev, |
| 844 | enum c_can_bus_error_types error_type) |
| 845 | { |
| 846 | unsigned int reg_err_counter; |
| 847 | unsigned int rx_err_passive; |
| 848 | struct c_can_priv *priv = netdev_priv(dev); |
| 849 | struct net_device_stats *stats = &dev->stats; |
| 850 | struct can_frame *cf; |
| 851 | struct sk_buff *skb; |
| 852 | struct can_berr_counter bec; |
| 853 | |
Thomas Gleixner | f058d54 | 2014-04-11 08:13:12 +0000 | [diff] [blame] | 854 | switch (error_type) { |
| 855 | case C_CAN_ERROR_WARNING: |
| 856 | /* error warning state */ |
| 857 | priv->can.can_stats.error_warning++; |
| 858 | priv->can.state = CAN_STATE_ERROR_WARNING; |
| 859 | break; |
| 860 | case C_CAN_ERROR_PASSIVE: |
| 861 | /* error passive state */ |
| 862 | priv->can.can_stats.error_passive++; |
| 863 | priv->can.state = CAN_STATE_ERROR_PASSIVE; |
| 864 | break; |
| 865 | case C_CAN_BUS_OFF: |
| 866 | /* bus-off state */ |
| 867 | priv->can.state = CAN_STATE_BUS_OFF; |
| 868 | can_bus_off(dev); |
| 869 | break; |
| 870 | default: |
| 871 | break; |
| 872 | } |
| 873 | |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 874 | /* propagate the error condition to the CAN stack */ |
Bhupesh Sharma | 881ff67 | 2011-02-13 22:51:44 -0800 | [diff] [blame] | 875 | skb = alloc_can_err_skb(dev, &cf); |
| 876 | if (unlikely(!skb)) |
| 877 | return 0; |
| 878 | |
Marc Kleine-Budde | e35d46a | 2013-11-24 23:31:24 +0100 | [diff] [blame] | 879 | __c_can_get_berr_counter(dev, &bec); |
AnilKumar Ch | 33f8100 | 2012-05-29 11:13:15 +0530 | [diff] [blame] | 880 | reg_err_counter = priv->read_reg(priv, C_CAN_ERR_CNT_REG); |
Bhupesh Sharma | 881ff67 | 2011-02-13 22:51:44 -0800 | [diff] [blame] | 881 | rx_err_passive = (reg_err_counter & ERR_CNT_RP_MASK) >> |
| 882 | ERR_CNT_RP_SHIFT; |
| 883 | |
| 884 | switch (error_type) { |
| 885 | case C_CAN_ERROR_WARNING: |
| 886 | /* error warning state */ |
Bhupesh Sharma | 881ff67 | 2011-02-13 22:51:44 -0800 | [diff] [blame] | 887 | cf->can_id |= CAN_ERR_CRTL; |
| 888 | cf->data[1] = (bec.txerr > bec.rxerr) ? |
| 889 | CAN_ERR_CRTL_TX_WARNING : |
| 890 | CAN_ERR_CRTL_RX_WARNING; |
| 891 | cf->data[6] = bec.txerr; |
| 892 | cf->data[7] = bec.rxerr; |
| 893 | |
| 894 | break; |
| 895 | case C_CAN_ERROR_PASSIVE: |
| 896 | /* error passive state */ |
Bhupesh Sharma | 881ff67 | 2011-02-13 22:51:44 -0800 | [diff] [blame] | 897 | cf->can_id |= CAN_ERR_CRTL; |
| 898 | if (rx_err_passive) |
| 899 | cf->data[1] |= CAN_ERR_CRTL_RX_PASSIVE; |
| 900 | if (bec.txerr > 127) |
| 901 | cf->data[1] |= CAN_ERR_CRTL_TX_PASSIVE; |
| 902 | |
| 903 | cf->data[6] = bec.txerr; |
| 904 | cf->data[7] = bec.rxerr; |
| 905 | break; |
| 906 | case C_CAN_BUS_OFF: |
| 907 | /* bus-off state */ |
Bhupesh Sharma | 881ff67 | 2011-02-13 22:51:44 -0800 | [diff] [blame] | 908 | cf->can_id |= CAN_ERR_BUSOFF; |
Bhupesh Sharma | 881ff67 | 2011-02-13 22:51:44 -0800 | [diff] [blame] | 909 | can_bus_off(dev); |
| 910 | break; |
| 911 | default: |
| 912 | break; |
| 913 | } |
| 914 | |
Bhupesh Sharma | 881ff67 | 2011-02-13 22:51:44 -0800 | [diff] [blame] | 915 | stats->rx_packets++; |
| 916 | stats->rx_bytes += cf->can_dlc; |
Thomas Gleixner | 9c64863 | 2014-04-11 08:13:12 +0000 | [diff] [blame] | 917 | netif_receive_skb(skb); |
Bhupesh Sharma | 881ff67 | 2011-02-13 22:51:44 -0800 | [diff] [blame] | 918 | |
| 919 | return 1; |
| 920 | } |
| 921 | |
| 922 | static int c_can_handle_bus_err(struct net_device *dev, |
| 923 | enum c_can_lec_type lec_type) |
| 924 | { |
| 925 | struct c_can_priv *priv = netdev_priv(dev); |
| 926 | struct net_device_stats *stats = &dev->stats; |
| 927 | struct can_frame *cf; |
| 928 | struct sk_buff *skb; |
| 929 | |
| 930 | /* |
| 931 | * early exit if no lec update or no error. |
| 932 | * no lec update means that no CAN bus event has been detected |
| 933 | * since CPU wrote 0x7 value to status reg. |
| 934 | */ |
| 935 | if (lec_type == LEC_UNUSED || lec_type == LEC_NO_ERROR) |
| 936 | return 0; |
| 937 | |
Thomas Gleixner | 097aec1 | 2014-04-11 08:13:13 +0000 | [diff] [blame] | 938 | if (!(priv->can.ctrlmode & CAN_CTRLMODE_BERR_REPORTING)) |
| 939 | return 0; |
| 940 | |
Thomas Gleixner | 1da394d | 2014-04-11 08:13:13 +0000 | [diff] [blame] | 941 | /* common for all type of bus errors */ |
| 942 | priv->can.can_stats.bus_error++; |
| 943 | stats->rx_errors++; |
| 944 | |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 945 | /* propagate the error condition to the CAN stack */ |
Bhupesh Sharma | 881ff67 | 2011-02-13 22:51:44 -0800 | [diff] [blame] | 946 | skb = alloc_can_err_skb(dev, &cf); |
| 947 | if (unlikely(!skb)) |
| 948 | return 0; |
| 949 | |
| 950 | /* |
| 951 | * check for 'last error code' which tells us the |
| 952 | * type of the last error to occur on the CAN bus |
| 953 | */ |
Bhupesh Sharma | 881ff67 | 2011-02-13 22:51:44 -0800 | [diff] [blame] | 954 | cf->can_id |= CAN_ERR_PROT | CAN_ERR_BUSERROR; |
| 955 | cf->data[2] |= CAN_ERR_PROT_UNSPEC; |
| 956 | |
| 957 | switch (lec_type) { |
| 958 | case LEC_STUFF_ERROR: |
| 959 | netdev_dbg(dev, "stuff error\n"); |
| 960 | cf->data[2] |= CAN_ERR_PROT_STUFF; |
| 961 | break; |
| 962 | case LEC_FORM_ERROR: |
| 963 | netdev_dbg(dev, "form error\n"); |
| 964 | cf->data[2] |= CAN_ERR_PROT_FORM; |
| 965 | break; |
| 966 | case LEC_ACK_ERROR: |
| 967 | netdev_dbg(dev, "ack error\n"); |
Olivier Sobrie | 6ea4588 | 2013-01-18 09:32:39 +0100 | [diff] [blame] | 968 | cf->data[3] |= (CAN_ERR_PROT_LOC_ACK | |
Bhupesh Sharma | 881ff67 | 2011-02-13 22:51:44 -0800 | [diff] [blame] | 969 | CAN_ERR_PROT_LOC_ACK_DEL); |
| 970 | break; |
| 971 | case LEC_BIT1_ERROR: |
| 972 | netdev_dbg(dev, "bit1 error\n"); |
| 973 | cf->data[2] |= CAN_ERR_PROT_BIT1; |
| 974 | break; |
| 975 | case LEC_BIT0_ERROR: |
| 976 | netdev_dbg(dev, "bit0 error\n"); |
| 977 | cf->data[2] |= CAN_ERR_PROT_BIT0; |
| 978 | break; |
| 979 | case LEC_CRC_ERROR: |
| 980 | netdev_dbg(dev, "CRC error\n"); |
Olivier Sobrie | 6ea4588 | 2013-01-18 09:32:39 +0100 | [diff] [blame] | 981 | cf->data[3] |= (CAN_ERR_PROT_LOC_CRC_SEQ | |
Bhupesh Sharma | 881ff67 | 2011-02-13 22:51:44 -0800 | [diff] [blame] | 982 | CAN_ERR_PROT_LOC_CRC_DEL); |
| 983 | break; |
| 984 | default: |
| 985 | break; |
| 986 | } |
| 987 | |
Bhupesh Sharma | 881ff67 | 2011-02-13 22:51:44 -0800 | [diff] [blame] | 988 | stats->rx_packets++; |
| 989 | stats->rx_bytes += cf->can_dlc; |
Thomas Gleixner | 9c64863 | 2014-04-11 08:13:12 +0000 | [diff] [blame] | 990 | netif_receive_skb(skb); |
Bhupesh Sharma | 881ff67 | 2011-02-13 22:51:44 -0800 | [diff] [blame] | 991 | return 1; |
| 992 | } |
| 993 | |
| 994 | static int c_can_poll(struct napi_struct *napi, int quota) |
| 995 | { |
Bhupesh Sharma | 881ff67 | 2011-02-13 22:51:44 -0800 | [diff] [blame] | 996 | struct net_device *dev = napi->dev; |
| 997 | struct c_can_priv *priv = netdev_priv(dev); |
Thomas Gleixner | fa39b54 | 2014-04-11 08:13:15 +0000 | [diff] [blame] | 998 | u16 curr, last = priv->last_status; |
| 999 | int work_done = 0; |
Bhupesh Sharma | 881ff67 | 2011-02-13 22:51:44 -0800 | [diff] [blame] | 1000 | |
Thomas Gleixner | fa39b54 | 2014-04-11 08:13:15 +0000 | [diff] [blame] | 1001 | priv->last_status = curr = priv->read_reg(priv, C_CAN_STS_REG); |
| 1002 | /* Ack status on C_CAN. D_CAN is self clearing */ |
| 1003 | if (priv->type != BOSCH_D_CAN) |
| 1004 | priv->write_reg(priv, C_CAN_STS_REG, LEC_UNUSED); |
Bhupesh Sharma | 881ff67 | 2011-02-13 22:51:44 -0800 | [diff] [blame] | 1005 | |
Thomas Gleixner | fa39b54 | 2014-04-11 08:13:15 +0000 | [diff] [blame] | 1006 | /* handle state changes */ |
| 1007 | if ((curr & STATUS_EWARN) && (!(last & STATUS_EWARN))) { |
| 1008 | netdev_dbg(dev, "entered error warning state\n"); |
| 1009 | work_done += c_can_handle_state_change(dev, C_CAN_ERROR_WARNING); |
Bhupesh Sharma | 881ff67 | 2011-02-13 22:51:44 -0800 | [diff] [blame] | 1010 | } |
| 1011 | |
Thomas Gleixner | fa39b54 | 2014-04-11 08:13:15 +0000 | [diff] [blame] | 1012 | if ((curr & STATUS_EPASS) && (!(last & STATUS_EPASS))) { |
| 1013 | netdev_dbg(dev, "entered error passive state\n"); |
| 1014 | work_done += c_can_handle_state_change(dev, C_CAN_ERROR_PASSIVE); |
| 1015 | } |
| 1016 | |
| 1017 | if ((curr & STATUS_BOFF) && (!(last & STATUS_BOFF))) { |
| 1018 | netdev_dbg(dev, "entered bus off state\n"); |
| 1019 | work_done += c_can_handle_state_change(dev, C_CAN_BUS_OFF); |
| 1020 | goto end; |
| 1021 | } |
| 1022 | |
| 1023 | /* handle bus recovery events */ |
| 1024 | if ((!(curr & STATUS_BOFF)) && (last & STATUS_BOFF)) { |
| 1025 | netdev_dbg(dev, "left bus off state\n"); |
| 1026 | priv->can.state = CAN_STATE_ERROR_ACTIVE; |
| 1027 | } |
| 1028 | if ((!(curr & STATUS_EPASS)) && (last & STATUS_EPASS)) { |
| 1029 | netdev_dbg(dev, "left error passive state\n"); |
| 1030 | priv->can.state = CAN_STATE_ERROR_ACTIVE; |
| 1031 | } |
| 1032 | |
| 1033 | /* handle lec errors on the bus */ |
| 1034 | work_done += c_can_handle_bus_err(dev, curr & LEC_MASK); |
| 1035 | |
| 1036 | /* Handle Tx/Rx events. We do this unconditionally */ |
| 1037 | work_done += c_can_do_rx_poll(dev, (quota - work_done)); |
| 1038 | c_can_do_tx(dev); |
| 1039 | |
Bhupesh Sharma | 881ff67 | 2011-02-13 22:51:44 -0800 | [diff] [blame] | 1040 | end: |
| 1041 | if (work_done < quota) { |
| 1042 | napi_complete(napi); |
Thomas Gleixner | ef1d2e2 | 2014-04-11 08:13:11 +0000 | [diff] [blame] | 1043 | /* enable all IRQs if we are not in bus off state */ |
| 1044 | if (priv->can.state != CAN_STATE_BUS_OFF) |
Thomas Gleixner | 2d5f4f8 | 2014-04-11 08:13:17 +0000 | [diff] [blame] | 1045 | c_can_irq_control(priv, true); |
Bhupesh Sharma | 881ff67 | 2011-02-13 22:51:44 -0800 | [diff] [blame] | 1046 | } |
| 1047 | |
| 1048 | return work_done; |
| 1049 | } |
| 1050 | |
| 1051 | static irqreturn_t c_can_isr(int irq, void *dev_id) |
| 1052 | { |
Bhupesh Sharma | 881ff67 | 2011-02-13 22:51:44 -0800 | [diff] [blame] | 1053 | struct net_device *dev = (struct net_device *)dev_id; |
| 1054 | struct c_can_priv *priv = netdev_priv(dev); |
| 1055 | |
Thomas Gleixner | fa39b54 | 2014-04-11 08:13:15 +0000 | [diff] [blame] | 1056 | if (!priv->read_reg(priv, C_CAN_INT_REG)) |
Bhupesh Sharma | 881ff67 | 2011-02-13 22:51:44 -0800 | [diff] [blame] | 1057 | return IRQ_NONE; |
| 1058 | |
| 1059 | /* disable all interrupts and schedule the NAPI */ |
Thomas Gleixner | 2d5f4f8 | 2014-04-11 08:13:17 +0000 | [diff] [blame] | 1060 | c_can_irq_control(priv, false); |
Bhupesh Sharma | 881ff67 | 2011-02-13 22:51:44 -0800 | [diff] [blame] | 1061 | napi_schedule(&priv->napi); |
| 1062 | |
| 1063 | return IRQ_HANDLED; |
| 1064 | } |
| 1065 | |
| 1066 | static int c_can_open(struct net_device *dev) |
| 1067 | { |
| 1068 | int err; |
| 1069 | struct c_can_priv *priv = netdev_priv(dev); |
| 1070 | |
AnilKumar Ch | 4cdd34b | 2012-08-20 16:50:54 +0530 | [diff] [blame] | 1071 | c_can_pm_runtime_get_sync(priv); |
AnilKumar Ch | 52cde85 | 2012-11-21 11:14:10 +0530 | [diff] [blame] | 1072 | c_can_reset_ram(priv, true); |
AnilKumar Ch | 4cdd34b | 2012-08-20 16:50:54 +0530 | [diff] [blame] | 1073 | |
Bhupesh Sharma | 881ff67 | 2011-02-13 22:51:44 -0800 | [diff] [blame] | 1074 | /* open the can device */ |
| 1075 | err = open_candev(dev); |
| 1076 | if (err) { |
| 1077 | netdev_err(dev, "failed to open can device\n"); |
AnilKumar Ch | 4cdd34b | 2012-08-20 16:50:54 +0530 | [diff] [blame] | 1078 | goto exit_open_fail; |
Bhupesh Sharma | 881ff67 | 2011-02-13 22:51:44 -0800 | [diff] [blame] | 1079 | } |
| 1080 | |
| 1081 | /* register interrupt handler */ |
| 1082 | err = request_irq(dev->irq, &c_can_isr, IRQF_SHARED, dev->name, |
| 1083 | dev); |
| 1084 | if (err < 0) { |
| 1085 | netdev_err(dev, "failed to request interrupt\n"); |
| 1086 | goto exit_irq_fail; |
| 1087 | } |
| 1088 | |
Marc Kleine-Budde | 130a517 | 2014-03-18 19:06:01 +0100 | [diff] [blame] | 1089 | /* start the c_can controller */ |
| 1090 | err = c_can_start(dev); |
| 1091 | if (err) |
| 1092 | goto exit_start_fail; |
AnilKumar Ch | f461f27 | 2012-05-23 17:45:11 +0530 | [diff] [blame] | 1093 | |
Fabio Baltieri | 5090f80 | 2012-12-18 18:51:01 +0100 | [diff] [blame] | 1094 | can_led_event(dev, CAN_LED_EVENT_OPEN); |
| 1095 | |
Marc Kleine-Budde | 130a517 | 2014-03-18 19:06:01 +0100 | [diff] [blame] | 1096 | napi_enable(&priv->napi); |
Thomas Gleixner | bed11db | 2014-04-11 08:13:10 +0000 | [diff] [blame] | 1097 | /* enable status change, error and module interrupts */ |
Thomas Gleixner | 2d5f4f8 | 2014-04-11 08:13:17 +0000 | [diff] [blame] | 1098 | c_can_irq_control(priv, true); |
Bhupesh Sharma | 881ff67 | 2011-02-13 22:51:44 -0800 | [diff] [blame] | 1099 | netif_start_queue(dev); |
| 1100 | |
| 1101 | return 0; |
| 1102 | |
Marc Kleine-Budde | 130a517 | 2014-03-18 19:06:01 +0100 | [diff] [blame] | 1103 | exit_start_fail: |
| 1104 | free_irq(dev->irq, dev); |
Bhupesh Sharma | 881ff67 | 2011-02-13 22:51:44 -0800 | [diff] [blame] | 1105 | exit_irq_fail: |
| 1106 | close_candev(dev); |
AnilKumar Ch | 4cdd34b | 2012-08-20 16:50:54 +0530 | [diff] [blame] | 1107 | exit_open_fail: |
AnilKumar Ch | 52cde85 | 2012-11-21 11:14:10 +0530 | [diff] [blame] | 1108 | c_can_reset_ram(priv, false); |
AnilKumar Ch | 4cdd34b | 2012-08-20 16:50:54 +0530 | [diff] [blame] | 1109 | c_can_pm_runtime_put_sync(priv); |
Bhupesh Sharma | 881ff67 | 2011-02-13 22:51:44 -0800 | [diff] [blame] | 1110 | return err; |
| 1111 | } |
| 1112 | |
| 1113 | static int c_can_close(struct net_device *dev) |
| 1114 | { |
| 1115 | struct c_can_priv *priv = netdev_priv(dev); |
| 1116 | |
| 1117 | netif_stop_queue(dev); |
| 1118 | napi_disable(&priv->napi); |
| 1119 | c_can_stop(dev); |
| 1120 | free_irq(dev->irq, dev); |
| 1121 | close_candev(dev); |
AnilKumar Ch | 52cde85 | 2012-11-21 11:14:10 +0530 | [diff] [blame] | 1122 | |
| 1123 | c_can_reset_ram(priv, false); |
AnilKumar Ch | 4cdd34b | 2012-08-20 16:50:54 +0530 | [diff] [blame] | 1124 | c_can_pm_runtime_put_sync(priv); |
Bhupesh Sharma | 881ff67 | 2011-02-13 22:51:44 -0800 | [diff] [blame] | 1125 | |
Fabio Baltieri | 5090f80 | 2012-12-18 18:51:01 +0100 | [diff] [blame] | 1126 | can_led_event(dev, CAN_LED_EVENT_STOP); |
| 1127 | |
Bhupesh Sharma | 881ff67 | 2011-02-13 22:51:44 -0800 | [diff] [blame] | 1128 | return 0; |
| 1129 | } |
| 1130 | |
| 1131 | struct net_device *alloc_c_can_dev(void) |
| 1132 | { |
| 1133 | struct net_device *dev; |
| 1134 | struct c_can_priv *priv; |
| 1135 | |
| 1136 | dev = alloc_candev(sizeof(struct c_can_priv), C_CAN_MSG_OBJ_TX_NUM); |
| 1137 | if (!dev) |
| 1138 | return NULL; |
| 1139 | |
| 1140 | priv = netdev_priv(dev); |
| 1141 | netif_napi_add(dev, &priv->napi, c_can_poll, C_CAN_NAPI_WEIGHT); |
| 1142 | |
| 1143 | priv->dev = dev; |
| 1144 | priv->can.bittiming_const = &c_can_bittiming_const; |
| 1145 | priv->can.do_set_mode = c_can_set_mode; |
| 1146 | priv->can.do_get_berr_counter = c_can_get_berr_counter; |
Marc Kleine-Budde | ee6f098 | 2011-03-24 02:34:32 +0000 | [diff] [blame] | 1147 | priv->can.ctrlmode_supported = CAN_CTRLMODE_LOOPBACK | |
Bhupesh Sharma | 881ff67 | 2011-02-13 22:51:44 -0800 | [diff] [blame] | 1148 | CAN_CTRLMODE_LISTENONLY | |
| 1149 | CAN_CTRLMODE_BERR_REPORTING; |
| 1150 | |
| 1151 | return dev; |
| 1152 | } |
| 1153 | EXPORT_SYMBOL_GPL(alloc_c_can_dev); |
| 1154 | |
AnilKumar Ch | 8212003 | 2012-09-21 15:29:01 +0530 | [diff] [blame] | 1155 | #ifdef CONFIG_PM |
| 1156 | int c_can_power_down(struct net_device *dev) |
| 1157 | { |
| 1158 | u32 val; |
| 1159 | unsigned long time_out; |
| 1160 | struct c_can_priv *priv = netdev_priv(dev); |
| 1161 | |
| 1162 | if (!(dev->flags & IFF_UP)) |
| 1163 | return 0; |
| 1164 | |
| 1165 | WARN_ON(priv->type != BOSCH_D_CAN); |
| 1166 | |
| 1167 | /* set PDR value so the device goes to power down mode */ |
| 1168 | val = priv->read_reg(priv, C_CAN_CTRL_EX_REG); |
| 1169 | val |= CONTROL_EX_PDR; |
| 1170 | priv->write_reg(priv, C_CAN_CTRL_EX_REG, val); |
| 1171 | |
| 1172 | /* Wait for the PDA bit to get set */ |
| 1173 | time_out = jiffies + msecs_to_jiffies(INIT_WAIT_MS); |
| 1174 | while (!(priv->read_reg(priv, C_CAN_STS_REG) & STATUS_PDA) && |
| 1175 | time_after(time_out, jiffies)) |
| 1176 | cpu_relax(); |
| 1177 | |
| 1178 | if (time_after(jiffies, time_out)) |
| 1179 | return -ETIMEDOUT; |
| 1180 | |
| 1181 | c_can_stop(dev); |
| 1182 | |
AnilKumar Ch | 52cde85 | 2012-11-21 11:14:10 +0530 | [diff] [blame] | 1183 | c_can_reset_ram(priv, false); |
AnilKumar Ch | 8212003 | 2012-09-21 15:29:01 +0530 | [diff] [blame] | 1184 | c_can_pm_runtime_put_sync(priv); |
| 1185 | |
| 1186 | return 0; |
| 1187 | } |
| 1188 | EXPORT_SYMBOL_GPL(c_can_power_down); |
| 1189 | |
| 1190 | int c_can_power_up(struct net_device *dev) |
| 1191 | { |
| 1192 | u32 val; |
| 1193 | unsigned long time_out; |
| 1194 | struct c_can_priv *priv = netdev_priv(dev); |
Thomas Gleixner | bed11db | 2014-04-11 08:13:10 +0000 | [diff] [blame] | 1195 | int ret; |
AnilKumar Ch | 8212003 | 2012-09-21 15:29:01 +0530 | [diff] [blame] | 1196 | |
| 1197 | if (!(dev->flags & IFF_UP)) |
| 1198 | return 0; |
| 1199 | |
| 1200 | WARN_ON(priv->type != BOSCH_D_CAN); |
| 1201 | |
| 1202 | c_can_pm_runtime_get_sync(priv); |
AnilKumar Ch | 52cde85 | 2012-11-21 11:14:10 +0530 | [diff] [blame] | 1203 | c_can_reset_ram(priv, true); |
AnilKumar Ch | 8212003 | 2012-09-21 15:29:01 +0530 | [diff] [blame] | 1204 | |
| 1205 | /* Clear PDR and INIT bits */ |
| 1206 | val = priv->read_reg(priv, C_CAN_CTRL_EX_REG); |
| 1207 | val &= ~CONTROL_EX_PDR; |
| 1208 | priv->write_reg(priv, C_CAN_CTRL_EX_REG, val); |
| 1209 | val = priv->read_reg(priv, C_CAN_CTRL_REG); |
| 1210 | val &= ~CONTROL_INIT; |
| 1211 | priv->write_reg(priv, C_CAN_CTRL_REG, val); |
| 1212 | |
| 1213 | /* Wait for the PDA bit to get clear */ |
| 1214 | time_out = jiffies + msecs_to_jiffies(INIT_WAIT_MS); |
| 1215 | while ((priv->read_reg(priv, C_CAN_STS_REG) & STATUS_PDA) && |
| 1216 | time_after(time_out, jiffies)) |
| 1217 | cpu_relax(); |
| 1218 | |
| 1219 | if (time_after(jiffies, time_out)) |
| 1220 | return -ETIMEDOUT; |
| 1221 | |
Thomas Gleixner | bed11db | 2014-04-11 08:13:10 +0000 | [diff] [blame] | 1222 | ret = c_can_start(dev); |
| 1223 | if (!ret) |
Thomas Gleixner | 2d5f4f8 | 2014-04-11 08:13:17 +0000 | [diff] [blame] | 1224 | c_can_irq_control(priv, true); |
Thomas Gleixner | bed11db | 2014-04-11 08:13:10 +0000 | [diff] [blame] | 1225 | |
| 1226 | return ret; |
AnilKumar Ch | 8212003 | 2012-09-21 15:29:01 +0530 | [diff] [blame] | 1227 | } |
| 1228 | EXPORT_SYMBOL_GPL(c_can_power_up); |
| 1229 | #endif |
| 1230 | |
Bhupesh Sharma | 881ff67 | 2011-02-13 22:51:44 -0800 | [diff] [blame] | 1231 | void free_c_can_dev(struct net_device *dev) |
| 1232 | { |
Marc Kleine-Budde | f29b423 | 2014-03-18 19:13:59 +0100 | [diff] [blame] | 1233 | struct c_can_priv *priv = netdev_priv(dev); |
| 1234 | |
| 1235 | netif_napi_del(&priv->napi); |
Bhupesh Sharma | 881ff67 | 2011-02-13 22:51:44 -0800 | [diff] [blame] | 1236 | free_candev(dev); |
| 1237 | } |
| 1238 | EXPORT_SYMBOL_GPL(free_c_can_dev); |
| 1239 | |
| 1240 | static const struct net_device_ops c_can_netdev_ops = { |
| 1241 | .ndo_open = c_can_open, |
| 1242 | .ndo_stop = c_can_close, |
| 1243 | .ndo_start_xmit = c_can_start_xmit, |
Oliver Hartkopp | c971fa2 | 2014-03-07 09:23:41 +0100 | [diff] [blame] | 1244 | .ndo_change_mtu = can_change_mtu, |
Bhupesh Sharma | 881ff67 | 2011-02-13 22:51:44 -0800 | [diff] [blame] | 1245 | }; |
| 1246 | |
| 1247 | int register_c_can_dev(struct net_device *dev) |
| 1248 | { |
AnilKumar Ch | 4cdd34b | 2012-08-20 16:50:54 +0530 | [diff] [blame] | 1249 | struct c_can_priv *priv = netdev_priv(dev); |
| 1250 | int err; |
| 1251 | |
| 1252 | c_can_pm_runtime_enable(priv); |
| 1253 | |
Bhupesh Sharma | 881ff67 | 2011-02-13 22:51:44 -0800 | [diff] [blame] | 1254 | dev->flags |= IFF_ECHO; /* we support local echo */ |
| 1255 | dev->netdev_ops = &c_can_netdev_ops; |
| 1256 | |
AnilKumar Ch | 4cdd34b | 2012-08-20 16:50:54 +0530 | [diff] [blame] | 1257 | err = register_candev(dev); |
| 1258 | if (err) |
| 1259 | c_can_pm_runtime_disable(priv); |
Fabio Baltieri | 5090f80 | 2012-12-18 18:51:01 +0100 | [diff] [blame] | 1260 | else |
| 1261 | devm_can_led_init(dev); |
AnilKumar Ch | 4cdd34b | 2012-08-20 16:50:54 +0530 | [diff] [blame] | 1262 | |
| 1263 | return err; |
Bhupesh Sharma | 881ff67 | 2011-02-13 22:51:44 -0800 | [diff] [blame] | 1264 | } |
| 1265 | EXPORT_SYMBOL_GPL(register_c_can_dev); |
| 1266 | |
| 1267 | void unregister_c_can_dev(struct net_device *dev) |
| 1268 | { |
| 1269 | struct c_can_priv *priv = netdev_priv(dev); |
| 1270 | |
Bhupesh Sharma | 881ff67 | 2011-02-13 22:51:44 -0800 | [diff] [blame] | 1271 | unregister_candev(dev); |
AnilKumar Ch | 4cdd34b | 2012-08-20 16:50:54 +0530 | [diff] [blame] | 1272 | |
| 1273 | c_can_pm_runtime_disable(priv); |
Bhupesh Sharma | 881ff67 | 2011-02-13 22:51:44 -0800 | [diff] [blame] | 1274 | } |
| 1275 | EXPORT_SYMBOL_GPL(unregister_c_can_dev); |
| 1276 | |
| 1277 | MODULE_AUTHOR("Bhupesh Sharma <bhupesh.sharma@st.com>"); |
| 1278 | MODULE_LICENSE("GPL v2"); |
| 1279 | MODULE_DESCRIPTION("CAN bus driver for Bosch C_CAN controller"); |