intel_scu_ipc: Support Medfield processors
[firefly-linux-kernel-4.4.55.git] / drivers / platform / x86 / intel_scu_ipc.c
1 /*
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>
26 #include <asm/mrst.h>
27 #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
70 static int ipc_probe(struct pci_dev *dev, const struct pci_device_id *id);
71 static void ipc_remove(struct pci_dev *pdev);
72
73 struct intel_scu_ipc_dev {
74         struct pci_dev *pdev;
75         void __iomem *ipc_base;
76         void __iomem *i2c_base;
77 };
78
79 static struct intel_scu_ipc_dev  ipcdev; /* Only one for now */
80
81 #define PLATFORM_LANGWELL 1
82 #define PLATFORM_PENWELL 2
83 static int platform;            /* Platform type */
84
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
95 static 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  */
103 static 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  */
113 static 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  */
122 static 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 */
131 static 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
144 static inline u8 ipc_read_status(void)
145 {
146         return __raw_readl(ipcdev.ipc_base + 0x04);
147 }
148
149 static inline u8 ipc_data_readb(u32 offset) /* Read ipc byte data */
150 {
151         return readb(ipcdev.ipc_base + IPC_READ_BUFFER + offset);
152 }
153
154 static inline u32 ipc_data_readl(u32 offset) /* Read ipc u32 data */
155 {
156         return readl(ipcdev.ipc_base + IPC_READ_BUFFER + offset);
157 }
158
159 static 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 */
179 static 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] = { };
185         u32 *wbuf = (u32 *)&cbuf;
186
187         mutex_lock(&ipclock);
188
189         if (ipcdev.pdev == NULL) {
190                 mutex_unlock(&ipclock);
191                 return -ENODEV;
192         }
193
194         if (platform == PLATFORM_LANGWELL) {
195                 /* Entry is 4 bytes for read/write, 5 bytes for read modify */
196                 for (nc = 0; nc < count; nc++, offset += 3) {
197                         cbuf[offset] = addr[nc];
198                         cbuf[offset + 1] = addr[nc] >> 8;
199                         if (id != IPC_CMD_PCNTRL_R)
200                                 cbuf[offset + 2] = data[nc];
201                         if (id == IPC_CMD_PCNTRL_M) {
202                                 cbuf[offset + 3] = data[nc + 1];
203                                 offset += 1;
204                         }
205                 }
206                 for (nc = 0, offset = 0; nc < count; nc++, offset += 4)
207                         ipc_data_writel(wbuf[nc], offset); /* Write wbuff */
208
209                 if (id != IPC_CMD_PCNTRL_M)
210                         ipc_command((count*4) << 16 |  id << 12 | 0 << 8 | op);
211                 else
212                         ipc_command((count*5) << 16 |  id << 12 | 0 << 8 | op);
213
214         } else {
215                 for (nc = 0; nc < count; nc++, offset += 2) {
216                         cbuf[offset] = addr[nc];
217                         cbuf[offset + 1] = addr[nc] >> 8;
218                 }
219
220                 if (id == IPC_CMD_PCNTRL_R) {
221                         for (nc = 0, offset = 0; nc < count; nc++, offset += 4)
222                                 ipc_data_writel(wbuf[nc], offset);
223                         ipc_command((count*2) << 16 |  id << 12 | 0 << 8 | op);
224                 } else if (id == IPC_CMD_PCNTRL_W) {
225                         for (nc = 0; nc < count; nc++, offset += 1)
226                                 cbuf[offset] = data[nc];
227                         for (nc = 0, offset = 0; nc < count; nc++, offset += 4)
228                                 ipc_data_writel(wbuf[nc], offset);
229                         ipc_command((count*3) << 16 |  id << 12 | 0 << 8 | op);
230                 } else if (id == IPC_CMD_PCNTRL_M) {
231                         cbuf[offset] = data[0];
232                         cbuf[offset + 1] = data[1];
233                         ipc_data_writel(wbuf[0], 0); /* Write wbuff */
234                         ipc_command(4 << 16 |  id << 12 | 0 << 8 | op);
235                 }
236         }
237
238         err = busy_loop();
239         if (id == IPC_CMD_PCNTRL_R) { /* Read rbuf */
240                 /* Workaround: values are read as 0 without memcpy_fromio */
241                 memcpy_fromio(cbuf, ipcdev.ipc_base + 0x90, 16);
242                 if (platform == PLATFORM_LANGWELL) {
243                         for (nc = 0, offset = 2; nc < count; nc++, offset += 3)
244                                 data[nc] = ipc_data_readb(offset);
245                 } else {
246                         for (nc = 0; nc < count; nc++)
247                                 data[nc] = ipc_data_readb(nc);
248                 }
249         }
250         mutex_unlock(&ipclock);
251         return err;
252 }
253
254 /**
255  *      intel_scu_ipc_ioread8           -       read a word via the SCU
256  *      @addr: register on SCU
257  *      @data: return pointer for read byte
258  *
259  *      Read a single register. Returns 0 on success or an error code. All
260  *      locking between SCU accesses is handled for the caller.
261  *
262  *      This function may sleep.
263  */
264 int intel_scu_ipc_ioread8(u16 addr, u8 *data)
265 {
266         return pwr_reg_rdwr(&addr, data, 1, IPCMSG_PCNTRL, IPC_CMD_PCNTRL_R);
267 }
268 EXPORT_SYMBOL(intel_scu_ipc_ioread8);
269
270 /**
271  *      intel_scu_ipc_ioread16          -       read a word via the SCU
272  *      @addr: register on SCU
273  *      @data: return pointer for read word
274  *
275  *      Read a register pair. Returns 0 on success or an error code. All
276  *      locking between SCU accesses is handled for the caller.
277  *
278  *      This function may sleep.
279  */
280 int intel_scu_ipc_ioread16(u16 addr, u16 *data)
281 {
282         u16 x[2] = {addr, addr + 1 };
283         return pwr_reg_rdwr(x, (u8 *)data, 2, IPCMSG_PCNTRL, IPC_CMD_PCNTRL_R);
284 }
285 EXPORT_SYMBOL(intel_scu_ipc_ioread16);
286
287 /**
288  *      intel_scu_ipc_ioread32          -       read a dword via the SCU
289  *      @addr: register on SCU
290  *      @data: return pointer for read dword
291  *
292  *      Read four registers. Returns 0 on success or an error code. All
293  *      locking between SCU accesses is handled for the caller.
294  *
295  *      This function may sleep.
296  */
297 int intel_scu_ipc_ioread32(u16 addr, u32 *data)
298 {
299         u16 x[4] = {addr, addr + 1, addr + 2, addr + 3};
300         return pwr_reg_rdwr(x, (u8 *)data, 4, IPCMSG_PCNTRL, IPC_CMD_PCNTRL_R);
301 }
302 EXPORT_SYMBOL(intel_scu_ipc_ioread32);
303
304 /**
305  *      intel_scu_ipc_iowrite8          -       write a byte via the SCU
306  *      @addr: register on SCU
307  *      @data: byte to write
308  *
309  *      Write a single register. Returns 0 on success or an error code. All
310  *      locking between SCU accesses is handled for the caller.
311  *
312  *      This function may sleep.
313  */
314 int intel_scu_ipc_iowrite8(u16 addr, u8 data)
315 {
316         return pwr_reg_rdwr(&addr, &data, 1, IPCMSG_PCNTRL, IPC_CMD_PCNTRL_W);
317 }
318 EXPORT_SYMBOL(intel_scu_ipc_iowrite8);
319
320 /**
321  *      intel_scu_ipc_iowrite16         -       write a word via the SCU
322  *      @addr: register on SCU
323  *      @data: word to write
324  *
325  *      Write two registers. Returns 0 on success or an error code. All
326  *      locking between SCU accesses is handled for the caller.
327  *
328  *      This function may sleep.
329  */
330 int intel_scu_ipc_iowrite16(u16 addr, u16 data)
331 {
332         u16 x[2] = {addr, addr + 1 };
333         return pwr_reg_rdwr(x, (u8 *)&data, 2, IPCMSG_PCNTRL, IPC_CMD_PCNTRL_W);
334 }
335 EXPORT_SYMBOL(intel_scu_ipc_iowrite16);
336
337 /**
338  *      intel_scu_ipc_iowrite32         -       write a dword via the SCU
339  *      @addr: register on SCU
340  *      @data: dword to write
341  *
342  *      Write four registers. Returns 0 on success or an error code. All
343  *      locking between SCU accesses is handled for the caller.
344  *
345  *      This function may sleep.
346  */
347 int intel_scu_ipc_iowrite32(u16 addr, u32 data)
348 {
349         u16 x[4] = {addr, addr + 1, addr + 2, addr + 3};
350         return pwr_reg_rdwr(x, (u8 *)&data, 4, IPCMSG_PCNTRL, IPC_CMD_PCNTRL_W);
351 }
352 EXPORT_SYMBOL(intel_scu_ipc_iowrite32);
353
354 /**
355  *      intel_scu_ipc_readvv            -       read a set of registers
356  *      @addr: register list
357  *      @data: bytes to return
358  *      @len: length of array
359  *
360  *      Read registers. Returns 0 on success or an error code. All
361  *      locking between SCU accesses is handled for the caller.
362  *
363  *      The largest array length permitted by the hardware is 5 items.
364  *
365  *      This function may sleep.
366  */
367 int intel_scu_ipc_readv(u16 *addr, u8 *data, int len)
368 {
369         return pwr_reg_rdwr(addr, data, len, IPCMSG_PCNTRL, IPC_CMD_PCNTRL_R);
370 }
371 EXPORT_SYMBOL(intel_scu_ipc_readv);
372
373 /**
374  *      intel_scu_ipc_writev            -       write a set of registers
375  *      @addr: register list
376  *      @data: bytes to write
377  *      @len: length of array
378  *
379  *      Write 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  *
386  */
387 int intel_scu_ipc_writev(u16 *addr, u8 *data, int len)
388 {
389         return pwr_reg_rdwr(addr, data, len, IPCMSG_PCNTRL, IPC_CMD_PCNTRL_W);
390 }
391 EXPORT_SYMBOL(intel_scu_ipc_writev);
392
393
394 /**
395  *      intel_scu_ipc_update_register   -       r/m/w a register
396  *      @addr: register address
397  *      @bits: bits to update
398  *      @mask: mask of bits to update
399  *
400  *      Read-modify-write power control unit register. The first data argument
401  *      must be register value and second is mask value
402  *      mask is a bitmap that indicates which bits to update.
403  *      0 = masked. Don't modify this bit, 1 = modify this bit.
404  *      returns 0 on success or an error code.
405  *
406  *      This function may sleep. Locking between SCU accesses is handled
407  *      for the caller.
408  */
409 int intel_scu_ipc_update_register(u16 addr, u8 bits, u8 mask)
410 {
411         u8 data[2] = { bits, mask };
412         return pwr_reg_rdwr(&addr, data, 1, IPCMSG_PCNTRL, IPC_CMD_PCNTRL_M);
413 }
414 EXPORT_SYMBOL(intel_scu_ipc_update_register);
415
416 /**
417  *      intel_scu_ipc_register_read     -       32bit indirect read
418  *      @addr: register address
419  *      @value: 32bit value return
420  *
421  *      Performs IA 32 bit indirect read, returns 0 on success, or an
422  *      error code.
423  *
424  *      Can be used when SCCB(System Controller Configuration Block) register
425  *      HRIM(Honor Restricted IPC Messages) is set (bit 23)
426  *
427  *      This function may sleep. Locking for SCU accesses is handled for
428  *      the caller.
429  */
430 int intel_scu_ipc_register_read(u32 addr, u32 *value)
431 {
432         u32 err = 0;
433
434         mutex_lock(&ipclock);
435         if (ipcdev.pdev == NULL) {
436                 mutex_unlock(&ipclock);
437                 return -ENODEV;
438         }
439         ipc_write_sptr(addr);
440         ipc_command(4 << 16 | IPC_CMD_INDIRECT_RD);
441         err = busy_loop();
442         *value = ipc_data_readl(0);
443         mutex_unlock(&ipclock);
444         return err;
445 }
446 EXPORT_SYMBOL(intel_scu_ipc_register_read);
447
448 /**
449  *      intel_scu_ipc_register_write    -       32bit indirect write
450  *      @addr: register address
451  *      @value: 32bit value to write
452  *
453  *      Performs IA 32 bit indirect write, returns 0 on success, or an
454  *      error code.
455  *
456  *      Can be used when SCCB(System Controller Configuration Block) register
457  *      HRIM(Honor Restricted IPC Messages) is set (bit 23)
458  *
459  *      This function may sleep. Locking for SCU accesses is handled for
460  *      the caller.
461  */
462 int intel_scu_ipc_register_write(u32 addr, u32 value)
463 {
464         u32 err = 0;
465
466         mutex_lock(&ipclock);
467         if (ipcdev.pdev == NULL) {
468                 mutex_unlock(&ipclock);
469                 return -ENODEV;
470         }
471         ipc_write_dptr(addr);
472         ipc_data_writel(value, 0);
473         ipc_command(4 << 16 | IPC_CMD_INDIRECT_WR);
474         err = busy_loop();
475         mutex_unlock(&ipclock);
476         return err;
477 }
478 EXPORT_SYMBOL(intel_scu_ipc_register_write);
479
480 /**
481  *      intel_scu_ipc_simple_command    -       send a simple command
482  *      @cmd: command
483  *      @sub: sub type
484  *
485  *      Issue a simple command to the SCU. Do not use this interface if
486  *      you must then access data as any data values may be overwritten
487  *      by another SCU access by the time this function returns.
488  *
489  *      This function may sleep. Locking for SCU accesses is handled for
490  *      the caller.
491  */
492 int intel_scu_ipc_simple_command(int cmd, int sub)
493 {
494         u32 err = 0;
495
496         mutex_lock(&ipclock);
497         if (ipcdev.pdev == NULL) {
498                 mutex_unlock(&ipclock);
499                 return -ENODEV;
500         }
501         ipc_command(sub << 12 | cmd);
502         err = busy_loop();
503         mutex_unlock(&ipclock);
504         return err;
505 }
506 EXPORT_SYMBOL(intel_scu_ipc_simple_command);
507
508 /**
509  *      intel_scu_ipc_command   -       command with data
510  *      @cmd: command
511  *      @sub: sub type
512  *      @in: input data
513  *      @inlen: input length in dwords
514  *      @out: output data
515  *      @outlein: output length in dwords
516  *
517  *      Issue a command to the SCU which involves data transfers. Do the
518  *      data copies under the lock but leave it for the caller to interpret
519  */
520
521 int intel_scu_ipc_command(int cmd, int sub, u32 *in, int inlen,
522                                                         u32 *out, int outlen)
523 {
524         u32 err = 0;
525         int i = 0;
526
527         mutex_lock(&ipclock);
528         if (ipcdev.pdev == NULL) {
529                 mutex_unlock(&ipclock);
530                 return -ENODEV;
531         }
532
533         for (i = 0; i < inlen; i++)
534                 ipc_data_writel(*in++, 4 * i);
535
536         ipc_command((sub << 12) | cmd | (inlen << 18));
537         err = busy_loop();
538
539         for (i = 0; i < outlen; i++)
540                 *out++ = ipc_data_readl(4 * i);
541
542         mutex_unlock(&ipclock);
543         return err;
544 }
545 EXPORT_SYMBOL(intel_scu_ipc_command);
546
547 /*I2C commands */
548 #define IPC_I2C_WRITE 1 /* I2C Write command */
549 #define IPC_I2C_READ  2 /* I2C Read command */
550
551 /**
552  *      intel_scu_ipc_i2c_cntrl         -       I2C read/write operations
553  *      @addr: I2C address + command bits
554  *      @data: data to read/write
555  *
556  *      Perform an an I2C read/write operation via the SCU. All locking is
557  *      handled for the caller. This function may sleep.
558  *
559  *      Returns an error code or 0 on success.
560  *
561  *      This has to be in the IPC driver for the locking.
562  */
563 int intel_scu_ipc_i2c_cntrl(u32 addr, u32 *data)
564 {
565         u32 cmd = 0;
566
567         mutex_lock(&ipclock);
568         if (ipcdev.pdev == NULL) {
569                 mutex_unlock(&ipclock);
570                 return -ENODEV;
571         }
572         cmd = (addr >> 24) & 0xFF;
573         if (cmd == IPC_I2C_READ) {
574                 writel(addr, ipcdev.i2c_base + IPC_I2C_CNTRL_ADDR);
575                 /* Write not getting updated without delay */
576                 mdelay(1);
577                 *data = readl(ipcdev.i2c_base + I2C_DATA_ADDR);
578         } else if (cmd == IPC_I2C_WRITE) {
579                 writel(addr, ipcdev.i2c_base + I2C_DATA_ADDR);
580                 mdelay(1);
581                 writel(addr, ipcdev.i2c_base + IPC_I2C_CNTRL_ADDR);
582         } else {
583                 dev_err(&ipcdev.pdev->dev,
584                         "intel_scu_ipc: I2C INVALID_CMD = 0x%x\n", cmd);
585
586                 mutex_unlock(&ipclock);
587                 return -1;
588         }
589         mutex_unlock(&ipclock);
590         return 0;
591 }
592 EXPORT_SYMBOL(intel_scu_ipc_i2c_cntrl);
593
594 #define IPC_FW_LOAD_ADDR 0xFFFC0000 /* Storage location for FW image */
595 #define IPC_FW_UPDATE_MBOX_ADDR 0xFFFFDFF4 /* Mailbox between ipc and scu */
596 #define IPC_MAX_FW_SIZE 262144 /* 256K storage size for loading the FW image */
597 #define IPC_FW_MIP_HEADER_SIZE 2048 /* Firmware MIP header size */
598 /* IPC inform SCU to get ready for update process */
599 #define IPC_CMD_FW_UPDATE_READY  0x10FE
600 /* IPC inform SCU to go for update process */
601 #define IPC_CMD_FW_UPDATE_GO     0x20FE
602 /* Status code for fw update */
603 #define IPC_FW_UPDATE_SUCCESS   0x444f4e45 /* Status code 'DONE' */
604 #define IPC_FW_UPDATE_BADN      0x4241444E /* Status code 'BADN' */
605 #define IPC_FW_TXHIGH           0x54784849 /* Status code 'IPC_FW_TXHIGH' */
606 #define IPC_FW_TXLOW            0x54784c4f /* Status code 'IPC_FW_TXLOW' */
607
608 struct fw_update_mailbox {
609         u32    status;
610         u32    scu_flag;
611         u32    driver_flag;
612 };
613
614
615 /**
616  *      intel_scu_ipc_fw_update -        Firmware update utility
617  *      @buffer: firmware buffer
618  *      @length: size of firmware buffer
619  *
620  *      This function provides an interface to load the firmware into
621  *      the SCU. Returns 0 on success or -1 on failure
622  */
623 int intel_scu_ipc_fw_update(u8 *buffer, u32 length)
624 {
625         void __iomem *fw_update_base;
626         struct fw_update_mailbox __iomem *mailbox = NULL;
627         int retry_cnt = 0;
628         u32 status;
629
630         mutex_lock(&ipclock);
631         fw_update_base = ioremap_nocache(IPC_FW_LOAD_ADDR, (128*1024));
632         if (fw_update_base == NULL) {
633                 mutex_unlock(&ipclock);
634                 return -ENOMEM;
635         }
636         mailbox = ioremap_nocache(IPC_FW_UPDATE_MBOX_ADDR,
637                                         sizeof(struct fw_update_mailbox));
638         if (mailbox == NULL) {
639                 iounmap(fw_update_base);
640                 mutex_unlock(&ipclock);
641                 return -ENOMEM;
642         }
643
644         ipc_command(IPC_CMD_FW_UPDATE_READY);
645
646         /* Intitialize mailbox */
647         writel(0, &mailbox->status);
648         writel(0, &mailbox->scu_flag);
649         writel(0, &mailbox->driver_flag);
650
651         /* Driver copies the 2KB MIP header to SRAM at 0xFFFC0000*/
652         memcpy_toio(fw_update_base, buffer, 0x800);
653
654         /* Driver sends "FW Update" IPC command (CMD_ID 0xFE; MSG_ID 0x02).
655         * Upon receiving this command, SCU will write the 2K MIP header
656         * from 0xFFFC0000 into NAND.
657         * SCU will write a status code into the Mailbox, and then set scu_flag.
658         */
659
660         ipc_command(IPC_CMD_FW_UPDATE_GO);
661
662         /*Driver stalls until scu_flag is set */
663         while (readl(&mailbox->scu_flag) != 1) {
664                 rmb();
665                 mdelay(1);
666         }
667
668         /* Driver checks Mailbox status.
669          * If the status is 'BADN', then abort (bad NAND).
670          * If the status is 'IPC_FW_TXLOW', then continue.
671          */
672         while (readl(&mailbox->status) != IPC_FW_TXLOW) {
673                 rmb();
674                 mdelay(10);
675         }
676         mdelay(10);
677
678 update_retry:
679         if (retry_cnt > 5)
680                 goto update_end;
681
682         if (readl(&mailbox->status) != IPC_FW_TXLOW)
683                 goto update_end;
684         buffer = buffer + 0x800;
685         memcpy_toio(fw_update_base, buffer, 0x20000);
686         writel(1, &mailbox->driver_flag);
687         while (readl(&mailbox->scu_flag) == 1) {
688                 rmb();
689                 mdelay(1);
690         }
691
692         /* check for 'BADN' */
693         if (readl(&mailbox->status) == IPC_FW_UPDATE_BADN)
694                 goto update_end;
695
696         while (readl(&mailbox->status) != IPC_FW_TXHIGH) {
697                 rmb();
698                 mdelay(10);
699         }
700         mdelay(10);
701
702         if (readl(&mailbox->status) != IPC_FW_TXHIGH)
703                 goto update_end;
704
705         buffer = buffer + 0x20000;
706         memcpy_toio(fw_update_base, buffer, 0x20000);
707         writel(0, &mailbox->driver_flag);
708
709         while (mailbox->scu_flag == 0) {
710                 rmb();
711                 mdelay(1);
712         }
713
714         /* check for 'BADN' */
715         if (readl(&mailbox->status) == IPC_FW_UPDATE_BADN)
716                 goto update_end;
717
718         if (readl(&mailbox->status) == IPC_FW_TXLOW) {
719                 ++retry_cnt;
720                 goto update_retry;
721         }
722
723 update_end:
724         status = readl(&mailbox->status);
725
726         iounmap(fw_update_base);
727         iounmap(mailbox);
728         mutex_unlock(&ipclock);
729
730         if (status == IPC_FW_UPDATE_SUCCESS)
731                 return 0;
732         return -1;
733 }
734 EXPORT_SYMBOL(intel_scu_ipc_fw_update);
735
736 /*
737  * Interrupt handler gets called when ioc bit of IPC_COMMAND_REG set to 1
738  * When ioc bit is set to 1, caller api must wait for interrupt handler called
739  * which in turn unlocks the caller api. Currently this is not used
740  *
741  * This is edge triggered so we need take no action to clear anything
742  */
743 static irqreturn_t ioc(int irq, void *dev_id)
744 {
745         return IRQ_HANDLED;
746 }
747
748 /**
749  *      ipc_probe       -       probe an Intel SCU IPC
750  *      @dev: the PCI device matching
751  *      @id: entry in the match table
752  *
753  *      Enable and install an intel SCU IPC. This appears in the PCI space
754  *      but uses some hard coded addresses as well.
755  */
756 static int ipc_probe(struct pci_dev *dev, const struct pci_device_id *id)
757 {
758         int err;
759         resource_size_t pci_resource;
760
761         if (ipcdev.pdev)                /* We support only one SCU */
762                 return -EBUSY;
763
764         ipcdev.pdev = pci_dev_get(dev);
765
766         err = pci_enable_device(dev);
767         if (err)
768                 return err;
769
770         err = pci_request_regions(dev, "intel_scu_ipc");
771         if (err)
772                 return err;
773
774         pci_resource = pci_resource_start(dev, 0);
775         if (!pci_resource)
776                 return -ENOMEM;
777
778         if (request_irq(dev->irq, ioc, 0, "intel_scu_ipc", &ipcdev))
779                 return -EBUSY;
780
781         ipcdev.ipc_base = ioremap_nocache(IPC_BASE_ADDR, IPC_MAX_ADDR);
782         if (!ipcdev.ipc_base)
783                 return -ENOMEM;
784
785         ipcdev.i2c_base = ioremap_nocache(IPC_I2C_BASE, IPC_I2C_MAX_ADDR);
786         if (!ipcdev.i2c_base) {
787                 iounmap(ipcdev.ipc_base);
788                 return -ENOMEM;
789         }
790         return 0;
791 }
792
793 /**
794  *      ipc_remove      -       remove a bound IPC device
795  *      @pdev: PCI device
796  *
797  *      In practice the SCU is not removable but this function is also
798  *      called for each device on a module unload or cleanup which is the
799  *      path that will get used.
800  *
801  *      Free up the mappings and release the PCI resources
802  */
803 static void ipc_remove(struct pci_dev *pdev)
804 {
805         free_irq(pdev->irq, &ipcdev);
806         pci_release_regions(pdev);
807         pci_dev_put(ipcdev.pdev);
808         iounmap(ipcdev.ipc_base);
809         iounmap(ipcdev.i2c_base);
810         ipcdev.pdev = NULL;
811 }
812
813 static const struct pci_device_id pci_ids[] = {
814         {PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x080e)},
815         {PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x082a)},
816         { 0,}
817 };
818 MODULE_DEVICE_TABLE(pci, pci_ids);
819
820 static struct pci_driver ipc_driver = {
821         .name = "intel_scu_ipc",
822         .id_table = pci_ids,
823         .probe = ipc_probe,
824         .remove = ipc_remove,
825 };
826
827
828 static int __init intel_scu_ipc_init(void)
829 {
830         if (boot_cpu_data.x86 == 6 &&
831                 boot_cpu_data.x86_model == 0x27 &&
832                 boot_cpu_data.x86_mask == 1)
833                         platform = PLATFORM_PENWELL;
834         else if (boot_cpu_data.x86 == 6 &&
835                 boot_cpu_data.x86_model == 0x26)
836                         platform = PLATFORM_LANGWELL;
837
838         return  pci_register_driver(&ipc_driver);
839 }
840
841 static void __exit intel_scu_ipc_exit(void)
842 {
843         pci_unregister_driver(&ipc_driver);
844 }
845
846 MODULE_AUTHOR("Sreedhara DS <sreedhara.ds@intel.com>");
847 MODULE_DESCRIPTION("Intel SCU IPC driver");
848 MODULE_LICENSE("GPL");
849
850 module_init(intel_scu_ipc_init);
851 module_exit(intel_scu_ipc_exit);