2 * Copyright (C) 2005 Sascha Hauer, Pengutronix
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
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.
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/>.
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>
30 #include <linux/of_irq.h>
34 #define DRV_NAME "sja1000_platform"
35 #define SP_CAN_CLOCK (16000000 / 2)
39 MODULE_DESCRIPTION("Socket-CAN driver for SJA1000 on the platform bus");
40 MODULE_ALIAS("platform:" DRV_NAME);
41 MODULE_LICENSE("GPL v2");
43 static u8 sp_read_reg8(const struct sja1000_priv *priv, int reg)
45 return ioread8(priv->reg_base + reg);
48 static void sp_write_reg8(const struct sja1000_priv *priv, int reg, u8 val)
50 iowrite8(val, priv->reg_base + reg);
53 static u8 sp_read_reg16(const struct sja1000_priv *priv, int reg)
55 return ioread8(priv->reg_base + reg * 2);
58 static void sp_write_reg16(const struct sja1000_priv *priv, int reg, u8 val)
60 iowrite8(val, priv->reg_base + reg * 2);
63 static u8 sp_read_reg32(const struct sja1000_priv *priv, int reg)
65 return ioread8(priv->reg_base + reg * 4);
68 static void sp_write_reg32(const struct sja1000_priv *priv, int reg, u8 val)
70 iowrite8(val, priv->reg_base + reg * 4);
73 static void sp_populate(struct sja1000_priv *priv,
74 struct sja1000_platform_data *pdata,
75 unsigned long resource_mem_flags)
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;
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;
87 case IORESOURCE_MEM_16BIT:
88 priv->read_reg = sp_read_reg16;
89 priv->write_reg = sp_write_reg16;
91 case IORESOURCE_MEM_8BIT:
93 priv->read_reg = sp_read_reg8;
94 priv->write_reg = sp_write_reg8;
99 static void sp_populate_of(struct sja1000_priv *priv, struct device_node *of)
104 err = of_property_read_u32(of, "reg-io-width", &prop);
106 prop = 1; /* 8 bit is default */
110 priv->read_reg = sp_read_reg32;
111 priv->write_reg = sp_write_reg32;
114 priv->read_reg = sp_read_reg16;
115 priv->write_reg = sp_write_reg16;
117 case 1: /* fallthrough */
119 priv->read_reg = sp_read_reg8;
120 priv->write_reg = sp_write_reg8;
123 err = of_property_read_u32(of, "nxp,external-clock-frequency", &prop);
125 priv->can.clock.freq = prop / 2;
127 priv->can.clock.freq = SP_CAN_CLOCK; /* default */
129 err = of_property_read_u32(of, "nxp,tx-output-mode", &prop);
131 priv->ocr |= prop & OCR_MODE_MASK;
133 priv->ocr |= OCR_MODE_NORMAL; /* default */
135 err = of_property_read_u32(of, "nxp,tx-output-config", &prop);
137 priv->ocr |= (prop << OCR_TX_SHIFT) & OCR_TX_MASK;
139 priv->ocr |= OCR_TX0_PULLDOWN; /* default */
141 err = of_property_read_u32(of, "nxp,clock-out-frequency", &prop);
143 u32 divider = priv->can.clock.freq * 2 / prop;
146 priv->cdr |= divider / 2 - 1;
148 priv->cdr |= CDR_CLKOUT_MASK;
150 priv->cdr |= CDR_CLK_OFF; /* default */
153 if (!of_property_read_bool(of, "nxp,no-comparator-bypass"))
154 priv->cdr |= CDR_CBP; /* default */
157 static int sp_probe(struct platform_device *pdev)
161 struct net_device *dev;
162 struct sja1000_priv *priv;
163 struct resource *res_mem, *res_irq = NULL;
164 struct sja1000_platform_data *pdata;
165 struct device_node *of = pdev->dev.of_node;
167 pdata = dev_get_platdata(&pdev->dev);
169 dev_err(&pdev->dev, "No platform data provided!\n");
173 res_mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
177 if (!devm_request_mem_region(&pdev->dev, res_mem->start,
178 resource_size(res_mem), DRV_NAME))
181 addr = devm_ioremap_nocache(&pdev->dev, res_mem->start,
182 resource_size(res_mem));
187 irq = irq_of_parse_and_map(of, 0);
189 res_irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
191 if (!irq && !res_irq)
194 dev = alloc_sja1000dev(0);
197 priv = netdev_priv(dev);
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;
205 priv->irq_flags = IRQF_SHARED;
209 priv->reg_base = addr;
212 sp_populate_of(priv, of);
214 sp_populate(priv, pdata, res_mem->flags);
216 platform_set_drvdata(pdev, dev);
217 SET_NETDEV_DEV(dev, &pdev->dev);
219 err = register_sja1000dev(dev);
221 dev_err(&pdev->dev, "registering %s failed (err=%d)\n",
226 dev_info(&pdev->dev, "%s device registered (reg_base=%p, irq=%d)\n",
227 DRV_NAME, priv->reg_base, dev->irq);
231 free_sja1000dev(dev);
235 static int sp_remove(struct platform_device *pdev)
237 struct net_device *dev = platform_get_drvdata(pdev);
239 unregister_sja1000dev(dev);
240 free_sja1000dev(dev);
245 static struct of_device_id sp_of_table[] = {
246 {.compatible = "nxp,sja1000"},
249 MODULE_DEVICE_TABLE(of, sp_of_table);
251 static struct platform_driver sp_driver = {
256 .owner = THIS_MODULE,
257 .of_match_table = sp_of_table,
261 module_platform_driver(sp_driver);