Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

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