1e4b29009SMischa Jonker /* 2e4b29009SMischa Jonker * Copyright (C) 2004, 2007-2010, 2011-2012 Synopsys, Inc. (www.synopsys.com) 3e4b29009SMischa Jonker * 4e4b29009SMischa Jonker * This program is free software; you can redistribute it and/or modify 5e4b29009SMischa Jonker * it under the terms of the GNU General Public License version 2 as 6e4b29009SMischa Jonker * published by the Free Software Foundation. 7e4b29009SMischa Jonker * 8e4b29009SMischa Jonker * Driver is originally developed by Pavel Sokolov <psokolov@synopsys.com> 9e4b29009SMischa Jonker */ 10e4b29009SMischa Jonker 118d1cbc98SThierry Reding #include <linux/err.h> 12e4b29009SMischa Jonker #include <linux/module.h> 13e4b29009SMischa Jonker #include <linux/interrupt.h> 14e4b29009SMischa Jonker #include <linux/input.h> 15e4b29009SMischa Jonker #include <linux/serio.h> 16e4b29009SMischa Jonker #include <linux/platform_device.h> 172c0a4f8bSMischa Jonker #include <linux/of.h> 18e4b29009SMischa Jonker #include <linux/io.h> 19e4b29009SMischa Jonker #include <linux/kernel.h> 20e4b29009SMischa Jonker #include <linux/slab.h> 21e4b29009SMischa Jonker 22e4b29009SMischa Jonker #define ARC_PS2_PORTS 2 23e4b29009SMischa Jonker 24e4b29009SMischa Jonker #define ARC_ARC_PS2_ID 0x0001f609 25e4b29009SMischa Jonker 26e4b29009SMischa Jonker #define STAT_TIMEOUT 128 27e4b29009SMischa Jonker 28e4b29009SMischa Jonker #define PS2_STAT_RX_FRM_ERR (1) 29e4b29009SMischa Jonker #define PS2_STAT_RX_BUF_OVER (1 << 1) 30e4b29009SMischa Jonker #define PS2_STAT_RX_INT_EN (1 << 2) 31e4b29009SMischa Jonker #define PS2_STAT_RX_VAL (1 << 3) 32e4b29009SMischa Jonker #define PS2_STAT_TX_ISNOT_FUL (1 << 4) 33e4b29009SMischa Jonker #define PS2_STAT_TX_INT_EN (1 << 5) 34e4b29009SMischa Jonker 35e4b29009SMischa Jonker struct arc_ps2_port { 36e4b29009SMischa Jonker void __iomem *data_addr; 37e4b29009SMischa Jonker void __iomem *status_addr; 38e4b29009SMischa Jonker struct serio *io; 39e4b29009SMischa Jonker }; 40e4b29009SMischa Jonker 41e4b29009SMischa Jonker struct arc_ps2_data { 42e4b29009SMischa Jonker struct arc_ps2_port port[ARC_PS2_PORTS]; 43e4b29009SMischa Jonker void __iomem *addr; 44e4b29009SMischa Jonker unsigned int frame_error; 45e4b29009SMischa Jonker unsigned int buf_overflow; 46e4b29009SMischa Jonker unsigned int total_int; 47e4b29009SMischa Jonker }; 48e4b29009SMischa Jonker 49e4b29009SMischa Jonker static void arc_ps2_check_rx(struct arc_ps2_data *arc_ps2, 50e4b29009SMischa Jonker struct arc_ps2_port *port) 51e4b29009SMischa Jonker { 52e4b29009SMischa Jonker unsigned int timeout = 1000; 53e4b29009SMischa Jonker unsigned int flag, status; 54e4b29009SMischa Jonker unsigned char data; 55e4b29009SMischa Jonker 56e4b29009SMischa Jonker do { 57e4b29009SMischa Jonker status = ioread32(port->status_addr); 58e4b29009SMischa Jonker if (!(status & PS2_STAT_RX_VAL)) 59e4b29009SMischa Jonker return; 60e4b29009SMischa Jonker 61e4b29009SMischa Jonker data = ioread32(port->data_addr) & 0xff; 62e4b29009SMischa Jonker 63e4b29009SMischa Jonker flag = 0; 64e4b29009SMischa Jonker arc_ps2->total_int++; 65e4b29009SMischa Jonker if (status & PS2_STAT_RX_FRM_ERR) { 66e4b29009SMischa Jonker arc_ps2->frame_error++; 67e4b29009SMischa Jonker flag |= SERIO_PARITY; 68e4b29009SMischa Jonker } else if (status & PS2_STAT_RX_BUF_OVER) { 69e4b29009SMischa Jonker arc_ps2->buf_overflow++; 70e4b29009SMischa Jonker flag |= SERIO_FRAME; 71e4b29009SMischa Jonker } 72e4b29009SMischa Jonker 73e4b29009SMischa Jonker serio_interrupt(port->io, data, flag); 74e4b29009SMischa Jonker } while (--timeout); 75e4b29009SMischa Jonker 76e4b29009SMischa Jonker dev_err(&port->io->dev, "PS/2 hardware stuck\n"); 77e4b29009SMischa Jonker } 78e4b29009SMischa Jonker 79e4b29009SMischa Jonker static irqreturn_t arc_ps2_interrupt(int irq, void *dev) 80e4b29009SMischa Jonker { 81e4b29009SMischa Jonker struct arc_ps2_data *arc_ps2 = dev; 82e4b29009SMischa Jonker int i; 83e4b29009SMischa Jonker 84e4b29009SMischa Jonker for (i = 0; i < ARC_PS2_PORTS; i++) 85e4b29009SMischa Jonker arc_ps2_check_rx(arc_ps2, &arc_ps2->port[i]); 86e4b29009SMischa Jonker 87e4b29009SMischa Jonker return IRQ_HANDLED; 88e4b29009SMischa Jonker } 89e4b29009SMischa Jonker 90e4b29009SMischa Jonker static int arc_ps2_write(struct serio *io, unsigned char val) 91e4b29009SMischa Jonker { 92e4b29009SMischa Jonker unsigned status; 93e4b29009SMischa Jonker struct arc_ps2_port *port = io->port_data; 94e4b29009SMischa Jonker int timeout = STAT_TIMEOUT; 95e4b29009SMischa Jonker 96e4b29009SMischa Jonker do { 97e4b29009SMischa Jonker status = ioread32(port->status_addr); 98e4b29009SMischa Jonker cpu_relax(); 99e4b29009SMischa Jonker 100e4b29009SMischa Jonker if (status & PS2_STAT_TX_ISNOT_FUL) { 101e4b29009SMischa Jonker iowrite32(val & 0xff, port->data_addr); 102e4b29009SMischa Jonker return 0; 103e4b29009SMischa Jonker } 104e4b29009SMischa Jonker 105e4b29009SMischa Jonker } while (--timeout); 106e4b29009SMischa Jonker 107e4b29009SMischa Jonker dev_err(&io->dev, "write timeout\n"); 108e4b29009SMischa Jonker return -ETIMEDOUT; 109e4b29009SMischa Jonker } 110e4b29009SMischa Jonker 111e4b29009SMischa Jonker static int arc_ps2_open(struct serio *io) 112e4b29009SMischa Jonker { 113e4b29009SMischa Jonker struct arc_ps2_port *port = io->port_data; 114e4b29009SMischa Jonker 115e4b29009SMischa Jonker iowrite32(PS2_STAT_RX_INT_EN, port->status_addr); 116e4b29009SMischa Jonker 117e4b29009SMischa Jonker return 0; 118e4b29009SMischa Jonker } 119e4b29009SMischa Jonker 120e4b29009SMischa Jonker static void arc_ps2_close(struct serio *io) 121e4b29009SMischa Jonker { 122e4b29009SMischa Jonker struct arc_ps2_port *port = io->port_data; 123e4b29009SMischa Jonker 124e4b29009SMischa Jonker iowrite32(ioread32(port->status_addr) & ~PS2_STAT_RX_INT_EN, 125e4b29009SMischa Jonker port->status_addr); 126e4b29009SMischa Jonker } 127e4b29009SMischa Jonker 1285298cc4cSBill Pemberton static void __iomem *arc_ps2_calc_addr(struct arc_ps2_data *arc_ps2, 129a4d6983cSDmitry Torokhov int index, bool status) 130a4d6983cSDmitry Torokhov { 131a4d6983cSDmitry Torokhov void __iomem *addr; 132a4d6983cSDmitry Torokhov 133a4d6983cSDmitry Torokhov addr = arc_ps2->addr + 4 + 4 * index; 134a4d6983cSDmitry Torokhov if (status) 135a4d6983cSDmitry Torokhov addr += ARC_PS2_PORTS * 4; 136a4d6983cSDmitry Torokhov 137a4d6983cSDmitry Torokhov return addr; 138a4d6983cSDmitry Torokhov } 139a4d6983cSDmitry Torokhov 1405298cc4cSBill Pemberton static void arc_ps2_inhibit_ports(struct arc_ps2_data *arc_ps2) 141a4d6983cSDmitry Torokhov { 142a4d6983cSDmitry Torokhov void __iomem *addr; 143a4d6983cSDmitry Torokhov u32 val; 144a4d6983cSDmitry Torokhov int i; 145a4d6983cSDmitry Torokhov 146a4d6983cSDmitry Torokhov for (i = 0; i < ARC_PS2_PORTS; i++) { 147a4d6983cSDmitry Torokhov addr = arc_ps2_calc_addr(arc_ps2, i, true); 148a4d6983cSDmitry Torokhov val = ioread32(addr); 149a4d6983cSDmitry Torokhov val &= ~(PS2_STAT_RX_INT_EN | PS2_STAT_TX_INT_EN); 150a4d6983cSDmitry Torokhov iowrite32(val, addr); 151a4d6983cSDmitry Torokhov } 152a4d6983cSDmitry Torokhov } 153a4d6983cSDmitry Torokhov 1545298cc4cSBill Pemberton static int arc_ps2_create_port(struct platform_device *pdev, 155e4b29009SMischa Jonker struct arc_ps2_data *arc_ps2, 156e4b29009SMischa Jonker int index) 157e4b29009SMischa Jonker { 158e4b29009SMischa Jonker struct arc_ps2_port *port = &arc_ps2->port[index]; 159e4b29009SMischa Jonker struct serio *io; 160e4b29009SMischa Jonker 161e4b29009SMischa Jonker io = kzalloc(sizeof(struct serio), GFP_KERNEL); 162e4b29009SMischa Jonker if (!io) 163e4b29009SMischa Jonker return -ENOMEM; 164e4b29009SMischa Jonker 165e4b29009SMischa Jonker io->id.type = SERIO_8042; 166e4b29009SMischa Jonker io->write = arc_ps2_write; 167e4b29009SMischa Jonker io->open = arc_ps2_open; 168e4b29009SMischa Jonker io->close = arc_ps2_close; 169e4b29009SMischa Jonker snprintf(io->name, sizeof(io->name), "ARC PS/2 port%d", index); 170e4b29009SMischa Jonker snprintf(io->phys, sizeof(io->phys), "arc/serio%d", index); 171e4b29009SMischa Jonker io->port_data = port; 172e4b29009SMischa Jonker 173e4b29009SMischa Jonker port->io = io; 174e4b29009SMischa Jonker 175a4d6983cSDmitry Torokhov port->data_addr = arc_ps2_calc_addr(arc_ps2, index, false); 176a4d6983cSDmitry Torokhov port->status_addr = arc_ps2_calc_addr(arc_ps2, index, true); 177e4b29009SMischa Jonker 178e4b29009SMischa Jonker dev_dbg(&pdev->dev, "port%d is allocated (data = 0x%p, status = 0x%p)\n", 179e4b29009SMischa Jonker index, port->data_addr, port->status_addr); 180e4b29009SMischa Jonker 181e4b29009SMischa Jonker serio_register_port(port->io); 182e4b29009SMischa Jonker return 0; 183e4b29009SMischa Jonker } 184e4b29009SMischa Jonker 1855298cc4cSBill Pemberton static int arc_ps2_probe(struct platform_device *pdev) 186e4b29009SMischa Jonker { 187e4b29009SMischa Jonker struct arc_ps2_data *arc_ps2; 188a4d6983cSDmitry Torokhov struct resource *res; 189a4d6983cSDmitry Torokhov int irq; 190e4b29009SMischa Jonker int error, id, i; 191e4b29009SMischa Jonker 192a4d6983cSDmitry Torokhov irq = platform_get_irq_byname(pdev, "arc_ps2_irq"); 193a4d6983cSDmitry Torokhov if (irq < 0) { 194a4d6983cSDmitry Torokhov dev_err(&pdev->dev, "no IRQ defined\n"); 195a4d6983cSDmitry Torokhov return -EINVAL; 196a4d6983cSDmitry Torokhov } 197a4d6983cSDmitry Torokhov 198a4d6983cSDmitry Torokhov arc_ps2 = devm_kzalloc(&pdev->dev, sizeof(struct arc_ps2_data), 199a4d6983cSDmitry Torokhov GFP_KERNEL); 200e4b29009SMischa Jonker if (!arc_ps2) { 201e4b29009SMischa Jonker dev_err(&pdev->dev, "out of memory\n"); 202e4b29009SMischa Jonker return -ENOMEM; 203e4b29009SMischa Jonker } 204e4b29009SMischa Jonker 205ef0aca7fSJulia Lawall res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 2068d1cbc98SThierry Reding arc_ps2->addr = devm_ioremap_resource(&pdev->dev, res); 2078d1cbc98SThierry Reding if (IS_ERR(arc_ps2->addr)) 2088d1cbc98SThierry Reding return PTR_ERR(arc_ps2->addr); 209e4b29009SMischa Jonker 210e4b29009SMischa Jonker dev_info(&pdev->dev, "irq = %d, address = 0x%p, ports = %i\n", 211a4d6983cSDmitry Torokhov irq, arc_ps2->addr, ARC_PS2_PORTS); 212e4b29009SMischa Jonker 213e4b29009SMischa Jonker id = ioread32(arc_ps2->addr); 214e4b29009SMischa Jonker if (id != ARC_ARC_PS2_ID) { 215e4b29009SMischa Jonker dev_err(&pdev->dev, "device id does not match\n"); 216a4d6983cSDmitry Torokhov return -ENXIO; 217a4d6983cSDmitry Torokhov } 218a4d6983cSDmitry Torokhov 219a4d6983cSDmitry Torokhov arc_ps2_inhibit_ports(arc_ps2); 220a4d6983cSDmitry Torokhov 221a4d6983cSDmitry Torokhov error = devm_request_irq(&pdev->dev, irq, arc_ps2_interrupt, 222a4d6983cSDmitry Torokhov 0, "arc_ps2", arc_ps2); 223a4d6983cSDmitry Torokhov if (error) { 224a4d6983cSDmitry Torokhov dev_err(&pdev->dev, "Could not allocate IRQ\n"); 225a4d6983cSDmitry Torokhov return error; 226e4b29009SMischa Jonker } 227e4b29009SMischa Jonker 228e4b29009SMischa Jonker for (i = 0; i < ARC_PS2_PORTS; i++) { 229e4b29009SMischa Jonker error = arc_ps2_create_port(pdev, arc_ps2, i); 230e4b29009SMischa Jonker if (error) { 231a4d6983cSDmitry Torokhov while (--i >= 0) 232a4d6983cSDmitry Torokhov serio_unregister_port(arc_ps2->port[i].io); 233a4d6983cSDmitry Torokhov return error; 234a4d6983cSDmitry Torokhov } 235e4b29009SMischa Jonker } 236e4b29009SMischa Jonker 237e4b29009SMischa Jonker platform_set_drvdata(pdev, arc_ps2); 238e4b29009SMischa Jonker 239e4b29009SMischa Jonker return 0; 240e4b29009SMischa Jonker } 241e4b29009SMischa Jonker 242e2619cf7SBill Pemberton static int arc_ps2_remove(struct platform_device *pdev) 243e4b29009SMischa Jonker { 244e4b29009SMischa Jonker struct arc_ps2_data *arc_ps2 = platform_get_drvdata(pdev); 245e4b29009SMischa Jonker int i; 246e4b29009SMischa Jonker 247e4b29009SMischa Jonker for (i = 0; i < ARC_PS2_PORTS; i++) 248e4b29009SMischa Jonker serio_unregister_port(arc_ps2->port[i].io); 249e4b29009SMischa Jonker 250e4b29009SMischa Jonker dev_dbg(&pdev->dev, "interrupt count = %i\n", arc_ps2->total_int); 251e4b29009SMischa Jonker dev_dbg(&pdev->dev, "frame error count = %i\n", arc_ps2->frame_error); 252e4b29009SMischa Jonker dev_dbg(&pdev->dev, "buffer overflow count = %i\n", 253e4b29009SMischa Jonker arc_ps2->buf_overflow); 254e4b29009SMischa Jonker 255e4b29009SMischa Jonker return 0; 256e4b29009SMischa Jonker } 257e4b29009SMischa Jonker 2582c0a4f8bSMischa Jonker #ifdef CONFIG_OF 2592c0a4f8bSMischa Jonker static const struct of_device_id arc_ps2_match[] = { 2602c0a4f8bSMischa Jonker { .compatible = "snps,arc_ps2" }, 2612c0a4f8bSMischa Jonker { }, 2622c0a4f8bSMischa Jonker }; 2632c0a4f8bSMischa Jonker MODULE_DEVICE_TABLE(of, arc_ps2_match); 2642c0a4f8bSMischa Jonker #endif 2652c0a4f8bSMischa Jonker 266e4b29009SMischa Jonker static struct platform_driver arc_ps2_driver = { 267e4b29009SMischa Jonker .driver = { 268e4b29009SMischa Jonker .name = "arc_ps2", 269e4b29009SMischa Jonker .owner = THIS_MODULE, 2702c0a4f8bSMischa Jonker .of_match_table = of_match_ptr(arc_ps2_match), 271e4b29009SMischa Jonker }, 272e4b29009SMischa Jonker .probe = arc_ps2_probe, 2731cb0aa88SBill Pemberton .remove = arc_ps2_remove, 274e4b29009SMischa Jonker }; 275e4b29009SMischa Jonker 276e4b29009SMischa Jonker module_platform_driver(arc_ps2_driver); 277e4b29009SMischa Jonker 278e4b29009SMischa Jonker MODULE_LICENSE("GPL"); 279e4b29009SMischa Jonker MODULE_AUTHOR("Pavel Sokolov <psokolov@synopsys.com>"); 280e4b29009SMischa Jonker MODULE_DESCRIPTION("ARC PS/2 Driver"); 281