olpc_apsp.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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. #include <linux/module.h>
  17. #include <linux/interrupt.h>
  18. #include <linux/serio.h>
  19. #include <linux/err.h>
  20. #include <linux/platform_device.h>
  21. #include <linux/io.h>
  22. #include <linux/of.h>
  23. #include <linux/slab.h>
  24. #include <linux/delay.h>
  25. /*
  26. * The OLPC XO-1.75 and XO-4 laptops do not have a hardware PS/2 controller.
  27. * Instead, the OLPC firmware runs a bit-banging PS/2 implementation on an
  28. * otherwise-unused slow processor which is included in the Marvell MMP2/MMP3
  29. * SoC, known as the "Security Processor" (SP) or "Wireless Trusted Module"
  30. * (WTM). This firmware then reports its results via the WTM registers,
  31. * which we read from the Application Processor (AP, i.e. main CPU) in this
  32. * driver.
  33. *
  34. * On the hardware side we have a PS/2 mouse and an AT keyboard, the data
  35. * is multiplexed through this system. We create a serio port for each one,
  36. * and demultiplex the data accordingly.
  37. */
  38. /* WTM register offsets */
  39. #define SECURE_PROCESSOR_COMMAND 0x40
  40. #define COMMAND_RETURN_STATUS 0x80
  41. #define COMMAND_FIFO_STATUS 0xc4
  42. #define PJ_RST_INTERRUPT 0xc8
  43. #define PJ_INTERRUPT_MASK 0xcc
  44. /*
  45. * The upper byte of SECURE_PROCESSOR_COMMAND and COMMAND_RETURN_STATUS is
  46. * used to identify which port (device) is being talked to. The lower byte
  47. * is the data being sent/received.
  48. */
  49. #define PORT_MASK 0xff00
  50. #define DATA_MASK 0x00ff
  51. #define PORT_SHIFT 8
  52. #define KEYBOARD_PORT 0
  53. #define TOUCHPAD_PORT 1
  54. /* COMMAND_FIFO_STATUS */
  55. #define CMD_CNTR_MASK 0x7 /* Number of pending/unprocessed commands */
  56. #define MAX_PENDING_CMDS 4 /* from device specs */
  57. /* PJ_RST_INTERRUPT */
  58. #define SP_COMMAND_COMPLETE_RESET 0x1
  59. /* PJ_INTERRUPT_MASK */
  60. #define INT_0 (1 << 0)
  61. /* COMMAND_FIFO_STATUS */
  62. #define CMD_STS_MASK 0x100
  63. struct 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. static int olpc_apsp_write(struct serio *port, unsigned char val)
  72. {
  73. struct olpc_apsp *priv = port->port_data;
  74. unsigned int i;
  75. u32 which = 0;
  76. if (port == priv->padio)
  77. which = TOUCHPAD_PORT << PORT_SHIFT;
  78. else
  79. which = KEYBOARD_PORT << PORT_SHIFT;
  80. dev_dbg(priv->dev, "olpc_apsp_write which=%x val=%x\n", which, val);
  81. for (i = 0; i < 50; i++) {
  82. u32 sts = readl(priv->base + COMMAND_FIFO_STATUS);
  83. if ((sts & CMD_CNTR_MASK) < MAX_PENDING_CMDS) {
  84. writel(which | val,
  85. priv->base + SECURE_PROCESSOR_COMMAND);
  86. return 0;
  87. }
  88. /* SP busy. This has not been seen in practice. */
  89. mdelay(1);
  90. }
  91. dev_dbg(priv->dev, "olpc_apsp_write timeout, status=%x\n",
  92. readl(priv->base + COMMAND_FIFO_STATUS));
  93. return -ETIMEDOUT;
  94. }
  95. static irqreturn_t olpc_apsp_rx(int irq, void *dev_id)
  96. {
  97. struct olpc_apsp *priv = dev_id;
  98. unsigned int w, tmp;
  99. struct serio *serio;
  100. /*
  101. * Write 1 to PJ_RST_INTERRUPT to acknowledge and clear the interrupt
  102. * Write 0xff00 to SECURE_PROCESSOR_COMMAND.
  103. */
  104. tmp = readl(priv->base + PJ_RST_INTERRUPT);
  105. if (!(tmp & SP_COMMAND_COMPLETE_RESET)) {
  106. dev_warn(priv->dev, "spurious interrupt?\n");
  107. return IRQ_NONE;
  108. }
  109. w = readl(priv->base + COMMAND_RETURN_STATUS);
  110. dev_dbg(priv->dev, "olpc_apsp_rx %x\n", w);
  111. if (w >> PORT_SHIFT == KEYBOARD_PORT)
  112. serio = priv->kbio;
  113. else
  114. serio = priv->padio;
  115. serio_interrupt(serio, w & DATA_MASK, 0);
  116. /* Ack and clear interrupt */
  117. writel(tmp | SP_COMMAND_COMPLETE_RESET, priv->base + PJ_RST_INTERRUPT);
  118. writel(PORT_MASK, priv->base + SECURE_PROCESSOR_COMMAND);
  119. pm_wakeup_event(priv->dev, 1000);
  120. return IRQ_HANDLED;
  121. }
  122. static int olpc_apsp_open(struct serio *port)
  123. {
  124. struct olpc_apsp *priv = port->port_data;
  125. unsigned int tmp;
  126. if (priv->open_count++ == 0) {
  127. /* Enable interrupt 0 by clearing its bit */
  128. tmp = readl(priv->base + PJ_INTERRUPT_MASK);
  129. writel(tmp & ~INT_0, priv->base + PJ_INTERRUPT_MASK);
  130. }
  131. return 0;
  132. }
  133. static void olpc_apsp_close(struct serio *port)
  134. {
  135. struct olpc_apsp *priv = port->port_data;
  136. unsigned int tmp;
  137. if (--priv->open_count == 0) {
  138. /* Disable interrupt 0 */
  139. tmp = readl(priv->base + PJ_INTERRUPT_MASK);
  140. writel(tmp | INT_0, priv->base + PJ_INTERRUPT_MASK);
  141. }
  142. }
  143. static int olpc_apsp_probe(struct platform_device *pdev)
  144. {
  145. struct serio *kb_serio, *pad_serio;
  146. struct olpc_apsp *priv;
  147. struct resource *res;
  148. struct device_node *np;
  149. unsigned long l;
  150. int error;
  151. priv = devm_kzalloc(&pdev->dev, sizeof(struct olpc_apsp), GFP_KERNEL);
  152. if (!priv)
  153. return -ENOMEM;
  154. np = pdev->dev.of_node;
  155. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  156. priv->base = devm_ioremap_resource(&pdev->dev, res);
  157. if (IS_ERR(priv->base)) {
  158. dev_err(&pdev->dev, "Failed to map WTM registers\n");
  159. return PTR_ERR(priv->base);
  160. }
  161. priv->irq = platform_get_irq(pdev, 0);
  162. if (priv->irq < 0)
  163. return priv->irq;
  164. l = readl(priv->base + COMMAND_FIFO_STATUS);
  165. if (!(l & CMD_STS_MASK)) {
  166. dev_err(&pdev->dev, "SP cannot accept commands.\n");
  167. return -EIO;
  168. }
  169. /* KEYBOARD */
  170. kb_serio = kzalloc(sizeof(struct serio), GFP_KERNEL);
  171. if (!kb_serio)
  172. return -ENOMEM;
  173. kb_serio->id.type = SERIO_8042_XL;
  174. kb_serio->write = olpc_apsp_write;
  175. kb_serio->open = olpc_apsp_open;
  176. kb_serio->close = olpc_apsp_close;
  177. kb_serio->port_data = priv;
  178. kb_serio->dev.parent = &pdev->dev;
  179. strlcpy(kb_serio->name, "sp keyboard", sizeof(kb_serio->name));
  180. strlcpy(kb_serio->phys, "sp/serio0", sizeof(kb_serio->phys));
  181. priv->kbio = kb_serio;
  182. serio_register_port(kb_serio);
  183. /* TOUCHPAD */
  184. pad_serio = kzalloc(sizeof(struct serio), GFP_KERNEL);
  185. if (!pad_serio) {
  186. error = -ENOMEM;
  187. goto err_pad;
  188. }
  189. pad_serio->id.type = SERIO_8042;
  190. pad_serio->write = olpc_apsp_write;
  191. pad_serio->open = olpc_apsp_open;
  192. pad_serio->close = olpc_apsp_close;
  193. pad_serio->port_data = priv;
  194. pad_serio->dev.parent = &pdev->dev;
  195. strlcpy(pad_serio->name, "sp touchpad", sizeof(pad_serio->name));
  196. strlcpy(pad_serio->phys, "sp/serio1", sizeof(pad_serio->phys));
  197. priv->padio = pad_serio;
  198. serio_register_port(pad_serio);
  199. error = request_irq(priv->irq, olpc_apsp_rx, 0, "olpc-apsp", priv);
  200. if (error) {
  201. dev_err(&pdev->dev, "Failed to request IRQ\n");
  202. goto err_irq;
  203. }
  204. priv->dev = &pdev->dev;
  205. device_init_wakeup(priv->dev, 1);
  206. platform_set_drvdata(pdev, priv);
  207. dev_dbg(&pdev->dev, "probed successfully.\n");
  208. return 0;
  209. err_irq:
  210. serio_unregister_port(pad_serio);
  211. err_pad:
  212. serio_unregister_port(kb_serio);
  213. return error;
  214. }
  215. static int olpc_apsp_remove(struct platform_device *pdev)
  216. {
  217. struct olpc_apsp *priv = platform_get_drvdata(pdev);
  218. free_irq(priv->irq, priv);
  219. serio_unregister_port(priv->kbio);
  220. serio_unregister_port(priv->padio);
  221. return 0;
  222. }
  223. static const struct of_device_id olpc_apsp_dt_ids[] = {
  224. { .compatible = "olpc,ap-sp", },
  225. {}
  226. };
  227. MODULE_DEVICE_TABLE(of, olpc_apsp_dt_ids);
  228. static struct platform_driver olpc_apsp_driver = {
  229. .probe = olpc_apsp_probe,
  230. .remove = olpc_apsp_remove,
  231. .driver = {
  232. .name = "olpc-apsp",
  233. .of_match_table = olpc_apsp_dt_ids,
  234. },
  235. };
  236. MODULE_DESCRIPTION("OLPC AP-SP serio driver");
  237. MODULE_LICENSE("GPL");
  238. module_platform_driver(olpc_apsp_driver);