intel_scu_ipc.c 22 KB

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