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