62
63
#define PEAK_PCI_VENDOR_ID 0x001C /* The PCI device and vendor IDs */
63
64
#define PEAK_PCI_DEVICE_ID 0x0001 /* for PCI/PCIe slot cards */
65
static const u16 peak_pci_icr_masks[] = {0x02, 0x01, 0x40, 0x80};
65
#define PEAK_PCIEC_DEVICE_ID 0x0002 /* for ExpressCard slot cards */
66
#define PEAK_PCIE_DEVICE_ID 0x0003 /* for nextgen PCIe slot cards */
67
#define PEAK_MPCI_DEVICE_ID 0x0008 /* The miniPCI slot cards */
69
#define PEAK_PCI_CHAN_MAX 4
71
static const u16 peak_pci_icr_masks[PEAK_PCI_CHAN_MAX] = {
72
0x02, 0x01, 0x40, 0x80
67
75
static DEFINE_PCI_DEVICE_TABLE(peak_pci_tbl) = {
68
76
{PEAK_PCI_VENDOR_ID, PEAK_PCI_DEVICE_ID, PCI_ANY_ID, PCI_ANY_ID,},
77
{PEAK_PCI_VENDOR_ID, PEAK_PCIE_DEVICE_ID, PCI_ANY_ID, PCI_ANY_ID,},
78
{PEAK_PCI_VENDOR_ID, PEAK_MPCI_DEVICE_ID, PCI_ANY_ID, PCI_ANY_ID,},
79
#ifdef CONFIG_CAN_PEAK_PCIEC
80
{PEAK_PCI_VENDOR_ID, PEAK_PCIEC_DEVICE_ID, PCI_ANY_ID, PCI_ANY_ID,},
72
85
MODULE_DEVICE_TABLE(pci, peak_pci_tbl);
87
#ifdef CONFIG_CAN_PEAK_PCIEC
89
* PCAN-ExpressCard needs I2C bit-banging configuration option.
92
/* GPIOICR byte access offsets */
93
#define PITA_GPOUT 0x18 /* GPx output value */
94
#define PITA_GPIN 0x19 /* GPx input value */
95
#define PITA_GPOEN 0x1A /* configure GPx as ouput pin */
98
#define PITA_GPIN_SCL 0x01 /* Serial Clock Line */
99
#define PITA_GPIN_SDA 0x04 /* Serial DAta line */
101
#define PCA9553_1_SLAVEADDR (0xC4 >> 1)
103
/* PCA9553 LS0 fields values */
112
#define PCA9553_ON PCA9553_LOW
113
#define PCA9553_OFF PCA9553_HIGHZ
114
#define PCA9553_SLOW PCA9553_PWM0
115
#define PCA9553_FAST PCA9553_PWM1
117
#define PCA9553_LED(c) (1 << (c))
118
#define PCA9553_LED_STATE(s, c) ((s) << ((c) << 1))
120
#define PCA9553_LED_ON(c) PCA9553_LED_STATE(PCA9553_ON, c)
121
#define PCA9553_LED_OFF(c) PCA9553_LED_STATE(PCA9553_OFF, c)
122
#define PCA9553_LED_SLOW(c) PCA9553_LED_STATE(PCA9553_SLOW, c)
123
#define PCA9553_LED_FAST(c) PCA9553_LED_STATE(PCA9553_FAST, c)
124
#define PCA9553_LED_MASK(c) PCA9553_LED_STATE(0x03, c)
126
#define PCA9553_LED_OFF_ALL (PCA9553_LED_OFF(0) | PCA9553_LED_OFF(1))
128
#define PCA9553_LS0_INIT 0x40 /* initial value (!= from 0x00) */
130
struct peak_pciec_chan {
131
struct net_device *netdev;
132
unsigned long prev_rx_bytes;
133
unsigned long prev_tx_bytes;
136
struct peak_pciec_card {
137
void __iomem *cfg_base; /* Common for all channels */
138
void __iomem *reg_base; /* first channel base address */
139
u8 led_cache; /* leds state cache */
141
/* PCIExpressCard i2c data */
142
struct i2c_algo_bit_data i2c_bit;
143
struct i2c_adapter led_chip;
144
struct delayed_work led_work; /* led delayed work */
146
struct peak_pciec_chan channel[PEAK_PCI_CHAN_MAX];
149
/* "normal" pci register write callback is overloaded for leds control */
150
static void peak_pci_write_reg(const struct sja1000_priv *priv,
153
static inline void pita_set_scl_highz(struct peak_pciec_card *card)
155
u8 gp_outen = readb(card->cfg_base + PITA_GPOEN) & ~PITA_GPIN_SCL;
156
writeb(gp_outen, card->cfg_base + PITA_GPOEN);
159
static inline void pita_set_sda_highz(struct peak_pciec_card *card)
161
u8 gp_outen = readb(card->cfg_base + PITA_GPOEN) & ~PITA_GPIN_SDA;
162
writeb(gp_outen, card->cfg_base + PITA_GPOEN);
165
static void peak_pciec_init_pita_gpio(struct peak_pciec_card *card)
167
/* raise SCL & SDA GPIOs to high-Z */
168
pita_set_scl_highz(card);
169
pita_set_sda_highz(card);
172
static void pita_setsda(void *data, int state)
174
struct peak_pciec_card *card = (struct peak_pciec_card *)data;
177
/* set output sda always to 0 */
178
gp_out = readb(card->cfg_base + PITA_GPOUT) & ~PITA_GPIN_SDA;
179
writeb(gp_out, card->cfg_base + PITA_GPOUT);
181
/* control output sda with GPOEN */
182
gp_outen = readb(card->cfg_base + PITA_GPOEN);
184
gp_outen &= ~PITA_GPIN_SDA;
186
gp_outen |= PITA_GPIN_SDA;
188
writeb(gp_outen, card->cfg_base + PITA_GPOEN);
191
static void pita_setscl(void *data, int state)
193
struct peak_pciec_card *card = (struct peak_pciec_card *)data;
196
/* set output scl always to 0 */
197
gp_out = readb(card->cfg_base + PITA_GPOUT) & ~PITA_GPIN_SCL;
198
writeb(gp_out, card->cfg_base + PITA_GPOUT);
200
/* control output scl with GPOEN */
201
gp_outen = readb(card->cfg_base + PITA_GPOEN);
203
gp_outen &= ~PITA_GPIN_SCL;
205
gp_outen |= PITA_GPIN_SCL;
207
writeb(gp_outen, card->cfg_base + PITA_GPOEN);
210
static int pita_getsda(void *data)
212
struct peak_pciec_card *card = (struct peak_pciec_card *)data;
215
pita_set_sda_highz(card);
217
return (readb(card->cfg_base + PITA_GPIN) & PITA_GPIN_SDA) ? 1 : 0;
220
static int pita_getscl(void *data)
222
struct peak_pciec_card *card = (struct peak_pciec_card *)data;
225
pita_set_scl_highz(card);
227
return (readb(card->cfg_base + PITA_GPIN) & PITA_GPIN_SCL) ? 1 : 0;
231
* write commands to the LED chip though the I2C-bus of the PCAN-PCIeC
233
static int peak_pciec_write_pca9553(struct peak_pciec_card *card,
240
struct i2c_msg msg = {
241
.addr = PCA9553_1_SLAVEADDR,
248
if ((offset == 5) && (data == card->led_cache))
251
ret = i2c_transfer(&card->led_chip, &msg, 1);
256
card->led_cache = data;
262
* delayed work callback used to control the LEDs
264
static void peak_pciec_led_work(struct work_struct *work)
266
struct peak_pciec_card *card =
267
container_of(work, struct peak_pciec_card, led_work.work);
268
struct net_device *netdev;
269
u8 new_led = card->led_cache;
272
/* first check what is to do */
273
for (i = 0; i < card->chan_count; i++) {
274
/* default is: not configured */
275
new_led &= ~PCA9553_LED_MASK(i);
276
new_led |= PCA9553_LED_ON(i);
278
netdev = card->channel[i].netdev;
279
if (!netdev || !(netdev->flags & IFF_UP))
284
/* no activity (but configured) */
285
new_led &= ~PCA9553_LED_MASK(i);
286
new_led |= PCA9553_LED_SLOW(i);
288
/* if bytes counters changed, set fast blinking led */
289
if (netdev->stats.rx_bytes != card->channel[i].prev_rx_bytes) {
290
card->channel[i].prev_rx_bytes = netdev->stats.rx_bytes;
291
new_led &= ~PCA9553_LED_MASK(i);
292
new_led |= PCA9553_LED_FAST(i);
294
if (netdev->stats.tx_bytes != card->channel[i].prev_tx_bytes) {
295
card->channel[i].prev_tx_bytes = netdev->stats.tx_bytes;
296
new_led &= ~PCA9553_LED_MASK(i);
297
new_led |= PCA9553_LED_FAST(i);
301
/* check if LS0 settings changed, only update i2c if so */
302
peak_pciec_write_pca9553(card, 5, new_led);
304
/* restart timer (except if no more configured channels) */
306
schedule_delayed_work(&card->led_work, HZ);
310
* set LEDs blinking state
312
static void peak_pciec_set_leds(struct peak_pciec_card *card, u8 led_mask, u8 s)
314
u8 new_led = card->led_cache;
317
/* first check what is to do */
318
for (i = 0; i < card->chan_count; i++)
319
if (led_mask & PCA9553_LED(i)) {
320
new_led &= ~PCA9553_LED_MASK(i);
321
new_led |= PCA9553_LED_STATE(s, i);
324
/* check if LS0 settings changed, only update i2c if so */
325
peak_pciec_write_pca9553(card, 5, new_led);
329
* start one second delayed work to control LEDs
331
static void peak_pciec_start_led_work(struct peak_pciec_card *card)
333
if (!delayed_work_pending(&card->led_work))
334
schedule_delayed_work(&card->led_work, HZ);
338
* stop LEDs delayed work
340
static void peak_pciec_stop_led_work(struct peak_pciec_card *card)
342
cancel_delayed_work_sync(&card->led_work);
346
* initialize the PCA9553 4-bit I2C-bus LED chip
348
static int peak_pciec_init_leds(struct peak_pciec_card *card)
352
/* prescaler for frequency 0: "SLOW" = 1 Hz = "44" */
353
err = peak_pciec_write_pca9553(card, 1, 44 / 1);
357
/* duty cycle 0: 50% */
358
err = peak_pciec_write_pca9553(card, 2, 0x80);
362
/* prescaler for frequency 1: "FAST" = 5 Hz */
363
err = peak_pciec_write_pca9553(card, 3, 44 / 5);
367
/* duty cycle 1: 50% */
368
err = peak_pciec_write_pca9553(card, 4, 0x80);
372
/* switch LEDs to initial state */
373
return peak_pciec_write_pca9553(card, 5, PCA9553_LS0_INIT);
377
* restore LEDs state to off peak_pciec_leds_exit
379
static void peak_pciec_leds_exit(struct peak_pciec_card *card)
381
/* switch LEDs to off */
382
peak_pciec_write_pca9553(card, 5, PCA9553_LED_OFF_ALL);
386
* normal write sja1000 register method overloaded to catch when controller
387
* is started or stopped, to control leds
389
static void peak_pciec_write_reg(const struct sja1000_priv *priv,
392
struct peak_pci_chan *chan = priv->priv;
393
struct peak_pciec_card *card = chan->pciec_card;
394
int c = (priv->reg_base - card->reg_base) / PEAK_PCI_CHAN_SIZE;
396
/* sja1000 register changes control the leds state */
400
/* Reset Mode: set led on */
401
peak_pciec_set_leds(card, PCA9553_LED(c), PCA9553_ON);
404
/* Normal Mode: led slow blinking and start led timer */
405
peak_pciec_set_leds(card, PCA9553_LED(c), PCA9553_SLOW);
406
peak_pciec_start_led_work(card);
412
/* call base function */
413
peak_pci_write_reg(priv, port, val);
416
static struct i2c_algo_bit_data peak_pciec_i2c_bit_ops = {
417
.setsda = pita_setsda,
418
.setscl = pita_setscl,
419
.getsda = pita_getsda,
420
.getscl = pita_getscl,
425
static int peak_pciec_probe(struct pci_dev *pdev, struct net_device *dev)
427
struct sja1000_priv *priv = netdev_priv(dev);
428
struct peak_pci_chan *chan = priv->priv;
429
struct peak_pciec_card *card;
432
/* copy i2c object address from 1st channel */
433
if (chan->prev_dev) {
434
struct sja1000_priv *prev_priv = netdev_priv(chan->prev_dev);
435
struct peak_pci_chan *prev_chan = prev_priv->priv;
437
card = prev_chan->pciec_card;
441
/* channel is the first one: do the init part */
443
/* create the bit banging I2C adapter structure */
444
card = kzalloc(sizeof(struct peak_pciec_card), GFP_KERNEL);
447
"failed allocating memory for i2c chip\n");
451
card->cfg_base = chan->cfg_base;
452
card->reg_base = priv->reg_base;
454
card->led_chip.owner = THIS_MODULE;
455
card->led_chip.dev.parent = &pdev->dev;
456
card->led_chip.algo_data = &card->i2c_bit;
457
strncpy(card->led_chip.name, "peak_i2c",
458
sizeof(card->led_chip.name));
460
card->i2c_bit = peak_pciec_i2c_bit_ops;
461
card->i2c_bit.udelay = 10;
462
card->i2c_bit.timeout = HZ;
463
card->i2c_bit.data = card;
465
peak_pciec_init_pita_gpio(card);
467
err = i2c_bit_add_bus(&card->led_chip);
469
dev_err(&pdev->dev, "i2c init failed\n");
470
goto pciec_init_err_1;
473
err = peak_pciec_init_leds(card);
475
dev_err(&pdev->dev, "leds hardware init failed\n");
476
goto pciec_init_err_2;
479
INIT_DELAYED_WORK(&card->led_work, peak_pciec_led_work);
480
/* PCAN-ExpressCard needs its own callback for leds */
481
priv->write_reg = peak_pciec_write_reg;
484
chan->pciec_card = card;
485
card->channel[card->chan_count++].netdev = dev;
490
i2c_del_adapter(&card->led_chip);
493
peak_pciec_init_pita_gpio(card);
499
static void peak_pciec_remove(struct peak_pciec_card *card)
501
peak_pciec_stop_led_work(card);
502
peak_pciec_leds_exit(card);
503
i2c_del_adapter(&card->led_chip);
504
peak_pciec_init_pita_gpio(card);
508
#else /* CONFIG_CAN_PEAK_PCIEC */
511
* Placebo functions when PCAN-ExpressCard support is not selected
513
static inline int peak_pciec_probe(struct pci_dev *pdev, struct net_device *dev)
518
static inline void peak_pciec_remove(struct peak_pciec_card *card)
521
#endif /* CONFIG_CAN_PEAK_PCIEC */
74
523
static u8 peak_pci_read_reg(const struct sja1000_priv *priv, int port)
76
525
return readb(priv->reg_base + (port << 2));