sja1000.c revision ad72c347e56bf3a0231b9d686e17764157d2961c
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 * Send feedback to <socketcan-users@lists.berlios.de> 44 * 45 */ 46 47#include <linux/module.h> 48#include <linux/init.h> 49#include <linux/kernel.h> 50#include <linux/sched.h> 51#include <linux/types.h> 52#include <linux/fcntl.h> 53#include <linux/interrupt.h> 54#include <linux/ptrace.h> 55#include <linux/string.h> 56#include <linux/errno.h> 57#include <linux/netdevice.h> 58#include <linux/if_arp.h> 59#include <linux/if_ether.h> 60#include <linux/skbuff.h> 61#include <linux/delay.h> 62 63#include <linux/can.h> 64#include <linux/can/dev.h> 65#include <linux/can/error.h> 66 67#include "sja1000.h" 68 69#define DRV_NAME "sja1000" 70 71MODULE_AUTHOR("Oliver Hartkopp <oliver.hartkopp@volkswagen.de>"); 72MODULE_LICENSE("Dual BSD/GPL"); 73MODULE_DESCRIPTION(DRV_NAME "CAN netdevice driver"); 74 75static struct can_bittiming_const sja1000_bittiming_const = { 76 .name = DRV_NAME, 77 .tseg1_min = 1, 78 .tseg1_max = 16, 79 .tseg2_min = 1, 80 .tseg2_max = 8, 81 .sjw_max = 4, 82 .brp_min = 1, 83 .brp_max = 64, 84 .brp_inc = 1, 85}; 86 87static int sja1000_probe_chip(struct net_device *dev) 88{ 89 struct sja1000_priv *priv = netdev_priv(dev); 90 91 if (priv->reg_base && (priv->read_reg(priv, 0) == 0xFF)) { 92 printk(KERN_INFO "%s: probing @0x%lX failed\n", 93 DRV_NAME, dev->base_addr); 94 return 0; 95 } 96 return -1; 97} 98 99static void set_reset_mode(struct net_device *dev) 100{ 101 struct sja1000_priv *priv = netdev_priv(dev); 102 unsigned char status = priv->read_reg(priv, REG_MOD); 103 int i; 104 105 /* disable interrupts */ 106 priv->write_reg(priv, REG_IER, IRQ_OFF); 107 108 for (i = 0; i < 100; i++) { 109 /* check reset bit */ 110 if (status & MOD_RM) { 111 priv->can.state = CAN_STATE_STOPPED; 112 return; 113 } 114 115 priv->write_reg(priv, REG_MOD, MOD_RM); /* reset chip */ 116 udelay(10); 117 status = priv->read_reg(priv, REG_MOD); 118 } 119 120 dev_err(dev->dev.parent, "setting SJA1000 into reset mode failed!\n"); 121} 122 123static void set_normal_mode(struct net_device *dev) 124{ 125 struct sja1000_priv *priv = netdev_priv(dev); 126 unsigned char status = priv->read_reg(priv, REG_MOD); 127 int i; 128 129 for (i = 0; i < 100; i++) { 130 /* check reset bit */ 131 if ((status & MOD_RM) == 0) { 132 priv->can.state = CAN_STATE_ERROR_ACTIVE; 133 /* enable all interrupts */ 134 priv->write_reg(priv, REG_IER, IRQ_ALL); 135 return; 136 } 137 138 /* set chip to normal mode */ 139 priv->write_reg(priv, REG_MOD, 0x00); 140 udelay(10); 141 status = priv->read_reg(priv, REG_MOD); 142 } 143 144 dev_err(dev->dev.parent, "setting SJA1000 into normal mode failed!\n"); 145} 146 147static void sja1000_start(struct net_device *dev) 148{ 149 struct sja1000_priv *priv = netdev_priv(dev); 150 151 /* leave reset mode */ 152 if (priv->can.state != CAN_STATE_STOPPED) 153 set_reset_mode(dev); 154 155 /* Clear error counters and error code capture */ 156 priv->write_reg(priv, REG_TXERR, 0x0); 157 priv->write_reg(priv, REG_RXERR, 0x0); 158 priv->read_reg(priv, REG_ECC); 159 160 /* leave reset mode */ 161 set_normal_mode(dev); 162} 163 164static int sja1000_set_mode(struct net_device *dev, enum can_mode mode) 165{ 166 struct sja1000_priv *priv = netdev_priv(dev); 167 168 if (!priv->open_time) 169 return -EINVAL; 170 171 switch (mode) { 172 case CAN_MODE_START: 173 sja1000_start(dev); 174 if (netif_queue_stopped(dev)) 175 netif_wake_queue(dev); 176 break; 177 178 default: 179 return -EOPNOTSUPP; 180 } 181 182 return 0; 183} 184 185static int sja1000_set_bittiming(struct net_device *dev) 186{ 187 struct sja1000_priv *priv = netdev_priv(dev); 188 struct can_bittiming *bt = &priv->can.bittiming; 189 u8 btr0, btr1; 190 191 btr0 = ((bt->brp - 1) & 0x3f) | (((bt->sjw - 1) & 0x3) << 6); 192 btr1 = ((bt->prop_seg + bt->phase_seg1 - 1) & 0xf) | 193 (((bt->phase_seg2 - 1) & 0x7) << 4); 194 if (priv->can.ctrlmode & CAN_CTRLMODE_3_SAMPLES) 195 btr1 |= 0x80; 196 197 dev_info(dev->dev.parent, 198 "setting BTR0=0x%02x BTR1=0x%02x\n", btr0, btr1); 199 200 priv->write_reg(priv, REG_BTR0, btr0); 201 priv->write_reg(priv, REG_BTR1, btr1); 202 203 return 0; 204} 205 206/* 207 * initialize SJA1000 chip: 208 * - reset chip 209 * - set output mode 210 * - set baudrate 211 * - enable interrupts 212 * - start operating mode 213 */ 214static void chipset_init(struct net_device *dev) 215{ 216 struct sja1000_priv *priv = netdev_priv(dev); 217 218 /* set clock divider and output control register */ 219 priv->write_reg(priv, REG_CDR, priv->cdr | CDR_PELICAN); 220 221 /* set acceptance filter (accept all) */ 222 priv->write_reg(priv, REG_ACCC0, 0x00); 223 priv->write_reg(priv, REG_ACCC1, 0x00); 224 priv->write_reg(priv, REG_ACCC2, 0x00); 225 priv->write_reg(priv, REG_ACCC3, 0x00); 226 227 priv->write_reg(priv, REG_ACCM0, 0xFF); 228 priv->write_reg(priv, REG_ACCM1, 0xFF); 229 priv->write_reg(priv, REG_ACCM2, 0xFF); 230 priv->write_reg(priv, REG_ACCM3, 0xFF); 231 232 priv->write_reg(priv, REG_OCR, priv->ocr | OCR_MODE_NORMAL); 233} 234 235/* 236 * transmit a CAN message 237 * message layout in the sk_buff should be like this: 238 * xx xx xx xx ff ll 00 11 22 33 44 55 66 77 239 * [ can-id ] [flags] [len] [can data (up to 8 bytes] 240 */ 241static netdev_tx_t sja1000_start_xmit(struct sk_buff *skb, 242 struct net_device *dev) 243{ 244 struct sja1000_priv *priv = netdev_priv(dev); 245 struct can_frame *cf = (struct can_frame *)skb->data; 246 uint8_t fi; 247 uint8_t dlc; 248 canid_t id; 249 uint8_t dreg; 250 int i; 251 252 if (can_dropped_invalid_skb(dev, skb)) 253 return NETDEV_TX_OK; 254 255 netif_stop_queue(dev); 256 257 fi = dlc = cf->can_dlc; 258 id = cf->can_id; 259 260 if (id & CAN_RTR_FLAG) 261 fi |= FI_RTR; 262 263 if (id & CAN_EFF_FLAG) { 264 fi |= FI_FF; 265 dreg = EFF_BUF; 266 priv->write_reg(priv, REG_FI, fi); 267 priv->write_reg(priv, REG_ID1, (id & 0x1fe00000) >> (5 + 16)); 268 priv->write_reg(priv, REG_ID2, (id & 0x001fe000) >> (5 + 8)); 269 priv->write_reg(priv, REG_ID3, (id & 0x00001fe0) >> 5); 270 priv->write_reg(priv, REG_ID4, (id & 0x0000001f) << 3); 271 } else { 272 dreg = SFF_BUF; 273 priv->write_reg(priv, REG_FI, fi); 274 priv->write_reg(priv, REG_ID1, (id & 0x000007f8) >> 3); 275 priv->write_reg(priv, REG_ID2, (id & 0x00000007) << 5); 276 } 277 278 for (i = 0; i < dlc; i++) 279 priv->write_reg(priv, dreg++, cf->data[i]); 280 281 dev->trans_start = jiffies; 282 283 can_put_echo_skb(skb, dev, 0); 284 285 priv->write_reg(priv, REG_CMR, CMD_TR); 286 287 return NETDEV_TX_OK; 288} 289 290static void sja1000_rx(struct net_device *dev) 291{ 292 struct sja1000_priv *priv = netdev_priv(dev); 293 struct net_device_stats *stats = &dev->stats; 294 struct can_frame *cf; 295 struct sk_buff *skb; 296 uint8_t fi; 297 uint8_t dreg; 298 canid_t id; 299 int i; 300 301 /* create zero'ed CAN frame buffer */ 302 skb = alloc_can_skb(dev, &cf); 303 if (skb == NULL) 304 return; 305 306 fi = priv->read_reg(priv, REG_FI); 307 308 if (fi & FI_FF) { 309 /* extended frame format (EFF) */ 310 dreg = EFF_BUF; 311 id = (priv->read_reg(priv, REG_ID1) << (5 + 16)) 312 | (priv->read_reg(priv, REG_ID2) << (5 + 8)) 313 | (priv->read_reg(priv, REG_ID3) << 5) 314 | (priv->read_reg(priv, REG_ID4) >> 3); 315 id |= CAN_EFF_FLAG; 316 } else { 317 /* standard frame format (SFF) */ 318 dreg = SFF_BUF; 319 id = (priv->read_reg(priv, REG_ID1) << 3) 320 | (priv->read_reg(priv, REG_ID2) >> 5); 321 } 322 323 if (fi & FI_RTR) { 324 id |= CAN_RTR_FLAG; 325 } else { 326 cf->can_dlc = get_can_dlc(fi & 0x0F); 327 for (i = 0; i < cf->can_dlc; i++) 328 cf->data[i] = priv->read_reg(priv, dreg++); 329 } 330 331 cf->can_id = id; 332 333 /* release receive buffer */ 334 priv->write_reg(priv, REG_CMR, CMD_RRB); 335 336 netif_rx(skb); 337 338 stats->rx_packets++; 339 stats->rx_bytes += cf->can_dlc; 340} 341 342static int sja1000_err(struct net_device *dev, uint8_t isrc, uint8_t status) 343{ 344 struct sja1000_priv *priv = netdev_priv(dev); 345 struct net_device_stats *stats = &dev->stats; 346 struct can_frame *cf; 347 struct sk_buff *skb; 348 enum can_state state = priv->can.state; 349 uint8_t ecc, alc; 350 351 skb = alloc_can_err_skb(dev, &cf); 352 if (skb == NULL) 353 return -ENOMEM; 354 355 if (isrc & IRQ_DOI) { 356 /* data overrun interrupt */ 357 dev_dbg(dev->dev.parent, "data overrun interrupt\n"); 358 cf->can_id |= CAN_ERR_CRTL; 359 cf->data[1] = CAN_ERR_CRTL_RX_OVERFLOW; 360 stats->rx_over_errors++; 361 stats->rx_errors++; 362 priv->write_reg(priv, REG_CMR, CMD_CDO); /* clear bit */ 363 } 364 365 if (isrc & IRQ_EI) { 366 /* error warning interrupt */ 367 dev_dbg(dev->dev.parent, "error warning interrupt\n"); 368 369 if (status & SR_BS) { 370 state = CAN_STATE_BUS_OFF; 371 cf->can_id |= CAN_ERR_BUSOFF; 372 can_bus_off(dev); 373 } else if (status & SR_ES) { 374 state = CAN_STATE_ERROR_WARNING; 375 } else 376 state = CAN_STATE_ERROR_ACTIVE; 377 } 378 if (isrc & IRQ_BEI) { 379 /* bus error interrupt */ 380 priv->can.can_stats.bus_error++; 381 stats->rx_errors++; 382 383 ecc = priv->read_reg(priv, REG_ECC); 384 385 cf->can_id |= CAN_ERR_PROT | CAN_ERR_BUSERROR; 386 387 switch (ecc & ECC_MASK) { 388 case ECC_BIT: 389 cf->data[2] |= CAN_ERR_PROT_BIT; 390 break; 391 case ECC_FORM: 392 cf->data[2] |= CAN_ERR_PROT_FORM; 393 break; 394 case ECC_STUFF: 395 cf->data[2] |= CAN_ERR_PROT_STUFF; 396 break; 397 default: 398 cf->data[2] |= CAN_ERR_PROT_UNSPEC; 399 cf->data[3] = ecc & ECC_SEG; 400 break; 401 } 402 /* Error occured during transmission? */ 403 if ((ecc & ECC_DIR) == 0) 404 cf->data[2] |= CAN_ERR_PROT_TX; 405 } 406 if (isrc & IRQ_EPI) { 407 /* error passive interrupt */ 408 dev_dbg(dev->dev.parent, "error passive interrupt\n"); 409 if (status & SR_ES) 410 state = CAN_STATE_ERROR_PASSIVE; 411 else 412 state = CAN_STATE_ERROR_ACTIVE; 413 } 414 if (isrc & IRQ_ALI) { 415 /* arbitration lost interrupt */ 416 dev_dbg(dev->dev.parent, "arbitration lost interrupt\n"); 417 alc = priv->read_reg(priv, REG_ALC); 418 priv->can.can_stats.arbitration_lost++; 419 stats->tx_errors++; 420 cf->can_id |= CAN_ERR_LOSTARB; 421 cf->data[0] = alc & 0x1f; 422 } 423 424 if (state != priv->can.state && (state == CAN_STATE_ERROR_WARNING || 425 state == CAN_STATE_ERROR_PASSIVE)) { 426 uint8_t rxerr = priv->read_reg(priv, REG_RXERR); 427 uint8_t txerr = priv->read_reg(priv, REG_TXERR); 428 cf->can_id |= CAN_ERR_CRTL; 429 if (state == CAN_STATE_ERROR_WARNING) { 430 priv->can.can_stats.error_warning++; 431 cf->data[1] = (txerr > rxerr) ? 432 CAN_ERR_CRTL_TX_WARNING : 433 CAN_ERR_CRTL_RX_WARNING; 434 } else { 435 priv->can.can_stats.error_passive++; 436 cf->data[1] = (txerr > rxerr) ? 437 CAN_ERR_CRTL_TX_PASSIVE : 438 CAN_ERR_CRTL_RX_PASSIVE; 439 } 440 } 441 442 priv->can.state = state; 443 444 netif_rx(skb); 445 446 stats->rx_packets++; 447 stats->rx_bytes += cf->can_dlc; 448 449 return 0; 450} 451 452irqreturn_t sja1000_interrupt(int irq, void *dev_id) 453{ 454 struct net_device *dev = (struct net_device *)dev_id; 455 struct sja1000_priv *priv = netdev_priv(dev); 456 struct net_device_stats *stats = &dev->stats; 457 uint8_t isrc, status; 458 int n = 0; 459 460 /* Shared interrupts and IRQ off? */ 461 if (priv->read_reg(priv, REG_IER) == IRQ_OFF) 462 return IRQ_NONE; 463 464 if (priv->pre_irq) 465 priv->pre_irq(priv); 466 467 while ((isrc = priv->read_reg(priv, REG_IR)) && (n < SJA1000_MAX_IRQ)) { 468 n++; 469 status = priv->read_reg(priv, REG_SR); 470 471 if (isrc & IRQ_WUI) 472 dev_warn(dev->dev.parent, "wakeup interrupt\n"); 473 474 if (isrc & IRQ_TI) { 475 /* transmission complete interrupt */ 476 stats->tx_bytes += priv->read_reg(priv, REG_FI) & 0xf; 477 stats->tx_packets++; 478 can_get_echo_skb(dev, 0); 479 netif_wake_queue(dev); 480 } 481 if (isrc & IRQ_RI) { 482 /* receive interrupt */ 483 while (status & SR_RBS) { 484 sja1000_rx(dev); 485 status = priv->read_reg(priv, REG_SR); 486 } 487 } 488 if (isrc & (IRQ_DOI | IRQ_EI | IRQ_BEI | IRQ_EPI | IRQ_ALI)) { 489 /* error interrupt */ 490 if (sja1000_err(dev, isrc, status)) 491 break; 492 } 493 } 494 495 if (priv->post_irq) 496 priv->post_irq(priv); 497 498 if (n >= SJA1000_MAX_IRQ) 499 dev_dbg(dev->dev.parent, "%d messages handled in ISR", n); 500 501 return (n) ? IRQ_HANDLED : IRQ_NONE; 502} 503EXPORT_SYMBOL_GPL(sja1000_interrupt); 504 505static int sja1000_open(struct net_device *dev) 506{ 507 struct sja1000_priv *priv = netdev_priv(dev); 508 int err; 509 510 /* set chip into reset mode */ 511 set_reset_mode(dev); 512 513 /* common open */ 514 err = open_candev(dev); 515 if (err) 516 return err; 517 518 /* register interrupt handler, if not done by the device driver */ 519 if (!(priv->flags & SJA1000_CUSTOM_IRQ_HANDLER)) { 520 err = request_irq(dev->irq, sja1000_interrupt, priv->irq_flags, 521 dev->name, (void *)dev); 522 if (err) { 523 close_candev(dev); 524 return -EAGAIN; 525 } 526 } 527 528 /* init and start chi */ 529 sja1000_start(dev); 530 priv->open_time = jiffies; 531 532 netif_start_queue(dev); 533 534 return 0; 535} 536 537static int sja1000_close(struct net_device *dev) 538{ 539 struct sja1000_priv *priv = netdev_priv(dev); 540 541 netif_stop_queue(dev); 542 set_reset_mode(dev); 543 544 if (!(priv->flags & SJA1000_CUSTOM_IRQ_HANDLER)) 545 free_irq(dev->irq, (void *)dev); 546 547 close_candev(dev); 548 549 priv->open_time = 0; 550 551 return 0; 552} 553 554struct net_device *alloc_sja1000dev(int sizeof_priv) 555{ 556 struct net_device *dev; 557 struct sja1000_priv *priv; 558 559 dev = alloc_candev(sizeof(struct sja1000_priv) + sizeof_priv, 560 SJA1000_ECHO_SKB_MAX); 561 if (!dev) 562 return NULL; 563 564 priv = netdev_priv(dev); 565 566 priv->dev = dev; 567 priv->can.bittiming_const = &sja1000_bittiming_const; 568 priv->can.do_set_bittiming = sja1000_set_bittiming; 569 priv->can.do_set_mode = sja1000_set_mode; 570 priv->can.ctrlmode_supported = CAN_CTRLMODE_3_SAMPLES; 571 572 if (sizeof_priv) 573 priv->priv = (void *)priv + sizeof(struct sja1000_priv); 574 575 return dev; 576} 577EXPORT_SYMBOL_GPL(alloc_sja1000dev); 578 579void free_sja1000dev(struct net_device *dev) 580{ 581 free_candev(dev); 582} 583EXPORT_SYMBOL_GPL(free_sja1000dev); 584 585static const struct net_device_ops sja1000_netdev_ops = { 586 .ndo_open = sja1000_open, 587 .ndo_stop = sja1000_close, 588 .ndo_start_xmit = sja1000_start_xmit, 589}; 590 591int register_sja1000dev(struct net_device *dev) 592{ 593 if (!sja1000_probe_chip(dev)) 594 return -ENODEV; 595 596 dev->flags |= IFF_ECHO; /* we support local echo */ 597 dev->netdev_ops = &sja1000_netdev_ops; 598 599 set_reset_mode(dev); 600 chipset_init(dev); 601 602 return register_candev(dev); 603} 604EXPORT_SYMBOL_GPL(register_sja1000dev); 605 606void unregister_sja1000dev(struct net_device *dev) 607{ 608 set_reset_mode(dev); 609 unregister_candev(dev); 610} 611EXPORT_SYMBOL_GPL(unregister_sja1000dev); 612 613static __init int sja1000_init(void) 614{ 615 printk(KERN_INFO "%s CAN netdevice driver\n", DRV_NAME); 616 617 return 0; 618} 619 620module_init(sja1000_init); 621 622static __exit void sja1000_exit(void) 623{ 624 printk(KERN_INFO "%s: driver removed\n", DRV_NAME); 625} 626 627module_exit(sja1000_exit); 628