Merge branch 'perf-urgent-for-linus' of git://git.kernel.org/pub/scm/linux/kernel...
[firefly-linux-kernel-4.4.55.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 <linux/of_address.h>
46 #include <linux/of_irq.h>
47
48 #include "sja1000.h"
49
50 #define DRV_NAME "sja1000_of_platform"
51
52 MODULE_AUTHOR("Wolfgang Grandegger <wg@grandegger.com>");
53 MODULE_DESCRIPTION("Socket-CAN driver for SJA1000 on the OF platform bus");
54 MODULE_LICENSE("GPL v2");
55
56 #define SJA1000_OFP_CAN_CLOCK  (16000000 / 2)
57
58 #define SJA1000_OFP_OCR        OCR_TX0_PULLDOWN
59 #define SJA1000_OFP_CDR        (CDR_CBP | CDR_CLK_OFF)
60
61 static u8 sja1000_ofp_read_reg(const struct sja1000_priv *priv, int reg)
62 {
63         return ioread8(priv->reg_base + reg);
64 }
65
66 static void sja1000_ofp_write_reg(const struct sja1000_priv *priv,
67                                   int reg, u8 val)
68 {
69         iowrite8(val, priv->reg_base + reg);
70 }
71
72 static int sja1000_ofp_remove(struct platform_device *ofdev)
73 {
74         struct net_device *dev = platform_get_drvdata(ofdev);
75         struct sja1000_priv *priv = netdev_priv(dev);
76         struct device_node *np = ofdev->dev.of_node;
77         struct resource res;
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
90 static int sja1000_ofp_probe(struct platform_device *ofdev)
91 {
92         struct device_node *np = ofdev->dev.of_node;
93         struct net_device *dev;
94         struct sja1000_priv *priv;
95         struct resource res;
96         u32 prop;
97         int err, irq, res_size;
98         void __iomem *base;
99
100         err = of_address_to_resource(np, 0, &res);
101         if (err) {
102                 dev_err(&ofdev->dev, "invalid address\n");
103                 return err;
104         }
105
106         res_size = resource_size(&res);
107
108         if (!request_mem_region(res.start, res_size, DRV_NAME)) {
109                 dev_err(&ofdev->dev, "couldn't request %pR\n", &res);
110                 return -EBUSY;
111         }
112
113         base = ioremap_nocache(res.start, res_size);
114         if (!base) {
115                 dev_err(&ofdev->dev, "couldn't ioremap %pR\n", &res);
116                 err = -ENOMEM;
117                 goto exit_release_mem;
118         }
119
120         irq = irq_of_parse_and_map(np, 0);
121         if (irq == 0) {
122                 dev_err(&ofdev->dev, "no irq found\n");
123                 err = -ENODEV;
124                 goto exit_unmap_mem;
125         }
126
127         dev = alloc_sja1000dev(0);
128         if (!dev) {
129                 err = -ENOMEM;
130                 goto exit_dispose_irq;
131         }
132
133         priv = netdev_priv(dev);
134
135         priv->read_reg = sja1000_ofp_read_reg;
136         priv->write_reg = sja1000_ofp_write_reg;
137
138         err = of_property_read_u32(np, "nxp,external-clock-frequency", &prop);
139         if (!err)
140                 priv->can.clock.freq = prop / 2;
141         else
142                 priv->can.clock.freq = SJA1000_OFP_CAN_CLOCK; /* default */
143
144         err = of_property_read_u32(np, "nxp,tx-output-mode", &prop);
145         if (!err)
146                 priv->ocr |= prop & OCR_MODE_MASK;
147         else
148                 priv->ocr |= OCR_MODE_NORMAL; /* default */
149
150         err = of_property_read_u32(np, "nxp,tx-output-config", &prop);
151         if (!err)
152                 priv->ocr |= (prop << OCR_TX_SHIFT) & OCR_TX_MASK;
153         else
154                 priv->ocr |= OCR_TX0_PULLDOWN; /* default */
155
156         err = of_property_read_u32(np, "nxp,clock-out-frequency", &prop);
157         if (!err && prop) {
158                 u32 divider = priv->can.clock.freq * 2 / prop;
159
160                 if (divider > 1)
161                         priv->cdr |= divider / 2 - 1;
162                 else
163                         priv->cdr |= CDR_CLKOUT_MASK;
164         } else {
165                 priv->cdr |= CDR_CLK_OFF; /* default */
166         }
167
168         if (!of_property_read_bool(np, "nxp,no-comparator-bypass"))
169                 priv->cdr |= CDR_CBP; /* default */
170
171         priv->irq_flags = IRQF_SHARED;
172         priv->reg_base = base;
173
174         dev->irq = irq;
175
176         dev_info(&ofdev->dev,
177                  "reg_base=0x%p irq=%d clock=%d ocr=0x%02x cdr=0x%02x\n",
178                  priv->reg_base, dev->irq, priv->can.clock.freq,
179                  priv->ocr, priv->cdr);
180
181         platform_set_drvdata(ofdev, dev);
182         SET_NETDEV_DEV(dev, &ofdev->dev);
183
184         err = register_sja1000dev(dev);
185         if (err) {
186                 dev_err(&ofdev->dev, "registering %s failed (err=%d)\n",
187                         DRV_NAME, err);
188                 goto exit_free_sja1000;
189         }
190
191         return 0;
192
193 exit_free_sja1000:
194         free_sja1000dev(dev);
195 exit_dispose_irq:
196         irq_dispose_mapping(irq);
197 exit_unmap_mem:
198         iounmap(base);
199 exit_release_mem:
200         release_mem_region(res.start, res_size);
201
202         return err;
203 }
204
205 static struct of_device_id sja1000_ofp_table[] = {
206         {.compatible = "nxp,sja1000"},
207         {},
208 };
209 MODULE_DEVICE_TABLE(of, sja1000_ofp_table);
210
211 static struct platform_driver sja1000_ofp_driver = {
212         .driver = {
213                 .owner = THIS_MODULE,
214                 .name = DRV_NAME,
215                 .of_match_table = sja1000_ofp_table,
216         },
217         .probe = sja1000_ofp_probe,
218         .remove = sja1000_ofp_remove,
219 };
220
221 module_platform_driver(sja1000_ofp_driver);