1aaa7cb26SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2edd2c26fSWolfgang Grandegger /*
3edd2c26fSWolfgang Grandegger * Driver for CC770 and AN82527 CAN controllers on the legacy ISA bus
4edd2c26fSWolfgang Grandegger *
5edd2c26fSWolfgang Grandegger * Copyright (C) 2009, 2011 Wolfgang Grandegger <wg@grandegger.com>
6edd2c26fSWolfgang Grandegger */
7edd2c26fSWolfgang Grandegger
8edd2c26fSWolfgang Grandegger /*
9edd2c26fSWolfgang Grandegger * Bosch CC770 and Intel AN82527 CAN controllers on the ISA or PC-104 bus.
10edd2c26fSWolfgang Grandegger * The I/O port or memory address and the IRQ number must be specified via
11edd2c26fSWolfgang Grandegger * module parameters:
12edd2c26fSWolfgang Grandegger *
13edd2c26fSWolfgang Grandegger * insmod cc770_isa.ko port=0x310,0x380 irq=7,11
14edd2c26fSWolfgang Grandegger *
15edd2c26fSWolfgang Grandegger * for ISA devices using I/O ports or:
16edd2c26fSWolfgang Grandegger *
17edd2c26fSWolfgang Grandegger * insmod cc770_isa.ko mem=0xd1000,0xd1000 irq=7,11
18edd2c26fSWolfgang Grandegger *
19edd2c26fSWolfgang Grandegger * for memory mapped ISA devices.
20edd2c26fSWolfgang Grandegger *
21edd2c26fSWolfgang Grandegger * Indirect access via address and data port is supported as well:
22edd2c26fSWolfgang Grandegger *
23edd2c26fSWolfgang Grandegger * insmod cc770_isa.ko port=0x310,0x380 indirect=1 irq=7,11
24edd2c26fSWolfgang Grandegger *
25edd2c26fSWolfgang Grandegger * Furthermore, the following mode parameter can be defined:
26edd2c26fSWolfgang Grandegger *
27edd2c26fSWolfgang Grandegger * clk: External oscillator clock frequency (default=16000000 [16 MHz])
28edd2c26fSWolfgang Grandegger * cir: CPU interface register (default=0x40 [DSC])
29edd2c26fSWolfgang Grandegger * bcr: Bus configuration register (default=0x40 [CBY])
30edd2c26fSWolfgang Grandegger * cor: Clockout register (default=0x00)
31edd2c26fSWolfgang Grandegger *
32edd2c26fSWolfgang Grandegger * Note: for clk, cir, bcr and cor, the first argument re-defines the
33edd2c26fSWolfgang Grandegger * default for all other devices, e.g.:
34edd2c26fSWolfgang Grandegger *
35edd2c26fSWolfgang Grandegger * insmod cc770_isa.ko mem=0xd1000,0xd1000 irq=7,11 clk=24000000
36edd2c26fSWolfgang Grandegger *
37edd2c26fSWolfgang Grandegger * is equivalent to
38edd2c26fSWolfgang Grandegger *
39edd2c26fSWolfgang Grandegger * insmod cc770_isa.ko mem=0xd1000,0xd1000 irq=7,11 clk=24000000,24000000
40edd2c26fSWolfgang Grandegger */
41edd2c26fSWolfgang Grandegger
42edd2c26fSWolfgang Grandegger #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
43edd2c26fSWolfgang Grandegger
44edd2c26fSWolfgang Grandegger #include <linux/kernel.h>
45edd2c26fSWolfgang Grandegger #include <linux/module.h>
46edd2c26fSWolfgang Grandegger #include <linux/platform_device.h>
47edd2c26fSWolfgang Grandegger #include <linux/interrupt.h>
48edd2c26fSWolfgang Grandegger #include <linux/netdevice.h>
49edd2c26fSWolfgang Grandegger #include <linux/delay.h>
50edd2c26fSWolfgang Grandegger #include <linux/irq.h>
51edd2c26fSWolfgang Grandegger #include <linux/io.h>
52edd2c26fSWolfgang Grandegger #include <linux/can.h>
53edd2c26fSWolfgang Grandegger #include <linux/can/dev.h>
54edd2c26fSWolfgang Grandegger #include <linux/can/platform/cc770.h>
55edd2c26fSWolfgang Grandegger
56edd2c26fSWolfgang Grandegger #include "cc770.h"
57edd2c26fSWolfgang Grandegger
58edd2c26fSWolfgang Grandegger #define MAXDEV 8
59edd2c26fSWolfgang Grandegger
60edd2c26fSWolfgang Grandegger MODULE_AUTHOR("Wolfgang Grandegger <wg@grandegger.com>");
61edd2c26fSWolfgang Grandegger MODULE_DESCRIPTION("Socket-CAN driver for CC770 on the ISA bus");
62edd2c26fSWolfgang Grandegger MODULE_LICENSE("GPL v2");
63edd2c26fSWolfgang Grandegger
64edd2c26fSWolfgang Grandegger #define CLK_DEFAULT 16000000 /* 16 MHz */
65edd2c26fSWolfgang Grandegger #define COR_DEFAULT 0x00
66edd2c26fSWolfgang Grandegger #define BCR_DEFAULT BUSCFG_CBY
67edd2c26fSWolfgang Grandegger
68edd2c26fSWolfgang Grandegger static unsigned long port[MAXDEV];
69edd2c26fSWolfgang Grandegger static unsigned long mem[MAXDEV];
703c8ac0f2SBill Pemberton static int irq[MAXDEV];
713c8ac0f2SBill Pemberton static int clk[MAXDEV];
723c8ac0f2SBill Pemberton static u8 cir[MAXDEV] = {[0 ... (MAXDEV - 1)] = 0xff};
733c8ac0f2SBill Pemberton static u8 cor[MAXDEV] = {[0 ... (MAXDEV - 1)] = 0xff};
743c8ac0f2SBill Pemberton static u8 bcr[MAXDEV] = {[0 ... (MAXDEV - 1)] = 0xff};
753c8ac0f2SBill Pemberton static int indirect[MAXDEV] = {[0 ... (MAXDEV - 1)] = -1};
76edd2c26fSWolfgang Grandegger
77d61e4038SJoe Perches module_param_hw_array(port, ulong, ioport, NULL, 0444);
78edd2c26fSWolfgang Grandegger MODULE_PARM_DESC(port, "I/O port number");
79edd2c26fSWolfgang Grandegger
80d61e4038SJoe Perches module_param_hw_array(mem, ulong, iomem, NULL, 0444);
81edd2c26fSWolfgang Grandegger MODULE_PARM_DESC(mem, "I/O memory address");
82edd2c26fSWolfgang Grandegger
83d61e4038SJoe Perches module_param_hw_array(indirect, int, ioport, NULL, 0444);
84edd2c26fSWolfgang Grandegger MODULE_PARM_DESC(indirect, "Indirect access via address and data port");
85edd2c26fSWolfgang Grandegger
86d61e4038SJoe Perches module_param_hw_array(irq, int, irq, NULL, 0444);
87edd2c26fSWolfgang Grandegger MODULE_PARM_DESC(irq, "IRQ number");
88edd2c26fSWolfgang Grandegger
89d61e4038SJoe Perches module_param_array(clk, int, NULL, 0444);
90edd2c26fSWolfgang Grandegger MODULE_PARM_DESC(clk, "External oscillator clock frequency "
91edd2c26fSWolfgang Grandegger "(default=16000000 [16 MHz])");
92edd2c26fSWolfgang Grandegger
93d61e4038SJoe Perches module_param_array(cir, byte, NULL, 0444);
94edd2c26fSWolfgang Grandegger MODULE_PARM_DESC(cir, "CPU interface register (default=0x40 [DSC])");
95edd2c26fSWolfgang Grandegger
96d61e4038SJoe Perches module_param_array(cor, byte, NULL, 0444);
97edd2c26fSWolfgang Grandegger MODULE_PARM_DESC(cor, "Clockout register (default=0x00)");
98edd2c26fSWolfgang Grandegger
99d61e4038SJoe Perches module_param_array(bcr, byte, NULL, 0444);
100edd2c26fSWolfgang Grandegger MODULE_PARM_DESC(bcr, "Bus configuration register (default=0x40 [CBY])");
101edd2c26fSWolfgang Grandegger
102edd2c26fSWolfgang Grandegger #define CC770_IOSIZE 0x20
103edd2c26fSWolfgang Grandegger #define CC770_IOSIZE_INDIRECT 0x02
104edd2c26fSWolfgang Grandegger
1052d5091e0SWolfgang Zarre /* Spinlock for cc770_isa_port_write_reg_indirect
1062d5091e0SWolfgang Zarre * and cc770_isa_port_read_reg_indirect
1072d5091e0SWolfgang Zarre */
1082d5091e0SWolfgang Zarre static DEFINE_SPINLOCK(cc770_isa_port_lock);
1092d5091e0SWolfgang Zarre
110edd2c26fSWolfgang Grandegger static struct platform_device *cc770_isa_devs[MAXDEV];
111edd2c26fSWolfgang Grandegger
cc770_isa_mem_read_reg(const struct cc770_priv * priv,int reg)112edd2c26fSWolfgang Grandegger static u8 cc770_isa_mem_read_reg(const struct cc770_priv *priv, int reg)
113edd2c26fSWolfgang Grandegger {
114edd2c26fSWolfgang Grandegger return readb(priv->reg_base + reg);
115edd2c26fSWolfgang Grandegger }
116edd2c26fSWolfgang Grandegger
cc770_isa_mem_write_reg(const struct cc770_priv * priv,int reg,u8 val)117edd2c26fSWolfgang Grandegger static void cc770_isa_mem_write_reg(const struct cc770_priv *priv,
118edd2c26fSWolfgang Grandegger int reg, u8 val)
119edd2c26fSWolfgang Grandegger {
120edd2c26fSWolfgang Grandegger writeb(val, priv->reg_base + reg);
121edd2c26fSWolfgang Grandegger }
122edd2c26fSWolfgang Grandegger
cc770_isa_port_read_reg(const struct cc770_priv * priv,int reg)123edd2c26fSWolfgang Grandegger static u8 cc770_isa_port_read_reg(const struct cc770_priv *priv, int reg)
124edd2c26fSWolfgang Grandegger {
125edd2c26fSWolfgang Grandegger return inb((unsigned long)priv->reg_base + reg);
126edd2c26fSWolfgang Grandegger }
127edd2c26fSWolfgang Grandegger
cc770_isa_port_write_reg(const struct cc770_priv * priv,int reg,u8 val)128edd2c26fSWolfgang Grandegger static void cc770_isa_port_write_reg(const struct cc770_priv *priv,
129edd2c26fSWolfgang Grandegger int reg, u8 val)
130edd2c26fSWolfgang Grandegger {
131edd2c26fSWolfgang Grandegger outb(val, (unsigned long)priv->reg_base + reg);
132edd2c26fSWolfgang Grandegger }
133edd2c26fSWolfgang Grandegger
cc770_isa_port_read_reg_indirect(const struct cc770_priv * priv,int reg)134edd2c26fSWolfgang Grandegger static u8 cc770_isa_port_read_reg_indirect(const struct cc770_priv *priv,
135edd2c26fSWolfgang Grandegger int reg)
136edd2c26fSWolfgang Grandegger {
137edd2c26fSWolfgang Grandegger unsigned long base = (unsigned long)priv->reg_base;
1382d5091e0SWolfgang Zarre unsigned long flags;
1392d5091e0SWolfgang Zarre u8 val;
140edd2c26fSWolfgang Grandegger
1412d5091e0SWolfgang Zarre spin_lock_irqsave(&cc770_isa_port_lock, flags);
142edd2c26fSWolfgang Grandegger outb(reg, base);
1432d5091e0SWolfgang Zarre val = inb(base + 1);
1442d5091e0SWolfgang Zarre spin_unlock_irqrestore(&cc770_isa_port_lock, flags);
1452d5091e0SWolfgang Zarre
1462d5091e0SWolfgang Zarre return val;
147edd2c26fSWolfgang Grandegger }
148edd2c26fSWolfgang Grandegger
cc770_isa_port_write_reg_indirect(const struct cc770_priv * priv,int reg,u8 val)149edd2c26fSWolfgang Grandegger static void cc770_isa_port_write_reg_indirect(const struct cc770_priv *priv,
150edd2c26fSWolfgang Grandegger int reg, u8 val)
151edd2c26fSWolfgang Grandegger {
152edd2c26fSWolfgang Grandegger unsigned long base = (unsigned long)priv->reg_base;
1532d5091e0SWolfgang Zarre unsigned long flags;
154edd2c26fSWolfgang Grandegger
1552d5091e0SWolfgang Zarre spin_lock_irqsave(&cc770_isa_port_lock, flags);
156edd2c26fSWolfgang Grandegger outb(reg, base);
157edd2c26fSWolfgang Grandegger outb(val, base + 1);
1582d5091e0SWolfgang Zarre spin_unlock_irqrestore(&cc770_isa_port_lock, flags);
159edd2c26fSWolfgang Grandegger }
160edd2c26fSWolfgang Grandegger
cc770_isa_probe(struct platform_device * pdev)1613c8ac0f2SBill Pemberton static int cc770_isa_probe(struct platform_device *pdev)
162edd2c26fSWolfgang Grandegger {
163edd2c26fSWolfgang Grandegger struct net_device *dev;
164edd2c26fSWolfgang Grandegger struct cc770_priv *priv;
165edd2c26fSWolfgang Grandegger void __iomem *base = NULL;
166edd2c26fSWolfgang Grandegger int iosize = CC770_IOSIZE;
167edd2c26fSWolfgang Grandegger int idx = pdev->id;
168edd2c26fSWolfgang Grandegger int err;
169edd2c26fSWolfgang Grandegger u32 clktmp;
170edd2c26fSWolfgang Grandegger
171edd2c26fSWolfgang Grandegger dev_dbg(&pdev->dev, "probing idx=%d: port=%#lx, mem=%#lx, irq=%d\n",
172edd2c26fSWolfgang Grandegger idx, port[idx], mem[idx], irq[idx]);
173edd2c26fSWolfgang Grandegger if (mem[idx]) {
174edd2c26fSWolfgang Grandegger if (!request_mem_region(mem[idx], iosize, KBUILD_MODNAME)) {
175edd2c26fSWolfgang Grandegger err = -EBUSY;
176edd2c26fSWolfgang Grandegger goto exit;
177edd2c26fSWolfgang Grandegger }
1784bdc0d67SChristoph Hellwig base = ioremap(mem[idx], iosize);
179edd2c26fSWolfgang Grandegger if (!base) {
180edd2c26fSWolfgang Grandegger err = -ENOMEM;
181edd2c26fSWolfgang Grandegger goto exit_release;
182edd2c26fSWolfgang Grandegger }
183edd2c26fSWolfgang Grandegger } else {
184edd2c26fSWolfgang Grandegger if (indirect[idx] > 0 ||
185edd2c26fSWolfgang Grandegger (indirect[idx] == -1 && indirect[0] > 0))
186edd2c26fSWolfgang Grandegger iosize = CC770_IOSIZE_INDIRECT;
187edd2c26fSWolfgang Grandegger if (!request_region(port[idx], iosize, KBUILD_MODNAME)) {
188edd2c26fSWolfgang Grandegger err = -EBUSY;
189edd2c26fSWolfgang Grandegger goto exit;
190edd2c26fSWolfgang Grandegger }
191edd2c26fSWolfgang Grandegger }
192edd2c26fSWolfgang Grandegger
193edd2c26fSWolfgang Grandegger dev = alloc_cc770dev(0);
194edd2c26fSWolfgang Grandegger if (!dev) {
195edd2c26fSWolfgang Grandegger err = -ENOMEM;
196edd2c26fSWolfgang Grandegger goto exit_unmap;
197edd2c26fSWolfgang Grandegger }
198edd2c26fSWolfgang Grandegger priv = netdev_priv(dev);
199edd2c26fSWolfgang Grandegger
200edd2c26fSWolfgang Grandegger dev->irq = irq[idx];
201edd2c26fSWolfgang Grandegger priv->irq_flags = IRQF_SHARED;
202edd2c26fSWolfgang Grandegger if (mem[idx]) {
203edd2c26fSWolfgang Grandegger priv->reg_base = base;
204edd2c26fSWolfgang Grandegger dev->base_addr = mem[idx];
205edd2c26fSWolfgang Grandegger priv->read_reg = cc770_isa_mem_read_reg;
206edd2c26fSWolfgang Grandegger priv->write_reg = cc770_isa_mem_write_reg;
207edd2c26fSWolfgang Grandegger } else {
208edd2c26fSWolfgang Grandegger priv->reg_base = (void __iomem *)port[idx];
209edd2c26fSWolfgang Grandegger dev->base_addr = port[idx];
210edd2c26fSWolfgang Grandegger
211edd2c26fSWolfgang Grandegger if (iosize == CC770_IOSIZE_INDIRECT) {
212edd2c26fSWolfgang Grandegger priv->read_reg = cc770_isa_port_read_reg_indirect;
213edd2c26fSWolfgang Grandegger priv->write_reg = cc770_isa_port_write_reg_indirect;
214edd2c26fSWolfgang Grandegger } else {
215edd2c26fSWolfgang Grandegger priv->read_reg = cc770_isa_port_read_reg;
216edd2c26fSWolfgang Grandegger priv->write_reg = cc770_isa_port_write_reg;
217edd2c26fSWolfgang Grandegger }
218edd2c26fSWolfgang Grandegger }
219edd2c26fSWolfgang Grandegger
220edd2c26fSWolfgang Grandegger if (clk[idx])
221edd2c26fSWolfgang Grandegger clktmp = clk[idx];
222edd2c26fSWolfgang Grandegger else if (clk[0])
223edd2c26fSWolfgang Grandegger clktmp = clk[0];
224edd2c26fSWolfgang Grandegger else
225edd2c26fSWolfgang Grandegger clktmp = CLK_DEFAULT;
226edd2c26fSWolfgang Grandegger priv->can.clock.freq = clktmp;
227edd2c26fSWolfgang Grandegger
228edd2c26fSWolfgang Grandegger if (cir[idx] != 0xff) {
229edd2c26fSWolfgang Grandegger priv->cpu_interface = cir[idx];
230edd2c26fSWolfgang Grandegger } else if (cir[0] != 0xff) {
231edd2c26fSWolfgang Grandegger priv->cpu_interface = cir[0];
232edd2c26fSWolfgang Grandegger } else {
233edd2c26fSWolfgang Grandegger /* The system clock may not exceed 10 MHz */
234edd2c26fSWolfgang Grandegger if (clktmp > 10000000) {
235edd2c26fSWolfgang Grandegger priv->cpu_interface |= CPUIF_DSC;
236edd2c26fSWolfgang Grandegger clktmp /= 2;
237edd2c26fSWolfgang Grandegger }
238edd2c26fSWolfgang Grandegger /* The memory clock may not exceed 8 MHz */
239edd2c26fSWolfgang Grandegger if (clktmp > 8000000)
240edd2c26fSWolfgang Grandegger priv->cpu_interface |= CPUIF_DMC;
241edd2c26fSWolfgang Grandegger }
242edd2c26fSWolfgang Grandegger
243edd2c26fSWolfgang Grandegger if (priv->cpu_interface & CPUIF_DSC)
244edd2c26fSWolfgang Grandegger priv->can.clock.freq /= 2;
245edd2c26fSWolfgang Grandegger
246edd2c26fSWolfgang Grandegger if (bcr[idx] != 0xff)
247edd2c26fSWolfgang Grandegger priv->bus_config = bcr[idx];
248edd2c26fSWolfgang Grandegger else if (bcr[0] != 0xff)
249edd2c26fSWolfgang Grandegger priv->bus_config = bcr[0];
250edd2c26fSWolfgang Grandegger else
251edd2c26fSWolfgang Grandegger priv->bus_config = BCR_DEFAULT;
252edd2c26fSWolfgang Grandegger
253edd2c26fSWolfgang Grandegger if (cor[idx] != 0xff)
254edd2c26fSWolfgang Grandegger priv->clkout = cor[idx];
255edd2c26fSWolfgang Grandegger else if (cor[0] != 0xff)
256edd2c26fSWolfgang Grandegger priv->clkout = cor[0];
257edd2c26fSWolfgang Grandegger else
258edd2c26fSWolfgang Grandegger priv->clkout = COR_DEFAULT;
259edd2c26fSWolfgang Grandegger
26000e4bbc8SJingoo Han platform_set_drvdata(pdev, dev);
261edd2c26fSWolfgang Grandegger SET_NETDEV_DEV(dev, &pdev->dev);
262edd2c26fSWolfgang Grandegger
263edd2c26fSWolfgang Grandegger err = register_cc770dev(dev);
264edd2c26fSWolfgang Grandegger if (err) {
265edd2c26fSWolfgang Grandegger dev_err(&pdev->dev,
266edd2c26fSWolfgang Grandegger "couldn't register device (err=%d)\n", err);
26762ec89e7SZhang Changzhong goto exit_free;
268edd2c26fSWolfgang Grandegger }
269edd2c26fSWolfgang Grandegger
270edd2c26fSWolfgang Grandegger dev_info(&pdev->dev, "device registered (reg_base=0x%p, irq=%d)\n",
271edd2c26fSWolfgang Grandegger priv->reg_base, dev->irq);
272edd2c26fSWolfgang Grandegger return 0;
273edd2c26fSWolfgang Grandegger
27462ec89e7SZhang Changzhong exit_free:
27562ec89e7SZhang Changzhong free_cc770dev(dev);
276edd2c26fSWolfgang Grandegger exit_unmap:
277edd2c26fSWolfgang Grandegger if (mem[idx])
278edd2c26fSWolfgang Grandegger iounmap(base);
279edd2c26fSWolfgang Grandegger exit_release:
280edd2c26fSWolfgang Grandegger if (mem[idx])
281edd2c26fSWolfgang Grandegger release_mem_region(mem[idx], iosize);
282edd2c26fSWolfgang Grandegger else
283edd2c26fSWolfgang Grandegger release_region(port[idx], iosize);
284edd2c26fSWolfgang Grandegger exit:
285edd2c26fSWolfgang Grandegger return err;
286edd2c26fSWolfgang Grandegger }
287edd2c26fSWolfgang Grandegger
cc770_isa_remove(struct platform_device * pdev)288*93810076SUwe Kleine-König static void cc770_isa_remove(struct platform_device *pdev)
289edd2c26fSWolfgang Grandegger {
29000e4bbc8SJingoo Han struct net_device *dev = platform_get_drvdata(pdev);
291edd2c26fSWolfgang Grandegger struct cc770_priv *priv = netdev_priv(dev);
292edd2c26fSWolfgang Grandegger int idx = pdev->id;
293edd2c26fSWolfgang Grandegger
294edd2c26fSWolfgang Grandegger unregister_cc770dev(dev);
295edd2c26fSWolfgang Grandegger
296edd2c26fSWolfgang Grandegger if (mem[idx]) {
297edd2c26fSWolfgang Grandegger iounmap(priv->reg_base);
298edd2c26fSWolfgang Grandegger release_mem_region(mem[idx], CC770_IOSIZE);
299edd2c26fSWolfgang Grandegger } else {
300edd2c26fSWolfgang Grandegger if (priv->read_reg == cc770_isa_port_read_reg_indirect)
301edd2c26fSWolfgang Grandegger release_region(port[idx], CC770_IOSIZE_INDIRECT);
302edd2c26fSWolfgang Grandegger else
303edd2c26fSWolfgang Grandegger release_region(port[idx], CC770_IOSIZE);
304edd2c26fSWolfgang Grandegger }
305edd2c26fSWolfgang Grandegger free_cc770dev(dev);
306edd2c26fSWolfgang Grandegger }
307edd2c26fSWolfgang Grandegger
308edd2c26fSWolfgang Grandegger static struct platform_driver cc770_isa_driver = {
309edd2c26fSWolfgang Grandegger .probe = cc770_isa_probe,
310*93810076SUwe Kleine-König .remove_new = cc770_isa_remove,
311edd2c26fSWolfgang Grandegger .driver = {
312edd2c26fSWolfgang Grandegger .name = KBUILD_MODNAME,
313edd2c26fSWolfgang Grandegger },
314edd2c26fSWolfgang Grandegger };
315edd2c26fSWolfgang Grandegger
cc770_isa_init(void)316edd2c26fSWolfgang Grandegger static int __init cc770_isa_init(void)
317edd2c26fSWolfgang Grandegger {
318edd2c26fSWolfgang Grandegger int idx, err;
319edd2c26fSWolfgang Grandegger
320edd2c26fSWolfgang Grandegger for (idx = 0; idx < ARRAY_SIZE(cc770_isa_devs); idx++) {
321edd2c26fSWolfgang Grandegger if ((port[idx] || mem[idx]) && irq[idx]) {
322edd2c26fSWolfgang Grandegger cc770_isa_devs[idx] =
323edd2c26fSWolfgang Grandegger platform_device_alloc(KBUILD_MODNAME, idx);
324edd2c26fSWolfgang Grandegger if (!cc770_isa_devs[idx]) {
325edd2c26fSWolfgang Grandegger err = -ENOMEM;
326edd2c26fSWolfgang Grandegger goto exit_free_devices;
327edd2c26fSWolfgang Grandegger }
328edd2c26fSWolfgang Grandegger err = platform_device_add(cc770_isa_devs[idx]);
329edd2c26fSWolfgang Grandegger if (err) {
330edd2c26fSWolfgang Grandegger platform_device_put(cc770_isa_devs[idx]);
331edd2c26fSWolfgang Grandegger goto exit_free_devices;
332edd2c26fSWolfgang Grandegger }
333edd2c26fSWolfgang Grandegger pr_debug("platform device %d: port=%#lx, mem=%#lx, "
334edd2c26fSWolfgang Grandegger "irq=%d\n",
335edd2c26fSWolfgang Grandegger idx, port[idx], mem[idx], irq[idx]);
336edd2c26fSWolfgang Grandegger } else if (idx == 0 || port[idx] || mem[idx]) {
337edd2c26fSWolfgang Grandegger pr_err("insufficient parameters supplied\n");
338edd2c26fSWolfgang Grandegger err = -EINVAL;
339edd2c26fSWolfgang Grandegger goto exit_free_devices;
340edd2c26fSWolfgang Grandegger }
341edd2c26fSWolfgang Grandegger }
342edd2c26fSWolfgang Grandegger
343edd2c26fSWolfgang Grandegger err = platform_driver_register(&cc770_isa_driver);
344edd2c26fSWolfgang Grandegger if (err)
345edd2c26fSWolfgang Grandegger goto exit_free_devices;
346edd2c26fSWolfgang Grandegger
347edd2c26fSWolfgang Grandegger pr_info("driver for max. %d devices registered\n", MAXDEV);
348edd2c26fSWolfgang Grandegger
349edd2c26fSWolfgang Grandegger return 0;
350edd2c26fSWolfgang Grandegger
351edd2c26fSWolfgang Grandegger exit_free_devices:
352edd2c26fSWolfgang Grandegger while (--idx >= 0) {
353edd2c26fSWolfgang Grandegger if (cc770_isa_devs[idx])
354edd2c26fSWolfgang Grandegger platform_device_unregister(cc770_isa_devs[idx]);
355edd2c26fSWolfgang Grandegger }
356edd2c26fSWolfgang Grandegger
357edd2c26fSWolfgang Grandegger return err;
358edd2c26fSWolfgang Grandegger }
359edd2c26fSWolfgang Grandegger module_init(cc770_isa_init);
360edd2c26fSWolfgang Grandegger
cc770_isa_exit(void)361edd2c26fSWolfgang Grandegger static void __exit cc770_isa_exit(void)
362edd2c26fSWolfgang Grandegger {
363edd2c26fSWolfgang Grandegger int idx;
364edd2c26fSWolfgang Grandegger
365edd2c26fSWolfgang Grandegger platform_driver_unregister(&cc770_isa_driver);
366edd2c26fSWolfgang Grandegger for (idx = 0; idx < ARRAY_SIZE(cc770_isa_devs); idx++) {
367edd2c26fSWolfgang Grandegger if (cc770_isa_devs[idx])
368edd2c26fSWolfgang Grandegger platform_device_unregister(cc770_isa_devs[idx]);
369edd2c26fSWolfgang Grandegger }
370edd2c26fSWolfgang Grandegger }
371edd2c26fSWolfgang Grandegger module_exit(cc770_isa_exit);
372