Full support for Ginger Console
[linux-ginger.git] / drivers / net / can / sja1000 / sja1000_of_platform.c
blob9dd076a626a5b329b1ff368f3682daabe516acd9
1 /*
2 * Driver for SJA1000 CAN controllers on the OpenFirmware platform bus
4 * Copyright (C) 2008-2009 Wolfgang Grandegger <wg@grandegger.com>
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
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.
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.
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:
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 * };
32 * See "Documentation/powerpc/dts-bindings/can/sja1000.txt" for further
33 * information.
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>
41 #include <linux/can.h>
42 #include <linux/can/dev.h>
44 #include <linux/of_platform.h>
45 #include <asm/prom.h>
47 #include "sja1000.h"
49 #define DRV_NAME "sja1000_of_platform"
51 MODULE_AUTHOR("Wolfgang Grandegger <wg@grandegger.com>");
52 MODULE_DESCRIPTION("Socket-CAN driver for SJA1000 on the OF platform bus");
53 MODULE_LICENSE("GPL v2");
55 #define SJA1000_OFP_CAN_CLOCK (16000000 / 2)
57 #define SJA1000_OFP_OCR OCR_TX0_PULLDOWN
58 #define SJA1000_OFP_CDR (CDR_CBP | CDR_CLK_OFF)
60 static u8 sja1000_ofp_read_reg(const struct sja1000_priv *priv, int reg)
62 return in_8(priv->reg_base + reg);
65 static void sja1000_ofp_write_reg(const struct sja1000_priv *priv,
66 int reg, u8 val)
68 out_8(priv->reg_base + reg, val);
71 static int __devexit sja1000_ofp_remove(struct of_device *ofdev)
73 struct net_device *dev = dev_get_drvdata(&ofdev->dev);
74 struct sja1000_priv *priv = netdev_priv(dev);
75 struct device_node *np = ofdev->node;
76 struct resource res;
78 dev_set_drvdata(&ofdev->dev, NULL);
80 unregister_sja1000dev(dev);
81 free_sja1000dev(dev);
82 iounmap(priv->reg_base);
83 irq_dispose_mapping(dev->irq);
85 of_address_to_resource(np, 0, &res);
86 release_mem_region(res.start, resource_size(&res));
88 return 0;
91 static int __devinit sja1000_ofp_probe(struct of_device *ofdev,
92 const struct of_device_id *id)
94 struct device_node *np = ofdev->node;
95 struct net_device *dev;
96 struct sja1000_priv *priv;
97 struct resource res;
98 const u32 *prop;
99 int err, irq, res_size, prop_size;
100 void __iomem *base;
102 err = of_address_to_resource(np, 0, &res);
103 if (err) {
104 dev_err(&ofdev->dev, "invalid address\n");
105 return err;
108 res_size = resource_size(&res);
110 if (!request_mem_region(res.start, res_size, DRV_NAME)) {
111 dev_err(&ofdev->dev, "couldn't request %#llx..%#llx\n",
112 (unsigned long long)res.start,
113 (unsigned long long)res.end);
114 return -EBUSY;
117 base = ioremap_nocache(res.start, res_size);
118 if (!base) {
119 dev_err(&ofdev->dev, "couldn't ioremap %#llx..%#llx\n",
120 (unsigned long long)res.start,
121 (unsigned long long)res.end);
122 err = -ENOMEM;
123 goto exit_release_mem;
126 irq = irq_of_parse_and_map(np, 0);
127 if (irq == NO_IRQ) {
128 dev_err(&ofdev->dev, "no irq found\n");
129 err = -ENODEV;
130 goto exit_unmap_mem;
133 dev = alloc_sja1000dev(0);
134 if (!dev) {
135 err = -ENOMEM;
136 goto exit_dispose_irq;
139 priv = netdev_priv(dev);
141 priv->read_reg = sja1000_ofp_read_reg;
142 priv->write_reg = sja1000_ofp_write_reg;
144 prop = of_get_property(np, "nxp,external-clock-frequency", &prop_size);
145 if (prop && (prop_size == sizeof(u32)))
146 priv->can.clock.freq = *prop / 2;
147 else
148 priv->can.clock.freq = SJA1000_OFP_CAN_CLOCK; /* default */
150 prop = of_get_property(np, "nxp,tx-output-mode", &prop_size);
151 if (prop && (prop_size == sizeof(u32)))
152 priv->ocr |= *prop & OCR_MODE_MASK;
153 else
154 priv->ocr |= OCR_MODE_NORMAL; /* default */
156 prop = of_get_property(np, "nxp,tx-output-config", &prop_size);
157 if (prop && (prop_size == sizeof(u32)))
158 priv->ocr |= (*prop << OCR_TX_SHIFT) & OCR_TX_MASK;
159 else
160 priv->ocr |= OCR_TX0_PULLDOWN; /* default */
162 prop = of_get_property(np, "nxp,clock-out-frequency", &prop_size);
163 if (prop && (prop_size == sizeof(u32)) && *prop) {
164 u32 divider = priv->can.clock.freq * 2 / *prop;
166 if (divider > 1)
167 priv->cdr |= divider / 2 - 1;
168 else
169 priv->cdr |= CDR_CLKOUT_MASK;
170 } else {
171 priv->cdr |= CDR_CLK_OFF; /* default */
174 prop = of_get_property(np, "nxp,no-comparator-bypass", NULL);
175 if (!prop)
176 priv->cdr |= CDR_CBP; /* default */
178 priv->irq_flags = IRQF_SHARED;
179 priv->reg_base = base;
181 dev->irq = irq;
183 dev_info(&ofdev->dev,
184 "reg_base=0x%p irq=%d clock=%d ocr=0x%02x cdr=0x%02x\n",
185 priv->reg_base, dev->irq, priv->can.clock.freq,
186 priv->ocr, priv->cdr);
188 dev_set_drvdata(&ofdev->dev, dev);
189 SET_NETDEV_DEV(dev, &ofdev->dev);
191 err = register_sja1000dev(dev);
192 if (err) {
193 dev_err(&ofdev->dev, "registering %s failed (err=%d)\n",
194 DRV_NAME, err);
195 goto exit_free_sja1000;
198 return 0;
200 exit_free_sja1000:
201 free_sja1000dev(dev);
202 exit_dispose_irq:
203 irq_dispose_mapping(irq);
204 exit_unmap_mem:
205 iounmap(base);
206 exit_release_mem:
207 release_mem_region(res.start, res_size);
209 return err;
212 static struct of_device_id __devinitdata sja1000_ofp_table[] = {
213 {.compatible = "nxp,sja1000"},
216 MODULE_DEVICE_TABLE(of, sja1000_ofp_table);
218 static struct of_platform_driver sja1000_ofp_driver = {
219 .owner = THIS_MODULE,
220 .name = DRV_NAME,
221 .probe = sja1000_ofp_probe,
222 .remove = __devexit_p(sja1000_ofp_remove),
223 .match_table = sja1000_ofp_table,
226 static int __init sja1000_ofp_init(void)
228 return of_register_platform_driver(&sja1000_ofp_driver);
230 module_init(sja1000_ofp_init);
232 static void __exit sja1000_ofp_exit(void)
234 return of_unregister_platform_driver(&sja1000_ofp_driver);
236 module_exit(sja1000_ofp_exit);