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