blob: b6a03447ea63d0bdf990bffed89b3e93dca13bdc [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
41/* Miscelaneous Command ids */
42#define IPC_CMD_INDIRECT_RD 2 /* 32bit indirect read */
43#define IPC_CMD_INDIRECT_WR 5 /* 32bit indirect write */
44
45/*
46 * IPC register summary
47 *
48 * IPC register blocks are memory mapped at fixed address of 0xFF11C000
49 * To read or write information to the SCU, driver writes to IPC-1 memory
50 * mapped registers (base address 0xFF11C000). The following is the IPC
51 * mechanism
52 *
53 * 1. IA core cDMI interface claims this transaction and converts it to a
54 * Transaction Layer Packet (TLP) message which is sent across the cDMI.
55 *
56 * 2. South Complex cDMI block receives this message and writes it to
57 * the IPC-1 register block, causing an interrupt to the SCU
58 *
59 * 3. SCU firmware decodes this interrupt and IPC message and the appropriate
60 * message handler is called within firmware.
61 */
62
63#define IPC_BASE_ADDR 0xFF11C000 /* IPC1 base register address */
64#define IPC_MAX_ADDR 0x100 /* Maximum IPC regisers */
65#define IPC_WWBUF_SIZE 16 /* IPC Write buffer Size */
66#define IPC_RWBUF_SIZE 16 /* IPC Read buffer Size */
67#define IPC_I2C_BASE 0xFF12B000 /* I2C control register base address */
68#define IPC_I2C_MAX_ADDR 0x10 /* Maximum I2C regisers */
69
70static int ipc_probe(struct pci_dev *dev, const struct pci_device_id *id);
71static void ipc_remove(struct pci_dev *pdev);
72
73struct intel_scu_ipc_dev {
74 struct pci_dev *pdev;
75 void __iomem *ipc_base;
76 void __iomem *i2c_base;
77};
78
79static struct intel_scu_ipc_dev ipcdev; /* Only one for now */
80
Sreedhara DS14d10f02010-07-26 10:02:25 +010081#define PLATFORM_LANGWELL 1
82#define PLATFORM_PENWELL 2
83static int platform; /* Platform type */
Sreedhara DS9a58a332010-04-26 18:13:05 +010084
85/*
86 * IPC Read Buffer (Read Only):
87 * 16 byte buffer for receiving data from SCU, if IPC command
88 * processing results in response data
89 */
90#define IPC_READ_BUFFER 0x90
91
92#define IPC_I2C_CNTRL_ADDR 0
93#define I2C_DATA_ADDR 0x04
94
95static DEFINE_MUTEX(ipclock); /* lock used to prevent multiple call to SCU */
96
97/*
98 * Command Register (Write Only):
99 * A write to this register results in an interrupt to the SCU core processor
100 * Format:
101 * |rfu2(8) | size(8) | command id(4) | rfu1(3) | ioc(1) | command(8)|
102 */
103static inline void ipc_command(u32 cmd) /* Send ipc command */
104{
105 writel(cmd, ipcdev.ipc_base);
106}
107
108/*
109 * IPC Write Buffer (Write Only):
110 * 16-byte buffer for sending data associated with IPC command to
111 * SCU. Size of the data is specified in the IPC_COMMAND_REG register
112 */
113static inline void ipc_data_writel(u32 data, u32 offset) /* Write ipc data */
114{
115 writel(data, ipcdev.ipc_base + 0x80 + offset);
116}
117
118/*
119 * IPC destination Pointer (Write Only):
120 * Use content as pointer for destination write
121 */
122static inline void ipc_write_dptr(u32 data) /* Write dptr data */
123{
124 writel(data, ipcdev.ipc_base + 0x0C);
125}
126
127/*
128 * IPC Source Pointer (Write Only):
129 * Use content as pointer for read location
130*/
131static inline void ipc_write_sptr(u32 data) /* Write dptr data */
132{
133 writel(data, ipcdev.ipc_base + 0x08);
134}
135
136/*
137 * Status Register (Read Only):
138 * Driver will read this register to get the ready/busy status of the IPC
139 * block and error status of the IPC command that was just processed by SCU
140 * Format:
141 * |rfu3(8)|error code(8)|initiator id(8)|cmd id(4)|rfu1(2)|error(1)|busy(1)|
142 */
143
144static inline u8 ipc_read_status(void)
145{
146 return __raw_readl(ipcdev.ipc_base + 0x04);
147}
148
149static inline u8 ipc_data_readb(u32 offset) /* Read ipc byte data */
150{
151 return readb(ipcdev.ipc_base + IPC_READ_BUFFER + offset);
152}
153
154static inline u8 ipc_data_readl(u32 offset) /* Read ipc u32 data */
155{
156 return readl(ipcdev.ipc_base + IPC_READ_BUFFER + offset);
157}
158
159static inline int busy_loop(void) /* Wait till scu status is busy */
160{
161 u32 status = 0;
162 u32 loop_count = 0;
163
164 status = ipc_read_status();
165 while (status & 1) {
166 udelay(1); /* scu processing time is in few u secods */
167 status = ipc_read_status();
168 loop_count++;
169 /* break if scu doesn't reset busy bit after huge retry */
170 if (loop_count > 100000) {
171 dev_err(&ipcdev.pdev->dev, "IPC timed out");
172 return -ETIMEDOUT;
173 }
174 }
175 return (status >> 1) & 1;
176}
177
178/* Read/Write power control(PMIC in Langwell, MSIC in PenWell) registers */
179static int pwr_reg_rdwr(u16 *addr, u8 *data, u32 count, u32 op, u32 id)
180{
181 int nc;
182 u32 offset = 0;
183 u32 err = 0;
184 u8 cbuf[IPC_WWBUF_SIZE] = { '\0' };
185 u32 *wbuf = (u32 *)&cbuf;
186
187 mutex_lock(&ipclock);
188 if (ipcdev.pdev == NULL) {
189 mutex_unlock(&ipclock);
190 return -ENODEV;
191 }
192
193 if (platform == 1) {
194 /* Entry is 4 bytes for read/write, 5 bytes for read modify */
195 for (nc = 0; nc < count; nc++) {
196 cbuf[offset] = addr[nc];
197 cbuf[offset + 1] = addr[nc] >> 8;
198 if (id != IPC_CMD_PCNTRL_R)
199 cbuf[offset + 2] = data[nc];
200 if (id == IPC_CMD_PCNTRL_M) {
201 cbuf[offset + 3] = data[nc + 1];
202 offset += 1;
203 }
204 offset += 3;
205 }
206 for (nc = 0, offset = 0; nc < count; nc++, offset += 4)
207 ipc_data_writel(wbuf[nc], offset); /* Write wbuff */
208
209 } else {
210 for (nc = 0, offset = 0; nc < count; nc++, offset += 2)
211 ipc_data_writel(addr[nc], offset); /* Write addresses */
212 if (id != IPC_CMD_PCNTRL_R) {
213 for (nc = 0; nc < count; nc++, offset++)
214 ipc_data_writel(data[nc], offset); /* Write data */
215 if (id == IPC_CMD_PCNTRL_M)
216 ipc_data_writel(data[nc + 1], offset); /* Mask value*/
217 }
218 }
219
220 if (id != IPC_CMD_PCNTRL_M)
221 ipc_command((count * 3) << 16 | id << 12 | 0 << 8 | op);
222 else
223 ipc_command((count * 4) << 16 | id << 12 | 0 << 8 | op);
224
225 err = busy_loop();
226
227 if (id == IPC_CMD_PCNTRL_R) { /* Read rbuf */
228 /* Workaround: values are read as 0 without memcpy_fromio */
229 memcpy_fromio(cbuf, ipcdev.ipc_base + IPC_READ_BUFFER, 16);
230 if (platform == 1) {
231 for (nc = 0, offset = 2; nc < count; nc++, offset += 3)
232 data[nc] = ipc_data_readb(offset);
233 } else {
234 for (nc = 0; nc < count; nc++)
235 data[nc] = ipc_data_readb(nc);
236 }
237 }
238 mutex_unlock(&ipclock);
239 return err;
240}
241
242/**
243 * intel_scu_ipc_ioread8 - read a word via the SCU
244 * @addr: register on SCU
245 * @data: return pointer for read byte
246 *
247 * Read a single register. Returns 0 on success or an error code. All
248 * locking between SCU accesses is handled for the caller.
249 *
250 * This function may sleep.
251 */
252int intel_scu_ipc_ioread8(u16 addr, u8 *data)
253{
254 return pwr_reg_rdwr(&addr, data, 1, IPCMSG_PCNTRL, IPC_CMD_PCNTRL_R);
255}
256EXPORT_SYMBOL(intel_scu_ipc_ioread8);
257
258/**
259 * intel_scu_ipc_ioread16 - read a word via the SCU
260 * @addr: register on SCU
261 * @data: return pointer for read word
262 *
263 * Read a register pair. 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_ioread16(u16 addr, u16 *data)
269{
270 u16 x[2] = {addr, addr + 1 };
271 return pwr_reg_rdwr(x, (u8 *)data, 2, IPCMSG_PCNTRL, IPC_CMD_PCNTRL_R);
272}
273EXPORT_SYMBOL(intel_scu_ipc_ioread16);
274
275/**
276 * intel_scu_ipc_ioread32 - read a dword via the SCU
277 * @addr: register on SCU
278 * @data: return pointer for read dword
279 *
280 * Read four registers. 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_ioread32(u16 addr, u32 *data)
286{
287 u16 x[4] = {addr, addr + 1, addr + 2, addr + 3};
288 return pwr_reg_rdwr(x, (u8 *)data, 4, IPCMSG_PCNTRL, IPC_CMD_PCNTRL_R);
289}
290EXPORT_SYMBOL(intel_scu_ipc_ioread32);
291
292/**
293 * intel_scu_ipc_iowrite8 - write a byte via the SCU
294 * @addr: register on SCU
295 * @data: byte to write
296 *
297 * Write a single register. Returns 0 on success or an error code. All
298 * locking between SCU accesses is handled for the caller.
299 *
300 * This function may sleep.
301 */
302int intel_scu_ipc_iowrite8(u16 addr, u8 data)
303{
304 return pwr_reg_rdwr(&addr, &data, 1, IPCMSG_PCNTRL, IPC_CMD_PCNTRL_W);
305}
306EXPORT_SYMBOL(intel_scu_ipc_iowrite8);
307
308/**
309 * intel_scu_ipc_iowrite16 - write a word via the SCU
310 * @addr: register on SCU
311 * @data: word to write
312 *
313 * Write two 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_iowrite16(u16 addr, u16 data)
319{
320 u16 x[2] = {addr, addr + 1 };
321 return pwr_reg_rdwr(x, (u8 *)&data, 2, IPCMSG_PCNTRL, IPC_CMD_PCNTRL_W);
322}
323EXPORT_SYMBOL(intel_scu_ipc_iowrite16);
324
325/**
326 * intel_scu_ipc_iowrite32 - write a dword via the SCU
327 * @addr: register on SCU
328 * @data: dword to write
329 *
330 * Write four registers. Returns 0 on success or an error code. All
331 * locking between SCU accesses is handled for the caller.
332 *
333 * This function may sleep.
334 */
335int intel_scu_ipc_iowrite32(u16 addr, u32 data)
336{
337 u16 x[4] = {addr, addr + 1, addr + 2, addr + 3};
338 return pwr_reg_rdwr(x, (u8 *)&data, 4, IPCMSG_PCNTRL, IPC_CMD_PCNTRL_W);
339}
340EXPORT_SYMBOL(intel_scu_ipc_iowrite32);
341
342/**
343 * intel_scu_ipc_readvv - read a set of registers
344 * @addr: register list
345 * @data: bytes to return
346 * @len: length of array
347 *
348 * Read registers. Returns 0 on success or an error code. All
349 * locking between SCU accesses is handled for the caller.
350 *
351 * The largest array length permitted by the hardware is 5 items.
352 *
353 * This function may sleep.
354 */
355int intel_scu_ipc_readv(u16 *addr, u8 *data, int len)
356{
357 return pwr_reg_rdwr(addr, data, len, IPCMSG_PCNTRL, IPC_CMD_PCNTRL_R);
358}
359EXPORT_SYMBOL(intel_scu_ipc_readv);
360
361/**
362 * intel_scu_ipc_writev - write a set of registers
363 * @addr: register list
364 * @data: bytes to write
365 * @len: length of array
366 *
367 * Write registers. Returns 0 on success or an error code. All
368 * locking between SCU accesses is handled for the caller.
369 *
370 * The largest array length permitted by the hardware is 5 items.
371 *
372 * This function may sleep.
373 *
374 */
375int intel_scu_ipc_writev(u16 *addr, u8 *data, int len)
376{
377 return pwr_reg_rdwr(addr, data, len, IPCMSG_PCNTRL, IPC_CMD_PCNTRL_W);
378}
379EXPORT_SYMBOL(intel_scu_ipc_writev);
380
381
382/**
383 * intel_scu_ipc_update_register - r/m/w a register
384 * @addr: register address
385 * @bits: bits to update
386 * @mask: mask of bits to update
387 *
388 * Read-modify-write power control unit register. The first data argument
389 * must be register value and second is mask value
390 * mask is a bitmap that indicates which bits to update.
391 * 0 = masked. Don't modify this bit, 1 = modify this bit.
392 * returns 0 on success or an error code.
393 *
394 * This function may sleep. Locking between SCU accesses is handled
395 * for the caller.
396 */
397int intel_scu_ipc_update_register(u16 addr, u8 bits, u8 mask)
398{
399 u8 data[2] = { bits, mask };
400 return pwr_reg_rdwr(&addr, data, 1, IPCMSG_PCNTRL, IPC_CMD_PCNTRL_M);
401}
402EXPORT_SYMBOL(intel_scu_ipc_update_register);
403
404/**
405 * intel_scu_ipc_register_read - 32bit indirect read
406 * @addr: register address
407 * @value: 32bit value return
408 *
409 * Performs IA 32 bit indirect read, returns 0 on success, or an
410 * error code.
411 *
412 * Can be used when SCCB(System Controller Configuration Block) register
413 * HRIM(Honor Restricted IPC Messages) is set (bit 23)
414 *
415 * This function may sleep. Locking for SCU accesses is handled for
416 * the caller.
417 */
418int intel_scu_ipc_register_read(u32 addr, u32 *value)
419{
420 u32 err = 0;
421
422 mutex_lock(&ipclock);
423 if (ipcdev.pdev == NULL) {
424 mutex_unlock(&ipclock);
425 return -ENODEV;
426 }
427 ipc_write_sptr(addr);
428 ipc_command(4 << 16 | IPC_CMD_INDIRECT_RD);
429 err = busy_loop();
430 *value = ipc_data_readl(0);
431 mutex_unlock(&ipclock);
432 return err;
433}
434EXPORT_SYMBOL(intel_scu_ipc_register_read);
435
436/**
437 * intel_scu_ipc_register_write - 32bit indirect write
438 * @addr: register address
439 * @value: 32bit value to write
440 *
441 * Performs IA 32 bit indirect write, returns 0 on success, or an
442 * error code.
443 *
444 * Can be used when SCCB(System Controller Configuration Block) register
445 * HRIM(Honor Restricted IPC Messages) is set (bit 23)
446 *
447 * This function may sleep. Locking for SCU accesses is handled for
448 * the caller.
449 */
450int intel_scu_ipc_register_write(u32 addr, u32 value)
451{
452 u32 err = 0;
453
454 mutex_lock(&ipclock);
455 if (ipcdev.pdev == NULL) {
456 mutex_unlock(&ipclock);
457 return -ENODEV;
458 }
459 ipc_write_dptr(addr);
460 ipc_data_writel(value, 0);
461 ipc_command(4 << 16 | IPC_CMD_INDIRECT_WR);
462 err = busy_loop();
463 mutex_unlock(&ipclock);
464 return err;
465}
466EXPORT_SYMBOL(intel_scu_ipc_register_write);
467
468/**
469 * intel_scu_ipc_simple_command - send a simple command
470 * @cmd: command
471 * @sub: sub type
472 *
473 * Issue a simple command to the SCU. Do not use this interface if
474 * you must then access data as any data values may be overwritten
475 * by another SCU access by the time this function returns.
476 *
477 * This function may sleep. Locking for SCU accesses is handled for
478 * the caller.
479 */
480int intel_scu_ipc_simple_command(int cmd, int sub)
481{
482 u32 err = 0;
483
484 mutex_lock(&ipclock);
485 if (ipcdev.pdev == NULL) {
486 mutex_unlock(&ipclock);
487 return -ENODEV;
488 }
Sreedhara DSb4fd4f82010-07-19 09:37:42 +0100489 ipc_command(sub << 12 | cmd);
Sreedhara DS9a58a332010-04-26 18:13:05 +0100490 err = busy_loop();
491 mutex_unlock(&ipclock);
492 return err;
493}
494EXPORT_SYMBOL(intel_scu_ipc_simple_command);
495
496/**
497 * intel_scu_ipc_command - command with data
498 * @cmd: command
499 * @sub: sub type
500 * @in: input data
Sreedhara DSb4fd4f82010-07-19 09:37:42 +0100501 * @inlen: input length in dwords
Sreedhara DS9a58a332010-04-26 18:13:05 +0100502 * @out: output data
Sreedhara DSb4fd4f82010-07-19 09:37:42 +0100503 * @outlein: output length in dwords
Sreedhara DS9a58a332010-04-26 18:13:05 +0100504 *
505 * Issue a command to the SCU which involves data transfers. Do the
506 * data copies under the lock but leave it for the caller to interpret
507 */
508
509int intel_scu_ipc_command(int cmd, int sub, u32 *in, int inlen,
510 u32 *out, int outlen)
511{
512 u32 err = 0;
513 int i = 0;
514
515 mutex_lock(&ipclock);
516 if (ipcdev.pdev == NULL) {
517 mutex_unlock(&ipclock);
518 return -ENODEV;
519 }
520
521 for (i = 0; i < inlen; i++)
522 ipc_data_writel(*in++, 4 * i);
523
Sreedhara DSb4fd4f82010-07-19 09:37:42 +0100524 ipc_command((sub << 12) | cmd | (inlen << 18));
Sreedhara DS9a58a332010-04-26 18:13:05 +0100525 err = busy_loop();
526
527 for (i = 0; i < outlen; i++)
528 *out++ = ipc_data_readl(4 * i);
529
530 mutex_unlock(&ipclock);
531 return err;
532}
533EXPORT_SYMBOL(intel_scu_ipc_command);
534
535/*I2C commands */
536#define IPC_I2C_WRITE 1 /* I2C Write command */
537#define IPC_I2C_READ 2 /* I2C Read command */
538
539/**
540 * intel_scu_ipc_i2c_cntrl - I2C read/write operations
541 * @addr: I2C address + command bits
542 * @data: data to read/write
543 *
544 * Perform an an I2C read/write operation via the SCU. All locking is
545 * handled for the caller. This function may sleep.
546 *
547 * Returns an error code or 0 on success.
548 *
549 * This has to be in the IPC driver for the locking.
550 */
551int intel_scu_ipc_i2c_cntrl(u32 addr, u32 *data)
552{
553 u32 cmd = 0;
554
555 mutex_lock(&ipclock);
Sreedhara DSb4fd4f82010-07-19 09:37:42 +0100556 if (ipcdev.pdev == NULL) {
557 mutex_unlock(&ipclock);
558 return -ENODEV;
559 }
Sreedhara DS9a58a332010-04-26 18:13:05 +0100560 cmd = (addr >> 24) & 0xFF;
561 if (cmd == IPC_I2C_READ) {
562 writel(addr, ipcdev.i2c_base + IPC_I2C_CNTRL_ADDR);
563 /* Write not getting updated without delay */
564 mdelay(1);
565 *data = readl(ipcdev.i2c_base + I2C_DATA_ADDR);
566 } else if (cmd == IPC_I2C_WRITE) {
567 writel(addr, ipcdev.i2c_base + I2C_DATA_ADDR);
568 mdelay(1);
569 writel(addr, ipcdev.i2c_base + IPC_I2C_CNTRL_ADDR);
570 } else {
571 dev_err(&ipcdev.pdev->dev,
572 "intel_scu_ipc: I2C INVALID_CMD = 0x%x\n", cmd);
573
574 mutex_unlock(&ipclock);
575 return -1;
576 }
577 mutex_unlock(&ipclock);
578 return 0;
579}
580EXPORT_SYMBOL(intel_scu_ipc_i2c_cntrl);
581
582#define IPC_FW_LOAD_ADDR 0xFFFC0000 /* Storage location for FW image */
583#define IPC_FW_UPDATE_MBOX_ADDR 0xFFFFDFF4 /* Mailbox between ipc and scu */
584#define IPC_MAX_FW_SIZE 262144 /* 256K storage size for loading the FW image */
585#define IPC_FW_MIP_HEADER_SIZE 2048 /* Firmware MIP header size */
586/* IPC inform SCU to get ready for update process */
587#define IPC_CMD_FW_UPDATE_READY 0x10FE
588/* IPC inform SCU to go for update process */
589#define IPC_CMD_FW_UPDATE_GO 0x20FE
590/* Status code for fw update */
591#define IPC_FW_UPDATE_SUCCESS 0x444f4e45 /* Status code 'DONE' */
592#define IPC_FW_UPDATE_BADN 0x4241444E /* Status code 'BADN' */
593#define IPC_FW_TXHIGH 0x54784849 /* Status code 'IPC_FW_TXHIGH' */
594#define IPC_FW_TXLOW 0x54784c4f /* Status code 'IPC_FW_TXLOW' */
595
596struct fw_update_mailbox {
597 u32 status;
598 u32 scu_flag;
599 u32 driver_flag;
600};
601
602
603/**
604 * intel_scu_ipc_fw_update - Firmware update utility
605 * @buffer: firmware buffer
606 * @length: size of firmware buffer
607 *
608 * This function provides an interface to load the firmware into
609 * the SCU. Returns 0 on success or -1 on failure
610 */
611int intel_scu_ipc_fw_update(u8 *buffer, u32 length)
612{
613 void __iomem *fw_update_base;
614 struct fw_update_mailbox __iomem *mailbox = NULL;
615 int retry_cnt = 0;
616 u32 status;
617
618 mutex_lock(&ipclock);
619 fw_update_base = ioremap_nocache(IPC_FW_LOAD_ADDR, (128*1024));
620 if (fw_update_base == NULL) {
621 mutex_unlock(&ipclock);
622 return -ENOMEM;
623 }
624 mailbox = ioremap_nocache(IPC_FW_UPDATE_MBOX_ADDR,
625 sizeof(struct fw_update_mailbox));
626 if (mailbox == NULL) {
627 iounmap(fw_update_base);
628 mutex_unlock(&ipclock);
629 return -ENOMEM;
630 }
631
632 ipc_command(IPC_CMD_FW_UPDATE_READY);
633
634 /* Intitialize mailbox */
635 writel(0, &mailbox->status);
636 writel(0, &mailbox->scu_flag);
637 writel(0, &mailbox->driver_flag);
638
639 /* Driver copies the 2KB MIP header to SRAM at 0xFFFC0000*/
640 memcpy_toio(fw_update_base, buffer, 0x800);
641
642 /* Driver sends "FW Update" IPC command (CMD_ID 0xFE; MSG_ID 0x02).
643 * Upon receiving this command, SCU will write the 2K MIP header
644 * from 0xFFFC0000 into NAND.
645 * SCU will write a status code into the Mailbox, and then set scu_flag.
646 */
647
648 ipc_command(IPC_CMD_FW_UPDATE_GO);
649
650 /*Driver stalls until scu_flag is set */
651 while (readl(&mailbox->scu_flag) != 1) {
652 rmb();
653 mdelay(1);
654 }
655
656 /* Driver checks Mailbox status.
657 * If the status is 'BADN', then abort (bad NAND).
658 * If the status is 'IPC_FW_TXLOW', then continue.
659 */
660 while (readl(&mailbox->status) != IPC_FW_TXLOW) {
661 rmb();
662 mdelay(10);
663 }
664 mdelay(10);
665
666update_retry:
667 if (retry_cnt > 5)
668 goto update_end;
669
670 if (readl(&mailbox->status) != IPC_FW_TXLOW)
671 goto update_end;
672 buffer = buffer + 0x800;
673 memcpy_toio(fw_update_base, buffer, 0x20000);
674 writel(1, &mailbox->driver_flag);
675 while (readl(&mailbox->scu_flag) == 1) {
676 rmb();
677 mdelay(1);
678 }
679
680 /* check for 'BADN' */
681 if (readl(&mailbox->status) == IPC_FW_UPDATE_BADN)
682 goto update_end;
683
684 while (readl(&mailbox->status) != IPC_FW_TXHIGH) {
685 rmb();
686 mdelay(10);
687 }
688 mdelay(10);
689
690 if (readl(&mailbox->status) != IPC_FW_TXHIGH)
691 goto update_end;
692
693 buffer = buffer + 0x20000;
694 memcpy_toio(fw_update_base, buffer, 0x20000);
695 writel(0, &mailbox->driver_flag);
696
697 while (mailbox->scu_flag == 0) {
698 rmb();
699 mdelay(1);
700 }
701
702 /* check for 'BADN' */
703 if (readl(&mailbox->status) == IPC_FW_UPDATE_BADN)
704 goto update_end;
705
706 if (readl(&mailbox->status) == IPC_FW_TXLOW) {
707 ++retry_cnt;
708 goto update_retry;
709 }
710
711update_end:
712 status = readl(&mailbox->status);
713
714 iounmap(fw_update_base);
715 iounmap(mailbox);
716 mutex_unlock(&ipclock);
717
718 if (status == IPC_FW_UPDATE_SUCCESS)
719 return 0;
720 return -1;
721}
722EXPORT_SYMBOL(intel_scu_ipc_fw_update);
723
724/*
725 * Interrupt handler gets called when ioc bit of IPC_COMMAND_REG set to 1
726 * When ioc bit is set to 1, caller api must wait for interrupt handler called
727 * which in turn unlocks the caller api. Currently this is not used
728 *
729 * This is edge triggered so we need take no action to clear anything
730 */
731static irqreturn_t ioc(int irq, void *dev_id)
732{
733 return IRQ_HANDLED;
734}
735
736/**
737 * ipc_probe - probe an Intel SCU IPC
738 * @dev: the PCI device matching
739 * @id: entry in the match table
740 *
741 * Enable and install an intel SCU IPC. This appears in the PCI space
742 * but uses some hard coded addresses as well.
743 */
744static int ipc_probe(struct pci_dev *dev, const struct pci_device_id *id)
745{
746 int err;
747 resource_size_t pci_resource;
748
749 if (ipcdev.pdev) /* We support only one SCU */
750 return -EBUSY;
751
752 ipcdev.pdev = pci_dev_get(dev);
753
754 err = pci_enable_device(dev);
755 if (err)
756 return err;
757
758 err = pci_request_regions(dev, "intel_scu_ipc");
759 if (err)
760 return err;
761
762 pci_resource = pci_resource_start(dev, 0);
763 if (!pci_resource)
764 return -ENOMEM;
765
766 if (request_irq(dev->irq, ioc, 0, "intel_scu_ipc", &ipcdev))
767 return -EBUSY;
768
769 ipcdev.ipc_base = ioremap_nocache(IPC_BASE_ADDR, IPC_MAX_ADDR);
770 if (!ipcdev.ipc_base)
771 return -ENOMEM;
772
773 ipcdev.i2c_base = ioremap_nocache(IPC_I2C_BASE, IPC_I2C_MAX_ADDR);
774 if (!ipcdev.i2c_base) {
775 iounmap(ipcdev.ipc_base);
776 return -ENOMEM;
777 }
778 return 0;
779}
780
781/**
782 * ipc_remove - remove a bound IPC device
783 * @pdev: PCI device
784 *
785 * In practice the SCU is not removable but this function is also
786 * called for each device on a module unload or cleanup which is the
787 * path that will get used.
788 *
789 * Free up the mappings and release the PCI resources
790 */
791static void ipc_remove(struct pci_dev *pdev)
792{
793 free_irq(pdev->irq, &ipcdev);
794 pci_release_regions(pdev);
795 pci_dev_put(ipcdev.pdev);
796 iounmap(ipcdev.ipc_base);
797 iounmap(ipcdev.i2c_base);
798 ipcdev.pdev = NULL;
799}
800
801static const struct pci_device_id pci_ids[] = {
802 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x080e)},
803 { 0,}
804};
805MODULE_DEVICE_TABLE(pci, pci_ids);
806
807static struct pci_driver ipc_driver = {
808 .name = "intel_scu_ipc",
809 .id_table = pci_ids,
810 .probe = ipc_probe,
811 .remove = ipc_remove,
812};
813
814
815static int __init intel_scu_ipc_init(void)
816{
Sreedhara DS14d10f02010-07-26 10:02:25 +0100817 if (boot_cpu_data.x86 == 6 &&
818 boot_cpu_data.x86_model == 0x27 &&
819 boot_cpu_data.x86_mask == 1)
820 platform = PLATFORM_PENWELL;
821 else if (boot_cpu_data.x86 == 6 &&
822 boot_cpu_data.x86_model == 0x26)
823 platform = PLATFORM_LANGWELL;
824
Sreedhara DS9a58a332010-04-26 18:13:05 +0100825 return pci_register_driver(&ipc_driver);
826}
827
828static void __exit intel_scu_ipc_exit(void)
829{
830 pci_unregister_driver(&ipc_driver);
831}
832
833MODULE_AUTHOR("Sreedhara DS <sreedhara.ds@intel.com>");
834MODULE_DESCRIPTION("Intel SCU IPC driver");
835MODULE_LICENSE("GPL");
836
837module_init(intel_scu_ipc_init);
838module_exit(intel_scu_ipc_exit);