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