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