1 /* 2 * sja1000.c - Philips SJA1000 network device driver 3 * 4 * Copyright (c) 2003 Matthias Brukner, Trajet Gmbh, Rebenring 33, 5 * 38106 Braunschweig, GERMANY 6 * 7 * Copyright (c) 2002-2007 Volkswagen Group Electronic Research 8 * All rights reserved. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 3. Neither the name of Volkswagen nor the names of its contributors 19 * may be used to endorse or promote products derived from this software 20 * without specific prior written permission. 21 * 22 * Alternatively, provided that this notice is retained in full, this 23 * software may be distributed under the terms of the GNU General 24 * Public License ("GPL") version 2, in which case the provisions of the 25 * GPL apply INSTEAD OF those given above. 26 * 27 * The provided data structures and external interfaces from this code 28 * are not restricted to be used by modules with a GPL compatible license. 29 * 30 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 31 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 32 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 33 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 34 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 35 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 36 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 37 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 38 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 39 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 40 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH 41 * DAMAGE. 42 * 43 */ 44 45 #include <linux/module.h> 46 #include <linux/init.h> 47 #include <linux/kernel.h> 48 #include <linux/sched.h> 49 #include <linux/types.h> 50 #include <linux/fcntl.h> 51 #include <linux/interrupt.h> 52 #include <linux/ptrace.h> 53 #include <linux/string.h> 54 #include <linux/errno.h> 55 #include <linux/netdevice.h> 56 #include <linux/if_arp.h> 57 #include <linux/if_ether.h> 58 #include <linux/skbuff.h> 59 #include <linux/delay.h> 60 61 #include <linux/can/dev.h> 62 #include <linux/can/error.h> 63 64 #include "sja1000.h" 65 66 #define DRV_NAME "sja1000" 67 68 MODULE_AUTHOR("Oliver Hartkopp <oliver.hartkopp@volkswagen.de>"); 69 MODULE_LICENSE("Dual BSD/GPL"); 70 MODULE_DESCRIPTION(DRV_NAME "CAN netdevice driver"); 71 72 static struct can_bittiming_const sja1000_bittiming_const = { 73 .name = DRV_NAME, 74 .tseg1_min = 1, 75 .tseg1_max = 16, 76 .tseg2_min = 1, 77 .tseg2_max = 8, 78 .sjw_max = 4, 79 .brp_min = 1, 80 .brp_max = 64, 81 .brp_inc = 1, 82 }; 83 84 static void sja1000_write_cmdreg(struct sja1000_priv *priv, u8 val) 85 { 86 unsigned long flags; 87 88 /* 89 * The command register needs some locking and time to settle 90 * the write_reg() operation - especially on SMP systems. 91 */ 92 spin_lock_irqsave(&priv->cmdreg_lock, flags); 93 priv->write_reg(priv, REG_CMR, val); 94 priv->read_reg(priv, REG_SR); 95 spin_unlock_irqrestore(&priv->cmdreg_lock, flags); 96 } 97 98 static int sja1000_probe_chip(struct net_device *dev) 99 { 100 struct sja1000_priv *priv = netdev_priv(dev); 101 102 if (priv->reg_base && (priv->read_reg(priv, 0) == 0xFF)) { 103 printk(KERN_INFO "%s: probing @0x%lX failed\n", 104 DRV_NAME, dev->base_addr); 105 return 0; 106 } 107 return -1; 108 } 109 110 static void set_reset_mode(struct net_device *dev) 111 { 112 struct sja1000_priv *priv = netdev_priv(dev); 113 unsigned char status = priv->read_reg(priv, REG_MOD); 114 int i; 115 116 /* disable interrupts */ 117 priv->write_reg(priv, REG_IER, IRQ_OFF); 118 119 for (i = 0; i < 100; i++) { 120 /* check reset bit */ 121 if (status & MOD_RM) { 122 priv->can.state = CAN_STATE_STOPPED; 123 return; 124 } 125 126 priv->write_reg(priv, REG_MOD, MOD_RM); /* reset chip */ 127 udelay(10); 128 status = priv->read_reg(priv, REG_MOD); 129 } 130 131 dev_err(dev->dev.parent, "setting SJA1000 into reset mode failed!\n"); 132 } 133 134 static void set_normal_mode(struct net_device *dev) 135 { 136 struct sja1000_priv *priv = netdev_priv(dev); 137 unsigned char status = priv->read_reg(priv, REG_MOD); 138 int i; 139 140 for (i = 0; i < 100; i++) { 141 /* check reset bit */ 142 if ((status & MOD_RM) == 0) { 143 priv->can.state = CAN_STATE_ERROR_ACTIVE; 144 /* enable interrupts */ 145 if (priv->can.ctrlmode & CAN_CTRLMODE_BERR_REPORTING) 146 priv->write_reg(priv, REG_IER, IRQ_ALL); 147 else 148 priv->write_reg(priv, REG_IER, 149 IRQ_ALL & ~IRQ_BEI); 150 return; 151 } 152 153 /* set chip to normal mode */ 154 priv->write_reg(priv, REG_MOD, 0x00); 155 udelay(10); 156 status = priv->read_reg(priv, REG_MOD); 157 } 158 159 dev_err(dev->dev.parent, "setting SJA1000 into normal mode failed!\n"); 160 } 161 162 static void sja1000_start(struct net_device *dev) 163 { 164 struct sja1000_priv *priv = netdev_priv(dev); 165 166 /* leave reset mode */ 167 if (priv->can.state != CAN_STATE_STOPPED) 168 set_reset_mode(dev); 169 170 /* Clear error counters and error code capture */ 171 priv->write_reg(priv, REG_TXERR, 0x0); 172 priv->write_reg(priv, REG_RXERR, 0x0); 173 priv->read_reg(priv, REG_ECC); 174 175 /* leave reset mode */ 176 set_normal_mode(dev); 177 } 178 179 static int sja1000_set_mode(struct net_device *dev, enum can_mode mode) 180 { 181 struct sja1000_priv *priv = netdev_priv(dev); 182 183 if (!priv->open_time) 184 return -EINVAL; 185 186 switch (mode) { 187 case CAN_MODE_START: 188 sja1000_start(dev); 189 if (netif_queue_stopped(dev)) 190 netif_wake_queue(dev); 191 break; 192 193 default: 194 return -EOPNOTSUPP; 195 } 196 197 return 0; 198 } 199 200 static int sja1000_set_bittiming(struct net_device *dev) 201 { 202 struct sja1000_priv *priv = netdev_priv(dev); 203 struct can_bittiming *bt = &priv->can.bittiming; 204 u8 btr0, btr1; 205 206 btr0 = ((bt->brp - 1) & 0x3f) | (((bt->sjw - 1) & 0x3) << 6); 207 btr1 = ((bt->prop_seg + bt->phase_seg1 - 1) & 0xf) | 208 (((bt->phase_seg2 - 1) & 0x7) << 4); 209 if (priv->can.ctrlmode & CAN_CTRLMODE_3_SAMPLES) 210 btr1 |= 0x80; 211 212 dev_info(dev->dev.parent, 213 "setting BTR0=0x%02x BTR1=0x%02x\n", btr0, btr1); 214 215 priv->write_reg(priv, REG_BTR0, btr0); 216 priv->write_reg(priv, REG_BTR1, btr1); 217 218 return 0; 219 } 220 221 static int sja1000_get_berr_counter(const struct net_device *dev, 222 struct can_berr_counter *bec) 223 { 224 struct sja1000_priv *priv = netdev_priv(dev); 225 226 bec->txerr = priv->read_reg(priv, REG_TXERR); 227 bec->rxerr = priv->read_reg(priv, REG_RXERR); 228 229 return 0; 230 } 231 232 /* 233 * initialize SJA1000 chip: 234 * - reset chip 235 * - set output mode 236 * - set baudrate 237 * - enable interrupts 238 * - start operating mode 239 */ 240 static void chipset_init(struct net_device *dev) 241 { 242 struct sja1000_priv *priv = netdev_priv(dev); 243 244 /* set clock divider and output control register */ 245 priv->write_reg(priv, REG_CDR, priv->cdr | CDR_PELICAN); 246 247 /* set acceptance filter (accept all) */ 248 priv->write_reg(priv, REG_ACCC0, 0x00); 249 priv->write_reg(priv, REG_ACCC1, 0x00); 250 priv->write_reg(priv, REG_ACCC2, 0x00); 251 priv->write_reg(priv, REG_ACCC3, 0x00); 252 253 priv->write_reg(priv, REG_ACCM0, 0xFF); 254 priv->write_reg(priv, REG_ACCM1, 0xFF); 255 priv->write_reg(priv, REG_ACCM2, 0xFF); 256 priv->write_reg(priv, REG_ACCM3, 0xFF); 257 258 priv->write_reg(priv, REG_OCR, priv->ocr | OCR_MODE_NORMAL); 259 } 260 261 /* 262 * transmit a CAN message 263 * message layout in the sk_buff should be like this: 264 * xx xx xx xx ff ll 00 11 22 33 44 55 66 77 265 * [ can-id ] [flags] [len] [can data (up to 8 bytes] 266 */ 267 static netdev_tx_t sja1000_start_xmit(struct sk_buff *skb, 268 struct net_device *dev) 269 { 270 struct sja1000_priv *priv = netdev_priv(dev); 271 struct can_frame *cf = (struct can_frame *)skb->data; 272 uint8_t fi; 273 uint8_t dlc; 274 canid_t id; 275 uint8_t dreg; 276 int i; 277 278 if (can_dropped_invalid_skb(dev, skb)) 279 return NETDEV_TX_OK; 280 281 netif_stop_queue(dev); 282 283 fi = dlc = cf->can_dlc; 284 id = cf->can_id; 285 286 if (id & CAN_RTR_FLAG) 287 fi |= FI_RTR; 288 289 if (id & CAN_EFF_FLAG) { 290 fi |= FI_FF; 291 dreg = EFF_BUF; 292 priv->write_reg(priv, REG_FI, fi); 293 priv->write_reg(priv, REG_ID1, (id & 0x1fe00000) >> (5 + 16)); 294 priv->write_reg(priv, REG_ID2, (id & 0x001fe000) >> (5 + 8)); 295 priv->write_reg(priv, REG_ID3, (id & 0x00001fe0) >> 5); 296 priv->write_reg(priv, REG_ID4, (id & 0x0000001f) << 3); 297 } else { 298 dreg = SFF_BUF; 299 priv->write_reg(priv, REG_FI, fi); 300 priv->write_reg(priv, REG_ID1, (id & 0x000007f8) >> 3); 301 priv->write_reg(priv, REG_ID2, (id & 0x00000007) << 5); 302 } 303 304 for (i = 0; i < dlc; i++) 305 priv->write_reg(priv, dreg++, cf->data[i]); 306 307 can_put_echo_skb(skb, dev, 0); 308 309 sja1000_write_cmdreg(priv, CMD_TR); 310 311 return NETDEV_TX_OK; 312 } 313 314 static void sja1000_rx(struct net_device *dev) 315 { 316 struct sja1000_priv *priv = netdev_priv(dev); 317 struct net_device_stats *stats = &dev->stats; 318 struct can_frame *cf; 319 struct sk_buff *skb; 320 uint8_t fi; 321 uint8_t dreg; 322 canid_t id; 323 int i; 324 325 /* create zero'ed CAN frame buffer */ 326 skb = alloc_can_skb(dev, &cf); 327 if (skb == NULL) 328 return; 329 330 fi = priv->read_reg(priv, REG_FI); 331 332 if (fi & FI_FF) { 333 /* extended frame format (EFF) */ 334 dreg = EFF_BUF; 335 id = (priv->read_reg(priv, REG_ID1) << (5 + 16)) 336 | (priv->read_reg(priv, REG_ID2) << (5 + 8)) 337 | (priv->read_reg(priv, REG_ID3) << 5) 338 | (priv->read_reg(priv, REG_ID4) >> 3); 339 id |= CAN_EFF_FLAG; 340 } else { 341 /* standard frame format (SFF) */ 342 dreg = SFF_BUF; 343 id = (priv->read_reg(priv, REG_ID1) << 3) 344 | (priv->read_reg(priv, REG_ID2) >> 5); 345 } 346 347 cf->can_dlc = get_can_dlc(fi & 0x0F); 348 if (fi & FI_RTR) { 349 id |= CAN_RTR_FLAG; 350 } else { 351 for (i = 0; i < cf->can_dlc; i++) 352 cf->data[i] = priv->read_reg(priv, dreg++); 353 } 354 355 cf->can_id = id; 356 357 /* release receive buffer */ 358 sja1000_write_cmdreg(priv, CMD_RRB); 359 360 netif_rx(skb); 361 362 stats->rx_packets++; 363 stats->rx_bytes += cf->can_dlc; 364 } 365 366 static int sja1000_err(struct net_device *dev, uint8_t isrc, uint8_t status) 367 { 368 struct sja1000_priv *priv = netdev_priv(dev); 369 struct net_device_stats *stats = &dev->stats; 370 struct can_frame *cf; 371 struct sk_buff *skb; 372 enum can_state state = priv->can.state; 373 uint8_t ecc, alc; 374 375 skb = alloc_can_err_skb(dev, &cf); 376 if (skb == NULL) 377 return -ENOMEM; 378 379 if (isrc & IRQ_DOI) { 380 /* data overrun interrupt */ 381 dev_dbg(dev->dev.parent, "data overrun interrupt\n"); 382 cf->can_id |= CAN_ERR_CRTL; 383 cf->data[1] = CAN_ERR_CRTL_RX_OVERFLOW; 384 stats->rx_over_errors++; 385 stats->rx_errors++; 386 sja1000_write_cmdreg(priv, CMD_CDO); /* clear bit */ 387 } 388 389 if (isrc & IRQ_EI) { 390 /* error warning interrupt */ 391 dev_dbg(dev->dev.parent, "error warning interrupt\n"); 392 393 if (status & SR_BS) { 394 state = CAN_STATE_BUS_OFF; 395 cf->can_id |= CAN_ERR_BUSOFF; 396 can_bus_off(dev); 397 } else if (status & SR_ES) { 398 state = CAN_STATE_ERROR_WARNING; 399 } else 400 state = CAN_STATE_ERROR_ACTIVE; 401 } 402 if (isrc & IRQ_BEI) { 403 /* bus error interrupt */ 404 priv->can.can_stats.bus_error++; 405 stats->rx_errors++; 406 407 ecc = priv->read_reg(priv, REG_ECC); 408 409 cf->can_id |= CAN_ERR_PROT | CAN_ERR_BUSERROR; 410 411 switch (ecc & ECC_MASK) { 412 case ECC_BIT: 413 cf->data[2] |= CAN_ERR_PROT_BIT; 414 break; 415 case ECC_FORM: 416 cf->data[2] |= CAN_ERR_PROT_FORM; 417 break; 418 case ECC_STUFF: 419 cf->data[2] |= CAN_ERR_PROT_STUFF; 420 break; 421 default: 422 cf->data[2] |= CAN_ERR_PROT_UNSPEC; 423 cf->data[3] = ecc & ECC_SEG; 424 break; 425 } 426 /* Error occurred during transmission? */ 427 if ((ecc & ECC_DIR) == 0) 428 cf->data[2] |= CAN_ERR_PROT_TX; 429 } 430 if (isrc & IRQ_EPI) { 431 /* error passive interrupt */ 432 dev_dbg(dev->dev.parent, "error passive interrupt\n"); 433 if (status & SR_ES) 434 state = CAN_STATE_ERROR_PASSIVE; 435 else 436 state = CAN_STATE_ERROR_ACTIVE; 437 } 438 if (isrc & IRQ_ALI) { 439 /* arbitration lost interrupt */ 440 dev_dbg(dev->dev.parent, "arbitration lost interrupt\n"); 441 alc = priv->read_reg(priv, REG_ALC); 442 priv->can.can_stats.arbitration_lost++; 443 stats->tx_errors++; 444 cf->can_id |= CAN_ERR_LOSTARB; 445 cf->data[0] = alc & 0x1f; 446 } 447 448 if (state != priv->can.state && (state == CAN_STATE_ERROR_WARNING || 449 state == CAN_STATE_ERROR_PASSIVE)) { 450 uint8_t rxerr = priv->read_reg(priv, REG_RXERR); 451 uint8_t txerr = priv->read_reg(priv, REG_TXERR); 452 cf->can_id |= CAN_ERR_CRTL; 453 if (state == CAN_STATE_ERROR_WARNING) { 454 priv->can.can_stats.error_warning++; 455 cf->data[1] = (txerr > rxerr) ? 456 CAN_ERR_CRTL_TX_WARNING : 457 CAN_ERR_CRTL_RX_WARNING; 458 } else { 459 priv->can.can_stats.error_passive++; 460 cf->data[1] = (txerr > rxerr) ? 461 CAN_ERR_CRTL_TX_PASSIVE : 462 CAN_ERR_CRTL_RX_PASSIVE; 463 } 464 cf->data[6] = txerr; 465 cf->data[7] = rxerr; 466 } 467 468 priv->can.state = state; 469 470 netif_rx(skb); 471 472 stats->rx_packets++; 473 stats->rx_bytes += cf->can_dlc; 474 475 return 0; 476 } 477 478 irqreturn_t sja1000_interrupt(int irq, void *dev_id) 479 { 480 struct net_device *dev = (struct net_device *)dev_id; 481 struct sja1000_priv *priv = netdev_priv(dev); 482 struct net_device_stats *stats = &dev->stats; 483 uint8_t isrc, status; 484 int n = 0; 485 486 /* Shared interrupts and IRQ off? */ 487 if (priv->read_reg(priv, REG_IER) == IRQ_OFF) 488 return IRQ_NONE; 489 490 if (priv->pre_irq) 491 priv->pre_irq(priv); 492 493 while ((isrc = priv->read_reg(priv, REG_IR)) && (n < SJA1000_MAX_IRQ)) { 494 n++; 495 status = priv->read_reg(priv, REG_SR); 496 497 if (isrc & IRQ_WUI) 498 dev_warn(dev->dev.parent, "wakeup interrupt\n"); 499 500 if (isrc & IRQ_TI) { 501 /* transmission complete interrupt */ 502 stats->tx_bytes += priv->read_reg(priv, REG_FI) & 0xf; 503 stats->tx_packets++; 504 can_get_echo_skb(dev, 0); 505 netif_wake_queue(dev); 506 } 507 if (isrc & IRQ_RI) { 508 /* receive interrupt */ 509 while (status & SR_RBS) { 510 sja1000_rx(dev); 511 status = priv->read_reg(priv, REG_SR); 512 } 513 } 514 if (isrc & (IRQ_DOI | IRQ_EI | IRQ_BEI | IRQ_EPI | IRQ_ALI)) { 515 /* error interrupt */ 516 if (sja1000_err(dev, isrc, status)) 517 break; 518 } 519 } 520 521 if (priv->post_irq) 522 priv->post_irq(priv); 523 524 if (n >= SJA1000_MAX_IRQ) 525 dev_dbg(dev->dev.parent, "%d messages handled in ISR", n); 526 527 return (n) ? IRQ_HANDLED : IRQ_NONE; 528 } 529 EXPORT_SYMBOL_GPL(sja1000_interrupt); 530 531 static int sja1000_open(struct net_device *dev) 532 { 533 struct sja1000_priv *priv = netdev_priv(dev); 534 int err; 535 536 /* set chip into reset mode */ 537 set_reset_mode(dev); 538 539 /* common open */ 540 err = open_candev(dev); 541 if (err) 542 return err; 543 544 /* register interrupt handler, if not done by the device driver */ 545 if (!(priv->flags & SJA1000_CUSTOM_IRQ_HANDLER)) { 546 err = request_irq(dev->irq, sja1000_interrupt, priv->irq_flags, 547 dev->name, (void *)dev); 548 if (err) { 549 close_candev(dev); 550 return -EAGAIN; 551 } 552 } 553 554 /* init and start chi */ 555 sja1000_start(dev); 556 priv->open_time = jiffies; 557 558 netif_start_queue(dev); 559 560 return 0; 561 } 562 563 static int sja1000_close(struct net_device *dev) 564 { 565 struct sja1000_priv *priv = netdev_priv(dev); 566 567 netif_stop_queue(dev); 568 set_reset_mode(dev); 569 570 if (!(priv->flags & SJA1000_CUSTOM_IRQ_HANDLER)) 571 free_irq(dev->irq, (void *)dev); 572 573 close_candev(dev); 574 575 priv->open_time = 0; 576 577 return 0; 578 } 579 580 struct net_device *alloc_sja1000dev(int sizeof_priv) 581 { 582 struct net_device *dev; 583 struct sja1000_priv *priv; 584 585 dev = alloc_candev(sizeof(struct sja1000_priv) + sizeof_priv, 586 SJA1000_ECHO_SKB_MAX); 587 if (!dev) 588 return NULL; 589 590 priv = netdev_priv(dev); 591 592 priv->dev = dev; 593 priv->can.bittiming_const = &sja1000_bittiming_const; 594 priv->can.do_set_bittiming = sja1000_set_bittiming; 595 priv->can.do_set_mode = sja1000_set_mode; 596 priv->can.do_get_berr_counter = sja1000_get_berr_counter; 597 priv->can.ctrlmode_supported = CAN_CTRLMODE_3_SAMPLES | 598 CAN_CTRLMODE_BERR_REPORTING; 599 600 spin_lock_init(&priv->cmdreg_lock); 601 602 if (sizeof_priv) 603 priv->priv = (void *)priv + sizeof(struct sja1000_priv); 604 605 return dev; 606 } 607 EXPORT_SYMBOL_GPL(alloc_sja1000dev); 608 609 void free_sja1000dev(struct net_device *dev) 610 { 611 free_candev(dev); 612 } 613 EXPORT_SYMBOL_GPL(free_sja1000dev); 614 615 static const struct net_device_ops sja1000_netdev_ops = { 616 .ndo_open = sja1000_open, 617 .ndo_stop = sja1000_close, 618 .ndo_start_xmit = sja1000_start_xmit, 619 }; 620 621 int register_sja1000dev(struct net_device *dev) 622 { 623 if (!sja1000_probe_chip(dev)) 624 return -ENODEV; 625 626 dev->flags |= IFF_ECHO; /* we support local echo */ 627 dev->netdev_ops = &sja1000_netdev_ops; 628 629 set_reset_mode(dev); 630 chipset_init(dev); 631 632 return register_candev(dev); 633 } 634 EXPORT_SYMBOL_GPL(register_sja1000dev); 635 636 void unregister_sja1000dev(struct net_device *dev) 637 { 638 set_reset_mode(dev); 639 unregister_candev(dev); 640 } 641 EXPORT_SYMBOL_GPL(unregister_sja1000dev); 642 643 static __init int sja1000_init(void) 644 { 645 printk(KERN_INFO "%s CAN netdevice driver\n", DRV_NAME); 646 647 return 0; 648 } 649 650 module_init(sja1000_init); 651 652 static __exit void sja1000_exit(void) 653 { 654 printk(KERN_INFO "%s: driver removed\n", DRV_NAME); 655 } 656 657 module_exit(sja1000_exit); 658