aspeed-lpc-snoop.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  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/platform_device.h>
  22. #include <linux/poll.h>
  23. #include <linux/regmap.h>
  24. #define DEVICE_NAME "aspeed-lpc-snoop"
  25. #define NUM_SNOOP_CHANNELS 2
  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. struct aspeed_lpc_snoop_channel {
  55. struct kfifo fifo;
  56. wait_queue_head_t wq;
  57. struct miscdevice miscdev;
  58. };
  59. struct aspeed_lpc_snoop {
  60. struct regmap *regmap;
  61. int irq;
  62. struct clk *clk;
  63. struct aspeed_lpc_snoop_channel chan[NUM_SNOOP_CHANNELS];
  64. };
  65. static struct aspeed_lpc_snoop_channel *snoop_file_to_chan(struct file *file)
  66. {
  67. return container_of(file->private_data,
  68. struct aspeed_lpc_snoop_channel,
  69. miscdev);
  70. }
  71. static ssize_t snoop_file_read(struct file *file, char __user *buffer,
  72. size_t count, loff_t *ppos)
  73. {
  74. struct aspeed_lpc_snoop_channel *chan = snoop_file_to_chan(file);
  75. unsigned int copied;
  76. int ret = 0;
  77. if (kfifo_is_empty(&chan->fifo)) {
  78. if (file->f_flags & O_NONBLOCK)
  79. return -EAGAIN;
  80. ret = wait_event_interruptible(chan->wq,
  81. !kfifo_is_empty(&chan->fifo));
  82. if (ret == -ERESTARTSYS)
  83. return -EINTR;
  84. }
  85. ret = kfifo_to_user(&chan->fifo, buffer, count, &copied);
  86. if (ret)
  87. return ret;
  88. return copied;
  89. }
  90. static __poll_t snoop_file_poll(struct file *file,
  91. struct poll_table_struct *pt)
  92. {
  93. struct aspeed_lpc_snoop_channel *chan = snoop_file_to_chan(file);
  94. poll_wait(file, &chan->wq, pt);
  95. return !kfifo_is_empty(&chan->fifo) ? EPOLLIN : 0;
  96. }
  97. static const struct file_operations snoop_fops = {
  98. .owner = THIS_MODULE,
  99. .read = snoop_file_read,
  100. .poll = snoop_file_poll,
  101. .llseek = noop_llseek,
  102. };
  103. /* Save a byte to a FIFO and discard the oldest byte if FIFO is full */
  104. static void put_fifo_with_discard(struct aspeed_lpc_snoop_channel *chan, u8 val)
  105. {
  106. if (!kfifo_initialized(&chan->fifo))
  107. return;
  108. if (kfifo_is_full(&chan->fifo))
  109. kfifo_skip(&chan->fifo);
  110. kfifo_put(&chan->fifo, val);
  111. wake_up_interruptible(&chan->wq);
  112. }
  113. static irqreturn_t aspeed_lpc_snoop_irq(int irq, void *arg)
  114. {
  115. struct aspeed_lpc_snoop *lpc_snoop = arg;
  116. u32 reg, data;
  117. if (regmap_read(lpc_snoop->regmap, HICR6, &reg))
  118. return IRQ_NONE;
  119. /* Check if one of the snoop channels is interrupting */
  120. reg &= (HICR6_STR_SNP0W | HICR6_STR_SNP1W);
  121. if (!reg)
  122. return IRQ_NONE;
  123. /* Ack pending IRQs */
  124. regmap_write(lpc_snoop->regmap, HICR6, reg);
  125. /* Read and save most recent snoop'ed data byte to FIFO */
  126. regmap_read(lpc_snoop->regmap, SNPWDR, &data);
  127. if (reg & HICR6_STR_SNP0W) {
  128. u8 val = (data & SNPWDR_CH0_MASK) >> SNPWDR_CH0_SHIFT;
  129. put_fifo_with_discard(&lpc_snoop->chan[0], val);
  130. }
  131. if (reg & HICR6_STR_SNP1W) {
  132. u8 val = (data & SNPWDR_CH1_MASK) >> SNPWDR_CH1_SHIFT;
  133. put_fifo_with_discard(&lpc_snoop->chan[1], val);
  134. }
  135. return IRQ_HANDLED;
  136. }
  137. static int aspeed_lpc_snoop_config_irq(struct aspeed_lpc_snoop *lpc_snoop,
  138. struct platform_device *pdev)
  139. {
  140. struct device *dev = &pdev->dev;
  141. int rc;
  142. lpc_snoop->irq = platform_get_irq(pdev, 0);
  143. if (!lpc_snoop->irq)
  144. return -ENODEV;
  145. rc = devm_request_irq(dev, lpc_snoop->irq,
  146. aspeed_lpc_snoop_irq, IRQF_SHARED,
  147. DEVICE_NAME, lpc_snoop);
  148. if (rc < 0) {
  149. dev_warn(dev, "Unable to request IRQ %d\n", lpc_snoop->irq);
  150. lpc_snoop->irq = 0;
  151. return rc;
  152. }
  153. return 0;
  154. }
  155. static int aspeed_lpc_enable_snoop(struct aspeed_lpc_snoop *lpc_snoop,
  156. struct device *dev,
  157. int channel, u16 lpc_port)
  158. {
  159. int rc = 0;
  160. u32 hicr5_en, snpwadr_mask, snpwadr_shift, hicrb_en;
  161. const struct aspeed_lpc_snoop_model_data *model_data =
  162. of_device_get_match_data(dev);
  163. init_waitqueue_head(&lpc_snoop->chan[channel].wq);
  164. /* Create FIFO datastructure */
  165. rc = kfifo_alloc(&lpc_snoop->chan[channel].fifo,
  166. SNOOP_FIFO_SIZE, GFP_KERNEL);
  167. if (rc)
  168. return rc;
  169. lpc_snoop->chan[channel].miscdev.minor = MISC_DYNAMIC_MINOR;
  170. lpc_snoop->chan[channel].miscdev.name =
  171. devm_kasprintf(dev, GFP_KERNEL, "%s%d", DEVICE_NAME, channel);
  172. lpc_snoop->chan[channel].miscdev.fops = &snoop_fops;
  173. lpc_snoop->chan[channel].miscdev.parent = dev;
  174. rc = misc_register(&lpc_snoop->chan[channel].miscdev);
  175. if (rc)
  176. return rc;
  177. /* Enable LPC snoop channel at requested port */
  178. switch (channel) {
  179. case 0:
  180. hicr5_en = HICR5_EN_SNP0W | HICR5_ENINT_SNP0W;
  181. snpwadr_mask = SNPWADR_CH0_MASK;
  182. snpwadr_shift = SNPWADR_CH0_SHIFT;
  183. hicrb_en = HICRB_ENSNP0D;
  184. break;
  185. case 1:
  186. hicr5_en = HICR5_EN_SNP1W | HICR5_ENINT_SNP1W;
  187. snpwadr_mask = SNPWADR_CH1_MASK;
  188. snpwadr_shift = SNPWADR_CH1_SHIFT;
  189. hicrb_en = HICRB_ENSNP1D;
  190. break;
  191. default:
  192. return -EINVAL;
  193. }
  194. regmap_update_bits(lpc_snoop->regmap, HICR5, hicr5_en, hicr5_en);
  195. regmap_update_bits(lpc_snoop->regmap, SNPWADR, snpwadr_mask,
  196. lpc_port << snpwadr_shift);
  197. if (model_data->has_hicrb_ensnp)
  198. regmap_update_bits(lpc_snoop->regmap, HICRB,
  199. hicrb_en, hicrb_en);
  200. return rc;
  201. }
  202. static void aspeed_lpc_disable_snoop(struct aspeed_lpc_snoop *lpc_snoop,
  203. int channel)
  204. {
  205. switch (channel) {
  206. case 0:
  207. regmap_update_bits(lpc_snoop->regmap, HICR5,
  208. HICR5_EN_SNP0W | HICR5_ENINT_SNP0W,
  209. 0);
  210. break;
  211. case 1:
  212. regmap_update_bits(lpc_snoop->regmap, HICR5,
  213. HICR5_EN_SNP1W | HICR5_ENINT_SNP1W,
  214. 0);
  215. break;
  216. default:
  217. return;
  218. }
  219. kfifo_free(&lpc_snoop->chan[channel].fifo);
  220. misc_deregister(&lpc_snoop->chan[channel].miscdev);
  221. }
  222. static int aspeed_lpc_snoop_probe(struct platform_device *pdev)
  223. {
  224. struct aspeed_lpc_snoop *lpc_snoop;
  225. struct device *dev;
  226. struct device_node *np;
  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. np = pdev->dev.parent->of_node;
  234. if (!of_device_is_compatible(np, "aspeed,ast2400-lpc-v2") &&
  235. !of_device_is_compatible(np, "aspeed,ast2500-lpc-v2") &&
  236. !of_device_is_compatible(np, "aspeed,ast2600-lpc-v2")) {
  237. dev_err(dev, "unsupported LPC device binding\n");
  238. return -ENODEV;
  239. }
  240. lpc_snoop->regmap = syscon_node_to_regmap(np);
  241. if (IS_ERR(lpc_snoop->regmap)) {
  242. dev_err(dev, "Couldn't get regmap\n");
  243. return -ENODEV;
  244. }
  245. dev_set_drvdata(&pdev->dev, lpc_snoop);
  246. rc = of_property_read_u32_index(dev->of_node, "snoop-ports", 0, &port);
  247. if (rc) {
  248. dev_err(dev, "no snoop ports configured\n");
  249. return -ENODEV;
  250. }
  251. lpc_snoop->clk = devm_clk_get(dev, NULL);
  252. if (IS_ERR(lpc_snoop->clk)) {
  253. rc = PTR_ERR(lpc_snoop->clk);
  254. if (rc != -EPROBE_DEFER)
  255. dev_err(dev, "couldn't get clock\n");
  256. return rc;
  257. }
  258. rc = clk_prepare_enable(lpc_snoop->clk);
  259. if (rc) {
  260. dev_err(dev, "couldn't enable clock\n");
  261. return rc;
  262. }
  263. rc = aspeed_lpc_snoop_config_irq(lpc_snoop, pdev);
  264. if (rc)
  265. goto err;
  266. rc = aspeed_lpc_enable_snoop(lpc_snoop, dev, 0, port);
  267. if (rc)
  268. goto err;
  269. /* Configuration of 2nd snoop channel port is optional */
  270. if (of_property_read_u32_index(dev->of_node, "snoop-ports",
  271. 1, &port) == 0) {
  272. rc = aspeed_lpc_enable_snoop(lpc_snoop, dev, 1, port);
  273. if (rc) {
  274. aspeed_lpc_disable_snoop(lpc_snoop, 0);
  275. goto err;
  276. }
  277. }
  278. return 0;
  279. err:
  280. clk_disable_unprepare(lpc_snoop->clk);
  281. return rc;
  282. }
  283. static void aspeed_lpc_snoop_remove(struct platform_device *pdev)
  284. {
  285. struct aspeed_lpc_snoop *lpc_snoop = dev_get_drvdata(&pdev->dev);
  286. /* Disable both snoop channels */
  287. aspeed_lpc_disable_snoop(lpc_snoop, 0);
  288. aspeed_lpc_disable_snoop(lpc_snoop, 1);
  289. clk_disable_unprepare(lpc_snoop->clk);
  290. }
  291. static const struct aspeed_lpc_snoop_model_data ast2400_model_data = {
  292. .has_hicrb_ensnp = 0,
  293. };
  294. static const struct aspeed_lpc_snoop_model_data ast2500_model_data = {
  295. .has_hicrb_ensnp = 1,
  296. };
  297. static const struct of_device_id aspeed_lpc_snoop_match[] = {
  298. { .compatible = "aspeed,ast2400-lpc-snoop",
  299. .data = &ast2400_model_data },
  300. { .compatible = "aspeed,ast2500-lpc-snoop",
  301. .data = &ast2500_model_data },
  302. { .compatible = "aspeed,ast2600-lpc-snoop",
  303. .data = &ast2500_model_data },
  304. { },
  305. };
  306. static struct platform_driver aspeed_lpc_snoop_driver = {
  307. .driver = {
  308. .name = DEVICE_NAME,
  309. .of_match_table = aspeed_lpc_snoop_match,
  310. },
  311. .probe = aspeed_lpc_snoop_probe,
  312. .remove_new = aspeed_lpc_snoop_remove,
  313. };
  314. module_platform_driver(aspeed_lpc_snoop_driver);
  315. MODULE_DEVICE_TABLE(of, aspeed_lpc_snoop_match);
  316. MODULE_LICENSE("GPL");
  317. MODULE_AUTHOR("Robert Lippert <rlippert@google.com>");
  318. MODULE_DESCRIPTION("Linux driver to control Aspeed LPC snoop functionality");