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