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:
25 * compatible = "nxp,sja1000";
26 * reg = <3 0x100 0x80>;
28 * interrupt-parent = <&mpic>;
29 * nxp,external-clock-frequency = <16000000>;
32 * See "Documentation/devicetree/bindings/net/can/sja1000.txt" for further
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>
42 #include <linux/can/dev.h>
44 #include <linux/of_platform.h>
45 #include <linux/of_address.h>
46 #include <linux/of_irq.h>
51 #define DRV_NAME "sja1000_of_platform"
53 MODULE_AUTHOR("Wolfgang Grandegger <wg@grandegger.com>");
54 MODULE_DESCRIPTION("Socket-CAN driver for SJA1000 on the OF platform bus");
55 MODULE_LICENSE("GPL v2");
57 #define SJA1000_OFP_CAN_CLOCK (16000000 / 2)
59 #define SJA1000_OFP_OCR OCR_TX0_PULLDOWN
60 #define SJA1000_OFP_CDR (CDR_CBP | CDR_CLK_OFF)
62 static u8 sja1000_ofp_read_reg(const struct sja1000_priv *priv, int reg)
64 return ioread8(priv->reg_base + reg);
67 static void sja1000_ofp_write_reg(const struct sja1000_priv *priv,
70 iowrite8(val, priv->reg_base + reg);
73 static int sja1000_ofp_remove(struct platform_device *ofdev)
75 struct net_device *dev = platform_get_drvdata(ofdev);
76 struct sja1000_priv *priv = netdev_priv(dev);
77 struct device_node *np = ofdev->dev.of_node;
80 unregister_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));
91 static int sja1000_ofp_probe(struct platform_device *ofdev)
93 struct device_node *np = ofdev->dev.of_node;
94 struct net_device *dev;
95 struct sja1000_priv *priv;
98 int err, irq, res_size;
101 err = of_address_to_resource(np, 0, &res);
103 dev_err(&ofdev->dev, "invalid address\n");
107 res_size = resource_size(&res);
109 if (!request_mem_region(res.start, res_size, DRV_NAME)) {
110 dev_err(&ofdev->dev, "couldn't request %pR\n", &res);
114 base = ioremap_nocache(res.start, res_size);
116 dev_err(&ofdev->dev, "couldn't ioremap %pR\n", &res);
118 goto exit_release_mem;
121 irq = irq_of_parse_and_map(np, 0);
123 dev_err(&ofdev->dev, "no irq found\n");
128 dev = alloc_sja1000dev(0);
131 goto exit_dispose_irq;
134 priv = netdev_priv(dev);
136 priv->read_reg = sja1000_ofp_read_reg;
137 priv->write_reg = sja1000_ofp_write_reg;
139 err = of_property_read_u32(np, "nxp,external-clock-frequency", &prop);
141 priv->can.clock.freq = prop / 2;
143 priv->can.clock.freq = SJA1000_OFP_CAN_CLOCK; /* default */
145 err = of_property_read_u32(np, "nxp,tx-output-mode", &prop);
147 priv->ocr |= prop & OCR_MODE_MASK;
149 priv->ocr |= OCR_MODE_NORMAL; /* default */
151 err = of_property_read_u32(np, "nxp,tx-output-config", &prop);
153 priv->ocr |= (prop << OCR_TX_SHIFT) & OCR_TX_MASK;
155 priv->ocr |= OCR_TX0_PULLDOWN; /* default */
157 err = of_property_read_u32(np, "nxp,clock-out-frequency", &prop);
159 u32 divider = priv->can.clock.freq * 2 / prop;
162 priv->cdr |= divider / 2 - 1;
164 priv->cdr |= CDR_CLKOUT_MASK;
166 priv->cdr |= CDR_CLK_OFF; /* default */
169 if (!of_property_read_bool(np, "nxp,no-comparator-bypass"))
170 priv->cdr |= CDR_CBP; /* default */
172 priv->irq_flags = IRQF_SHARED;
173 priv->reg_base = base;
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);
182 platform_set_drvdata(ofdev, dev);
183 SET_NETDEV_DEV(dev, &ofdev->dev);
185 err = register_sja1000dev(dev);
187 dev_err(&ofdev->dev, "registering %s failed (err=%d)\n",
189 goto exit_free_sja1000;
195 free_sja1000dev(dev);
197 irq_dispose_mapping(irq);
201 release_mem_region(res.start, res_size);
206 static struct of_device_id sja1000_ofp_table[] = {
207 {.compatible = "nxp,sja1000"},
210 MODULE_DEVICE_TABLE(of, sja1000_ofp_table);
212 static struct platform_driver sja1000_ofp_driver = {
214 .owner = THIS_MODULE,
216 .of_match_table = sja1000_ofp_table,
218 .probe = sja1000_ofp_probe,
219 .remove = sja1000_ofp_remove,
222 module_platform_driver(sja1000_ofp_driver);