aspeed-lpc-snoop.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  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/dev_printk.h>
  15. #include <linux/interrupt.h>
  16. #include <linux/fs.h>
  17. #include <linux/kfifo.h>
  18. #include <linux/mfd/syscon.h>
  19. #include <linux/miscdevice.h>
  20. #include <linux/module.h>
  21. #include <linux/of.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 SNOOP_FIFO_SIZE 2048
  27. #define HICR5 0x80
  28. #define HICR5_EN_SNP0W BIT(0)
  29. #define HICR5_ENINT_SNP0W BIT(1)
  30. #define HICR5_EN_SNP1W BIT(2)
  31. #define HICR5_ENINT_SNP1W BIT(3)
  32. #define HICR6 0x84
  33. #define HICR6_STR_SNP0W BIT(0)
  34. #define HICR6_STR_SNP1W BIT(1)
  35. #define SNPWADR 0x90
  36. #define SNPWADR_CH0_MASK GENMASK(15, 0)
  37. #define SNPWADR_CH0_SHIFT 0
  38. #define SNPWADR_CH1_MASK GENMASK(31, 16)
  39. #define SNPWADR_CH1_SHIFT 16
  40. #define SNPWDR 0x94
  41. #define SNPWDR_CH0_MASK GENMASK(7, 0)
  42. #define SNPWDR_CH0_SHIFT 0
  43. #define SNPWDR_CH1_MASK GENMASK(15, 8)
  44. #define SNPWDR_CH1_SHIFT 8
  45. #define HICRB 0x100
  46. #define HICRB_ENSNP0D BIT(14)
  47. #define HICRB_ENSNP1D BIT(15)
  48. struct aspeed_lpc_snoop_model_data {
  49. /* The ast2400 has bits 14 and 15 as reserved, whereas the ast2500
  50. * can use them.
  51. */
  52. unsigned int has_hicrb_ensnp;
  53. };
  54. enum aspeed_lpc_snoop_index {
  55. ASPEED_LPC_SNOOP_INDEX_0 = 0,
  56. ASPEED_LPC_SNOOP_INDEX_1 = 1,
  57. ASPEED_LPC_SNOOP_INDEX_MAX = ASPEED_LPC_SNOOP_INDEX_1,
  58. };
  59. struct aspeed_lpc_snoop_channel_cfg {
  60. enum aspeed_lpc_snoop_index index;
  61. u32 hicr5_en;
  62. u32 snpwadr_mask;
  63. u32 snpwadr_shift;
  64. u32 hicrb_en;
  65. };
  66. struct aspeed_lpc_snoop_channel {
  67. const struct aspeed_lpc_snoop_channel_cfg *cfg;
  68. bool enabled;
  69. struct kfifo fifo;
  70. wait_queue_head_t wq;
  71. struct miscdevice miscdev;
  72. };
  73. struct aspeed_lpc_snoop {
  74. struct regmap *regmap;
  75. int irq;
  76. struct clk *clk;
  77. struct aspeed_lpc_snoop_channel chan[ASPEED_LPC_SNOOP_INDEX_MAX + 1];
  78. };
  79. static const struct aspeed_lpc_snoop_channel_cfg channel_cfgs[ASPEED_LPC_SNOOP_INDEX_MAX + 1] = {
  80. {
  81. .index = ASPEED_LPC_SNOOP_INDEX_0,
  82. .hicr5_en = HICR5_EN_SNP0W | HICR5_ENINT_SNP0W,
  83. .snpwadr_mask = SNPWADR_CH0_MASK,
  84. .snpwadr_shift = SNPWADR_CH0_SHIFT,
  85. .hicrb_en = HICRB_ENSNP0D,
  86. },
  87. {
  88. .index = ASPEED_LPC_SNOOP_INDEX_1,
  89. .hicr5_en = HICR5_EN_SNP1W | HICR5_ENINT_SNP1W,
  90. .snpwadr_mask = SNPWADR_CH1_MASK,
  91. .snpwadr_shift = SNPWADR_CH1_SHIFT,
  92. .hicrb_en = HICRB_ENSNP1D,
  93. },
  94. };
  95. static struct aspeed_lpc_snoop_channel *snoop_file_to_chan(struct file *file)
  96. {
  97. return container_of(file->private_data,
  98. struct aspeed_lpc_snoop_channel,
  99. miscdev);
  100. }
  101. static ssize_t snoop_file_read(struct file *file, char __user *buffer,
  102. size_t count, loff_t *ppos)
  103. {
  104. struct aspeed_lpc_snoop_channel *chan = snoop_file_to_chan(file);
  105. unsigned int copied;
  106. int ret = 0;
  107. if (kfifo_is_empty(&chan->fifo)) {
  108. if (file->f_flags & O_NONBLOCK)
  109. return -EAGAIN;
  110. ret = wait_event_interruptible(chan->wq,
  111. !kfifo_is_empty(&chan->fifo));
  112. if (ret == -ERESTARTSYS)
  113. return -EINTR;
  114. }
  115. ret = kfifo_to_user(&chan->fifo, buffer, count, &copied);
  116. if (ret)
  117. return ret;
  118. return copied;
  119. }
  120. static __poll_t snoop_file_poll(struct file *file,
  121. struct poll_table_struct *pt)
  122. {
  123. struct aspeed_lpc_snoop_channel *chan = snoop_file_to_chan(file);
  124. poll_wait(file, &chan->wq, pt);
  125. return !kfifo_is_empty(&chan->fifo) ? EPOLLIN : 0;
  126. }
  127. static const struct file_operations snoop_fops = {
  128. .owner = THIS_MODULE,
  129. .read = snoop_file_read,
  130. .poll = snoop_file_poll,
  131. .llseek = noop_llseek,
  132. };
  133. /* Save a byte to a FIFO and discard the oldest byte if FIFO is full */
  134. static void put_fifo_with_discard(struct aspeed_lpc_snoop_channel *chan, u8 val)
  135. {
  136. if (!kfifo_initialized(&chan->fifo))
  137. return;
  138. if (kfifo_is_full(&chan->fifo))
  139. kfifo_skip(&chan->fifo);
  140. kfifo_put(&chan->fifo, val);
  141. wake_up_interruptible(&chan->wq);
  142. }
  143. static irqreturn_t aspeed_lpc_snoop_irq(int irq, void *arg)
  144. {
  145. struct aspeed_lpc_snoop *lpc_snoop = arg;
  146. u32 reg, data;
  147. if (regmap_read(lpc_snoop->regmap, HICR6, &reg))
  148. return IRQ_NONE;
  149. /* Check if one of the snoop channels is interrupting */
  150. reg &= (HICR6_STR_SNP0W | HICR6_STR_SNP1W);
  151. if (!reg)
  152. return IRQ_NONE;
  153. /* Ack pending IRQs */
  154. regmap_write(lpc_snoop->regmap, HICR6, reg);
  155. /* Read and save most recent snoop'ed data byte to FIFO */
  156. regmap_read(lpc_snoop->regmap, SNPWDR, &data);
  157. if (reg & HICR6_STR_SNP0W) {
  158. u8 val = (data & SNPWDR_CH0_MASK) >> SNPWDR_CH0_SHIFT;
  159. put_fifo_with_discard(&lpc_snoop->chan[0], val);
  160. }
  161. if (reg & HICR6_STR_SNP1W) {
  162. u8 val = (data & SNPWDR_CH1_MASK) >> SNPWDR_CH1_SHIFT;
  163. put_fifo_with_discard(&lpc_snoop->chan[1], val);
  164. }
  165. return IRQ_HANDLED;
  166. }
  167. static int aspeed_lpc_snoop_config_irq(struct aspeed_lpc_snoop *lpc_snoop,
  168. struct platform_device *pdev)
  169. {
  170. struct device *dev = &pdev->dev;
  171. int rc;
  172. lpc_snoop->irq = platform_get_irq(pdev, 0);
  173. if (lpc_snoop->irq < 0)
  174. return -ENODEV;
  175. rc = devm_request_irq(dev, lpc_snoop->irq,
  176. aspeed_lpc_snoop_irq, IRQF_SHARED,
  177. DEVICE_NAME, lpc_snoop);
  178. if (rc < 0) {
  179. dev_warn(dev, "Unable to request IRQ %d\n", lpc_snoop->irq);
  180. lpc_snoop->irq = 0;
  181. return rc;
  182. }
  183. return 0;
  184. }
  185. __attribute__((nonnull))
  186. static int aspeed_lpc_enable_snoop(struct device *dev,
  187. struct aspeed_lpc_snoop *lpc_snoop,
  188. struct aspeed_lpc_snoop_channel *channel,
  189. const struct aspeed_lpc_snoop_channel_cfg *cfg,
  190. u16 lpc_port)
  191. {
  192. const struct aspeed_lpc_snoop_model_data *model_data;
  193. int rc = 0;
  194. if (WARN_ON(channel->enabled))
  195. return -EBUSY;
  196. init_waitqueue_head(&channel->wq);
  197. channel->cfg = cfg;
  198. channel->miscdev.minor = MISC_DYNAMIC_MINOR;
  199. channel->miscdev.fops = &snoop_fops;
  200. channel->miscdev.parent = dev;
  201. channel->miscdev.name =
  202. devm_kasprintf(dev, GFP_KERNEL, "%s%d", DEVICE_NAME, cfg->index);
  203. if (!channel->miscdev.name)
  204. return -ENOMEM;
  205. rc = kfifo_alloc(&channel->fifo, SNOOP_FIFO_SIZE, GFP_KERNEL);
  206. if (rc)
  207. return rc;
  208. rc = misc_register(&channel->miscdev);
  209. if (rc)
  210. goto err_free_fifo;
  211. /* Enable LPC snoop channel at requested port */
  212. regmap_set_bits(lpc_snoop->regmap, HICR5, cfg->hicr5_en);
  213. regmap_update_bits(lpc_snoop->regmap, SNPWADR, cfg->snpwadr_mask,
  214. lpc_port << cfg->snpwadr_shift);
  215. model_data = of_device_get_match_data(dev);
  216. if (model_data && model_data->has_hicrb_ensnp)
  217. regmap_set_bits(lpc_snoop->regmap, HICRB, cfg->hicrb_en);
  218. channel->enabled = true;
  219. return 0;
  220. err_free_fifo:
  221. kfifo_free(&channel->fifo);
  222. return rc;
  223. }
  224. __attribute__((nonnull))
  225. static void aspeed_lpc_disable_snoop(struct aspeed_lpc_snoop *lpc_snoop,
  226. struct aspeed_lpc_snoop_channel *channel)
  227. {
  228. if (!channel->enabled)
  229. return;
  230. /* Disable interrupts along with the device */
  231. regmap_clear_bits(lpc_snoop->regmap, HICR5, channel->cfg->hicr5_en);
  232. channel->enabled = false;
  233. /* Consider improving safety wrt concurrent reader(s) */
  234. misc_deregister(&channel->miscdev);
  235. kfifo_free(&channel->fifo);
  236. }
  237. static void aspeed_lpc_snoop_remove(struct platform_device *pdev)
  238. {
  239. struct aspeed_lpc_snoop *lpc_snoop = dev_get_drvdata(&pdev->dev);
  240. /* Disable both snoop channels */
  241. aspeed_lpc_disable_snoop(lpc_snoop, &lpc_snoop->chan[0]);
  242. aspeed_lpc_disable_snoop(lpc_snoop, &lpc_snoop->chan[1]);
  243. }
  244. static int aspeed_lpc_snoop_probe(struct platform_device *pdev)
  245. {
  246. struct aspeed_lpc_snoop *lpc_snoop;
  247. struct device_node *np;
  248. struct device *dev;
  249. int idx;
  250. int rc;
  251. dev = &pdev->dev;
  252. lpc_snoop = devm_kzalloc(dev, sizeof(*lpc_snoop), GFP_KERNEL);
  253. if (!lpc_snoop)
  254. return -ENOMEM;
  255. np = pdev->dev.parent->of_node;
  256. if (!of_device_is_compatible(np, "aspeed,ast2400-lpc-v2") &&
  257. !of_device_is_compatible(np, "aspeed,ast2500-lpc-v2") &&
  258. !of_device_is_compatible(np, "aspeed,ast2600-lpc-v2")) {
  259. dev_err(dev, "unsupported LPC device binding\n");
  260. return -ENODEV;
  261. }
  262. lpc_snoop->regmap = syscon_node_to_regmap(np);
  263. if (IS_ERR(lpc_snoop->regmap))
  264. return dev_err_probe(dev, PTR_ERR(lpc_snoop->regmap), "Couldn't get regmap\n");
  265. dev_set_drvdata(&pdev->dev, lpc_snoop);
  266. lpc_snoop->clk = devm_clk_get_enabled(dev, NULL);
  267. if (IS_ERR(lpc_snoop->clk))
  268. return dev_err_probe(dev, PTR_ERR(lpc_snoop->clk), "couldn't get clock");
  269. rc = aspeed_lpc_snoop_config_irq(lpc_snoop, pdev);
  270. if (rc)
  271. return rc;
  272. static_assert(ARRAY_SIZE(channel_cfgs) == ARRAY_SIZE(lpc_snoop->chan),
  273. "Broken implementation assumption regarding cfg count");
  274. for (idx = ASPEED_LPC_SNOOP_INDEX_0; idx <= ASPEED_LPC_SNOOP_INDEX_MAX; idx++) {
  275. u32 port;
  276. rc = of_property_read_u32_index(dev->of_node, "snoop-ports", idx, &port);
  277. if (rc)
  278. break;
  279. rc = aspeed_lpc_enable_snoop(dev, lpc_snoop, &lpc_snoop->chan[idx],
  280. &channel_cfgs[idx], port);
  281. if (rc)
  282. goto cleanup_channels;
  283. }
  284. return idx == ASPEED_LPC_SNOOP_INDEX_0 ? -ENODEV : 0;
  285. cleanup_channels:
  286. aspeed_lpc_snoop_remove(pdev);
  287. return rc;
  288. }
  289. static const struct aspeed_lpc_snoop_model_data ast2400_model_data = {
  290. .has_hicrb_ensnp = 0,
  291. };
  292. static const struct aspeed_lpc_snoop_model_data ast2500_model_data = {
  293. .has_hicrb_ensnp = 1,
  294. };
  295. static const struct of_device_id aspeed_lpc_snoop_match[] = {
  296. { .compatible = "aspeed,ast2400-lpc-snoop",
  297. .data = &ast2400_model_data },
  298. { .compatible = "aspeed,ast2500-lpc-snoop",
  299. .data = &ast2500_model_data },
  300. { .compatible = "aspeed,ast2600-lpc-snoop",
  301. .data = &ast2500_model_data },
  302. { },
  303. };
  304. static struct platform_driver aspeed_lpc_snoop_driver = {
  305. .driver = {
  306. .name = DEVICE_NAME,
  307. .of_match_table = aspeed_lpc_snoop_match,
  308. },
  309. .probe = aspeed_lpc_snoop_probe,
  310. .remove = aspeed_lpc_snoop_remove,
  311. };
  312. module_platform_driver(aspeed_lpc_snoop_driver);
  313. MODULE_DEVICE_TABLE(of, aspeed_lpc_snoop_match);
  314. MODULE_LICENSE("GPL");
  315. MODULE_AUTHOR("Robert Lippert <rlippert@google.com>");
  316. MODULE_DESCRIPTION("Linux driver to control Aspeed LPC snoop functionality");