1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Core driver for the CC770 and AN82527 CAN controllers 4 * 5 * Copyright (C) 2009, 2011 Wolfgang Grandegger <wg@grandegger.com> 6 */ 7 8 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 9 10 #include <linux/module.h> 11 #include <linux/init.h> 12 #include <linux/kernel.h> 13 #include <linux/sched.h> 14 #include <linux/types.h> 15 #include <linux/fcntl.h> 16 #include <linux/interrupt.h> 17 #include <linux/ptrace.h> 18 #include <linux/string.h> 19 #include <linux/errno.h> 20 #include <linux/netdevice.h> 21 #include <linux/if_arp.h> 22 #include <linux/if_ether.h> 23 #include <linux/skbuff.h> 24 #include <linux/delay.h> 25 26 #include <linux/can.h> 27 #include <linux/can/dev.h> 28 #include <linux/can/error.h> 29 #include <linux/can/platform/cc770.h> 30 31 #include "cc770.h" 32 33 MODULE_AUTHOR("Wolfgang Grandegger <wg@grandegger.com>"); 34 MODULE_LICENSE("GPL v2"); 35 MODULE_DESCRIPTION(KBUILD_MODNAME "CAN netdevice driver"); 36 37 /* 38 * The CC770 is a CAN controller from Bosch, which is 100% compatible 39 * with the AN82527 from Intel, but with "bugs" being fixed and some 40 * additional functionality, mainly: 41 * 42 * 1. RX and TX error counters are readable. 43 * 2. Support of silent (listen-only) mode. 44 * 3. Message object 15 can receive all types of frames, also RTR and EFF. 45 * 46 * Details are available from Bosch's "CC770_Product_Info_2007-01.pdf", 47 * which explains in detail the compatibility between the CC770 and the 48 * 82527. This driver use the additional functionality 3. on real CC770 49 * devices. Unfortunately, the CC770 does still not store the message 50 * identifier of received remote transmission request frames and 51 * therefore it's set to 0. 52 * 53 * The message objects 1..14 can be used for TX and RX while the message 54 * objects 15 is optimized for RX. It has a shadow register for reliable 55 * data reception under heavy bus load. Therefore it makes sense to use 56 * this message object for the needed use case. The frame type (EFF/SFF) 57 * for the message object 15 can be defined via kernel module parameter 58 * "msgobj15_eff". If not equal 0, it will receive 29-bit EFF frames, 59 * otherwise 11 bit SFF messages. 60 */ 61 static int msgobj15_eff; 62 module_param(msgobj15_eff, int, 0444); 63 MODULE_PARM_DESC(msgobj15_eff, "Extended 29-bit frames for message object 15 " 64 "(default: 11-bit standard frames)"); 65 66 static int i82527_compat; 67 module_param(i82527_compat, int, 0444); 68 MODULE_PARM_DESC(i82527_compat, "Strict Intel 82527 compatibility mode " 69 "without using additional functions"); 70 71 /* 72 * This driver uses the last 5 message objects 11..15. The definitions 73 * and structure below allows to configure and assign them to the real 74 * message object. 75 */ 76 static unsigned char cc770_obj_flags[CC770_OBJ_MAX] = { 77 [CC770_OBJ_RX0] = CC770_OBJ_FLAG_RX, 78 [CC770_OBJ_RX1] = CC770_OBJ_FLAG_RX | CC770_OBJ_FLAG_EFF, 79 [CC770_OBJ_RX_RTR0] = CC770_OBJ_FLAG_RX | CC770_OBJ_FLAG_RTR, 80 [CC770_OBJ_RX_RTR1] = CC770_OBJ_FLAG_RX | CC770_OBJ_FLAG_RTR | 81 CC770_OBJ_FLAG_EFF, 82 [CC770_OBJ_TX] = 0, 83 }; 84 85 static const struct can_bittiming_const cc770_bittiming_const = { 86 .name = KBUILD_MODNAME, 87 .tseg1_min = 1, 88 .tseg1_max = 16, 89 .tseg2_min = 1, 90 .tseg2_max = 8, 91 .sjw_max = 4, 92 .brp_min = 1, 93 .brp_max = 64, 94 .brp_inc = 1, 95 }; 96 97 static inline int intid2obj(unsigned int intid) 98 { 99 if (intid == 2) 100 return 0; 101 else 102 return MSGOBJ_LAST + 2 - intid; 103 } 104 105 static void enable_all_objs(const struct net_device *dev) 106 { 107 struct cc770_priv *priv = netdev_priv(dev); 108 u8 msgcfg; 109 unsigned char obj_flags; 110 unsigned int o, mo; 111 112 for (o = 0; o < ARRAY_SIZE(priv->obj_flags); o++) { 113 obj_flags = priv->obj_flags[o]; 114 mo = obj2msgobj(o); 115 116 if (obj_flags & CC770_OBJ_FLAG_RX) { 117 /* 118 * We don't need extra objects for RTR and EFF if 119 * the additional CC770 functions are enabled. 120 */ 121 if (priv->control_normal_mode & CTRL_EAF) { 122 if (o > 0) 123 continue; 124 netdev_dbg(dev, "Message object %d for " 125 "RX data, RTR, SFF and EFF\n", mo); 126 } else { 127 netdev_dbg(dev, 128 "Message object %d for RX %s %s\n", 129 mo, obj_flags & CC770_OBJ_FLAG_RTR ? 130 "RTR" : "data", 131 obj_flags & CC770_OBJ_FLAG_EFF ? 132 "EFF" : "SFF"); 133 } 134 135 if (obj_flags & CC770_OBJ_FLAG_EFF) 136 msgcfg = MSGCFG_XTD; 137 else 138 msgcfg = 0; 139 if (obj_flags & CC770_OBJ_FLAG_RTR) 140 msgcfg |= MSGCFG_DIR; 141 142 cc770_write_reg(priv, msgobj[mo].config, msgcfg); 143 cc770_write_reg(priv, msgobj[mo].ctrl0, 144 MSGVAL_SET | TXIE_RES | 145 RXIE_SET | INTPND_RES); 146 147 if (obj_flags & CC770_OBJ_FLAG_RTR) 148 cc770_write_reg(priv, msgobj[mo].ctrl1, 149 NEWDAT_RES | CPUUPD_SET | 150 TXRQST_RES | RMTPND_RES); 151 else 152 cc770_write_reg(priv, msgobj[mo].ctrl1, 153 NEWDAT_RES | MSGLST_RES | 154 TXRQST_RES | RMTPND_RES); 155 } else { 156 netdev_dbg(dev, "Message object %d for " 157 "TX data, RTR, SFF and EFF\n", mo); 158 159 cc770_write_reg(priv, msgobj[mo].ctrl1, 160 RMTPND_RES | TXRQST_RES | 161 CPUUPD_RES | NEWDAT_RES); 162 cc770_write_reg(priv, msgobj[mo].ctrl0, 163 MSGVAL_RES | TXIE_RES | 164 RXIE_RES | INTPND_RES); 165 } 166 } 167 } 168 169 static void disable_all_objs(const struct cc770_priv *priv) 170 { 171 int o, mo; 172 173 for (o = 0; o < ARRAY_SIZE(priv->obj_flags); o++) { 174 mo = obj2msgobj(o); 175 176 if (priv->obj_flags[o] & CC770_OBJ_FLAG_RX) { 177 if (o > 0 && priv->control_normal_mode & CTRL_EAF) 178 continue; 179 180 cc770_write_reg(priv, msgobj[mo].ctrl1, 181 NEWDAT_RES | MSGLST_RES | 182 TXRQST_RES | RMTPND_RES); 183 cc770_write_reg(priv, msgobj[mo].ctrl0, 184 MSGVAL_RES | TXIE_RES | 185 RXIE_RES | INTPND_RES); 186 } else { 187 /* Clear message object for send */ 188 cc770_write_reg(priv, msgobj[mo].ctrl1, 189 RMTPND_RES | TXRQST_RES | 190 CPUUPD_RES | NEWDAT_RES); 191 cc770_write_reg(priv, msgobj[mo].ctrl0, 192 MSGVAL_RES | TXIE_RES | 193 RXIE_RES | INTPND_RES); 194 } 195 } 196 } 197 198 static void set_reset_mode(struct net_device *dev) 199 { 200 struct cc770_priv *priv = netdev_priv(dev); 201 202 /* Enable configuration and puts chip in bus-off, disable interrupts */ 203 cc770_write_reg(priv, control, CTRL_CCE | CTRL_INI); 204 205 priv->can.state = CAN_STATE_STOPPED; 206 207 /* Clear interrupts */ 208 cc770_read_reg(priv, interrupt); 209 210 /* Clear status register */ 211 cc770_write_reg(priv, status, 0); 212 213 /* Disable all used message objects */ 214 disable_all_objs(priv); 215 } 216 217 static void set_normal_mode(struct net_device *dev) 218 { 219 struct cc770_priv *priv = netdev_priv(dev); 220 221 /* Clear interrupts */ 222 cc770_read_reg(priv, interrupt); 223 224 /* Clear status register and pre-set last error code */ 225 cc770_write_reg(priv, status, STAT_LEC_MASK); 226 227 /* Enable all used message objects*/ 228 enable_all_objs(dev); 229 230 /* 231 * Clear bus-off, interrupts only for errors, 232 * not for status change 233 */ 234 cc770_write_reg(priv, control, priv->control_normal_mode); 235 236 priv->can.state = CAN_STATE_ERROR_ACTIVE; 237 } 238 239 static void chipset_init(struct cc770_priv *priv) 240 { 241 int mo, id, data; 242 243 /* Enable configuration and put chip in bus-off, disable interrupts */ 244 cc770_write_reg(priv, control, (CTRL_CCE | CTRL_INI)); 245 246 /* Set CLKOUT divider and slew rates */ 247 cc770_write_reg(priv, clkout, priv->clkout); 248 249 /* Configure CPU interface / CLKOUT enable */ 250 cc770_write_reg(priv, cpu_interface, priv->cpu_interface); 251 252 /* Set bus configuration */ 253 cc770_write_reg(priv, bus_config, priv->bus_config); 254 255 /* Clear interrupts */ 256 cc770_read_reg(priv, interrupt); 257 258 /* Clear status register */ 259 cc770_write_reg(priv, status, 0); 260 261 /* Clear and invalidate message objects */ 262 for (mo = MSGOBJ_FIRST; mo <= MSGOBJ_LAST; mo++) { 263 cc770_write_reg(priv, msgobj[mo].ctrl0, 264 INTPND_UNC | RXIE_RES | 265 TXIE_RES | MSGVAL_RES); 266 cc770_write_reg(priv, msgobj[mo].ctrl0, 267 INTPND_RES | RXIE_RES | 268 TXIE_RES | MSGVAL_RES); 269 cc770_write_reg(priv, msgobj[mo].ctrl1, 270 NEWDAT_RES | MSGLST_RES | 271 TXRQST_RES | RMTPND_RES); 272 for (data = 0; data < 8; data++) 273 cc770_write_reg(priv, msgobj[mo].data[data], 0); 274 for (id = 0; id < 4; id++) 275 cc770_write_reg(priv, msgobj[mo].id[id], 0); 276 cc770_write_reg(priv, msgobj[mo].config, 0); 277 } 278 279 /* Set all global ID masks to "don't care" */ 280 cc770_write_reg(priv, global_mask_std[0], 0); 281 cc770_write_reg(priv, global_mask_std[1], 0); 282 cc770_write_reg(priv, global_mask_ext[0], 0); 283 cc770_write_reg(priv, global_mask_ext[1], 0); 284 cc770_write_reg(priv, global_mask_ext[2], 0); 285 cc770_write_reg(priv, global_mask_ext[3], 0); 286 287 } 288 289 static int cc770_probe_chip(struct net_device *dev) 290 { 291 struct cc770_priv *priv = netdev_priv(dev); 292 293 /* Enable configuration, put chip in bus-off, disable ints */ 294 cc770_write_reg(priv, control, CTRL_CCE | CTRL_EAF | CTRL_INI); 295 /* Configure cpu interface / CLKOUT disable */ 296 cc770_write_reg(priv, cpu_interface, priv->cpu_interface); 297 298 /* 299 * Check if hardware reset is still inactive or maybe there 300 * is no chip in this address space 301 */ 302 if (cc770_read_reg(priv, cpu_interface) & CPUIF_RST) { 303 netdev_info(dev, "probing @0x%p failed (reset)\n", 304 priv->reg_base); 305 return -ENODEV; 306 } 307 308 /* Write and read back test pattern (some arbitrary values) */ 309 cc770_write_reg(priv, msgobj[1].data[1], 0x25); 310 cc770_write_reg(priv, msgobj[2].data[3], 0x52); 311 cc770_write_reg(priv, msgobj[10].data[6], 0xc3); 312 if ((cc770_read_reg(priv, msgobj[1].data[1]) != 0x25) || 313 (cc770_read_reg(priv, msgobj[2].data[3]) != 0x52) || 314 (cc770_read_reg(priv, msgobj[10].data[6]) != 0xc3)) { 315 netdev_info(dev, "probing @0x%p failed (pattern)\n", 316 priv->reg_base); 317 return -ENODEV; 318 } 319 320 /* Check if this chip is a CC770 supporting additional functions */ 321 if (cc770_read_reg(priv, control) & CTRL_EAF) 322 priv->control_normal_mode |= CTRL_EAF; 323 324 return 0; 325 } 326 327 static void cc770_start(struct net_device *dev) 328 { 329 struct cc770_priv *priv = netdev_priv(dev); 330 331 /* leave reset mode */ 332 if (priv->can.state != CAN_STATE_STOPPED) 333 set_reset_mode(dev); 334 335 /* leave reset mode */ 336 set_normal_mode(dev); 337 } 338 339 static int cc770_set_mode(struct net_device *dev, enum can_mode mode) 340 { 341 switch (mode) { 342 case CAN_MODE_START: 343 cc770_start(dev); 344 netif_wake_queue(dev); 345 break; 346 347 default: 348 return -EOPNOTSUPP; 349 } 350 351 return 0; 352 } 353 354 static int cc770_set_bittiming(struct net_device *dev) 355 { 356 struct cc770_priv *priv = netdev_priv(dev); 357 struct can_bittiming *bt = &priv->can.bittiming; 358 u8 btr0, btr1; 359 360 btr0 = ((bt->brp - 1) & 0x3f) | (((bt->sjw - 1) & 0x3) << 6); 361 btr1 = ((bt->prop_seg + bt->phase_seg1 - 1) & 0xf) | 362 (((bt->phase_seg2 - 1) & 0x7) << 4); 363 if (priv->can.ctrlmode & CAN_CTRLMODE_3_SAMPLES) 364 btr1 |= 0x80; 365 366 netdev_info(dev, "setting BTR0=0x%02x BTR1=0x%02x\n", btr0, btr1); 367 368 cc770_write_reg(priv, bit_timing_0, btr0); 369 cc770_write_reg(priv, bit_timing_1, btr1); 370 371 return 0; 372 } 373 374 static int cc770_get_berr_counter(const struct net_device *dev, 375 struct can_berr_counter *bec) 376 { 377 struct cc770_priv *priv = netdev_priv(dev); 378 379 bec->txerr = cc770_read_reg(priv, tx_error_counter); 380 bec->rxerr = cc770_read_reg(priv, rx_error_counter); 381 382 return 0; 383 } 384 385 static void cc770_tx(struct net_device *dev, int mo) 386 { 387 struct cc770_priv *priv = netdev_priv(dev); 388 struct can_frame *cf = (struct can_frame *)priv->tx_skb->data; 389 u8 dlc, rtr; 390 u32 id; 391 int i; 392 393 dlc = cf->len; 394 id = cf->can_id; 395 rtr = cf->can_id & CAN_RTR_FLAG ? 0 : MSGCFG_DIR; 396 397 cc770_write_reg(priv, msgobj[mo].ctrl0, 398 MSGVAL_RES | TXIE_RES | RXIE_RES | INTPND_RES); 399 cc770_write_reg(priv, msgobj[mo].ctrl1, 400 RMTPND_RES | TXRQST_RES | CPUUPD_SET | NEWDAT_RES); 401 402 if (id & CAN_EFF_FLAG) { 403 id &= CAN_EFF_MASK; 404 cc770_write_reg(priv, msgobj[mo].config, 405 (dlc << 4) | rtr | MSGCFG_XTD); 406 cc770_write_reg(priv, msgobj[mo].id[3], id << 3); 407 cc770_write_reg(priv, msgobj[mo].id[2], id >> 5); 408 cc770_write_reg(priv, msgobj[mo].id[1], id >> 13); 409 cc770_write_reg(priv, msgobj[mo].id[0], id >> 21); 410 } else { 411 id &= CAN_SFF_MASK; 412 cc770_write_reg(priv, msgobj[mo].config, (dlc << 4) | rtr); 413 cc770_write_reg(priv, msgobj[mo].id[0], id >> 3); 414 cc770_write_reg(priv, msgobj[mo].id[1], id << 5); 415 } 416 417 for (i = 0; i < dlc; i++) 418 cc770_write_reg(priv, msgobj[mo].data[i], cf->data[i]); 419 420 cc770_write_reg(priv, msgobj[mo].ctrl1, 421 RMTPND_UNC | TXRQST_SET | CPUUPD_RES | NEWDAT_UNC); 422 cc770_write_reg(priv, msgobj[mo].ctrl0, 423 MSGVAL_SET | TXIE_SET | RXIE_SET | INTPND_UNC); 424 } 425 426 static netdev_tx_t cc770_start_xmit(struct sk_buff *skb, struct net_device *dev) 427 { 428 struct cc770_priv *priv = netdev_priv(dev); 429 unsigned int mo = obj2msgobj(CC770_OBJ_TX); 430 431 if (can_dropped_invalid_skb(dev, skb)) 432 return NETDEV_TX_OK; 433 434 netif_stop_queue(dev); 435 436 if ((cc770_read_reg(priv, 437 msgobj[mo].ctrl1) & TXRQST_UNC) == TXRQST_SET) { 438 netdev_err(dev, "TX register is still occupied!\n"); 439 return NETDEV_TX_BUSY; 440 } 441 442 priv->tx_skb = skb; 443 cc770_tx(dev, mo); 444 445 return NETDEV_TX_OK; 446 } 447 448 static void cc770_rx(struct net_device *dev, unsigned int mo, u8 ctrl1) 449 { 450 struct cc770_priv *priv = netdev_priv(dev); 451 struct net_device_stats *stats = &dev->stats; 452 struct can_frame *cf; 453 struct sk_buff *skb; 454 u8 config; 455 u32 id; 456 int i; 457 458 skb = alloc_can_skb(dev, &cf); 459 if (!skb) 460 return; 461 462 config = cc770_read_reg(priv, msgobj[mo].config); 463 464 if (ctrl1 & RMTPND_SET) { 465 /* 466 * Unfortunately, the chip does not store the real message 467 * identifier of the received remote transmission request 468 * frame. Therefore we set it to 0. 469 */ 470 cf->can_id = CAN_RTR_FLAG; 471 if (config & MSGCFG_XTD) 472 cf->can_id |= CAN_EFF_FLAG; 473 cf->len = 0; 474 } else { 475 if (config & MSGCFG_XTD) { 476 id = cc770_read_reg(priv, msgobj[mo].id[3]); 477 id |= cc770_read_reg(priv, msgobj[mo].id[2]) << 8; 478 id |= cc770_read_reg(priv, msgobj[mo].id[1]) << 16; 479 id |= cc770_read_reg(priv, msgobj[mo].id[0]) << 24; 480 id >>= 3; 481 id |= CAN_EFF_FLAG; 482 } else { 483 id = cc770_read_reg(priv, msgobj[mo].id[1]); 484 id |= cc770_read_reg(priv, msgobj[mo].id[0]) << 8; 485 id >>= 5; 486 } 487 488 cf->can_id = id; 489 cf->len = can_cc_dlc2len((config & 0xf0) >> 4); 490 for (i = 0; i < cf->len; i++) 491 cf->data[i] = cc770_read_reg(priv, msgobj[mo].data[i]); 492 493 stats->rx_bytes += cf->len; 494 } 495 stats->rx_packets++; 496 497 netif_rx(skb); 498 } 499 500 static int cc770_err(struct net_device *dev, u8 status) 501 { 502 struct cc770_priv *priv = netdev_priv(dev); 503 struct can_frame *cf; 504 struct sk_buff *skb; 505 u8 lec; 506 507 netdev_dbg(dev, "status interrupt (%#x)\n", status); 508 509 skb = alloc_can_err_skb(dev, &cf); 510 if (!skb) 511 return -ENOMEM; 512 513 /* Use extended functions of the CC770 */ 514 if (priv->control_normal_mode & CTRL_EAF) { 515 cf->can_id |= CAN_ERR_CNT; 516 cf->data[6] = cc770_read_reg(priv, tx_error_counter); 517 cf->data[7] = cc770_read_reg(priv, rx_error_counter); 518 } 519 520 if (status & STAT_BOFF) { 521 /* Disable interrupts */ 522 cc770_write_reg(priv, control, CTRL_INI); 523 cf->can_id |= CAN_ERR_BUSOFF; 524 priv->can.state = CAN_STATE_BUS_OFF; 525 priv->can.can_stats.bus_off++; 526 can_bus_off(dev); 527 } else if (status & STAT_WARN) { 528 cf->can_id |= CAN_ERR_CRTL; 529 /* Only the CC770 does show error passive */ 530 if (cf->data[7] > 127) { 531 cf->data[1] = CAN_ERR_CRTL_RX_PASSIVE | 532 CAN_ERR_CRTL_TX_PASSIVE; 533 priv->can.state = CAN_STATE_ERROR_PASSIVE; 534 priv->can.can_stats.error_passive++; 535 } else { 536 cf->data[1] = CAN_ERR_CRTL_RX_WARNING | 537 CAN_ERR_CRTL_TX_WARNING; 538 priv->can.state = CAN_STATE_ERROR_WARNING; 539 priv->can.can_stats.error_warning++; 540 } 541 } else { 542 /* Back to error active */ 543 cf->can_id |= CAN_ERR_PROT; 544 cf->data[2] = CAN_ERR_PROT_ACTIVE; 545 priv->can.state = CAN_STATE_ERROR_ACTIVE; 546 } 547 548 lec = status & STAT_LEC_MASK; 549 if (lec < 7 && lec > 0) { 550 if (lec == STAT_LEC_ACK) { 551 cf->can_id |= CAN_ERR_ACK; 552 } else { 553 cf->can_id |= CAN_ERR_PROT; 554 switch (lec) { 555 case STAT_LEC_STUFF: 556 cf->data[2] |= CAN_ERR_PROT_STUFF; 557 break; 558 case STAT_LEC_FORM: 559 cf->data[2] |= CAN_ERR_PROT_FORM; 560 break; 561 case STAT_LEC_BIT1: 562 cf->data[2] |= CAN_ERR_PROT_BIT1; 563 break; 564 case STAT_LEC_BIT0: 565 cf->data[2] |= CAN_ERR_PROT_BIT0; 566 break; 567 case STAT_LEC_CRC: 568 cf->data[3] = CAN_ERR_PROT_LOC_CRC_SEQ; 569 break; 570 } 571 } 572 } 573 574 575 netif_rx(skb); 576 577 return 0; 578 } 579 580 static int cc770_status_interrupt(struct net_device *dev) 581 { 582 struct cc770_priv *priv = netdev_priv(dev); 583 u8 status; 584 585 status = cc770_read_reg(priv, status); 586 /* Reset the status register including RXOK and TXOK */ 587 cc770_write_reg(priv, status, STAT_LEC_MASK); 588 589 if (status & (STAT_WARN | STAT_BOFF) || 590 (status & STAT_LEC_MASK) != STAT_LEC_MASK) { 591 cc770_err(dev, status); 592 return status & STAT_BOFF; 593 } 594 595 return 0; 596 } 597 598 static void cc770_rx_interrupt(struct net_device *dev, unsigned int o) 599 { 600 struct cc770_priv *priv = netdev_priv(dev); 601 struct net_device_stats *stats = &dev->stats; 602 unsigned int mo = obj2msgobj(o); 603 u8 ctrl1; 604 int n = CC770_MAX_MSG; 605 606 while (n--) { 607 ctrl1 = cc770_read_reg(priv, msgobj[mo].ctrl1); 608 609 if (!(ctrl1 & NEWDAT_SET)) { 610 /* Check for RTR if additional functions are enabled */ 611 if (priv->control_normal_mode & CTRL_EAF) { 612 if (!(cc770_read_reg(priv, msgobj[mo].ctrl0) & 613 INTPND_SET)) 614 break; 615 } else { 616 break; 617 } 618 } 619 620 if (ctrl1 & MSGLST_SET) { 621 stats->rx_over_errors++; 622 stats->rx_errors++; 623 } 624 if (mo < MSGOBJ_LAST) 625 cc770_write_reg(priv, msgobj[mo].ctrl1, 626 NEWDAT_RES | MSGLST_RES | 627 TXRQST_UNC | RMTPND_UNC); 628 cc770_rx(dev, mo, ctrl1); 629 630 cc770_write_reg(priv, msgobj[mo].ctrl0, 631 MSGVAL_SET | TXIE_RES | 632 RXIE_SET | INTPND_RES); 633 cc770_write_reg(priv, msgobj[mo].ctrl1, 634 NEWDAT_RES | MSGLST_RES | 635 TXRQST_RES | RMTPND_RES); 636 } 637 } 638 639 static void cc770_rtr_interrupt(struct net_device *dev, unsigned int o) 640 { 641 struct cc770_priv *priv = netdev_priv(dev); 642 unsigned int mo = obj2msgobj(o); 643 u8 ctrl0, ctrl1; 644 int n = CC770_MAX_MSG; 645 646 while (n--) { 647 ctrl0 = cc770_read_reg(priv, msgobj[mo].ctrl0); 648 if (!(ctrl0 & INTPND_SET)) 649 break; 650 651 ctrl1 = cc770_read_reg(priv, msgobj[mo].ctrl1); 652 cc770_rx(dev, mo, ctrl1); 653 654 cc770_write_reg(priv, msgobj[mo].ctrl0, 655 MSGVAL_SET | TXIE_RES | 656 RXIE_SET | INTPND_RES); 657 cc770_write_reg(priv, msgobj[mo].ctrl1, 658 NEWDAT_RES | CPUUPD_SET | 659 TXRQST_RES | RMTPND_RES); 660 } 661 } 662 663 static void cc770_tx_interrupt(struct net_device *dev, unsigned int o) 664 { 665 struct cc770_priv *priv = netdev_priv(dev); 666 struct net_device_stats *stats = &dev->stats; 667 unsigned int mo = obj2msgobj(o); 668 u8 ctrl1; 669 670 ctrl1 = cc770_read_reg(priv, msgobj[mo].ctrl1); 671 672 cc770_write_reg(priv, msgobj[mo].ctrl0, 673 MSGVAL_RES | TXIE_RES | RXIE_RES | INTPND_RES); 674 cc770_write_reg(priv, msgobj[mo].ctrl1, 675 RMTPND_RES | TXRQST_RES | MSGLST_RES | NEWDAT_RES); 676 677 if (unlikely(!priv->tx_skb)) { 678 netdev_err(dev, "missing tx skb in tx interrupt\n"); 679 return; 680 } 681 682 if (unlikely(ctrl1 & MSGLST_SET)) { 683 stats->rx_over_errors++; 684 stats->rx_errors++; 685 } 686 687 /* When the CC770 is sending an RTR message and it receives a regular 688 * message that matches the id of the RTR message, it will overwrite the 689 * outgoing message in the TX register. When this happens we must 690 * process the received message and try to transmit the outgoing skb 691 * again. 692 */ 693 if (unlikely(ctrl1 & NEWDAT_SET)) { 694 cc770_rx(dev, mo, ctrl1); 695 cc770_tx(dev, mo); 696 return; 697 } 698 699 can_put_echo_skb(priv->tx_skb, dev, 0, 0); 700 stats->tx_bytes += can_get_echo_skb(dev, 0, NULL); 701 stats->tx_packets++; 702 priv->tx_skb = NULL; 703 704 netif_wake_queue(dev); 705 } 706 707 static irqreturn_t cc770_interrupt(int irq, void *dev_id) 708 { 709 struct net_device *dev = (struct net_device *)dev_id; 710 struct cc770_priv *priv = netdev_priv(dev); 711 u8 intid; 712 int o, n = 0; 713 714 /* Shared interrupts and IRQ off? */ 715 if (priv->can.state == CAN_STATE_STOPPED) 716 return IRQ_NONE; 717 718 if (priv->pre_irq) 719 priv->pre_irq(priv); 720 721 while (n < CC770_MAX_IRQ) { 722 /* Read the highest pending interrupt request */ 723 intid = cc770_read_reg(priv, interrupt); 724 if (!intid) 725 break; 726 n++; 727 728 if (intid == 1) { 729 /* Exit in case of bus-off */ 730 if (cc770_status_interrupt(dev)) 731 break; 732 } else { 733 o = intid2obj(intid); 734 735 if (o >= CC770_OBJ_MAX) { 736 netdev_err(dev, "Unexpected interrupt id %d\n", 737 intid); 738 continue; 739 } 740 741 if (priv->obj_flags[o] & CC770_OBJ_FLAG_RTR) 742 cc770_rtr_interrupt(dev, o); 743 else if (priv->obj_flags[o] & CC770_OBJ_FLAG_RX) 744 cc770_rx_interrupt(dev, o); 745 else 746 cc770_tx_interrupt(dev, o); 747 } 748 } 749 750 if (priv->post_irq) 751 priv->post_irq(priv); 752 753 if (n >= CC770_MAX_IRQ) 754 netdev_dbg(dev, "%d messages handled in ISR", n); 755 756 return (n) ? IRQ_HANDLED : IRQ_NONE; 757 } 758 759 static int cc770_open(struct net_device *dev) 760 { 761 struct cc770_priv *priv = netdev_priv(dev); 762 int err; 763 764 /* set chip into reset mode */ 765 set_reset_mode(dev); 766 767 /* common open */ 768 err = open_candev(dev); 769 if (err) 770 return err; 771 772 err = request_irq(dev->irq, &cc770_interrupt, priv->irq_flags, 773 dev->name, dev); 774 if (err) { 775 close_candev(dev); 776 return -EAGAIN; 777 } 778 779 /* init and start chip */ 780 cc770_start(dev); 781 782 netif_start_queue(dev); 783 784 return 0; 785 } 786 787 static int cc770_close(struct net_device *dev) 788 { 789 netif_stop_queue(dev); 790 set_reset_mode(dev); 791 792 free_irq(dev->irq, dev); 793 close_candev(dev); 794 795 return 0; 796 } 797 798 struct net_device *alloc_cc770dev(int sizeof_priv) 799 { 800 struct net_device *dev; 801 struct cc770_priv *priv; 802 803 dev = alloc_candev(sizeof(struct cc770_priv) + sizeof_priv, 804 CC770_ECHO_SKB_MAX); 805 if (!dev) 806 return NULL; 807 808 priv = netdev_priv(dev); 809 810 priv->dev = dev; 811 priv->can.bittiming_const = &cc770_bittiming_const; 812 priv->can.do_set_bittiming = cc770_set_bittiming; 813 priv->can.do_set_mode = cc770_set_mode; 814 priv->can.ctrlmode_supported = CAN_CTRLMODE_3_SAMPLES; 815 priv->tx_skb = NULL; 816 817 memcpy(priv->obj_flags, cc770_obj_flags, sizeof(cc770_obj_flags)); 818 819 if (sizeof_priv) 820 priv->priv = (void *)priv + sizeof(struct cc770_priv); 821 822 return dev; 823 } 824 EXPORT_SYMBOL_GPL(alloc_cc770dev); 825 826 void free_cc770dev(struct net_device *dev) 827 { 828 free_candev(dev); 829 } 830 EXPORT_SYMBOL_GPL(free_cc770dev); 831 832 static const struct net_device_ops cc770_netdev_ops = { 833 .ndo_open = cc770_open, 834 .ndo_stop = cc770_close, 835 .ndo_start_xmit = cc770_start_xmit, 836 .ndo_change_mtu = can_change_mtu, 837 }; 838 839 int register_cc770dev(struct net_device *dev) 840 { 841 struct cc770_priv *priv = netdev_priv(dev); 842 int err; 843 844 err = cc770_probe_chip(dev); 845 if (err) 846 return err; 847 848 dev->netdev_ops = &cc770_netdev_ops; 849 850 dev->flags |= IFF_ECHO; /* we support local echo */ 851 852 /* Should we use additional functions? */ 853 if (!i82527_compat && priv->control_normal_mode & CTRL_EAF) { 854 priv->can.do_get_berr_counter = cc770_get_berr_counter; 855 priv->control_normal_mode = CTRL_IE | CTRL_EAF | CTRL_EIE; 856 netdev_dbg(dev, "i82527 mode with additional functions\n"); 857 } else { 858 priv->control_normal_mode = CTRL_IE | CTRL_EIE; 859 netdev_dbg(dev, "strict i82527 compatibility mode\n"); 860 } 861 862 chipset_init(priv); 863 set_reset_mode(dev); 864 865 return register_candev(dev); 866 } 867 EXPORT_SYMBOL_GPL(register_cc770dev); 868 869 void unregister_cc770dev(struct net_device *dev) 870 { 871 set_reset_mode(dev); 872 unregister_candev(dev); 873 } 874 EXPORT_SYMBOL_GPL(unregister_cc770dev); 875 876 static __init int cc770_init(void) 877 { 878 if (msgobj15_eff) { 879 cc770_obj_flags[CC770_OBJ_RX0] |= CC770_OBJ_FLAG_EFF; 880 cc770_obj_flags[CC770_OBJ_RX1] &= ~CC770_OBJ_FLAG_EFF; 881 } 882 883 pr_info("CAN netdevice driver\n"); 884 885 return 0; 886 } 887 module_init(cc770_init); 888 889 static __exit void cc770_exit(void) 890 { 891 pr_info("driver removed\n"); 892 } 893 module_exit(cc770_exit); 894