Linux Audio

Check our new training course

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