blob: 60ea476a91305c8f357f1950064cee54e92f0f29 [file] [log] [blame]
Sreedhara DS9a58a332010-04-26 18:13:05 +01001/*
2 * intel_scu_ipc.c: Driver for the Intel SCU IPC mechanism
3 *
4 * (C) Copyright 2008-2010 Intel Corporation
5 * Author: Sreedhara DS (sreedhara.ds@intel.com)
6 *
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
10 * of the License.
11 *
Lucas De Marchic8440332011-03-17 17:18:22 -030012 * SCU running in ARC processor communicates with other entity running in IA
Sreedhara DS9a58a332010-04-26 18:13:05 +010013 * 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.
18 */
19#include <linux/delay.h>
20#include <linux/errno.h>
21#include <linux/init.h>
Kay Sieversedbaa602011-12-21 16:26:03 -080022#include <linux/device.h>
Sreedhara DS9a58a332010-04-26 18:13:05 +010023#include <linux/pm.h>
24#include <linux/pci.h>
25#include <linux/interrupt.h>
Alan Cox209009b2010-09-13 15:55:05 +010026#include <linux/sfi.h>
Paul Gortmaker7c52d552011-05-27 12:33:10 -040027#include <linux/module.h>
Kuppuswamy Sathyanarayanan05454c22013-10-17 15:35:27 -070028#include <asm/intel-mid.h>
Sreedhara DS9a58a332010-04-26 18:13:05 +010029#include <asm/intel_scu_ipc.h>
30
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 */
37
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 */
42
Sreedhara DS9a58a332010-04-26 18:13:05 +010043/*
44 * IPC register summary
45 *
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
49 * mechanism
50 *
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.
53 *
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
56 *
57 * 3. SCU firmware decodes this interrupt and IPC message and the appropriate
58 * message handler is called within firmware.
59 */
60
Arjan van de Ven51cd5252010-07-26 10:04:24 +010061#define IPC_WWBUF_SIZE 20 /* IPC Write buffer Size */
62#define IPC_RWBUF_SIZE 20 /* IPC Read buffer Size */
Kuppuswamy Sathyanarayananed12f292013-11-15 16:21:54 -080063#define IPC_IOC 0x100 /* IPC command register IOC bit */
Kuppuswamy Sathyanarayanane97a1c92013-11-14 14:15:04 -080064
65enum {
66 SCU_IPC_LINCROFT,
Kuppuswamy Sathyanarayanan7f95afb2013-11-14 14:15:05 -080067 SCU_IPC_PENWELL,
68 SCU_IPC_CLOVERVIEW,
69 SCU_IPC_TANGIER,
Kuppuswamy Sathyanarayanane97a1c92013-11-14 14:15:04 -080070};
71
72/* intel scu ipc driver data*/
73struct intel_scu_ipc_pdata_t {
74 u32 ipc_base;
75 u32 i2c_base;
76 u32 ipc_len;
77 u32 i2c_len;
Kuppuswamy Sathyanarayananed12f292013-11-15 16:21:54 -080078 u8 irq_mode;
Kuppuswamy Sathyanarayanane97a1c92013-11-14 14:15:04 -080079};
80
81static struct intel_scu_ipc_pdata_t intel_scu_ipc_pdata[] = {
82 [SCU_IPC_LINCROFT] = {
83 .ipc_base = 0xff11c000,
84 .i2c_base = 0xff12b000,
85 .ipc_len = 0x100,
86 .i2c_len = 0x10,
Kuppuswamy Sathyanarayananed12f292013-11-15 16:21:54 -080087 .irq_mode = 0,
Kuppuswamy Sathyanarayanane97a1c92013-11-14 14:15:04 -080088 },
Kuppuswamy Sathyanarayanan7f95afb2013-11-14 14:15:05 -080089 [SCU_IPC_PENWELL] = {
90 .ipc_base = 0xff11c000,
91 .i2c_base = 0xff12b000,
92 .ipc_len = 0x100,
93 .i2c_len = 0x10,
Kuppuswamy Sathyanarayananed12f292013-11-15 16:21:54 -080094 .irq_mode = 1,
Kuppuswamy Sathyanarayanan7f95afb2013-11-14 14:15:05 -080095 },
96 [SCU_IPC_CLOVERVIEW] = {
97 .ipc_base = 0xff11c000,
98 .i2c_base = 0xff12b000,
99 .ipc_len = 0x100,
100 .i2c_len = 0x10,
Kuppuswamy Sathyanarayananed12f292013-11-15 16:21:54 -0800101 .irq_mode = 1,
Kuppuswamy Sathyanarayanan7f95afb2013-11-14 14:15:05 -0800102 },
103 [SCU_IPC_TANGIER] = {
104 .ipc_base = 0xff009000,
105 .i2c_base = 0xff00d000,
106 .ipc_len = 0x100,
107 .i2c_len = 0x10,
Kuppuswamy Sathyanarayananed12f292013-11-15 16:21:54 -0800108 .irq_mode = 0,
Kuppuswamy Sathyanarayanan7f95afb2013-11-14 14:15:05 -0800109 },
Kuppuswamy Sathyanarayanane97a1c92013-11-14 14:15:04 -0800110};
Sreedhara DS9a58a332010-04-26 18:13:05 +0100111
112static int ipc_probe(struct pci_dev *dev, const struct pci_device_id *id);
113static void ipc_remove(struct pci_dev *pdev);
114
115struct intel_scu_ipc_dev {
116 struct pci_dev *pdev;
117 void __iomem *ipc_base;
118 void __iomem *i2c_base;
Kuppuswamy Sathyanarayananed12f292013-11-15 16:21:54 -0800119 struct completion cmd_complete;
120 u8 irq_mode;
Sreedhara DS9a58a332010-04-26 18:13:05 +0100121};
122
123static struct intel_scu_ipc_dev ipcdev; /* Only one for now */
124
Sreedhara DS14d10f02010-07-26 10:02:25 +0100125static int platform; /* Platform type */
Sreedhara DS9a58a332010-04-26 18:13:05 +0100126
127/*
128 * IPC Read Buffer (Read Only):
129 * 16 byte buffer for receiving data from SCU, if IPC command
130 * processing results in response data
131 */
132#define IPC_READ_BUFFER 0x90
133
134#define IPC_I2C_CNTRL_ADDR 0
135#define I2C_DATA_ADDR 0x04
136
137static DEFINE_MUTEX(ipclock); /* lock used to prevent multiple call to SCU */
138
139/*
140 * Command Register (Write Only):
141 * A write to this register results in an interrupt to the SCU core processor
142 * Format:
143 * |rfu2(8) | size(8) | command id(4) | rfu1(3) | ioc(1) | command(8)|
144 */
145static inline void ipc_command(u32 cmd) /* Send ipc command */
146{
Kuppuswamy Sathyanarayananed12f292013-11-15 16:21:54 -0800147 if (ipcdev.irq_mode) {
148 reinit_completion(&ipcdev.cmd_complete);
149 writel(cmd | IPC_IOC, ipcdev.ipc_base);
150 }
Sreedhara DS9a58a332010-04-26 18:13:05 +0100151 writel(cmd, ipcdev.ipc_base);
152}
153
154/*
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
158 */
159static inline void ipc_data_writel(u32 data, u32 offset) /* Write ipc data */
160{
161 writel(data, ipcdev.ipc_base + 0x80 + offset);
162}
163
164/*
Sreedhara DS9a58a332010-04-26 18:13:05 +0100165 * 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
168 * Format:
169 * |rfu3(8)|error code(8)|initiator id(8)|cmd id(4)|rfu1(2)|error(1)|busy(1)|
170 */
171
172static inline u8 ipc_read_status(void)
173{
174 return __raw_readl(ipcdev.ipc_base + 0x04);
175}
176
177static inline u8 ipc_data_readb(u32 offset) /* Read ipc byte data */
178{
179 return readb(ipcdev.ipc_base + IPC_READ_BUFFER + offset);
180}
181
Sreedhara DSe3359fd2010-07-26 10:02:46 +0100182static inline u32 ipc_data_readl(u32 offset) /* Read ipc u32 data */
Sreedhara DS9a58a332010-04-26 18:13:05 +0100183{
184 return readl(ipcdev.ipc_base + IPC_READ_BUFFER + offset);
185}
186
187static inline int busy_loop(void) /* Wait till scu status is busy */
188{
189 u32 status = 0;
190 u32 loop_count = 0;
191
192 status = ipc_read_status();
193 while (status & 1) {
194 udelay(1); /* scu processing time is in few u secods */
195 status = ipc_read_status();
196 loop_count++;
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");
200 return -ETIMEDOUT;
201 }
202 }
Hong Liu77e01d62010-07-26 10:06:12 +0100203 if ((status >> 1) & 1)
204 return -EIO;
205
206 return 0;
Sreedhara DS9a58a332010-04-26 18:13:05 +0100207}
208
Kuppuswamy Sathyanarayananed12f292013-11-15 16:21:54 -0800209/* Wait till ipc ioc interrupt is received or timeout in 3 HZ */
210static inline int ipc_wait_for_interrupt(void)
211{
212 int status;
213
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");
217 return -ETIMEDOUT;
218 }
219
220 status = ipc_read_status();
221
222 if ((status >> 1) & 1)
223 return -EIO;
224
225 return 0;
226}
227
228int intel_scu_ipc_check_status(void)
229{
230 return ipcdev.irq_mode ? ipc_wait_for_interrupt() : busy_loop();
231}
232
Sreedhara DS9a58a332010-04-26 18:13:05 +0100233/* Read/Write power control(PMIC in Langwell, MSIC in PenWell) registers */
234static int pwr_reg_rdwr(u16 *addr, u8 *data, u32 count, u32 op, u32 id)
235{
Alan Cox47073752012-03-05 15:01:02 -0800236 int nc;
Sreedhara DS9a58a332010-04-26 18:13:05 +0100237 u32 offset = 0;
Axel Linecb56462011-01-25 14:12:12 +0000238 int err;
Sreedhara DSe3359fd2010-07-26 10:02:46 +0100239 u8 cbuf[IPC_WWBUF_SIZE] = { };
Sreedhara DS9a58a332010-04-26 18:13:05 +0100240 u32 *wbuf = (u32 *)&cbuf;
241
242 mutex_lock(&ipclock);
Sreedhara DSe3359fd2010-07-26 10:02:46 +0100243
Arjan van de Vened6f2b42010-07-26 10:04:37 +0100244 memset(cbuf, 0, sizeof(cbuf));
245
Sreedhara DS9a58a332010-04-26 18:13:05 +0100246 if (ipcdev.pdev == NULL) {
247 mutex_unlock(&ipclock);
248 return -ENODEV;
249 }
250
Alan Cox47073752012-03-05 15:01:02 -0800251 for (nc = 0; nc < count; nc++, offset += 2) {
252 cbuf[offset] = addr[nc];
253 cbuf[offset + 1] = addr[nc] >> 8;
254 }
Sreedhara DSe3359fd2010-07-26 10:02:46 +0100255
Alan Cox47073752012-03-05 15:01:02 -0800256 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);
Sreedhara DS9a58a332010-04-26 18:13:05 +0100271 }
272
Kuppuswamy Sathyanarayananed12f292013-11-15 16:21:54 -0800273 err = intel_scu_ipc_check_status();
Kuppuswamy Sathyanarayananc7094d12013-11-14 14:15:06 -0800274 if (!err && id == IPC_CMD_PCNTRL_R) { /* Read rbuf */
Sreedhara DS9a58a332010-04-26 18:13:05 +0100275 /* Workaround: values are read as 0 without memcpy_fromio */
Sreedhara DSe3359fd2010-07-26 10:02:46 +0100276 memcpy_fromio(cbuf, ipcdev.ipc_base + 0x90, 16);
Alan Cox47073752012-03-05 15:01:02 -0800277 for (nc = 0; nc < count; nc++)
278 data[nc] = ipc_data_readb(nc);
Sreedhara DS9a58a332010-04-26 18:13:05 +0100279 }
280 mutex_unlock(&ipclock);
281 return err;
282}
283
284/**
285 * intel_scu_ipc_ioread8 - read a word via the SCU
286 * @addr: register on SCU
287 * @data: return pointer for read byte
288 *
289 * Read a single register. Returns 0 on success or an error code. All
290 * locking between SCU accesses is handled for the caller.
291 *
292 * This function may sleep.
293 */
294int intel_scu_ipc_ioread8(u16 addr, u8 *data)
295{
296 return pwr_reg_rdwr(&addr, data, 1, IPCMSG_PCNTRL, IPC_CMD_PCNTRL_R);
297}
298EXPORT_SYMBOL(intel_scu_ipc_ioread8);
299
300/**
301 * intel_scu_ipc_ioread16 - read a word via the SCU
302 * @addr: register on SCU
303 * @data: return pointer for read word
304 *
305 * Read a register pair. Returns 0 on success or an error code. All
306 * locking between SCU accesses is handled for the caller.
307 *
308 * This function may sleep.
309 */
310int intel_scu_ipc_ioread16(u16 addr, u16 *data)
311{
312 u16 x[2] = {addr, addr + 1 };
313 return pwr_reg_rdwr(x, (u8 *)data, 2, IPCMSG_PCNTRL, IPC_CMD_PCNTRL_R);
314}
315EXPORT_SYMBOL(intel_scu_ipc_ioread16);
316
317/**
318 * intel_scu_ipc_ioread32 - read a dword via the SCU
319 * @addr: register on SCU
320 * @data: return pointer for read dword
321 *
322 * Read four registers. Returns 0 on success or an error code. All
323 * locking between SCU accesses is handled for the caller.
324 *
325 * This function may sleep.
326 */
327int intel_scu_ipc_ioread32(u16 addr, u32 *data)
328{
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);
331}
332EXPORT_SYMBOL(intel_scu_ipc_ioread32);
333
334/**
335 * intel_scu_ipc_iowrite8 - write a byte via the SCU
336 * @addr: register on SCU
337 * @data: byte to write
338 *
339 * Write a single register. Returns 0 on success or an error code. All
340 * locking between SCU accesses is handled for the caller.
341 *
342 * This function may sleep.
343 */
344int intel_scu_ipc_iowrite8(u16 addr, u8 data)
345{
346 return pwr_reg_rdwr(&addr, &data, 1, IPCMSG_PCNTRL, IPC_CMD_PCNTRL_W);
347}
348EXPORT_SYMBOL(intel_scu_ipc_iowrite8);
349
350/**
351 * intel_scu_ipc_iowrite16 - write a word via the SCU
352 * @addr: register on SCU
353 * @data: word to write
354 *
355 * Write two registers. Returns 0 on success or an error code. All
356 * locking between SCU accesses is handled for the caller.
357 *
358 * This function may sleep.
359 */
360int intel_scu_ipc_iowrite16(u16 addr, u16 data)
361{
362 u16 x[2] = {addr, addr + 1 };
363 return pwr_reg_rdwr(x, (u8 *)&data, 2, IPCMSG_PCNTRL, IPC_CMD_PCNTRL_W);
364}
365EXPORT_SYMBOL(intel_scu_ipc_iowrite16);
366
367/**
368 * intel_scu_ipc_iowrite32 - write a dword via the SCU
369 * @addr: register on SCU
370 * @data: dword to write
371 *
372 * Write four registers. Returns 0 on success or an error code. All
373 * locking between SCU accesses is handled for the caller.
374 *
375 * This function may sleep.
376 */
377int intel_scu_ipc_iowrite32(u16 addr, u32 data)
378{
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);
381}
382EXPORT_SYMBOL(intel_scu_ipc_iowrite32);
383
384/**
385 * intel_scu_ipc_readvv - read a set of registers
386 * @addr: register list
387 * @data: bytes to return
388 * @len: length of array
389 *
390 * Read registers. Returns 0 on success or an error code. All
391 * locking between SCU accesses is handled for the caller.
392 *
393 * The largest array length permitted by the hardware is 5 items.
394 *
395 * This function may sleep.
396 */
397int intel_scu_ipc_readv(u16 *addr, u8 *data, int len)
398{
399 return pwr_reg_rdwr(addr, data, len, IPCMSG_PCNTRL, IPC_CMD_PCNTRL_R);
400}
401EXPORT_SYMBOL(intel_scu_ipc_readv);
402
403/**
404 * intel_scu_ipc_writev - write a set of registers
405 * @addr: register list
406 * @data: bytes to write
407 * @len: length of array
408 *
409 * Write registers. Returns 0 on success or an error code. All
410 * locking between SCU accesses is handled for the caller.
411 *
412 * The largest array length permitted by the hardware is 5 items.
413 *
414 * This function may sleep.
415 *
416 */
417int intel_scu_ipc_writev(u16 *addr, u8 *data, int len)
418{
419 return pwr_reg_rdwr(addr, data, len, IPCMSG_PCNTRL, IPC_CMD_PCNTRL_W);
420}
421EXPORT_SYMBOL(intel_scu_ipc_writev);
422
423
424/**
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
429 *
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.
435 *
436 * This function may sleep. Locking between SCU accesses is handled
437 * for the caller.
438 */
439int intel_scu_ipc_update_register(u16 addr, u8 bits, u8 mask)
440{
441 u8 data[2] = { bits, mask };
442 return pwr_reg_rdwr(&addr, data, 1, IPCMSG_PCNTRL, IPC_CMD_PCNTRL_M);
443}
444EXPORT_SYMBOL(intel_scu_ipc_update_register);
445
446/**
Sreedhara DS9a58a332010-04-26 18:13:05 +0100447 * intel_scu_ipc_simple_command - send a simple command
448 * @cmd: command
449 * @sub: sub type
450 *
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.
454 *
455 * This function may sleep. Locking for SCU accesses is handled for
456 * the caller.
457 */
458int intel_scu_ipc_simple_command(int cmd, int sub)
459{
Axel Linecb56462011-01-25 14:12:12 +0000460 int err;
Sreedhara DS9a58a332010-04-26 18:13:05 +0100461
462 mutex_lock(&ipclock);
463 if (ipcdev.pdev == NULL) {
464 mutex_unlock(&ipclock);
465 return -ENODEV;
466 }
Sreedhara DSb4fd4f82010-07-19 09:37:42 +0100467 ipc_command(sub << 12 | cmd);
Kuppuswamy Sathyanarayananed12f292013-11-15 16:21:54 -0800468 err = intel_scu_ipc_check_status();
Sreedhara DS9a58a332010-04-26 18:13:05 +0100469 mutex_unlock(&ipclock);
470 return err;
471}
472EXPORT_SYMBOL(intel_scu_ipc_simple_command);
473
474/**
475 * intel_scu_ipc_command - command with data
476 * @cmd: command
477 * @sub: sub type
478 * @in: input data
Sreedhara DSb4fd4f82010-07-19 09:37:42 +0100479 * @inlen: input length in dwords
Sreedhara DS9a58a332010-04-26 18:13:05 +0100480 * @out: output data
Sreedhara DSb4fd4f82010-07-19 09:37:42 +0100481 * @outlein: output length in dwords
Sreedhara DS9a58a332010-04-26 18:13:05 +0100482 *
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
485 */
486
487int intel_scu_ipc_command(int cmd, int sub, u32 *in, int inlen,
488 u32 *out, int outlen)
489{
Axel Linecb56462011-01-25 14:12:12 +0000490 int i, err;
Sreedhara DS9a58a332010-04-26 18:13:05 +0100491
492 mutex_lock(&ipclock);
493 if (ipcdev.pdev == NULL) {
494 mutex_unlock(&ipclock);
495 return -ENODEV;
496 }
497
498 for (i = 0; i < inlen; i++)
499 ipc_data_writel(*in++, 4 * i);
500
Hong Liu5aa06932010-07-26 10:06:31 +0100501 ipc_command((inlen << 16) | (sub << 12) | cmd);
Kuppuswamy Sathyanarayananed12f292013-11-15 16:21:54 -0800502 err = intel_scu_ipc_check_status();
Sreedhara DS9a58a332010-04-26 18:13:05 +0100503
Kuppuswamy Sathyanarayananc7094d12013-11-14 14:15:06 -0800504 if (!err) {
505 for (i = 0; i < outlen; i++)
506 *out++ = ipc_data_readl(4 * i);
507 }
Sreedhara DS9a58a332010-04-26 18:13:05 +0100508
509 mutex_unlock(&ipclock);
510 return err;
511}
512EXPORT_SYMBOL(intel_scu_ipc_command);
513
514/*I2C commands */
515#define IPC_I2C_WRITE 1 /* I2C Write command */
516#define IPC_I2C_READ 2 /* I2C Read command */
517
518/**
519 * intel_scu_ipc_i2c_cntrl - I2C read/write operations
520 * @addr: I2C address + command bits
521 * @data: data to read/write
522 *
523 * Perform an an I2C read/write operation via the SCU. All locking is
524 * handled for the caller. This function may sleep.
525 *
526 * Returns an error code or 0 on success.
527 *
528 * This has to be in the IPC driver for the locking.
529 */
530int intel_scu_ipc_i2c_cntrl(u32 addr, u32 *data)
531{
532 u32 cmd = 0;
533
534 mutex_lock(&ipclock);
Sreedhara DSb4fd4f82010-07-19 09:37:42 +0100535 if (ipcdev.pdev == NULL) {
536 mutex_unlock(&ipclock);
537 return -ENODEV;
538 }
Sreedhara DS9a58a332010-04-26 18:13:05 +0100539 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 */
543 mdelay(1);
544 *data = readl(ipcdev.i2c_base + I2C_DATA_ADDR);
545 } else if (cmd == IPC_I2C_WRITE) {
Jianwei Yang32e2f632010-08-24 14:32:38 +0100546 writel(*data, ipcdev.i2c_base + I2C_DATA_ADDR);
Sreedhara DS9a58a332010-04-26 18:13:05 +0100547 mdelay(1);
548 writel(addr, ipcdev.i2c_base + IPC_I2C_CNTRL_ADDR);
549 } else {
550 dev_err(&ipcdev.pdev->dev,
551 "intel_scu_ipc: I2C INVALID_CMD = 0x%x\n", cmd);
552
553 mutex_unlock(&ipclock);
Sreedhara DS5369c02d2010-10-22 15:43:55 +0100554 return -EIO;
Sreedhara DS9a58a332010-04-26 18:13:05 +0100555 }
556 mutex_unlock(&ipclock);
557 return 0;
558}
559EXPORT_SYMBOL(intel_scu_ipc_i2c_cntrl);
560
Sreedhara DS9a58a332010-04-26 18:13:05 +0100561/*
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
565 *
566 * This is edge triggered so we need take no action to clear anything
567 */
568static irqreturn_t ioc(int irq, void *dev_id)
569{
Kuppuswamy Sathyanarayananed12f292013-11-15 16:21:54 -0800570 if (ipcdev.irq_mode)
571 complete(&ipcdev.cmd_complete);
572
Sreedhara DS9a58a332010-04-26 18:13:05 +0100573 return IRQ_HANDLED;
574}
575
576/**
577 * ipc_probe - probe an Intel SCU IPC
578 * @dev: the PCI device matching
579 * @id: entry in the match table
580 *
581 * Enable and install an intel SCU IPC. This appears in the PCI space
582 * but uses some hard coded addresses as well.
583 */
584static int ipc_probe(struct pci_dev *dev, const struct pci_device_id *id)
585{
Kuppuswamy Sathyanarayanane97a1c92013-11-14 14:15:04 -0800586 int err, pid;
587 struct intel_scu_ipc_pdata_t *pdata;
Sreedhara DS9a58a332010-04-26 18:13:05 +0100588 resource_size_t pci_resource;
589
590 if (ipcdev.pdev) /* We support only one SCU */
591 return -EBUSY;
592
Kuppuswamy Sathyanarayanane97a1c92013-11-14 14:15:04 -0800593 pid = id->driver_data;
594 pdata = &intel_scu_ipc_pdata[pid];
595
Sreedhara DS9a58a332010-04-26 18:13:05 +0100596 ipcdev.pdev = pci_dev_get(dev);
Kuppuswamy Sathyanarayananed12f292013-11-15 16:21:54 -0800597 ipcdev.irq_mode = pdata->irq_mode;
Sreedhara DS9a58a332010-04-26 18:13:05 +0100598
599 err = pci_enable_device(dev);
600 if (err)
601 return err;
602
603 err = pci_request_regions(dev, "intel_scu_ipc");
604 if (err)
605 return err;
606
607 pci_resource = pci_resource_start(dev, 0);
608 if (!pci_resource)
609 return -ENOMEM;
610
Kuppuswamy Sathyanarayananed12f292013-11-15 16:21:54 -0800611 init_completion(&ipcdev.cmd_complete);
612
Sreedhara DS9a58a332010-04-26 18:13:05 +0100613 if (request_irq(dev->irq, ioc, 0, "intel_scu_ipc", &ipcdev))
614 return -EBUSY;
615
Kuppuswamy Sathyanarayanane97a1c92013-11-14 14:15:04 -0800616 ipcdev.ipc_base = ioremap_nocache(pdata->ipc_base, pdata->ipc_len);
Sreedhara DS9a58a332010-04-26 18:13:05 +0100617 if (!ipcdev.ipc_base)
618 return -ENOMEM;
619
Kuppuswamy Sathyanarayanane97a1c92013-11-14 14:15:04 -0800620 ipcdev.i2c_base = ioremap_nocache(pdata->i2c_base, pdata->i2c_len);
Sreedhara DS9a58a332010-04-26 18:13:05 +0100621 if (!ipcdev.i2c_base) {
622 iounmap(ipcdev.ipc_base);
623 return -ENOMEM;
624 }
Feng Tang1da4b1c2010-11-09 11:22:58 +0000625
626 intel_scu_devices_create();
627
Sreedhara DS9a58a332010-04-26 18:13:05 +0100628 return 0;
629}
630
631/**
632 * ipc_remove - remove a bound IPC device
633 * @pdev: PCI device
634 *
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.
638 *
639 * Free up the mappings and release the PCI resources
640 */
641static void ipc_remove(struct pci_dev *pdev)
642{
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);
648 ipcdev.pdev = NULL;
Feng Tang1da4b1c2010-11-09 11:22:58 +0000649 intel_scu_devices_destroy();
Sreedhara DS9a58a332010-04-26 18:13:05 +0100650}
651
Axel Lindaa77692011-07-07 10:22:46 +0800652static DEFINE_PCI_DEVICE_TABLE(pci_ids) = {
Kuppuswamy Sathyanarayanane97a1c92013-11-14 14:15:04 -0800653 {PCI_VDEVICE(INTEL, 0x082a), SCU_IPC_LINCROFT},
Kuppuswamy Sathyanarayanan7f95afb2013-11-14 14:15:05 -0800654 {PCI_VDEVICE(INTEL, 0x080e), SCU_IPC_PENWELL},
655 {PCI_VDEVICE(INTEL, 0x08ea), SCU_IPC_CLOVERVIEW},
656 {PCI_VDEVICE(INTEL, 0x11a0), SCU_IPC_TANGIER},
Sreedhara DS9a58a332010-04-26 18:13:05 +0100657 { 0,}
658};
659MODULE_DEVICE_TABLE(pci, pci_ids);
660
661static struct pci_driver ipc_driver = {
662 .name = "intel_scu_ipc",
663 .id_table = pci_ids,
664 .probe = ipc_probe,
665 .remove = ipc_remove,
666};
667
668
669static int __init intel_scu_ipc_init(void)
670{
Kuppuswamy Sathyanarayanan712b6aa2013-10-17 15:35:29 -0700671 platform = intel_mid_identify_cpu();
Alan Cox9dd3ade2010-07-26 10:03:58 +0100672 if (platform == 0)
673 return -ENODEV;
Sreedhara DS9a58a332010-04-26 18:13:05 +0100674 return pci_register_driver(&ipc_driver);
675}
676
677static void __exit intel_scu_ipc_exit(void)
678{
679 pci_unregister_driver(&ipc_driver);
680}
681
682MODULE_AUTHOR("Sreedhara DS <sreedhara.ds@intel.com>");
683MODULE_DESCRIPTION("Intel SCU IPC driver");
684MODULE_LICENSE("GPL");
685
686module_init(intel_scu_ipc_init);
687module_exit(intel_scu_ipc_exit);