sja1000_platform.c revision 342180f7dcfb00e4019a0cd0f0e9bfd0c186c710
1/*
2 * Copyright (C) 2005 Sascha Hauer, Pengutronix
3 * Copyright (C) 2007 Wolfgang Grandegger <wg@grandegger.com>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the version 2 of the GNU General Public License
7 * as published by the Free Software Foundation
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, see <http://www.gnu.org/licenses/>.
16 */
17
18#include <linux/kernel.h>
19#include <linux/module.h>
20#include <linux/interrupt.h>
21#include <linux/netdevice.h>
22#include <linux/delay.h>
23#include <linux/pci.h>
24#include <linux/platform_device.h>
25#include <linux/irq.h>
26#include <linux/can/dev.h>
27#include <linux/can/platform/sja1000.h>
28#include <linux/io.h>
29
30#include "sja1000.h"
31
32#define DRV_NAME "sja1000_platform"
33
34MODULE_AUTHOR("Sascha Hauer <s.hauer@pengutronix.de>");
35MODULE_DESCRIPTION("Socket-CAN driver for SJA1000 on the platform bus");
36MODULE_ALIAS("platform:" DRV_NAME);
37MODULE_LICENSE("GPL v2");
38
39static u8 sp_read_reg8(const struct sja1000_priv *priv, int reg)
40{
41	return ioread8(priv->reg_base + reg);
42}
43
44static void sp_write_reg8(const struct sja1000_priv *priv, int reg, u8 val)
45{
46	iowrite8(val, priv->reg_base + reg);
47}
48
49static u8 sp_read_reg16(const struct sja1000_priv *priv, int reg)
50{
51	return ioread8(priv->reg_base + reg * 2);
52}
53
54static void sp_write_reg16(const struct sja1000_priv *priv, int reg, u8 val)
55{
56	iowrite8(val, priv->reg_base + reg * 2);
57}
58
59static u8 sp_read_reg32(const struct sja1000_priv *priv, int reg)
60{
61	return ioread8(priv->reg_base + reg * 4);
62}
63
64static void sp_write_reg32(const struct sja1000_priv *priv, int reg, u8 val)
65{
66	iowrite8(val, priv->reg_base + reg * 4);
67}
68
69static int sp_probe(struct platform_device *pdev)
70{
71	int err;
72	void __iomem *addr;
73	struct net_device *dev;
74	struct sja1000_priv *priv;
75	struct resource *res_mem, *res_irq;
76	struct sja1000_platform_data *pdata;
77
78	pdata = dev_get_platdata(&pdev->dev);
79	if (!pdata) {
80		dev_err(&pdev->dev, "No platform data provided!\n");
81		return -ENODEV;
82	}
83
84	res_mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
85	res_irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
86	if (!res_mem || !res_irq)
87		return -ENODEV;
88
89	if (!devm_request_mem_region(&pdev->dev, res_mem->start,
90				     resource_size(res_mem), DRV_NAME))
91		return -EBUSY;
92
93	addr = devm_ioremap_nocache(&pdev->dev, res_mem->start,
94				    resource_size(res_mem));
95	if (!addr)
96		return -ENOMEM;
97
98	dev = alloc_sja1000dev(0);
99	if (!dev)
100		return -ENOMEM;
101	priv = netdev_priv(dev);
102
103	dev->irq = res_irq->start;
104	priv->irq_flags = res_irq->flags & IRQF_TRIGGER_MASK;
105	if (res_irq->flags & IORESOURCE_IRQ_SHAREABLE)
106		priv->irq_flags |= IRQF_SHARED;
107	priv->reg_base = addr;
108	/* The CAN clock frequency is half the oscillator clock frequency */
109	priv->can.clock.freq = pdata->osc_freq / 2;
110	priv->ocr = pdata->ocr;
111	priv->cdr = pdata->cdr;
112
113	switch (res_mem->flags & IORESOURCE_MEM_TYPE_MASK) {
114	case IORESOURCE_MEM_32BIT:
115		priv->read_reg = sp_read_reg32;
116		priv->write_reg = sp_write_reg32;
117		break;
118	case IORESOURCE_MEM_16BIT:
119		priv->read_reg = sp_read_reg16;
120		priv->write_reg = sp_write_reg16;
121		break;
122	case IORESOURCE_MEM_8BIT:
123	default:
124		priv->read_reg = sp_read_reg8;
125		priv->write_reg = sp_write_reg8;
126		break;
127	}
128
129	platform_set_drvdata(pdev, dev);
130	SET_NETDEV_DEV(dev, &pdev->dev);
131
132	err = register_sja1000dev(dev);
133	if (err) {
134		dev_err(&pdev->dev, "registering %s failed (err=%d)\n",
135			DRV_NAME, err);
136		goto exit_free;
137	}
138
139	dev_info(&pdev->dev, "%s device registered (reg_base=%p, irq=%d)\n",
140		 DRV_NAME, priv->reg_base, dev->irq);
141	return 0;
142
143 exit_free:
144	free_sja1000dev(dev);
145	return err;
146}
147
148static int sp_remove(struct platform_device *pdev)
149{
150	struct net_device *dev = platform_get_drvdata(pdev);
151
152	unregister_sja1000dev(dev);
153	free_sja1000dev(dev);
154
155	return 0;
156}
157
158static struct platform_driver sp_driver = {
159	.probe = sp_probe,
160	.remove = sp_remove,
161	.driver = {
162		.name = DRV_NAME,
163		.owner = THIS_MODULE,
164	},
165};
166
167module_platform_driver(sp_driver);
168