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>
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_LICENSE("GPL v2");
39 static u8
sp_read_reg8(const struct sja1000_priv
*priv
, int reg
)
41 return ioread8(priv
->reg_base
+ reg
);
44 static void sp_write_reg8(const struct sja1000_priv
*priv
, int reg
, u8 val
)
46 iowrite8(val
, priv
->reg_base
+ reg
);
49 static u8
sp_read_reg16(const struct sja1000_priv
*priv
, int reg
)
51 return ioread8(priv
->reg_base
+ reg
* 2);
54 static void sp_write_reg16(const struct sja1000_priv
*priv
, int reg
, u8 val
)
56 iowrite8(val
, priv
->reg_base
+ reg
* 2);
59 static u8
sp_read_reg32(const struct sja1000_priv
*priv
, int reg
)
61 return ioread8(priv
->reg_base
+ reg
* 4);
64 static void sp_write_reg32(const struct sja1000_priv
*priv
, int reg
, u8 val
)
66 iowrite8(val
, priv
->reg_base
+ reg
* 4);
69 static int sp_probe(struct platform_device
*pdev
)
73 struct net_device
*dev
;
74 struct sja1000_priv
*priv
;
75 struct resource
*res_mem
, *res_irq
;
76 struct sja1000_platform_data
*pdata
;
78 pdata
= pdev
->dev
.platform_data
;
80 dev_err(&pdev
->dev
, "No platform data provided!\n");
85 res_mem
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
86 res_irq
= platform_get_resource(pdev
, IORESOURCE_IRQ
, 0);
87 if (!res_mem
|| !res_irq
) {
92 if (!request_mem_region(res_mem
->start
, resource_size(res_mem
),
98 addr
= ioremap_nocache(res_mem
->start
, resource_size(res_mem
));
104 dev
= alloc_sja1000dev(0);
109 priv
= netdev_priv(dev
);
111 dev
->irq
= res_irq
->start
;
112 priv
->irq_flags
= res_irq
->flags
& (IRQF_TRIGGER_MASK
| IRQF_SHARED
);
113 priv
->reg_base
= addr
;
114 /* The CAN clock frequency is half the oscillator clock frequency */
115 priv
->can
.clock
.freq
= pdata
->osc_freq
/ 2;
116 priv
->ocr
= pdata
->ocr
;
117 priv
->cdr
= pdata
->cdr
;
119 switch (res_mem
->flags
& IORESOURCE_MEM_TYPE_MASK
) {
120 case IORESOURCE_MEM_32BIT
:
121 priv
->read_reg
= sp_read_reg32
;
122 priv
->write_reg
= sp_write_reg32
;
124 case IORESOURCE_MEM_16BIT
:
125 priv
->read_reg
= sp_read_reg16
;
126 priv
->write_reg
= sp_write_reg16
;
128 case IORESOURCE_MEM_8BIT
:
130 priv
->read_reg
= sp_read_reg8
;
131 priv
->write_reg
= sp_write_reg8
;
135 dev_set_drvdata(&pdev
->dev
, dev
);
136 SET_NETDEV_DEV(dev
, &pdev
->dev
);
138 err
= register_sja1000dev(dev
);
140 dev_err(&pdev
->dev
, "registering %s failed (err=%d)\n",
145 dev_info(&pdev
->dev
, "%s device registered (reg_base=%p, irq=%d)\n",
146 DRV_NAME
, priv
->reg_base
, dev
->irq
);
150 free_sja1000dev(dev
);
154 release_mem_region(res_mem
->start
, resource_size(res_mem
));
159 static int sp_remove(struct platform_device
*pdev
)
161 struct net_device
*dev
= dev_get_drvdata(&pdev
->dev
);
162 struct sja1000_priv
*priv
= netdev_priv(dev
);
163 struct resource
*res
;
165 unregister_sja1000dev(dev
);
166 dev_set_drvdata(&pdev
->dev
, NULL
);
169 iounmap(priv
->reg_base
);
171 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
172 release_mem_region(res
->start
, resource_size(res
));
174 free_sja1000dev(dev
);
179 static struct platform_driver sp_driver
= {
184 .owner
= THIS_MODULE
,
188 static int __init
sp_init(void)
190 return platform_driver_register(&sp_driver
);
193 static void __exit
sp_exit(void)
195 platform_driver_unregister(&sp_driver
);
198 module_init(sp_init
);
199 module_exit(sp_exit
);