olpc_apsp.c 7.5 KB

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