Linux 3.11-rc3
[cris-mirror.git] / drivers / net / can / sja1000 / sja1000_platform.c
blob8e259c541036c575fc181796ebfe21f0c8798a1d
1 /*
2 * Copyright (C) 2005 Sascha Hauer, Pengutronix
3 * Copyright (C) 2007 Wolfgang Grandegger <wg@grandegger.com>
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
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.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 #include <linux/kernel.h>
20 #include <linux/module.h>
21 #include <linux/interrupt.h>
22 #include <linux/netdevice.h>
23 #include <linux/delay.h>
24 #include <linux/pci.h>
25 #include <linux/platform_device.h>
26 #include <linux/irq.h>
27 #include <linux/can/dev.h>
28 #include <linux/can/platform/sja1000.h>
29 #include <linux/io.h>
31 #include "sja1000.h"
33 #define DRV_NAME "sja1000_platform"
35 MODULE_AUTHOR("Sascha Hauer <s.hauer@pengutronix.de>");
36 MODULE_DESCRIPTION("Socket-CAN driver for SJA1000 on the platform bus");
37 MODULE_ALIAS("platform:" DRV_NAME);
38 MODULE_LICENSE("GPL v2");
40 static u8 sp_read_reg8(const struct sja1000_priv *priv, int reg)
42 return ioread8(priv->reg_base + reg);
45 static void sp_write_reg8(const struct sja1000_priv *priv, int reg, u8 val)
47 iowrite8(val, priv->reg_base + reg);
50 static u8 sp_read_reg16(const struct sja1000_priv *priv, int reg)
52 return ioread8(priv->reg_base + reg * 2);
55 static void sp_write_reg16(const struct sja1000_priv *priv, int reg, u8 val)
57 iowrite8(val, priv->reg_base + reg * 2);
60 static u8 sp_read_reg32(const struct sja1000_priv *priv, int reg)
62 return ioread8(priv->reg_base + reg * 4);
65 static void sp_write_reg32(const struct sja1000_priv *priv, int reg, u8 val)
67 iowrite8(val, priv->reg_base + reg * 4);
70 static int sp_probe(struct platform_device *pdev)
72 int err;
73 void __iomem *addr;
74 struct net_device *dev;
75 struct sja1000_priv *priv;
76 struct resource *res_mem, *res_irq;
77 struct sja1000_platform_data *pdata;
79 pdata = pdev->dev.platform_data;
80 if (!pdata) {
81 dev_err(&pdev->dev, "No platform data provided!\n");
82 err = -ENODEV;
83 goto exit;
86 res_mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
87 res_irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
88 if (!res_mem || !res_irq) {
89 err = -ENODEV;
90 goto exit;
93 if (!request_mem_region(res_mem->start, resource_size(res_mem),
94 DRV_NAME)) {
95 err = -EBUSY;
96 goto exit;
99 addr = ioremap_nocache(res_mem->start, resource_size(res_mem));
100 if (!addr) {
101 err = -ENOMEM;
102 goto exit_release;
105 dev = alloc_sja1000dev(0);
106 if (!dev) {
107 err = -ENOMEM;
108 goto exit_iounmap;
110 priv = netdev_priv(dev);
112 dev->irq = res_irq->start;
113 priv->irq_flags = res_irq->flags & IRQF_TRIGGER_MASK;
114 if (res_irq->flags & IORESOURCE_IRQ_SHAREABLE)
115 priv->irq_flags |= IRQF_SHARED;
116 priv->reg_base = addr;
117 /* The CAN clock frequency is half the oscillator clock frequency */
118 priv->can.clock.freq = pdata->osc_freq / 2;
119 priv->ocr = pdata->ocr;
120 priv->cdr = pdata->cdr;
122 switch (res_mem->flags & IORESOURCE_MEM_TYPE_MASK) {
123 case IORESOURCE_MEM_32BIT:
124 priv->read_reg = sp_read_reg32;
125 priv->write_reg = sp_write_reg32;
126 break;
127 case IORESOURCE_MEM_16BIT:
128 priv->read_reg = sp_read_reg16;
129 priv->write_reg = sp_write_reg16;
130 break;
131 case IORESOURCE_MEM_8BIT:
132 default:
133 priv->read_reg = sp_read_reg8;
134 priv->write_reg = sp_write_reg8;
135 break;
138 platform_set_drvdata(pdev, dev);
139 SET_NETDEV_DEV(dev, &pdev->dev);
141 err = register_sja1000dev(dev);
142 if (err) {
143 dev_err(&pdev->dev, "registering %s failed (err=%d)\n",
144 DRV_NAME, err);
145 goto exit_free;
148 dev_info(&pdev->dev, "%s device registered (reg_base=%p, irq=%d)\n",
149 DRV_NAME, priv->reg_base, dev->irq);
150 return 0;
152 exit_free:
153 free_sja1000dev(dev);
154 exit_iounmap:
155 iounmap(addr);
156 exit_release:
157 release_mem_region(res_mem->start, resource_size(res_mem));
158 exit:
159 return err;
162 static int sp_remove(struct platform_device *pdev)
164 struct net_device *dev = platform_get_drvdata(pdev);
165 struct sja1000_priv *priv = netdev_priv(dev);
166 struct resource *res;
168 unregister_sja1000dev(dev);
170 if (priv->reg_base)
171 iounmap(priv->reg_base);
173 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
174 release_mem_region(res->start, resource_size(res));
176 free_sja1000dev(dev);
178 return 0;
181 static struct platform_driver sp_driver = {
182 .probe = sp_probe,
183 .remove = sp_remove,
184 .driver = {
185 .name = DRV_NAME,
186 .owner = THIS_MODULE,
190 module_platform_driver(sp_driver);