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, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17  */
18 
19 #include <linux/kernel.h>
20 #include <linux/module.h>
21 #include <linux/interrupt.h>
22 #include <linux/netdevice.h>
23 #include <linux/delay.h>
24 #include <linux/pci.h>
25 #include <linux/platform_device.h>
26 #include <linux/irq.h>
27 #include <linux/can/dev.h>
28 #include <linux/can/platform/sja1000.h>
29 #include <linux/io.h>
30 
31 #include "sja1000.h"
32 
33 #define DRV_NAME "sja1000_platform"
34 
35 MODULE_AUTHOR("Sascha Hauer <s.hauer@pengutronix.de>");
36 MODULE_DESCRIPTION("Socket-CAN driver for SJA1000 on the platform bus");
37 MODULE_ALIAS("platform:" DRV_NAME);
38 MODULE_LICENSE("GPL v2");
39 
40 static u8 sp_read_reg8(const struct sja1000_priv *priv, int reg)
41 {
42 	return ioread8(priv->reg_base + reg);
43 }
44 
45 static void sp_write_reg8(const struct sja1000_priv *priv, int reg, u8 val)
46 {
47 	iowrite8(val, priv->reg_base + reg);
48 }
49 
50 static u8 sp_read_reg16(const struct sja1000_priv *priv, int reg)
51 {
52 	return ioread8(priv->reg_base + reg * 2);
53 }
54 
55 static void sp_write_reg16(const struct sja1000_priv *priv, int reg, u8 val)
56 {
57 	iowrite8(val, priv->reg_base + reg * 2);
58 }
59 
60 static u8 sp_read_reg32(const struct sja1000_priv *priv, int reg)
61 {
62 	return ioread8(priv->reg_base + reg * 4);
63 }
64 
65 static void sp_write_reg32(const struct sja1000_priv *priv, int reg, u8 val)
66 {
67 	iowrite8(val, priv->reg_base + reg * 4);
68 }
69 
70 static int sp_probe(struct platform_device *pdev)
71 {
72 	int err;
73 	void __iomem *addr;
74 	struct net_device *dev;
75 	struct sja1000_priv *priv;
76 	struct resource *res_mem, *res_irq;
77 	struct sja1000_platform_data *pdata;
78 
79 	pdata = dev_get_platdata(&pdev->dev);
80 	if (!pdata) {
81 		dev_err(&pdev->dev, "No platform data provided!\n");
82 		err = -ENODEV;
83 		goto exit;
84 	}
85 
86 	res_mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
87 	res_irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
88 	if (!res_mem || !res_irq) {
89 		err = -ENODEV;
90 		goto exit;
91 	}
92 
93 	if (!request_mem_region(res_mem->start, resource_size(res_mem),
94 				DRV_NAME)) {
95 		err = -EBUSY;
96 		goto exit;
97 	}
98 
99 	addr = ioremap_nocache(res_mem->start, resource_size(res_mem));
100 	if (!addr) {
101 		err = -ENOMEM;
102 		goto exit_release;
103 	}
104 
105 	dev = alloc_sja1000dev(0);
106 	if (!dev) {
107 		err = -ENOMEM;
108 		goto exit_iounmap;
109 	}
110 	priv = netdev_priv(dev);
111 
112 	dev->irq = res_irq->start;
113 	priv->irq_flags = res_irq->flags & IRQF_TRIGGER_MASK;
114 	if (res_irq->flags & IORESOURCE_IRQ_SHAREABLE)
115 		priv->irq_flags |= IRQF_SHARED;
116 	priv->reg_base = addr;
117 	/* The CAN clock frequency is half the oscillator clock frequency */
118 	priv->can.clock.freq = pdata->osc_freq / 2;
119 	priv->ocr = pdata->ocr;
120 	priv->cdr = pdata->cdr;
121 
122 	switch (res_mem->flags & IORESOURCE_MEM_TYPE_MASK) {
123 	case IORESOURCE_MEM_32BIT:
124 		priv->read_reg = sp_read_reg32;
125 		priv->write_reg = sp_write_reg32;
126 		break;
127 	case IORESOURCE_MEM_16BIT:
128 		priv->read_reg = sp_read_reg16;
129 		priv->write_reg = sp_write_reg16;
130 		break;
131 	case IORESOURCE_MEM_8BIT:
132 	default:
133 		priv->read_reg = sp_read_reg8;
134 		priv->write_reg = sp_write_reg8;
135 		break;
136 	}
137 
138 	platform_set_drvdata(pdev, dev);
139 	SET_NETDEV_DEV(dev, &pdev->dev);
140 
141 	err = register_sja1000dev(dev);
142 	if (err) {
143 		dev_err(&pdev->dev, "registering %s failed (err=%d)\n",
144 			DRV_NAME, err);
145 		goto exit_free;
146 	}
147 
148 	dev_info(&pdev->dev, "%s device registered (reg_base=%p, irq=%d)\n",
149 		 DRV_NAME, priv->reg_base, dev->irq);
150 	return 0;
151 
152  exit_free:
153 	free_sja1000dev(dev);
154  exit_iounmap:
155 	iounmap(addr);
156  exit_release:
157 	release_mem_region(res_mem->start, resource_size(res_mem));
158  exit:
159 	return err;
160 }
161 
162 static int sp_remove(struct platform_device *pdev)
163 {
164 	struct net_device *dev = platform_get_drvdata(pdev);
165 	struct sja1000_priv *priv = netdev_priv(dev);
166 	struct resource *res;
167 
168 	unregister_sja1000dev(dev);
169 
170 	if (priv->reg_base)
171 		iounmap(priv->reg_base);
172 
173 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
174 	release_mem_region(res->start, resource_size(res));
175 
176 	free_sja1000dev(dev);
177 
178 	return 0;
179 }
180 
181 static struct platform_driver sp_driver = {
182 	.probe = sp_probe,
183 	.remove = sp_remove,
184 	.driver = {
185 		.name = DRV_NAME,
186 		.owner = THIS_MODULE,
187 	},
188 };
189 
190 module_platform_driver(sp_driver);
191