sun4i-ps2.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. /*
  2. * Driver for Allwinner A10 PS2 host controller
  3. *
  4. * Author: Vishnu Patekar <vishnupatekar0510@gmail.com>
  5. * Aaron.maoye <leafy.myeh@newbietech.com>
  6. */
  7. #include <linux/module.h>
  8. #include <linux/serio.h>
  9. #include <linux/interrupt.h>
  10. #include <linux/errno.h>
  11. #include <linux/slab.h>
  12. #include <linux/io.h>
  13. #include <linux/clk.h>
  14. #include <linux/mod_devicetable.h>
  15. #include <linux/platform_device.h>
  16. #define DRIVER_NAME "sun4i-ps2"
  17. /* register offset definitions */
  18. #define PS2_REG_GCTL 0x00 /* PS2 Module Global Control Reg */
  19. #define PS2_REG_DATA 0x04 /* PS2 Module Data Reg */
  20. #define PS2_REG_LCTL 0x08 /* PS2 Module Line Control Reg */
  21. #define PS2_REG_LSTS 0x0C /* PS2 Module Line Status Reg */
  22. #define PS2_REG_FCTL 0x10 /* PS2 Module FIFO Control Reg */
  23. #define PS2_REG_FSTS 0x14 /* PS2 Module FIFO Status Reg */
  24. #define PS2_REG_CLKDR 0x18 /* PS2 Module Clock Divider Reg*/
  25. /* PS2 GLOBAL CONTROL REGISTER PS2_GCTL */
  26. #define PS2_GCTL_INTFLAG BIT(4)
  27. #define PS2_GCTL_INTEN BIT(3)
  28. #define PS2_GCTL_RESET BIT(2)
  29. #define PS2_GCTL_MASTER BIT(1)
  30. #define PS2_GCTL_BUSEN BIT(0)
  31. /* PS2 LINE CONTROL REGISTER */
  32. #define PS2_LCTL_NOACK BIT(18)
  33. #define PS2_LCTL_TXDTOEN BIT(8)
  34. #define PS2_LCTL_STOPERREN BIT(3)
  35. #define PS2_LCTL_ACKERREN BIT(2)
  36. #define PS2_LCTL_PARERREN BIT(1)
  37. #define PS2_LCTL_RXDTOEN BIT(0)
  38. /* PS2 LINE STATUS REGISTER */
  39. #define PS2_LSTS_TXTDO BIT(8)
  40. #define PS2_LSTS_STOPERR BIT(3)
  41. #define PS2_LSTS_ACKERR BIT(2)
  42. #define PS2_LSTS_PARERR BIT(1)
  43. #define PS2_LSTS_RXTDO BIT(0)
  44. #define PS2_LINE_ERROR_BIT \
  45. (PS2_LSTS_TXTDO | PS2_LSTS_STOPERR | PS2_LSTS_ACKERR | \
  46. PS2_LSTS_PARERR | PS2_LSTS_RXTDO)
  47. /* PS2 FIFO CONTROL REGISTER */
  48. #define PS2_FCTL_TXRST BIT(17)
  49. #define PS2_FCTL_RXRST BIT(16)
  50. #define PS2_FCTL_TXUFIEN BIT(10)
  51. #define PS2_FCTL_TXOFIEN BIT(9)
  52. #define PS2_FCTL_TXRDYIEN BIT(8)
  53. #define PS2_FCTL_RXUFIEN BIT(2)
  54. #define PS2_FCTL_RXOFIEN BIT(1)
  55. #define PS2_FCTL_RXRDYIEN BIT(0)
  56. /* PS2 FIFO STATUS REGISTER */
  57. #define PS2_FSTS_TXUF BIT(10)
  58. #define PS2_FSTS_TXOF BIT(9)
  59. #define PS2_FSTS_TXRDY BIT(8)
  60. #define PS2_FSTS_RXUF BIT(2)
  61. #define PS2_FSTS_RXOF BIT(1)
  62. #define PS2_FSTS_RXRDY BIT(0)
  63. #define PS2_FIFO_ERROR_BIT \
  64. (PS2_FSTS_TXUF | PS2_FSTS_TXOF | PS2_FSTS_RXUF | PS2_FSTS_RXOF)
  65. #define PS2_SAMPLE_CLK 1000000
  66. #define PS2_SCLK 125000
  67. struct sun4i_ps2data {
  68. struct serio *serio;
  69. struct device *dev;
  70. /* IO mapping base */
  71. void __iomem *reg_base;
  72. /* clock management */
  73. struct clk *clk;
  74. /* irq */
  75. spinlock_t lock;
  76. int irq;
  77. };
  78. static irqreturn_t sun4i_ps2_interrupt(int irq, void *dev_id)
  79. {
  80. struct sun4i_ps2data *drvdata = dev_id;
  81. u32 intr_status;
  82. u32 fifo_status;
  83. unsigned char byte;
  84. unsigned int rxflags = 0;
  85. u32 rval;
  86. spin_lock(&drvdata->lock);
  87. /* Get the PS/2 interrupts and clear them */
  88. intr_status = readl(drvdata->reg_base + PS2_REG_LSTS);
  89. fifo_status = readl(drvdata->reg_base + PS2_REG_FSTS);
  90. /* Check line status register */
  91. if (intr_status & PS2_LINE_ERROR_BIT) {
  92. rxflags = (intr_status & PS2_LINE_ERROR_BIT) ? SERIO_FRAME : 0;
  93. rxflags |= (intr_status & PS2_LSTS_PARERR) ? SERIO_PARITY : 0;
  94. rxflags |= (intr_status & PS2_LSTS_PARERR) ? SERIO_TIMEOUT : 0;
  95. rval = PS2_LSTS_TXTDO | PS2_LSTS_STOPERR | PS2_LSTS_ACKERR |
  96. PS2_LSTS_PARERR | PS2_LSTS_RXTDO;
  97. writel(rval, drvdata->reg_base + PS2_REG_LSTS);
  98. }
  99. /* Check FIFO status register */
  100. if (fifo_status & PS2_FIFO_ERROR_BIT) {
  101. rval = PS2_FSTS_TXUF | PS2_FSTS_TXOF | PS2_FSTS_TXRDY |
  102. PS2_FSTS_RXUF | PS2_FSTS_RXOF | PS2_FSTS_RXRDY;
  103. writel(rval, drvdata->reg_base + PS2_REG_FSTS);
  104. }
  105. rval = (fifo_status >> 16) & 0x3;
  106. while (rval--) {
  107. byte = readl(drvdata->reg_base + PS2_REG_DATA) & 0xff;
  108. serio_interrupt(drvdata->serio, byte, rxflags);
  109. }
  110. writel(intr_status, drvdata->reg_base + PS2_REG_LSTS);
  111. writel(fifo_status, drvdata->reg_base + PS2_REG_FSTS);
  112. spin_unlock(&drvdata->lock);
  113. return IRQ_HANDLED;
  114. }
  115. static int sun4i_ps2_open(struct serio *serio)
  116. {
  117. struct sun4i_ps2data *drvdata = serio->port_data;
  118. u32 src_clk = 0;
  119. u32 clk_scdf;
  120. u32 clk_pcdf;
  121. u32 rval;
  122. unsigned long flags;
  123. /* Set line control and enable interrupt */
  124. rval = PS2_LCTL_STOPERREN | PS2_LCTL_ACKERREN
  125. | PS2_LCTL_PARERREN | PS2_LCTL_RXDTOEN;
  126. writel(rval, drvdata->reg_base + PS2_REG_LCTL);
  127. /* Reset FIFO */
  128. rval = PS2_FCTL_TXRST | PS2_FCTL_RXRST | PS2_FCTL_TXUFIEN
  129. | PS2_FCTL_TXOFIEN | PS2_FCTL_RXUFIEN
  130. | PS2_FCTL_RXOFIEN | PS2_FCTL_RXRDYIEN;
  131. writel(rval, drvdata->reg_base + PS2_REG_FCTL);
  132. src_clk = clk_get_rate(drvdata->clk);
  133. /* Set clock divider register */
  134. clk_scdf = src_clk / PS2_SAMPLE_CLK - 1;
  135. clk_pcdf = PS2_SAMPLE_CLK / PS2_SCLK - 1;
  136. rval = (clk_scdf << 8) | clk_pcdf;
  137. writel(rval, drvdata->reg_base + PS2_REG_CLKDR);
  138. /* Set global control register */
  139. rval = PS2_GCTL_RESET | PS2_GCTL_INTEN | PS2_GCTL_MASTER
  140. | PS2_GCTL_BUSEN;
  141. spin_lock_irqsave(&drvdata->lock, flags);
  142. writel(rval, drvdata->reg_base + PS2_REG_GCTL);
  143. spin_unlock_irqrestore(&drvdata->lock, flags);
  144. return 0;
  145. }
  146. static void sun4i_ps2_close(struct serio *serio)
  147. {
  148. struct sun4i_ps2data *drvdata = serio->port_data;
  149. u32 rval;
  150. /* Shut off the interrupt */
  151. rval = readl(drvdata->reg_base + PS2_REG_GCTL);
  152. writel(rval & ~(PS2_GCTL_INTEN), drvdata->reg_base + PS2_REG_GCTL);
  153. synchronize_irq(drvdata->irq);
  154. }
  155. static int sun4i_ps2_write(struct serio *serio, unsigned char val)
  156. {
  157. unsigned long expire = jiffies + msecs_to_jiffies(10000);
  158. struct sun4i_ps2data *drvdata = serio->port_data;
  159. do {
  160. if (readl(drvdata->reg_base + PS2_REG_FSTS) & PS2_FSTS_TXRDY) {
  161. writel(val, drvdata->reg_base + PS2_REG_DATA);
  162. return 0;
  163. }
  164. } while (time_before(jiffies, expire));
  165. return SERIO_TIMEOUT;
  166. }
  167. static int sun4i_ps2_probe(struct platform_device *pdev)
  168. {
  169. struct resource *res; /* IO mem resources */
  170. struct sun4i_ps2data *drvdata;
  171. struct serio *serio;
  172. struct device *dev = &pdev->dev;
  173. unsigned int irq;
  174. int error;
  175. drvdata = kzalloc(sizeof(struct sun4i_ps2data), GFP_KERNEL);
  176. serio = kzalloc(sizeof(struct serio), GFP_KERNEL);
  177. if (!drvdata || !serio) {
  178. error = -ENOMEM;
  179. goto err_free_mem;
  180. }
  181. spin_lock_init(&drvdata->lock);
  182. /* IO */
  183. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  184. if (!res) {
  185. dev_err(dev, "failed to locate registers\n");
  186. error = -ENXIO;
  187. goto err_free_mem;
  188. }
  189. drvdata->reg_base = ioremap(res->start, resource_size(res));
  190. if (!drvdata->reg_base) {
  191. dev_err(dev, "failed to map registers\n");
  192. error = -ENOMEM;
  193. goto err_free_mem;
  194. }
  195. drvdata->clk = clk_get(dev, NULL);
  196. if (IS_ERR(drvdata->clk)) {
  197. error = PTR_ERR(drvdata->clk);
  198. dev_err(dev, "couldn't get clock %d\n", error);
  199. goto err_ioremap;
  200. }
  201. error = clk_prepare_enable(drvdata->clk);
  202. if (error) {
  203. dev_err(dev, "failed to enable clock %d\n", error);
  204. goto err_clk;
  205. }
  206. serio->id.type = SERIO_8042;
  207. serio->write = sun4i_ps2_write;
  208. serio->open = sun4i_ps2_open;
  209. serio->close = sun4i_ps2_close;
  210. serio->port_data = drvdata;
  211. serio->dev.parent = dev;
  212. strlcpy(serio->name, dev_name(dev), sizeof(serio->name));
  213. strlcpy(serio->phys, dev_name(dev), sizeof(serio->phys));
  214. /* shutoff interrupt */
  215. writel(0, drvdata->reg_base + PS2_REG_GCTL);
  216. /* Get IRQ for the device */
  217. irq = platform_get_irq(pdev, 0);
  218. if (!irq) {
  219. dev_err(dev, "no IRQ found\n");
  220. error = -ENXIO;
  221. goto err_disable_clk;
  222. }
  223. drvdata->irq = irq;
  224. drvdata->serio = serio;
  225. drvdata->dev = dev;
  226. error = request_irq(drvdata->irq, sun4i_ps2_interrupt, 0,
  227. DRIVER_NAME, drvdata);
  228. if (error) {
  229. dev_err(drvdata->dev, "failed to allocate interrupt %d: %d\n",
  230. drvdata->irq, error);
  231. goto err_disable_clk;
  232. }
  233. serio_register_port(serio);
  234. platform_set_drvdata(pdev, drvdata);
  235. return 0; /* success */
  236. err_disable_clk:
  237. clk_disable_unprepare(drvdata->clk);
  238. err_clk:
  239. clk_put(drvdata->clk);
  240. err_ioremap:
  241. iounmap(drvdata->reg_base);
  242. err_free_mem:
  243. kfree(serio);
  244. kfree(drvdata);
  245. return error;
  246. }
  247. static int sun4i_ps2_remove(struct platform_device *pdev)
  248. {
  249. struct sun4i_ps2data *drvdata = platform_get_drvdata(pdev);
  250. serio_unregister_port(drvdata->serio);
  251. free_irq(drvdata->irq, drvdata);
  252. clk_disable_unprepare(drvdata->clk);
  253. clk_put(drvdata->clk);
  254. iounmap(drvdata->reg_base);
  255. kfree(drvdata);
  256. return 0;
  257. }
  258. static const struct of_device_id sun4i_ps2_match[] = {
  259. { .compatible = "allwinner,sun4i-a10-ps2", },
  260. { },
  261. };
  262. MODULE_DEVICE_TABLE(of, sun4i_ps2_match);
  263. static struct platform_driver sun4i_ps2_driver = {
  264. .probe = sun4i_ps2_probe,
  265. .remove = sun4i_ps2_remove,
  266. .driver = {
  267. .name = DRIVER_NAME,
  268. .of_match_table = sun4i_ps2_match,
  269. },
  270. };
  271. module_platform_driver(sun4i_ps2_driver);
  272. MODULE_AUTHOR("Vishnu Patekar <vishnupatekar0510@gmail.com>");
  273. MODULE_AUTHOR("Aaron.maoye <leafy.myeh@newbietech.com>");
  274. MODULE_DESCRIPTION("Allwinner A10/Sun4i PS/2 driver");
  275. MODULE_LICENSE("GPL v2");