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