intel_scu_ipc.c 20 KB

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