2 * intel_scu_ipc.c: Driver for the Intel SCU IPC mechanism
4 * (C) Copyright 2008-2010 Intel Corporation
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; version 2
12 * SCU running in ARC processor communicates with other entity running in IA
13 * core through IPC mechanism which in turn messaging between IA core ad SCU.
14 * SCU has two IPC mechanism IPC-1 and IPC-2. IPC-1 is used between IA32 and
15 * SCU where IPC-2 is used between P-Unit and SCU. This driver delas with
16 * IPC-1 Driver provides an API for power control unit registers (e.g. MSIC)
17 * along with other APIs.
19 #include <linux/delay.h>
20 #include <linux/errno.h>
21 #include <linux/init.h>
22 #include <linux/device.h>
24 #include <linux/pci.h>
25 #include <linux/interrupt.h>
26 #include <linux/sfi.h>
27 #include <linux/module.h>
28 #include <asm/intel-mid.h>
29 #include <asm/intel_scu_ipc.h>
31 /* IPC defines the following message types */
32 #define IPCMSG_WATCHDOG_TIMER 0xF8 /* Set Kernel Watchdog Threshold */
33 #define IPCMSG_BATTERY 0xEF /* Coulomb Counter Accumulator */
34 #define IPCMSG_FW_UPDATE 0xFE /* Firmware update */
35 #define IPCMSG_PCNTRL 0xFF /* Power controller unit read/write */
36 #define IPCMSG_FW_REVISION 0xF4 /* Get firmware revision */
38 /* Command id associated with message IPCMSG_PCNTRL */
39 #define IPC_CMD_PCNTRL_W 0 /* Register write */
40 #define IPC_CMD_PCNTRL_R 1 /* Register read */
41 #define IPC_CMD_PCNTRL_M 2 /* Register read-modify-write */
44 * IPC register summary
46 * IPC register blocks are memory mapped at fixed address of 0xFF11C000
47 * To read or write information to the SCU, driver writes to IPC-1 memory
48 * mapped registers (base address 0xFF11C000). The following is the IPC
51 * 1. IA core cDMI interface claims this transaction and converts it to a
52 * Transaction Layer Packet (TLP) message which is sent across the cDMI.
54 * 2. South Complex cDMI block receives this message and writes it to
55 * the IPC-1 register block, causing an interrupt to the SCU
57 * 3. SCU firmware decodes this interrupt and IPC message and the appropriate
58 * message handler is called within firmware.
61 #define IPC_WWBUF_SIZE 20 /* IPC Write buffer Size */
62 #define IPC_RWBUF_SIZE 20 /* IPC Read buffer Size */
63 #define IPC_IOC 0x100 /* IPC command register IOC bit */
72 /* intel scu ipc driver data*/
73 struct intel_scu_ipc_pdata_t {
81 static struct intel_scu_ipc_pdata_t intel_scu_ipc_pdata[] = {
82 [SCU_IPC_LINCROFT] = {
83 .ipc_base = 0xff11c000,
84 .i2c_base = 0xff12b000,
90 .ipc_base = 0xff11c000,
91 .i2c_base = 0xff12b000,
96 [SCU_IPC_CLOVERVIEW] = {
97 .ipc_base = 0xff11c000,
98 .i2c_base = 0xff12b000,
103 [SCU_IPC_TANGIER] = {
104 .ipc_base = 0xff009000,
105 .i2c_base = 0xff00d000,
112 static int ipc_probe(struct pci_dev *dev, const struct pci_device_id *id);
113 static void ipc_remove(struct pci_dev *pdev);
115 struct intel_scu_ipc_dev {
116 struct pci_dev *pdev;
117 void __iomem *ipc_base;
118 void __iomem *i2c_base;
119 struct completion cmd_complete;
123 static struct intel_scu_ipc_dev ipcdev; /* Only one for now */
125 static int platform; /* Platform type */
128 * IPC Read Buffer (Read Only):
129 * 16 byte buffer for receiving data from SCU, if IPC command
130 * processing results in response data
132 #define IPC_READ_BUFFER 0x90
134 #define IPC_I2C_CNTRL_ADDR 0
135 #define I2C_DATA_ADDR 0x04
137 static DEFINE_MUTEX(ipclock); /* lock used to prevent multiple call to SCU */
140 * Command Register (Write Only):
141 * A write to this register results in an interrupt to the SCU core processor
143 * |rfu2(8) | size(8) | command id(4) | rfu1(3) | ioc(1) | command(8)|
145 static inline void ipc_command(u32 cmd) /* Send ipc command */
147 if (ipcdev.irq_mode) {
148 reinit_completion(&ipcdev.cmd_complete);
149 writel(cmd | IPC_IOC, ipcdev.ipc_base);
151 writel(cmd, ipcdev.ipc_base);
155 * IPC Write Buffer (Write Only):
156 * 16-byte buffer for sending data associated with IPC command to
157 * SCU. Size of the data is specified in the IPC_COMMAND_REG register
159 static inline void ipc_data_writel(u32 data, u32 offset) /* Write ipc data */
161 writel(data, ipcdev.ipc_base + 0x80 + offset);
165 * Status Register (Read Only):
166 * Driver will read this register to get the ready/busy status of the IPC
167 * block and error status of the IPC command that was just processed by SCU
169 * |rfu3(8)|error code(8)|initiator id(8)|cmd id(4)|rfu1(2)|error(1)|busy(1)|
172 static inline u8 ipc_read_status(void)
174 return __raw_readl(ipcdev.ipc_base + 0x04);
177 static inline u8 ipc_data_readb(u32 offset) /* Read ipc byte data */
179 return readb(ipcdev.ipc_base + IPC_READ_BUFFER + offset);
182 static inline u32 ipc_data_readl(u32 offset) /* Read ipc u32 data */
184 return readl(ipcdev.ipc_base + IPC_READ_BUFFER + offset);
187 static inline int busy_loop(void) /* Wait till scu status is busy */
192 status = ipc_read_status();
194 udelay(1); /* scu processing time is in few u secods */
195 status = ipc_read_status();
197 /* break if scu doesn't reset busy bit after huge retry */
198 if (loop_count > 100000) {
199 dev_err(&ipcdev.pdev->dev, "IPC timed out");
203 if ((status >> 1) & 1)
209 /* Wait till ipc ioc interrupt is received or timeout in 3 HZ */
210 static inline int ipc_wait_for_interrupt(void)
214 if (!wait_for_completion_timeout(&ipcdev.cmd_complete, 3 * HZ)) {
215 struct device *dev = &ipcdev.pdev->dev;
216 dev_err(dev, "IPC timed out\n");
220 status = ipc_read_status();
222 if ((status >> 1) & 1)
228 int intel_scu_ipc_check_status(void)
230 return ipcdev.irq_mode ? ipc_wait_for_interrupt() : busy_loop();
233 /* Read/Write power control(PMIC in Langwell, MSIC in PenWell) registers */
234 static int pwr_reg_rdwr(u16 *addr, u8 *data, u32 count, u32 op, u32 id)
239 u8 cbuf[IPC_WWBUF_SIZE] = { };
240 u32 *wbuf = (u32 *)&cbuf;
242 mutex_lock(&ipclock);
244 memset(cbuf, 0, sizeof(cbuf));
246 if (ipcdev.pdev == NULL) {
247 mutex_unlock(&ipclock);
251 for (nc = 0; nc < count; nc++, offset += 2) {
252 cbuf[offset] = addr[nc];
253 cbuf[offset + 1] = addr[nc] >> 8;
256 if (id == IPC_CMD_PCNTRL_R) {
257 for (nc = 0, offset = 0; nc < count; nc++, offset += 4)
258 ipc_data_writel(wbuf[nc], offset);
259 ipc_command((count*2) << 16 | id << 12 | 0 << 8 | op);
260 } else if (id == IPC_CMD_PCNTRL_W) {
261 for (nc = 0; nc < count; nc++, offset += 1)
262 cbuf[offset] = data[nc];
263 for (nc = 0, offset = 0; nc < count; nc++, offset += 4)
264 ipc_data_writel(wbuf[nc], offset);
265 ipc_command((count*3) << 16 | id << 12 | 0 << 8 | op);
266 } else if (id == IPC_CMD_PCNTRL_M) {
267 cbuf[offset] = data[0];
268 cbuf[offset + 1] = data[1];
269 ipc_data_writel(wbuf[0], 0); /* Write wbuff */
270 ipc_command(4 << 16 | id << 12 | 0 << 8 | op);
273 err = intel_scu_ipc_check_status();
274 if (!err && id == IPC_CMD_PCNTRL_R) { /* Read rbuf */
275 /* Workaround: values are read as 0 without memcpy_fromio */
276 memcpy_fromio(cbuf, ipcdev.ipc_base + 0x90, 16);
277 for (nc = 0; nc < count; nc++)
278 data[nc] = ipc_data_readb(nc);
280 mutex_unlock(&ipclock);
285 * intel_scu_ipc_ioread8 - read a word via the SCU
286 * @addr: register on SCU
287 * @data: return pointer for read byte
289 * Read a single register. Returns 0 on success or an error code. All
290 * locking between SCU accesses is handled for the caller.
292 * This function may sleep.
294 int intel_scu_ipc_ioread8(u16 addr, u8 *data)
296 return pwr_reg_rdwr(&addr, data, 1, IPCMSG_PCNTRL, IPC_CMD_PCNTRL_R);
298 EXPORT_SYMBOL(intel_scu_ipc_ioread8);
301 * intel_scu_ipc_ioread16 - read a word via the SCU
302 * @addr: register on SCU
303 * @data: return pointer for read word
305 * Read a register pair. Returns 0 on success or an error code. All
306 * locking between SCU accesses is handled for the caller.
308 * This function may sleep.
310 int intel_scu_ipc_ioread16(u16 addr, u16 *data)
312 u16 x[2] = {addr, addr + 1 };
313 return pwr_reg_rdwr(x, (u8 *)data, 2, IPCMSG_PCNTRL, IPC_CMD_PCNTRL_R);
315 EXPORT_SYMBOL(intel_scu_ipc_ioread16);
318 * intel_scu_ipc_ioread32 - read a dword via the SCU
319 * @addr: register on SCU
320 * @data: return pointer for read dword
322 * Read four registers. Returns 0 on success or an error code. All
323 * locking between SCU accesses is handled for the caller.
325 * This function may sleep.
327 int intel_scu_ipc_ioread32(u16 addr, u32 *data)
329 u16 x[4] = {addr, addr + 1, addr + 2, addr + 3};
330 return pwr_reg_rdwr(x, (u8 *)data, 4, IPCMSG_PCNTRL, IPC_CMD_PCNTRL_R);
332 EXPORT_SYMBOL(intel_scu_ipc_ioread32);
335 * intel_scu_ipc_iowrite8 - write a byte via the SCU
336 * @addr: register on SCU
337 * @data: byte to write
339 * Write a single register. Returns 0 on success or an error code. All
340 * locking between SCU accesses is handled for the caller.
342 * This function may sleep.
344 int intel_scu_ipc_iowrite8(u16 addr, u8 data)
346 return pwr_reg_rdwr(&addr, &data, 1, IPCMSG_PCNTRL, IPC_CMD_PCNTRL_W);
348 EXPORT_SYMBOL(intel_scu_ipc_iowrite8);
351 * intel_scu_ipc_iowrite16 - write a word via the SCU
352 * @addr: register on SCU
353 * @data: word to write
355 * Write two registers. Returns 0 on success or an error code. All
356 * locking between SCU accesses is handled for the caller.
358 * This function may sleep.
360 int intel_scu_ipc_iowrite16(u16 addr, u16 data)
362 u16 x[2] = {addr, addr + 1 };
363 return pwr_reg_rdwr(x, (u8 *)&data, 2, IPCMSG_PCNTRL, IPC_CMD_PCNTRL_W);
365 EXPORT_SYMBOL(intel_scu_ipc_iowrite16);
368 * intel_scu_ipc_iowrite32 - write a dword via the SCU
369 * @addr: register on SCU
370 * @data: dword to write
372 * Write four registers. Returns 0 on success or an error code. All
373 * locking between SCU accesses is handled for the caller.
375 * This function may sleep.
377 int intel_scu_ipc_iowrite32(u16 addr, u32 data)
379 u16 x[4] = {addr, addr + 1, addr + 2, addr + 3};
380 return pwr_reg_rdwr(x, (u8 *)&data, 4, IPCMSG_PCNTRL, IPC_CMD_PCNTRL_W);
382 EXPORT_SYMBOL(intel_scu_ipc_iowrite32);
385 * intel_scu_ipc_readvv - read a set of registers
386 * @addr: register list
387 * @data: bytes to return
388 * @len: length of array
390 * Read registers. Returns 0 on success or an error code. All
391 * locking between SCU accesses is handled for the caller.
393 * The largest array length permitted by the hardware is 5 items.
395 * This function may sleep.
397 int intel_scu_ipc_readv(u16 *addr, u8 *data, int len)
399 return pwr_reg_rdwr(addr, data, len, IPCMSG_PCNTRL, IPC_CMD_PCNTRL_R);
401 EXPORT_SYMBOL(intel_scu_ipc_readv);
404 * intel_scu_ipc_writev - write a set of registers
405 * @addr: register list
406 * @data: bytes to write
407 * @len: length of array
409 * Write registers. Returns 0 on success or an error code. All
410 * locking between SCU accesses is handled for the caller.
412 * The largest array length permitted by the hardware is 5 items.
414 * This function may sleep.
417 int intel_scu_ipc_writev(u16 *addr, u8 *data, int len)
419 return pwr_reg_rdwr(addr, data, len, IPCMSG_PCNTRL, IPC_CMD_PCNTRL_W);
421 EXPORT_SYMBOL(intel_scu_ipc_writev);
425 * intel_scu_ipc_update_register - r/m/w a register
426 * @addr: register address
427 * @bits: bits to update
428 * @mask: mask of bits to update
430 * Read-modify-write power control unit register. The first data argument
431 * must be register value and second is mask value
432 * mask is a bitmap that indicates which bits to update.
433 * 0 = masked. Don't modify this bit, 1 = modify this bit.
434 * returns 0 on success or an error code.
436 * This function may sleep. Locking between SCU accesses is handled
439 int intel_scu_ipc_update_register(u16 addr, u8 bits, u8 mask)
441 u8 data[2] = { bits, mask };
442 return pwr_reg_rdwr(&addr, data, 1, IPCMSG_PCNTRL, IPC_CMD_PCNTRL_M);
444 EXPORT_SYMBOL(intel_scu_ipc_update_register);
447 * intel_scu_ipc_simple_command - send a simple command
451 * Issue a simple command to the SCU. Do not use this interface if
452 * you must then access data as any data values may be overwritten
453 * by another SCU access by the time this function returns.
455 * This function may sleep. Locking for SCU accesses is handled for
458 int intel_scu_ipc_simple_command(int cmd, int sub)
462 mutex_lock(&ipclock);
463 if (ipcdev.pdev == NULL) {
464 mutex_unlock(&ipclock);
467 ipc_command(sub << 12 | cmd);
468 err = intel_scu_ipc_check_status();
469 mutex_unlock(&ipclock);
472 EXPORT_SYMBOL(intel_scu_ipc_simple_command);
475 * intel_scu_ipc_command - command with data
479 * @inlen: input length in dwords
481 * @outlein: output length in dwords
483 * Issue a command to the SCU which involves data transfers. Do the
484 * data copies under the lock but leave it for the caller to interpret
487 int intel_scu_ipc_command(int cmd, int sub, u32 *in, int inlen,
488 u32 *out, int outlen)
492 mutex_lock(&ipclock);
493 if (ipcdev.pdev == NULL) {
494 mutex_unlock(&ipclock);
498 for (i = 0; i < inlen; i++)
499 ipc_data_writel(*in++, 4 * i);
501 ipc_command((inlen << 16) | (sub << 12) | cmd);
502 err = intel_scu_ipc_check_status();
505 for (i = 0; i < outlen; i++)
506 *out++ = ipc_data_readl(4 * i);
509 mutex_unlock(&ipclock);
512 EXPORT_SYMBOL(intel_scu_ipc_command);
515 #define IPC_I2C_WRITE 1 /* I2C Write command */
516 #define IPC_I2C_READ 2 /* I2C Read command */
519 * intel_scu_ipc_i2c_cntrl - I2C read/write operations
520 * @addr: I2C address + command bits
521 * @data: data to read/write
523 * Perform an an I2C read/write operation via the SCU. All locking is
524 * handled for the caller. This function may sleep.
526 * Returns an error code or 0 on success.
528 * This has to be in the IPC driver for the locking.
530 int intel_scu_ipc_i2c_cntrl(u32 addr, u32 *data)
534 mutex_lock(&ipclock);
535 if (ipcdev.pdev == NULL) {
536 mutex_unlock(&ipclock);
539 cmd = (addr >> 24) & 0xFF;
540 if (cmd == IPC_I2C_READ) {
541 writel(addr, ipcdev.i2c_base + IPC_I2C_CNTRL_ADDR);
542 /* Write not getting updated without delay */
544 *data = readl(ipcdev.i2c_base + I2C_DATA_ADDR);
545 } else if (cmd == IPC_I2C_WRITE) {
546 writel(*data, ipcdev.i2c_base + I2C_DATA_ADDR);
548 writel(addr, ipcdev.i2c_base + IPC_I2C_CNTRL_ADDR);
550 dev_err(&ipcdev.pdev->dev,
551 "intel_scu_ipc: I2C INVALID_CMD = 0x%x\n", cmd);
553 mutex_unlock(&ipclock);
556 mutex_unlock(&ipclock);
559 EXPORT_SYMBOL(intel_scu_ipc_i2c_cntrl);
562 * Interrupt handler gets called when ioc bit of IPC_COMMAND_REG set to 1
563 * When ioc bit is set to 1, caller api must wait for interrupt handler called
564 * which in turn unlocks the caller api. Currently this is not used
566 * This is edge triggered so we need take no action to clear anything
568 static irqreturn_t ioc(int irq, void *dev_id)
571 complete(&ipcdev.cmd_complete);
577 * ipc_probe - probe an Intel SCU IPC
578 * @dev: the PCI device matching
579 * @id: entry in the match table
581 * Enable and install an intel SCU IPC. This appears in the PCI space
582 * but uses some hard coded addresses as well.
584 static int ipc_probe(struct pci_dev *dev, const struct pci_device_id *id)
587 struct intel_scu_ipc_pdata_t *pdata;
588 resource_size_t pci_resource;
590 if (ipcdev.pdev) /* We support only one SCU */
593 pid = id->driver_data;
594 pdata = &intel_scu_ipc_pdata[pid];
596 ipcdev.pdev = pci_dev_get(dev);
597 ipcdev.irq_mode = pdata->irq_mode;
599 err = pci_enable_device(dev);
603 err = pci_request_regions(dev, "intel_scu_ipc");
607 pci_resource = pci_resource_start(dev, 0);
611 init_completion(&ipcdev.cmd_complete);
613 if (request_irq(dev->irq, ioc, 0, "intel_scu_ipc", &ipcdev))
616 ipcdev.ipc_base = ioremap_nocache(pdata->ipc_base, pdata->ipc_len);
617 if (!ipcdev.ipc_base)
620 ipcdev.i2c_base = ioremap_nocache(pdata->i2c_base, pdata->i2c_len);
621 if (!ipcdev.i2c_base) {
622 iounmap(ipcdev.ipc_base);
626 intel_scu_devices_create();
632 * ipc_remove - remove a bound IPC device
635 * In practice the SCU is not removable but this function is also
636 * called for each device on a module unload or cleanup which is the
637 * path that will get used.
639 * Free up the mappings and release the PCI resources
641 static void ipc_remove(struct pci_dev *pdev)
643 free_irq(pdev->irq, &ipcdev);
644 pci_release_regions(pdev);
645 pci_dev_put(ipcdev.pdev);
646 iounmap(ipcdev.ipc_base);
647 iounmap(ipcdev.i2c_base);
649 intel_scu_devices_destroy();
652 static DEFINE_PCI_DEVICE_TABLE(pci_ids) = {
653 {PCI_VDEVICE(INTEL, 0x082a), SCU_IPC_LINCROFT},
654 {PCI_VDEVICE(INTEL, 0x080e), SCU_IPC_PENWELL},
655 {PCI_VDEVICE(INTEL, 0x08ea), SCU_IPC_CLOVERVIEW},
656 {PCI_VDEVICE(INTEL, 0x11a0), SCU_IPC_TANGIER},
659 MODULE_DEVICE_TABLE(pci, pci_ids);
661 static struct pci_driver ipc_driver = {
662 .name = "intel_scu_ipc",
665 .remove = ipc_remove,
669 static int __init intel_scu_ipc_init(void)
671 platform = intel_mid_identify_cpu();
674 return pci_register_driver(&ipc_driver);
677 static void __exit intel_scu_ipc_exit(void)
679 pci_unregister_driver(&ipc_driver);
683 MODULE_DESCRIPTION("Intel SCU IPC driver");
684 MODULE_LICENSE("GPL");
686 module_init(intel_scu_ipc_init);
687 module_exit(intel_scu_ipc_exit);