Linux-libre 4.19.20-gnu
[librecmc/linux-libre.git] / drivers / net / can / sja1000 / sja1000_platform.c
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
15  * along with this program; if not, see <http://www.gnu.org/licenses/>.
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>
26 #include <linux/can/dev.h>
27 #include <linux/can/platform/sja1000.h>
28 #include <linux/io.h>
29 #include <linux/of.h>
30 #include <linux/of_device.h>
31 #include <linux/of_irq.h>
32
33 #include "sja1000.h"
34
35 #define DRV_NAME "sja1000_platform"
36 #define SP_CAN_CLOCK  (16000000 / 2)
37
38 MODULE_AUTHOR("Sascha Hauer <s.hauer@pengutronix.de>");
39 MODULE_AUTHOR("Wolfgang Grandegger <wg@grandegger.com>");
40 MODULE_DESCRIPTION("Socket-CAN driver for SJA1000 on the platform bus");
41 MODULE_ALIAS("platform:" DRV_NAME);
42 MODULE_LICENSE("GPL v2");
43
44 struct sja1000_of_data {
45         size_t  priv_sz;
46         int     (*init)(struct sja1000_priv *priv, struct device_node *of);
47 };
48
49 struct technologic_priv {
50         spinlock_t      io_lock;
51 };
52
53 static u8 sp_read_reg8(const struct sja1000_priv *priv, int reg)
54 {
55         return ioread8(priv->reg_base + reg);
56 }
57
58 static void sp_write_reg8(const struct sja1000_priv *priv, int reg, u8 val)
59 {
60         iowrite8(val, priv->reg_base + reg);
61 }
62
63 static u8 sp_read_reg16(const struct sja1000_priv *priv, int reg)
64 {
65         return ioread8(priv->reg_base + reg * 2);
66 }
67
68 static void sp_write_reg16(const struct sja1000_priv *priv, int reg, u8 val)
69 {
70         iowrite8(val, priv->reg_base + reg * 2);
71 }
72
73 static u8 sp_read_reg32(const struct sja1000_priv *priv, int reg)
74 {
75         return ioread8(priv->reg_base + reg * 4);
76 }
77
78 static void sp_write_reg32(const struct sja1000_priv *priv, int reg, u8 val)
79 {
80         iowrite8(val, priv->reg_base + reg * 4);
81 }
82
83 static u8 sp_technologic_read_reg16(const struct sja1000_priv *priv, int reg)
84 {
85         struct technologic_priv *tp = priv->priv;
86         unsigned long flags;
87         u8 val;
88
89         spin_lock_irqsave(&tp->io_lock, flags);
90         iowrite16(reg, priv->reg_base + 0);
91         val = ioread16(priv->reg_base + 2);
92         spin_unlock_irqrestore(&tp->io_lock, flags);
93
94         return val;
95 }
96
97 static void sp_technologic_write_reg16(const struct sja1000_priv *priv,
98                                        int reg, u8 val)
99 {
100         struct technologic_priv *tp = priv->priv;
101         unsigned long flags;
102
103         spin_lock_irqsave(&tp->io_lock, flags);
104         iowrite16(reg, priv->reg_base + 0);
105         iowrite16(val, priv->reg_base + 2);
106         spin_unlock_irqrestore(&tp->io_lock, flags);
107 }
108
109 static int sp_technologic_init(struct sja1000_priv *priv, struct device_node *of)
110 {
111         struct technologic_priv *tp = priv->priv;
112
113         priv->read_reg = sp_technologic_read_reg16;
114         priv->write_reg = sp_technologic_write_reg16;
115         spin_lock_init(&tp->io_lock);
116
117         return 0;
118 }
119
120 static void sp_populate(struct sja1000_priv *priv,
121                         struct sja1000_platform_data *pdata,
122                         unsigned long resource_mem_flags)
123 {
124         /* The CAN clock frequency is half the oscillator clock frequency */
125         priv->can.clock.freq = pdata->osc_freq / 2;
126         priv->ocr = pdata->ocr;
127         priv->cdr = pdata->cdr;
128
129         switch (resource_mem_flags & IORESOURCE_MEM_TYPE_MASK) {
130         case IORESOURCE_MEM_32BIT:
131                 priv->read_reg = sp_read_reg32;
132                 priv->write_reg = sp_write_reg32;
133                 break;
134         case IORESOURCE_MEM_16BIT:
135                 priv->read_reg = sp_read_reg16;
136                 priv->write_reg = sp_write_reg16;
137                 break;
138         case IORESOURCE_MEM_8BIT:
139         default:
140                 priv->read_reg = sp_read_reg8;
141                 priv->write_reg = sp_write_reg8;
142                 break;
143         }
144 }
145
146 static void sp_populate_of(struct sja1000_priv *priv, struct device_node *of)
147 {
148         int err;
149         u32 prop;
150
151         err = of_property_read_u32(of, "reg-io-width", &prop);
152         if (err)
153                 prop = 1; /* 8 bit is default */
154
155         switch (prop) {
156         case 4:
157                 priv->read_reg = sp_read_reg32;
158                 priv->write_reg = sp_write_reg32;
159                 break;
160         case 2:
161                 priv->read_reg = sp_read_reg16;
162                 priv->write_reg = sp_write_reg16;
163                 break;
164         case 1: /* fallthrough */
165         default:
166                 priv->read_reg = sp_read_reg8;
167                 priv->write_reg = sp_write_reg8;
168         }
169
170         err = of_property_read_u32(of, "nxp,external-clock-frequency", &prop);
171         if (!err)
172                 priv->can.clock.freq = prop / 2;
173         else
174                 priv->can.clock.freq = SP_CAN_CLOCK; /* default */
175
176         err = of_property_read_u32(of, "nxp,tx-output-mode", &prop);
177         if (!err)
178                 priv->ocr |= prop & OCR_MODE_MASK;
179         else
180                 priv->ocr |= OCR_MODE_NORMAL; /* default */
181
182         err = of_property_read_u32(of, "nxp,tx-output-config", &prop);
183         if (!err)
184                 priv->ocr |= (prop << OCR_TX_SHIFT) & OCR_TX_MASK;
185         else
186                 priv->ocr |= OCR_TX0_PULLDOWN; /* default */
187
188         err = of_property_read_u32(of, "nxp,clock-out-frequency", &prop);
189         if (!err && prop) {
190                 u32 divider = priv->can.clock.freq * 2 / prop;
191
192                 if (divider > 1)
193                         priv->cdr |= divider / 2 - 1;
194                 else
195                         priv->cdr |= CDR_CLKOUT_MASK;
196         } else {
197                 priv->cdr |= CDR_CLK_OFF; /* default */
198         }
199
200         if (!of_property_read_bool(of, "nxp,no-comparator-bypass"))
201                 priv->cdr |= CDR_CBP; /* default */
202 }
203
204 static struct sja1000_of_data technologic_data = {
205         .priv_sz = sizeof(struct technologic_priv),
206         .init = sp_technologic_init,
207 };
208
209 static const struct of_device_id sp_of_table[] = {
210         { .compatible = "nxp,sja1000", .data = NULL, },
211         { .compatible = "technologic,sja1000", .data = &technologic_data, },
212         { /* sentinel */ },
213 };
214 MODULE_DEVICE_TABLE(of, sp_of_table);
215
216 static int sp_probe(struct platform_device *pdev)
217 {
218         int err, irq = 0;
219         void __iomem *addr;
220         struct net_device *dev;
221         struct sja1000_priv *priv;
222         struct resource *res_mem, *res_irq = NULL;
223         struct sja1000_platform_data *pdata;
224         struct device_node *of = pdev->dev.of_node;
225         const struct of_device_id *of_id;
226         const struct sja1000_of_data *of_data = NULL;
227         size_t priv_sz = 0;
228
229         pdata = dev_get_platdata(&pdev->dev);
230         if (!pdata && !of) {
231                 dev_err(&pdev->dev, "No platform data provided!\n");
232                 return -ENODEV;
233         }
234
235         res_mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
236         if (!res_mem)
237                 return -ENODEV;
238
239         if (!devm_request_mem_region(&pdev->dev, res_mem->start,
240                                      resource_size(res_mem), DRV_NAME))
241                 return -EBUSY;
242
243         addr = devm_ioremap_nocache(&pdev->dev, res_mem->start,
244                                     resource_size(res_mem));
245         if (!addr)
246                 return -ENOMEM;
247
248         if (of)
249                 irq = irq_of_parse_and_map(of, 0);
250         else
251                 res_irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
252
253         if (!irq && !res_irq)
254                 return -ENODEV;
255
256         of_id = of_match_device(sp_of_table, &pdev->dev);
257         if (of_id && of_id->data) {
258                 of_data = of_id->data;
259                 priv_sz = of_data->priv_sz;
260         }
261
262         dev = alloc_sja1000dev(priv_sz);
263         if (!dev)
264                 return -ENOMEM;
265         priv = netdev_priv(dev);
266
267         if (res_irq) {
268                 irq = res_irq->start;
269                 priv->irq_flags = res_irq->flags & IRQF_TRIGGER_MASK;
270                 if (res_irq->flags & IORESOURCE_IRQ_SHAREABLE)
271                         priv->irq_flags |= IRQF_SHARED;
272         } else {
273                 priv->irq_flags = IRQF_SHARED;
274         }
275
276         dev->irq = irq;
277         priv->reg_base = addr;
278
279         if (of) {
280                 sp_populate_of(priv, of);
281
282                 if (of_data && of_data->init) {
283                         err = of_data->init(priv, of);
284                         if (err)
285                                 goto exit_free;
286                 }
287         } else {
288                 sp_populate(priv, pdata, res_mem->flags);
289         }
290
291         platform_set_drvdata(pdev, dev);
292         SET_NETDEV_DEV(dev, &pdev->dev);
293
294         err = register_sja1000dev(dev);
295         if (err) {
296                 dev_err(&pdev->dev, "registering %s failed (err=%d)\n",
297                         DRV_NAME, err);
298                 goto exit_free;
299         }
300
301         dev_info(&pdev->dev, "%s device registered (reg_base=%p, irq=%d)\n",
302                  DRV_NAME, priv->reg_base, dev->irq);
303         return 0;
304
305  exit_free:
306         free_sja1000dev(dev);
307         return err;
308 }
309
310 static int sp_remove(struct platform_device *pdev)
311 {
312         struct net_device *dev = platform_get_drvdata(pdev);
313
314         unregister_sja1000dev(dev);
315         free_sja1000dev(dev);
316
317         return 0;
318 }
319
320 static struct platform_driver sp_driver = {
321         .probe = sp_probe,
322         .remove = sp_remove,
323         .driver = {
324                 .name = DRV_NAME,
325                 .of_match_table = sp_of_table,
326         },
327 };
328
329 module_platform_driver(sp_driver);