smu.c

来自「linux 内核源代码」· C语言 代码 · 共 1,301 行 · 第 1/2 页

C
1,301
字号
/* * PowerMac G5 SMU driver * * Copyright 2004 J. Mayer <l_indien@magic.fr> * Copyright 2005 Benjamin Herrenschmidt, IBM Corp. * * Released under the term of the GNU GPL v2. *//* * TODO: *  - maybe add timeout to commands ? *  - blocking version of time functions *  - polling version of i2c commands (including timer that works with *    interrutps off) *  - maybe avoid some data copies with i2c by directly using the smu cmd *    buffer and a lower level internal interface *  - understand SMU -> CPU events and implement reception of them via *    the userland interface */#include <linux/types.h>#include <linux/kernel.h>#include <linux/device.h>#include <linux/dmapool.h>#include <linux/bootmem.h>#include <linux/vmalloc.h>#include <linux/highmem.h>#include <linux/jiffies.h>#include <linux/interrupt.h>#include <linux/rtc.h>#include <linux/completion.h>#include <linux/miscdevice.h>#include <linux/delay.h>#include <linux/sysdev.h>#include <linux/poll.h>#include <linux/mutex.h>#include <asm/byteorder.h>#include <asm/io.h>#include <asm/prom.h>#include <asm/machdep.h>#include <asm/pmac_feature.h>#include <asm/smu.h>#include <asm/sections.h>#include <asm/abs_addr.h>#include <asm/uaccess.h>#include <asm/of_device.h>#include <asm/of_platform.h>#define VERSION "0.7"#define AUTHOR  "(c) 2005 Benjamin Herrenschmidt, IBM Corp."#undef DEBUG_SMU#ifdef DEBUG_SMU#define DPRINTK(fmt, args...) do { printk(KERN_DEBUG fmt , ##args); } while (0)#else#define DPRINTK(fmt, args...) do { } while (0)#endif/* * This is the command buffer passed to the SMU hardware */#define SMU_MAX_DATA	254struct smu_cmd_buf {	u8 cmd;	u8 length;	u8 data[SMU_MAX_DATA];};struct smu_device {	spinlock_t		lock;	struct device_node	*of_node;	struct of_device	*of_dev;	int			doorbell;	/* doorbell gpio */	u32 __iomem		*db_buf;	/* doorbell buffer */	struct device_node	*db_node;	unsigned int		db_irq;	int			msg;	struct device_node	*msg_node;	unsigned int		msg_irq;	struct smu_cmd_buf	*cmd_buf;	/* command buffer virtual */	u32			cmd_buf_abs;	/* command buffer absolute */	struct list_head	cmd_list;	struct smu_cmd		*cmd_cur;	/* pending command */	struct list_head	cmd_i2c_list;	struct smu_i2c_cmd	*cmd_i2c_cur;	/* pending i2c command */	struct timer_list	i2c_timer;};/* * I don't think there will ever be more than one SMU, so * for now, just hard code that */static struct smu_device	*smu;static DEFINE_MUTEX(smu_part_access);static int smu_irq_inited;static void smu_i2c_retry(unsigned long data);/* * SMU driver low level stuff */static void smu_start_cmd(void){	unsigned long faddr, fend;	struct smu_cmd *cmd;	if (list_empty(&smu->cmd_list))		return;	/* Fetch first command in queue */	cmd = list_entry(smu->cmd_list.next, struct smu_cmd, link);	smu->cmd_cur = cmd;	list_del(&cmd->link);	DPRINTK("SMU: starting cmd %x, %d bytes data\n", cmd->cmd,		cmd->data_len);	DPRINTK("SMU: data buffer: %02x %02x %02x %02x %02x %02x %02x %02x\n",		((u8 *)cmd->data_buf)[0], ((u8 *)cmd->data_buf)[1],		((u8 *)cmd->data_buf)[2], ((u8 *)cmd->data_buf)[3],		((u8 *)cmd->data_buf)[4], ((u8 *)cmd->data_buf)[5],		((u8 *)cmd->data_buf)[6], ((u8 *)cmd->data_buf)[7]);	/* Fill the SMU command buffer */	smu->cmd_buf->cmd = cmd->cmd;	smu->cmd_buf->length = cmd->data_len;	memcpy(smu->cmd_buf->data, cmd->data_buf, cmd->data_len);	/* Flush command and data to RAM */	faddr = (unsigned long)smu->cmd_buf;	fend = faddr + smu->cmd_buf->length + 2;	flush_inval_dcache_range(faddr, fend);	/* This isn't exactly a DMA mapping here, I suspect	 * the SMU is actually communicating with us via i2c to the	 * northbridge or the CPU to access RAM.	 */	writel(smu->cmd_buf_abs, smu->db_buf);	/* Ring the SMU doorbell */	pmac_do_feature_call(PMAC_FTR_WRITE_GPIO, NULL, smu->doorbell, 4);}static irqreturn_t smu_db_intr(int irq, void *arg){	unsigned long flags;	struct smu_cmd *cmd;	void (*done)(struct smu_cmd *cmd, void *misc) = NULL;	void *misc = NULL;	u8 gpio;	int rc = 0;	/* SMU completed the command, well, we hope, let's make sure	 * of it	 */	spin_lock_irqsave(&smu->lock, flags);	gpio = pmac_do_feature_call(PMAC_FTR_READ_GPIO, NULL, smu->doorbell);	if ((gpio & 7) != 7) {		spin_unlock_irqrestore(&smu->lock, flags);		return IRQ_HANDLED;	}	cmd = smu->cmd_cur;	smu->cmd_cur = NULL;	if (cmd == NULL)		goto bail;	if (rc == 0) {		unsigned long faddr;		int reply_len;		u8 ack;		/* CPU might have brought back the cache line, so we need		 * to flush again before peeking at the SMU response. We		 * flush the entire buffer for now as we haven't read the		 * reply lenght (it's only 2 cache lines anyway)		 */		faddr = (unsigned long)smu->cmd_buf;		flush_inval_dcache_range(faddr, faddr + 256);		/* Now check ack */		ack = (~cmd->cmd) & 0xff;		if (ack != smu->cmd_buf->cmd) {			DPRINTK("SMU: incorrect ack, want %x got %x\n",				ack, smu->cmd_buf->cmd);			rc = -EIO;		}		reply_len = rc == 0 ? smu->cmd_buf->length : 0;		DPRINTK("SMU: reply len: %d\n", reply_len);		if (reply_len > cmd->reply_len) {			printk(KERN_WARNING "SMU: reply buffer too small,"			       "got %d bytes for a %d bytes buffer\n",			       reply_len, cmd->reply_len);			reply_len = cmd->reply_len;		}		cmd->reply_len = reply_len;		if (cmd->reply_buf && reply_len)			memcpy(cmd->reply_buf, smu->cmd_buf->data, reply_len);	}	/* Now complete the command. Write status last in order as we lost	 * ownership of the command structure as soon as it's no longer -1	 */	done = cmd->done;	misc = cmd->misc;	mb();	cmd->status = rc; bail:	/* Start next command if any */	smu_start_cmd();	spin_unlock_irqrestore(&smu->lock, flags);	/* Call command completion handler if any */	if (done)		done(cmd, misc);	/* It's an edge interrupt, nothing to do */	return IRQ_HANDLED;}static irqreturn_t smu_msg_intr(int irq, void *arg){	/* I don't quite know what to do with this one, we seem to never	 * receive it, so I suspect we have to arm it someway in the SMU	 * to start getting events that way.	 */	printk(KERN_INFO "SMU: message interrupt !\n");	/* It's an edge interrupt, nothing to do */	return IRQ_HANDLED;}/* * Queued command management. * */int smu_queue_cmd(struct smu_cmd *cmd){	unsigned long flags;	if (smu == NULL)		return -ENODEV;	if (cmd->data_len > SMU_MAX_DATA ||	    cmd->reply_len > SMU_MAX_DATA)		return -EINVAL;	cmd->status = 1;	spin_lock_irqsave(&smu->lock, flags);	list_add_tail(&cmd->link, &smu->cmd_list);	if (smu->cmd_cur == NULL)		smu_start_cmd();	spin_unlock_irqrestore(&smu->lock, flags);	/* Workaround for early calls when irq isn't available */	if (!smu_irq_inited || smu->db_irq == NO_IRQ)		smu_spinwait_cmd(cmd);	return 0;}EXPORT_SYMBOL(smu_queue_cmd);int smu_queue_simple(struct smu_simple_cmd *scmd, u8 command,		     unsigned int data_len,		     void (*done)(struct smu_cmd *cmd, void *misc),		     void *misc, ...){	struct smu_cmd *cmd = &scmd->cmd;	va_list list;	int i;	if (data_len > sizeof(scmd->buffer))		return -EINVAL;	memset(scmd, 0, sizeof(*scmd));	cmd->cmd = command;	cmd->data_len = data_len;	cmd->data_buf = scmd->buffer;	cmd->reply_len = sizeof(scmd->buffer);	cmd->reply_buf = scmd->buffer;	cmd->done = done;	cmd->misc = misc;	va_start(list, misc);	for (i = 0; i < data_len; ++i)		scmd->buffer[i] = (u8)va_arg(list, int);	va_end(list);	return smu_queue_cmd(cmd);}EXPORT_SYMBOL(smu_queue_simple);void smu_poll(void){	u8 gpio;	if (smu == NULL)		return;	gpio = pmac_do_feature_call(PMAC_FTR_READ_GPIO, NULL, smu->doorbell);	if ((gpio & 7) == 7)		smu_db_intr(smu->db_irq, smu);}EXPORT_SYMBOL(smu_poll);void smu_done_complete(struct smu_cmd *cmd, void *misc){	struct completion *comp = misc;	complete(comp);}EXPORT_SYMBOL(smu_done_complete);void smu_spinwait_cmd(struct smu_cmd *cmd){	while(cmd->status == 1)		smu_poll();}EXPORT_SYMBOL(smu_spinwait_cmd);/* RTC low level commands */static inline int bcd2hex (int n){	return (((n & 0xf0) >> 4) * 10) + (n & 0xf);}static inline int hex2bcd (int n){	return ((n / 10) << 4) + (n % 10);}static inline void smu_fill_set_rtc_cmd(struct smu_cmd_buf *cmd_buf,					struct rtc_time *time){	cmd_buf->cmd = 0x8e;	cmd_buf->length = 8;	cmd_buf->data[0] = 0x80;	cmd_buf->data[1] = hex2bcd(time->tm_sec);	cmd_buf->data[2] = hex2bcd(time->tm_min);	cmd_buf->data[3] = hex2bcd(time->tm_hour);	cmd_buf->data[4] = time->tm_wday;	cmd_buf->data[5] = hex2bcd(time->tm_mday);	cmd_buf->data[6] = hex2bcd(time->tm_mon) + 1;	cmd_buf->data[7] = hex2bcd(time->tm_year - 100);}int smu_get_rtc_time(struct rtc_time *time, int spinwait){	struct smu_simple_cmd cmd;	int rc;	if (smu == NULL)		return -ENODEV;	memset(time, 0, sizeof(struct rtc_time));	rc = smu_queue_simple(&cmd, SMU_CMD_RTC_COMMAND, 1, NULL, NULL,			      SMU_CMD_RTC_GET_DATETIME);	if (rc)		return rc;	smu_spinwait_simple(&cmd);	time->tm_sec = bcd2hex(cmd.buffer[0]);	time->tm_min = bcd2hex(cmd.buffer[1]);	time->tm_hour = bcd2hex(cmd.buffer[2]);	time->tm_wday = bcd2hex(cmd.buffer[3]);	time->tm_mday = bcd2hex(cmd.buffer[4]);	time->tm_mon = bcd2hex(cmd.buffer[5]) - 1;	time->tm_year = bcd2hex(cmd.buffer[6]) + 100;	return 0;}int smu_set_rtc_time(struct rtc_time *time, int spinwait){	struct smu_simple_cmd cmd;	int rc;	if (smu == NULL)		return -ENODEV;	rc = smu_queue_simple(&cmd, SMU_CMD_RTC_COMMAND, 8, NULL, NULL,			      SMU_CMD_RTC_SET_DATETIME,			      hex2bcd(time->tm_sec),			      hex2bcd(time->tm_min),			      hex2bcd(time->tm_hour),			      time->tm_wday,			      hex2bcd(time->tm_mday),			      hex2bcd(time->tm_mon) + 1,			      hex2bcd(time->tm_year - 100));	if (rc)		return rc;	smu_spinwait_simple(&cmd);	return 0;}void smu_shutdown(void){	struct smu_simple_cmd cmd;	if (smu == NULL)		return;	if (smu_queue_simple(&cmd, SMU_CMD_POWER_COMMAND, 9, NULL, NULL,			     'S', 'H', 'U', 'T', 'D', 'O', 'W', 'N', 0))		return;	smu_spinwait_simple(&cmd);	for (;;)		;}void smu_restart(void){	struct smu_simple_cmd cmd;	if (smu == NULL)		return;	if (smu_queue_simple(&cmd, SMU_CMD_POWER_COMMAND, 8, NULL, NULL,			     'R', 'E', 'S', 'T', 'A', 'R', 'T', 0))		return;	smu_spinwait_simple(&cmd);	for (;;)		;}int smu_present(void){	return smu != NULL;}EXPORT_SYMBOL(smu_present);int __init smu_init (void){	struct device_node *np;	const u32 *data;        np = of_find_node_by_type(NULL, "smu");        if (np == NULL)		return -ENODEV;	printk(KERN_INFO "SMU driver %s %s\n", VERSION, AUTHOR);	if (smu_cmdbuf_abs == 0) {		printk(KERN_ERR "SMU: Command buffer not allocated !\n");		return -EINVAL;	}	smu = alloc_bootmem(sizeof(struct smu_device));	if (smu == NULL)		return -ENOMEM;	memset(smu, 0, sizeof(*smu));	spin_lock_init(&smu->lock);	INIT_LIST_HEAD(&smu->cmd_list);	INIT_LIST_HEAD(&smu->cmd_i2c_list);	smu->of_node = np;	smu->db_irq = NO_IRQ;	smu->msg_irq = NO_IRQ;	/* smu_cmdbuf_abs is in the low 2G of RAM, can be converted to a	 * 32 bits value safely	 */	smu->cmd_buf_abs = (u32)smu_cmdbuf_abs;	smu->cmd_buf = (struct smu_cmd_buf *)abs_to_virt(smu_cmdbuf_abs);	smu->db_node = of_find_node_by_name(NULL, "smu-doorbell");	if (smu->db_node == NULL) {		printk(KERN_ERR "SMU: Can't find doorbell GPIO !\n");		goto fail;	}	data = of_get_property(smu->db_node, "reg", NULL);	if (data == NULL) {		of_node_put(smu->db_node);		smu->db_node = NULL;		printk(KERN_ERR "SMU: Can't find doorbell GPIO address !\n");		goto fail;	}	/* Current setup has one doorbell GPIO that does both doorbell	 * and ack. GPIOs are at 0x50, best would be to find that out	 * in the device-tree though.	 */	smu->doorbell = *data;	if (smu->doorbell < 0x50)		smu->doorbell += 0x50;	/* Now look for the smu-interrupt GPIO */	do {		smu->msg_node = of_find_node_by_name(NULL, "smu-interrupt");		if (smu->msg_node == NULL)			break;		data = of_get_property(smu->msg_node, "reg", NULL);		if (data == NULL) {			of_node_put(smu->msg_node);			smu->msg_node = NULL;			break;		}		smu->msg = *data;		if (smu->msg < 0x50)			smu->msg += 0x50;	} while(0);	/* Doorbell buffer is currently hard-coded, I didn't find a proper	 * device-tree entry giving the address. Best would probably to use	 * an offset for K2 base though, but let's do it that way for now.	 */	smu->db_buf = ioremap(0x8000860c, 0x1000);	if (smu->db_buf == NULL) {		printk(KERN_ERR "SMU: Can't map doorbell buffer pointer !\n");		goto fail;	}	sys_ctrler = SYS_CTRLER_SMU;	return 0; fail:	smu = NULL;	return -ENXIO;}static int smu_late_init(void){	if (!smu)		return 0;	init_timer(&smu->i2c_timer);	smu->i2c_timer.function = smu_i2c_retry;	smu->i2c_timer.data = (unsigned long)smu;	if (smu->db_node) {		smu->db_irq = irq_of_parse_and_map(smu->db_node, 0);		if (smu->db_irq == NO_IRQ)			printk(KERN_ERR "smu: failed to map irq for node %s\n",			       smu->db_node->full_name);	}	if (smu->msg_node) {		smu->msg_irq = irq_of_parse_and_map(smu->msg_node, 0);		if (smu->msg_irq == NO_IRQ)			printk(KERN_ERR "smu: failed to map irq for node %s\n",			       smu->msg_node->full_name);	}	/*	 * Try to request the interrupts	 */	if (smu->db_irq != NO_IRQ) {		if (request_irq(smu->db_irq, smu_db_intr,				IRQF_SHARED, "SMU doorbell", smu) < 0) {			printk(KERN_WARNING "SMU: can't "			       "request interrupt %d\n",			       smu->db_irq);			smu->db_irq = NO_IRQ;		}	}	if (smu->msg_irq != NO_IRQ) {		if (request_irq(smu->msg_irq, smu_msg_intr,				IRQF_SHARED, "SMU message", smu) < 0) {			printk(KERN_WARNING "SMU: can't "			       "request interrupt %d\n",			       smu->msg_irq);			smu->msg_irq = NO_IRQ;		}	}	smu_irq_inited = 1;	return 0;}/* This has to be before arch_initcall as the low i2c stuff relies on the * above having been done before we reach arch_initcalls */core_initcall(smu_late_init);/* * sysfs visibility */static void smu_expose_childs(struct work_struct *unused){	struct device_node *np;	for (np = NULL; (np = of_get_next_child(smu->of_node, np)) != NULL;)		if (of_device_is_compatible(np, "smu-sensors"))			of_platform_device_create(np, "smu-sensors",						  &smu->of_dev->dev);}static DECLARE_WORK(smu_expose_childs_work, smu_expose_childs);static int smu_platform_probe(struct of_device* dev,			      const struct of_device_id *match){	if (!smu)		return -ENODEV;	smu->of_dev = dev;	/*	 * Ok, we are matched, now expose all i2c busses. We have to defer	 * that unfortunately or it would deadlock inside the device model	 */	schedule_work(&smu_expose_childs_work);	return 0;}static struct of_device_id smu_platform_match[] ={	{		.type		= "smu",	},	{},};static struct of_platform_driver smu_of_platform_driver ={	.name 		= "smu",	.match_table	= smu_platform_match,	.probe		= smu_platform_probe,};static int __init smu_init_sysfs(void){	/*	 * Due to sysfs bogosity, a sysdev is not a real device, so	 * we should in fact create both if we want sysdev semantics

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?