2 * intel_scu_ipc.c: Driver for the Intel SCU IPC mechanism
4 * (C) Copyright 2008-2010,2015 Intel Corporation
5 * Author: Sreedhara DS (sreedhara.ds@intel.com)
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 <asm/intel-mid.h>
28 #include <asm/intel_scu_ipc.h>
30 /* IPC defines the following message types */
31 #define IPCMSG_WATCHDOG_TIMER 0xF8 /* Set Kernel Watchdog Threshold */
32 #define IPCMSG_BATTERY 0xEF /* Coulomb Counter Accumulator */
33 #define IPCMSG_FW_UPDATE 0xFE /* Firmware update */
34 #define IPCMSG_PCNTRL 0xFF /* Power controller unit read/write */
35 #define IPCMSG_FW_REVISION 0xF4 /* Get firmware revision */
37 /* Command id associated with message IPCMSG_PCNTRL */
38 #define IPC_CMD_PCNTRL_W 0 /* Register write */
39 #define IPC_CMD_PCNTRL_R 1 /* Register read */
40 #define IPC_CMD_PCNTRL_M 2 /* Register read-modify-write */
43 * IPC register summary
45 * IPC register blocks are memory mapped at fixed address of PCI BAR 0.
46 * To read or write information to the SCU, driver writes to IPC-1 memory
47 * mapped registers. The following is the IPC mechanism
49 * 1. IA core cDMI interface claims this transaction and converts it to a
50 * Transaction Layer Packet (TLP) message which is sent across the cDMI.
52 * 2. South Complex cDMI block receives this message and writes it to
53 * the IPC-1 register block, causing an interrupt to the SCU
55 * 3. SCU firmware decodes this interrupt and IPC message and the appropriate
56 * message handler is called within firmware.
59 #define IPC_WWBUF_SIZE 20 /* IPC Write buffer Size */
60 #define IPC_RWBUF_SIZE 20 /* IPC Read buffer Size */
61 #define IPC_IOC 0x100 /* IPC command register IOC bit */
63 #define PCI_DEVICE_ID_LINCROFT 0x082a
64 #define PCI_DEVICE_ID_PENWELL 0x080e
65 #define PCI_DEVICE_ID_CLOVERVIEW 0x08ea
66 #define PCI_DEVICE_ID_TANGIER 0x11a0
68 /* intel scu ipc driver data */
69 struct intel_scu_ipc_pdata_t
{
74 static const struct intel_scu_ipc_pdata_t intel_scu_ipc_lincroft_pdata
= {
75 .i2c_base
= 0xff12b000,
79 /* Penwell and Cloverview */
80 static const struct intel_scu_ipc_pdata_t intel_scu_ipc_penwell_pdata
= {
81 .i2c_base
= 0xff12b000,
85 static const struct intel_scu_ipc_pdata_t intel_scu_ipc_tangier_pdata
= {
86 .i2c_base
= 0xff00d000,
90 struct intel_scu_ipc_dev
{
92 void __iomem
*ipc_base
;
93 void __iomem
*i2c_base
;
94 struct completion cmd_complete
;
98 static struct intel_scu_ipc_dev ipcdev
; /* Only one for now */
100 #define IPC_STATUS 0x04
101 #define IPC_STATUS_IRQ BIT(2)
104 * IPC Read Buffer (Read Only):
105 * 16 byte buffer for receiving data from SCU, if IPC command
106 * processing results in response data
108 #define IPC_READ_BUFFER 0x90
110 #define IPC_I2C_CNTRL_ADDR 0
111 #define I2C_DATA_ADDR 0x04
113 static DEFINE_MUTEX(ipclock
); /* lock used to prevent multiple call to SCU */
117 * Command Register (Write Only):
118 * A write to this register results in an interrupt to the SCU core processor
120 * |rfu2(8) | size(8) | command id(4) | rfu1(3) | ioc(1) | command(8)|
122 static inline void ipc_command(struct intel_scu_ipc_dev
*scu
, u32 cmd
)
124 reinit_completion(&scu
->cmd_complete
);
125 writel(cmd
| IPC_IOC
, scu
->ipc_base
);
130 * IPC Write Buffer (Write Only):
131 * 16-byte buffer for sending data associated with IPC command to
132 * SCU. Size of the data is specified in the IPC_COMMAND_REG register
134 static inline void ipc_data_writel(struct intel_scu_ipc_dev
*scu
, u32 data
, u32 offset
)
136 writel(data
, scu
->ipc_base
+ 0x80 + offset
);
140 * Status Register (Read Only):
141 * Driver will read this register to get the ready/busy status of the IPC
142 * block and error status of the IPC command that was just processed by SCU
144 * |rfu3(8)|error code(8)|initiator id(8)|cmd id(4)|rfu1(2)|error(1)|busy(1)|
146 static inline u8
ipc_read_status(struct intel_scu_ipc_dev
*scu
)
148 return __raw_readl(scu
->ipc_base
+ 0x04);
151 /* Read ipc byte data */
152 static inline u8
ipc_data_readb(struct intel_scu_ipc_dev
*scu
, u32 offset
)
154 return readb(scu
->ipc_base
+ IPC_READ_BUFFER
+ offset
);
157 /* Read ipc u32 data */
158 static inline u32
ipc_data_readl(struct intel_scu_ipc_dev
*scu
, u32 offset
)
160 return readl(scu
->ipc_base
+ IPC_READ_BUFFER
+ offset
);
163 /* Wait till scu status is busy */
164 static inline int busy_loop(struct intel_scu_ipc_dev
*scu
)
166 u32 status
= ipc_read_status(scu
);
167 u32 loop_count
= 100000;
169 /* break if scu doesn't reset busy bit after huge retry */
170 while ((status
& BIT(0)) && --loop_count
) {
171 udelay(1); /* scu processing time is in few u secods */
172 status
= ipc_read_status(scu
);
175 if (status
& BIT(0)) {
176 dev_err(scu
->dev
, "IPC timed out");
186 /* Wait till ipc ioc interrupt is received or timeout in 3 HZ */
187 static inline int ipc_wait_for_interrupt(struct intel_scu_ipc_dev
*scu
)
191 if (!wait_for_completion_timeout(&scu
->cmd_complete
, 3 * HZ
)) {
192 dev_err(scu
->dev
, "IPC timed out\n");
196 status
= ipc_read_status(scu
);
203 static int intel_scu_ipc_check_status(struct intel_scu_ipc_dev
*scu
)
205 return scu
->irq_mode
? ipc_wait_for_interrupt(scu
) : busy_loop(scu
);
208 /* Read/Write power control(PMIC in Langwell, MSIC in PenWell) registers */
209 static int pwr_reg_rdwr(u16
*addr
, u8
*data
, u32 count
, u32 op
, u32 id
)
211 struct intel_scu_ipc_dev
*scu
= &ipcdev
;
215 u8 cbuf
[IPC_WWBUF_SIZE
];
216 u32
*wbuf
= (u32
*)&cbuf
;
218 memset(cbuf
, 0, sizeof(cbuf
));
220 mutex_lock(&ipclock
);
222 if (scu
->dev
== NULL
) {
223 mutex_unlock(&ipclock
);
227 for (nc
= 0; nc
< count
; nc
++, offset
+= 2) {
228 cbuf
[offset
] = addr
[nc
];
229 cbuf
[offset
+ 1] = addr
[nc
] >> 8;
232 if (id
== IPC_CMD_PCNTRL_R
) {
233 for (nc
= 0, offset
= 0; nc
< count
; nc
++, offset
+= 4)
234 ipc_data_writel(scu
, wbuf
[nc
], offset
);
235 ipc_command(scu
, (count
* 2) << 16 | id
<< 12 | 0 << 8 | op
);
236 } else if (id
== IPC_CMD_PCNTRL_W
) {
237 for (nc
= 0; nc
< count
; nc
++, offset
+= 1)
238 cbuf
[offset
] = data
[nc
];
239 for (nc
= 0, offset
= 0; nc
< count
; nc
++, offset
+= 4)
240 ipc_data_writel(scu
, wbuf
[nc
], offset
);
241 ipc_command(scu
, (count
* 3) << 16 | id
<< 12 | 0 << 8 | op
);
242 } else if (id
== IPC_CMD_PCNTRL_M
) {
243 cbuf
[offset
] = data
[0];
244 cbuf
[offset
+ 1] = data
[1];
245 ipc_data_writel(scu
, wbuf
[0], 0); /* Write wbuff */
246 ipc_command(scu
, 4 << 16 | id
<< 12 | 0 << 8 | op
);
249 err
= intel_scu_ipc_check_status(scu
);
250 if (!err
&& id
== IPC_CMD_PCNTRL_R
) { /* Read rbuf */
251 /* Workaround: values are read as 0 without memcpy_fromio */
252 memcpy_fromio(cbuf
, scu
->ipc_base
+ 0x90, 16);
253 for (nc
= 0; nc
< count
; nc
++)
254 data
[nc
] = ipc_data_readb(scu
, nc
);
256 mutex_unlock(&ipclock
);
261 * intel_scu_ipc_ioread8 - read a word via the SCU
262 * @addr: register on SCU
263 * @data: return pointer for read byte
265 * Read a single register. Returns 0 on success or an error code. All
266 * locking between SCU accesses is handled for the caller.
268 * This function may sleep.
270 int intel_scu_ipc_ioread8(u16 addr
, u8
*data
)
272 return pwr_reg_rdwr(&addr
, data
, 1, IPCMSG_PCNTRL
, IPC_CMD_PCNTRL_R
);
274 EXPORT_SYMBOL(intel_scu_ipc_ioread8
);
277 * intel_scu_ipc_ioread16 - read a word via the SCU
278 * @addr: register on SCU
279 * @data: return pointer for read word
281 * Read a register pair. Returns 0 on success or an error code. All
282 * locking between SCU accesses is handled for the caller.
284 * This function may sleep.
286 int intel_scu_ipc_ioread16(u16 addr
, u16
*data
)
288 u16 x
[2] = {addr
, addr
+ 1};
289 return pwr_reg_rdwr(x
, (u8
*)data
, 2, IPCMSG_PCNTRL
, IPC_CMD_PCNTRL_R
);
291 EXPORT_SYMBOL(intel_scu_ipc_ioread16
);
294 * intel_scu_ipc_ioread32 - read a dword via the SCU
295 * @addr: register on SCU
296 * @data: return pointer for read dword
298 * Read four registers. Returns 0 on success or an error code. All
299 * locking between SCU accesses is handled for the caller.
301 * This function may sleep.
303 int intel_scu_ipc_ioread32(u16 addr
, u32
*data
)
305 u16 x
[4] = {addr
, addr
+ 1, addr
+ 2, addr
+ 3};
306 return pwr_reg_rdwr(x
, (u8
*)data
, 4, IPCMSG_PCNTRL
, IPC_CMD_PCNTRL_R
);
308 EXPORT_SYMBOL(intel_scu_ipc_ioread32
);
311 * intel_scu_ipc_iowrite8 - write a byte via the SCU
312 * @addr: register on SCU
313 * @data: byte to write
315 * Write a single register. Returns 0 on success or an error code. All
316 * locking between SCU accesses is handled for the caller.
318 * This function may sleep.
320 int intel_scu_ipc_iowrite8(u16 addr
, u8 data
)
322 return pwr_reg_rdwr(&addr
, &data
, 1, IPCMSG_PCNTRL
, IPC_CMD_PCNTRL_W
);
324 EXPORT_SYMBOL(intel_scu_ipc_iowrite8
);
327 * intel_scu_ipc_iowrite16 - write a word via the SCU
328 * @addr: register on SCU
329 * @data: word to write
331 * Write two registers. Returns 0 on success or an error code. All
332 * locking between SCU accesses is handled for the caller.
334 * This function may sleep.
336 int intel_scu_ipc_iowrite16(u16 addr
, u16 data
)
338 u16 x
[2] = {addr
, addr
+ 1};
339 return pwr_reg_rdwr(x
, (u8
*)&data
, 2, IPCMSG_PCNTRL
, IPC_CMD_PCNTRL_W
);
341 EXPORT_SYMBOL(intel_scu_ipc_iowrite16
);
344 * intel_scu_ipc_iowrite32 - write a dword via the SCU
345 * @addr: register on SCU
346 * @data: dword to write
348 * Write four registers. Returns 0 on success or an error code. All
349 * locking between SCU accesses is handled for the caller.
351 * This function may sleep.
353 int intel_scu_ipc_iowrite32(u16 addr
, u32 data
)
355 u16 x
[4] = {addr
, addr
+ 1, addr
+ 2, addr
+ 3};
356 return pwr_reg_rdwr(x
, (u8
*)&data
, 4, IPCMSG_PCNTRL
, IPC_CMD_PCNTRL_W
);
358 EXPORT_SYMBOL(intel_scu_ipc_iowrite32
);
361 * intel_scu_ipc_readvv - read a set of registers
362 * @addr: register list
363 * @data: bytes to return
364 * @len: length of array
366 * Read registers. Returns 0 on success or an error code. All
367 * locking between SCU accesses is handled for the caller.
369 * The largest array length permitted by the hardware is 5 items.
371 * This function may sleep.
373 int intel_scu_ipc_readv(u16
*addr
, u8
*data
, int len
)
375 return pwr_reg_rdwr(addr
, data
, len
, IPCMSG_PCNTRL
, IPC_CMD_PCNTRL_R
);
377 EXPORT_SYMBOL(intel_scu_ipc_readv
);
380 * intel_scu_ipc_writev - write a set of registers
381 * @addr: register list
382 * @data: bytes to write
383 * @len: length of array
385 * Write registers. Returns 0 on success or an error code. All
386 * locking between SCU accesses is handled for the caller.
388 * The largest array length permitted by the hardware is 5 items.
390 * This function may sleep.
393 int intel_scu_ipc_writev(u16
*addr
, u8
*data
, int len
)
395 return pwr_reg_rdwr(addr
, data
, len
, IPCMSG_PCNTRL
, IPC_CMD_PCNTRL_W
);
397 EXPORT_SYMBOL(intel_scu_ipc_writev
);
400 * intel_scu_ipc_update_register - r/m/w a register
401 * @addr: register address
402 * @bits: bits to update
403 * @mask: mask of bits to update
405 * Read-modify-write power control unit register. The first data argument
406 * must be register value and second is mask value
407 * mask is a bitmap that indicates which bits to update.
408 * 0 = masked. Don't modify this bit, 1 = modify this bit.
409 * returns 0 on success or an error code.
411 * This function may sleep. Locking between SCU accesses is handled
414 int intel_scu_ipc_update_register(u16 addr
, u8 bits
, u8 mask
)
416 u8 data
[2] = { bits
, mask
};
417 return pwr_reg_rdwr(&addr
, data
, 1, IPCMSG_PCNTRL
, IPC_CMD_PCNTRL_M
);
419 EXPORT_SYMBOL(intel_scu_ipc_update_register
);
422 * intel_scu_ipc_simple_command - send a simple command
426 * Issue a simple command to the SCU. Do not use this interface if
427 * you must then access data as any data values may be overwritten
428 * by another SCU access by the time this function returns.
430 * This function may sleep. Locking for SCU accesses is handled for
433 int intel_scu_ipc_simple_command(int cmd
, int sub
)
435 struct intel_scu_ipc_dev
*scu
= &ipcdev
;
438 mutex_lock(&ipclock
);
439 if (scu
->dev
== NULL
) {
440 mutex_unlock(&ipclock
);
443 ipc_command(scu
, sub
<< 12 | cmd
);
444 err
= intel_scu_ipc_check_status(scu
);
445 mutex_unlock(&ipclock
);
448 EXPORT_SYMBOL(intel_scu_ipc_simple_command
);
451 * intel_scu_ipc_command - command with data
455 * @inlen: input length in dwords
457 * @outlein: output length in dwords
459 * Issue a command to the SCU which involves data transfers. Do the
460 * data copies under the lock but leave it for the caller to interpret
462 int intel_scu_ipc_command(int cmd
, int sub
, u32
*in
, int inlen
,
463 u32
*out
, int outlen
)
465 struct intel_scu_ipc_dev
*scu
= &ipcdev
;
468 mutex_lock(&ipclock
);
469 if (scu
->dev
== NULL
) {
470 mutex_unlock(&ipclock
);
474 for (i
= 0; i
< inlen
; i
++)
475 ipc_data_writel(scu
, *in
++, 4 * i
);
477 ipc_command(scu
, (inlen
<< 16) | (sub
<< 12) | cmd
);
478 err
= intel_scu_ipc_check_status(scu
);
481 for (i
= 0; i
< outlen
; i
++)
482 *out
++ = ipc_data_readl(scu
, 4 * i
);
485 mutex_unlock(&ipclock
);
488 EXPORT_SYMBOL(intel_scu_ipc_command
);
490 #define IPC_SPTR 0x08
491 #define IPC_DPTR 0x0C
494 * intel_scu_ipc_raw_command() - IPC command with data and pointers
495 * @cmd: IPC command code.
496 * @sub: IPC command sub type.
497 * @in: input data of this IPC command.
498 * @inlen: input data length in dwords.
499 * @out: output data of this IPC command.
500 * @outlen: output data length in dwords.
501 * @sptr: data writing to SPTR register.
502 * @dptr: data writing to DPTR register.
504 * Send an IPC command to SCU with input/output data and source/dest pointers.
506 * Return: an IPC error code or 0 on success.
508 int intel_scu_ipc_raw_command(int cmd
, int sub
, u8
*in
, int inlen
,
509 u32
*out
, int outlen
, u32 dptr
, u32 sptr
)
511 struct intel_scu_ipc_dev
*scu
= &ipcdev
;
512 int inbuflen
= DIV_ROUND_UP(inlen
, 4);
520 mutex_lock(&ipclock
);
521 if (scu
->dev
== NULL
) {
522 mutex_unlock(&ipclock
);
526 writel(dptr
, scu
->ipc_base
+ IPC_DPTR
);
527 writel(sptr
, scu
->ipc_base
+ IPC_SPTR
);
530 * SRAM controller doesn't support 8-bit writes, it only
531 * supports 32-bit writes, so we have to copy input data into
532 * the temporary buffer, and SCU FW will use the inlen to
533 * determine the actual input data length in the temporary
536 memcpy(inbuf
, in
, inlen
);
538 for (i
= 0; i
< inbuflen
; i
++)
539 ipc_data_writel(scu
, inbuf
[i
], 4 * i
);
541 ipc_command(scu
, (inlen
<< 16) | (sub
<< 12) | cmd
);
542 err
= intel_scu_ipc_check_status(scu
);
544 for (i
= 0; i
< outlen
; i
++)
545 *out
++ = ipc_data_readl(scu
, 4 * i
);
548 mutex_unlock(&ipclock
);
551 EXPORT_SYMBOL_GPL(intel_scu_ipc_raw_command
);
554 #define IPC_I2C_WRITE 1 /* I2C Write command */
555 #define IPC_I2C_READ 2 /* I2C Read command */
558 * intel_scu_ipc_i2c_cntrl - I2C read/write operations
559 * @addr: I2C address + command bits
560 * @data: data to read/write
562 * Perform an an I2C read/write operation via the SCU. All locking is
563 * handled for the caller. This function may sleep.
565 * Returns an error code or 0 on success.
567 * This has to be in the IPC driver for the locking.
569 int intel_scu_ipc_i2c_cntrl(u32 addr
, u32
*data
)
571 struct intel_scu_ipc_dev
*scu
= &ipcdev
;
574 mutex_lock(&ipclock
);
575 if (scu
->dev
== NULL
) {
576 mutex_unlock(&ipclock
);
579 cmd
= (addr
>> 24) & 0xFF;
580 if (cmd
== IPC_I2C_READ
) {
581 writel(addr
, scu
->i2c_base
+ IPC_I2C_CNTRL_ADDR
);
582 /* Write not getting updated without delay */
583 usleep_range(1000, 2000);
584 *data
= readl(scu
->i2c_base
+ I2C_DATA_ADDR
);
585 } else if (cmd
== IPC_I2C_WRITE
) {
586 writel(*data
, scu
->i2c_base
+ I2C_DATA_ADDR
);
587 usleep_range(1000, 2000);
588 writel(addr
, scu
->i2c_base
+ IPC_I2C_CNTRL_ADDR
);
591 "intel_scu_ipc: I2C INVALID_CMD = 0x%x\n", cmd
);
593 mutex_unlock(&ipclock
);
596 mutex_unlock(&ipclock
);
599 EXPORT_SYMBOL(intel_scu_ipc_i2c_cntrl
);
602 * Interrupt handler gets called when ioc bit of IPC_COMMAND_REG set to 1
603 * When ioc bit is set to 1, caller api must wait for interrupt handler called
604 * which in turn unlocks the caller api. Currently this is not used
606 * This is edge triggered so we need take no action to clear anything
608 static irqreturn_t
ioc(int irq
, void *dev_id
)
610 struct intel_scu_ipc_dev
*scu
= dev_id
;
611 int status
= ipc_read_status(scu
);
613 writel(status
| IPC_STATUS_IRQ
, scu
->ipc_base
+ IPC_STATUS
);
614 complete(&scu
->cmd_complete
);
620 * ipc_probe - probe an Intel SCU IPC
621 * @pdev: the PCI device matching
622 * @id: entry in the match table
624 * Enable and install an intel SCU IPC. This appears in the PCI space
625 * but uses some hard coded addresses as well.
627 static int ipc_probe(struct pci_dev
*pdev
, const struct pci_device_id
*id
)
630 struct intel_scu_ipc_dev
*scu
= &ipcdev
;
631 struct intel_scu_ipc_pdata_t
*pdata
;
633 if (scu
->dev
) /* We support only one SCU */
636 pdata
= (struct intel_scu_ipc_pdata_t
*)id
->driver_data
;
640 err
= pcim_enable_device(pdev
);
644 err
= pcim_iomap_regions(pdev
, 1 << 0, pci_name(pdev
));
648 init_completion(&scu
->cmd_complete
);
650 scu
->ipc_base
= pcim_iomap_table(pdev
)[0];
652 scu
->i2c_base
= ioremap_nocache(pdata
->i2c_base
, pdata
->i2c_len
);
656 err
= devm_request_irq(&pdev
->dev
, pdev
->irq
, ioc
, 0, "intel_scu_ipc",
661 /* Assign device at last */
662 scu
->dev
= &pdev
->dev
;
664 intel_scu_devices_create();
666 pci_set_drvdata(pdev
, scu
);
670 #define SCU_DEVICE(id, pdata) {PCI_VDEVICE(INTEL, id), (kernel_ulong_t)&pdata}
672 static const struct pci_device_id pci_ids
[] = {
673 SCU_DEVICE(PCI_DEVICE_ID_LINCROFT
, intel_scu_ipc_lincroft_pdata
),
674 SCU_DEVICE(PCI_DEVICE_ID_PENWELL
, intel_scu_ipc_penwell_pdata
),
675 SCU_DEVICE(PCI_DEVICE_ID_CLOVERVIEW
, intel_scu_ipc_penwell_pdata
),
676 SCU_DEVICE(PCI_DEVICE_ID_TANGIER
, intel_scu_ipc_tangier_pdata
),
680 static struct pci_driver ipc_driver
= {
682 .suppress_bind_attrs
= true,
684 .name
= "intel_scu_ipc",
688 builtin_pci_driver(ipc_driver
);