1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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>
23#include <linux/io.h>
24#include <pcmcia/cistpl.h>
25#include <pcmcia/ds.h>
26#include <linux/can.h>
27#include <linux/can/dev.h>
28#include "sja1000.h"
29
30MODULE_AUTHOR("Stephane Grosjean <s.grosjean@peak-system.com>");
31MODULE_DESCRIPTION("CAN driver for PEAK-System PCAN-PC Cards");
32MODULE_LICENSE("GPL v2");
33MODULE_SUPPORTED_DEVICE("PEAK PCAN-PC Card");
34
35
36#define PCC_NAME "peak_pcmcia"
37
38#define PCC_CHAN_MAX 2
39
40#define PCC_CAN_CLOCK (16000000 / 2)
41
42#define PCC_MANF_ID 0x0377
43#define PCC_CARD_ID 0x0001
44
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
49
50
51#define PCC_CCR 0x00
52#define PCC_CSR 0x02
53#define PCC_CPR 0x04
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
60
61
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
67
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
71
72
73#define PCC_LED(c) (1 << (c))
74#define PCC_LED_ALL (PCC_LED(0) | PCC_LED(1))
75
76
77#define PCC_LED_ON 0x00
78#define PCC_LED_FAST 0x01
79#define PCC_LED_SLOW 0x02
80#define PCC_LED_OFF 0x03
81
82#define PCC_CCR_LED_CHAN(s, c) ((s) << (((c) + 2) << 1))
83
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
92
93#define PCC_CCR_INIT (PCC_CCR_CLK_16 | PCC_CCR_RST_ALL | PCC_CCR_LED_OFF_ALL)
94
95
96#define PCC_CSR_SPI_BUSY 0x04
97
98
99#define PCC_SPI_MAX_BUSY_WAIT_MS 3
100
101
102
103#define PCC_WRITE_MAX_LOOP 1000
104
105
106#define PCC_ISR_MAX_LOOP 10
107
108
109
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
113#define PCC_EEP_RDSR 0x05
114#define PCC_EEP_WREN 0x06
115
116
117#define PCC_EEP_SR_WEN 0x02
118#define PCC_EEP_SR_WIP 0x01
119
120
121
122
123
124
125
126
127
128#define PCC_OCR (OCR_TX0_PUSHPULL | OCR_TX1_PUSHPULL)
129
130
131
132
133
134
135
136#define PCC_CDR (CDR_CBP | CDR_CLKOUT_MASK)
137
138struct pcan_channel {
139 struct net_device *netdev;
140 unsigned long prev_rx_bytes;
141 unsigned long prev_tx_bytes;
142};
143
144
145struct pcan_pccard {
146 struct pcmcia_device *pdev;
147 int chan_count;
148 struct pcan_channel channel[PCC_CHAN_MAX];
149 u8 ccr;
150 u8 fw_major;
151 u8 fw_minor;
152 void __iomem *ioport_addr;
153 struct timer_list led_timer;
154};
155
156static struct pcmcia_device_id pcan_table[] = {
157 PCMCIA_DEVICE_MANF_CARD(PCC_MANF_ID, PCC_CARD_ID),
158 PCMCIA_DEVICE_NULL,
159};
160
161MODULE_DEVICE_TABLE(pcmcia, pcan_table);
162
163static void pcan_set_leds(struct pcan_pccard *card, u8 mask, u8 state);
164
165
166
167
168static void pcan_start_led_timer(struct pcan_pccard *card)
169{
170 if (!timer_pending(&card->led_timer))
171 mod_timer(&card->led_timer, jiffies + HZ);
172}
173
174
175
176
177static void pcan_stop_led_timer(struct pcan_pccard *card)
178{
179 del_timer_sync(&card->led_timer);
180}
181
182
183
184
185static u8 pcan_read_canreg(const struct sja1000_priv *priv, int port)
186{
187 return ioread8(priv->reg_base + port);
188}
189
190
191
192
193static void pcan_write_canreg(const struct sja1000_priv *priv, int port, u8 v)
194{
195 struct pcan_pccard *card = priv->priv;
196 int c = (priv->reg_base - card->ioport_addr) / PCC_CHAN_SIZE;
197
198
199 if (port == REG_MOD)
200 switch (v) {
201 case MOD_RM:
202
203 pcan_set_leds(card, PCC_LED(c), PCC_LED_ON);
204 break;
205 case 0x00:
206
207 pcan_set_leds(card, PCC_LED(c), PCC_LED_SLOW);
208 pcan_start_led_timer(card);
209 break;
210 default:
211 break;
212 }
213
214 iowrite8(v, priv->reg_base + port);
215}
216
217
218
219
220static u8 pcan_read_reg(struct pcan_pccard *card, int port)
221{
222 return ioread8(card->ioport_addr + PCC_COMN_OFF + port);
223}
224
225
226
227
228static void pcan_write_reg(struct pcan_pccard *card, int port, u8 v)
229{
230
231 if (port == PCC_CCR) {
232 if (card->ccr == v)
233 return;
234 card->ccr = v;
235 }
236
237 iowrite8(v, card->ioport_addr + PCC_COMN_OFF + port);
238}
239
240
241
242
243
244static inline int pcan_pccard_present(struct pcan_pccard *card)
245{
246 return ((pcan_read_reg(card, PCC_FW_MAJOR) == card->fw_major) &&
247 (pcan_read_reg(card, PCC_FW_MINOR) == card->fw_minor));
248}
249
250
251
252
253static int pcan_wait_spi_busy(struct pcan_pccard *card)
254{
255 unsigned long timeout = jiffies +
256 msecs_to_jiffies(PCC_SPI_MAX_BUSY_WAIT_MS) + 1;
257
258
259 while (pcan_read_reg(card, PCC_CSR) & PCC_CSR_SPI_BUSY) {
260 if (time_after(jiffies, timeout))
261 return -EBUSY;
262 schedule();
263 }
264
265 return 0;
266}
267
268
269
270
271static int pcan_write_eeprom(struct pcan_pccard *card, u16 addr, u8 v)
272{
273 u8 status;
274 int err, i;
275
276
277 pcan_write_reg(card, PCC_SPI_IR, PCC_EEP_WREN);
278 err = pcan_wait_spi_busy(card);
279 if (err)
280 goto we_spi_err;
281
282
283 for (i = 0; i < PCC_WRITE_MAX_LOOP; i++) {
284
285 pcan_write_reg(card, PCC_SPI_IR, PCC_EEP_RDSR);
286 err = pcan_wait_spi_busy(card);
287 if (err)
288 goto we_spi_err;
289
290
291 status = pcan_read_reg(card, PCC_SPI_DIR);
292 if (status & PCC_EEP_SR_WEN)
293 break;
294 }
295
296 if (i >= PCC_WRITE_MAX_LOOP) {
297 dev_err(&card->pdev->dev,
298 "stop waiting to be allowed to write in eeprom\n");
299 return -EIO;
300 }
301
302
303 pcan_write_reg(card, PCC_SPI_ADR, addr & 0xff);
304 pcan_write_reg(card, PCC_SPI_DOR, v);
305
306
307
308
309
310 pcan_write_reg(card, PCC_SPI_IR, PCC_EEP_WRITE(addr));
311 err = pcan_wait_spi_busy(card);
312 if (err)
313 goto we_spi_err;
314
315
316 for (i = 0; i < PCC_WRITE_MAX_LOOP; i++) {
317
318 pcan_write_reg(card, PCC_SPI_IR, PCC_EEP_RDSR);
319 err = pcan_wait_spi_busy(card);
320 if (err)
321 goto we_spi_err;
322
323
324 status = pcan_read_reg(card, PCC_SPI_DIR);
325 if (!(status & PCC_EEP_SR_WIP))
326 break;
327 }
328
329 if (i >= PCC_WRITE_MAX_LOOP) {
330 dev_err(&card->pdev->dev,
331 "stop waiting for write in eeprom to complete\n");
332 return -EIO;
333 }
334
335
336 pcan_write_reg(card, PCC_SPI_IR, PCC_EEP_WRDI);
337 err = pcan_wait_spi_busy(card);
338 if (err)
339 goto we_spi_err;
340
341 return 0;
342
343we_spi_err:
344 dev_err(&card->pdev->dev,
345 "stop waiting (spi engine always busy) err %d\n", err);
346
347 return err;
348}
349
350static void pcan_set_leds(struct pcan_pccard *card, u8 led_mask, u8 state)
351{
352 u8 ccr = card->ccr;
353 int i;
354
355 for (i = 0; i < card->chan_count; i++)
356 if (led_mask & PCC_LED(i)) {
357
358 ccr &= ~PCC_CCR_LED_MASK_CHAN(i);
359
360 ccr |= PCC_CCR_LED_CHAN(state, i);
361 }
362
363
364 pcan_write_reg(card, PCC_CCR, ccr);
365}
366
367
368
369
370static inline void pcan_set_can_power(struct pcan_pccard *card, int onoff)
371{
372 int err;
373
374 err = pcan_write_eeprom(card, 0, !!onoff);
375 if (err)
376 dev_err(&card->pdev->dev,
377 "failed setting power %s to can connectors (err %d)\n",
378 (onoff) ? "on" : "off", err);
379}
380
381
382
383
384static void pcan_led_timer(unsigned long arg)
385{
386 struct pcan_pccard *card = (struct pcan_pccard *)arg;
387 struct net_device *netdev;
388 int i, up_count = 0;
389 u8 ccr;
390
391 ccr = card->ccr;
392 for (i = 0; i < card->chan_count; i++) {
393
394 ccr &= ~PCC_CCR_LED_MASK_CHAN(i);
395 ccr |= PCC_CCR_LED_ON_CHAN(i);
396
397 netdev = card->channel[i].netdev;
398 if (!netdev || !(netdev->flags & IFF_UP))
399 continue;
400
401 up_count++;
402
403
404 ccr &= ~PCC_CCR_LED_MASK_CHAN(i);
405 ccr |= PCC_CCR_LED_SLOW_CHAN(i);
406
407
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);
412 }
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);
417 }
418 }
419
420
421 pcan_write_reg(card, PCC_CCR, ccr);
422
423
424 if (up_count)
425 mod_timer(&card->led_timer, jiffies + HZ);
426}
427
428
429
430
431static irqreturn_t pcan_isr(int irq, void *dev_id)
432{
433 struct pcan_pccard *card = dev_id;
434 int irq_handled;
435
436
437 for (irq_handled = 0; irq_handled < PCC_ISR_MAX_LOOP; irq_handled++) {
438
439 int nothing_to_handle = 1;
440 int i;
441
442
443 for (i = 0; i < card->chan_count; i++) {
444 struct net_device *netdev;
445
446
447
448
449
450 if (!pcan_pccard_present(card)) {
451
452 return IRQ_NONE;
453 }
454
455
456
457
458
459 netdev = card->channel[i].netdev;
460 if (netdev &&
461 sja1000_interrupt(irq, netdev) == IRQ_HANDLED)
462 nothing_to_handle = 0;
463 }
464
465 if (nothing_to_handle)
466 break;
467 }
468
469 return (irq_handled) ? IRQ_HANDLED : IRQ_NONE;
470}
471
472
473
474
475static void pcan_free_channels(struct pcan_pccard *card)
476{
477 int i;
478 u8 led_mask = 0;
479
480 for (i = 0; i < card->chan_count; i++) {
481 struct net_device *netdev;
482 char name[IFNAMSIZ];
483
484 led_mask |= PCC_LED(i);
485
486 netdev = card->channel[i].netdev;
487 if (!netdev)
488 continue;
489
490 strncpy(name, netdev->name, IFNAMSIZ);
491
492 unregister_sja1000dev(netdev);
493
494 free_sja1000dev(netdev);
495
496 dev_info(&card->pdev->dev, "%s removed\n", name);
497 }
498
499
500 if (pcan_pccard_present(card)) {
501 pcan_set_leds(card, led_mask, PCC_LED_OFF);
502 pcan_set_can_power(card, 0);
503 }
504}
505
506
507
508
509static inline int pcan_channel_present(struct sja1000_priv *priv)
510{
511
512 pcan_write_canreg(priv, REG_MOD, 1);
513 pcan_write_canreg(priv, REG_CDR, CDR_PELICAN);
514
515
516 if (pcan_read_canreg(priv, REG_CDR) == CDR_PELICAN)
517 return 1;
518
519 return 0;
520}
521
522static int pcan_add_channels(struct pcan_pccard *card)
523{
524 struct pcmcia_device *pdev = card->pdev;
525 int i, err = 0;
526 u8 ccr = PCC_CCR_INIT;
527
528
529 card->ccr = ~ccr;
530 pcan_write_reg(card, PCC_CCR, ccr);
531
532
533 mdelay(2);
534
535 ccr &= ~PCC_CCR_RST_ALL;
536 pcan_write_reg(card, PCC_CCR, ccr);
537
538
539 for (i = 0; i < ARRAY_SIZE(card->channel); i++) {
540 struct net_device *netdev;
541 struct sja1000_priv *priv;
542
543 netdev = alloc_sja1000dev(0);
544 if (!netdev) {
545 err = -ENOMEM;
546 break;
547 }
548
549
550 priv = netdev_priv(netdev);
551 priv->priv = card;
552 SET_NETDEV_DEV(netdev, &pdev->dev);
553
554 priv->irq_flags = IRQF_SHARED;
555 netdev->irq = pdev->irq;
556 priv->reg_base = card->ioport_addr + PCC_CHAN_OFF(i);
557
558
559 if (!pcan_channel_present(priv)) {
560 dev_err(&pdev->dev, "channel %d not present\n", i);
561 free_sja1000dev(netdev);
562 continue;
563 }
564
565 priv->read_reg = pcan_read_canreg;
566 priv->write_reg = pcan_write_canreg;
567 priv->can.clock.freq = PCC_CAN_CLOCK;
568 priv->ocr = PCC_OCR;
569 priv->cdr = PCC_CDR;
570
571
572 if (i > 0)
573 priv->cdr |= CDR_CLK_OFF;
574
575 priv->flags |= SJA1000_CUSTOM_IRQ_HANDLER;
576
577
578 err = register_sja1000dev(netdev);
579 if (err) {
580 free_sja1000dev(netdev);
581 continue;
582 }
583
584 card->channel[i].netdev = netdev;
585 card->chan_count++;
586
587
588 ccr &= ~PCC_CCR_LED_OFF_CHAN(i);
589
590 dev_info(&pdev->dev,
591 "%s on channel %d at 0x%p irq %d\n",
592 netdev->name, i, priv->reg_base, pdev->irq);
593 }
594
595
596 pcan_write_reg(card, PCC_CCR, ccr);
597
598 return err;
599}
600
601static int pcan_conf_check(struct pcmcia_device *pdev, void *priv_data)
602{
603 pdev->resource[0]->flags &= ~IO_DATA_PATH_WIDTH;
604 pdev->resource[0]->flags |= IO_DATA_PATH_WIDTH_8;
605 pdev->io_lines = 10;
606
607
608 return pcmcia_request_io(pdev);
609}
610
611
612
613
614static void pcan_free(struct pcmcia_device *pdev)
615{
616 struct pcan_pccard *card = pdev->priv;
617
618 if (!card)
619 return;
620
621 free_irq(pdev->irq, card);
622 pcan_stop_led_timer(card);
623
624 pcan_free_channels(card);
625
626 ioport_unmap(card->ioport_addr);
627
628 kfree(card);
629 pdev->priv = NULL;
630}
631
632
633
634
635static int __devinit pcan_probe(struct pcmcia_device *pdev)
636{
637 struct pcan_pccard *card;
638 int err;
639
640 pdev->config_flags |= CONF_ENABLE_IRQ | CONF_AUTO_SET_IO;
641
642 err = pcmcia_loop_config(pdev, pcan_conf_check, NULL);
643 if (err) {
644 dev_err(&pdev->dev, "pcmcia_loop_config() error %d\n", err);
645 goto probe_err_1;
646 }
647
648 if (!pdev->irq) {
649 dev_err(&pdev->dev, "no irq assigned\n");
650 err = -ENODEV;
651 goto probe_err_1;
652 }
653
654 err = pcmcia_enable_device(pdev);
655 if (err) {
656 dev_err(&pdev->dev, "pcmcia_enable_device failed err=%d\n",
657 err);
658 goto probe_err_1;
659 }
660
661 card = kzalloc(sizeof(struct pcan_pccard), GFP_KERNEL);
662 if (!card) {
663 dev_err(&pdev->dev, "couldn't allocate card memory\n");
664 err = -ENOMEM;
665 goto probe_err_2;
666 }
667
668 card->pdev = pdev;
669 pdev->priv = card;
670
671
672 card->ioport_addr = ioport_map(pdev->resource[0]->start,
673 resource_size(pdev->resource[0]));
674 if (!card->ioport_addr) {
675 dev_err(&pdev->dev, "couldn't map io port into io memory\n");
676 err = -ENOMEM;
677 goto probe_err_3;
678 }
679 card->fw_major = pcan_read_reg(card, PCC_FW_MAJOR);
680 card->fw_minor = pcan_read_reg(card, PCC_FW_MINOR);
681
682
683 dev_info(&pdev->dev, "PEAK-System pcmcia card %s fw %d.%d\n",
684 pdev->prod_id[1] ? pdev->prod_id[1] : "PCAN-PC Card",
685 card->fw_major, card->fw_minor);
686
687
688 pcan_add_channels(card);
689 if (!card->chan_count) {
690 err = -ENOMEM;
691 goto probe_err_4;
692 }
693
694
695 init_timer(&card->led_timer);
696 card->led_timer.function = pcan_led_timer;
697 card->led_timer.data = (unsigned long)card;
698
699
700 err = request_irq(pdev->irq, &pcan_isr, IRQF_SHARED, PCC_NAME, card);
701 if (err) {
702 dev_err(&pdev->dev, "couldn't request irq%d\n", pdev->irq);
703 goto probe_err_5;
704 }
705
706
707 pcan_set_can_power(card, 1);
708
709 return 0;
710
711probe_err_5:
712
713 pcan_free_channels(card);
714
715probe_err_4:
716 ioport_unmap(card->ioport_addr);
717
718probe_err_3:
719 kfree(card);
720 pdev->priv = NULL;
721
722probe_err_2:
723 pcmcia_disable_device(pdev);
724
725probe_err_1:
726 return err;
727}
728
729
730
731
732static void pcan_remove(struct pcmcia_device *pdev)
733{
734 pcan_free(pdev);
735 pcmcia_disable_device(pdev);
736}
737
738static struct pcmcia_driver pcan_driver = {
739 .name = PCC_NAME,
740 .probe = pcan_probe,
741 .remove = pcan_remove,
742 .id_table = pcan_table,
743};
744
745static int __init pcan_init(void)
746{
747 return pcmcia_register_driver(&pcan_driver);
748}
749module_init(pcan_init);
750
751static void __exit pcan_exit(void)
752{
753 pcmcia_unregister_driver(&pcan_driver);
754}
755module_exit(pcan_exit);
756