2 * Copyright 2017 Google Inc
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.
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.
13 * Typically used by the BMC to "watch" host boot progress via port
14 * 0x80 writes made by the BIOS during the boot process.
17 #include <linux/bitops.h>
18 #include <linux/clk.h>
19 #include <linux/interrupt.h>
21 #include <linux/kfifo.h>
22 #include <linux/mfd/syscon.h>
23 #include <linux/miscdevice.h>
24 #include <linux/module.h>
26 #include <linux/of_device.h>
27 #include <linux/platform_device.h>
28 #include <linux/poll.h>
29 #include <linux/regmap.h>
31 #define DEVICE_NAME "aspeed-lpc-snoop"
33 #define NUM_SNOOP_CHANNELS 2
34 #define SNOOP_FIFO_SIZE 2048
37 #define HICR5_EN_SNP0W BIT(0)
38 #define HICR5_ENINT_SNP0W BIT(1)
39 #define HICR5_EN_SNP1W BIT(2)
40 #define HICR5_ENINT_SNP1W BIT(3)
43 #define HICR6_STR_SNP0W BIT(0)
44 #define HICR6_STR_SNP1W BIT(1)
46 #define SNPWADR_CH0_MASK GENMASK(15, 0)
47 #define SNPWADR_CH0_SHIFT 0
48 #define SNPWADR_CH1_MASK GENMASK(31, 16)
49 #define SNPWADR_CH1_SHIFT 16
51 #define SNPWDR_CH0_MASK GENMASK(7, 0)
52 #define SNPWDR_CH0_SHIFT 0
53 #define SNPWDR_CH1_MASK GENMASK(15, 8)
54 #define SNPWDR_CH1_SHIFT 8
56 #define HICRB_ENSNP0D BIT(14)
57 #define HICRB_ENSNP1D BIT(15)
59 struct aspeed_lpc_snoop_model_data {
60 /* The ast2400 has bits 14 and 15 as reserved, whereas the ast2500
63 unsigned int has_hicrb_ensnp;
66 struct aspeed_lpc_snoop_channel {
69 struct miscdevice miscdev;
72 struct aspeed_lpc_snoop {
73 struct regmap *regmap;
76 struct aspeed_lpc_snoop_channel chan[NUM_SNOOP_CHANNELS];
79 static struct aspeed_lpc_snoop_channel *snoop_file_to_chan(struct file *file)
81 return container_of(file->private_data,
82 struct aspeed_lpc_snoop_channel,
86 static ssize_t snoop_file_read(struct file *file, char __user *buffer,
87 size_t count, loff_t *ppos)
89 struct aspeed_lpc_snoop_channel *chan = snoop_file_to_chan(file);
93 if (kfifo_is_empty(&chan->fifo)) {
94 if (file->f_flags & O_NONBLOCK)
96 ret = wait_event_interruptible(chan->wq,
97 !kfifo_is_empty(&chan->fifo));
98 if (ret == -ERESTARTSYS)
101 ret = kfifo_to_user(&chan->fifo, buffer, count, &copied);
108 static __poll_t snoop_file_poll(struct file *file,
109 struct poll_table_struct *pt)
111 struct aspeed_lpc_snoop_channel *chan = snoop_file_to_chan(file);
113 poll_wait(file, &chan->wq, pt);
114 return !kfifo_is_empty(&chan->fifo) ? EPOLLIN : 0;
117 static const struct file_operations snoop_fops = {
118 .owner = THIS_MODULE,
119 .read = snoop_file_read,
120 .poll = snoop_file_poll,
121 .llseek = noop_llseek,
124 /* Save a byte to a FIFO and discard the oldest byte if FIFO is full */
125 static void put_fifo_with_discard(struct aspeed_lpc_snoop_channel *chan, u8 val)
127 if (!kfifo_initialized(&chan->fifo))
129 if (kfifo_is_full(&chan->fifo))
130 kfifo_skip(&chan->fifo);
131 kfifo_put(&chan->fifo, val);
132 wake_up_interruptible(&chan->wq);
135 static irqreturn_t aspeed_lpc_snoop_irq(int irq, void *arg)
137 struct aspeed_lpc_snoop *lpc_snoop = arg;
140 if (regmap_read(lpc_snoop->regmap, HICR6, ®))
143 /* Check if one of the snoop channels is interrupting */
144 reg &= (HICR6_STR_SNP0W | HICR6_STR_SNP1W);
148 /* Ack pending IRQs */
149 regmap_write(lpc_snoop->regmap, HICR6, reg);
151 /* Read and save most recent snoop'ed data byte to FIFO */
152 regmap_read(lpc_snoop->regmap, SNPWDR, &data);
154 if (reg & HICR6_STR_SNP0W) {
155 u8 val = (data & SNPWDR_CH0_MASK) >> SNPWDR_CH0_SHIFT;
157 put_fifo_with_discard(&lpc_snoop->chan[0], val);
159 if (reg & HICR6_STR_SNP1W) {
160 u8 val = (data & SNPWDR_CH1_MASK) >> SNPWDR_CH1_SHIFT;
162 put_fifo_with_discard(&lpc_snoop->chan[1], val);
168 static int aspeed_lpc_snoop_config_irq(struct aspeed_lpc_snoop *lpc_snoop,
169 struct platform_device *pdev)
171 struct device *dev = &pdev->dev;
174 lpc_snoop->irq = platform_get_irq(pdev, 0);
178 rc = devm_request_irq(dev, lpc_snoop->irq,
179 aspeed_lpc_snoop_irq, IRQF_SHARED,
180 DEVICE_NAME, lpc_snoop);
182 dev_warn(dev, "Unable to request IRQ %d\n", lpc_snoop->irq);
190 static int aspeed_lpc_enable_snoop(struct aspeed_lpc_snoop *lpc_snoop,
192 int channel, u16 lpc_port)
195 u32 hicr5_en, snpwadr_mask, snpwadr_shift, hicrb_en;
196 const struct aspeed_lpc_snoop_model_data *model_data =
197 of_device_get_match_data(dev);
199 init_waitqueue_head(&lpc_snoop->chan[channel].wq);
200 /* Create FIFO datastructure */
201 rc = kfifo_alloc(&lpc_snoop->chan[channel].fifo,
202 SNOOP_FIFO_SIZE, GFP_KERNEL);
206 lpc_snoop->chan[channel].miscdev.minor = MISC_DYNAMIC_MINOR;
207 lpc_snoop->chan[channel].miscdev.name =
208 devm_kasprintf(dev, GFP_KERNEL, "%s%d", DEVICE_NAME, channel);
209 lpc_snoop->chan[channel].miscdev.fops = &snoop_fops;
210 lpc_snoop->chan[channel].miscdev.parent = dev;
211 rc = misc_register(&lpc_snoop->chan[channel].miscdev);
215 /* Enable LPC snoop channel at requested port */
218 hicr5_en = HICR5_EN_SNP0W | HICR5_ENINT_SNP0W;
219 snpwadr_mask = SNPWADR_CH0_MASK;
220 snpwadr_shift = SNPWADR_CH0_SHIFT;
221 hicrb_en = HICRB_ENSNP0D;
224 hicr5_en = HICR5_EN_SNP1W | HICR5_ENINT_SNP1W;
225 snpwadr_mask = SNPWADR_CH1_MASK;
226 snpwadr_shift = SNPWADR_CH1_SHIFT;
227 hicrb_en = HICRB_ENSNP1D;
233 regmap_update_bits(lpc_snoop->regmap, HICR5, hicr5_en, hicr5_en);
234 regmap_update_bits(lpc_snoop->regmap, SNPWADR, snpwadr_mask,
235 lpc_port << snpwadr_shift);
236 if (model_data->has_hicrb_ensnp)
237 regmap_update_bits(lpc_snoop->regmap, HICRB,
243 static void aspeed_lpc_disable_snoop(struct aspeed_lpc_snoop *lpc_snoop,
248 regmap_update_bits(lpc_snoop->regmap, HICR5,
249 HICR5_EN_SNP0W | HICR5_ENINT_SNP0W,
253 regmap_update_bits(lpc_snoop->regmap, HICR5,
254 HICR5_EN_SNP1W | HICR5_ENINT_SNP1W,
261 kfifo_free(&lpc_snoop->chan[channel].fifo);
262 misc_deregister(&lpc_snoop->chan[channel].miscdev);
265 static int aspeed_lpc_snoop_probe(struct platform_device *pdev)
267 struct aspeed_lpc_snoop *lpc_snoop;
274 lpc_snoop = devm_kzalloc(dev, sizeof(*lpc_snoop), GFP_KERNEL);
278 lpc_snoop->regmap = syscon_node_to_regmap(
279 pdev->dev.parent->of_node);
280 if (IS_ERR(lpc_snoop->regmap)) {
281 dev_err(dev, "Couldn't get regmap\n");
285 dev_set_drvdata(&pdev->dev, lpc_snoop);
287 rc = of_property_read_u32_index(dev->of_node, "snoop-ports", 0, &port);
289 dev_err(dev, "no snoop ports configured\n");
293 lpc_snoop->clk = devm_clk_get(dev, NULL);
294 if (IS_ERR(lpc_snoop->clk)) {
295 rc = PTR_ERR(lpc_snoop->clk);
296 if (rc != -EPROBE_DEFER)
297 dev_err(dev, "couldn't get clock\n");
300 rc = clk_prepare_enable(lpc_snoop->clk);
302 dev_err(dev, "couldn't enable clock\n");
306 rc = aspeed_lpc_snoop_config_irq(lpc_snoop, pdev);
310 rc = aspeed_lpc_enable_snoop(lpc_snoop, dev, 0, port);
314 /* Configuration of 2nd snoop channel port is optional */
315 if (of_property_read_u32_index(dev->of_node, "snoop-ports",
317 rc = aspeed_lpc_enable_snoop(lpc_snoop, dev, 1, port);
319 aspeed_lpc_disable_snoop(lpc_snoop, 0);
327 clk_disable_unprepare(lpc_snoop->clk);
332 static int aspeed_lpc_snoop_remove(struct platform_device *pdev)
334 struct aspeed_lpc_snoop *lpc_snoop = dev_get_drvdata(&pdev->dev);
336 /* Disable both snoop channels */
337 aspeed_lpc_disable_snoop(lpc_snoop, 0);
338 aspeed_lpc_disable_snoop(lpc_snoop, 1);
340 clk_disable_unprepare(lpc_snoop->clk);
345 static const struct aspeed_lpc_snoop_model_data ast2400_model_data = {
346 .has_hicrb_ensnp = 0,
349 static const struct aspeed_lpc_snoop_model_data ast2500_model_data = {
350 .has_hicrb_ensnp = 1,
353 static const struct of_device_id aspeed_lpc_snoop_match[] = {
354 { .compatible = "aspeed,ast2400-lpc-snoop",
355 .data = &ast2400_model_data },
356 { .compatible = "aspeed,ast2500-lpc-snoop",
357 .data = &ast2500_model_data },
361 static struct platform_driver aspeed_lpc_snoop_driver = {
364 .of_match_table = aspeed_lpc_snoop_match,
366 .probe = aspeed_lpc_snoop_probe,
367 .remove = aspeed_lpc_snoop_remove,
370 module_platform_driver(aspeed_lpc_snoop_driver);
372 MODULE_DEVICE_TABLE(of, aspeed_lpc_snoop_match);
373 MODULE_LICENSE("GPL");
374 MODULE_AUTHOR("Robert Lippert <rlippert@google.com>");
375 MODULE_DESCRIPTION("Linux driver to control Aspeed LPC snoop functionality");