aspeed-lpc-snoop.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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/kfifo.h>
  19. #include <linux/mfd/syscon.h>
  20. #include <linux/module.h>
  21. #include <linux/of.h>
  22. #include <linux/of_device.h>
  23. #include <linux/platform_device.h>
  24. #include <linux/regmap.h>
  25. #define DEVICE_NAME "aspeed-lpc-snoop"
  26. #define NUM_SNOOP_CHANNELS 2
  27. #define SNOOP_FIFO_SIZE 2048
  28. #define HICR5 0x0
  29. #define HICR5_EN_SNP0W BIT(0)
  30. #define HICR5_ENINT_SNP0W BIT(1)
  31. #define HICR5_EN_SNP1W BIT(2)
  32. #define HICR5_ENINT_SNP1W BIT(3)
  33. #define HICR6 0x4
  34. #define HICR6_STR_SNP0W BIT(0)
  35. #define HICR6_STR_SNP1W BIT(1)
  36. #define SNPWADR 0x10
  37. #define SNPWADR_CH0_MASK GENMASK(15, 0)
  38. #define SNPWADR_CH0_SHIFT 0
  39. #define SNPWADR_CH1_MASK GENMASK(31, 16)
  40. #define SNPWADR_CH1_SHIFT 16
  41. #define SNPWDR 0x14
  42. #define SNPWDR_CH0_MASK GENMASK(7, 0)
  43. #define SNPWDR_CH0_SHIFT 0
  44. #define SNPWDR_CH1_MASK GENMASK(15, 8)
  45. #define SNPWDR_CH1_SHIFT 8
  46. #define HICRB 0x80
  47. #define HICRB_ENSNP0D BIT(14)
  48. #define HICRB_ENSNP1D BIT(15)
  49. struct aspeed_lpc_snoop_model_data {
  50. /* The ast2400 has bits 14 and 15 as reserved, whereas the ast2500
  51. * can use them.
  52. */
  53. unsigned int has_hicrb_ensnp;
  54. };
  55. struct aspeed_lpc_snoop {
  56. struct regmap *regmap;
  57. int irq;
  58. struct kfifo snoop_fifo[NUM_SNOOP_CHANNELS];
  59. };
  60. /* Save a byte to a FIFO and discard the oldest byte if FIFO is full */
  61. static void put_fifo_with_discard(struct kfifo *fifo, u8 val)
  62. {
  63. if (!kfifo_initialized(fifo))
  64. return;
  65. if (kfifo_is_full(fifo))
  66. kfifo_skip(fifo);
  67. kfifo_put(fifo, val);
  68. }
  69. static irqreturn_t aspeed_lpc_snoop_irq(int irq, void *arg)
  70. {
  71. struct aspeed_lpc_snoop *lpc_snoop = arg;
  72. u32 reg, data;
  73. if (regmap_read(lpc_snoop->regmap, HICR6, &reg))
  74. return IRQ_NONE;
  75. /* Check if one of the snoop channels is interrupting */
  76. reg &= (HICR6_STR_SNP0W | HICR6_STR_SNP1W);
  77. if (!reg)
  78. return IRQ_NONE;
  79. /* Ack pending IRQs */
  80. regmap_write(lpc_snoop->regmap, HICR6, reg);
  81. /* Read and save most recent snoop'ed data byte to FIFO */
  82. regmap_read(lpc_snoop->regmap, SNPWDR, &data);
  83. if (reg & HICR6_STR_SNP0W) {
  84. u8 val = (data & SNPWDR_CH0_MASK) >> SNPWDR_CH0_SHIFT;
  85. put_fifo_with_discard(&lpc_snoop->snoop_fifo[0], val);
  86. }
  87. if (reg & HICR6_STR_SNP1W) {
  88. u8 val = (data & SNPWDR_CH1_MASK) >> SNPWDR_CH1_SHIFT;
  89. put_fifo_with_discard(&lpc_snoop->snoop_fifo[1], val);
  90. }
  91. return IRQ_HANDLED;
  92. }
  93. static int aspeed_lpc_snoop_config_irq(struct aspeed_lpc_snoop *lpc_snoop,
  94. struct platform_device *pdev)
  95. {
  96. struct device *dev = &pdev->dev;
  97. int rc;
  98. lpc_snoop->irq = platform_get_irq(pdev, 0);
  99. if (!lpc_snoop->irq)
  100. return -ENODEV;
  101. rc = devm_request_irq(dev, lpc_snoop->irq,
  102. aspeed_lpc_snoop_irq, IRQF_SHARED,
  103. DEVICE_NAME, lpc_snoop);
  104. if (rc < 0) {
  105. dev_warn(dev, "Unable to request IRQ %d\n", lpc_snoop->irq);
  106. lpc_snoop->irq = 0;
  107. return rc;
  108. }
  109. return 0;
  110. }
  111. static int aspeed_lpc_enable_snoop(struct aspeed_lpc_snoop *lpc_snoop,
  112. struct device *dev,
  113. int channel, u16 lpc_port)
  114. {
  115. int rc = 0;
  116. u32 hicr5_en, snpwadr_mask, snpwadr_shift, hicrb_en;
  117. const struct aspeed_lpc_snoop_model_data *model_data =
  118. of_device_get_match_data(dev);
  119. /* Create FIFO datastructure */
  120. rc = kfifo_alloc(&lpc_snoop->snoop_fifo[channel],
  121. SNOOP_FIFO_SIZE, GFP_KERNEL);
  122. if (rc)
  123. return rc;
  124. /* Enable LPC snoop channel at requested port */
  125. switch (channel) {
  126. case 0:
  127. hicr5_en = HICR5_EN_SNP0W | HICR5_ENINT_SNP0W;
  128. snpwadr_mask = SNPWADR_CH0_MASK;
  129. snpwadr_shift = SNPWADR_CH0_SHIFT;
  130. hicrb_en = HICRB_ENSNP0D;
  131. break;
  132. case 1:
  133. hicr5_en = HICR5_EN_SNP1W | HICR5_ENINT_SNP1W;
  134. snpwadr_mask = SNPWADR_CH1_MASK;
  135. snpwadr_shift = SNPWADR_CH1_SHIFT;
  136. hicrb_en = HICRB_ENSNP1D;
  137. break;
  138. default:
  139. return -EINVAL;
  140. }
  141. regmap_update_bits(lpc_snoop->regmap, HICR5, hicr5_en, hicr5_en);
  142. regmap_update_bits(lpc_snoop->regmap, SNPWADR, snpwadr_mask,
  143. lpc_port << snpwadr_shift);
  144. if (model_data->has_hicrb_ensnp)
  145. regmap_update_bits(lpc_snoop->regmap, HICRB,
  146. hicrb_en, hicrb_en);
  147. return rc;
  148. }
  149. static void aspeed_lpc_disable_snoop(struct aspeed_lpc_snoop *lpc_snoop,
  150. int channel)
  151. {
  152. switch (channel) {
  153. case 0:
  154. regmap_update_bits(lpc_snoop->regmap, HICR5,
  155. HICR5_EN_SNP0W | HICR5_ENINT_SNP0W,
  156. 0);
  157. break;
  158. case 1:
  159. regmap_update_bits(lpc_snoop->regmap, HICR5,
  160. HICR5_EN_SNP1W | HICR5_ENINT_SNP1W,
  161. 0);
  162. break;
  163. default:
  164. return;
  165. }
  166. kfifo_free(&lpc_snoop->snoop_fifo[channel]);
  167. }
  168. static int aspeed_lpc_snoop_probe(struct platform_device *pdev)
  169. {
  170. struct aspeed_lpc_snoop *lpc_snoop;
  171. struct device *dev;
  172. u32 port;
  173. int rc;
  174. dev = &pdev->dev;
  175. lpc_snoop = devm_kzalloc(dev, sizeof(*lpc_snoop), GFP_KERNEL);
  176. if (!lpc_snoop)
  177. return -ENOMEM;
  178. lpc_snoop->regmap = syscon_node_to_regmap(
  179. pdev->dev.parent->of_node);
  180. if (IS_ERR(lpc_snoop->regmap)) {
  181. dev_err(dev, "Couldn't get regmap\n");
  182. return -ENODEV;
  183. }
  184. dev_set_drvdata(&pdev->dev, lpc_snoop);
  185. rc = of_property_read_u32_index(dev->of_node, "snoop-ports", 0, &port);
  186. if (rc) {
  187. dev_err(dev, "no snoop ports configured\n");
  188. return -ENODEV;
  189. }
  190. rc = aspeed_lpc_snoop_config_irq(lpc_snoop, pdev);
  191. if (rc)
  192. return rc;
  193. rc = aspeed_lpc_enable_snoop(lpc_snoop, dev, 0, port);
  194. if (rc)
  195. return rc;
  196. /* Configuration of 2nd snoop channel port is optional */
  197. if (of_property_read_u32_index(dev->of_node, "snoop-ports",
  198. 1, &port) == 0) {
  199. rc = aspeed_lpc_enable_snoop(lpc_snoop, dev, 1, port);
  200. if (rc)
  201. aspeed_lpc_disable_snoop(lpc_snoop, 0);
  202. }
  203. return rc;
  204. }
  205. static int aspeed_lpc_snoop_remove(struct platform_device *pdev)
  206. {
  207. struct aspeed_lpc_snoop *lpc_snoop = dev_get_drvdata(&pdev->dev);
  208. /* Disable both snoop channels */
  209. aspeed_lpc_disable_snoop(lpc_snoop, 0);
  210. aspeed_lpc_disable_snoop(lpc_snoop, 1);
  211. return 0;
  212. }
  213. static const struct aspeed_lpc_snoop_model_data ast2400_model_data = {
  214. .has_hicrb_ensnp = 0,
  215. };
  216. static const struct aspeed_lpc_snoop_model_data ast2500_model_data = {
  217. .has_hicrb_ensnp = 1,
  218. };
  219. static const struct of_device_id aspeed_lpc_snoop_match[] = {
  220. { .compatible = "aspeed,ast2400-lpc-snoop",
  221. .data = &ast2400_model_data },
  222. { .compatible = "aspeed,ast2500-lpc-snoop",
  223. .data = &ast2500_model_data },
  224. { },
  225. };
  226. static struct platform_driver aspeed_lpc_snoop_driver = {
  227. .driver = {
  228. .name = DEVICE_NAME,
  229. .of_match_table = aspeed_lpc_snoop_match,
  230. },
  231. .probe = aspeed_lpc_snoop_probe,
  232. .remove = aspeed_lpc_snoop_remove,
  233. };
  234. module_platform_driver(aspeed_lpc_snoop_driver);
  235. MODULE_DEVICE_TABLE(of, aspeed_lpc_snoop_match);
  236. MODULE_LICENSE("GPL");
  237. MODULE_AUTHOR("Robert Lippert <rlippert@google.com>");
  238. MODULE_DESCRIPTION("Linux driver to control Aspeed LPC snoop functionality");