]> bbs.cooldavid.org Git - net-next-2.6.git/blame - drivers/net/can/sja1000/sja1000_of_platform.c
Merge branch 'drm-core-next' of git://git.kernel.org/pub/scm/linux/kernel/git/airlied...
[net-next-2.6.git] / drivers / net / can / sja1000 / sja1000_of_platform.c
CommitLineData
d1a277c5
WG
1/*
2 * Driver for SJA1000 CAN controllers on the OpenFirmware platform bus
3 *
4 * Copyright (C) 2008-2009 Wolfgang Grandegger <wg@grandegger.com>
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 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software Foundation,
17 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 */
19
20/* This is a generic driver for SJA1000 chips on the OpenFirmware platform
21 * bus found on embedded PowerPC systems. You need a SJA1000 CAN node
22 * definition in your flattened device tree source (DTS) file similar to:
23 *
24 * can@3,100 {
25 * compatible = "nxp,sja1000";
26 * reg = <3 0x100 0x80>;
27 * interrupts = <2 0>;
28 * interrupt-parent = <&mpic>;
29 * nxp,external-clock-frequency = <16000000>;
30 * };
31 *
32 * See "Documentation/powerpc/dts-bindings/can/sja1000.txt" for further
33 * information.
34 */
35
36#include <linux/kernel.h>
37#include <linux/module.h>
38#include <linux/interrupt.h>
39#include <linux/netdevice.h>
40#include <linux/delay.h>
d1a277c5
WG
41#include <linux/can/dev.h>
42
43#include <linux/of_platform.h>
44#include <asm/prom.h>
45
46#include "sja1000.h"
47
48#define DRV_NAME "sja1000_of_platform"
49
50MODULE_AUTHOR("Wolfgang Grandegger <wg@grandegger.com>");
51MODULE_DESCRIPTION("Socket-CAN driver for SJA1000 on the OF platform bus");
52MODULE_LICENSE("GPL v2");
53
54#define SJA1000_OFP_CAN_CLOCK (16000000 / 2)
55
56#define SJA1000_OFP_OCR OCR_TX0_PULLDOWN
57#define SJA1000_OFP_CDR (CDR_CBP | CDR_CLK_OFF)
58
59static u8 sja1000_ofp_read_reg(const struct sja1000_priv *priv, int reg)
60{
61 return in_8(priv->reg_base + reg);
62}
63
64static void sja1000_ofp_write_reg(const struct sja1000_priv *priv,
65 int reg, u8 val)
66{
67 out_8(priv->reg_base + reg, val);
68}
69
70static int __devexit sja1000_ofp_remove(struct of_device *ofdev)
71{
72 struct net_device *dev = dev_get_drvdata(&ofdev->dev);
73 struct sja1000_priv *priv = netdev_priv(dev);
61c7a080 74 struct device_node *np = ofdev->dev.of_node;
d1a277c5
WG
75 struct resource res;
76
77 dev_set_drvdata(&ofdev->dev, NULL);
78
79 unregister_sja1000dev(dev);
80 free_sja1000dev(dev);
81 iounmap(priv->reg_base);
82 irq_dispose_mapping(dev->irq);
83
84 of_address_to_resource(np, 0, &res);
85 release_mem_region(res.start, resource_size(&res));
86
87 return 0;
88}
89
90static int __devinit sja1000_ofp_probe(struct of_device *ofdev,
91 const struct of_device_id *id)
92{
61c7a080 93 struct device_node *np = ofdev->dev.of_node;
d1a277c5
WG
94 struct net_device *dev;
95 struct sja1000_priv *priv;
96 struct resource res;
97 const u32 *prop;
98 int err, irq, res_size, prop_size;
99 void __iomem *base;
100
101 err = of_address_to_resource(np, 0, &res);
102 if (err) {
103 dev_err(&ofdev->dev, "invalid address\n");
104 return err;
105 }
106
107 res_size = resource_size(&res);
108
109 if (!request_mem_region(res.start, res_size, DRV_NAME)) {
51611a12
WG
110 dev_err(&ofdev->dev, "couldn't request %#llx..%#llx\n",
111 (unsigned long long)res.start,
112 (unsigned long long)res.end);
d1a277c5
WG
113 return -EBUSY;
114 }
115
116 base = ioremap_nocache(res.start, res_size);
117 if (!base) {
51611a12
WG
118 dev_err(&ofdev->dev, "couldn't ioremap %#llx..%#llx\n",
119 (unsigned long long)res.start,
120 (unsigned long long)res.end);
d1a277c5
WG
121 err = -ENOMEM;
122 goto exit_release_mem;
123 }
124
125 irq = irq_of_parse_and_map(np, 0);
126 if (irq == NO_IRQ) {
127 dev_err(&ofdev->dev, "no irq found\n");
128 err = -ENODEV;
129 goto exit_unmap_mem;
130 }
131
132 dev = alloc_sja1000dev(0);
133 if (!dev) {
134 err = -ENOMEM;
135 goto exit_dispose_irq;
136 }
137
138 priv = netdev_priv(dev);
139
140 priv->read_reg = sja1000_ofp_read_reg;
141 priv->write_reg = sja1000_ofp_write_reg;
142
143 prop = of_get_property(np, "nxp,external-clock-frequency", &prop_size);
144 if (prop && (prop_size == sizeof(u32)))
145 priv->can.clock.freq = *prop / 2;
146 else
147 priv->can.clock.freq = SJA1000_OFP_CAN_CLOCK; /* default */
148
149 prop = of_get_property(np, "nxp,tx-output-mode", &prop_size);
150 if (prop && (prop_size == sizeof(u32)))
151 priv->ocr |= *prop & OCR_MODE_MASK;
152 else
153 priv->ocr |= OCR_MODE_NORMAL; /* default */
154
155 prop = of_get_property(np, "nxp,tx-output-config", &prop_size);
156 if (prop && (prop_size == sizeof(u32)))
157 priv->ocr |= (*prop << OCR_TX_SHIFT) & OCR_TX_MASK;
158 else
159 priv->ocr |= OCR_TX0_PULLDOWN; /* default */
160
161 prop = of_get_property(np, "nxp,clock-out-frequency", &prop_size);
162 if (prop && (prop_size == sizeof(u32)) && *prop) {
163 u32 divider = priv->can.clock.freq * 2 / *prop;
164
165 if (divider > 1)
166 priv->cdr |= divider / 2 - 1;
167 else
168 priv->cdr |= CDR_CLKOUT_MASK;
169 } else {
170 priv->cdr |= CDR_CLK_OFF; /* default */
171 }
172
173 prop = of_get_property(np, "nxp,no-comparator-bypass", NULL);
174 if (!prop)
175 priv->cdr |= CDR_CBP; /* default */
176
177 priv->irq_flags = IRQF_SHARED;
178 priv->reg_base = base;
179
180 dev->irq = irq;
181
182 dev_info(&ofdev->dev,
183 "reg_base=0x%p irq=%d clock=%d ocr=0x%02x cdr=0x%02x\n",
184 priv->reg_base, dev->irq, priv->can.clock.freq,
185 priv->ocr, priv->cdr);
186
187 dev_set_drvdata(&ofdev->dev, dev);
188 SET_NETDEV_DEV(dev, &ofdev->dev);
189
190 err = register_sja1000dev(dev);
191 if (err) {
192 dev_err(&ofdev->dev, "registering %s failed (err=%d)\n",
193 DRV_NAME, err);
194 goto exit_free_sja1000;
195 }
196
197 return 0;
198
199exit_free_sja1000:
200 free_sja1000dev(dev);
201exit_dispose_irq:
202 irq_dispose_mapping(irq);
203exit_unmap_mem:
204 iounmap(base);
205exit_release_mem:
206 release_mem_region(res.start, res_size);
207
208 return err;
209}
210
211static struct of_device_id __devinitdata sja1000_ofp_table[] = {
212 {.compatible = "nxp,sja1000"},
213 {},
214};
e72701ac 215MODULE_DEVICE_TABLE(of, sja1000_ofp_table);
d1a277c5
WG
216
217static struct of_platform_driver sja1000_ofp_driver = {
4018294b
GL
218 .driver = {
219 .owner = THIS_MODULE,
220 .name = DRV_NAME,
221 .of_match_table = sja1000_ofp_table,
222 },
d1a277c5
WG
223 .probe = sja1000_ofp_probe,
224 .remove = __devexit_p(sja1000_ofp_remove),
d1a277c5
WG
225};
226
227static int __init sja1000_ofp_init(void)
228{
229 return of_register_platform_driver(&sja1000_ofp_driver);
230}
231module_init(sja1000_ofp_init);
232
233static void __exit sja1000_ofp_exit(void)
234{
235 return of_unregister_platform_driver(&sja1000_ofp_driver);
236};
237module_exit(sja1000_ofp_exit);