aspeed-lpc-snoop.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright 2017 Google Inc
  4. *
  5. * Provides a simple driver to control the ASPEED LPC snoop interface which
  6. * allows the BMC to listen on and save the data written by
  7. * the host to an arbitrary LPC I/O port.
  8. *
  9. * Typically used by the BMC to "watch" host boot progress via port
  10. * 0x80 writes made by the BIOS during the boot process.
  11. */
  12. #include <linux/bitops.h>
  13. #include <linux/clk.h>
  14. #include <linux/interrupt.h>
  15. #include <linux/fs.h>
  16. #include <linux/kfifo.h>
  17. #include <linux/mfd/syscon.h>
  18. #include <linux/miscdevice.h>
  19. #include <linux/module.h>
  20. #include <linux/of.h>
  21. #include <linux/of_device.h>
  22. #include <linux/platform_device.h>
  23. #include <linux/poll.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_channel {
  56. struct kfifo fifo;
  57. wait_queue_head_t wq;
  58. struct miscdevice miscdev;
  59. };
  60. struct aspeed_lpc_snoop {
  61. struct regmap *regmap;
  62. int irq;
  63. struct clk *clk;
  64. struct aspeed_lpc_snoop_channel chan[NUM_SNOOP_CHANNELS];
  65. };
  66. static struct aspeed_lpc_snoop_channel *snoop_file_to_chan(struct file *file)
  67. {
  68. return container_of(file->private_data,
  69. struct aspeed_lpc_snoop_channel,
  70. miscdev);
  71. }
  72. static ssize_t snoop_file_read(struct file *file, char __user *buffer,
  73. size_t count, loff_t *ppos)
  74. {
  75. struct aspeed_lpc_snoop_channel *chan = snoop_file_to_chan(file);
  76. unsigned int copied;
  77. int ret = 0;
  78. if (kfifo_is_empty(&chan->fifo)) {
  79. if (file->f_flags & O_NONBLOCK)
  80. return -EAGAIN;
  81. ret = wait_event_interruptible(chan->wq,
  82. !kfifo_is_empty(&chan->fifo));
  83. if (ret == -ERESTARTSYS)
  84. return -EINTR;
  85. }
  86. ret = kfifo_to_user(&chan->fifo, buffer, count, &copied);
  87. if (ret)
  88. return ret;
  89. return 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. lpc_snoop->clk = devm_clk_get(dev, NULL);
  246. if (IS_ERR(lpc_snoop->clk)) {
  247. rc = PTR_ERR(lpc_snoop->clk);
  248. if (rc != -EPROBE_DEFER)
  249. dev_err(dev, "couldn't get clock\n");
  250. return rc;
  251. }
  252. rc = clk_prepare_enable(lpc_snoop->clk);
  253. if (rc) {
  254. dev_err(dev, "couldn't enable clock\n");
  255. return rc;
  256. }
  257. rc = aspeed_lpc_snoop_config_irq(lpc_snoop, pdev);
  258. if (rc)
  259. goto err;
  260. rc = aspeed_lpc_enable_snoop(lpc_snoop, dev, 0, port);
  261. if (rc)
  262. goto err;
  263. /* Configuration of 2nd snoop channel port is optional */
  264. if (of_property_read_u32_index(dev->of_node, "snoop-ports",
  265. 1, &port) == 0) {
  266. rc = aspeed_lpc_enable_snoop(lpc_snoop, dev, 1, port);
  267. if (rc) {
  268. aspeed_lpc_disable_snoop(lpc_snoop, 0);
  269. goto err;
  270. }
  271. }
  272. return 0;
  273. err:
  274. clk_disable_unprepare(lpc_snoop->clk);
  275. return rc;
  276. }
  277. static int aspeed_lpc_snoop_remove(struct platform_device *pdev)
  278. {
  279. struct aspeed_lpc_snoop *lpc_snoop = dev_get_drvdata(&pdev->dev);
  280. /* Disable both snoop channels */
  281. aspeed_lpc_disable_snoop(lpc_snoop, 0);
  282. aspeed_lpc_disable_snoop(lpc_snoop, 1);
  283. clk_disable_unprepare(lpc_snoop->clk);
  284. return 0;
  285. }
  286. static const struct aspeed_lpc_snoop_model_data ast2400_model_data = {
  287. .has_hicrb_ensnp = 0,
  288. };
  289. static const struct aspeed_lpc_snoop_model_data ast2500_model_data = {
  290. .has_hicrb_ensnp = 1,
  291. };
  292. static const struct of_device_id aspeed_lpc_snoop_match[] = {
  293. { .compatible = "aspeed,ast2400-lpc-snoop",
  294. .data = &ast2400_model_data },
  295. { .compatible = "aspeed,ast2500-lpc-snoop",
  296. .data = &ast2500_model_data },
  297. { },
  298. };
  299. static struct platform_driver aspeed_lpc_snoop_driver = {
  300. .driver = {
  301. .name = DEVICE_NAME,
  302. .of_match_table = aspeed_lpc_snoop_match,
  303. },
  304. .probe = aspeed_lpc_snoop_probe,
  305. .remove = aspeed_lpc_snoop_remove,
  306. };
  307. module_platform_driver(aspeed_lpc_snoop_driver);
  308. MODULE_DEVICE_TABLE(of, aspeed_lpc_snoop_match);
  309. MODULE_LICENSE("GPL");
  310. MODULE_AUTHOR("Robert Lippert <rlippert@google.com>");
  311. MODULE_DESCRIPTION("Linux driver to control Aspeed LPC snoop functionality");