Linux Audio

Check our new training course

Loading...
v4.6
 
  1/*
  2 * OLPC serio driver for multiplexed input from Marvell MMP security processor
  3 *
  4 * Copyright (C) 2011-2013 One Laptop Per Child
  5 *
  6 * This program is free software; you can redistribute it and/or modify
  7 * it under the terms of the GNU General Public License as published by
  8 * the Free Software Foundation; either version 2 of the License, or
  9 * (at your option) any later version.
 10 *
 11 * This program is distributed in the hope that it will be useful,
 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 14 * GNU General Public License for more details.
 15 */
 16
 17#include <linux/module.h>
 18#include <linux/interrupt.h>
 19#include <linux/serio.h>
 20#include <linux/err.h>
 21#include <linux/platform_device.h>
 22#include <linux/io.h>
 23#include <linux/of.h>
 24#include <linux/slab.h>
 25#include <linux/delay.h>
 26
 27/*
 28 * The OLPC XO-1.75 and XO-4 laptops do not have a hardware PS/2 controller.
 29 * Instead, the OLPC firmware runs a bit-banging PS/2 implementation on an
 30 * otherwise-unused slow processor which is included in the Marvell MMP2/MMP3
 31 * SoC, known as the "Security Processor" (SP) or "Wireless Trusted Module"
 32 * (WTM). This firmware then reports its results via the WTM registers,
 33 * which we read from the Application Processor (AP, i.e. main CPU) in this
 34 * driver.
 35 *
 36 * On the hardware side we have a PS/2 mouse and an AT keyboard, the data
 37 * is multiplexed through this system. We create a serio port for each one,
 38 * and demultiplex the data accordingly.
 39 */
 40
 41/* WTM register offsets */
 42#define SECURE_PROCESSOR_COMMAND	0x40
 43#define COMMAND_RETURN_STATUS		0x80
 44#define COMMAND_FIFO_STATUS		0xc4
 45#define PJ_RST_INTERRUPT		0xc8
 46#define PJ_INTERRUPT_MASK		0xcc
 47
 48/*
 49 * The upper byte of SECURE_PROCESSOR_COMMAND and COMMAND_RETURN_STATUS is
 50 * used to identify which port (device) is being talked to. The lower byte
 51 * is the data being sent/received.
 52 */
 53#define PORT_MASK	0xff00
 54#define DATA_MASK	0x00ff
 55#define PORT_SHIFT	8
 56#define KEYBOARD_PORT	0
 57#define TOUCHPAD_PORT	1
 58
 59/* COMMAND_FIFO_STATUS */
 60#define CMD_CNTR_MASK		0x7 /* Number of pending/unprocessed commands */
 61#define MAX_PENDING_CMDS	4   /* from device specs */
 62
 63/* PJ_RST_INTERRUPT */
 64#define SP_COMMAND_COMPLETE_RESET	0x1
 65
 66/* PJ_INTERRUPT_MASK */
 67#define INT_0	(1 << 0)
 68
 69/* COMMAND_FIFO_STATUS */
 70#define CMD_STS_MASK	0x100
 71
 72struct olpc_apsp {
 73	struct device *dev;
 74	struct serio *kbio;
 75	struct serio *padio;
 76	void __iomem *base;
 77	int open_count;
 78	int irq;
 79};
 80
 81static int olpc_apsp_write(struct serio *port, unsigned char val)
 82{
 83	struct olpc_apsp *priv = port->port_data;
 84	unsigned int i;
 85	u32 which = 0;
 86
 87	if (port == priv->padio)
 88		which = TOUCHPAD_PORT << PORT_SHIFT;
 89	else
 90		which = KEYBOARD_PORT << PORT_SHIFT;
 91
 92	dev_dbg(priv->dev, "olpc_apsp_write which=%x val=%x\n", which, val);
 93	for (i = 0; i < 50; i++) {
 94		u32 sts = readl(priv->base + COMMAND_FIFO_STATUS);
 95		if ((sts & CMD_CNTR_MASK) < MAX_PENDING_CMDS) {
 96			writel(which | val,
 97			       priv->base + SECURE_PROCESSOR_COMMAND);
 98			return 0;
 99		}
100		/* SP busy. This has not been seen in practice. */
101		mdelay(1);
102	}
103
104	dev_dbg(priv->dev, "olpc_apsp_write timeout, status=%x\n",
105		readl(priv->base + COMMAND_FIFO_STATUS));
106
107	return -ETIMEDOUT;
108}
109
110static irqreturn_t olpc_apsp_rx(int irq, void *dev_id)
111{
112	struct olpc_apsp *priv = dev_id;
113	unsigned int w, tmp;
114	struct serio *serio;
115
116	/*
117	 * Write 1 to PJ_RST_INTERRUPT to acknowledge and clear the interrupt
118	 * Write 0xff00 to SECURE_PROCESSOR_COMMAND.
119	 */
120	tmp = readl(priv->base + PJ_RST_INTERRUPT);
121	if (!(tmp & SP_COMMAND_COMPLETE_RESET)) {
122		dev_warn(priv->dev, "spurious interrupt?\n");
123		return IRQ_NONE;
124	}
125
126	w = readl(priv->base + COMMAND_RETURN_STATUS);
127	dev_dbg(priv->dev, "olpc_apsp_rx %x\n", w);
128
129	if (w >> PORT_SHIFT == KEYBOARD_PORT)
130		serio = priv->kbio;
131	else
132		serio = priv->padio;
133
134	serio_interrupt(serio, w & DATA_MASK, 0);
135
136	/* Ack and clear interrupt */
137	writel(tmp | SP_COMMAND_COMPLETE_RESET, priv->base + PJ_RST_INTERRUPT);
138	writel(PORT_MASK, priv->base + SECURE_PROCESSOR_COMMAND);
139
140	pm_wakeup_event(priv->dev, 1000);
141	return IRQ_HANDLED;
142}
143
144static int olpc_apsp_open(struct serio *port)
145{
146	struct olpc_apsp *priv = port->port_data;
147	unsigned int tmp;
 
148
149	if (priv->open_count++ == 0) {
 
 
 
 
 
 
150		/* Enable interrupt 0 by clearing its bit */
151		tmp = readl(priv->base + PJ_INTERRUPT_MASK);
152		writel(tmp & ~INT_0, priv->base + PJ_INTERRUPT_MASK);
153	}
154
155	return 0;
156}
157
158static void olpc_apsp_close(struct serio *port)
159{
160	struct olpc_apsp *priv = port->port_data;
161	unsigned int tmp;
162
163	if (--priv->open_count == 0) {
164		/* Disable interrupt 0 */
165		tmp = readl(priv->base + PJ_INTERRUPT_MASK);
166		writel(tmp | INT_0, priv->base + PJ_INTERRUPT_MASK);
167	}
168}
169
170static int olpc_apsp_probe(struct platform_device *pdev)
171{
172	struct serio *kb_serio, *pad_serio;
173	struct olpc_apsp *priv;
174	struct resource *res;
175	struct device_node *np;
176	unsigned long l;
177	int error;
178
179	priv = devm_kzalloc(&pdev->dev, sizeof(struct olpc_apsp), GFP_KERNEL);
180	if (!priv)
181		return -ENOMEM;
182
183	np = pdev->dev.of_node;
 
184	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
185	priv->base = devm_ioremap_resource(&pdev->dev, res);
186	if (IS_ERR(priv->base)) {
187		dev_err(&pdev->dev, "Failed to map WTM registers\n");
188		return PTR_ERR(priv->base);
189	}
190
191	priv->irq = platform_get_irq(pdev, 0);
192	if (priv->irq < 0)
193		return priv->irq;
194
195	l = readl(priv->base + COMMAND_FIFO_STATUS);
196	if (!(l & CMD_STS_MASK)) {
197		dev_err(&pdev->dev, "SP cannot accept commands.\n");
198		return -EIO;
199	}
200
201	/* KEYBOARD */
202	kb_serio = kzalloc(sizeof(struct serio), GFP_KERNEL);
203	if (!kb_serio)
204		return -ENOMEM;
205	kb_serio->id.type	= SERIO_8042_XL;
206	kb_serio->write		= olpc_apsp_write;
207	kb_serio->open		= olpc_apsp_open;
208	kb_serio->close		= olpc_apsp_close;
209	kb_serio->port_data	= priv;
210	kb_serio->dev.parent	= &pdev->dev;
211	strlcpy(kb_serio->name, "sp keyboard", sizeof(kb_serio->name));
212	strlcpy(kb_serio->phys, "sp/serio0", sizeof(kb_serio->phys));
213	priv->kbio		= kb_serio;
214	serio_register_port(kb_serio);
215
216	/* TOUCHPAD */
217	pad_serio = kzalloc(sizeof(struct serio), GFP_KERNEL);
218	if (!pad_serio) {
219		error = -ENOMEM;
220		goto err_pad;
221	}
222	pad_serio->id.type	= SERIO_8042;
223	pad_serio->write	= olpc_apsp_write;
224	pad_serio->open		= olpc_apsp_open;
225	pad_serio->close	= olpc_apsp_close;
226	pad_serio->port_data	= priv;
227	pad_serio->dev.parent	= &pdev->dev;
228	strlcpy(pad_serio->name, "sp touchpad", sizeof(pad_serio->name));
229	strlcpy(pad_serio->phys, "sp/serio1", sizeof(pad_serio->phys));
230	priv->padio		= pad_serio;
231	serio_register_port(pad_serio);
232
233	error = request_irq(priv->irq, olpc_apsp_rx, 0, "olpc-apsp", priv);
234	if (error) {
235		dev_err(&pdev->dev, "Failed to request IRQ\n");
236		goto err_irq;
237	}
238
239	priv->dev = &pdev->dev;
240	device_init_wakeup(priv->dev, 1);
241	platform_set_drvdata(pdev, priv);
242
243	dev_dbg(&pdev->dev, "probed successfully.\n");
244	return 0;
245
246err_irq:
247	serio_unregister_port(pad_serio);
248err_pad:
249	serio_unregister_port(kb_serio);
250	return error;
251}
252
253static int olpc_apsp_remove(struct platform_device *pdev)
254{
255	struct olpc_apsp *priv = platform_get_drvdata(pdev);
256
257	free_irq(priv->irq, priv);
258
259	serio_unregister_port(priv->kbio);
260	serio_unregister_port(priv->padio);
261
262	return 0;
263}
264
265static const struct of_device_id olpc_apsp_dt_ids[] = {
266	{ .compatible = "olpc,ap-sp", },
267	{}
268};
269MODULE_DEVICE_TABLE(of, olpc_apsp_dt_ids);
270
271static struct platform_driver olpc_apsp_driver = {
272	.probe		= olpc_apsp_probe,
273	.remove		= olpc_apsp_remove,
274	.driver		= {
275		.name	= "olpc-apsp",
276		.of_match_table = olpc_apsp_dt_ids,
277	},
278};
279
280MODULE_DESCRIPTION("OLPC AP-SP serio driver");
281MODULE_LICENSE("GPL");
282module_platform_driver(olpc_apsp_driver);
v6.2
  1// SPDX-License-Identifier: GPL-2.0-or-later
  2/*
  3 * OLPC serio driver for multiplexed input from Marvell MMP security processor
  4 *
  5 * Copyright (C) 2011-2013 One Laptop Per Child
 
 
 
 
 
 
 
 
 
 
  6 */
  7
  8#include <linux/module.h>
  9#include <linux/interrupt.h>
 10#include <linux/serio.h>
 11#include <linux/err.h>
 12#include <linux/platform_device.h>
 13#include <linux/io.h>
 14#include <linux/of.h>
 15#include <linux/slab.h>
 16#include <linux/delay.h>
 17
 18/*
 19 * The OLPC XO-1.75 and XO-4 laptops do not have a hardware PS/2 controller.
 20 * Instead, the OLPC firmware runs a bit-banging PS/2 implementation on an
 21 * otherwise-unused slow processor which is included in the Marvell MMP2/MMP3
 22 * SoC, known as the "Security Processor" (SP) or "Wireless Trusted Module"
 23 * (WTM). This firmware then reports its results via the WTM registers,
 24 * which we read from the Application Processor (AP, i.e. main CPU) in this
 25 * driver.
 26 *
 27 * On the hardware side we have a PS/2 mouse and an AT keyboard, the data
 28 * is multiplexed through this system. We create a serio port for each one,
 29 * and demultiplex the data accordingly.
 30 */
 31
 32/* WTM register offsets */
 33#define SECURE_PROCESSOR_COMMAND	0x40
 34#define COMMAND_RETURN_STATUS		0x80
 35#define COMMAND_FIFO_STATUS		0xc4
 36#define PJ_RST_INTERRUPT		0xc8
 37#define PJ_INTERRUPT_MASK		0xcc
 38
 39/*
 40 * The upper byte of SECURE_PROCESSOR_COMMAND and COMMAND_RETURN_STATUS is
 41 * used to identify which port (device) is being talked to. The lower byte
 42 * is the data being sent/received.
 43 */
 44#define PORT_MASK	0xff00
 45#define DATA_MASK	0x00ff
 46#define PORT_SHIFT	8
 47#define KEYBOARD_PORT	0
 48#define TOUCHPAD_PORT	1
 49
 50/* COMMAND_FIFO_STATUS */
 51#define CMD_CNTR_MASK		0x7 /* Number of pending/unprocessed commands */
 52#define MAX_PENDING_CMDS	4   /* from device specs */
 53
 54/* PJ_RST_INTERRUPT */
 55#define SP_COMMAND_COMPLETE_RESET	0x1
 56
 57/* PJ_INTERRUPT_MASK */
 58#define INT_0	(1 << 0)
 59
 60/* COMMAND_FIFO_STATUS */
 61#define CMD_STS_MASK	0x100
 62
 63struct olpc_apsp {
 64	struct device *dev;
 65	struct serio *kbio;
 66	struct serio *padio;
 67	void __iomem *base;
 68	int open_count;
 69	int irq;
 70};
 71
 72static int olpc_apsp_write(struct serio *port, unsigned char val)
 73{
 74	struct olpc_apsp *priv = port->port_data;
 75	unsigned int i;
 76	u32 which = 0;
 77
 78	if (port == priv->padio)
 79		which = TOUCHPAD_PORT << PORT_SHIFT;
 80	else
 81		which = KEYBOARD_PORT << PORT_SHIFT;
 82
 83	dev_dbg(priv->dev, "olpc_apsp_write which=%x val=%x\n", which, val);
 84	for (i = 0; i < 50; i++) {
 85		u32 sts = readl(priv->base + COMMAND_FIFO_STATUS);
 86		if ((sts & CMD_CNTR_MASK) < MAX_PENDING_CMDS) {
 87			writel(which | val,
 88			       priv->base + SECURE_PROCESSOR_COMMAND);
 89			return 0;
 90		}
 91		/* SP busy. This has not been seen in practice. */
 92		mdelay(1);
 93	}
 94
 95	dev_dbg(priv->dev, "olpc_apsp_write timeout, status=%x\n",
 96		readl(priv->base + COMMAND_FIFO_STATUS));
 97
 98	return -ETIMEDOUT;
 99}
100
101static irqreturn_t olpc_apsp_rx(int irq, void *dev_id)
102{
103	struct olpc_apsp *priv = dev_id;
104	unsigned int w, tmp;
105	struct serio *serio;
106
107	/*
108	 * Write 1 to PJ_RST_INTERRUPT to acknowledge and clear the interrupt
109	 * Write 0xff00 to SECURE_PROCESSOR_COMMAND.
110	 */
111	tmp = readl(priv->base + PJ_RST_INTERRUPT);
112	if (!(tmp & SP_COMMAND_COMPLETE_RESET)) {
113		dev_warn(priv->dev, "spurious interrupt?\n");
114		return IRQ_NONE;
115	}
116
117	w = readl(priv->base + COMMAND_RETURN_STATUS);
118	dev_dbg(priv->dev, "olpc_apsp_rx %x\n", w);
119
120	if (w >> PORT_SHIFT == KEYBOARD_PORT)
121		serio = priv->kbio;
122	else
123		serio = priv->padio;
124
125	serio_interrupt(serio, w & DATA_MASK, 0);
126
127	/* Ack and clear interrupt */
128	writel(tmp | SP_COMMAND_COMPLETE_RESET, priv->base + PJ_RST_INTERRUPT);
129	writel(PORT_MASK, priv->base + SECURE_PROCESSOR_COMMAND);
130
131	pm_wakeup_event(priv->dev, 1000);
132	return IRQ_HANDLED;
133}
134
135static int olpc_apsp_open(struct serio *port)
136{
137	struct olpc_apsp *priv = port->port_data;
138	unsigned int tmp;
139	unsigned long l;
140
141	if (priv->open_count++ == 0) {
142		l = readl(priv->base + COMMAND_FIFO_STATUS);
143		if (!(l & CMD_STS_MASK)) {
144			dev_err(priv->dev, "SP cannot accept commands.\n");
145			return -EIO;
146		}
147
148		/* Enable interrupt 0 by clearing its bit */
149		tmp = readl(priv->base + PJ_INTERRUPT_MASK);
150		writel(tmp & ~INT_0, priv->base + PJ_INTERRUPT_MASK);
151	}
152
153	return 0;
154}
155
156static void olpc_apsp_close(struct serio *port)
157{
158	struct olpc_apsp *priv = port->port_data;
159	unsigned int tmp;
160
161	if (--priv->open_count == 0) {
162		/* Disable interrupt 0 */
163		tmp = readl(priv->base + PJ_INTERRUPT_MASK);
164		writel(tmp | INT_0, priv->base + PJ_INTERRUPT_MASK);
165	}
166}
167
168static int olpc_apsp_probe(struct platform_device *pdev)
169{
170	struct serio *kb_serio, *pad_serio;
171	struct olpc_apsp *priv;
172	struct resource *res;
 
 
173	int error;
174
175	priv = devm_kzalloc(&pdev->dev, sizeof(struct olpc_apsp), GFP_KERNEL);
176	if (!priv)
177		return -ENOMEM;
178
179	priv->dev = &pdev->dev;
180
181	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
182	priv->base = devm_ioremap_resource(&pdev->dev, res);
183	if (IS_ERR(priv->base)) {
184		dev_err(&pdev->dev, "Failed to map WTM registers\n");
185		return PTR_ERR(priv->base);
186	}
187
188	priv->irq = platform_get_irq(pdev, 0);
189	if (priv->irq < 0)
190		return priv->irq;
191
 
 
 
 
 
 
192	/* KEYBOARD */
193	kb_serio = kzalloc(sizeof(struct serio), GFP_KERNEL);
194	if (!kb_serio)
195		return -ENOMEM;
196	kb_serio->id.type	= SERIO_8042_XL;
197	kb_serio->write		= olpc_apsp_write;
198	kb_serio->open		= olpc_apsp_open;
199	kb_serio->close		= olpc_apsp_close;
200	kb_serio->port_data	= priv;
201	kb_serio->dev.parent	= &pdev->dev;
202	strscpy(kb_serio->name, "sp keyboard", sizeof(kb_serio->name));
203	strscpy(kb_serio->phys, "sp/serio0", sizeof(kb_serio->phys));
204	priv->kbio		= kb_serio;
205	serio_register_port(kb_serio);
206
207	/* TOUCHPAD */
208	pad_serio = kzalloc(sizeof(struct serio), GFP_KERNEL);
209	if (!pad_serio) {
210		error = -ENOMEM;
211		goto err_pad;
212	}
213	pad_serio->id.type	= SERIO_8042;
214	pad_serio->write	= olpc_apsp_write;
215	pad_serio->open		= olpc_apsp_open;
216	pad_serio->close	= olpc_apsp_close;
217	pad_serio->port_data	= priv;
218	pad_serio->dev.parent	= &pdev->dev;
219	strscpy(pad_serio->name, "sp touchpad", sizeof(pad_serio->name));
220	strscpy(pad_serio->phys, "sp/serio1", sizeof(pad_serio->phys));
221	priv->padio		= pad_serio;
222	serio_register_port(pad_serio);
223
224	error = request_irq(priv->irq, olpc_apsp_rx, 0, "olpc-apsp", priv);
225	if (error) {
226		dev_err(&pdev->dev, "Failed to request IRQ\n");
227		goto err_irq;
228	}
229
 
230	device_init_wakeup(priv->dev, 1);
231	platform_set_drvdata(pdev, priv);
232
233	dev_dbg(&pdev->dev, "probed successfully.\n");
234	return 0;
235
236err_irq:
237	serio_unregister_port(pad_serio);
238err_pad:
239	serio_unregister_port(kb_serio);
240	return error;
241}
242
243static int olpc_apsp_remove(struct platform_device *pdev)
244{
245	struct olpc_apsp *priv = platform_get_drvdata(pdev);
246
247	free_irq(priv->irq, priv);
248
249	serio_unregister_port(priv->kbio);
250	serio_unregister_port(priv->padio);
251
252	return 0;
253}
254
255static const struct of_device_id olpc_apsp_dt_ids[] = {
256	{ .compatible = "olpc,ap-sp", },
257	{}
258};
259MODULE_DEVICE_TABLE(of, olpc_apsp_dt_ids);
260
261static struct platform_driver olpc_apsp_driver = {
262	.probe		= olpc_apsp_probe,
263	.remove		= olpc_apsp_remove,
264	.driver		= {
265		.name	= "olpc-apsp",
266		.of_match_table = olpc_apsp_dt_ids,
267	},
268};
269
270MODULE_DESCRIPTION("OLPC AP-SP serio driver");
271MODULE_LICENSE("GPL");
272module_platform_driver(olpc_apsp_driver);