can: constify of_device_id array
[linux-2.6-block.git] / drivers / net / can / sja1000 / sja1000_platform.c
CommitLineData
f534e52f
WG
1/*
2 * Copyright (C) 2005 Sascha Hauer, Pengutronix
3 * Copyright (C) 2007 Wolfgang Grandegger <wg@grandegger.com>
4 *
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
8 *
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.
13 *
14 * You should have received a copy of the GNU General Public License
05780d98 15 * along with this program; if not, see <http://www.gnu.org/licenses/>.
f534e52f
WG
16 */
17
18#include <linux/kernel.h>
19#include <linux/module.h>
20#include <linux/interrupt.h>
21#include <linux/netdevice.h>
22#include <linux/delay.h>
23#include <linux/pci.h>
24#include <linux/platform_device.h>
25#include <linux/irq.h>
f534e52f
WG
26#include <linux/can/dev.h>
27#include <linux/can/platform/sja1000.h>
28#include <linux/io.h>
02729c3d
FV
29#include <linux/of.h>
30#include <linux/of_irq.h>
f534e52f
WG
31
32#include "sja1000.h"
33
34#define DRV_NAME "sja1000_platform"
02729c3d 35#define SP_CAN_CLOCK (16000000 / 2)
f534e52f
WG
36
37MODULE_AUTHOR("Sascha Hauer <s.hauer@pengutronix.de>");
02729c3d 38MODULE_AUTHOR("Wolfgang Grandegger <wg@grandegger.com>");
f534e52f 39MODULE_DESCRIPTION("Socket-CAN driver for SJA1000 on the platform bus");
d8c4386d 40MODULE_ALIAS("platform:" DRV_NAME);
f534e52f
WG
41MODULE_LICENSE("GPL v2");
42
986917b7 43static u8 sp_read_reg8(const struct sja1000_priv *priv, int reg)
f534e52f 44{
255a9154 45 return ioread8(priv->reg_base + reg);
f534e52f
WG
46}
47
986917b7 48static void sp_write_reg8(const struct sja1000_priv *priv, int reg, u8 val)
f534e52f 49{
255a9154 50 iowrite8(val, priv->reg_base + reg);
f534e52f
WG
51}
52
986917b7
YY
53static u8 sp_read_reg16(const struct sja1000_priv *priv, int reg)
54{
55 return ioread8(priv->reg_base + reg * 2);
56}
57
58static void sp_write_reg16(const struct sja1000_priv *priv, int reg, u8 val)
59{
60 iowrite8(val, priv->reg_base + reg * 2);
61}
62
63static u8 sp_read_reg32(const struct sja1000_priv *priv, int reg)
64{
65 return ioread8(priv->reg_base + reg * 4);
66}
67
68static void sp_write_reg32(const struct sja1000_priv *priv, int reg, u8 val)
69{
70 iowrite8(val, priv->reg_base + reg * 4);
71}
72
02729c3d
FV
73static void sp_populate(struct sja1000_priv *priv,
74 struct sja1000_platform_data *pdata,
75 unsigned long resource_mem_flags)
76{
77 /* The CAN clock frequency is half the oscillator clock frequency */
78 priv->can.clock.freq = pdata->osc_freq / 2;
79 priv->ocr = pdata->ocr;
80 priv->cdr = pdata->cdr;
81
82 switch (resource_mem_flags & IORESOURCE_MEM_TYPE_MASK) {
83 case IORESOURCE_MEM_32BIT:
84 priv->read_reg = sp_read_reg32;
85 priv->write_reg = sp_write_reg32;
86 break;
87 case IORESOURCE_MEM_16BIT:
88 priv->read_reg = sp_read_reg16;
89 priv->write_reg = sp_write_reg16;
90 break;
91 case IORESOURCE_MEM_8BIT:
92 default:
93 priv->read_reg = sp_read_reg8;
94 priv->write_reg = sp_write_reg8;
95 break;
96 }
97}
98
99static void sp_populate_of(struct sja1000_priv *priv, struct device_node *of)
f534e52f
WG
100{
101 int err;
02729c3d
FV
102 u32 prop;
103
b18ec27c
FV
104 err = of_property_read_u32(of, "reg-io-width", &prop);
105 if (err)
106 prop = 1; /* 8 bit is default */
107
108 switch (prop) {
109 case 4:
110 priv->read_reg = sp_read_reg32;
111 priv->write_reg = sp_write_reg32;
112 break;
113 case 2:
114 priv->read_reg = sp_read_reg16;
115 priv->write_reg = sp_write_reg16;
116 break;
117 case 1: /* fallthrough */
118 default:
119 priv->read_reg = sp_read_reg8;
120 priv->write_reg = sp_write_reg8;
121 }
02729c3d
FV
122
123 err = of_property_read_u32(of, "nxp,external-clock-frequency", &prop);
124 if (!err)
125 priv->can.clock.freq = prop / 2;
126 else
127 priv->can.clock.freq = SP_CAN_CLOCK; /* default */
128
129 err = of_property_read_u32(of, "nxp,tx-output-mode", &prop);
130 if (!err)
131 priv->ocr |= prop & OCR_MODE_MASK;
132 else
133 priv->ocr |= OCR_MODE_NORMAL; /* default */
134
135 err = of_property_read_u32(of, "nxp,tx-output-config", &prop);
136 if (!err)
137 priv->ocr |= (prop << OCR_TX_SHIFT) & OCR_TX_MASK;
138 else
139 priv->ocr |= OCR_TX0_PULLDOWN; /* default */
140
141 err = of_property_read_u32(of, "nxp,clock-out-frequency", &prop);
142 if (!err && prop) {
143 u32 divider = priv->can.clock.freq * 2 / prop;
144
145 if (divider > 1)
146 priv->cdr |= divider / 2 - 1;
147 else
148 priv->cdr |= CDR_CLKOUT_MASK;
149 } else {
150 priv->cdr |= CDR_CLK_OFF; /* default */
151 }
152
153 if (!of_property_read_bool(of, "nxp,no-comparator-bypass"))
154 priv->cdr |= CDR_CBP; /* default */
155}
156
157static int sp_probe(struct platform_device *pdev)
158{
159 int err, irq = 0;
f534e52f
WG
160 void __iomem *addr;
161 struct net_device *dev;
162 struct sja1000_priv *priv;
02729c3d 163 struct resource *res_mem, *res_irq = NULL;
f534e52f 164 struct sja1000_platform_data *pdata;
02729c3d 165 struct device_node *of = pdev->dev.of_node;
f534e52f 166
ecd78d97 167 pdata = dev_get_platdata(&pdev->dev);
02729c3d 168 if (!pdata && !of) {
f534e52f 169 dev_err(&pdev->dev, "No platform data provided!\n");
342180f7 170 return -ENODEV;
f534e52f
WG
171 }
172
173 res_mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
02729c3d 174 if (!res_mem)
342180f7 175 return -ENODEV;
f534e52f 176
342180f7
FV
177 if (!devm_request_mem_region(&pdev->dev, res_mem->start,
178 resource_size(res_mem), DRV_NAME))
179 return -EBUSY;
f534e52f 180
342180f7
FV
181 addr = devm_ioremap_nocache(&pdev->dev, res_mem->start,
182 resource_size(res_mem));
183 if (!addr)
184 return -ENOMEM;
f534e52f 185
02729c3d
FV
186 if (of)
187 irq = irq_of_parse_and_map(of, 0);
188 else
189 res_irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
190
191 if (!irq && !res_irq)
192 return -ENODEV;
193
f534e52f 194 dev = alloc_sja1000dev(0);
342180f7
FV
195 if (!dev)
196 return -ENOMEM;
f534e52f
WG
197 priv = netdev_priv(dev);
198
02729c3d
FV
199 if (res_irq) {
200 irq = res_irq->start;
201 priv->irq_flags = res_irq->flags & IRQF_TRIGGER_MASK;
202 if (res_irq->flags & IORESOURCE_IRQ_SHAREABLE)
203 priv->irq_flags |= IRQF_SHARED;
204 } else {
205 priv->irq_flags = IRQF_SHARED;
206 }
207
208 dev->irq = irq;
255a9154 209 priv->reg_base = addr;
f534e52f 210
02729c3d
FV
211 if (of)
212 sp_populate_of(priv, of);
213 else
214 sp_populate(priv, pdata, res_mem->flags);
986917b7 215
00e4bbc8 216 platform_set_drvdata(pdev, dev);
f534e52f
WG
217 SET_NETDEV_DEV(dev, &pdev->dev);
218
219 err = register_sja1000dev(dev);
220 if (err) {
221 dev_err(&pdev->dev, "registering %s failed (err=%d)\n",
222 DRV_NAME, err);
223 goto exit_free;
224 }
225
255a9154
WG
226 dev_info(&pdev->dev, "%s device registered (reg_base=%p, irq=%d)\n",
227 DRV_NAME, priv->reg_base, dev->irq);
f534e52f
WG
228 return 0;
229
230 exit_free:
231 free_sja1000dev(dev);
f534e52f
WG
232 return err;
233}
234
235static int sp_remove(struct platform_device *pdev)
236{
00e4bbc8 237 struct net_device *dev = platform_get_drvdata(pdev);
f534e52f
WG
238
239 unregister_sja1000dev(dev);
f534e52f
WG
240 free_sja1000dev(dev);
241
242 return 0;
243}
244
486e9570 245static const struct of_device_id sp_of_table[] = {
02729c3d
FV
246 {.compatible = "nxp,sja1000"},
247 {},
248};
249MODULE_DEVICE_TABLE(of, sp_of_table);
250
f534e52f
WG
251static struct platform_driver sp_driver = {
252 .probe = sp_probe,
253 .remove = sp_remove,
254 .driver = {
255 .name = DRV_NAME,
02729c3d 256 .of_match_table = sp_of_table,
f534e52f
WG
257 },
258};
259
871d3372 260module_platform_driver(sp_driver);