17 #include <linux/kernel.h>
18 #include <linux/module.h>
20 #include <linux/netdevice.h>
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
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
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
99 #define PCC_SPI_MAX_BUSY_WAIT_MS 3
103 #define PCC_WRITE_MAX_LOOP 1000
106 #define PCC_ISR_MAX_LOOP 10
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
117 #define PCC_EEP_SR_WEN 0x02
118 #define PCC_EEP_SR_WIP 0x01
128 #define PCC_OCR (OCR_TX0_PUSHPULL | OCR_TX1_PUSHPULL)
136 #define PCC_CDR (CDR_CBP | CDR_CLKOUT_MASK)
177 static void pcan_stop_led_timer(
struct pcan_pccard *card)
208 pcan_start_led_timer(card);
228 static void pcan_write_reg(
struct pcan_pccard *card,
int port,
u8 v)
244 static inline int pcan_pccard_present(
struct pcan_pccard *card)
253 static int pcan_wait_spi_busy(
struct pcan_pccard *card)
278 err = pcan_wait_spi_busy(card);
286 err = pcan_wait_spi_busy(card);
296 if (i >= PCC_WRITE_MAX_LOOP) {
298 "stop waiting to be allowed to write in eeprom\n");
311 err = pcan_wait_spi_busy(card);
319 err = pcan_wait_spi_busy(card);
329 if (i >= PCC_WRITE_MAX_LOOP) {
331 "stop waiting for write in eeprom to complete\n");
337 err = pcan_wait_spi_busy(card);
345 "stop waiting (spi engine always busy) err %d\n", err);
364 pcan_write_reg(card,
PCC_CCR, ccr);
374 err = pcan_write_eeprom(card, 0, !!onoff);
377 "failed setting power %s to can connectors (err %d)\n",
378 (onoff) ?
"on" :
"off", err);
384 static void pcan_led_timer(
unsigned long arg)
408 if (netdev->
stats.rx_bytes != card->
channel[i].prev_rx_bytes) {
413 if (netdev->
stats.tx_bytes != card->
channel[i].prev_tx_bytes) {
421 pcan_write_reg(card,
PCC_CCR, ccr);
439 int nothing_to_handle = 1;
450 if (!pcan_pccard_present(card)) {
462 nothing_to_handle = 0;
465 if (nothing_to_handle)
475 static void pcan_free_channels(
struct pcan_pccard *card)
500 if (pcan_pccard_present(card)) {
502 pcan_set_can_power(card, 0);
509 static inline int pcan_channel_present(
struct sja1000_priv *priv)
512 pcan_write_canreg(priv,
REG_MOD, 1);
522 static int pcan_add_channels(
struct pcan_pccard *card)
524 struct pcmcia_device *
pdev = card->
pdev;
530 pcan_write_reg(card,
PCC_CCR, ccr);
536 pcan_write_reg(card,
PCC_CCR, ccr);
550 priv = netdev_priv(netdev);
555 netdev->
irq = pdev->irq;
559 if (!pcan_channel_present(priv)) {
560 dev_err(&pdev->dev,
"channel %d not present\n", i);
591 "%s on channel %d at 0x%p irq %d\n",
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;
614 static void pcan_free(
struct pcmcia_device *pdev)
622 pcan_stop_led_timer(card);
624 pcan_free_channels(card);
635 static int __devinit pcan_probe(
struct pcmcia_device *pdev)
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");
656 dev_err(&pdev->dev,
"pcmcia_enable_device failed err=%d\n",
663 dev_err(&pdev->dev,
"couldn't allocate card memory\n");
673 resource_size(pdev->resource[0]));
675 dev_err(&pdev->dev,
"couldn't map io port into io memory\n");
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",
688 pcan_add_channels(card);
696 card->
led_timer.function = pcan_led_timer;
702 dev_err(&pdev->dev,
"couldn't request irq%d\n", pdev->irq);
707 pcan_set_can_power(card, 1);
713 pcan_free_channels(card);
732 static void pcan_remove(
struct pcmcia_device *pdev)
738 static struct pcmcia_driver pcan_driver = {
741 .remove = pcan_remove,
742 .id_table = pcan_table,
745 static int __init pcan_init(
void)
751 static void __exit pcan_exit(
void)