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