1aaa7cb26SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2fd734c6fSOliver Hartkopp /*
3fd734c6fSOliver Hartkopp * Copyright (C) 2008 Sebastian Haas (initial chardev implementation)
4fd734c6fSOliver Hartkopp * Copyright (C) 2010 Markus Plessing <plessing@ems-wuensche.com>
5fd734c6fSOliver Hartkopp * Rework for mainline by Oliver Hartkopp <socketcan@hartkopp.net>
6fd734c6fSOliver Hartkopp */
7fd734c6fSOliver Hartkopp
8fd734c6fSOliver Hartkopp #include <linux/kernel.h>
9fd734c6fSOliver Hartkopp #include <linux/module.h>
10fd734c6fSOliver Hartkopp #include <linux/interrupt.h>
11fd734c6fSOliver Hartkopp #include <linux/netdevice.h>
12fd734c6fSOliver Hartkopp #include <linux/delay.h>
13fd734c6fSOliver Hartkopp #include <linux/io.h>
14fd734c6fSOliver Hartkopp #include <pcmcia/cistpl.h>
15fd734c6fSOliver Hartkopp #include <pcmcia/ds.h>
16fd734c6fSOliver Hartkopp #include <linux/can.h>
17fd734c6fSOliver Hartkopp #include <linux/can/dev.h>
18fd734c6fSOliver Hartkopp #include "sja1000.h"
19fd734c6fSOliver Hartkopp
20fd734c6fSOliver Hartkopp #define DRV_NAME "ems_pcmcia"
21fd734c6fSOliver Hartkopp
22fd734c6fSOliver Hartkopp MODULE_AUTHOR("Markus Plessing <plessing@ems-wuensche.com>");
23fd734c6fSOliver Hartkopp MODULE_DESCRIPTION("Socket-CAN driver for EMS CPC-CARD cards");
24fd734c6fSOliver Hartkopp MODULE_LICENSE("GPL v2");
25fd734c6fSOliver Hartkopp
26fd734c6fSOliver Hartkopp #define EMS_PCMCIA_MAX_CHAN 2
27fd734c6fSOliver Hartkopp
28fd734c6fSOliver Hartkopp struct ems_pcmcia_card {
29fd734c6fSOliver Hartkopp int channels;
30fd734c6fSOliver Hartkopp struct pcmcia_device *pcmcia_dev;
31fd734c6fSOliver Hartkopp struct net_device *net_dev[EMS_PCMCIA_MAX_CHAN];
32fd734c6fSOliver Hartkopp void __iomem *base_addr;
33fd734c6fSOliver Hartkopp };
34fd734c6fSOliver Hartkopp
35fd734c6fSOliver Hartkopp #define EMS_PCMCIA_CAN_CLOCK (16000000 / 2)
36fd734c6fSOliver Hartkopp
37fd734c6fSOliver Hartkopp /*
38fd734c6fSOliver Hartkopp * The board configuration is probably following:
39fd734c6fSOliver Hartkopp * RX1 is connected to ground.
40fd734c6fSOliver Hartkopp * TX1 is not connected.
41fd734c6fSOliver Hartkopp * CLKO is not connected.
42fd734c6fSOliver Hartkopp * Setting the OCR register to 0xDA is a good idea.
43fd734c6fSOliver Hartkopp * This means normal output mode , push-pull and the correct polarity.
44fd734c6fSOliver Hartkopp */
45fd734c6fSOliver Hartkopp #define EMS_PCMCIA_OCR (OCR_TX0_PUSHPULL | OCR_TX1_PUSHPULL)
46fd734c6fSOliver Hartkopp
47fd734c6fSOliver Hartkopp /*
48fd734c6fSOliver Hartkopp * In the CDR register, you should set CBP to 1.
49fd734c6fSOliver Hartkopp * You will probably also want to set the clock divider value to 7
50fd734c6fSOliver Hartkopp * (meaning direct oscillator output) because the second SJA1000 chip
51fd734c6fSOliver Hartkopp * is driven by the first one CLKOUT output.
52fd734c6fSOliver Hartkopp */
53fd734c6fSOliver Hartkopp #define EMS_PCMCIA_CDR (CDR_CBP | CDR_CLKOUT_MASK)
54fd734c6fSOliver Hartkopp #define EMS_PCMCIA_MEM_SIZE 4096 /* Size of the remapped io-memory */
55fd734c6fSOliver Hartkopp #define EMS_PCMCIA_CAN_BASE_OFFSET 0x100 /* Offset where controllers starts */
56fd734c6fSOliver Hartkopp #define EMS_PCMCIA_CAN_CTRL_SIZE 0x80 /* Memory size for each controller */
57fd734c6fSOliver Hartkopp
58fd734c6fSOliver Hartkopp #define EMS_CMD_RESET 0x00 /* Perform a reset of the card */
59fd734c6fSOliver Hartkopp #define EMS_CMD_MAP 0x03 /* Map CAN controllers into card' memory */
60fd734c6fSOliver Hartkopp #define EMS_CMD_UMAP 0x02 /* Unmap CAN controllers from card' memory */
61fd734c6fSOliver Hartkopp
62fd734c6fSOliver Hartkopp static struct pcmcia_device_id ems_pcmcia_tbl[] = {
63fd734c6fSOliver Hartkopp PCMCIA_DEVICE_PROD_ID123("EMS_T_W", "CPC-Card", "V2.0", 0xeab1ea23,
64fd734c6fSOliver Hartkopp 0xa338573f, 0xe4575800),
65fd734c6fSOliver Hartkopp PCMCIA_DEVICE_NULL,
66fd734c6fSOliver Hartkopp };
67fd734c6fSOliver Hartkopp
68fd734c6fSOliver Hartkopp MODULE_DEVICE_TABLE(pcmcia, ems_pcmcia_tbl);
69fd734c6fSOliver Hartkopp
ems_pcmcia_read_reg(const struct sja1000_priv * priv,int port)70fd734c6fSOliver Hartkopp static u8 ems_pcmcia_read_reg(const struct sja1000_priv *priv, int port)
71fd734c6fSOliver Hartkopp {
72fd734c6fSOliver Hartkopp return readb(priv->reg_base + port);
73fd734c6fSOliver Hartkopp }
74fd734c6fSOliver Hartkopp
ems_pcmcia_write_reg(const struct sja1000_priv * priv,int port,u8 val)75fd734c6fSOliver Hartkopp static void ems_pcmcia_write_reg(const struct sja1000_priv *priv, int port,
76fd734c6fSOliver Hartkopp u8 val)
77fd734c6fSOliver Hartkopp {
78fd734c6fSOliver Hartkopp writeb(val, priv->reg_base + port);
79fd734c6fSOliver Hartkopp }
80fd734c6fSOliver Hartkopp
ems_pcmcia_interrupt(int irq,void * dev_id)81fd734c6fSOliver Hartkopp static irqreturn_t ems_pcmcia_interrupt(int irq, void *dev_id)
82fd734c6fSOliver Hartkopp {
83fd734c6fSOliver Hartkopp struct ems_pcmcia_card *card = dev_id;
84fd734c6fSOliver Hartkopp struct net_device *dev;
85fd734c6fSOliver Hartkopp irqreturn_t retval = IRQ_NONE;
86fd734c6fSOliver Hartkopp int i, again;
87fd734c6fSOliver Hartkopp
88fd734c6fSOliver Hartkopp /* Card not present */
89fd734c6fSOliver Hartkopp if (readw(card->base_addr) != 0xAA55)
90fd734c6fSOliver Hartkopp return IRQ_HANDLED;
91fd734c6fSOliver Hartkopp
92fd734c6fSOliver Hartkopp do {
93fd734c6fSOliver Hartkopp again = 0;
94fd734c6fSOliver Hartkopp
95fd734c6fSOliver Hartkopp /* Check interrupt for each channel */
96fd734c6fSOliver Hartkopp for (i = 0; i < card->channels; i++) {
97fd734c6fSOliver Hartkopp dev = card->net_dev[i];
98fd734c6fSOliver Hartkopp if (!dev)
99fd734c6fSOliver Hartkopp continue;
100fd734c6fSOliver Hartkopp
101fd734c6fSOliver Hartkopp if (sja1000_interrupt(irq, dev) == IRQ_HANDLED)
102fd734c6fSOliver Hartkopp again = 1;
103fd734c6fSOliver Hartkopp }
104fd734c6fSOliver Hartkopp /* At least one channel handled the interrupt */
105fd734c6fSOliver Hartkopp if (again)
106fd734c6fSOliver Hartkopp retval = IRQ_HANDLED;
107fd734c6fSOliver Hartkopp
108fd734c6fSOliver Hartkopp } while (again);
109fd734c6fSOliver Hartkopp
110fd734c6fSOliver Hartkopp return retval;
111fd734c6fSOliver Hartkopp }
112fd734c6fSOliver Hartkopp
113fd734c6fSOliver Hartkopp /*
114fd734c6fSOliver Hartkopp * Check if a CAN controller is present at the specified location
115fd734c6fSOliver Hartkopp * by trying to set 'em into the PeliCAN mode
116fd734c6fSOliver Hartkopp */
ems_pcmcia_check_chan(struct sja1000_priv * priv)117fd734c6fSOliver Hartkopp static inline int ems_pcmcia_check_chan(struct sja1000_priv *priv)
118fd734c6fSOliver Hartkopp {
119fd734c6fSOliver Hartkopp /* Make sure SJA1000 is in reset mode */
12006e1d1d7SOliver Hartkopp ems_pcmcia_write_reg(priv, SJA1000_MOD, 1);
12106e1d1d7SOliver Hartkopp ems_pcmcia_write_reg(priv, SJA1000_CDR, CDR_PELICAN);
122fd734c6fSOliver Hartkopp
123fd734c6fSOliver Hartkopp /* read reset-values */
12406e1d1d7SOliver Hartkopp if (ems_pcmcia_read_reg(priv, SJA1000_CDR) == CDR_PELICAN)
125fd734c6fSOliver Hartkopp return 1;
126fd734c6fSOliver Hartkopp
127fd734c6fSOliver Hartkopp return 0;
128fd734c6fSOliver Hartkopp }
129fd734c6fSOliver Hartkopp
ems_pcmcia_del_card(struct pcmcia_device * pdev)130fd734c6fSOliver Hartkopp static void ems_pcmcia_del_card(struct pcmcia_device *pdev)
131fd734c6fSOliver Hartkopp {
132fd734c6fSOliver Hartkopp struct ems_pcmcia_card *card = pdev->priv;
133fd734c6fSOliver Hartkopp struct net_device *dev;
134fd734c6fSOliver Hartkopp int i;
135fd734c6fSOliver Hartkopp
136fd734c6fSOliver Hartkopp free_irq(pdev->irq, card);
137fd734c6fSOliver Hartkopp
138fd734c6fSOliver Hartkopp for (i = 0; i < card->channels; i++) {
139fd734c6fSOliver Hartkopp dev = card->net_dev[i];
140fd734c6fSOliver Hartkopp if (!dev)
141fd734c6fSOliver Hartkopp continue;
142fd734c6fSOliver Hartkopp
143fd734c6fSOliver Hartkopp printk(KERN_INFO "%s: removing %s on channel #%d\n",
144fd734c6fSOliver Hartkopp DRV_NAME, dev->name, i);
145fd734c6fSOliver Hartkopp unregister_sja1000dev(dev);
146fd734c6fSOliver Hartkopp free_sja1000dev(dev);
147fd734c6fSOliver Hartkopp }
148fd734c6fSOliver Hartkopp
149fd734c6fSOliver Hartkopp writeb(EMS_CMD_UMAP, card->base_addr);
150fd734c6fSOliver Hartkopp iounmap(card->base_addr);
151fd734c6fSOliver Hartkopp kfree(card);
152fd734c6fSOliver Hartkopp
153fd734c6fSOliver Hartkopp pdev->priv = NULL;
154fd734c6fSOliver Hartkopp }
155fd734c6fSOliver Hartkopp
156fd734c6fSOliver Hartkopp /*
157fd734c6fSOliver Hartkopp * Probe PCI device for EMS CAN signature and register each available
158fd734c6fSOliver Hartkopp * CAN channel to SJA1000 Socket-CAN subsystem.
159fd734c6fSOliver Hartkopp */
ems_pcmcia_add_card(struct pcmcia_device * pdev,unsigned long base)1601dd06ae8SGreg Kroah-Hartman static int ems_pcmcia_add_card(struct pcmcia_device *pdev, unsigned long base)
161fd734c6fSOliver Hartkopp {
162fd734c6fSOliver Hartkopp struct sja1000_priv *priv;
163fd734c6fSOliver Hartkopp struct net_device *dev;
164fd734c6fSOliver Hartkopp struct ems_pcmcia_card *card;
165fd734c6fSOliver Hartkopp int err, i;
166fd734c6fSOliver Hartkopp
167fd734c6fSOliver Hartkopp /* Allocating card structures to hold addresses, ... */
168fd734c6fSOliver Hartkopp card = kzalloc(sizeof(struct ems_pcmcia_card), GFP_KERNEL);
169fd734c6fSOliver Hartkopp if (!card)
170fd734c6fSOliver Hartkopp return -ENOMEM;
171fd734c6fSOliver Hartkopp
172fd734c6fSOliver Hartkopp pdev->priv = card;
173fd734c6fSOliver Hartkopp card->channels = 0;
174fd734c6fSOliver Hartkopp
175fd734c6fSOliver Hartkopp card->base_addr = ioremap(base, EMS_PCMCIA_MEM_SIZE);
176fd734c6fSOliver Hartkopp if (!card->base_addr) {
177fd734c6fSOliver Hartkopp err = -ENOMEM;
178fd734c6fSOliver Hartkopp goto failure_cleanup;
179fd734c6fSOliver Hartkopp }
180fd734c6fSOliver Hartkopp
181fd734c6fSOliver Hartkopp /* Check for unique EMS CAN signature */
182fd734c6fSOliver Hartkopp if (readw(card->base_addr) != 0xAA55) {
183fd734c6fSOliver Hartkopp err = -ENODEV;
184fd734c6fSOliver Hartkopp goto failure_cleanup;
185fd734c6fSOliver Hartkopp }
186fd734c6fSOliver Hartkopp
187fd734c6fSOliver Hartkopp /* Request board reset */
188fd734c6fSOliver Hartkopp writeb(EMS_CMD_RESET, card->base_addr);
189fd734c6fSOliver Hartkopp
190fd734c6fSOliver Hartkopp /* Make sure CAN controllers are mapped into card's memory space */
191fd734c6fSOliver Hartkopp writeb(EMS_CMD_MAP, card->base_addr);
192fd734c6fSOliver Hartkopp
193fd734c6fSOliver Hartkopp /* Detect available channels */
194fd734c6fSOliver Hartkopp for (i = 0; i < EMS_PCMCIA_MAX_CHAN; i++) {
195fd734c6fSOliver Hartkopp dev = alloc_sja1000dev(0);
196fd734c6fSOliver Hartkopp if (!dev) {
197fd734c6fSOliver Hartkopp err = -ENOMEM;
198fd734c6fSOliver Hartkopp goto failure_cleanup;
199fd734c6fSOliver Hartkopp }
200fd734c6fSOliver Hartkopp
201fd734c6fSOliver Hartkopp card->net_dev[i] = dev;
202fd734c6fSOliver Hartkopp priv = netdev_priv(dev);
203fd734c6fSOliver Hartkopp priv->priv = card;
204fd734c6fSOliver Hartkopp SET_NETDEV_DEV(dev, &pdev->dev);
2053e66d013SChristopher R. Baker dev->dev_id = i;
206fd734c6fSOliver Hartkopp
207fd734c6fSOliver Hartkopp priv->irq_flags = IRQF_SHARED;
208fd734c6fSOliver Hartkopp dev->irq = pdev->irq;
209fd734c6fSOliver Hartkopp priv->reg_base = card->base_addr + EMS_PCMCIA_CAN_BASE_OFFSET +
210fd734c6fSOliver Hartkopp (i * EMS_PCMCIA_CAN_CTRL_SIZE);
211fd734c6fSOliver Hartkopp
212fd734c6fSOliver Hartkopp /* Check if channel is present */
213fd734c6fSOliver Hartkopp if (ems_pcmcia_check_chan(priv)) {
214fd734c6fSOliver Hartkopp priv->read_reg = ems_pcmcia_read_reg;
215fd734c6fSOliver Hartkopp priv->write_reg = ems_pcmcia_write_reg;
216fd734c6fSOliver Hartkopp priv->can.clock.freq = EMS_PCMCIA_CAN_CLOCK;
217fd734c6fSOliver Hartkopp priv->ocr = EMS_PCMCIA_OCR;
218fd734c6fSOliver Hartkopp priv->cdr = EMS_PCMCIA_CDR;
219fd734c6fSOliver Hartkopp priv->flags |= SJA1000_CUSTOM_IRQ_HANDLER;
220fd734c6fSOliver Hartkopp
221fd734c6fSOliver Hartkopp /* Register SJA1000 device */
222fd734c6fSOliver Hartkopp err = register_sja1000dev(dev);
223fd734c6fSOliver Hartkopp if (err) {
224fd734c6fSOliver Hartkopp free_sja1000dev(dev);
225fd734c6fSOliver Hartkopp goto failure_cleanup;
226fd734c6fSOliver Hartkopp }
227fd734c6fSOliver Hartkopp
228fd734c6fSOliver Hartkopp card->channels++;
229fd734c6fSOliver Hartkopp
230fd734c6fSOliver Hartkopp printk(KERN_INFO "%s: registered %s on channel "
231fd734c6fSOliver Hartkopp "#%d at 0x%p, irq %d\n", DRV_NAME, dev->name,
232fd734c6fSOliver Hartkopp i, priv->reg_base, dev->irq);
233fd734c6fSOliver Hartkopp } else
234fd734c6fSOliver Hartkopp free_sja1000dev(dev);
235fd734c6fSOliver Hartkopp }
236fd734c6fSOliver Hartkopp
237*3ec6ca6bSDan Carpenter if (!card->channels) {
238*3ec6ca6bSDan Carpenter err = -ENODEV;
239*3ec6ca6bSDan Carpenter goto failure_cleanup;
240*3ec6ca6bSDan Carpenter }
241*3ec6ca6bSDan Carpenter
242*3ec6ca6bSDan Carpenter err = request_irq(pdev->irq, &ems_pcmcia_interrupt, IRQF_SHARED,
243fd734c6fSOliver Hartkopp DRV_NAME, card);
244fd734c6fSOliver Hartkopp if (!err)
245fd734c6fSOliver Hartkopp return 0;
246fd734c6fSOliver Hartkopp
247fd734c6fSOliver Hartkopp failure_cleanup:
248fd734c6fSOliver Hartkopp ems_pcmcia_del_card(pdev);
249fd734c6fSOliver Hartkopp return err;
250fd734c6fSOliver Hartkopp }
251fd734c6fSOliver Hartkopp
252fd734c6fSOliver Hartkopp /*
253fd734c6fSOliver Hartkopp * Setup PCMCIA socket and probe for EMS CPC-CARD
254fd734c6fSOliver Hartkopp */
ems_pcmcia_probe(struct pcmcia_device * dev)2553c8ac0f2SBill Pemberton static int ems_pcmcia_probe(struct pcmcia_device *dev)
256fd734c6fSOliver Hartkopp {
257fd734c6fSOliver Hartkopp int csval;
258fd734c6fSOliver Hartkopp
259fd734c6fSOliver Hartkopp /* General socket configuration */
260fd734c6fSOliver Hartkopp dev->config_flags |= CONF_ENABLE_IRQ;
261fd734c6fSOliver Hartkopp dev->config_index = 1;
262fd734c6fSOliver Hartkopp dev->config_regs = PRESENT_OPTION;
263fd734c6fSOliver Hartkopp
264fd734c6fSOliver Hartkopp /* The io structure describes IO port mapping */
265fd734c6fSOliver Hartkopp dev->resource[0]->end = 16;
266fd734c6fSOliver Hartkopp dev->resource[0]->flags |= IO_DATA_PATH_WIDTH_8;
267fd734c6fSOliver Hartkopp dev->resource[1]->end = 16;
268fd734c6fSOliver Hartkopp dev->resource[1]->flags |= IO_DATA_PATH_WIDTH_16;
269fd734c6fSOliver Hartkopp dev->io_lines = 5;
270fd734c6fSOliver Hartkopp
271fd734c6fSOliver Hartkopp /* Allocate a memory window */
272fd734c6fSOliver Hartkopp dev->resource[2]->flags =
273fd734c6fSOliver Hartkopp (WIN_DATA_WIDTH_8 | WIN_MEMORY_TYPE_CM | WIN_ENABLE);
274fd734c6fSOliver Hartkopp dev->resource[2]->start = dev->resource[2]->end = 0;
275fd734c6fSOliver Hartkopp
276fd734c6fSOliver Hartkopp csval = pcmcia_request_window(dev, dev->resource[2], 0);
277fd734c6fSOliver Hartkopp if (csval) {
278fd734c6fSOliver Hartkopp dev_err(&dev->dev, "pcmcia_request_window failed (err=%d)\n",
279fd734c6fSOliver Hartkopp csval);
280fd734c6fSOliver Hartkopp return 0;
281fd734c6fSOliver Hartkopp }
282fd734c6fSOliver Hartkopp
283fd734c6fSOliver Hartkopp csval = pcmcia_map_mem_page(dev, dev->resource[2], dev->config_base);
284fd734c6fSOliver Hartkopp if (csval) {
285fd734c6fSOliver Hartkopp dev_err(&dev->dev, "pcmcia_map_mem_page failed (err=%d)\n",
286fd734c6fSOliver Hartkopp csval);
287fd734c6fSOliver Hartkopp return 0;
288fd734c6fSOliver Hartkopp }
289fd734c6fSOliver Hartkopp
290fd734c6fSOliver Hartkopp csval = pcmcia_enable_device(dev);
291fd734c6fSOliver Hartkopp if (csval) {
292fd734c6fSOliver Hartkopp dev_err(&dev->dev, "pcmcia_enable_device failed (err=%d)\n",
293fd734c6fSOliver Hartkopp csval);
294fd734c6fSOliver Hartkopp return 0;
295fd734c6fSOliver Hartkopp }
296fd734c6fSOliver Hartkopp
297fd734c6fSOliver Hartkopp ems_pcmcia_add_card(dev, dev->resource[2]->start);
298fd734c6fSOliver Hartkopp return 0;
299fd734c6fSOliver Hartkopp }
300fd734c6fSOliver Hartkopp
301fd734c6fSOliver Hartkopp /*
302fd734c6fSOliver Hartkopp * Release claimed resources
303fd734c6fSOliver Hartkopp */
ems_pcmcia_remove(struct pcmcia_device * dev)304fd734c6fSOliver Hartkopp static void ems_pcmcia_remove(struct pcmcia_device *dev)
305fd734c6fSOliver Hartkopp {
306fd734c6fSOliver Hartkopp ems_pcmcia_del_card(dev);
307fd734c6fSOliver Hartkopp pcmcia_disable_device(dev);
308fd734c6fSOliver Hartkopp }
309fd734c6fSOliver Hartkopp
310fd734c6fSOliver Hartkopp static struct pcmcia_driver ems_pcmcia_driver = {
311fd734c6fSOliver Hartkopp .name = DRV_NAME,
312fd734c6fSOliver Hartkopp .probe = ems_pcmcia_probe,
313fd734c6fSOliver Hartkopp .remove = ems_pcmcia_remove,
314fd734c6fSOliver Hartkopp .id_table = ems_pcmcia_tbl,
315fd734c6fSOliver Hartkopp };
316fdd3f29eSH Hartley Sweeten module_pcmcia_driver(ems_pcmcia_driver);
317