aspeed-lpc-snoop.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. /*
  2. * Copyright 2017 Google Inc
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License
  6. * as published by the Free Software Foundation; either version
  7. * 2 of the License, or (at your option) any later version.
  8. *
  9. * Provides a simple driver to control the ASPEED LPC snoop interface which
  10. * allows the BMC to listen on and save the data written by
  11. * the host to an arbitrary LPC I/O port.
  12. *
  13. * Typically used by the BMC to "watch" host boot progress via port
  14. * 0x80 writes made by the BIOS during the boot process.
  15. */
  16. #include <linux/bitops.h>
  17. #include <linux/interrupt.h>
  18. #include <linux/fs.h>
  19. #include <linux/kfifo.h>
  20. #include <linux/mfd/syscon.h>
  21. #include <linux/miscdevice.h>
  22. #include <linux/module.h>
  23. #include <linux/of.h>
  24. #include <linux/of_device.h>
  25. #include <linux/platform_device.h>
  26. #include <linux/poll.h>
  27. #include <linux/regmap.h>
  28. #define DEVICE_NAME "aspeed-lpc-snoop"
  29. #define NUM_SNOOP_CHANNELS 2
  30. #define SNOOP_FIFO_SIZE 2048
  31. #define HICR5 0x0
  32. #define HICR5_EN_SNP0W BIT(0)
  33. #define HICR5_ENINT_SNP0W BIT(1)
  34. #define HICR5_EN_SNP1W BIT(2)
  35. #define HICR5_ENINT_SNP1W BIT(3)
  36. #define HICR6 0x4
  37. #define HICR6_STR_SNP0W BIT(0)
  38. #define HICR6_STR_SNP1W BIT(1)
  39. #define SNPWADR 0x10
  40. #define SNPWADR_CH0_MASK GENMASK(15, 0)
  41. #define SNPWADR_CH0_SHIFT 0
  42. #define SNPWADR_CH1_MASK GENMASK(31, 16)
  43. #define SNPWADR_CH1_SHIFT 16
  44. #define SNPWDR 0x14
  45. #define SNPWDR_CH0_MASK GENMASK(7, 0)
  46. #define SNPWDR_CH0_SHIFT 0
  47. #define SNPWDR_CH1_MASK GENMASK(15, 8)
  48. #define SNPWDR_CH1_SHIFT 8
  49. #define HICRB 0x80
  50. #define HICRB_ENSNP0D BIT(14)
  51. #define HICRB_ENSNP1D BIT(15)
  52. struct aspeed_lpc_snoop_model_data {
  53. /* The ast2400 has bits 14 and 15 as reserved, whereas the ast2500
  54. * can use them.
  55. */
  56. unsigned int has_hicrb_ensnp;
  57. };
  58. struct aspeed_lpc_snoop_channel {
  59. struct kfifo fifo;
  60. wait_queue_head_t wq;
  61. struct miscdevice miscdev;
  62. };
  63. struct aspeed_lpc_snoop {
  64. struct regmap *regmap;
  65. int irq;
  66. struct aspeed_lpc_snoop_channel chan[NUM_SNOOP_CHANNELS];
  67. };
  68. static struct aspeed_lpc_snoop_channel *snoop_file_to_chan(struct file *file)
  69. {
  70. return container_of(file->private_data,
  71. struct aspeed_lpc_snoop_channel,
  72. miscdev);
  73. }
  74. static ssize_t snoop_file_read(struct file *file, char __user *buffer,
  75. size_t count, loff_t *ppos)
  76. {
  77. struct aspeed_lpc_snoop_channel *chan = snoop_file_to_chan(file);
  78. unsigned int copied;
  79. int ret = 0;
  80. if (kfifo_is_empty(&chan->fifo)) {
  81. if (file->f_flags & O_NONBLOCK)
  82. return -EAGAIN;
  83. ret = wait_event_interruptible(chan->wq,
  84. !kfifo_is_empty(&chan->fifo));
  85. if (ret == -ERESTARTSYS)
  86. return -EINTR;
  87. }
  88. ret = kfifo_to_user(&chan->fifo, buffer, count, &copied);
  89. return ret ? ret : copied;
  90. }
  91. static __poll_t snoop_file_poll(struct file *file,
  92. struct poll_table_struct *pt)
  93. {
  94. struct aspeed_lpc_snoop_channel *chan = snoop_file_to_chan(file);
  95. poll_wait(file, &chan->wq, pt);
  96. return !kfifo_is_empty(&chan->fifo) ? EPOLLIN : 0;
  97. }
  98. static const struct file_operations snoop_fops = {
  99. .owner = THIS_MODULE,
  100. .read = snoop_file_read,
  101. .poll = snoop_file_poll,
  102. .llseek = noop_llseek,
  103. };
  104. /* Save a byte to a FIFO and discard the oldest byte if FIFO is full */
  105. static void put_fifo_with_discard(struct aspeed_lpc_snoop_channel *chan, u8 val)
  106. {
  107. if (!kfifo_initialized(&chan->fifo))
  108. return;
  109. if (kfifo_is_full(&chan->fifo))
  110. kfifo_skip(&chan->fifo);
  111. kfifo_put(&chan->fifo, val);
  112. wake_up_interruptible(&chan->wq);
  113. }
  114. static irqreturn_t aspeed_lpc_snoop_irq(int irq, void *arg)
  115. {
  116. struct aspeed_lpc_snoop *lpc_snoop = arg;
  117. u32 reg, data;
  118. if (regmap_read(lpc_snoop->regmap, HICR6, &reg))
  119. return IRQ_NONE;
  120. /* Check if one of the snoop channels is interrupting */
  121. reg &= (HICR6_STR_SNP0W | HICR6_STR_SNP1W);
  122. if (!reg)
  123. return IRQ_NONE;
  124. /* Ack pending IRQs */
  125. regmap_write(lpc_snoop->regmap, HICR6, reg);
  126. /* Read and save most recent snoop'ed data byte to FIFO */
  127. regmap_read(lpc_snoop->regmap, SNPWDR, &data);
  128. if (reg & HICR6_STR_SNP0W) {
  129. u8 val = (data & SNPWDR_CH0_MASK) >> SNPWDR_CH0_SHIFT;
  130. put_fifo_with_discard(&lpc_snoop->chan[0], val);
  131. }
  132. if (reg & HICR6_STR_SNP1W) {
  133. u8 val = (data & SNPWDR_CH1_MASK) >> SNPWDR_CH1_SHIFT;
  134. put_fifo_with_discard(&lpc_snoop->chan[1], val);
  135. }
  136. return IRQ_HANDLED;
  137. }
  138. static int aspeed_lpc_snoop_config_irq(struct aspeed_lpc_snoop *lpc_snoop,
  139. struct platform_device *pdev)
  140. {
  141. struct device *dev = &pdev->dev;
  142. int rc;
  143. lpc_snoop->irq = platform_get_irq(pdev, 0);
  144. if (!lpc_snoop->irq)
  145. return -ENODEV;
  146. rc = devm_request_irq(dev, lpc_snoop->irq,
  147. aspeed_lpc_snoop_irq, IRQF_SHARED,
  148. DEVICE_NAME, lpc_snoop);
  149. if (rc < 0) {
  150. dev_warn(dev, "Unable to request IRQ %d\n", lpc_snoop->irq);
  151. lpc_snoop->irq = 0;
  152. return rc;
  153. }
  154. return 0;
  155. }
  156. static int aspeed_lpc_enable_snoop(struct aspeed_lpc_snoop *lpc_snoop,
  157. struct device *dev,
  158. int channel, u16 lpc_port)
  159. {
  160. int rc = 0;
  161. u32 hicr5_en, snpwadr_mask, snpwadr_shift, hicrb_en;
  162. const struct aspeed_lpc_snoop_model_data *model_data =
  163. of_device_get_match_data(dev);
  164. init_waitqueue_head(&lpc_snoop->chan[channel].wq);
  165. /* Create FIFO datastructure */
  166. rc = kfifo_alloc(&lpc_snoop->chan[channel].fifo,
  167. SNOOP_FIFO_SIZE, GFP_KERNEL);
  168. if (rc)
  169. return rc;
  170. lpc_snoop->chan[channel].miscdev.minor = MISC_DYNAMIC_MINOR;
  171. lpc_snoop->chan[channel].miscdev.name =
  172. devm_kasprintf(dev, GFP_KERNEL, "%s%d", DEVICE_NAME, channel);
  173. lpc_snoop->chan[channel].miscdev.fops = &snoop_fops;
  174. lpc_snoop->chan[channel].miscdev.parent = dev;
  175. rc = misc_register(&lpc_snoop->chan[channel].miscdev);
  176. if (rc)
  177. return rc;
  178. /* Enable LPC snoop channel at requested port */
  179. switch (channel) {
  180. case 0:
  181. hicr5_en = HICR5_EN_SNP0W | HICR5_ENINT_SNP0W;
  182. snpwadr_mask = SNPWADR_CH0_MASK;
  183. snpwadr_shift = SNPWADR_CH0_SHIFT;
  184. hicrb_en = HICRB_ENSNP0D;
  185. break;
  186. case 1:
  187. hicr5_en = HICR5_EN_SNP1W | HICR5_ENINT_SNP1W;
  188. snpwadr_mask = SNPWADR_CH1_MASK;
  189. snpwadr_shift = SNPWADR_CH1_SHIFT;
  190. hicrb_en = HICRB_ENSNP1D;
  191. break;
  192. default:
  193. return -EINVAL;
  194. }
  195. regmap_update_bits(lpc_snoop->regmap, HICR5, hicr5_en, hicr5_en);
  196. regmap_update_bits(lpc_snoop->regmap, SNPWADR, snpwadr_mask,
  197. lpc_port << snpwadr_shift);
  198. if (model_data->has_hicrb_ensnp)
  199. regmap_update_bits(lpc_snoop->regmap, HICRB,
  200. hicrb_en, hicrb_en);
  201. return rc;
  202. }
  203. static void aspeed_lpc_disable_snoop(struct aspeed_lpc_snoop *lpc_snoop,
  204. int channel)
  205. {
  206. switch (channel) {
  207. case 0:
  208. regmap_update_bits(lpc_snoop->regmap, HICR5,
  209. HICR5_EN_SNP0W | HICR5_ENINT_SNP0W,
  210. 0);
  211. break;
  212. case 1:
  213. regmap_update_bits(lpc_snoop->regmap, HICR5,
  214. HICR5_EN_SNP1W | HICR5_ENINT_SNP1W,
  215. 0);
  216. break;
  217. default:
  218. return;
  219. }
  220. kfifo_free(&lpc_snoop->chan[channel].fifo);
  221. misc_deregister(&lpc_snoop->chan[channel].miscdev);
  222. }
  223. static int aspeed_lpc_snoop_probe(struct platform_device *pdev)
  224. {
  225. struct aspeed_lpc_snoop *lpc_snoop;
  226. struct device *dev;
  227. u32 port;
  228. int rc;
  229. dev = &pdev->dev;
  230. lpc_snoop = devm_kzalloc(dev, sizeof(*lpc_snoop), GFP_KERNEL);
  231. if (!lpc_snoop)
  232. return -ENOMEM;
  233. lpc_snoop->regmap = syscon_node_to_regmap(
  234. pdev->dev.parent->of_node);
  235. if (IS_ERR(lpc_snoop->regmap)) {
  236. dev_err(dev, "Couldn't get regmap\n");
  237. return -ENODEV;
  238. }
  239. dev_set_drvdata(&pdev->dev, lpc_snoop);
  240. rc = of_property_read_u32_index(dev->of_node, "snoop-ports", 0, &port);
  241. if (rc) {
  242. dev_err(dev, "no snoop ports configured\n");
  243. return -ENODEV;
  244. }
  245. rc = aspeed_lpc_snoop_config_irq(lpc_snoop, pdev);
  246. if (rc)
  247. return rc;
  248. rc = aspeed_lpc_enable_snoop(lpc_snoop, dev, 0, port);
  249. if (rc)
  250. return rc;
  251. /* Configuration of 2nd snoop channel port is optional */
  252. if (of_property_read_u32_index(dev->of_node, "snoop-ports",
  253. 1, &port) == 0) {
  254. rc = aspeed_lpc_enable_snoop(lpc_snoop, dev, 1, port);
  255. if (rc)
  256. aspeed_lpc_disable_snoop(lpc_snoop, 0);
  257. }
  258. return rc;
  259. }
  260. static int aspeed_lpc_snoop_remove(struct platform_device *pdev)
  261. {
  262. struct aspeed_lpc_snoop *lpc_snoop = dev_get_drvdata(&pdev->dev);
  263. /* Disable both snoop channels */
  264. aspeed_lpc_disable_snoop(lpc_snoop, 0);
  265. aspeed_lpc_disable_snoop(lpc_snoop, 1);
  266. return 0;
  267. }
  268. static const struct aspeed_lpc_snoop_model_data ast2400_model_data = {
  269. .has_hicrb_ensnp = 0,
  270. };
  271. static const struct aspeed_lpc_snoop_model_data ast2500_model_data = {
  272. .has_hicrb_ensnp = 1,
  273. };
  274. static const struct of_device_id aspeed_lpc_snoop_match[] = {
  275. { .compatible = "aspeed,ast2400-lpc-snoop",
  276. .data = &ast2400_model_data },
  277. { .compatible = "aspeed,ast2500-lpc-snoop",
  278. .data = &ast2500_model_data },
  279. { },
  280. };
  281. static struct platform_driver aspeed_lpc_snoop_driver = {
  282. .driver = {
  283. .name = DEVICE_NAME,
  284. .of_match_table = aspeed_lpc_snoop_match,
  285. },
  286. .probe = aspeed_lpc_snoop_probe,
  287. .remove = aspeed_lpc_snoop_remove,
  288. };
  289. module_platform_driver(aspeed_lpc_snoop_driver);
  290. MODULE_DEVICE_TABLE(of, aspeed_lpc_snoop_match);
  291. MODULE_LICENSE("GPL");
  292. MODULE_AUTHOR("Robert Lippert <rlippert@google.com>");
  293. MODULE_DESCRIPTION("Linux driver to control Aspeed LPC snoop functionality");