Linux Audio

Check our new training course

Loading...
v6.8
  1// SPDX-License-Identifier: GPL-2.0-or-later
  2/*
  3 * Xilinx XPS PS/2 device driver
  4 *
  5 * (c) 2005 MontaVista Software, Inc.
  6 * (c) 2008 Xilinx, Inc.
 
 
 
 
 
 
 
 
 
  7 */
  8
  9
 10#include <linux/module.h>
 11#include <linux/serio.h>
 12#include <linux/interrupt.h>
 13#include <linux/errno.h>
 14#include <linux/slab.h>
 15#include <linux/list.h>
 16#include <linux/io.h>
 17#include <linux/mod_devicetable.h>
 18#include <linux/of_address.h>
 
 19#include <linux/of_irq.h>
 20#include <linux/platform_device.h>
 21
 22#define DRIVER_NAME		"xilinx_ps2"
 23
 24/* Register offsets for the xps2 device */
 25#define XPS2_SRST_OFFSET	0x00000000 /* Software Reset register */
 26#define XPS2_STATUS_OFFSET	0x00000004 /* Status register */
 27#define XPS2_RX_DATA_OFFSET	0x00000008 /* Receive Data register */
 28#define XPS2_TX_DATA_OFFSET	0x0000000C /* Transmit Data register */
 29#define XPS2_GIER_OFFSET	0x0000002C /* Global Interrupt Enable reg */
 30#define XPS2_IPISR_OFFSET	0x00000030 /* Interrupt Status register */
 31#define XPS2_IPIER_OFFSET	0x00000038 /* Interrupt Enable register */
 32
 33/* Reset Register Bit Definitions */
 34#define XPS2_SRST_RESET		0x0000000A /* Software Reset  */
 35
 36/* Status Register Bit Positions */
 37#define XPS2_STATUS_RX_FULL	0x00000001 /* Receive Full  */
 38#define XPS2_STATUS_TX_FULL	0x00000002 /* Transmit Full  */
 39
 40/*
 41 * Bit definitions for ISR/IER registers. Both the registers have the same bit
 42 * definitions and are only defined once.
 43 */
 44#define XPS2_IPIXR_WDT_TOUT	0x00000001 /* Watchdog Timeout Interrupt */
 45#define XPS2_IPIXR_TX_NOACK	0x00000002 /* Transmit No ACK Interrupt */
 46#define XPS2_IPIXR_TX_ACK	0x00000004 /* Transmit ACK (Data) Interrupt */
 47#define XPS2_IPIXR_RX_OVF	0x00000008 /* Receive Overflow Interrupt */
 48#define XPS2_IPIXR_RX_ERR	0x00000010 /* Receive Error Interrupt */
 49#define XPS2_IPIXR_RX_FULL	0x00000020 /* Receive Data Interrupt */
 50
 51/* Mask for all the Transmit Interrupts */
 52#define XPS2_IPIXR_TX_ALL	(XPS2_IPIXR_TX_NOACK | XPS2_IPIXR_TX_ACK)
 53
 54/* Mask for all the Receive Interrupts */
 55#define XPS2_IPIXR_RX_ALL	(XPS2_IPIXR_RX_OVF | XPS2_IPIXR_RX_ERR |  \
 56				 XPS2_IPIXR_RX_FULL)
 57
 58/* Mask for all the Interrupts */
 59#define XPS2_IPIXR_ALL		(XPS2_IPIXR_TX_ALL | XPS2_IPIXR_RX_ALL |  \
 60				 XPS2_IPIXR_WDT_TOUT)
 61
 62/* Global Interrupt Enable mask */
 63#define XPS2_GIER_GIE_MASK	0x80000000
 64
 65struct xps2data {
 66	int irq;
 67	spinlock_t lock;
 68	void __iomem *base_address;	/* virt. address of control registers */
 69	unsigned int flags;
 70	struct serio *serio;		/* serio */
 71	struct device *dev;
 72};
 73
 74/************************************/
 75/* XPS PS/2 data transmission calls */
 76/************************************/
 77
 78/**
 79 * xps2_recv() - attempts to receive a byte from the PS/2 port.
 80 * @drvdata:	pointer to ps2 device private data structure
 81 * @byte:	address where the read data will be copied
 82 *
 83 * If there is any data available in the PS/2 receiver, this functions reads
 84 * the data, otherwise it returns error.
 85 */
 86static int xps2_recv(struct xps2data *drvdata, u8 *byte)
 87{
 88	u32 sr;
 89	int status = -1;
 90
 91	/* If there is data available in the PS/2 receiver, read it */
 92	sr = in_be32(drvdata->base_address + XPS2_STATUS_OFFSET);
 93	if (sr & XPS2_STATUS_RX_FULL) {
 94		*byte = in_be32(drvdata->base_address + XPS2_RX_DATA_OFFSET);
 95		status = 0;
 96	}
 97
 98	return status;
 99}
100
101/*********************/
102/* Interrupt handler */
103/*********************/
104static irqreturn_t xps2_interrupt(int irq, void *dev_id)
105{
106	struct xps2data *drvdata = dev_id;
107	u32 intr_sr;
108	u8 c;
109	int status;
110
111	/* Get the PS/2 interrupts and clear them */
112	intr_sr = in_be32(drvdata->base_address + XPS2_IPISR_OFFSET);
113	out_be32(drvdata->base_address + XPS2_IPISR_OFFSET, intr_sr);
114
115	/* Check which interrupt is active */
116	if (intr_sr & XPS2_IPIXR_RX_OVF)
117		dev_warn(drvdata->dev, "receive overrun error\n");
118
119	if (intr_sr & XPS2_IPIXR_RX_ERR)
120		drvdata->flags |= SERIO_PARITY;
121
122	if (intr_sr & (XPS2_IPIXR_TX_NOACK | XPS2_IPIXR_WDT_TOUT))
123		drvdata->flags |= SERIO_TIMEOUT;
124
125	if (intr_sr & XPS2_IPIXR_RX_FULL) {
126		status = xps2_recv(drvdata, &c);
127
128		/* Error, if a byte is not received */
129		if (status) {
130			dev_err(drvdata->dev,
131				"wrong rcvd byte count (%d)\n", status);
132		} else {
133			serio_interrupt(drvdata->serio, c, drvdata->flags);
134			drvdata->flags = 0;
135		}
136	}
137
138	return IRQ_HANDLED;
139}
140
141/*******************/
142/* serio callbacks */
143/*******************/
144
145/**
146 * sxps2_write() - sends a byte out through the PS/2 port.
147 * @pserio:	pointer to the serio structure of the PS/2 port
148 * @c:		data that needs to be written to the PS/2 port
149 *
150 * This function checks if the PS/2 transmitter is empty and sends a byte.
151 * Otherwise it returns error. Transmission fails only when nothing is connected
152 * to the PS/2 port. Thats why, we do not try to resend the data in case of a
153 * failure.
154 */
155static int sxps2_write(struct serio *pserio, unsigned char c)
156{
157	struct xps2data *drvdata = pserio->port_data;
158	unsigned long flags;
159	u32 sr;
160	int status = -1;
161
162	spin_lock_irqsave(&drvdata->lock, flags);
163
164	/* If the PS/2 transmitter is empty send a byte of data */
165	sr = in_be32(drvdata->base_address + XPS2_STATUS_OFFSET);
166	if (!(sr & XPS2_STATUS_TX_FULL)) {
167		out_be32(drvdata->base_address + XPS2_TX_DATA_OFFSET, c);
168		status = 0;
169	}
170
171	spin_unlock_irqrestore(&drvdata->lock, flags);
172
173	return status;
174}
175
176/**
177 * sxps2_open() - called when a port is opened by the higher layer.
178 * @pserio:	pointer to the serio structure of the PS/2 device
179 *
180 * This function requests irq and enables interrupts for the PS/2 device.
181 */
182static int sxps2_open(struct serio *pserio)
183{
184	struct xps2data *drvdata = pserio->port_data;
185	int error;
186	u8 c;
187
188	error = request_irq(drvdata->irq, &xps2_interrupt, 0,
189				DRIVER_NAME, drvdata);
190	if (error) {
191		dev_err(drvdata->dev,
192			"Couldn't allocate interrupt %d\n", drvdata->irq);
193		return error;
194	}
195
196	/* start reception by enabling the interrupts */
197	out_be32(drvdata->base_address + XPS2_GIER_OFFSET, XPS2_GIER_GIE_MASK);
198	out_be32(drvdata->base_address + XPS2_IPIER_OFFSET, XPS2_IPIXR_RX_ALL);
199	(void)xps2_recv(drvdata, &c);
200
201	return 0;		/* success */
202}
203
204/**
205 * sxps2_close() - frees the interrupt.
206 * @pserio:	pointer to the serio structure of the PS/2 device
207 *
208 * This function frees the irq and disables interrupts for the PS/2 device.
209 */
210static void sxps2_close(struct serio *pserio)
211{
212	struct xps2data *drvdata = pserio->port_data;
213
214	/* Disable the PS2 interrupts */
215	out_be32(drvdata->base_address + XPS2_GIER_OFFSET, 0x00);
216	out_be32(drvdata->base_address + XPS2_IPIER_OFFSET, 0x00);
217	free_irq(drvdata->irq, drvdata);
218}
219
220/**
221 * xps2_of_probe - probe method for the PS/2 device.
222 * @of_dev:	pointer to OF device structure
223 * @match:	pointer to the structure used for matching a device
224 *
225 * This function probes the PS/2 device in the device tree.
226 * It initializes the driver data structure and the hardware.
227 * It returns 0, if the driver is bound to the PS/2 device, or a negative
228 * value if there is an error.
229 */
230static int xps2_of_probe(struct platform_device *ofdev)
231{
232	struct resource r_mem; /* IO mem resources */
233	struct xps2data *drvdata;
234	struct serio *serio;
235	struct device *dev = &ofdev->dev;
236	resource_size_t remap_size, phys_addr;
237	unsigned int irq;
238	int error;
239
240	dev_info(dev, "Device Tree Probing \'%pOFn\'\n", dev->of_node);
 
241
242	/* Get iospace for the device */
243	error = of_address_to_resource(dev->of_node, 0, &r_mem);
244	if (error) {
245		dev_err(dev, "invalid address\n");
246		return error;
247	}
248
249	/* Get IRQ for the device */
250	irq = irq_of_parse_and_map(dev->of_node, 0);
251	if (!irq) {
252		dev_err(dev, "no IRQ found\n");
253		return -ENODEV;
254	}
255
256	drvdata = kzalloc(sizeof(struct xps2data), GFP_KERNEL);
257	serio = kzalloc(sizeof(struct serio), GFP_KERNEL);
258	if (!drvdata || !serio) {
259		error = -ENOMEM;
260		goto failed1;
261	}
262
263	spin_lock_init(&drvdata->lock);
264	drvdata->irq = irq;
265	drvdata->serio = serio;
266	drvdata->dev = dev;
267
268	phys_addr = r_mem.start;
269	remap_size = resource_size(&r_mem);
270	if (!request_mem_region(phys_addr, remap_size, DRIVER_NAME)) {
271		dev_err(dev, "Couldn't lock memory region at 0x%08llX\n",
272			(unsigned long long)phys_addr);
273		error = -EBUSY;
274		goto failed1;
275	}
276
277	/* Fill in configuration data and add them to the list */
278	drvdata->base_address = ioremap(phys_addr, remap_size);
279	if (drvdata->base_address == NULL) {
280		dev_err(dev, "Couldn't ioremap memory at 0x%08llX\n",
281			(unsigned long long)phys_addr);
282		error = -EFAULT;
283		goto failed2;
284	}
285
286	/* Disable all the interrupts, just in case */
287	out_be32(drvdata->base_address + XPS2_IPIER_OFFSET, 0);
288
289	/*
290	 * Reset the PS2 device and abort any current transaction,
291	 * to make sure we have the PS2 in a good state.
292	 */
293	out_be32(drvdata->base_address + XPS2_SRST_OFFSET, XPS2_SRST_RESET);
294
295	dev_info(dev, "Xilinx PS2 at 0x%08llX mapped to 0x%p, irq=%d\n",
296		 (unsigned long long)phys_addr, drvdata->base_address,
297		 drvdata->irq);
298
299	serio->id.type = SERIO_8042;
300	serio->write = sxps2_write;
301	serio->open = sxps2_open;
302	serio->close = sxps2_close;
303	serio->port_data = drvdata;
304	serio->dev.parent = dev;
305	snprintf(serio->name, sizeof(serio->name),
306		 "Xilinx XPS PS/2 at %08llX", (unsigned long long)phys_addr);
307	snprintf(serio->phys, sizeof(serio->phys),
308		 "xilinxps2/serio at %08llX", (unsigned long long)phys_addr);
309
310	serio_register_port(serio);
311
312	platform_set_drvdata(ofdev, drvdata);
313	return 0;		/* success */
314
315failed2:
316	release_mem_region(phys_addr, remap_size);
317failed1:
318	kfree(serio);
319	kfree(drvdata);
320
321	return error;
322}
323
324/**
325 * xps2_of_remove - unbinds the driver from the PS/2 device.
326 * @of_dev:	pointer to OF device structure
327 *
328 * This function is called if a device is physically removed from the system or
329 * if the driver module is being unloaded. It frees any resources allocated to
330 * the device.
331 */
332static void xps2_of_remove(struct platform_device *of_dev)
333{
334	struct xps2data *drvdata = platform_get_drvdata(of_dev);
335	struct resource r_mem; /* IO mem resources */
336
337	serio_unregister_port(drvdata->serio);
338	iounmap(drvdata->base_address);
339
340	/* Get iospace of the device */
341	if (of_address_to_resource(of_dev->dev.of_node, 0, &r_mem))
342		dev_err(drvdata->dev, "invalid address\n");
343	else
344		release_mem_region(r_mem.start, resource_size(&r_mem));
345
346	kfree(drvdata);
 
 
347}
348
349/* Match table for of_platform binding */
350static const struct of_device_id xps2_of_match[] = {
351	{ .compatible = "xlnx,xps-ps2-1.00.a", },
352	{ /* end of list */ },
353};
354MODULE_DEVICE_TABLE(of, xps2_of_match);
355
356static struct platform_driver xps2_of_driver = {
357	.driver = {
358		.name = DRIVER_NAME,
 
359		.of_match_table = xps2_of_match,
360	},
361	.probe		= xps2_of_probe,
362	.remove_new	= xps2_of_remove,
363};
364module_platform_driver(xps2_of_driver);
365
366MODULE_AUTHOR("Xilinx, Inc.");
367MODULE_DESCRIPTION("Xilinx XPS PS/2 driver");
368MODULE_LICENSE("GPL");
369
v3.15
 
  1/*
  2 * Xilinx XPS PS/2 device driver
  3 *
  4 * (c) 2005 MontaVista Software, Inc.
  5 * (c) 2008 Xilinx, Inc.
  6 *
  7 * This program is free software; you can redistribute it and/or modify it
  8 * under the terms of the GNU General Public License as published by the
  9 * Free Software Foundation; either version 2 of the License, or (at your
 10 * option) any later version.
 11 *
 12 * You should have received a copy of the GNU General Public License along
 13 * with this program; if not, write to the Free Software Foundation, Inc.,
 14 * 675 Mass Ave, Cambridge, MA 02139, USA.
 15 */
 16
 17
 18#include <linux/module.h>
 19#include <linux/serio.h>
 20#include <linux/interrupt.h>
 21#include <linux/errno.h>
 22#include <linux/slab.h>
 23#include <linux/list.h>
 24#include <linux/io.h>
 
 25#include <linux/of_address.h>
 26#include <linux/of_device.h>
 27#include <linux/of_irq.h>
 28#include <linux/of_platform.h>
 29
 30#define DRIVER_NAME		"xilinx_ps2"
 31
 32/* Register offsets for the xps2 device */
 33#define XPS2_SRST_OFFSET	0x00000000 /* Software Reset register */
 34#define XPS2_STATUS_OFFSET	0x00000004 /* Status register */
 35#define XPS2_RX_DATA_OFFSET	0x00000008 /* Receive Data register */
 36#define XPS2_TX_DATA_OFFSET	0x0000000C /* Transmit Data register */
 37#define XPS2_GIER_OFFSET	0x0000002C /* Global Interrupt Enable reg */
 38#define XPS2_IPISR_OFFSET	0x00000030 /* Interrupt Status register */
 39#define XPS2_IPIER_OFFSET	0x00000038 /* Interrupt Enable register */
 40
 41/* Reset Register Bit Definitions */
 42#define XPS2_SRST_RESET		0x0000000A /* Software Reset  */
 43
 44/* Status Register Bit Positions */
 45#define XPS2_STATUS_RX_FULL	0x00000001 /* Receive Full  */
 46#define XPS2_STATUS_TX_FULL	0x00000002 /* Transmit Full  */
 47
 48/* Bit definitions for ISR/IER registers. Both the registers have the same bit
 49 * definitions and are only defined once. */
 
 
 50#define XPS2_IPIXR_WDT_TOUT	0x00000001 /* Watchdog Timeout Interrupt */
 51#define XPS2_IPIXR_TX_NOACK	0x00000002 /* Transmit No ACK Interrupt */
 52#define XPS2_IPIXR_TX_ACK	0x00000004 /* Transmit ACK (Data) Interrupt */
 53#define XPS2_IPIXR_RX_OVF	0x00000008 /* Receive Overflow Interrupt */
 54#define XPS2_IPIXR_RX_ERR	0x00000010 /* Receive Error Interrupt */
 55#define XPS2_IPIXR_RX_FULL	0x00000020 /* Receive Data Interrupt */
 56
 57/* Mask for all the Transmit Interrupts */
 58#define XPS2_IPIXR_TX_ALL	(XPS2_IPIXR_TX_NOACK | XPS2_IPIXR_TX_ACK)
 59
 60/* Mask for all the Receive Interrupts */
 61#define XPS2_IPIXR_RX_ALL	(XPS2_IPIXR_RX_OVF | XPS2_IPIXR_RX_ERR |  \
 62				 XPS2_IPIXR_RX_FULL)
 63
 64/* Mask for all the Interrupts */
 65#define XPS2_IPIXR_ALL		(XPS2_IPIXR_TX_ALL | XPS2_IPIXR_RX_ALL |  \
 66				 XPS2_IPIXR_WDT_TOUT)
 67
 68/* Global Interrupt Enable mask */
 69#define XPS2_GIER_GIE_MASK	0x80000000
 70
 71struct xps2data {
 72	int irq;
 73	spinlock_t lock;
 74	void __iomem *base_address;	/* virt. address of control registers */
 75	unsigned int flags;
 76	struct serio *serio;		/* serio */
 77	struct device *dev;
 78};
 79
 80/************************************/
 81/* XPS PS/2 data transmission calls */
 82/************************************/
 83
 84/**
 85 * xps2_recv() - attempts to receive a byte from the PS/2 port.
 86 * @drvdata:	pointer to ps2 device private data structure
 87 * @byte:	address where the read data will be copied
 88 *
 89 * If there is any data available in the PS/2 receiver, this functions reads
 90 * the data, otherwise it returns error.
 91 */
 92static int xps2_recv(struct xps2data *drvdata, u8 *byte)
 93{
 94	u32 sr;
 95	int status = -1;
 96
 97	/* If there is data available in the PS/2 receiver, read it */
 98	sr = in_be32(drvdata->base_address + XPS2_STATUS_OFFSET);
 99	if (sr & XPS2_STATUS_RX_FULL) {
100		*byte = in_be32(drvdata->base_address + XPS2_RX_DATA_OFFSET);
101		status = 0;
102	}
103
104	return status;
105}
106
107/*********************/
108/* Interrupt handler */
109/*********************/
110static irqreturn_t xps2_interrupt(int irq, void *dev_id)
111{
112	struct xps2data *drvdata = dev_id;
113	u32 intr_sr;
114	u8 c;
115	int status;
116
117	/* Get the PS/2 interrupts and clear them */
118	intr_sr = in_be32(drvdata->base_address + XPS2_IPISR_OFFSET);
119	out_be32(drvdata->base_address + XPS2_IPISR_OFFSET, intr_sr);
120
121	/* Check which interrupt is active */
122	if (intr_sr & XPS2_IPIXR_RX_OVF)
123		dev_warn(drvdata->dev, "receive overrun error\n");
124
125	if (intr_sr & XPS2_IPIXR_RX_ERR)
126		drvdata->flags |= SERIO_PARITY;
127
128	if (intr_sr & (XPS2_IPIXR_TX_NOACK | XPS2_IPIXR_WDT_TOUT))
129		drvdata->flags |= SERIO_TIMEOUT;
130
131	if (intr_sr & XPS2_IPIXR_RX_FULL) {
132		status = xps2_recv(drvdata, &c);
133
134		/* Error, if a byte is not received */
135		if (status) {
136			dev_err(drvdata->dev,
137				"wrong rcvd byte count (%d)\n", status);
138		} else {
139			serio_interrupt(drvdata->serio, c, drvdata->flags);
140			drvdata->flags = 0;
141		}
142	}
143
144	return IRQ_HANDLED;
145}
146
147/*******************/
148/* serio callbacks */
149/*******************/
150
151/**
152 * sxps2_write() - sends a byte out through the PS/2 port.
153 * @pserio:	pointer to the serio structure of the PS/2 port
154 * @c:		data that needs to be written to the PS/2 port
155 *
156 * This function checks if the PS/2 transmitter is empty and sends a byte.
157 * Otherwise it returns error. Transmission fails only when nothing is connected
158 * to the PS/2 port. Thats why, we do not try to resend the data in case of a
159 * failure.
160 */
161static int sxps2_write(struct serio *pserio, unsigned char c)
162{
163	struct xps2data *drvdata = pserio->port_data;
164	unsigned long flags;
165	u32 sr;
166	int status = -1;
167
168	spin_lock_irqsave(&drvdata->lock, flags);
169
170	/* If the PS/2 transmitter is empty send a byte of data */
171	sr = in_be32(drvdata->base_address + XPS2_STATUS_OFFSET);
172	if (!(sr & XPS2_STATUS_TX_FULL)) {
173		out_be32(drvdata->base_address + XPS2_TX_DATA_OFFSET, c);
174		status = 0;
175	}
176
177	spin_unlock_irqrestore(&drvdata->lock, flags);
178
179	return status;
180}
181
182/**
183 * sxps2_open() - called when a port is opened by the higher layer.
184 * @pserio:	pointer to the serio structure of the PS/2 device
185 *
186 * This function requests irq and enables interrupts for the PS/2 device.
187 */
188static int sxps2_open(struct serio *pserio)
189{
190	struct xps2data *drvdata = pserio->port_data;
191	int error;
192	u8 c;
193
194	error = request_irq(drvdata->irq, &xps2_interrupt, 0,
195				DRIVER_NAME, drvdata);
196	if (error) {
197		dev_err(drvdata->dev,
198			"Couldn't allocate interrupt %d\n", drvdata->irq);
199		return error;
200	}
201
202	/* start reception by enabling the interrupts */
203	out_be32(drvdata->base_address + XPS2_GIER_OFFSET, XPS2_GIER_GIE_MASK);
204	out_be32(drvdata->base_address + XPS2_IPIER_OFFSET, XPS2_IPIXR_RX_ALL);
205	(void)xps2_recv(drvdata, &c);
206
207	return 0;		/* success */
208}
209
210/**
211 * sxps2_close() - frees the interrupt.
212 * @pserio:	pointer to the serio structure of the PS/2 device
213 *
214 * This function frees the irq and disables interrupts for the PS/2 device.
215 */
216static void sxps2_close(struct serio *pserio)
217{
218	struct xps2data *drvdata = pserio->port_data;
219
220	/* Disable the PS2 interrupts */
221	out_be32(drvdata->base_address + XPS2_GIER_OFFSET, 0x00);
222	out_be32(drvdata->base_address + XPS2_IPIER_OFFSET, 0x00);
223	free_irq(drvdata->irq, drvdata);
224}
225
226/**
227 * xps2_of_probe - probe method for the PS/2 device.
228 * @of_dev:	pointer to OF device structure
229 * @match:	pointer to the structure used for matching a device
230 *
231 * This function probes the PS/2 device in the device tree.
232 * It initializes the driver data structure and the hardware.
233 * It returns 0, if the driver is bound to the PS/2 device, or a negative
234 * value if there is an error.
235 */
236static int xps2_of_probe(struct platform_device *ofdev)
237{
238	struct resource r_mem; /* IO mem resources */
239	struct xps2data *drvdata;
240	struct serio *serio;
241	struct device *dev = &ofdev->dev;
242	resource_size_t remap_size, phys_addr;
243	unsigned int irq;
244	int error;
245
246	dev_info(dev, "Device Tree Probing \'%s\'\n",
247			ofdev->dev.of_node->name);
248
249	/* Get iospace for the device */
250	error = of_address_to_resource(ofdev->dev.of_node, 0, &r_mem);
251	if (error) {
252		dev_err(dev, "invalid address\n");
253		return error;
254	}
255
256	/* Get IRQ for the device */
257	irq = irq_of_parse_and_map(ofdev->dev.of_node, 0);
258	if (!irq) {
259		dev_err(dev, "no IRQ found\n");
260		return -ENODEV;
261	}
262
263	drvdata = kzalloc(sizeof(struct xps2data), GFP_KERNEL);
264	serio = kzalloc(sizeof(struct serio), GFP_KERNEL);
265	if (!drvdata || !serio) {
266		error = -ENOMEM;
267		goto failed1;
268	}
269
270	spin_lock_init(&drvdata->lock);
271	drvdata->irq = irq;
272	drvdata->serio = serio;
273	drvdata->dev = dev;
274
275	phys_addr = r_mem.start;
276	remap_size = resource_size(&r_mem);
277	if (!request_mem_region(phys_addr, remap_size, DRIVER_NAME)) {
278		dev_err(dev, "Couldn't lock memory region at 0x%08llX\n",
279			(unsigned long long)phys_addr);
280		error = -EBUSY;
281		goto failed1;
282	}
283
284	/* Fill in configuration data and add them to the list */
285	drvdata->base_address = ioremap(phys_addr, remap_size);
286	if (drvdata->base_address == NULL) {
287		dev_err(dev, "Couldn't ioremap memory at 0x%08llX\n",
288			(unsigned long long)phys_addr);
289		error = -EFAULT;
290		goto failed2;
291	}
292
293	/* Disable all the interrupts, just in case */
294	out_be32(drvdata->base_address + XPS2_IPIER_OFFSET, 0);
295
296	/* Reset the PS2 device and abort any current transaction, to make sure
297	 * we have the PS2 in a good state */
 
 
298	out_be32(drvdata->base_address + XPS2_SRST_OFFSET, XPS2_SRST_RESET);
299
300	dev_info(dev, "Xilinx PS2 at 0x%08llX mapped to 0x%p, irq=%d\n",
301		 (unsigned long long)phys_addr, drvdata->base_address,
302		 drvdata->irq);
303
304	serio->id.type = SERIO_8042;
305	serio->write = sxps2_write;
306	serio->open = sxps2_open;
307	serio->close = sxps2_close;
308	serio->port_data = drvdata;
309	serio->dev.parent = dev;
310	snprintf(serio->name, sizeof(serio->name),
311		 "Xilinx XPS PS/2 at %08llX", (unsigned long long)phys_addr);
312	snprintf(serio->phys, sizeof(serio->phys),
313		 "xilinxps2/serio at %08llX", (unsigned long long)phys_addr);
314
315	serio_register_port(serio);
316
317	platform_set_drvdata(ofdev, drvdata);
318	return 0;		/* success */
319
320failed2:
321	release_mem_region(phys_addr, remap_size);
322failed1:
323	kfree(serio);
324	kfree(drvdata);
325
326	return error;
327}
328
329/**
330 * xps2_of_remove - unbinds the driver from the PS/2 device.
331 * @of_dev:	pointer to OF device structure
332 *
333 * This function is called if a device is physically removed from the system or
334 * if the driver module is being unloaded. It frees any resources allocated to
335 * the device.
336 */
337static int xps2_of_remove(struct platform_device *of_dev)
338{
339	struct xps2data *drvdata = platform_get_drvdata(of_dev);
340	struct resource r_mem; /* IO mem resources */
341
342	serio_unregister_port(drvdata->serio);
343	iounmap(drvdata->base_address);
344
345	/* Get iospace of the device */
346	if (of_address_to_resource(of_dev->dev.of_node, 0, &r_mem))
347		dev_err(drvdata->dev, "invalid address\n");
348	else
349		release_mem_region(r_mem.start, resource_size(&r_mem));
350
351	kfree(drvdata);
352
353	return 0;
354}
355
356/* Match table for of_platform binding */
357static const struct of_device_id xps2_of_match[] = {
358	{ .compatible = "xlnx,xps-ps2-1.00.a", },
359	{ /* end of list */ },
360};
361MODULE_DEVICE_TABLE(of, xps2_of_match);
362
363static struct platform_driver xps2_of_driver = {
364	.driver = {
365		.name = DRIVER_NAME,
366		.owner = THIS_MODULE,
367		.of_match_table = xps2_of_match,
368	},
369	.probe		= xps2_of_probe,
370	.remove		= xps2_of_remove,
371};
372module_platform_driver(xps2_of_driver);
373
374MODULE_AUTHOR("Xilinx, Inc.");
375MODULE_DESCRIPTION("Xilinx XPS PS/2 driver");
376MODULE_LICENSE("GPL");
377