2 * Copyright (C) 2010-2012 Stephane Grosjean <s.grosjean@peak-system.com>
4 * CAN driver for PEAK-System PCAN-PC Card
5 * Derived from the PCAN project file driver/src/pcan_pccard.c
6 * Copyright (C) 2006-2010 PEAK System-Technik GmbH
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the version 2 of the GNU General Public License
10 * as published by the Free Software Foundation
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 #include <linux/kernel.h>
18 #include <linux/module.h>
19 #include <linux/interrupt.h>
20 #include <linux/netdevice.h>
21 #include <linux/delay.h>
22 #include <linux/timer.h>
24 #include <pcmcia/cistpl.h>
25 #include <pcmcia/ds.h>
26 #include <linux/can.h>
27 #include <linux/can/dev.h>
30 MODULE_AUTHOR("Stephane Grosjean <s.grosjean@peak-system.com>");
31 MODULE_DESCRIPTION("CAN driver for PEAK-System PCAN-PC Cards");
32 MODULE_LICENSE("GPL v2");
33 MODULE_SUPPORTED_DEVICE("PEAK PCAN-PC Card");
35 /* PEAK-System PCMCIA driver name */
36 #define PCC_NAME "peak_pcmcia"
38 #define PCC_CHAN_MAX 2
40 #define PCC_CAN_CLOCK (16000000 / 2)
42 #define PCC_MANF_ID 0x0377
43 #define PCC_CARD_ID 0x0001
45 #define PCC_CHAN_SIZE 0x20
46 #define PCC_CHAN_OFF(c) ((c) * PCC_CHAN_SIZE)
47 #define PCC_COMN_OFF (PCC_CHAN_OFF(PCC_CHAN_MAX))
48 #define PCC_COMN_SIZE 0x40
50 /* common area registers */
54 #define PCC_SPI_DIR 0x06
55 #define PCC_SPI_DOR 0x08
56 #define PCC_SPI_ADR 0x0a
57 #define PCC_SPI_IR 0x0c
58 #define PCC_FW_MAJOR 0x10
59 #define PCC_FW_MINOR 0x12
62 #define PCC_CCR_CLK_16 0x00
63 #define PCC_CCR_CLK_10 0x01
64 #define PCC_CCR_CLK_21 0x02
65 #define PCC_CCR_CLK_8 0x03
66 #define PCC_CCR_CLK_MASK PCC_CCR_CLK_8
68 #define PCC_CCR_RST_CHAN(c) (0x01 << ((c) + 2))
69 #define PCC_CCR_RST_ALL (PCC_CCR_RST_CHAN(0) | PCC_CCR_RST_CHAN(1))
70 #define PCC_CCR_RST_MASK PCC_CCR_RST_ALL
72 /* led selection bits */
73 #define PCC_LED(c) (1 << (c))
74 #define PCC_LED_ALL (PCC_LED(0) | PCC_LED(1))
77 #define PCC_LED_ON 0x00
78 #define PCC_LED_FAST 0x01
79 #define PCC_LED_SLOW 0x02
80 #define PCC_LED_OFF 0x03
82 #define PCC_CCR_LED_CHAN(s, c) ((s) << (((c) + 2) << 1))
84 #define PCC_CCR_LED_ON_CHAN(c) PCC_CCR_LED_CHAN(PCC_LED_ON, c)
85 #define PCC_CCR_LED_FAST_CHAN(c) PCC_CCR_LED_CHAN(PCC_LED_FAST, c)
86 #define PCC_CCR_LED_SLOW_CHAN(c) PCC_CCR_LED_CHAN(PCC_LED_SLOW, c)
87 #define PCC_CCR_LED_OFF_CHAN(c) PCC_CCR_LED_CHAN(PCC_LED_OFF, c)
88 #define PCC_CCR_LED_MASK_CHAN(c) PCC_CCR_LED_OFF_CHAN(c)
89 #define PCC_CCR_LED_OFF_ALL (PCC_CCR_LED_OFF_CHAN(0) | \
90 PCC_CCR_LED_OFF_CHAN(1))
91 #define PCC_CCR_LED_MASK PCC_CCR_LED_OFF_ALL
93 #define PCC_CCR_INIT (PCC_CCR_CLK_16 | PCC_CCR_RST_ALL | PCC_CCR_LED_OFF_ALL)
96 #define PCC_CSR_SPI_BUSY 0x04
98 /* time waiting for SPI busy (prevent from infinite loop) */
99 #define PCC_SPI_MAX_BUSY_WAIT_MS 3
101 /* max count of reading the SPI status register waiting for a change */
102 /* (prevent from infinite loop) */
103 #define PCC_WRITE_MAX_LOOP 1000
105 /* max nb of int handled by that isr in one shot (prevent from infinite loop) */
106 #define PCC_ISR_MAX_LOOP 10
108 /* EEPROM chip instruction set */
109 /* note: EEPROM Read/Write instructions include A8 bit */
110 #define PCC_EEP_WRITE(a) (0x02 | (((a) & 0x100) >> 5))
111 #define PCC_EEP_READ(a) (0x03 | (((a) & 0x100) >> 5))
112 #define PCC_EEP_WRDI 0x04 /* EEPROM Write Disable */
113 #define PCC_EEP_RDSR 0x05 /* EEPROM Read Status Register */
114 #define PCC_EEP_WREN 0x06 /* EEPROM Write Enable */
116 /* EEPROM Status Register bits */
117 #define PCC_EEP_SR_WEN 0x02 /* EEPROM SR Write Enable bit */
118 #define PCC_EEP_SR_WIP 0x01 /* EEPROM SR Write In Progress bit */
121 * The board configuration is probably following:
122 * RX1 is connected to ground.
123 * TX1 is not connected.
124 * CLKO is not connected.
125 * Setting the OCR register to 0xDA is a good idea.
126 * This means normal output mode, push-pull and the correct polarity.
128 #define PCC_OCR (OCR_TX0_PUSHPULL | OCR_TX1_PUSHPULL)
131 * In the CDR register, you should set CBP to 1.
132 * You will probably also want to set the clock divider value to 7
133 * (meaning direct oscillator output) because the second SJA1000 chip
134 * is driven by the first one CLKOUT output.
136 #define PCC_CDR (CDR_CBP | CDR_CLKOUT_MASK)
138 struct pcan_channel
{
139 struct net_device
*netdev
;
140 unsigned long prev_rx_bytes
;
141 unsigned long prev_tx_bytes
;
144 /* PCAN-PC Card private structure */
146 struct pcmcia_device
*pdev
;
148 struct pcan_channel channel
[PCC_CHAN_MAX
];
152 void __iomem
*ioport_addr
;
153 struct timer_list led_timer
;
156 static struct pcmcia_device_id pcan_table
[] = {
157 PCMCIA_DEVICE_MANF_CARD(PCC_MANF_ID
, PCC_CARD_ID
),
161 MODULE_DEVICE_TABLE(pcmcia
, pcan_table
);
163 static void pcan_set_leds(struct pcan_pccard
*card
, u8 mask
, u8 state
);
166 * start timer which controls leds state
168 static void pcan_start_led_timer(struct pcan_pccard
*card
)
170 if (!timer_pending(&card
->led_timer
))
171 mod_timer(&card
->led_timer
, jiffies
+ HZ
);
175 * stop the timer which controls leds state
177 static void pcan_stop_led_timer(struct pcan_pccard
*card
)
179 del_timer_sync(&card
->led_timer
);
183 * read a sja1000 register
185 static u8
pcan_read_canreg(const struct sja1000_priv
*priv
, int port
)
187 return ioread8(priv
->reg_base
+ port
);
191 * write a sja1000 register
193 static void pcan_write_canreg(const struct sja1000_priv
*priv
, int port
, u8 v
)
195 struct pcan_pccard
*card
= priv
->priv
;
196 int c
= (priv
->reg_base
- card
->ioport_addr
) / PCC_CHAN_SIZE
;
198 /* sja1000 register changes control the leds state */
199 if (port
== SJA1000_MOD
)
202 /* Reset Mode: set led on */
203 pcan_set_leds(card
, PCC_LED(c
), PCC_LED_ON
);
206 /* Normal Mode: led slow blinking and start led timer */
207 pcan_set_leds(card
, PCC_LED(c
), PCC_LED_SLOW
);
208 pcan_start_led_timer(card
);
214 iowrite8(v
, priv
->reg_base
+ port
);
218 * read a register from the common area
220 static u8
pcan_read_reg(struct pcan_pccard
*card
, int port
)
222 return ioread8(card
->ioport_addr
+ PCC_COMN_OFF
+ port
);
226 * write a register into the common area
228 static void pcan_write_reg(struct pcan_pccard
*card
, int port
, u8 v
)
230 /* cache ccr value */
231 if (port
== PCC_CCR
) {
237 iowrite8(v
, card
->ioport_addr
+ PCC_COMN_OFF
+ port
);
241 * check whether the card is present by checking its fw version numbers
242 * against values read at probing time.
244 static inline int pcan_pccard_present(struct pcan_pccard
*card
)
246 return ((pcan_read_reg(card
, PCC_FW_MAJOR
) == card
->fw_major
) &&
247 (pcan_read_reg(card
, PCC_FW_MINOR
) == card
->fw_minor
));
251 * wait for SPI engine while it is busy
253 static int pcan_wait_spi_busy(struct pcan_pccard
*card
)
255 unsigned long timeout
= jiffies
+
256 msecs_to_jiffies(PCC_SPI_MAX_BUSY_WAIT_MS
) + 1;
258 /* be sure to read status at least once after sleeping */
259 while (pcan_read_reg(card
, PCC_CSR
) & PCC_CSR_SPI_BUSY
) {
260 if (time_after(jiffies
, timeout
))
269 * write data in device eeprom
271 static int pcan_write_eeprom(struct pcan_pccard
*card
, u16 addr
, u8 v
)
276 /* write instruction enabling write */
277 pcan_write_reg(card
, PCC_SPI_IR
, PCC_EEP_WREN
);
278 err
= pcan_wait_spi_busy(card
);
282 /* wait until write enabled */
283 for (i
= 0; i
< PCC_WRITE_MAX_LOOP
; i
++) {
284 /* write instruction reading the status register */
285 pcan_write_reg(card
, PCC_SPI_IR
, PCC_EEP_RDSR
);
286 err
= pcan_wait_spi_busy(card
);
290 /* get status register value and check write enable bit */
291 status
= pcan_read_reg(card
, PCC_SPI_DIR
);
292 if (status
& PCC_EEP_SR_WEN
)
296 if (i
>= PCC_WRITE_MAX_LOOP
) {
297 dev_err(&card
->pdev
->dev
,
298 "stop waiting to be allowed to write in eeprom\n");
302 /* set address and data */
303 pcan_write_reg(card
, PCC_SPI_ADR
, addr
& 0xff);
304 pcan_write_reg(card
, PCC_SPI_DOR
, v
);
307 * write instruction with bit[3] set according to address value:
308 * if addr refers to upper half of the memory array: bit[3] = 1
310 pcan_write_reg(card
, PCC_SPI_IR
, PCC_EEP_WRITE(addr
));
311 err
= pcan_wait_spi_busy(card
);
315 /* wait while write in progress */
316 for (i
= 0; i
< PCC_WRITE_MAX_LOOP
; i
++) {
317 /* write instruction reading the status register */
318 pcan_write_reg(card
, PCC_SPI_IR
, PCC_EEP_RDSR
);
319 err
= pcan_wait_spi_busy(card
);
323 /* get status register value and check write in progress bit */
324 status
= pcan_read_reg(card
, PCC_SPI_DIR
);
325 if (!(status
& PCC_EEP_SR_WIP
))
329 if (i
>= PCC_WRITE_MAX_LOOP
) {
330 dev_err(&card
->pdev
->dev
,
331 "stop waiting for write in eeprom to complete\n");
335 /* write instruction disabling write */
336 pcan_write_reg(card
, PCC_SPI_IR
, PCC_EEP_WRDI
);
337 err
= pcan_wait_spi_busy(card
);
344 dev_err(&card
->pdev
->dev
,
345 "stop waiting (spi engine always busy) err %d\n", err
);
350 static void pcan_set_leds(struct pcan_pccard
*card
, u8 led_mask
, u8 state
)
355 for (i
= 0; i
< card
->chan_count
; i
++)
356 if (led_mask
& PCC_LED(i
)) {
357 /* clear corresponding led bits in ccr */
358 ccr
&= ~PCC_CCR_LED_MASK_CHAN(i
);
359 /* then set new bits */
360 ccr
|= PCC_CCR_LED_CHAN(state
, i
);
363 /* real write only if something has changed in ccr */
364 pcan_write_reg(card
, PCC_CCR
, ccr
);
368 * enable/disable CAN connectors power
370 static inline void pcan_set_can_power(struct pcan_pccard
*card
, int onoff
)
374 err
= pcan_write_eeprom(card
, 0, !!onoff
);
376 dev_err(&card
->pdev
->dev
,
377 "failed setting power %s to can connectors (err %d)\n",
378 (onoff
) ? "on" : "off", err
);
382 * set leds state according to channel activity
384 static void pcan_led_timer(unsigned long arg
)
386 struct pcan_pccard
*card
= (struct pcan_pccard
*)arg
;
387 struct net_device
*netdev
;
392 for (i
= 0; i
< card
->chan_count
; i
++) {
393 /* default is: not configured */
394 ccr
&= ~PCC_CCR_LED_MASK_CHAN(i
);
395 ccr
|= PCC_CCR_LED_ON_CHAN(i
);
397 netdev
= card
->channel
[i
].netdev
;
398 if (!netdev
|| !(netdev
->flags
& IFF_UP
))
403 /* no activity (but configured) */
404 ccr
&= ~PCC_CCR_LED_MASK_CHAN(i
);
405 ccr
|= PCC_CCR_LED_SLOW_CHAN(i
);
407 /* if bytes counters changed, set fast blinking led */
408 if (netdev
->stats
.rx_bytes
!= card
->channel
[i
].prev_rx_bytes
) {
409 card
->channel
[i
].prev_rx_bytes
= netdev
->stats
.rx_bytes
;
410 ccr
&= ~PCC_CCR_LED_MASK_CHAN(i
);
411 ccr
|= PCC_CCR_LED_FAST_CHAN(i
);
413 if (netdev
->stats
.tx_bytes
!= card
->channel
[i
].prev_tx_bytes
) {
414 card
->channel
[i
].prev_tx_bytes
= netdev
->stats
.tx_bytes
;
415 ccr
&= ~PCC_CCR_LED_MASK_CHAN(i
);
416 ccr
|= PCC_CCR_LED_FAST_CHAN(i
);
420 /* write the new leds state */
421 pcan_write_reg(card
, PCC_CCR
, ccr
);
423 /* restart timer (except if no more configured channels) */
425 mod_timer(&card
->led_timer
, jiffies
+ HZ
);
429 * interrupt service routine
431 static irqreturn_t
pcan_isr(int irq
, void *dev_id
)
433 struct pcan_pccard
*card
= dev_id
;
436 /* prevent from infinite loop */
437 for (irq_handled
= 0; irq_handled
< PCC_ISR_MAX_LOOP
; irq_handled
++) {
438 /* handle shared interrupt and next loop */
439 int nothing_to_handle
= 1;
442 /* check interrupt for each channel */
443 for (i
= 0; i
< card
->chan_count
; i
++) {
444 struct net_device
*netdev
;
447 * check whether the card is present before calling
448 * sja1000_interrupt() to speed up hotplug detection
450 if (!pcan_pccard_present(card
)) {
451 /* card unplugged during isr */
456 * should check whether all or SJA1000_MAX_IRQ
457 * interrupts have been handled: loop again to be sure.
459 netdev
= card
->channel
[i
].netdev
;
461 sja1000_interrupt(irq
, netdev
) == IRQ_HANDLED
)
462 nothing_to_handle
= 0;
465 if (nothing_to_handle
)
469 return (irq_handled
) ? IRQ_HANDLED
: IRQ_NONE
;
473 * free all resources used by the channels and switch off leds and can power
475 static void pcan_free_channels(struct pcan_pccard
*card
)
480 for (i
= 0; i
< card
->chan_count
; i
++) {
481 struct net_device
*netdev
;
484 led_mask
|= PCC_LED(i
);
486 netdev
= card
->channel
[i
].netdev
;
490 strncpy(name
, netdev
->name
, IFNAMSIZ
);
492 unregister_sja1000dev(netdev
);
494 free_sja1000dev(netdev
);
496 dev_info(&card
->pdev
->dev
, "%s removed\n", name
);
499 /* do it only if device not removed */
500 if (pcan_pccard_present(card
)) {
501 pcan_set_leds(card
, led_mask
, PCC_LED_OFF
);
502 pcan_set_can_power(card
, 0);
507 * check if a CAN controller is present at the specified location
509 static inline int pcan_channel_present(struct sja1000_priv
*priv
)
511 /* make sure SJA1000 is in reset mode */
512 pcan_write_canreg(priv
, SJA1000_MOD
, 1);
513 pcan_write_canreg(priv
, SJA1000_CDR
, CDR_PELICAN
);
515 /* read reset-values */
516 if (pcan_read_canreg(priv
, SJA1000_CDR
) == CDR_PELICAN
)
522 static int pcan_add_channels(struct pcan_pccard
*card
)
524 struct pcmcia_device
*pdev
= card
->pdev
;
526 u8 ccr
= PCC_CCR_INIT
;
528 /* init common registers (reset channels and leds off) */
530 pcan_write_reg(card
, PCC_CCR
, ccr
);
532 /* wait 2ms before unresetting channels */
535 ccr
&= ~PCC_CCR_RST_ALL
;
536 pcan_write_reg(card
, PCC_CCR
, ccr
);
538 /* create one network device per channel detected */
539 for (i
= 0; i
< ARRAY_SIZE(card
->channel
); i
++) {
540 struct net_device
*netdev
;
541 struct sja1000_priv
*priv
;
543 netdev
= alloc_sja1000dev(0);
549 /* update linkages */
550 priv
= netdev_priv(netdev
);
552 SET_NETDEV_DEV(netdev
, &pdev
->dev
);
554 priv
->irq_flags
= IRQF_SHARED
;
555 netdev
->irq
= pdev
->irq
;
556 priv
->reg_base
= card
->ioport_addr
+ PCC_CHAN_OFF(i
);
558 /* check if channel is present */
559 if (!pcan_channel_present(priv
)) {
560 dev_err(&pdev
->dev
, "channel %d not present\n", i
);
561 free_sja1000dev(netdev
);
565 priv
->read_reg
= pcan_read_canreg
;
566 priv
->write_reg
= pcan_write_canreg
;
567 priv
->can
.clock
.freq
= PCC_CAN_CLOCK
;
571 /* Neither a slave device distributes the clock */
573 priv
->cdr
|= CDR_CLK_OFF
;
575 priv
->flags
|= SJA1000_CUSTOM_IRQ_HANDLER
;
577 /* register SJA1000 device */
578 err
= register_sja1000dev(netdev
);
580 free_sja1000dev(netdev
);
584 card
->channel
[i
].netdev
= netdev
;
587 /* set corresponding led on in the new ccr */
588 ccr
&= ~PCC_CCR_LED_OFF_CHAN(i
);
591 "%s on channel %d at 0x%p irq %d\n",
592 netdev
->name
, i
, priv
->reg_base
, pdev
->irq
);
595 /* write new ccr (change leds state) */
596 pcan_write_reg(card
, PCC_CCR
, ccr
);
601 static int pcan_conf_check(struct pcmcia_device
*pdev
, void *priv_data
)
603 pdev
->resource
[0]->flags
&= ~IO_DATA_PATH_WIDTH
;
604 pdev
->resource
[0]->flags
|= IO_DATA_PATH_WIDTH_8
; /* only */
607 /* This reserves IO space but doesn't actually enable it */
608 return pcmcia_request_io(pdev
);
612 * free all resources used by the device
614 static void pcan_free(struct pcmcia_device
*pdev
)
616 struct pcan_pccard
*card
= pdev
->priv
;
621 free_irq(pdev
->irq
, card
);
622 pcan_stop_led_timer(card
);
624 pcan_free_channels(card
);
626 ioport_unmap(card
->ioport_addr
);
633 * setup PCMCIA socket and probe for PEAK-System PC-CARD
635 static int pcan_probe(struct pcmcia_device
*pdev
)
637 struct pcan_pccard
*card
;
640 pdev
->config_flags
|= CONF_ENABLE_IRQ
| CONF_AUTO_SET_IO
;
642 err
= pcmcia_loop_config(pdev
, pcan_conf_check
, NULL
);
644 dev_err(&pdev
->dev
, "pcmcia_loop_config() error %d\n", err
);
649 dev_err(&pdev
->dev
, "no irq assigned\n");
654 err
= pcmcia_enable_device(pdev
);
656 dev_err(&pdev
->dev
, "pcmcia_enable_device failed err=%d\n",
661 card
= kzalloc(sizeof(struct pcan_pccard
), GFP_KERNEL
);
670 /* sja1000 api uses iomem */
671 card
->ioport_addr
= ioport_map(pdev
->resource
[0]->start
,
672 resource_size(pdev
->resource
[0]));
673 if (!card
->ioport_addr
) {
674 dev_err(&pdev
->dev
, "couldn't map io port into io memory\n");
678 card
->fw_major
= pcan_read_reg(card
, PCC_FW_MAJOR
);
679 card
->fw_minor
= pcan_read_reg(card
, PCC_FW_MINOR
);
681 /* display board name and firware version */
682 dev_info(&pdev
->dev
, "PEAK-System pcmcia card %s fw %d.%d\n",
683 pdev
->prod_id
[1] ? pdev
->prod_id
[1] : "PCAN-PC Card",
684 card
->fw_major
, card
->fw_minor
);
686 /* detect available channels */
687 pcan_add_channels(card
);
688 if (!card
->chan_count
) {
693 /* init the timer which controls the leds */
694 init_timer(&card
->led_timer
);
695 card
->led_timer
.function
= pcan_led_timer
;
696 card
->led_timer
.data
= (unsigned long)card
;
698 /* request the given irq */
699 err
= request_irq(pdev
->irq
, &pcan_isr
, IRQF_SHARED
, PCC_NAME
, card
);
701 dev_err(&pdev
->dev
, "couldn't request irq%d\n", pdev
->irq
);
705 /* power on the connectors */
706 pcan_set_can_power(card
, 1);
711 /* unregister can devices from network */
712 pcan_free_channels(card
);
715 ioport_unmap(card
->ioport_addr
);
722 pcmcia_disable_device(pdev
);
729 * release claimed resources
731 static void pcan_remove(struct pcmcia_device
*pdev
)
734 pcmcia_disable_device(pdev
);
737 static struct pcmcia_driver pcan_driver
= {
740 .remove
= pcan_remove
,
741 .id_table
= pcan_table
,
743 module_pcmcia_driver(pcan_driver
);