blob: e6430a3454cffebadf68c3a6b87d7d1f0369a65f [file] [log] [blame]
Sreedhara DS9a58a332010-04-26 18:13:05 +01001/*
2 * intel_scu_ipc.c: Driver for the Intel SCU IPC mechanism
3 *
Andy Shevchenko7c2e3c72015-01-21 21:38:09 +02004 * (C) Copyright 2008-2010,2015 Intel Corporation
Sreedhara DS9a58a332010-04-26 18:13:05 +01005 * 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
David Cohenb4b0b4a2013-12-02 16:20:01 -080065#define PCI_DEVICE_ID_LINCROFT 0x082a
66#define PCI_DEVICE_ID_PENWELL 0x080e
67#define PCI_DEVICE_ID_CLOVERVIEW 0x08ea
68#define PCI_DEVICE_ID_TANGIER 0x11a0
69
Andy Shevchenko7c2e3c72015-01-21 21:38:09 +020070/* intel scu ipc driver data */
Kuppuswamy Sathyanarayanane97a1c92013-11-14 14:15:04 -080071struct intel_scu_ipc_pdata_t {
72 u32 ipc_base;
73 u32 i2c_base;
74 u32 ipc_len;
75 u32 i2c_len;
Kuppuswamy Sathyanarayananed12f292013-11-15 16:21:54 -080076 u8 irq_mode;
Kuppuswamy Sathyanarayanane97a1c92013-11-14 14:15:04 -080077};
78
David Cohen694e5232013-12-02 16:20:00 -080079static struct intel_scu_ipc_pdata_t intel_scu_ipc_lincroft_pdata = {
80 .ipc_base = 0xff11c000,
81 .i2c_base = 0xff12b000,
82 .ipc_len = 0x100,
83 .i2c_len = 0x10,
84 .irq_mode = 0,
85};
86
87/* Penwell and Cloverview */
88static struct intel_scu_ipc_pdata_t intel_scu_ipc_penwell_pdata = {
89 .ipc_base = 0xff11c000,
90 .i2c_base = 0xff12b000,
91 .ipc_len = 0x100,
92 .i2c_len = 0x10,
93 .irq_mode = 1,
94};
95
96static struct intel_scu_ipc_pdata_t intel_scu_ipc_tangier_pdata = {
97 .ipc_base = 0xff009000,
98 .i2c_base = 0xff00d000,
99 .ipc_len = 0x100,
100 .i2c_len = 0x10,
101 .irq_mode = 0,
Kuppuswamy Sathyanarayanane97a1c92013-11-14 14:15:04 -0800102};
Sreedhara DS9a58a332010-04-26 18:13:05 +0100103
104static int ipc_probe(struct pci_dev *dev, const struct pci_device_id *id);
105static void ipc_remove(struct pci_dev *pdev);
106
107struct intel_scu_ipc_dev {
108 struct pci_dev *pdev;
109 void __iomem *ipc_base;
110 void __iomem *i2c_base;
Kuppuswamy Sathyanarayananed12f292013-11-15 16:21:54 -0800111 struct completion cmd_complete;
112 u8 irq_mode;
Sreedhara DS9a58a332010-04-26 18:13:05 +0100113};
114
115static struct intel_scu_ipc_dev ipcdev; /* Only one for now */
116
Sreedhara DS9a58a332010-04-26 18:13:05 +0100117/*
118 * IPC Read Buffer (Read Only):
119 * 16 byte buffer for receiving data from SCU, if IPC command
120 * processing results in response data
121 */
122#define IPC_READ_BUFFER 0x90
123
124#define IPC_I2C_CNTRL_ADDR 0
125#define I2C_DATA_ADDR 0x04
126
127static DEFINE_MUTEX(ipclock); /* lock used to prevent multiple call to SCU */
128
129/*
130 * Command Register (Write Only):
131 * A write to this register results in an interrupt to the SCU core processor
132 * Format:
133 * |rfu2(8) | size(8) | command id(4) | rfu1(3) | ioc(1) | command(8)|
134 */
135static inline void ipc_command(u32 cmd) /* Send ipc command */
136{
Kuppuswamy Sathyanarayananed12f292013-11-15 16:21:54 -0800137 if (ipcdev.irq_mode) {
138 reinit_completion(&ipcdev.cmd_complete);
139 writel(cmd | IPC_IOC, ipcdev.ipc_base);
140 }
Sreedhara DS9a58a332010-04-26 18:13:05 +0100141 writel(cmd, ipcdev.ipc_base);
142}
143
144/*
145 * IPC Write Buffer (Write Only):
146 * 16-byte buffer for sending data associated with IPC command to
147 * SCU. Size of the data is specified in the IPC_COMMAND_REG register
148 */
149static inline void ipc_data_writel(u32 data, u32 offset) /* Write ipc data */
150{
151 writel(data, ipcdev.ipc_base + 0x80 + offset);
152}
153
154/*
Sreedhara DS9a58a332010-04-26 18:13:05 +0100155 * Status Register (Read Only):
156 * Driver will read this register to get the ready/busy status of the IPC
157 * block and error status of the IPC command that was just processed by SCU
158 * Format:
159 * |rfu3(8)|error code(8)|initiator id(8)|cmd id(4)|rfu1(2)|error(1)|busy(1)|
160 */
Sreedhara DS9a58a332010-04-26 18:13:05 +0100161static inline u8 ipc_read_status(void)
162{
163 return __raw_readl(ipcdev.ipc_base + 0x04);
164}
165
166static inline u8 ipc_data_readb(u32 offset) /* Read ipc byte data */
167{
168 return readb(ipcdev.ipc_base + IPC_READ_BUFFER + offset);
169}
170
Sreedhara DSe3359fd2010-07-26 10:02:46 +0100171static inline u32 ipc_data_readl(u32 offset) /* Read ipc u32 data */
Sreedhara DS9a58a332010-04-26 18:13:05 +0100172{
173 return readl(ipcdev.ipc_base + IPC_READ_BUFFER + offset);
174}
175
Andy Shevchenko7c2e3c72015-01-21 21:38:09 +0200176/* Wait till scu status is busy */
177static inline int busy_loop(void)
Sreedhara DS9a58a332010-04-26 18:13:05 +0100178{
Andy Shevchenkof0295a32015-01-21 21:38:10 +0200179 u32 status = ipc_read_status();
180 u32 loop_count = 100000;
Sreedhara DS9a58a332010-04-26 18:13:05 +0100181
Andy Shevchenkof0295a32015-01-21 21:38:10 +0200182 /* break if scu doesn't reset busy bit after huge retry */
183 while ((status & BIT(0)) && --loop_count) {
Sreedhara DS9a58a332010-04-26 18:13:05 +0100184 udelay(1); /* scu processing time is in few u secods */
185 status = ipc_read_status();
Sreedhara DS9a58a332010-04-26 18:13:05 +0100186 }
Andy Shevchenkof0295a32015-01-21 21:38:10 +0200187
188 if (status & BIT(0)) {
189 dev_err(&ipcdev.pdev->dev, "IPC timed out");
190 return -ETIMEDOUT;
191 }
192
193 if (status & BIT(1))
Hong Liu77e01d62010-07-26 10:06:12 +0100194 return -EIO;
195
196 return 0;
Sreedhara DS9a58a332010-04-26 18:13:05 +0100197}
198
Kuppuswamy Sathyanarayananed12f292013-11-15 16:21:54 -0800199/* Wait till ipc ioc interrupt is received or timeout in 3 HZ */
200static inline int ipc_wait_for_interrupt(void)
201{
202 int status;
203
204 if (!wait_for_completion_timeout(&ipcdev.cmd_complete, 3 * HZ)) {
205 struct device *dev = &ipcdev.pdev->dev;
206 dev_err(dev, "IPC timed out\n");
207 return -ETIMEDOUT;
208 }
209
210 status = ipc_read_status();
Andy Shevchenkof0295a32015-01-21 21:38:10 +0200211 if (status & BIT(1))
Kuppuswamy Sathyanarayananed12f292013-11-15 16:21:54 -0800212 return -EIO;
213
214 return 0;
215}
216
Andy Shevchenko7c2e3c72015-01-21 21:38:09 +0200217static int intel_scu_ipc_check_status(void)
Kuppuswamy Sathyanarayananed12f292013-11-15 16:21:54 -0800218{
219 return ipcdev.irq_mode ? ipc_wait_for_interrupt() : busy_loop();
220}
221
Sreedhara DS9a58a332010-04-26 18:13:05 +0100222/* Read/Write power control(PMIC in Langwell, MSIC in PenWell) registers */
223static int pwr_reg_rdwr(u16 *addr, u8 *data, u32 count, u32 op, u32 id)
224{
Alan Cox47073752012-03-05 15:01:02 -0800225 int nc;
Sreedhara DS9a58a332010-04-26 18:13:05 +0100226 u32 offset = 0;
Axel Linecb56462011-01-25 14:12:12 +0000227 int err;
Sreedhara DSe3359fd2010-07-26 10:02:46 +0100228 u8 cbuf[IPC_WWBUF_SIZE] = { };
Sreedhara DS9a58a332010-04-26 18:13:05 +0100229 u32 *wbuf = (u32 *)&cbuf;
230
231 mutex_lock(&ipclock);
Sreedhara DSe3359fd2010-07-26 10:02:46 +0100232
Arjan van de Vened6f2b42010-07-26 10:04:37 +0100233 memset(cbuf, 0, sizeof(cbuf));
234
Sreedhara DS9a58a332010-04-26 18:13:05 +0100235 if (ipcdev.pdev == NULL) {
236 mutex_unlock(&ipclock);
237 return -ENODEV;
238 }
239
Alan Cox47073752012-03-05 15:01:02 -0800240 for (nc = 0; nc < count; nc++, offset += 2) {
241 cbuf[offset] = addr[nc];
242 cbuf[offset + 1] = addr[nc] >> 8;
243 }
Sreedhara DSe3359fd2010-07-26 10:02:46 +0100244
Alan Cox47073752012-03-05 15:01:02 -0800245 if (id == IPC_CMD_PCNTRL_R) {
246 for (nc = 0, offset = 0; nc < count; nc++, offset += 4)
247 ipc_data_writel(wbuf[nc], offset);
Andy Shevchenko7c2e3c72015-01-21 21:38:09 +0200248 ipc_command((count * 2) << 16 | id << 12 | 0 << 8 | op);
Alan Cox47073752012-03-05 15:01:02 -0800249 } else if (id == IPC_CMD_PCNTRL_W) {
250 for (nc = 0; nc < count; nc++, offset += 1)
251 cbuf[offset] = data[nc];
252 for (nc = 0, offset = 0; nc < count; nc++, offset += 4)
253 ipc_data_writel(wbuf[nc], offset);
Andy Shevchenko7c2e3c72015-01-21 21:38:09 +0200254 ipc_command((count * 3) << 16 | id << 12 | 0 << 8 | op);
Alan Cox47073752012-03-05 15:01:02 -0800255 } else if (id == IPC_CMD_PCNTRL_M) {
256 cbuf[offset] = data[0];
257 cbuf[offset + 1] = data[1];
258 ipc_data_writel(wbuf[0], 0); /* Write wbuff */
Andy Shevchenko7c2e3c72015-01-21 21:38:09 +0200259 ipc_command(4 << 16 | id << 12 | 0 << 8 | op);
Sreedhara DS9a58a332010-04-26 18:13:05 +0100260 }
261
Kuppuswamy Sathyanarayananed12f292013-11-15 16:21:54 -0800262 err = intel_scu_ipc_check_status();
Kuppuswamy Sathyanarayananc7094d12013-11-14 14:15:06 -0800263 if (!err && id == IPC_CMD_PCNTRL_R) { /* Read rbuf */
Sreedhara DS9a58a332010-04-26 18:13:05 +0100264 /* Workaround: values are read as 0 without memcpy_fromio */
Sreedhara DSe3359fd2010-07-26 10:02:46 +0100265 memcpy_fromio(cbuf, ipcdev.ipc_base + 0x90, 16);
Alan Cox47073752012-03-05 15:01:02 -0800266 for (nc = 0; nc < count; nc++)
267 data[nc] = ipc_data_readb(nc);
Sreedhara DS9a58a332010-04-26 18:13:05 +0100268 }
269 mutex_unlock(&ipclock);
270 return err;
271}
272
273/**
274 * intel_scu_ipc_ioread8 - read a word via the SCU
275 * @addr: register on SCU
276 * @data: return pointer for read byte
277 *
278 * Read a single register. Returns 0 on success or an error code. All
279 * locking between SCU accesses is handled for the caller.
280 *
281 * This function may sleep.
282 */
283int intel_scu_ipc_ioread8(u16 addr, u8 *data)
284{
285 return pwr_reg_rdwr(&addr, data, 1, IPCMSG_PCNTRL, IPC_CMD_PCNTRL_R);
286}
287EXPORT_SYMBOL(intel_scu_ipc_ioread8);
288
289/**
290 * intel_scu_ipc_ioread16 - read a word via the SCU
291 * @addr: register on SCU
292 * @data: return pointer for read word
293 *
294 * Read a register pair. Returns 0 on success or an error code. All
295 * locking between SCU accesses is handled for the caller.
296 *
297 * This function may sleep.
298 */
299int intel_scu_ipc_ioread16(u16 addr, u16 *data)
300{
Andy Shevchenko7c2e3c72015-01-21 21:38:09 +0200301 u16 x[2] = {addr, addr + 1};
Sreedhara DS9a58a332010-04-26 18:13:05 +0100302 return pwr_reg_rdwr(x, (u8 *)data, 2, IPCMSG_PCNTRL, IPC_CMD_PCNTRL_R);
303}
304EXPORT_SYMBOL(intel_scu_ipc_ioread16);
305
306/**
307 * intel_scu_ipc_ioread32 - read a dword via the SCU
308 * @addr: register on SCU
309 * @data: return pointer for read dword
310 *
311 * Read four registers. Returns 0 on success or an error code. All
312 * locking between SCU accesses is handled for the caller.
313 *
314 * This function may sleep.
315 */
316int intel_scu_ipc_ioread32(u16 addr, u32 *data)
317{
318 u16 x[4] = {addr, addr + 1, addr + 2, addr + 3};
319 return pwr_reg_rdwr(x, (u8 *)data, 4, IPCMSG_PCNTRL, IPC_CMD_PCNTRL_R);
320}
321EXPORT_SYMBOL(intel_scu_ipc_ioread32);
322
323/**
324 * intel_scu_ipc_iowrite8 - write a byte via the SCU
325 * @addr: register on SCU
326 * @data: byte to write
327 *
328 * Write a single register. Returns 0 on success or an error code. All
329 * locking between SCU accesses is handled for the caller.
330 *
331 * This function may sleep.
332 */
333int intel_scu_ipc_iowrite8(u16 addr, u8 data)
334{
335 return pwr_reg_rdwr(&addr, &data, 1, IPCMSG_PCNTRL, IPC_CMD_PCNTRL_W);
336}
337EXPORT_SYMBOL(intel_scu_ipc_iowrite8);
338
339/**
340 * intel_scu_ipc_iowrite16 - write a word via the SCU
341 * @addr: register on SCU
342 * @data: word to write
343 *
344 * Write two registers. Returns 0 on success or an error code. All
345 * locking between SCU accesses is handled for the caller.
346 *
347 * This function may sleep.
348 */
349int intel_scu_ipc_iowrite16(u16 addr, u16 data)
350{
Andy Shevchenko7c2e3c72015-01-21 21:38:09 +0200351 u16 x[2] = {addr, addr + 1};
Sreedhara DS9a58a332010-04-26 18:13:05 +0100352 return pwr_reg_rdwr(x, (u8 *)&data, 2, IPCMSG_PCNTRL, IPC_CMD_PCNTRL_W);
353}
354EXPORT_SYMBOL(intel_scu_ipc_iowrite16);
355
356/**
357 * intel_scu_ipc_iowrite32 - write a dword via the SCU
358 * @addr: register on SCU
359 * @data: dword to write
360 *
361 * Write four registers. Returns 0 on success or an error code. All
362 * locking between SCU accesses is handled for the caller.
363 *
364 * This function may sleep.
365 */
366int intel_scu_ipc_iowrite32(u16 addr, u32 data)
367{
368 u16 x[4] = {addr, addr + 1, addr + 2, addr + 3};
369 return pwr_reg_rdwr(x, (u8 *)&data, 4, IPCMSG_PCNTRL, IPC_CMD_PCNTRL_W);
370}
371EXPORT_SYMBOL(intel_scu_ipc_iowrite32);
372
373/**
374 * intel_scu_ipc_readvv - read a set of registers
375 * @addr: register list
376 * @data: bytes to return
377 * @len: length of array
378 *
379 * Read registers. Returns 0 on success or an error code. All
380 * locking between SCU accesses is handled for the caller.
381 *
382 * The largest array length permitted by the hardware is 5 items.
383 *
384 * This function may sleep.
385 */
386int intel_scu_ipc_readv(u16 *addr, u8 *data, int len)
387{
388 return pwr_reg_rdwr(addr, data, len, IPCMSG_PCNTRL, IPC_CMD_PCNTRL_R);
389}
390EXPORT_SYMBOL(intel_scu_ipc_readv);
391
392/**
393 * intel_scu_ipc_writev - write a set of registers
394 * @addr: register list
395 * @data: bytes to write
396 * @len: length of array
397 *
398 * Write registers. Returns 0 on success or an error code. All
399 * locking between SCU accesses is handled for the caller.
400 *
401 * The largest array length permitted by the hardware is 5 items.
402 *
403 * This function may sleep.
404 *
405 */
406int intel_scu_ipc_writev(u16 *addr, u8 *data, int len)
407{
408 return pwr_reg_rdwr(addr, data, len, IPCMSG_PCNTRL, IPC_CMD_PCNTRL_W);
409}
410EXPORT_SYMBOL(intel_scu_ipc_writev);
411
Sreedhara DS9a58a332010-04-26 18:13:05 +0100412/**
413 * intel_scu_ipc_update_register - r/m/w a register
414 * @addr: register address
415 * @bits: bits to update
416 * @mask: mask of bits to update
417 *
418 * Read-modify-write power control unit register. The first data argument
419 * must be register value and second is mask value
420 * mask is a bitmap that indicates which bits to update.
421 * 0 = masked. Don't modify this bit, 1 = modify this bit.
422 * returns 0 on success or an error code.
423 *
424 * This function may sleep. Locking between SCU accesses is handled
425 * for the caller.
426 */
427int intel_scu_ipc_update_register(u16 addr, u8 bits, u8 mask)
428{
429 u8 data[2] = { bits, mask };
430 return pwr_reg_rdwr(&addr, data, 1, IPCMSG_PCNTRL, IPC_CMD_PCNTRL_M);
431}
432EXPORT_SYMBOL(intel_scu_ipc_update_register);
433
434/**
Sreedhara DS9a58a332010-04-26 18:13:05 +0100435 * intel_scu_ipc_simple_command - send a simple command
436 * @cmd: command
437 * @sub: sub type
438 *
439 * Issue a simple command to the SCU. Do not use this interface if
440 * you must then access data as any data values may be overwritten
441 * by another SCU access by the time this function returns.
442 *
443 * This function may sleep. Locking for SCU accesses is handled for
444 * the caller.
445 */
446int intel_scu_ipc_simple_command(int cmd, int sub)
447{
Axel Linecb56462011-01-25 14:12:12 +0000448 int err;
Sreedhara DS9a58a332010-04-26 18:13:05 +0100449
450 mutex_lock(&ipclock);
451 if (ipcdev.pdev == NULL) {
452 mutex_unlock(&ipclock);
453 return -ENODEV;
454 }
Sreedhara DSb4fd4f82010-07-19 09:37:42 +0100455 ipc_command(sub << 12 | cmd);
Kuppuswamy Sathyanarayananed12f292013-11-15 16:21:54 -0800456 err = intel_scu_ipc_check_status();
Sreedhara DS9a58a332010-04-26 18:13:05 +0100457 mutex_unlock(&ipclock);
458 return err;
459}
460EXPORT_SYMBOL(intel_scu_ipc_simple_command);
461
462/**
463 * intel_scu_ipc_command - command with data
464 * @cmd: command
465 * @sub: sub type
466 * @in: input data
Sreedhara DSb4fd4f82010-07-19 09:37:42 +0100467 * @inlen: input length in dwords
Sreedhara DS9a58a332010-04-26 18:13:05 +0100468 * @out: output data
Sreedhara DSb4fd4f82010-07-19 09:37:42 +0100469 * @outlein: output length in dwords
Sreedhara DS9a58a332010-04-26 18:13:05 +0100470 *
471 * Issue a command to the SCU which involves data transfers. Do the
472 * data copies under the lock but leave it for the caller to interpret
473 */
Sreedhara DS9a58a332010-04-26 18:13:05 +0100474int intel_scu_ipc_command(int cmd, int sub, u32 *in, int inlen,
Andy Shevchenko7c2e3c72015-01-21 21:38:09 +0200475 u32 *out, int outlen)
Sreedhara DS9a58a332010-04-26 18:13:05 +0100476{
Axel Linecb56462011-01-25 14:12:12 +0000477 int i, err;
Sreedhara DS9a58a332010-04-26 18:13:05 +0100478
479 mutex_lock(&ipclock);
480 if (ipcdev.pdev == NULL) {
481 mutex_unlock(&ipclock);
482 return -ENODEV;
483 }
484
485 for (i = 0; i < inlen; i++)
486 ipc_data_writel(*in++, 4 * i);
487
Hong Liu5aa06932010-07-26 10:06:31 +0100488 ipc_command((inlen << 16) | (sub << 12) | cmd);
Kuppuswamy Sathyanarayananed12f292013-11-15 16:21:54 -0800489 err = intel_scu_ipc_check_status();
Sreedhara DS9a58a332010-04-26 18:13:05 +0100490
Kuppuswamy Sathyanarayananc7094d12013-11-14 14:15:06 -0800491 if (!err) {
492 for (i = 0; i < outlen; i++)
493 *out++ = ipc_data_readl(4 * i);
494 }
Sreedhara DS9a58a332010-04-26 18:13:05 +0100495
496 mutex_unlock(&ipclock);
497 return err;
498}
499EXPORT_SYMBOL(intel_scu_ipc_command);
500
Andy Shevchenko7c2e3c72015-01-21 21:38:09 +0200501/* I2C commands */
Sreedhara DS9a58a332010-04-26 18:13:05 +0100502#define IPC_I2C_WRITE 1 /* I2C Write command */
503#define IPC_I2C_READ 2 /* I2C Read command */
504
505/**
506 * intel_scu_ipc_i2c_cntrl - I2C read/write operations
507 * @addr: I2C address + command bits
508 * @data: data to read/write
509 *
510 * Perform an an I2C read/write operation via the SCU. All locking is
511 * handled for the caller. This function may sleep.
512 *
513 * Returns an error code or 0 on success.
514 *
515 * This has to be in the IPC driver for the locking.
516 */
517int intel_scu_ipc_i2c_cntrl(u32 addr, u32 *data)
518{
519 u32 cmd = 0;
520
521 mutex_lock(&ipclock);
Sreedhara DSb4fd4f82010-07-19 09:37:42 +0100522 if (ipcdev.pdev == NULL) {
523 mutex_unlock(&ipclock);
524 return -ENODEV;
525 }
Sreedhara DS9a58a332010-04-26 18:13:05 +0100526 cmd = (addr >> 24) & 0xFF;
527 if (cmd == IPC_I2C_READ) {
528 writel(addr, ipcdev.i2c_base + IPC_I2C_CNTRL_ADDR);
529 /* Write not getting updated without delay */
530 mdelay(1);
531 *data = readl(ipcdev.i2c_base + I2C_DATA_ADDR);
532 } else if (cmd == IPC_I2C_WRITE) {
Jianwei Yang32e2f632010-08-24 14:32:38 +0100533 writel(*data, ipcdev.i2c_base + I2C_DATA_ADDR);
Sreedhara DS9a58a332010-04-26 18:13:05 +0100534 mdelay(1);
535 writel(addr, ipcdev.i2c_base + IPC_I2C_CNTRL_ADDR);
536 } else {
537 dev_err(&ipcdev.pdev->dev,
538 "intel_scu_ipc: I2C INVALID_CMD = 0x%x\n", cmd);
539
540 mutex_unlock(&ipclock);
Sreedhara DS5369c02d2010-10-22 15:43:55 +0100541 return -EIO;
Sreedhara DS9a58a332010-04-26 18:13:05 +0100542 }
543 mutex_unlock(&ipclock);
544 return 0;
545}
546EXPORT_SYMBOL(intel_scu_ipc_i2c_cntrl);
547
Sreedhara DS9a58a332010-04-26 18:13:05 +0100548/*
549 * Interrupt handler gets called when ioc bit of IPC_COMMAND_REG set to 1
550 * When ioc bit is set to 1, caller api must wait for interrupt handler called
551 * which in turn unlocks the caller api. Currently this is not used
552 *
553 * This is edge triggered so we need take no action to clear anything
554 */
555static irqreturn_t ioc(int irq, void *dev_id)
556{
Kuppuswamy Sathyanarayananed12f292013-11-15 16:21:54 -0800557 if (ipcdev.irq_mode)
558 complete(&ipcdev.cmd_complete);
559
Sreedhara DS9a58a332010-04-26 18:13:05 +0100560 return IRQ_HANDLED;
561}
562
563/**
564 * ipc_probe - probe an Intel SCU IPC
565 * @dev: the PCI device matching
566 * @id: entry in the match table
567 *
568 * Enable and install an intel SCU IPC. This appears in the PCI space
569 * but uses some hard coded addresses as well.
570 */
571static int ipc_probe(struct pci_dev *dev, const struct pci_device_id *id)
572{
David Cohen694e5232013-12-02 16:20:00 -0800573 int err;
Kuppuswamy Sathyanarayanane97a1c92013-11-14 14:15:04 -0800574 struct intel_scu_ipc_pdata_t *pdata;
Sreedhara DS9a58a332010-04-26 18:13:05 +0100575 resource_size_t pci_resource;
576
577 if (ipcdev.pdev) /* We support only one SCU */
578 return -EBUSY;
579
David Cohen694e5232013-12-02 16:20:00 -0800580 pdata = (struct intel_scu_ipc_pdata_t *)id->driver_data;
Kuppuswamy Sathyanarayanane97a1c92013-11-14 14:15:04 -0800581
Sreedhara DS9a58a332010-04-26 18:13:05 +0100582 ipcdev.pdev = pci_dev_get(dev);
Kuppuswamy Sathyanarayananed12f292013-11-15 16:21:54 -0800583 ipcdev.irq_mode = pdata->irq_mode;
Sreedhara DS9a58a332010-04-26 18:13:05 +0100584
585 err = pci_enable_device(dev);
586 if (err)
587 return err;
588
589 err = pci_request_regions(dev, "intel_scu_ipc");
590 if (err)
591 return err;
592
593 pci_resource = pci_resource_start(dev, 0);
594 if (!pci_resource)
595 return -ENOMEM;
596
Kuppuswamy Sathyanarayananed12f292013-11-15 16:21:54 -0800597 init_completion(&ipcdev.cmd_complete);
598
Sreedhara DS9a58a332010-04-26 18:13:05 +0100599 if (request_irq(dev->irq, ioc, 0, "intel_scu_ipc", &ipcdev))
600 return -EBUSY;
601
Kuppuswamy Sathyanarayanane97a1c92013-11-14 14:15:04 -0800602 ipcdev.ipc_base = ioremap_nocache(pdata->ipc_base, pdata->ipc_len);
Sreedhara DS9a58a332010-04-26 18:13:05 +0100603 if (!ipcdev.ipc_base)
604 return -ENOMEM;
605
Kuppuswamy Sathyanarayanane97a1c92013-11-14 14:15:04 -0800606 ipcdev.i2c_base = ioremap_nocache(pdata->i2c_base, pdata->i2c_len);
Sreedhara DS9a58a332010-04-26 18:13:05 +0100607 if (!ipcdev.i2c_base) {
608 iounmap(ipcdev.ipc_base);
609 return -ENOMEM;
610 }
Feng Tang1da4b1c2010-11-09 11:22:58 +0000611
612 intel_scu_devices_create();
613
Sreedhara DS9a58a332010-04-26 18:13:05 +0100614 return 0;
615}
616
617/**
618 * ipc_remove - remove a bound IPC device
619 * @pdev: PCI device
620 *
621 * In practice the SCU is not removable but this function is also
622 * called for each device on a module unload or cleanup which is the
623 * path that will get used.
624 *
625 * Free up the mappings and release the PCI resources
626 */
627static void ipc_remove(struct pci_dev *pdev)
628{
629 free_irq(pdev->irq, &ipcdev);
630 pci_release_regions(pdev);
631 pci_dev_put(ipcdev.pdev);
632 iounmap(ipcdev.ipc_base);
633 iounmap(ipcdev.i2c_base);
634 ipcdev.pdev = NULL;
Feng Tang1da4b1c2010-11-09 11:22:58 +0000635 intel_scu_devices_destroy();
Sreedhara DS9a58a332010-04-26 18:13:05 +0100636}
637
Benoit Taine9baa3c32014-08-08 15:56:03 +0200638static const struct pci_device_id pci_ids[] = {
David Cohen694e5232013-12-02 16:20:00 -0800639 {
David Cohenb4b0b4a2013-12-02 16:20:01 -0800640 PCI_VDEVICE(INTEL, PCI_DEVICE_ID_LINCROFT),
David Cohen694e5232013-12-02 16:20:00 -0800641 (kernel_ulong_t)&intel_scu_ipc_lincroft_pdata,
642 }, {
David Cohenb4b0b4a2013-12-02 16:20:01 -0800643 PCI_VDEVICE(INTEL, PCI_DEVICE_ID_PENWELL),
David Cohen694e5232013-12-02 16:20:00 -0800644 (kernel_ulong_t)&intel_scu_ipc_penwell_pdata,
645 }, {
David Cohenb4b0b4a2013-12-02 16:20:01 -0800646 PCI_VDEVICE(INTEL, PCI_DEVICE_ID_CLOVERVIEW),
David Cohen694e5232013-12-02 16:20:00 -0800647 (kernel_ulong_t)&intel_scu_ipc_penwell_pdata,
648 }, {
David Cohenb4b0b4a2013-12-02 16:20:01 -0800649 PCI_VDEVICE(INTEL, PCI_DEVICE_ID_TANGIER),
David Cohen694e5232013-12-02 16:20:00 -0800650 (kernel_ulong_t)&intel_scu_ipc_tangier_pdata,
651 }, {
652 0,
653 }
Sreedhara DS9a58a332010-04-26 18:13:05 +0100654};
655MODULE_DEVICE_TABLE(pci, pci_ids);
656
657static struct pci_driver ipc_driver = {
658 .name = "intel_scu_ipc",
659 .id_table = pci_ids,
660 .probe = ipc_probe,
661 .remove = ipc_remove,
662};
663
Sreedhara DS9a58a332010-04-26 18:13:05 +0100664static int __init intel_scu_ipc_init(void)
665{
Andy Shevchenko7c2e3c72015-01-21 21:38:09 +0200666 int platform; /* Platform type */
667
Kuppuswamy Sathyanarayanan712b6aa2013-10-17 15:35:29 -0700668 platform = intel_mid_identify_cpu();
Alan Cox9dd3ade2010-07-26 10:03:58 +0100669 if (platform == 0)
670 return -ENODEV;
Sreedhara DS9a58a332010-04-26 18:13:05 +0100671 return pci_register_driver(&ipc_driver);
672}
673
674static void __exit intel_scu_ipc_exit(void)
675{
676 pci_unregister_driver(&ipc_driver);
677}
678
679MODULE_AUTHOR("Sreedhara DS <sreedhara.ds@intel.com>");
680MODULE_DESCRIPTION("Intel SCU IPC driver");
681MODULE_LICENSE("GPL");
682
683module_init(intel_scu_ipc_init);
684module_exit(intel_scu_ipc_exit);