xref: /openbmc/linux/drivers/net/can/sja1000/peak_pci.c (revision 1f0dee39e3cb272c32bb878d0a78d91300326d2b)
1aaa7cb26SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
238034518SWolfgang Grandegger /*
338034518SWolfgang Grandegger  * Copyright (C) 2007, 2011 Wolfgang Grandegger <wg@grandegger.com>
4e6d9c80bSStephane Grosjean  * Copyright (C) 2012 Stephane Grosjean <s.grosjean@peak-system.com>
538034518SWolfgang Grandegger  *
638034518SWolfgang Grandegger  * Derived from the PCAN project file driver/src/pcan_pci.c:
738034518SWolfgang Grandegger  *
838034518SWolfgang Grandegger  * Copyright (C) 2001-2006  PEAK System-Technik GmbH
938034518SWolfgang Grandegger  */
1038034518SWolfgang Grandegger 
1138034518SWolfgang Grandegger #include <linux/kernel.h>
1238034518SWolfgang Grandegger #include <linux/module.h>
1338034518SWolfgang Grandegger #include <linux/interrupt.h>
1438034518SWolfgang Grandegger #include <linux/netdevice.h>
1538034518SWolfgang Grandegger #include <linux/delay.h>
1638034518SWolfgang Grandegger #include <linux/pci.h>
1738034518SWolfgang Grandegger #include <linux/io.h>
18e6d9c80bSStephane Grosjean #include <linux/i2c.h>
19e6d9c80bSStephane Grosjean #include <linux/i2c-algo-bit.h>
2038034518SWolfgang Grandegger #include <linux/can.h>
2138034518SWolfgang Grandegger #include <linux/can/dev.h>
2238034518SWolfgang Grandegger 
2338034518SWolfgang Grandegger #include "sja1000.h"
2438034518SWolfgang Grandegger 
25fc09e367SStephane Grosjean MODULE_AUTHOR("Stephane Grosjean <s.grosjean@peak-system.com>");
26e6d9c80bSStephane Grosjean MODULE_DESCRIPTION("Socket-CAN driver for PEAK PCAN PCI family cards");
27e6d9c80bSStephane Grosjean MODULE_SUPPORTED_DEVICE("PEAK PCAN PCI/PCIe/PCIeC miniPCI CAN cards");
28fc09e367SStephane Grosjean MODULE_SUPPORTED_DEVICE("PEAK PCAN miniPCIe/cPCI PC/104+ PCI/104e CAN Cards");
2938034518SWolfgang Grandegger MODULE_LICENSE("GPL v2");
3038034518SWolfgang Grandegger 
3138034518SWolfgang Grandegger #define DRV_NAME  "peak_pci"
3238034518SWolfgang Grandegger 
33e6d9c80bSStephane Grosjean struct peak_pciec_card;
3438034518SWolfgang Grandegger struct peak_pci_chan {
3538034518SWolfgang Grandegger 	void __iomem *cfg_base;		/* Common for all channels */
3629830406SStephane Grosjean 	struct net_device *prev_dev;	/* Chain of network devices */
3738034518SWolfgang Grandegger 	u16 icr_mask;			/* Interrupt mask for fast ack */
38e6d9c80bSStephane Grosjean 	struct peak_pciec_card *pciec_card;	/* only for PCIeC LEDs */
3938034518SWolfgang Grandegger };
4038034518SWolfgang Grandegger 
4138034518SWolfgang Grandegger #define PEAK_PCI_CAN_CLOCK	(16000000 / 2)
4238034518SWolfgang Grandegger 
4338034518SWolfgang Grandegger #define PEAK_PCI_CDR		(CDR_CBP | CDR_CLKOUT_MASK)
4438034518SWolfgang Grandegger #define PEAK_PCI_OCR		OCR_TX0_PUSHPULL
4538034518SWolfgang Grandegger 
4638034518SWolfgang Grandegger /*
4738034518SWolfgang Grandegger  * Important PITA registers
4838034518SWolfgang Grandegger  */
4938034518SWolfgang Grandegger #define PITA_ICR		0x00	/* Interrupt control register */
5038034518SWolfgang Grandegger #define PITA_GPIOICR		0x18	/* GPIO interface control register */
5138034518SWolfgang Grandegger #define PITA_MISC		0x1C	/* Miscellaneous register */
5238034518SWolfgang Grandegger 
5338034518SWolfgang Grandegger #define PEAK_PCI_CFG_SIZE	0x1000	/* Size of the config PCI bar */
5438034518SWolfgang Grandegger #define PEAK_PCI_CHAN_SIZE	0x0400	/* Size used by the channel */
5538034518SWolfgang Grandegger 
5638034518SWolfgang Grandegger #define PEAK_PCI_VENDOR_ID	0x001C	/* The PCI device and vendor IDs */
5738034518SWolfgang Grandegger #define PEAK_PCI_DEVICE_ID	0x0001	/* for PCI/PCIe slot cards */
58e6d9c80bSStephane Grosjean #define PEAK_PCIEC_DEVICE_ID	0x0002	/* for ExpressCard slot cards */
59e6d9c80bSStephane Grosjean #define PEAK_PCIE_DEVICE_ID	0x0003	/* for nextgen PCIe slot cards */
60fc09e367SStephane Grosjean #define PEAK_CPCI_DEVICE_ID	0x0004	/* for nextgen cPCI slot cards */
61fc09e367SStephane Grosjean #define PEAK_MPCI_DEVICE_ID	0x0005	/* for nextgen miniPCI slot cards */
62fc09e367SStephane Grosjean #define PEAK_PC_104P_DEVICE_ID	0x0006	/* PCAN-PC/104+ cards */
63fc09e367SStephane Grosjean #define PEAK_PCI_104E_DEVICE_ID	0x0007	/* PCAN-PCI/104 Express cards */
64fc09e367SStephane Grosjean #define PEAK_MPCIE_DEVICE_ID	0x0008	/* The miniPCIe slot cards */
654be0015cSOliver Hartkopp #define PEAK_PCIE_OEM_ID	0x0009	/* PCAN-PCI Express OEM */
664be0015cSOliver Hartkopp #define PEAK_PCIEC34_DEVICE_ID	0x000A	/* PCAN-PCI Express 34 (one channel) */
6738034518SWolfgang Grandegger 
68e6d9c80bSStephane Grosjean #define PEAK_PCI_CHAN_MAX	4
69e6d9c80bSStephane Grosjean 
70e6d9c80bSStephane Grosjean static const u16 peak_pci_icr_masks[PEAK_PCI_CHAN_MAX] = {
71e6d9c80bSStephane Grosjean 	0x02, 0x01, 0x40, 0x80
72e6d9c80bSStephane Grosjean };
7338034518SWolfgang Grandegger 
749baa3c34SBenoit Taine static const struct pci_device_id peak_pci_tbl[] = {
7538034518SWolfgang Grandegger 	{PEAK_PCI_VENDOR_ID, PEAK_PCI_DEVICE_ID, PCI_ANY_ID, PCI_ANY_ID,},
76e6d9c80bSStephane Grosjean 	{PEAK_PCI_VENDOR_ID, PEAK_PCIE_DEVICE_ID, PCI_ANY_ID, PCI_ANY_ID,},
77e6d9c80bSStephane Grosjean 	{PEAK_PCI_VENDOR_ID, PEAK_MPCI_DEVICE_ID, PCI_ANY_ID, PCI_ANY_ID,},
78fc09e367SStephane Grosjean 	{PEAK_PCI_VENDOR_ID, PEAK_MPCIE_DEVICE_ID, PCI_ANY_ID, PCI_ANY_ID,},
79fc09e367SStephane Grosjean 	{PEAK_PCI_VENDOR_ID, PEAK_PC_104P_DEVICE_ID, PCI_ANY_ID, PCI_ANY_ID,},
80fc09e367SStephane Grosjean 	{PEAK_PCI_VENDOR_ID, PEAK_PCI_104E_DEVICE_ID, PCI_ANY_ID, PCI_ANY_ID,},
81fc09e367SStephane Grosjean 	{PEAK_PCI_VENDOR_ID, PEAK_CPCI_DEVICE_ID, PCI_ANY_ID, PCI_ANY_ID,},
827253054eSStephane Grosjean 	{PEAK_PCI_VENDOR_ID, PEAK_PCIE_OEM_ID, PCI_ANY_ID, PCI_ANY_ID,},
83e6d9c80bSStephane Grosjean #ifdef CONFIG_CAN_PEAK_PCIEC
84e6d9c80bSStephane Grosjean 	{PEAK_PCI_VENDOR_ID, PEAK_PCIEC_DEVICE_ID, PCI_ANY_ID, PCI_ANY_ID,},
854be0015cSOliver Hartkopp 	{PEAK_PCI_VENDOR_ID, PEAK_PCIEC34_DEVICE_ID, PCI_ANY_ID, PCI_ANY_ID,},
86e6d9c80bSStephane Grosjean #endif
8738034518SWolfgang Grandegger 	{0,}
8838034518SWolfgang Grandegger };
8938034518SWolfgang Grandegger 
9038034518SWolfgang Grandegger MODULE_DEVICE_TABLE(pci, peak_pci_tbl);
9138034518SWolfgang Grandegger 
92e6d9c80bSStephane Grosjean #ifdef CONFIG_CAN_PEAK_PCIEC
93e6d9c80bSStephane Grosjean /*
94e6d9c80bSStephane Grosjean  * PCAN-ExpressCard needs I2C bit-banging configuration option.
95e6d9c80bSStephane Grosjean  */
96e6d9c80bSStephane Grosjean 
97e6d9c80bSStephane Grosjean /* GPIOICR byte access offsets */
98e6d9c80bSStephane Grosjean #define PITA_GPOUT		0x18	/* GPx output value */
99e6d9c80bSStephane Grosjean #define PITA_GPIN		0x19	/* GPx input value */
100e6d9c80bSStephane Grosjean #define PITA_GPOEN		0x1A	/* configure GPx as ouput pin */
101e6d9c80bSStephane Grosjean 
102e6d9c80bSStephane Grosjean /* I2C GP bits */
103e6d9c80bSStephane Grosjean #define PITA_GPIN_SCL		0x01	/* Serial Clock Line */
104e6d9c80bSStephane Grosjean #define PITA_GPIN_SDA		0x04	/* Serial DAta line */
105e6d9c80bSStephane Grosjean 
106e6d9c80bSStephane Grosjean #define PCA9553_1_SLAVEADDR	(0xC4 >> 1)
107e6d9c80bSStephane Grosjean 
108e6d9c80bSStephane Grosjean /* PCA9553 LS0 fields values */
109e6d9c80bSStephane Grosjean enum {
110e6d9c80bSStephane Grosjean 	PCA9553_LOW,
111e6d9c80bSStephane Grosjean 	PCA9553_HIGHZ,
112e6d9c80bSStephane Grosjean 	PCA9553_PWM0,
113e6d9c80bSStephane Grosjean 	PCA9553_PWM1
114e6d9c80bSStephane Grosjean };
115e6d9c80bSStephane Grosjean 
116e6d9c80bSStephane Grosjean /* LEDs control */
117e6d9c80bSStephane Grosjean #define PCA9553_ON		PCA9553_LOW
118e6d9c80bSStephane Grosjean #define PCA9553_OFF		PCA9553_HIGHZ
119e6d9c80bSStephane Grosjean #define PCA9553_SLOW		PCA9553_PWM0
120e6d9c80bSStephane Grosjean #define PCA9553_FAST		PCA9553_PWM1
121e6d9c80bSStephane Grosjean 
122e6d9c80bSStephane Grosjean #define PCA9553_LED(c)		(1 << (c))
123e6d9c80bSStephane Grosjean #define PCA9553_LED_STATE(s, c)	((s) << ((c) << 1))
124e6d9c80bSStephane Grosjean 
125e6d9c80bSStephane Grosjean #define PCA9553_LED_ON(c)	PCA9553_LED_STATE(PCA9553_ON, c)
126e6d9c80bSStephane Grosjean #define PCA9553_LED_OFF(c)	PCA9553_LED_STATE(PCA9553_OFF, c)
127e6d9c80bSStephane Grosjean #define PCA9553_LED_SLOW(c)	PCA9553_LED_STATE(PCA9553_SLOW, c)
128e6d9c80bSStephane Grosjean #define PCA9553_LED_FAST(c)	PCA9553_LED_STATE(PCA9553_FAST, c)
129e6d9c80bSStephane Grosjean #define PCA9553_LED_MASK(c)	PCA9553_LED_STATE(0x03, c)
130e6d9c80bSStephane Grosjean 
131e6d9c80bSStephane Grosjean #define PCA9553_LED_OFF_ALL	(PCA9553_LED_OFF(0) | PCA9553_LED_OFF(1))
132e6d9c80bSStephane Grosjean 
133e6d9c80bSStephane Grosjean #define PCA9553_LS0_INIT	0x40 /* initial value (!= from 0x00) */
134e6d9c80bSStephane Grosjean 
135e6d9c80bSStephane Grosjean struct peak_pciec_chan {
136e6d9c80bSStephane Grosjean 	struct net_device *netdev;
137e6d9c80bSStephane Grosjean 	unsigned long prev_rx_bytes;
138e6d9c80bSStephane Grosjean 	unsigned long prev_tx_bytes;
139e6d9c80bSStephane Grosjean };
140e6d9c80bSStephane Grosjean 
141e6d9c80bSStephane Grosjean struct peak_pciec_card {
142e6d9c80bSStephane Grosjean 	void __iomem *cfg_base;		/* Common for all channels */
143e6d9c80bSStephane Grosjean 	void __iomem *reg_base;		/* first channel base address */
144e6d9c80bSStephane Grosjean 	u8 led_cache;			/* leds state cache */
145e6d9c80bSStephane Grosjean 
146e6d9c80bSStephane Grosjean 	/* PCIExpressCard i2c data */
147e6d9c80bSStephane Grosjean 	struct i2c_algo_bit_data i2c_bit;
148e6d9c80bSStephane Grosjean 	struct i2c_adapter led_chip;
149e6d9c80bSStephane Grosjean 	struct delayed_work led_work;	/* led delayed work */
150e6d9c80bSStephane Grosjean 	int chan_count;
151e6d9c80bSStephane Grosjean 	struct peak_pciec_chan channel[PEAK_PCI_CHAN_MAX];
152e6d9c80bSStephane Grosjean };
153e6d9c80bSStephane Grosjean 
154e6d9c80bSStephane Grosjean /* "normal" pci register write callback is overloaded for leds control */
155e6d9c80bSStephane Grosjean static void peak_pci_write_reg(const struct sja1000_priv *priv,
156e6d9c80bSStephane Grosjean 			       int port, u8 val);
157e6d9c80bSStephane Grosjean 
158e6d9c80bSStephane Grosjean static inline void pita_set_scl_highz(struct peak_pciec_card *card)
159e6d9c80bSStephane Grosjean {
160e6d9c80bSStephane Grosjean 	u8 gp_outen = readb(card->cfg_base + PITA_GPOEN) & ~PITA_GPIN_SCL;
161e6d9c80bSStephane Grosjean 	writeb(gp_outen, card->cfg_base + PITA_GPOEN);
162e6d9c80bSStephane Grosjean }
163e6d9c80bSStephane Grosjean 
164e6d9c80bSStephane Grosjean static inline void pita_set_sda_highz(struct peak_pciec_card *card)
165e6d9c80bSStephane Grosjean {
166e6d9c80bSStephane Grosjean 	u8 gp_outen = readb(card->cfg_base + PITA_GPOEN) & ~PITA_GPIN_SDA;
167e6d9c80bSStephane Grosjean 	writeb(gp_outen, card->cfg_base + PITA_GPOEN);
168e6d9c80bSStephane Grosjean }
169e6d9c80bSStephane Grosjean 
170e6d9c80bSStephane Grosjean static void peak_pciec_init_pita_gpio(struct peak_pciec_card *card)
171e6d9c80bSStephane Grosjean {
172e6d9c80bSStephane Grosjean 	/* raise SCL & SDA GPIOs to high-Z */
173e6d9c80bSStephane Grosjean 	pita_set_scl_highz(card);
174e6d9c80bSStephane Grosjean 	pita_set_sda_highz(card);
175e6d9c80bSStephane Grosjean }
176e6d9c80bSStephane Grosjean 
177e6d9c80bSStephane Grosjean static void pita_setsda(void *data, int state)
178e6d9c80bSStephane Grosjean {
179e6d9c80bSStephane Grosjean 	struct peak_pciec_card *card = (struct peak_pciec_card *)data;
180e6d9c80bSStephane Grosjean 	u8 gp_out, gp_outen;
181e6d9c80bSStephane Grosjean 
182e6d9c80bSStephane Grosjean 	/* set output sda always to 0 */
183e6d9c80bSStephane Grosjean 	gp_out = readb(card->cfg_base + PITA_GPOUT) & ~PITA_GPIN_SDA;
184e6d9c80bSStephane Grosjean 	writeb(gp_out, card->cfg_base + PITA_GPOUT);
185e6d9c80bSStephane Grosjean 
186e6d9c80bSStephane Grosjean 	/* control output sda with GPOEN */
187e6d9c80bSStephane Grosjean 	gp_outen = readb(card->cfg_base + PITA_GPOEN);
188e6d9c80bSStephane Grosjean 	if (state)
189e6d9c80bSStephane Grosjean 		gp_outen &= ~PITA_GPIN_SDA;
190e6d9c80bSStephane Grosjean 	else
191e6d9c80bSStephane Grosjean 		gp_outen |= PITA_GPIN_SDA;
192e6d9c80bSStephane Grosjean 
193e6d9c80bSStephane Grosjean 	writeb(gp_outen, card->cfg_base + PITA_GPOEN);
194e6d9c80bSStephane Grosjean }
195e6d9c80bSStephane Grosjean 
196e6d9c80bSStephane Grosjean static void pita_setscl(void *data, int state)
197e6d9c80bSStephane Grosjean {
198e6d9c80bSStephane Grosjean 	struct peak_pciec_card *card = (struct peak_pciec_card *)data;
199e6d9c80bSStephane Grosjean 	u8 gp_out, gp_outen;
200e6d9c80bSStephane Grosjean 
201e6d9c80bSStephane Grosjean 	/* set output scl always to 0 */
202e6d9c80bSStephane Grosjean 	gp_out = readb(card->cfg_base + PITA_GPOUT) & ~PITA_GPIN_SCL;
203e6d9c80bSStephane Grosjean 	writeb(gp_out, card->cfg_base + PITA_GPOUT);
204e6d9c80bSStephane Grosjean 
205e6d9c80bSStephane Grosjean 	/* control output scl with GPOEN */
206e6d9c80bSStephane Grosjean 	gp_outen = readb(card->cfg_base + PITA_GPOEN);
207e6d9c80bSStephane Grosjean 	if (state)
208e6d9c80bSStephane Grosjean 		gp_outen &= ~PITA_GPIN_SCL;
209e6d9c80bSStephane Grosjean 	else
210e6d9c80bSStephane Grosjean 		gp_outen |= PITA_GPIN_SCL;
211e6d9c80bSStephane Grosjean 
212e6d9c80bSStephane Grosjean 	writeb(gp_outen, card->cfg_base + PITA_GPOEN);
213e6d9c80bSStephane Grosjean }
214e6d9c80bSStephane Grosjean 
215e6d9c80bSStephane Grosjean static int pita_getsda(void *data)
216e6d9c80bSStephane Grosjean {
217e6d9c80bSStephane Grosjean 	struct peak_pciec_card *card = (struct peak_pciec_card *)data;
218e6d9c80bSStephane Grosjean 
219e6d9c80bSStephane Grosjean 	/* set tristate */
220e6d9c80bSStephane Grosjean 	pita_set_sda_highz(card);
221e6d9c80bSStephane Grosjean 
222e6d9c80bSStephane Grosjean 	return (readb(card->cfg_base + PITA_GPIN) & PITA_GPIN_SDA) ? 1 : 0;
223e6d9c80bSStephane Grosjean }
224e6d9c80bSStephane Grosjean 
225e6d9c80bSStephane Grosjean static int pita_getscl(void *data)
226e6d9c80bSStephane Grosjean {
227e6d9c80bSStephane Grosjean 	struct peak_pciec_card *card = (struct peak_pciec_card *)data;
228e6d9c80bSStephane Grosjean 
229e6d9c80bSStephane Grosjean 	/* set tristate */
230e6d9c80bSStephane Grosjean 	pita_set_scl_highz(card);
231e6d9c80bSStephane Grosjean 
232e6d9c80bSStephane Grosjean 	return (readb(card->cfg_base + PITA_GPIN) & PITA_GPIN_SCL) ? 1 : 0;
233e6d9c80bSStephane Grosjean }
234e6d9c80bSStephane Grosjean 
235e6d9c80bSStephane Grosjean /*
236e6d9c80bSStephane Grosjean  * write commands to the LED chip though the I2C-bus of the PCAN-PCIeC
237e6d9c80bSStephane Grosjean  */
238e6d9c80bSStephane Grosjean static int peak_pciec_write_pca9553(struct peak_pciec_card *card,
239e6d9c80bSStephane Grosjean 				    u8 offset, u8 data)
240e6d9c80bSStephane Grosjean {
241e6d9c80bSStephane Grosjean 	u8 buffer[2] = {
242e6d9c80bSStephane Grosjean 		offset,
243e6d9c80bSStephane Grosjean 		data
244e6d9c80bSStephane Grosjean 	};
245e6d9c80bSStephane Grosjean 	struct i2c_msg msg = {
246e6d9c80bSStephane Grosjean 		.addr = PCA9553_1_SLAVEADDR,
247e6d9c80bSStephane Grosjean 		.len = 2,
248e6d9c80bSStephane Grosjean 		.buf = buffer,
249e6d9c80bSStephane Grosjean 	};
250e6d9c80bSStephane Grosjean 	int ret;
251e6d9c80bSStephane Grosjean 
252e6d9c80bSStephane Grosjean 	/* cache led mask */
253e6d9c80bSStephane Grosjean 	if ((offset == 5) && (data == card->led_cache))
254e6d9c80bSStephane Grosjean 		return 0;
255e6d9c80bSStephane Grosjean 
256e6d9c80bSStephane Grosjean 	ret = i2c_transfer(&card->led_chip, &msg, 1);
257e6d9c80bSStephane Grosjean 	if (ret < 0)
258e6d9c80bSStephane Grosjean 		return ret;
259e6d9c80bSStephane Grosjean 
260e6d9c80bSStephane Grosjean 	if (offset == 5)
261e6d9c80bSStephane Grosjean 		card->led_cache = data;
262e6d9c80bSStephane Grosjean 
263e6d9c80bSStephane Grosjean 	return 0;
264e6d9c80bSStephane Grosjean }
265e6d9c80bSStephane Grosjean 
266e6d9c80bSStephane Grosjean /*
267e6d9c80bSStephane Grosjean  * delayed work callback used to control the LEDs
268e6d9c80bSStephane Grosjean  */
269e6d9c80bSStephane Grosjean static void peak_pciec_led_work(struct work_struct *work)
270e6d9c80bSStephane Grosjean {
271e6d9c80bSStephane Grosjean 	struct peak_pciec_card *card =
272e6d9c80bSStephane Grosjean 		container_of(work, struct peak_pciec_card, led_work.work);
273e6d9c80bSStephane Grosjean 	struct net_device *netdev;
274e6d9c80bSStephane Grosjean 	u8 new_led = card->led_cache;
275e6d9c80bSStephane Grosjean 	int i, up_count = 0;
276e6d9c80bSStephane Grosjean 
277e6d9c80bSStephane Grosjean 	/* first check what is to do */
278e6d9c80bSStephane Grosjean 	for (i = 0; i < card->chan_count; i++) {
279e6d9c80bSStephane Grosjean 		/* default is: not configured */
280e6d9c80bSStephane Grosjean 		new_led &= ~PCA9553_LED_MASK(i);
281e6d9c80bSStephane Grosjean 		new_led |= PCA9553_LED_ON(i);
282e6d9c80bSStephane Grosjean 
283e6d9c80bSStephane Grosjean 		netdev = card->channel[i].netdev;
284e6d9c80bSStephane Grosjean 		if (!netdev || !(netdev->flags & IFF_UP))
285e6d9c80bSStephane Grosjean 			continue;
286e6d9c80bSStephane Grosjean 
287e6d9c80bSStephane Grosjean 		up_count++;
288e6d9c80bSStephane Grosjean 
289e6d9c80bSStephane Grosjean 		/* no activity (but configured) */
290e6d9c80bSStephane Grosjean 		new_led &= ~PCA9553_LED_MASK(i);
291e6d9c80bSStephane Grosjean 		new_led |= PCA9553_LED_SLOW(i);
292e6d9c80bSStephane Grosjean 
293e6d9c80bSStephane Grosjean 		/* if bytes counters changed, set fast blinking led */
294e6d9c80bSStephane Grosjean 		if (netdev->stats.rx_bytes != card->channel[i].prev_rx_bytes) {
295e6d9c80bSStephane Grosjean 			card->channel[i].prev_rx_bytes = netdev->stats.rx_bytes;
296e6d9c80bSStephane Grosjean 			new_led &= ~PCA9553_LED_MASK(i);
297e6d9c80bSStephane Grosjean 			new_led |= PCA9553_LED_FAST(i);
298e6d9c80bSStephane Grosjean 		}
299e6d9c80bSStephane Grosjean 		if (netdev->stats.tx_bytes != card->channel[i].prev_tx_bytes) {
300e6d9c80bSStephane Grosjean 			card->channel[i].prev_tx_bytes = netdev->stats.tx_bytes;
301e6d9c80bSStephane Grosjean 			new_led &= ~PCA9553_LED_MASK(i);
302e6d9c80bSStephane Grosjean 			new_led |= PCA9553_LED_FAST(i);
303e6d9c80bSStephane Grosjean 		}
304e6d9c80bSStephane Grosjean 	}
305e6d9c80bSStephane Grosjean 
306e6d9c80bSStephane Grosjean 	/* check if LS0 settings changed, only update i2c if so */
307e6d9c80bSStephane Grosjean 	peak_pciec_write_pca9553(card, 5, new_led);
308e6d9c80bSStephane Grosjean 
309e6d9c80bSStephane Grosjean 	/* restart timer (except if no more configured channels) */
310e6d9c80bSStephane Grosjean 	if (up_count)
311e6d9c80bSStephane Grosjean 		schedule_delayed_work(&card->led_work, HZ);
312e6d9c80bSStephane Grosjean }
313e6d9c80bSStephane Grosjean 
314e6d9c80bSStephane Grosjean /*
315e6d9c80bSStephane Grosjean  * set LEDs blinking state
316e6d9c80bSStephane Grosjean  */
317e6d9c80bSStephane Grosjean static void peak_pciec_set_leds(struct peak_pciec_card *card, u8 led_mask, u8 s)
318e6d9c80bSStephane Grosjean {
319e6d9c80bSStephane Grosjean 	u8 new_led = card->led_cache;
320e6d9c80bSStephane Grosjean 	int i;
321e6d9c80bSStephane Grosjean 
322e6d9c80bSStephane Grosjean 	/* first check what is to do */
323e6d9c80bSStephane Grosjean 	for (i = 0; i < card->chan_count; i++)
324e6d9c80bSStephane Grosjean 		if (led_mask & PCA9553_LED(i)) {
325e6d9c80bSStephane Grosjean 			new_led &= ~PCA9553_LED_MASK(i);
326e6d9c80bSStephane Grosjean 			new_led |= PCA9553_LED_STATE(s, i);
327e6d9c80bSStephane Grosjean 		}
328e6d9c80bSStephane Grosjean 
329e6d9c80bSStephane Grosjean 	/* check if LS0 settings changed, only update i2c if so */
330e6d9c80bSStephane Grosjean 	peak_pciec_write_pca9553(card, 5, new_led);
331e6d9c80bSStephane Grosjean }
332e6d9c80bSStephane Grosjean 
333e6d9c80bSStephane Grosjean /*
334e6d9c80bSStephane Grosjean  * start one second delayed work to control LEDs
335e6d9c80bSStephane Grosjean  */
336e6d9c80bSStephane Grosjean static void peak_pciec_start_led_work(struct peak_pciec_card *card)
337e6d9c80bSStephane Grosjean {
338e6d9c80bSStephane Grosjean 	schedule_delayed_work(&card->led_work, HZ);
339e6d9c80bSStephane Grosjean }
340e6d9c80bSStephane Grosjean 
341e6d9c80bSStephane Grosjean /*
342e6d9c80bSStephane Grosjean  * stop LEDs delayed work
343e6d9c80bSStephane Grosjean  */
344e6d9c80bSStephane Grosjean static void peak_pciec_stop_led_work(struct peak_pciec_card *card)
345e6d9c80bSStephane Grosjean {
346e6d9c80bSStephane Grosjean 	cancel_delayed_work_sync(&card->led_work);
347e6d9c80bSStephane Grosjean }
348e6d9c80bSStephane Grosjean 
349e6d9c80bSStephane Grosjean /*
350e6d9c80bSStephane Grosjean  * initialize the PCA9553 4-bit I2C-bus LED chip
351e6d9c80bSStephane Grosjean  */
352e6d9c80bSStephane Grosjean static int peak_pciec_init_leds(struct peak_pciec_card *card)
353e6d9c80bSStephane Grosjean {
354e6d9c80bSStephane Grosjean 	int err;
355e6d9c80bSStephane Grosjean 
356e6d9c80bSStephane Grosjean 	/* prescaler for frequency 0: "SLOW" = 1 Hz = "44" */
357e6d9c80bSStephane Grosjean 	err = peak_pciec_write_pca9553(card, 1, 44 / 1);
358e6d9c80bSStephane Grosjean 	if (err)
359e6d9c80bSStephane Grosjean 		return err;
360e6d9c80bSStephane Grosjean 
361e6d9c80bSStephane Grosjean 	/* duty cycle 0: 50% */
362e6d9c80bSStephane Grosjean 	err = peak_pciec_write_pca9553(card, 2, 0x80);
363e6d9c80bSStephane Grosjean 	if (err)
364e6d9c80bSStephane Grosjean 		return err;
365e6d9c80bSStephane Grosjean 
366e6d9c80bSStephane Grosjean 	/* prescaler for frequency 1: "FAST" = 5 Hz */
367e6d9c80bSStephane Grosjean 	err = peak_pciec_write_pca9553(card, 3, 44 / 5);
368e6d9c80bSStephane Grosjean 	if (err)
369e6d9c80bSStephane Grosjean 		return err;
370e6d9c80bSStephane Grosjean 
371e6d9c80bSStephane Grosjean 	/* duty cycle 1: 50% */
372e6d9c80bSStephane Grosjean 	err = peak_pciec_write_pca9553(card, 4, 0x80);
373e6d9c80bSStephane Grosjean 	if (err)
374e6d9c80bSStephane Grosjean 		return err;
375e6d9c80bSStephane Grosjean 
376e6d9c80bSStephane Grosjean 	/* switch LEDs to initial state */
377e6d9c80bSStephane Grosjean 	return peak_pciec_write_pca9553(card, 5, PCA9553_LS0_INIT);
378e6d9c80bSStephane Grosjean }
379e6d9c80bSStephane Grosjean 
380e6d9c80bSStephane Grosjean /*
381e6d9c80bSStephane Grosjean  * restore LEDs state to off peak_pciec_leds_exit
382e6d9c80bSStephane Grosjean  */
383e6d9c80bSStephane Grosjean static void peak_pciec_leds_exit(struct peak_pciec_card *card)
384e6d9c80bSStephane Grosjean {
385e6d9c80bSStephane Grosjean 	/* switch LEDs to off */
386e6d9c80bSStephane Grosjean 	peak_pciec_write_pca9553(card, 5, PCA9553_LED_OFF_ALL);
387e6d9c80bSStephane Grosjean }
388e6d9c80bSStephane Grosjean 
389e6d9c80bSStephane Grosjean /*
390e6d9c80bSStephane Grosjean  * normal write sja1000 register method overloaded to catch when controller
391e6d9c80bSStephane Grosjean  * is started or stopped, to control leds
392e6d9c80bSStephane Grosjean  */
393e6d9c80bSStephane Grosjean static void peak_pciec_write_reg(const struct sja1000_priv *priv,
394e6d9c80bSStephane Grosjean 				 int port, u8 val)
395e6d9c80bSStephane Grosjean {
396e6d9c80bSStephane Grosjean 	struct peak_pci_chan *chan = priv->priv;
397e6d9c80bSStephane Grosjean 	struct peak_pciec_card *card = chan->pciec_card;
398e6d9c80bSStephane Grosjean 	int c = (priv->reg_base - card->reg_base) / PEAK_PCI_CHAN_SIZE;
399e6d9c80bSStephane Grosjean 
400e6d9c80bSStephane Grosjean 	/* sja1000 register changes control the leds state */
40106e1d1d7SOliver Hartkopp 	if (port == SJA1000_MOD)
402e6d9c80bSStephane Grosjean 		switch (val) {
403e6d9c80bSStephane Grosjean 		case MOD_RM:
404e6d9c80bSStephane Grosjean 			/* Reset Mode: set led on */
405e6d9c80bSStephane Grosjean 			peak_pciec_set_leds(card, PCA9553_LED(c), PCA9553_ON);
406e6d9c80bSStephane Grosjean 			break;
407e6d9c80bSStephane Grosjean 		case 0x00:
408e6d9c80bSStephane Grosjean 			/* Normal Mode: led slow blinking and start led timer */
409e6d9c80bSStephane Grosjean 			peak_pciec_set_leds(card, PCA9553_LED(c), PCA9553_SLOW);
410e6d9c80bSStephane Grosjean 			peak_pciec_start_led_work(card);
411e6d9c80bSStephane Grosjean 			break;
412e6d9c80bSStephane Grosjean 		default:
413e6d9c80bSStephane Grosjean 			break;
414e6d9c80bSStephane Grosjean 		}
415e6d9c80bSStephane Grosjean 
416e6d9c80bSStephane Grosjean 	/* call base function */
417e6d9c80bSStephane Grosjean 	peak_pci_write_reg(priv, port, val);
418e6d9c80bSStephane Grosjean }
419e6d9c80bSStephane Grosjean 
420*1f0dee39SNishka Dasgupta static const struct i2c_algo_bit_data peak_pciec_i2c_bit_ops = {
421e6d9c80bSStephane Grosjean 	.setsda	= pita_setsda,
422e6d9c80bSStephane Grosjean 	.setscl	= pita_setscl,
423e6d9c80bSStephane Grosjean 	.getsda	= pita_getsda,
424e6d9c80bSStephane Grosjean 	.getscl	= pita_getscl,
425e6d9c80bSStephane Grosjean 	.udelay	= 10,
426e6d9c80bSStephane Grosjean 	.timeout = HZ,
427e6d9c80bSStephane Grosjean };
428e6d9c80bSStephane Grosjean 
429e6d9c80bSStephane Grosjean static int peak_pciec_probe(struct pci_dev *pdev, struct net_device *dev)
430e6d9c80bSStephane Grosjean {
431e6d9c80bSStephane Grosjean 	struct sja1000_priv *priv = netdev_priv(dev);
432e6d9c80bSStephane Grosjean 	struct peak_pci_chan *chan = priv->priv;
433e6d9c80bSStephane Grosjean 	struct peak_pciec_card *card;
434e6d9c80bSStephane Grosjean 	int err;
435e6d9c80bSStephane Grosjean 
436e6d9c80bSStephane Grosjean 	/* copy i2c object address from 1st channel */
437e6d9c80bSStephane Grosjean 	if (chan->prev_dev) {
438e6d9c80bSStephane Grosjean 		struct sja1000_priv *prev_priv = netdev_priv(chan->prev_dev);
439e6d9c80bSStephane Grosjean 		struct peak_pci_chan *prev_chan = prev_priv->priv;
440e6d9c80bSStephane Grosjean 
441e6d9c80bSStephane Grosjean 		card = prev_chan->pciec_card;
442e6d9c80bSStephane Grosjean 		if (!card)
443e6d9c80bSStephane Grosjean 			return -ENODEV;
444e6d9c80bSStephane Grosjean 
445e6d9c80bSStephane Grosjean 	/* channel is the first one: do the init part */
446e6d9c80bSStephane Grosjean 	} else {
447e6d9c80bSStephane Grosjean 		/* create the bit banging I2C adapter structure */
448e6d9c80bSStephane Grosjean 		card = kzalloc(sizeof(struct peak_pciec_card), GFP_KERNEL);
44909da6c5fSJoe Perches 		if (!card)
450e6d9c80bSStephane Grosjean 			return -ENOMEM;
451e6d9c80bSStephane Grosjean 
452e6d9c80bSStephane Grosjean 		card->cfg_base = chan->cfg_base;
453e6d9c80bSStephane Grosjean 		card->reg_base = priv->reg_base;
454e6d9c80bSStephane Grosjean 
455e6d9c80bSStephane Grosjean 		card->led_chip.owner = THIS_MODULE;
456e6d9c80bSStephane Grosjean 		card->led_chip.dev.parent = &pdev->dev;
457e6d9c80bSStephane Grosjean 		card->led_chip.algo_data = &card->i2c_bit;
458e6d9c80bSStephane Grosjean 		strncpy(card->led_chip.name, "peak_i2c",
459e6d9c80bSStephane Grosjean 			sizeof(card->led_chip.name));
460e6d9c80bSStephane Grosjean 
461e6d9c80bSStephane Grosjean 		card->i2c_bit = peak_pciec_i2c_bit_ops;
462e6d9c80bSStephane Grosjean 		card->i2c_bit.udelay = 10;
463e6d9c80bSStephane Grosjean 		card->i2c_bit.timeout = HZ;
464e6d9c80bSStephane Grosjean 		card->i2c_bit.data = card;
465e6d9c80bSStephane Grosjean 
466e6d9c80bSStephane Grosjean 		peak_pciec_init_pita_gpio(card);
467e6d9c80bSStephane Grosjean 
468e6d9c80bSStephane Grosjean 		err = i2c_bit_add_bus(&card->led_chip);
469e6d9c80bSStephane Grosjean 		if (err) {
470e6d9c80bSStephane Grosjean 			dev_err(&pdev->dev, "i2c init failed\n");
471e6d9c80bSStephane Grosjean 			goto pciec_init_err_1;
472e6d9c80bSStephane Grosjean 		}
473e6d9c80bSStephane Grosjean 
474e6d9c80bSStephane Grosjean 		err = peak_pciec_init_leds(card);
475e6d9c80bSStephane Grosjean 		if (err) {
476e6d9c80bSStephane Grosjean 			dev_err(&pdev->dev, "leds hardware init failed\n");
477e6d9c80bSStephane Grosjean 			goto pciec_init_err_2;
478e6d9c80bSStephane Grosjean 		}
479e6d9c80bSStephane Grosjean 
480e6d9c80bSStephane Grosjean 		INIT_DELAYED_WORK(&card->led_work, peak_pciec_led_work);
481e6d9c80bSStephane Grosjean 		/* PCAN-ExpressCard needs its own callback for leds */
482e6d9c80bSStephane Grosjean 		priv->write_reg = peak_pciec_write_reg;
483e6d9c80bSStephane Grosjean 	}
484e6d9c80bSStephane Grosjean 
485e6d9c80bSStephane Grosjean 	chan->pciec_card = card;
486e6d9c80bSStephane Grosjean 	card->channel[card->chan_count++].netdev = dev;
487e6d9c80bSStephane Grosjean 
488e6d9c80bSStephane Grosjean 	return 0;
489e6d9c80bSStephane Grosjean 
490e6d9c80bSStephane Grosjean pciec_init_err_2:
491e6d9c80bSStephane Grosjean 	i2c_del_adapter(&card->led_chip);
492e6d9c80bSStephane Grosjean 
493e6d9c80bSStephane Grosjean pciec_init_err_1:
494e6d9c80bSStephane Grosjean 	peak_pciec_init_pita_gpio(card);
495e6d9c80bSStephane Grosjean 	kfree(card);
496e6d9c80bSStephane Grosjean 
497e6d9c80bSStephane Grosjean 	return err;
498e6d9c80bSStephane Grosjean }
499e6d9c80bSStephane Grosjean 
500e6d9c80bSStephane Grosjean static void peak_pciec_remove(struct peak_pciec_card *card)
501e6d9c80bSStephane Grosjean {
502e6d9c80bSStephane Grosjean 	peak_pciec_stop_led_work(card);
503e6d9c80bSStephane Grosjean 	peak_pciec_leds_exit(card);
504e6d9c80bSStephane Grosjean 	i2c_del_adapter(&card->led_chip);
505e6d9c80bSStephane Grosjean 	peak_pciec_init_pita_gpio(card);
506e6d9c80bSStephane Grosjean 	kfree(card);
507e6d9c80bSStephane Grosjean }
508e6d9c80bSStephane Grosjean 
509e6d9c80bSStephane Grosjean #else /* CONFIG_CAN_PEAK_PCIEC */
510e6d9c80bSStephane Grosjean 
511e6d9c80bSStephane Grosjean /*
512e6d9c80bSStephane Grosjean  * Placebo functions when PCAN-ExpressCard support is not selected
513e6d9c80bSStephane Grosjean  */
514e6d9c80bSStephane Grosjean static inline int peak_pciec_probe(struct pci_dev *pdev, struct net_device *dev)
515e6d9c80bSStephane Grosjean {
516e6d9c80bSStephane Grosjean 	return -ENODEV;
517e6d9c80bSStephane Grosjean }
518e6d9c80bSStephane Grosjean 
519e6d9c80bSStephane Grosjean static inline void peak_pciec_remove(struct peak_pciec_card *card)
520e6d9c80bSStephane Grosjean {
521e6d9c80bSStephane Grosjean }
522e6d9c80bSStephane Grosjean #endif /* CONFIG_CAN_PEAK_PCIEC */
523e6d9c80bSStephane Grosjean 
52438034518SWolfgang Grandegger static u8 peak_pci_read_reg(const struct sja1000_priv *priv, int port)
52538034518SWolfgang Grandegger {
52638034518SWolfgang Grandegger 	return readb(priv->reg_base + (port << 2));
52738034518SWolfgang Grandegger }
52838034518SWolfgang Grandegger 
52938034518SWolfgang Grandegger static void peak_pci_write_reg(const struct sja1000_priv *priv,
53038034518SWolfgang Grandegger 			       int port, u8 val)
53138034518SWolfgang Grandegger {
53238034518SWolfgang Grandegger 	writeb(val, priv->reg_base + (port << 2));
53338034518SWolfgang Grandegger }
53438034518SWolfgang Grandegger 
53538034518SWolfgang Grandegger static void peak_pci_post_irq(const struct sja1000_priv *priv)
53638034518SWolfgang Grandegger {
53738034518SWolfgang Grandegger 	struct peak_pci_chan *chan = priv->priv;
53838034518SWolfgang Grandegger 	u16 icr;
53938034518SWolfgang Grandegger 
54038034518SWolfgang Grandegger 	/* Select and clear in PITA stored interrupt */
54138034518SWolfgang Grandegger 	icr = readw(chan->cfg_base + PITA_ICR);
54238034518SWolfgang Grandegger 	if (icr & chan->icr_mask)
54338034518SWolfgang Grandegger 		writew(chan->icr_mask, chan->cfg_base + PITA_ICR);
54438034518SWolfgang Grandegger }
54538034518SWolfgang Grandegger 
5461dd06ae8SGreg Kroah-Hartman static int peak_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
54738034518SWolfgang Grandegger {
54838034518SWolfgang Grandegger 	struct sja1000_priv *priv;
54938034518SWolfgang Grandegger 	struct peak_pci_chan *chan;
5500b5a958cSStephane Grosjean 	struct net_device *dev, *prev_dev;
55138034518SWolfgang Grandegger 	void __iomem *cfg_base, *reg_base;
55238034518SWolfgang Grandegger 	u16 sub_sys_id, icr;
55338034518SWolfgang Grandegger 	int i, err, channels;
55438034518SWolfgang Grandegger 
55538034518SWolfgang Grandegger 	err = pci_enable_device(pdev);
55638034518SWolfgang Grandegger 	if (err)
55738034518SWolfgang Grandegger 		return err;
55838034518SWolfgang Grandegger 
55938034518SWolfgang Grandegger 	err = pci_request_regions(pdev, DRV_NAME);
56038034518SWolfgang Grandegger 	if (err)
56138034518SWolfgang Grandegger 		goto failure_disable_pci;
56238034518SWolfgang Grandegger 
56338034518SWolfgang Grandegger 	err = pci_read_config_word(pdev, 0x2e, &sub_sys_id);
56438034518SWolfgang Grandegger 	if (err)
56538034518SWolfgang Grandegger 		goto failure_release_regions;
56638034518SWolfgang Grandegger 
56738034518SWolfgang Grandegger 	dev_dbg(&pdev->dev, "probing device %04x:%04x:%04x\n",
56838034518SWolfgang Grandegger 		pdev->vendor, pdev->device, sub_sys_id);
56938034518SWolfgang Grandegger 
57038034518SWolfgang Grandegger 	err = pci_write_config_word(pdev, 0x44, 0);
57138034518SWolfgang Grandegger 	if (err)
57238034518SWolfgang Grandegger 		goto failure_release_regions;
57338034518SWolfgang Grandegger 
57438034518SWolfgang Grandegger 	if (sub_sys_id >= 12)
57538034518SWolfgang Grandegger 		channels = 4;
57638034518SWolfgang Grandegger 	else if (sub_sys_id >= 10)
57738034518SWolfgang Grandegger 		channels = 3;
57838034518SWolfgang Grandegger 	else if (sub_sys_id >= 4)
57938034518SWolfgang Grandegger 		channels = 2;
58038034518SWolfgang Grandegger 	else
58138034518SWolfgang Grandegger 		channels = 1;
58238034518SWolfgang Grandegger 
58338034518SWolfgang Grandegger 	cfg_base = pci_iomap(pdev, 0, PEAK_PCI_CFG_SIZE);
58438034518SWolfgang Grandegger 	if (!cfg_base) {
58538034518SWolfgang Grandegger 		dev_err(&pdev->dev, "failed to map PCI resource #0\n");
5864a4bfdcdSPeter Senna Tschudin 		err = -ENOMEM;
58738034518SWolfgang Grandegger 		goto failure_release_regions;
58838034518SWolfgang Grandegger 	}
58938034518SWolfgang Grandegger 
59038034518SWolfgang Grandegger 	reg_base = pci_iomap(pdev, 1, PEAK_PCI_CHAN_SIZE * channels);
59138034518SWolfgang Grandegger 	if (!reg_base) {
59238034518SWolfgang Grandegger 		dev_err(&pdev->dev, "failed to map PCI resource #1\n");
5934a4bfdcdSPeter Senna Tschudin 		err = -ENOMEM;
59438034518SWolfgang Grandegger 		goto failure_unmap_cfg_base;
59538034518SWolfgang Grandegger 	}
59638034518SWolfgang Grandegger 
59738034518SWolfgang Grandegger 	/* Set GPIO control register */
59838034518SWolfgang Grandegger 	writew(0x0005, cfg_base + PITA_GPIOICR + 2);
59938034518SWolfgang Grandegger 	/* Enable all channels of this card */
60038034518SWolfgang Grandegger 	writeb(0x00, cfg_base + PITA_GPIOICR);
60138034518SWolfgang Grandegger 	/* Toggle reset */
60238034518SWolfgang Grandegger 	writeb(0x05, cfg_base + PITA_MISC + 3);
603276b7361SJia-Ju Bai 	usleep_range(5000, 6000);
60438034518SWolfgang Grandegger 	/* Leave parport mux mode */
60538034518SWolfgang Grandegger 	writeb(0x04, cfg_base + PITA_MISC + 3);
60638034518SWolfgang Grandegger 
60738034518SWolfgang Grandegger 	icr = readw(cfg_base + PITA_ICR + 2);
60838034518SWolfgang Grandegger 
60938034518SWolfgang Grandegger 	for (i = 0; i < channels; i++) {
61038034518SWolfgang Grandegger 		dev = alloc_sja1000dev(sizeof(struct peak_pci_chan));
61138034518SWolfgang Grandegger 		if (!dev) {
61238034518SWolfgang Grandegger 			err = -ENOMEM;
61338034518SWolfgang Grandegger 			goto failure_remove_channels;
61438034518SWolfgang Grandegger 		}
61538034518SWolfgang Grandegger 
61638034518SWolfgang Grandegger 		priv = netdev_priv(dev);
61738034518SWolfgang Grandegger 		chan = priv->priv;
61838034518SWolfgang Grandegger 
61938034518SWolfgang Grandegger 		chan->cfg_base = cfg_base;
62038034518SWolfgang Grandegger 		priv->reg_base = reg_base + i * PEAK_PCI_CHAN_SIZE;
62138034518SWolfgang Grandegger 
62238034518SWolfgang Grandegger 		priv->read_reg = peak_pci_read_reg;
62338034518SWolfgang Grandegger 		priv->write_reg = peak_pci_write_reg;
62438034518SWolfgang Grandegger 		priv->post_irq = peak_pci_post_irq;
62538034518SWolfgang Grandegger 
62638034518SWolfgang Grandegger 		priv->can.clock.freq = PEAK_PCI_CAN_CLOCK;
62738034518SWolfgang Grandegger 		priv->ocr = PEAK_PCI_OCR;
62838034518SWolfgang Grandegger 		priv->cdr = PEAK_PCI_CDR;
62938034518SWolfgang Grandegger 		/* Neither a slave nor a single device distributes the clock */
63038034518SWolfgang Grandegger 		if (channels == 1 || i > 0)
63138034518SWolfgang Grandegger 			priv->cdr |= CDR_CLK_OFF;
63238034518SWolfgang Grandegger 
63338034518SWolfgang Grandegger 		/* Setup interrupt handling */
63438034518SWolfgang Grandegger 		priv->irq_flags = IRQF_SHARED;
63538034518SWolfgang Grandegger 		dev->irq = pdev->irq;
63638034518SWolfgang Grandegger 
63738034518SWolfgang Grandegger 		chan->icr_mask = peak_pci_icr_masks[i];
63838034518SWolfgang Grandegger 		icr |= chan->icr_mask;
63938034518SWolfgang Grandegger 
64038034518SWolfgang Grandegger 		SET_NETDEV_DEV(dev, &pdev->dev);
6413e66d013SChristopher R. Baker 		dev->dev_id = i;
64238034518SWolfgang Grandegger 
64338034518SWolfgang Grandegger 		/* Create chain of SJA1000 devices */
64429830406SStephane Grosjean 		chan->prev_dev = pci_get_drvdata(pdev);
64529830406SStephane Grosjean 		pci_set_drvdata(pdev, dev);
64638034518SWolfgang Grandegger 
647e6d9c80bSStephane Grosjean 		/*
648e6d9c80bSStephane Grosjean 		 * PCAN-ExpressCard needs some additional i2c init.
649e6d9c80bSStephane Grosjean 		 * This must be done *before* register_sja1000dev() but
650e6d9c80bSStephane Grosjean 		 * *after* devices linkage
651e6d9c80bSStephane Grosjean 		 */
6524be0015cSOliver Hartkopp 		if (pdev->device == PEAK_PCIEC_DEVICE_ID ||
6534be0015cSOliver Hartkopp 		    pdev->device == PEAK_PCIEC34_DEVICE_ID) {
654e6d9c80bSStephane Grosjean 			err = peak_pciec_probe(pdev, dev);
655e6d9c80bSStephane Grosjean 			if (err) {
656e6d9c80bSStephane Grosjean 				dev_err(&pdev->dev,
657e6d9c80bSStephane Grosjean 					"failed to probe device (err %d)\n",
658e6d9c80bSStephane Grosjean 					err);
659e6d9c80bSStephane Grosjean 				goto failure_free_dev;
660e6d9c80bSStephane Grosjean 			}
661e6d9c80bSStephane Grosjean 		}
662e6d9c80bSStephane Grosjean 
663e6d9c80bSStephane Grosjean 		err = register_sja1000dev(dev);
664e6d9c80bSStephane Grosjean 		if (err) {
665e6d9c80bSStephane Grosjean 			dev_err(&pdev->dev, "failed to register device\n");
666e6d9c80bSStephane Grosjean 			goto failure_free_dev;
667e6d9c80bSStephane Grosjean 		}
668e6d9c80bSStephane Grosjean 
66938034518SWolfgang Grandegger 		dev_info(&pdev->dev,
67038034518SWolfgang Grandegger 			 "%s at reg_base=0x%p cfg_base=0x%p irq=%d\n",
67138034518SWolfgang Grandegger 			 dev->name, priv->reg_base, chan->cfg_base, dev->irq);
67238034518SWolfgang Grandegger 	}
67338034518SWolfgang Grandegger 
67438034518SWolfgang Grandegger 	/* Enable interrupts */
67538034518SWolfgang Grandegger 	writew(icr, cfg_base + PITA_ICR + 2);
67638034518SWolfgang Grandegger 
67738034518SWolfgang Grandegger 	return 0;
67838034518SWolfgang Grandegger 
679e6d9c80bSStephane Grosjean failure_free_dev:
680e6d9c80bSStephane Grosjean 	pci_set_drvdata(pdev, chan->prev_dev);
681e6d9c80bSStephane Grosjean 	free_sja1000dev(dev);
682e6d9c80bSStephane Grosjean 
68338034518SWolfgang Grandegger failure_remove_channels:
68438034518SWolfgang Grandegger 	/* Disable interrupts */
68538034518SWolfgang Grandegger 	writew(0x0, cfg_base + PITA_ICR + 2);
68638034518SWolfgang Grandegger 
687e6d9c80bSStephane Grosjean 	chan = NULL;
6880b5a958cSStephane Grosjean 	for (dev = pci_get_drvdata(pdev); dev; dev = prev_dev) {
68938034518SWolfgang Grandegger 		priv = netdev_priv(dev);
69038034518SWolfgang Grandegger 		chan = priv->priv;
6910b5a958cSStephane Grosjean 		prev_dev = chan->prev_dev;
6920b5a958cSStephane Grosjean 
6930b5a958cSStephane Grosjean 		unregister_sja1000dev(dev);
6940b5a958cSStephane Grosjean 		free_sja1000dev(dev);
69538034518SWolfgang Grandegger 	}
69638034518SWolfgang Grandegger 
697e6d9c80bSStephane Grosjean 	/* free any PCIeC resources too */
698e6d9c80bSStephane Grosjean 	if (chan && chan->pciec_card)
699e6d9c80bSStephane Grosjean 		peak_pciec_remove(chan->pciec_card);
700e6d9c80bSStephane Grosjean 
70138034518SWolfgang Grandegger 	pci_iounmap(pdev, reg_base);
70238034518SWolfgang Grandegger 
70338034518SWolfgang Grandegger failure_unmap_cfg_base:
70438034518SWolfgang Grandegger 	pci_iounmap(pdev, cfg_base);
70538034518SWolfgang Grandegger 
70638034518SWolfgang Grandegger failure_release_regions:
70738034518SWolfgang Grandegger 	pci_release_regions(pdev);
70838034518SWolfgang Grandegger 
70938034518SWolfgang Grandegger failure_disable_pci:
71038034518SWolfgang Grandegger 	pci_disable_device(pdev);
71138034518SWolfgang Grandegger 
7125c2cb02eSStephane Grosjean 	/* pci_xxx_config_word() return positive PCIBIOS_xxx error codes while
7135c2cb02eSStephane Grosjean 	 * the probe() function must return a negative errno in case of failure
7145c2cb02eSStephane Grosjean 	 * (err is unchanged if negative) */
7155c2cb02eSStephane Grosjean 	return pcibios_err_to_errno(err);
71638034518SWolfgang Grandegger }
71738034518SWolfgang Grandegger 
7183c8ac0f2SBill Pemberton static void peak_pci_remove(struct pci_dev *pdev)
71938034518SWolfgang Grandegger {
72029830406SStephane Grosjean 	struct net_device *dev = pci_get_drvdata(pdev); /* Last device */
72138034518SWolfgang Grandegger 	struct sja1000_priv *priv = netdev_priv(dev);
72238034518SWolfgang Grandegger 	struct peak_pci_chan *chan = priv->priv;
72338034518SWolfgang Grandegger 	void __iomem *cfg_base = chan->cfg_base;
72438034518SWolfgang Grandegger 	void __iomem *reg_base = priv->reg_base;
72538034518SWolfgang Grandegger 
72638034518SWolfgang Grandegger 	/* Disable interrupts */
72738034518SWolfgang Grandegger 	writew(0x0, cfg_base + PITA_ICR + 2);
72838034518SWolfgang Grandegger 
72938034518SWolfgang Grandegger 	/* Loop over all registered devices */
73038034518SWolfgang Grandegger 	while (1) {
7310b5a958cSStephane Grosjean 		struct net_device *prev_dev = chan->prev_dev;
7320b5a958cSStephane Grosjean 
73338034518SWolfgang Grandegger 		dev_info(&pdev->dev, "removing device %s\n", dev->name);
73438034518SWolfgang Grandegger 		unregister_sja1000dev(dev);
73538034518SWolfgang Grandegger 		free_sja1000dev(dev);
7360b5a958cSStephane Grosjean 		dev = prev_dev;
737e6d9c80bSStephane Grosjean 
738e6d9c80bSStephane Grosjean 		if (!dev) {
739e6d9c80bSStephane Grosjean 			/* do that only for first channel */
740e6d9c80bSStephane Grosjean 			if (chan->pciec_card)
741e6d9c80bSStephane Grosjean 				peak_pciec_remove(chan->pciec_card);
74238034518SWolfgang Grandegger 			break;
743e6d9c80bSStephane Grosjean 		}
74438034518SWolfgang Grandegger 		priv = netdev_priv(dev);
74538034518SWolfgang Grandegger 		chan = priv->priv;
74638034518SWolfgang Grandegger 	}
74738034518SWolfgang Grandegger 
74838034518SWolfgang Grandegger 	pci_iounmap(pdev, reg_base);
74938034518SWolfgang Grandegger 	pci_iounmap(pdev, cfg_base);
75038034518SWolfgang Grandegger 	pci_release_regions(pdev);
75138034518SWolfgang Grandegger 	pci_disable_device(pdev);
75238034518SWolfgang Grandegger }
75338034518SWolfgang Grandegger 
75438034518SWolfgang Grandegger static struct pci_driver peak_pci_driver = {
75538034518SWolfgang Grandegger 	.name = DRV_NAME,
75638034518SWolfgang Grandegger 	.id_table = peak_pci_tbl,
75738034518SWolfgang Grandegger 	.probe = peak_pci_probe,
7583c8ac0f2SBill Pemberton 	.remove = peak_pci_remove,
75938034518SWolfgang Grandegger };
76038034518SWolfgang Grandegger 
761fb7944b3SAxel Lin module_pci_driver(peak_pci_driver);
762