blob: 5055c523c5e2695020a824cfcab727e5af6bfb5c [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 *
12 * SCU runing 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.
18 */
19#include <linux/delay.h>
20#include <linux/errno.h>
21#include <linux/init.h>
22#include <linux/sysdev.h>
23#include <linux/pm.h>
24#include <linux/pci.h>
25#include <linux/interrupt.h>
Sreedhara DS14d10f02010-07-26 10:02:25 +010026#include <asm/mrst.h>
Sreedhara DS9a58a332010-04-26 18:13:05 +010027#include <asm/intel_scu_ipc.h>
28
29/* IPC defines the following message types */
30#define IPCMSG_WATCHDOG_TIMER 0xF8 /* Set Kernel Watchdog Threshold */
31#define IPCMSG_BATTERY 0xEF /* Coulomb Counter Accumulator */
32#define IPCMSG_FW_UPDATE 0xFE /* Firmware update */
33#define IPCMSG_PCNTRL 0xFF /* Power controller unit read/write */
34#define IPCMSG_FW_REVISION 0xF4 /* Get firmware revision */
35
36/* Command id associated with message IPCMSG_PCNTRL */
37#define IPC_CMD_PCNTRL_W 0 /* Register write */
38#define IPC_CMD_PCNTRL_R 1 /* Register read */
39#define IPC_CMD_PCNTRL_M 2 /* Register read-modify-write */
40
Sreedhara DS9a58a332010-04-26 18:13:05 +010041/*
42 * IPC register summary
43 *
44 * IPC register blocks are memory mapped at fixed address of 0xFF11C000
45 * To read or write information to the SCU, driver writes to IPC-1 memory
46 * mapped registers (base address 0xFF11C000). The following is the IPC
47 * mechanism
48 *
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.
51 *
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
54 *
55 * 3. SCU firmware decodes this interrupt and IPC message and the appropriate
56 * message handler is called within firmware.
57 */
58
59#define IPC_BASE_ADDR 0xFF11C000 /* IPC1 base register address */
60#define IPC_MAX_ADDR 0x100 /* Maximum IPC regisers */
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 */
Sreedhara DS9a58a332010-04-26 18:13:05 +010063#define IPC_I2C_BASE 0xFF12B000 /* I2C control register base address */
64#define IPC_I2C_MAX_ADDR 0x10 /* Maximum I2C regisers */
65
66static int ipc_probe(struct pci_dev *dev, const struct pci_device_id *id);
67static void ipc_remove(struct pci_dev *pdev);
68
69struct intel_scu_ipc_dev {
70 struct pci_dev *pdev;
71 void __iomem *ipc_base;
72 void __iomem *i2c_base;
73};
74
75static struct intel_scu_ipc_dev ipcdev; /* Only one for now */
76
Sreedhara DS14d10f02010-07-26 10:02:25 +010077static int platform; /* Platform type */
Sreedhara DS9a58a332010-04-26 18:13:05 +010078
79/*
80 * IPC Read Buffer (Read Only):
81 * 16 byte buffer for receiving data from SCU, if IPC command
82 * processing results in response data
83 */
84#define IPC_READ_BUFFER 0x90
85
86#define IPC_I2C_CNTRL_ADDR 0
87#define I2C_DATA_ADDR 0x04
88
89static DEFINE_MUTEX(ipclock); /* lock used to prevent multiple call to SCU */
90
91/*
92 * Command Register (Write Only):
93 * A write to this register results in an interrupt to the SCU core processor
94 * Format:
95 * |rfu2(8) | size(8) | command id(4) | rfu1(3) | ioc(1) | command(8)|
96 */
97static inline void ipc_command(u32 cmd) /* Send ipc command */
98{
99 writel(cmd, ipcdev.ipc_base);
100}
101
102/*
103 * IPC Write Buffer (Write Only):
104 * 16-byte buffer for sending data associated with IPC command to
105 * SCU. Size of the data is specified in the IPC_COMMAND_REG register
106 */
107static inline void ipc_data_writel(u32 data, u32 offset) /* Write ipc data */
108{
109 writel(data, ipcdev.ipc_base + 0x80 + offset);
110}
111
112/*
Sreedhara DS9a58a332010-04-26 18:13:05 +0100113 * Status Register (Read Only):
114 * Driver will read this register to get the ready/busy status of the IPC
115 * block and error status of the IPC command that was just processed by SCU
116 * Format:
117 * |rfu3(8)|error code(8)|initiator id(8)|cmd id(4)|rfu1(2)|error(1)|busy(1)|
118 */
119
120static inline u8 ipc_read_status(void)
121{
122 return __raw_readl(ipcdev.ipc_base + 0x04);
123}
124
125static inline u8 ipc_data_readb(u32 offset) /* Read ipc byte data */
126{
127 return readb(ipcdev.ipc_base + IPC_READ_BUFFER + offset);
128}
129
Sreedhara DSe3359fd2010-07-26 10:02:46 +0100130static inline u32 ipc_data_readl(u32 offset) /* Read ipc u32 data */
Sreedhara DS9a58a332010-04-26 18:13:05 +0100131{
132 return readl(ipcdev.ipc_base + IPC_READ_BUFFER + offset);
133}
134
135static inline int busy_loop(void) /* Wait till scu status is busy */
136{
137 u32 status = 0;
138 u32 loop_count = 0;
139
140 status = ipc_read_status();
141 while (status & 1) {
142 udelay(1); /* scu processing time is in few u secods */
143 status = ipc_read_status();
144 loop_count++;
145 /* break if scu doesn't reset busy bit after huge retry */
146 if (loop_count > 100000) {
147 dev_err(&ipcdev.pdev->dev, "IPC timed out");
148 return -ETIMEDOUT;
149 }
150 }
151 return (status >> 1) & 1;
152}
153
154/* Read/Write power control(PMIC in Langwell, MSIC in PenWell) registers */
155static int pwr_reg_rdwr(u16 *addr, u8 *data, u32 count, u32 op, u32 id)
156{
Andy Ross6c8d0fd2010-07-26 10:05:03 +0100157 int i, nc, bytes;
Sreedhara DS9a58a332010-04-26 18:13:05 +0100158 u32 offset = 0;
159 u32 err = 0;
Sreedhara DSe3359fd2010-07-26 10:02:46 +0100160 u8 cbuf[IPC_WWBUF_SIZE] = { };
Sreedhara DS9a58a332010-04-26 18:13:05 +0100161 u32 *wbuf = (u32 *)&cbuf;
162
163 mutex_lock(&ipclock);
Sreedhara DSe3359fd2010-07-26 10:02:46 +0100164
Arjan van de Vened6f2b42010-07-26 10:04:37 +0100165 memset(cbuf, 0, sizeof(cbuf));
166
Sreedhara DS9a58a332010-04-26 18:13:05 +0100167 if (ipcdev.pdev == NULL) {
168 mutex_unlock(&ipclock);
169 return -ENODEV;
170 }
171
Alan Cox9dd3ade2010-07-26 10:03:58 +0100172 if (platform != MRST_CPU_CHIP_PENWELL) {
Andy Ross6c8d0fd2010-07-26 10:05:03 +0100173 bytes = 0;
174 for(i=0; i<count; i++) {
175 cbuf[bytes++] = addr[i];
176 cbuf[bytes++] = addr[i] >> 8;
Sreedhara DS9a58a332010-04-26 18:13:05 +0100177 if (id != IPC_CMD_PCNTRL_R)
Andy Ross6c8d0fd2010-07-26 10:05:03 +0100178 cbuf[bytes++] = data[i];
179 if (id == IPC_CMD_PCNTRL_M)
180 cbuf[bytes++] = data[i + 1];
Sreedhara DS9a58a332010-04-26 18:13:05 +0100181 }
Andy Ross6c8d0fd2010-07-26 10:05:03 +0100182 for(i=0; i<bytes; i+=4)
183 ipc_data_writel(wbuf[i/4], i);
184 ipc_command(bytes << 16 | id << 12 | 0 << 8 | op);
Sreedhara DS9a58a332010-04-26 18:13:05 +0100185 } else {
Sreedhara DSe3359fd2010-07-26 10:02:46 +0100186 for (nc = 0; nc < count; nc++, offset += 2) {
187 cbuf[offset] = addr[nc];
188 cbuf[offset + 1] = addr[nc] >> 8;
189 }
190
191 if (id == IPC_CMD_PCNTRL_R) {
192 for (nc = 0, offset = 0; nc < count; nc++, offset += 4)
193 ipc_data_writel(wbuf[nc], offset);
194 ipc_command((count*2) << 16 | id << 12 | 0 << 8 | op);
195 } else if (id == IPC_CMD_PCNTRL_W) {
196 for (nc = 0; nc < count; nc++, offset += 1)
197 cbuf[offset] = data[nc];
198 for (nc = 0, offset = 0; nc < count; nc++, offset += 4)
199 ipc_data_writel(wbuf[nc], offset);
200 ipc_command((count*3) << 16 | id << 12 | 0 << 8 | op);
201 } else if (id == IPC_CMD_PCNTRL_M) {
202 cbuf[offset] = data[0];
203 cbuf[offset + 1] = data[1];
204 ipc_data_writel(wbuf[0], 0); /* Write wbuff */
205 ipc_command(4 << 16 | id << 12 | 0 << 8 | op);
Sreedhara DS9a58a332010-04-26 18:13:05 +0100206 }
207 }
208
Sreedhara DS9a58a332010-04-26 18:13:05 +0100209 err = busy_loop();
Sreedhara DS9a58a332010-04-26 18:13:05 +0100210 if (id == IPC_CMD_PCNTRL_R) { /* Read rbuf */
211 /* Workaround: values are read as 0 without memcpy_fromio */
Sreedhara DSe3359fd2010-07-26 10:02:46 +0100212 memcpy_fromio(cbuf, ipcdev.ipc_base + 0x90, 16);
Alan Cox9dd3ade2010-07-26 10:03:58 +0100213 if (platform != MRST_CPU_CHIP_PENWELL) {
Sreedhara DS9a58a332010-04-26 18:13:05 +0100214 for (nc = 0, offset = 2; nc < count; nc++, offset += 3)
215 data[nc] = ipc_data_readb(offset);
216 } else {
217 for (nc = 0; nc < count; nc++)
218 data[nc] = ipc_data_readb(nc);
219 }
220 }
221 mutex_unlock(&ipclock);
222 return err;
223}
224
225/**
226 * intel_scu_ipc_ioread8 - read a word via the SCU
227 * @addr: register on SCU
228 * @data: return pointer for read byte
229 *
230 * Read a single register. Returns 0 on success or an error code. All
231 * locking between SCU accesses is handled for the caller.
232 *
233 * This function may sleep.
234 */
235int intel_scu_ipc_ioread8(u16 addr, u8 *data)
236{
237 return pwr_reg_rdwr(&addr, data, 1, IPCMSG_PCNTRL, IPC_CMD_PCNTRL_R);
238}
239EXPORT_SYMBOL(intel_scu_ipc_ioread8);
240
241/**
242 * intel_scu_ipc_ioread16 - read a word via the SCU
243 * @addr: register on SCU
244 * @data: return pointer for read word
245 *
246 * Read a register pair. Returns 0 on success or an error code. All
247 * locking between SCU accesses is handled for the caller.
248 *
249 * This function may sleep.
250 */
251int intel_scu_ipc_ioread16(u16 addr, u16 *data)
252{
253 u16 x[2] = {addr, addr + 1 };
254 return pwr_reg_rdwr(x, (u8 *)data, 2, IPCMSG_PCNTRL, IPC_CMD_PCNTRL_R);
255}
256EXPORT_SYMBOL(intel_scu_ipc_ioread16);
257
258/**
259 * intel_scu_ipc_ioread32 - read a dword via the SCU
260 * @addr: register on SCU
261 * @data: return pointer for read dword
262 *
263 * Read four registers. Returns 0 on success or an error code. All
264 * locking between SCU accesses is handled for the caller.
265 *
266 * This function may sleep.
267 */
268int intel_scu_ipc_ioread32(u16 addr, u32 *data)
269{
270 u16 x[4] = {addr, addr + 1, addr + 2, addr + 3};
271 return pwr_reg_rdwr(x, (u8 *)data, 4, IPCMSG_PCNTRL, IPC_CMD_PCNTRL_R);
272}
273EXPORT_SYMBOL(intel_scu_ipc_ioread32);
274
275/**
276 * intel_scu_ipc_iowrite8 - write a byte via the SCU
277 * @addr: register on SCU
278 * @data: byte to write
279 *
280 * Write a single register. Returns 0 on success or an error code. All
281 * locking between SCU accesses is handled for the caller.
282 *
283 * This function may sleep.
284 */
285int intel_scu_ipc_iowrite8(u16 addr, u8 data)
286{
287 return pwr_reg_rdwr(&addr, &data, 1, IPCMSG_PCNTRL, IPC_CMD_PCNTRL_W);
288}
289EXPORT_SYMBOL(intel_scu_ipc_iowrite8);
290
291/**
292 * intel_scu_ipc_iowrite16 - write a word via the SCU
293 * @addr: register on SCU
294 * @data: word to write
295 *
296 * Write two registers. Returns 0 on success or an error code. All
297 * locking between SCU accesses is handled for the caller.
298 *
299 * This function may sleep.
300 */
301int intel_scu_ipc_iowrite16(u16 addr, u16 data)
302{
303 u16 x[2] = {addr, addr + 1 };
304 return pwr_reg_rdwr(x, (u8 *)&data, 2, IPCMSG_PCNTRL, IPC_CMD_PCNTRL_W);
305}
306EXPORT_SYMBOL(intel_scu_ipc_iowrite16);
307
308/**
309 * intel_scu_ipc_iowrite32 - write a dword via the SCU
310 * @addr: register on SCU
311 * @data: dword to write
312 *
313 * Write four registers. Returns 0 on success or an error code. All
314 * locking between SCU accesses is handled for the caller.
315 *
316 * This function may sleep.
317 */
318int intel_scu_ipc_iowrite32(u16 addr, u32 data)
319{
320 u16 x[4] = {addr, addr + 1, addr + 2, addr + 3};
321 return pwr_reg_rdwr(x, (u8 *)&data, 4, IPCMSG_PCNTRL, IPC_CMD_PCNTRL_W);
322}
323EXPORT_SYMBOL(intel_scu_ipc_iowrite32);
324
325/**
326 * intel_scu_ipc_readvv - read a set of registers
327 * @addr: register list
328 * @data: bytes to return
329 * @len: length of array
330 *
331 * Read registers. Returns 0 on success or an error code. All
332 * locking between SCU accesses is handled for the caller.
333 *
334 * The largest array length permitted by the hardware is 5 items.
335 *
336 * This function may sleep.
337 */
338int intel_scu_ipc_readv(u16 *addr, u8 *data, int len)
339{
340 return pwr_reg_rdwr(addr, data, len, IPCMSG_PCNTRL, IPC_CMD_PCNTRL_R);
341}
342EXPORT_SYMBOL(intel_scu_ipc_readv);
343
344/**
345 * intel_scu_ipc_writev - write a set of registers
346 * @addr: register list
347 * @data: bytes to write
348 * @len: length of array
349 *
350 * Write registers. Returns 0 on success or an error code. All
351 * locking between SCU accesses is handled for the caller.
352 *
353 * The largest array length permitted by the hardware is 5 items.
354 *
355 * This function may sleep.
356 *
357 */
358int intel_scu_ipc_writev(u16 *addr, u8 *data, int len)
359{
360 return pwr_reg_rdwr(addr, data, len, IPCMSG_PCNTRL, IPC_CMD_PCNTRL_W);
361}
362EXPORT_SYMBOL(intel_scu_ipc_writev);
363
364
365/**
366 * intel_scu_ipc_update_register - r/m/w a register
367 * @addr: register address
368 * @bits: bits to update
369 * @mask: mask of bits to update
370 *
371 * Read-modify-write power control unit register. The first data argument
372 * must be register value and second is mask value
373 * mask is a bitmap that indicates which bits to update.
374 * 0 = masked. Don't modify this bit, 1 = modify this bit.
375 * returns 0 on success or an error code.
376 *
377 * This function may sleep. Locking between SCU accesses is handled
378 * for the caller.
379 */
380int intel_scu_ipc_update_register(u16 addr, u8 bits, u8 mask)
381{
382 u8 data[2] = { bits, mask };
383 return pwr_reg_rdwr(&addr, data, 1, IPCMSG_PCNTRL, IPC_CMD_PCNTRL_M);
384}
385EXPORT_SYMBOL(intel_scu_ipc_update_register);
386
387/**
Sreedhara DS9a58a332010-04-26 18:13:05 +0100388 * intel_scu_ipc_simple_command - send a simple command
389 * @cmd: command
390 * @sub: sub type
391 *
392 * Issue a simple command to the SCU. Do not use this interface if
393 * you must then access data as any data values may be overwritten
394 * by another SCU access by the time this function returns.
395 *
396 * This function may sleep. Locking for SCU accesses is handled for
397 * the caller.
398 */
399int intel_scu_ipc_simple_command(int cmd, int sub)
400{
401 u32 err = 0;
402
403 mutex_lock(&ipclock);
404 if (ipcdev.pdev == NULL) {
405 mutex_unlock(&ipclock);
406 return -ENODEV;
407 }
Sreedhara DSb4fd4f82010-07-19 09:37:42 +0100408 ipc_command(sub << 12 | cmd);
Sreedhara DS9a58a332010-04-26 18:13:05 +0100409 err = busy_loop();
410 mutex_unlock(&ipclock);
411 return err;
412}
413EXPORT_SYMBOL(intel_scu_ipc_simple_command);
414
415/**
416 * intel_scu_ipc_command - command with data
417 * @cmd: command
418 * @sub: sub type
419 * @in: input data
Sreedhara DSb4fd4f82010-07-19 09:37:42 +0100420 * @inlen: input length in dwords
Sreedhara DS9a58a332010-04-26 18:13:05 +0100421 * @out: output data
Sreedhara DSb4fd4f82010-07-19 09:37:42 +0100422 * @outlein: output length in dwords
Sreedhara DS9a58a332010-04-26 18:13:05 +0100423 *
424 * Issue a command to the SCU which involves data transfers. Do the
425 * data copies under the lock but leave it for the caller to interpret
426 */
427
428int intel_scu_ipc_command(int cmd, int sub, u32 *in, int inlen,
429 u32 *out, int outlen)
430{
431 u32 err = 0;
432 int i = 0;
433
434 mutex_lock(&ipclock);
435 if (ipcdev.pdev == NULL) {
436 mutex_unlock(&ipclock);
437 return -ENODEV;
438 }
439
440 for (i = 0; i < inlen; i++)
441 ipc_data_writel(*in++, 4 * i);
442
Sreedhara DSb4fd4f82010-07-19 09:37:42 +0100443 ipc_command((sub << 12) | cmd | (inlen << 18));
Sreedhara DS9a58a332010-04-26 18:13:05 +0100444 err = busy_loop();
445
446 for (i = 0; i < outlen; i++)
447 *out++ = ipc_data_readl(4 * i);
448
449 mutex_unlock(&ipclock);
450 return err;
451}
452EXPORT_SYMBOL(intel_scu_ipc_command);
453
454/*I2C commands */
455#define IPC_I2C_WRITE 1 /* I2C Write command */
456#define IPC_I2C_READ 2 /* I2C Read command */
457
458/**
459 * intel_scu_ipc_i2c_cntrl - I2C read/write operations
460 * @addr: I2C address + command bits
461 * @data: data to read/write
462 *
463 * Perform an an I2C read/write operation via the SCU. All locking is
464 * handled for the caller. This function may sleep.
465 *
466 * Returns an error code or 0 on success.
467 *
468 * This has to be in the IPC driver for the locking.
469 */
470int intel_scu_ipc_i2c_cntrl(u32 addr, u32 *data)
471{
472 u32 cmd = 0;
473
474 mutex_lock(&ipclock);
Sreedhara DSb4fd4f82010-07-19 09:37:42 +0100475 if (ipcdev.pdev == NULL) {
476 mutex_unlock(&ipclock);
477 return -ENODEV;
478 }
Sreedhara DS9a58a332010-04-26 18:13:05 +0100479 cmd = (addr >> 24) & 0xFF;
480 if (cmd == IPC_I2C_READ) {
481 writel(addr, ipcdev.i2c_base + IPC_I2C_CNTRL_ADDR);
482 /* Write not getting updated without delay */
483 mdelay(1);
484 *data = readl(ipcdev.i2c_base + I2C_DATA_ADDR);
485 } else if (cmd == IPC_I2C_WRITE) {
486 writel(addr, ipcdev.i2c_base + I2C_DATA_ADDR);
487 mdelay(1);
488 writel(addr, ipcdev.i2c_base + IPC_I2C_CNTRL_ADDR);
489 } else {
490 dev_err(&ipcdev.pdev->dev,
491 "intel_scu_ipc: I2C INVALID_CMD = 0x%x\n", cmd);
492
493 mutex_unlock(&ipclock);
494 return -1;
495 }
496 mutex_unlock(&ipclock);
497 return 0;
498}
499EXPORT_SYMBOL(intel_scu_ipc_i2c_cntrl);
500
501#define IPC_FW_LOAD_ADDR 0xFFFC0000 /* Storage location for FW image */
502#define IPC_FW_UPDATE_MBOX_ADDR 0xFFFFDFF4 /* Mailbox between ipc and scu */
503#define IPC_MAX_FW_SIZE 262144 /* 256K storage size for loading the FW image */
504#define IPC_FW_MIP_HEADER_SIZE 2048 /* Firmware MIP header size */
505/* IPC inform SCU to get ready for update process */
506#define IPC_CMD_FW_UPDATE_READY 0x10FE
507/* IPC inform SCU to go for update process */
508#define IPC_CMD_FW_UPDATE_GO 0x20FE
509/* Status code for fw update */
510#define IPC_FW_UPDATE_SUCCESS 0x444f4e45 /* Status code 'DONE' */
511#define IPC_FW_UPDATE_BADN 0x4241444E /* Status code 'BADN' */
512#define IPC_FW_TXHIGH 0x54784849 /* Status code 'IPC_FW_TXHIGH' */
513#define IPC_FW_TXLOW 0x54784c4f /* Status code 'IPC_FW_TXLOW' */
514
515struct fw_update_mailbox {
516 u32 status;
517 u32 scu_flag;
518 u32 driver_flag;
519};
520
521
522/**
523 * intel_scu_ipc_fw_update - Firmware update utility
524 * @buffer: firmware buffer
525 * @length: size of firmware buffer
526 *
527 * This function provides an interface to load the firmware into
528 * the SCU. Returns 0 on success or -1 on failure
529 */
530int intel_scu_ipc_fw_update(u8 *buffer, u32 length)
531{
532 void __iomem *fw_update_base;
533 struct fw_update_mailbox __iomem *mailbox = NULL;
534 int retry_cnt = 0;
535 u32 status;
536
537 mutex_lock(&ipclock);
538 fw_update_base = ioremap_nocache(IPC_FW_LOAD_ADDR, (128*1024));
539 if (fw_update_base == NULL) {
540 mutex_unlock(&ipclock);
541 return -ENOMEM;
542 }
543 mailbox = ioremap_nocache(IPC_FW_UPDATE_MBOX_ADDR,
544 sizeof(struct fw_update_mailbox));
545 if (mailbox == NULL) {
546 iounmap(fw_update_base);
547 mutex_unlock(&ipclock);
548 return -ENOMEM;
549 }
550
551 ipc_command(IPC_CMD_FW_UPDATE_READY);
552
553 /* Intitialize mailbox */
554 writel(0, &mailbox->status);
555 writel(0, &mailbox->scu_flag);
556 writel(0, &mailbox->driver_flag);
557
558 /* Driver copies the 2KB MIP header to SRAM at 0xFFFC0000*/
559 memcpy_toio(fw_update_base, buffer, 0x800);
560
561 /* Driver sends "FW Update" IPC command (CMD_ID 0xFE; MSG_ID 0x02).
562 * Upon receiving this command, SCU will write the 2K MIP header
563 * from 0xFFFC0000 into NAND.
564 * SCU will write a status code into the Mailbox, and then set scu_flag.
565 */
566
567 ipc_command(IPC_CMD_FW_UPDATE_GO);
568
569 /*Driver stalls until scu_flag is set */
570 while (readl(&mailbox->scu_flag) != 1) {
571 rmb();
572 mdelay(1);
573 }
574
575 /* Driver checks Mailbox status.
576 * If the status is 'BADN', then abort (bad NAND).
577 * If the status is 'IPC_FW_TXLOW', then continue.
578 */
579 while (readl(&mailbox->status) != IPC_FW_TXLOW) {
580 rmb();
581 mdelay(10);
582 }
583 mdelay(10);
584
585update_retry:
586 if (retry_cnt > 5)
587 goto update_end;
588
589 if (readl(&mailbox->status) != IPC_FW_TXLOW)
590 goto update_end;
591 buffer = buffer + 0x800;
592 memcpy_toio(fw_update_base, buffer, 0x20000);
593 writel(1, &mailbox->driver_flag);
594 while (readl(&mailbox->scu_flag) == 1) {
595 rmb();
596 mdelay(1);
597 }
598
599 /* check for 'BADN' */
600 if (readl(&mailbox->status) == IPC_FW_UPDATE_BADN)
601 goto update_end;
602
603 while (readl(&mailbox->status) != IPC_FW_TXHIGH) {
604 rmb();
605 mdelay(10);
606 }
607 mdelay(10);
608
609 if (readl(&mailbox->status) != IPC_FW_TXHIGH)
610 goto update_end;
611
612 buffer = buffer + 0x20000;
613 memcpy_toio(fw_update_base, buffer, 0x20000);
614 writel(0, &mailbox->driver_flag);
615
616 while (mailbox->scu_flag == 0) {
617 rmb();
618 mdelay(1);
619 }
620
621 /* check for 'BADN' */
622 if (readl(&mailbox->status) == IPC_FW_UPDATE_BADN)
623 goto update_end;
624
625 if (readl(&mailbox->status) == IPC_FW_TXLOW) {
626 ++retry_cnt;
627 goto update_retry;
628 }
629
630update_end:
631 status = readl(&mailbox->status);
632
633 iounmap(fw_update_base);
634 iounmap(mailbox);
635 mutex_unlock(&ipclock);
636
637 if (status == IPC_FW_UPDATE_SUCCESS)
638 return 0;
639 return -1;
640}
641EXPORT_SYMBOL(intel_scu_ipc_fw_update);
642
643/*
644 * Interrupt handler gets called when ioc bit of IPC_COMMAND_REG set to 1
645 * When ioc bit is set to 1, caller api must wait for interrupt handler called
646 * which in turn unlocks the caller api. Currently this is not used
647 *
648 * This is edge triggered so we need take no action to clear anything
649 */
650static irqreturn_t ioc(int irq, void *dev_id)
651{
652 return IRQ_HANDLED;
653}
654
655/**
656 * ipc_probe - probe an Intel SCU IPC
657 * @dev: the PCI device matching
658 * @id: entry in the match table
659 *
660 * Enable and install an intel SCU IPC. This appears in the PCI space
661 * but uses some hard coded addresses as well.
662 */
663static int ipc_probe(struct pci_dev *dev, const struct pci_device_id *id)
664{
665 int err;
666 resource_size_t pci_resource;
667
668 if (ipcdev.pdev) /* We support only one SCU */
669 return -EBUSY;
670
671 ipcdev.pdev = pci_dev_get(dev);
672
673 err = pci_enable_device(dev);
674 if (err)
675 return err;
676
677 err = pci_request_regions(dev, "intel_scu_ipc");
678 if (err)
679 return err;
680
681 pci_resource = pci_resource_start(dev, 0);
682 if (!pci_resource)
683 return -ENOMEM;
684
685 if (request_irq(dev->irq, ioc, 0, "intel_scu_ipc", &ipcdev))
686 return -EBUSY;
687
688 ipcdev.ipc_base = ioremap_nocache(IPC_BASE_ADDR, IPC_MAX_ADDR);
689 if (!ipcdev.ipc_base)
690 return -ENOMEM;
691
692 ipcdev.i2c_base = ioremap_nocache(IPC_I2C_BASE, IPC_I2C_MAX_ADDR);
693 if (!ipcdev.i2c_base) {
694 iounmap(ipcdev.ipc_base);
695 return -ENOMEM;
696 }
697 return 0;
698}
699
700/**
701 * ipc_remove - remove a bound IPC device
702 * @pdev: PCI device
703 *
704 * In practice the SCU is not removable but this function is also
705 * called for each device on a module unload or cleanup which is the
706 * path that will get used.
707 *
708 * Free up the mappings and release the PCI resources
709 */
710static void ipc_remove(struct pci_dev *pdev)
711{
712 free_irq(pdev->irq, &ipcdev);
713 pci_release_regions(pdev);
714 pci_dev_put(ipcdev.pdev);
715 iounmap(ipcdev.ipc_base);
716 iounmap(ipcdev.i2c_base);
717 ipcdev.pdev = NULL;
718}
719
720static const struct pci_device_id pci_ids[] = {
721 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x080e)},
Sreedhara DSe3359fd2010-07-26 10:02:46 +0100722 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x082a)},
Sreedhara DS9a58a332010-04-26 18:13:05 +0100723 { 0,}
724};
725MODULE_DEVICE_TABLE(pci, pci_ids);
726
727static struct pci_driver ipc_driver = {
728 .name = "intel_scu_ipc",
729 .id_table = pci_ids,
730 .probe = ipc_probe,
731 .remove = ipc_remove,
732};
733
734
735static int __init intel_scu_ipc_init(void)
736{
Alan Cox9dd3ade2010-07-26 10:03:58 +0100737 platform = mrst_identify_cpu();
738 if (platform == 0)
739 return -ENODEV;
Sreedhara DS9a58a332010-04-26 18:13:05 +0100740 return pci_register_driver(&ipc_driver);
741}
742
743static void __exit intel_scu_ipc_exit(void)
744{
745 pci_unregister_driver(&ipc_driver);
746}
747
748MODULE_AUTHOR("Sreedhara DS <sreedhara.ds@intel.com>");
749MODULE_DESCRIPTION("Intel SCU IPC driver");
750MODULE_LICENSE("GPL");
751
752module_init(intel_scu_ipc_init);
753module_exit(intel_scu_ipc_exit);