19 #include <linux/kernel.h>
20 #include <linux/module.h>
22 #include <linux/netdevice.h>
24 #include <linux/pci.h>
26 #include <linux/i2c.h>
39 #define DRV_NAME "peak_pci"
41 struct peak_pciec_card;
49 #define PEAK_PCI_CAN_CLOCK (16000000 / 2)
51 #define PEAK_PCI_CDR (CDR_CBP | CDR_CLKOUT_MASK)
52 #define PEAK_PCI_OCR OCR_TX0_PUSHPULL
58 #define PITA_GPIOICR 0x18
59 #define PITA_MISC 0x1C
61 #define PEAK_PCI_CFG_SIZE 0x1000
62 #define PEAK_PCI_CHAN_SIZE 0x0400
64 #define PEAK_PCI_VENDOR_ID 0x001C
65 #define PEAK_PCI_DEVICE_ID 0x0001
66 #define PEAK_PCIEC_DEVICE_ID 0x0002
67 #define PEAK_PCIE_DEVICE_ID 0x0003
68 #define PEAK_CPCI_DEVICE_ID 0x0004
69 #define PEAK_MPCI_DEVICE_ID 0x0005
70 #define PEAK_PC_104P_DEVICE_ID 0x0006
71 #define PEAK_PCI_104E_DEVICE_ID 0x0007
72 #define PEAK_MPCIE_DEVICE_ID 0x0008
74 #define PEAK_PCI_CHAN_MAX 4
77 0x02, 0x01, 0x40, 0x80
88 #ifdef CONFIG_CAN_PEAK_PCIEC
96 #ifdef CONFIG_CAN_PEAK_PCIEC
102 #define PITA_GPOUT 0x18
103 #define PITA_GPIN 0x19
104 #define PITA_GPOEN 0x1A
107 #define PITA_GPIN_SCL 0x01
108 #define PITA_GPIN_SDA 0x04
110 #define PCA9553_1_SLAVEADDR (0xC4 >> 1)
121 #define PCA9553_ON PCA9553_LOW
122 #define PCA9553_OFF PCA9553_HIGHZ
123 #define PCA9553_SLOW PCA9553_PWM0
124 #define PCA9553_FAST PCA9553_PWM1
126 #define PCA9553_LED(c) (1 << (c))
127 #define PCA9553_LED_STATE(s, c) ((s) << ((c) << 1))
129 #define PCA9553_LED_ON(c) PCA9553_LED_STATE(PCA9553_ON, c)
130 #define PCA9553_LED_OFF(c) PCA9553_LED_STATE(PCA9553_OFF, c)
131 #define PCA9553_LED_SLOW(c) PCA9553_LED_STATE(PCA9553_SLOW, c)
132 #define PCA9553_LED_FAST(c) PCA9553_LED_STATE(PCA9553_FAST, c)
133 #define PCA9553_LED_MASK(c) PCA9553_LED_STATE(0x03, c)
135 #define PCA9553_LED_OFF_ALL (PCA9553_LED_OFF(0) | PCA9553_LED_OFF(1))
137 #define PCA9553_LS0_INIT 0x40
139 struct peak_pciec_chan {
141 unsigned long prev_rx_bytes;
142 unsigned long prev_tx_bytes;
145 struct peak_pciec_card {
162 static inline void pita_set_scl_highz(
struct peak_pciec_card *
card)
164 u8 gp_outen =
readb(card->cfg_base + PITA_GPOEN) & ~PITA_GPIN_SCL;
165 writeb(gp_outen, card->cfg_base + PITA_GPOEN);
168 static inline void pita_set_sda_highz(
struct peak_pciec_card *
card)
170 u8 gp_outen =
readb(card->cfg_base + PITA_GPOEN) & ~PITA_GPIN_SDA;
171 writeb(gp_outen, card->cfg_base + PITA_GPOEN);
174 static void peak_pciec_init_pita_gpio(
struct peak_pciec_card *
card)
177 pita_set_scl_highz(card);
178 pita_set_sda_highz(card);
181 static void pita_setsda(
void *
data,
int state)
183 struct peak_pciec_card *
card = (
struct peak_pciec_card *)data;
187 gp_out =
readb(card->cfg_base + PITA_GPOUT) & ~PITA_GPIN_SDA;
188 writeb(gp_out, card->cfg_base + PITA_GPOUT);
191 gp_outen =
readb(card->cfg_base + PITA_GPOEN);
193 gp_outen &= ~PITA_GPIN_SDA;
195 gp_outen |= PITA_GPIN_SDA;
197 writeb(gp_outen, card->cfg_base + PITA_GPOEN);
200 static void pita_setscl(
void *data,
int state)
202 struct peak_pciec_card *card = (
struct peak_pciec_card *)data;
206 gp_out =
readb(card->cfg_base + PITA_GPOUT) & ~PITA_GPIN_SCL;
207 writeb(gp_out, card->cfg_base + PITA_GPOUT);
210 gp_outen =
readb(card->cfg_base + PITA_GPOEN);
212 gp_outen &= ~PITA_GPIN_SCL;
214 gp_outen |= PITA_GPIN_SCL;
216 writeb(gp_outen, card->cfg_base + PITA_GPOEN);
219 static int pita_getsda(
void *data)
221 struct peak_pciec_card *card = (
struct peak_pciec_card *)data;
224 pita_set_sda_highz(card);
226 return (
readb(card->cfg_base + PITA_GPIN) & PITA_GPIN_SDA) ? 1 : 0;
229 static int pita_getscl(
void *data)
231 struct peak_pciec_card *card = (
struct peak_pciec_card *)data;
234 pita_set_scl_highz(card);
236 return (
readb(card->cfg_base + PITA_GPIN) & PITA_GPIN_SCL) ? 1 : 0;
242 static int peak_pciec_write_pca9553(
struct peak_pciec_card *card,
250 .addr = PCA9553_1_SLAVEADDR,
257 if ((offset == 5) && (data == card->led_cache))
265 card->led_cache =
data;
275 struct peak_pciec_card *card =
276 container_of(work,
struct peak_pciec_card, led_work.work);
278 u8 new_led = card->led_cache;
282 for (i = 0; i < card->chan_count; i++) {
284 new_led &= ~PCA9553_LED_MASK(i);
285 new_led |= PCA9553_LED_ON(i);
287 netdev = card->channel[
i].netdev;
294 new_led &= ~PCA9553_LED_MASK(i);
295 new_led |= PCA9553_LED_SLOW(i);
298 if (netdev->
stats.rx_bytes != card->channel[i].prev_rx_bytes) {
299 card->channel[
i].prev_rx_bytes = netdev->
stats.rx_bytes;
300 new_led &= ~PCA9553_LED_MASK(i);
301 new_led |= PCA9553_LED_FAST(i);
303 if (netdev->
stats.tx_bytes != card->channel[i].prev_tx_bytes) {
304 card->channel[
i].prev_tx_bytes = netdev->
stats.tx_bytes;
305 new_led &= ~PCA9553_LED_MASK(i);
306 new_led |= PCA9553_LED_FAST(i);
311 peak_pciec_write_pca9553(card, 5, new_led);
321 static void peak_pciec_set_leds(
struct peak_pciec_card *card,
u8 led_mask,
u8 s)
323 u8 new_led = card->led_cache;
327 for (i = 0; i < card->chan_count; i++)
328 if (led_mask & PCA9553_LED(i)) {
329 new_led &= ~PCA9553_LED_MASK(i);
330 new_led |= PCA9553_LED_STATE(s, i);
334 peak_pciec_write_pca9553(card, 5, new_led);
340 static void peak_pciec_start_led_work(
struct peak_pciec_card *card)
349 static void peak_pciec_stop_led_work(
struct peak_pciec_card *card)
357 static int peak_pciec_init_leds(
struct peak_pciec_card *card)
362 err = peak_pciec_write_pca9553(card, 1, 44 / 1);
367 err = peak_pciec_write_pca9553(card, 2, 0x80);
372 err = peak_pciec_write_pca9553(card, 3, 44 / 5);
377 err = peak_pciec_write_pca9553(card, 4, 0x80);
382 return peak_pciec_write_pca9553(card, 5, PCA9553_LS0_INIT);
388 static void peak_pciec_leds_exit(
struct peak_pciec_card *card)
391 peak_pciec_write_pca9553(card, 5, PCA9553_LED_OFF_ALL);
402 struct peak_pciec_card *card = chan->
pciec_card;
410 peak_pciec_set_leds(card, PCA9553_LED(c), PCA9553_ON);
414 peak_pciec_set_leds(card, PCA9553_LED(c), PCA9553_SLOW);
415 peak_pciec_start_led_work(card);
422 peak_pci_write_reg(priv, port, val);
427 .setscl = pita_setscl,
428 .getsda = pita_getsda,
429 .getscl = pita_getscl,
438 struct peak_pciec_card *
card;
453 card = kzalloc(
sizeof(
struct peak_pciec_card),
GFP_KERNEL);
456 "failed allocating memory for i2c chip\n");
464 card->led_chip.dev.parent = &pdev->
dev;
465 card->led_chip.algo_data = &card->i2c_bit;
466 strncpy(card->led_chip.name,
"peak_i2c",
467 sizeof(card->led_chip.name));
469 card->i2c_bit = peak_pciec_i2c_bit_ops;
470 card->i2c_bit.udelay = 10;
471 card->i2c_bit.timeout =
HZ;
472 card->i2c_bit.data =
card;
474 peak_pciec_init_pita_gpio(card);
479 goto pciec_init_err_1;
482 err = peak_pciec_init_leds(card);
484 dev_err(&pdev->
dev,
"leds hardware init failed\n");
485 goto pciec_init_err_2;
494 card->channel[card->chan_count++].netdev =
dev;
502 peak_pciec_init_pita_gpio(card);
508 static void peak_pciec_remove(
struct peak_pciec_card *card)
510 peak_pciec_stop_led_work(card);
511 peak_pciec_leds_exit(card);
513 peak_pciec_init_pita_gpio(card);
527 static inline void peak_pciec_remove(
struct peak_pciec_card *card)
532 static u8 peak_pci_read_reg(
const struct sja1000_priv *priv,
int port)
537 static void peak_pci_write_reg(
const struct sja1000_priv *priv,
543 static void peak_pci_post_irq(
const struct sja1000_priv *priv)
560 void __iomem *cfg_base, *reg_base;
570 goto failure_disable_pci;
572 err = pci_read_config_word(pdev, 0x2e, &sub_sys_id);
574 goto failure_release_regions;
576 dev_dbg(&pdev->
dev,
"probing device %04x:%04x:%04x\n",
579 err = pci_write_config_word(pdev, 0x44, 0);
581 goto failure_release_regions;
583 if (sub_sys_id >= 12)
585 else if (sub_sys_id >= 10)
587 else if (sub_sys_id >= 4)
594 dev_err(&pdev->
dev,
"failed to map PCI resource #0\n");
596 goto failure_release_regions;
601 dev_err(&pdev->
dev,
"failed to map PCI resource #1\n");
603 goto failure_unmap_cfg_base;
622 goto failure_remove_channels;
625 priv = netdev_priv(dev);
639 if (channels == 1 || i > 0)
652 chan->
prev_dev = pci_get_drvdata(pdev);
653 pci_set_drvdata(pdev, dev);
661 err = peak_pciec_probe(pdev, dev);
664 "failed to probe device (err %d)\n",
666 goto failure_free_dev;
672 dev_err(&pdev->
dev,
"failed to register device\n");
673 goto failure_free_dev;
677 "%s at reg_base=0x%p cfg_base=0x%p irq=%d\n",
687 pci_set_drvdata(pdev, chan->
prev_dev);
690 failure_remove_channels:
695 for (dev = pci_get_drvdata(pdev);
dev; dev = chan->
prev_dev) {
698 priv = netdev_priv(dev);
708 failure_unmap_cfg_base:
711 failure_release_regions:
722 struct net_device *dev = pci_get_drvdata(pdev);
744 priv = netdev_priv(dev);
753 pci_set_drvdata(pdev,
NULL);
758 .id_table = peak_pci_tbl,
759 .probe = peak_pci_probe,