Linux-libre 3.2.72-gnu1
[librecmc/linux-libre.git] / drivers / net / can / sja1000 / sja1000_of_platform.c
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/devicetree/bindings/net/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>
41 #include <linux/io.h>
42 #include <linux/can/dev.h>
43
44 #include <linux/of_platform.h>
45 #include <asm/prom.h>
46
47 #include "sja1000.h"
48
49 #define DRV_NAME "sja1000_of_platform"
50
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");
54
55 #define SJA1000_OFP_CAN_CLOCK  (16000000 / 2)
56
57 #define SJA1000_OFP_OCR        OCR_TX0_PULLDOWN
58 #define SJA1000_OFP_CDR        (CDR_CBP | CDR_CLK_OFF)
59
60 static u8 sja1000_ofp_read_reg(const struct sja1000_priv *priv, int reg)
61 {
62         return in_8(priv->reg_base + reg);
63 }
64
65 static void sja1000_ofp_write_reg(const struct sja1000_priv *priv,
66                                   int reg, u8 val)
67 {
68         out_8(priv->reg_base + reg, val);
69 }
70
71 static int __devexit sja1000_ofp_remove(struct platform_device *ofdev)
72 {
73         struct net_device *dev = dev_get_drvdata(&ofdev->dev);
74         struct sja1000_priv *priv = netdev_priv(dev);
75         struct device_node *np = ofdev->dev.of_node;
76         struct resource res;
77
78         dev_set_drvdata(&ofdev->dev, NULL);
79
80         unregister_sja1000dev(dev);
81         free_sja1000dev(dev);
82         iounmap(priv->reg_base);
83         irq_dispose_mapping(dev->irq);
84
85         of_address_to_resource(np, 0, &res);
86         release_mem_region(res.start, resource_size(&res));
87
88         return 0;
89 }
90
91 static int __devinit sja1000_ofp_probe(struct platform_device *ofdev)
92 {
93         struct device_node *np = ofdev->dev.of_node;
94         struct net_device *dev;
95         struct sja1000_priv *priv;
96         struct resource res;
97         u32 prop;
98         int err, irq, res_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)) {
110                 dev_err(&ofdev->dev, "couldn't request %pR\n", &res);
111                 return -EBUSY;
112         }
113
114         base = ioremap_nocache(res.start, res_size);
115         if (!base) {
116                 dev_err(&ofdev->dev, "couldn't ioremap %pR\n", &res);
117                 err = -ENOMEM;
118                 goto exit_release_mem;
119         }
120
121         irq = irq_of_parse_and_map(np, 0);
122         if (irq == NO_IRQ) {
123                 dev_err(&ofdev->dev, "no irq found\n");
124                 err = -ENODEV;
125                 goto exit_unmap_mem;
126         }
127
128         dev = alloc_sja1000dev(0);
129         if (!dev) {
130                 err = -ENOMEM;
131                 goto exit_dispose_irq;
132         }
133
134         priv = netdev_priv(dev);
135
136         priv->read_reg = sja1000_ofp_read_reg;
137         priv->write_reg = sja1000_ofp_write_reg;
138
139         err = of_property_read_u32(np, "nxp,external-clock-frequency", &prop);
140         if (!err)
141                 priv->can.clock.freq = prop / 2;
142         else
143                 priv->can.clock.freq = SJA1000_OFP_CAN_CLOCK; /* default */
144
145         err = of_property_read_u32(np, "nxp,tx-output-mode", &prop);
146         if (!err)
147                 priv->ocr |= prop & OCR_MODE_MASK;
148         else
149                 priv->ocr |= OCR_MODE_NORMAL; /* default */
150
151         err = of_property_read_u32(np, "nxp,tx-output-config", &prop);
152         if (!err)
153                 priv->ocr |= (prop << OCR_TX_SHIFT) & OCR_TX_MASK;
154         else
155                 priv->ocr |= OCR_TX0_PULLDOWN; /* default */
156
157         err = of_property_read_u32(np, "nxp,clock-out-frequency", &prop);
158         if (!err && prop) {
159                 u32 divider = priv->can.clock.freq * 2 / prop;
160
161                 if (divider > 1)
162                         priv->cdr |= divider / 2 - 1;
163                 else
164                         priv->cdr |= CDR_CLKOUT_MASK;
165         } else {
166                 priv->cdr |= CDR_CLK_OFF; /* default */
167         }
168
169         if (!of_property_read_bool(np, "nxp,no-comparator-bypass"))
170                 priv->cdr |= CDR_CBP; /* default */
171
172         priv->irq_flags = IRQF_SHARED;
173         priv->reg_base = base;
174
175         dev->irq = irq;
176
177         dev_info(&ofdev->dev,
178                  "reg_base=0x%p irq=%d clock=%d ocr=0x%02x cdr=0x%02x\n",
179                  priv->reg_base, dev->irq, priv->can.clock.freq,
180                  priv->ocr, priv->cdr);
181
182         dev_set_drvdata(&ofdev->dev, dev);
183         SET_NETDEV_DEV(dev, &ofdev->dev);
184
185         err = register_sja1000dev(dev);
186         if (err) {
187                 dev_err(&ofdev->dev, "registering %s failed (err=%d)\n",
188                         DRV_NAME, err);
189                 goto exit_free_sja1000;
190         }
191
192         return 0;
193
194 exit_free_sja1000:
195         free_sja1000dev(dev);
196 exit_dispose_irq:
197         irq_dispose_mapping(irq);
198 exit_unmap_mem:
199         iounmap(base);
200 exit_release_mem:
201         release_mem_region(res.start, res_size);
202
203         return err;
204 }
205
206 static struct of_device_id __devinitdata sja1000_ofp_table[] = {
207         {.compatible = "nxp,sja1000"},
208         {},
209 };
210 MODULE_DEVICE_TABLE(of, sja1000_ofp_table);
211
212 static struct platform_driver sja1000_ofp_driver = {
213         .driver = {
214                 .owner = THIS_MODULE,
215                 .name = DRV_NAME,
216                 .of_match_table = sja1000_ofp_table,
217         },
218         .probe = sja1000_ofp_probe,
219         .remove = __devexit_p(sja1000_ofp_remove),
220 };
221
222 static int __init sja1000_ofp_init(void)
223 {
224         return platform_driver_register(&sja1000_ofp_driver);
225 }
226 module_init(sja1000_ofp_init);
227
228 static void __exit sja1000_ofp_exit(void)
229 {
230         return platform_driver_unregister(&sja1000_ofp_driver);
231 };
232 module_exit(sja1000_ofp_exit);