intel_scu_ipc.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688
  1. /*
  2. * intel_scu_ipc.c: Driver for the Intel SCU IPC mechanism
  3. *
  4. * (C) Copyright 2008-2010,2015 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 running 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/device.h>
  23. #include <linux/pm.h>
  24. #include <linux/pci.h>
  25. #include <linux/interrupt.h>
  26. #include <linux/sfi.h>
  27. #include <asm/intel-mid.h>
  28. #include <asm/intel_scu_ipc.h>
  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. /* Command id associated with message IPCMSG_PCNTRL */
  36. #define IPC_CMD_PCNTRL_W 0 /* Register write */
  37. #define IPC_CMD_PCNTRL_R 1 /* Register read */
  38. #define IPC_CMD_PCNTRL_M 2 /* Register read-modify-write */
  39. /*
  40. * IPC register summary
  41. *
  42. * IPC register blocks are memory mapped at fixed address of PCI BAR 0.
  43. * To read or write information to the SCU, driver writes to IPC-1 memory
  44. * mapped registers. The following is the IPC mechanism
  45. *
  46. * 1. IA core cDMI interface claims this transaction and converts it to a
  47. * Transaction Layer Packet (TLP) message which is sent across the cDMI.
  48. *
  49. * 2. South Complex cDMI block receives this message and writes it to
  50. * the IPC-1 register block, causing an interrupt to the SCU
  51. *
  52. * 3. SCU firmware decodes this interrupt and IPC message and the appropriate
  53. * message handler is called within firmware.
  54. */
  55. #define IPC_WWBUF_SIZE 20 /* IPC Write buffer Size */
  56. #define IPC_RWBUF_SIZE 20 /* IPC Read buffer Size */
  57. #define IPC_IOC 0x100 /* IPC command register IOC bit */
  58. #define PCI_DEVICE_ID_LINCROFT 0x082a
  59. #define PCI_DEVICE_ID_PENWELL 0x080e
  60. #define PCI_DEVICE_ID_CLOVERVIEW 0x08ea
  61. #define PCI_DEVICE_ID_TANGIER 0x11a0
  62. /* intel scu ipc driver data */
  63. struct intel_scu_ipc_pdata_t {
  64. u32 i2c_base;
  65. u32 i2c_len;
  66. };
  67. static const struct intel_scu_ipc_pdata_t intel_scu_ipc_lincroft_pdata = {
  68. .i2c_base = 0xff12b000,
  69. .i2c_len = 0x10,
  70. };
  71. /* Penwell and Cloverview */
  72. static const struct intel_scu_ipc_pdata_t intel_scu_ipc_penwell_pdata = {
  73. .i2c_base = 0xff12b000,
  74. .i2c_len = 0x10,
  75. };
  76. static const struct intel_scu_ipc_pdata_t intel_scu_ipc_tangier_pdata = {
  77. .i2c_base = 0xff00d000,
  78. .i2c_len = 0x10,
  79. };
  80. struct intel_scu_ipc_dev {
  81. struct device *dev;
  82. void __iomem *ipc_base;
  83. void __iomem *i2c_base;
  84. struct completion cmd_complete;
  85. u8 irq_mode;
  86. };
  87. static struct intel_scu_ipc_dev ipcdev; /* Only one for now */
  88. #define IPC_STATUS 0x04
  89. #define IPC_STATUS_IRQ BIT(2)
  90. /*
  91. * IPC Read Buffer (Read Only):
  92. * 16 byte buffer for receiving data from SCU, if IPC command
  93. * processing results in response data
  94. */
  95. #define IPC_READ_BUFFER 0x90
  96. #define IPC_I2C_CNTRL_ADDR 0
  97. #define I2C_DATA_ADDR 0x04
  98. static DEFINE_MUTEX(ipclock); /* lock used to prevent multiple call to SCU */
  99. /*
  100. * Send ipc command
  101. * Command Register (Write Only):
  102. * A write to this register results in an interrupt to the SCU core processor
  103. * Format:
  104. * |rfu2(8) | size(8) | command id(4) | rfu1(3) | ioc(1) | command(8)|
  105. */
  106. static inline void ipc_command(struct intel_scu_ipc_dev *scu, u32 cmd)
  107. {
  108. reinit_completion(&scu->cmd_complete);
  109. writel(cmd | IPC_IOC, scu->ipc_base);
  110. }
  111. /*
  112. * Write ipc data
  113. * IPC Write Buffer (Write Only):
  114. * 16-byte buffer for sending data associated with IPC command to
  115. * SCU. Size of the data is specified in the IPC_COMMAND_REG register
  116. */
  117. static inline void ipc_data_writel(struct intel_scu_ipc_dev *scu, u32 data, u32 offset)
  118. {
  119. writel(data, scu->ipc_base + 0x80 + offset);
  120. }
  121. /*
  122. * Status Register (Read Only):
  123. * Driver will read this register to get the ready/busy status of the IPC
  124. * block and error status of the IPC command that was just processed by SCU
  125. * Format:
  126. * |rfu3(8)|error code(8)|initiator id(8)|cmd id(4)|rfu1(2)|error(1)|busy(1)|
  127. */
  128. static inline u8 ipc_read_status(struct intel_scu_ipc_dev *scu)
  129. {
  130. return __raw_readl(scu->ipc_base + 0x04);
  131. }
  132. /* Read ipc byte data */
  133. static inline u8 ipc_data_readb(struct intel_scu_ipc_dev *scu, u32 offset)
  134. {
  135. return readb(scu->ipc_base + IPC_READ_BUFFER + offset);
  136. }
  137. /* Read ipc u32 data */
  138. static inline u32 ipc_data_readl(struct intel_scu_ipc_dev *scu, u32 offset)
  139. {
  140. return readl(scu->ipc_base + IPC_READ_BUFFER + offset);
  141. }
  142. /* Wait till scu status is busy */
  143. static inline int busy_loop(struct intel_scu_ipc_dev *scu)
  144. {
  145. u32 status = ipc_read_status(scu);
  146. u32 loop_count = 100000;
  147. /* break if scu doesn't reset busy bit after huge retry */
  148. while ((status & BIT(0)) && --loop_count) {
  149. udelay(1); /* scu processing time is in few u secods */
  150. status = ipc_read_status(scu);
  151. }
  152. if (status & BIT(0)) {
  153. dev_err(scu->dev, "IPC timed out");
  154. return -ETIMEDOUT;
  155. }
  156. if (status & BIT(1))
  157. return -EIO;
  158. return 0;
  159. }
  160. /* Wait till ipc ioc interrupt is received or timeout in 3 HZ */
  161. static inline int ipc_wait_for_interrupt(struct intel_scu_ipc_dev *scu)
  162. {
  163. int status;
  164. if (!wait_for_completion_timeout(&scu->cmd_complete, 3 * HZ)) {
  165. dev_err(scu->dev, "IPC timed out\n");
  166. return -ETIMEDOUT;
  167. }
  168. status = ipc_read_status(scu);
  169. if (status & BIT(1))
  170. return -EIO;
  171. return 0;
  172. }
  173. static int intel_scu_ipc_check_status(struct intel_scu_ipc_dev *scu)
  174. {
  175. return scu->irq_mode ? ipc_wait_for_interrupt(scu) : busy_loop(scu);
  176. }
  177. /* Read/Write power control(PMIC in Langwell, MSIC in PenWell) registers */
  178. static int pwr_reg_rdwr(u16 *addr, u8 *data, u32 count, u32 op, u32 id)
  179. {
  180. struct intel_scu_ipc_dev *scu = &ipcdev;
  181. int nc;
  182. u32 offset = 0;
  183. int err;
  184. u8 cbuf[IPC_WWBUF_SIZE];
  185. u32 *wbuf = (u32 *)&cbuf;
  186. memset(cbuf, 0, sizeof(cbuf));
  187. mutex_lock(&ipclock);
  188. if (scu->dev == NULL) {
  189. mutex_unlock(&ipclock);
  190. return -ENODEV;
  191. }
  192. for (nc = 0; nc < count; nc++, offset += 2) {
  193. cbuf[offset] = addr[nc];
  194. cbuf[offset + 1] = addr[nc] >> 8;
  195. }
  196. if (id == IPC_CMD_PCNTRL_R) {
  197. for (nc = 0, offset = 0; nc < count; nc++, offset += 4)
  198. ipc_data_writel(scu, wbuf[nc], offset);
  199. ipc_command(scu, (count * 2) << 16 | id << 12 | 0 << 8 | op);
  200. } else if (id == IPC_CMD_PCNTRL_W) {
  201. for (nc = 0; nc < count; nc++, offset += 1)
  202. cbuf[offset] = data[nc];
  203. for (nc = 0, offset = 0; nc < count; nc++, offset += 4)
  204. ipc_data_writel(scu, wbuf[nc], offset);
  205. ipc_command(scu, (count * 3) << 16 | id << 12 | 0 << 8 | op);
  206. } else if (id == IPC_CMD_PCNTRL_M) {
  207. cbuf[offset] = data[0];
  208. cbuf[offset + 1] = data[1];
  209. ipc_data_writel(scu, wbuf[0], 0); /* Write wbuff */
  210. ipc_command(scu, 4 << 16 | id << 12 | 0 << 8 | op);
  211. }
  212. err = intel_scu_ipc_check_status(scu);
  213. if (!err && id == IPC_CMD_PCNTRL_R) { /* Read rbuf */
  214. /* Workaround: values are read as 0 without memcpy_fromio */
  215. memcpy_fromio(cbuf, scu->ipc_base + 0x90, 16);
  216. for (nc = 0; nc < count; nc++)
  217. data[nc] = ipc_data_readb(scu, nc);
  218. }
  219. mutex_unlock(&ipclock);
  220. return err;
  221. }
  222. /**
  223. * intel_scu_ipc_ioread8 - read a word via the SCU
  224. * @addr: register on SCU
  225. * @data: return pointer for read byte
  226. *
  227. * Read a single register. Returns 0 on success or an error code. All
  228. * locking between SCU accesses is handled for the caller.
  229. *
  230. * This function may sleep.
  231. */
  232. int intel_scu_ipc_ioread8(u16 addr, u8 *data)
  233. {
  234. return pwr_reg_rdwr(&addr, data, 1, IPCMSG_PCNTRL, IPC_CMD_PCNTRL_R);
  235. }
  236. EXPORT_SYMBOL(intel_scu_ipc_ioread8);
  237. /**
  238. * intel_scu_ipc_ioread16 - read a word via the SCU
  239. * @addr: register on SCU
  240. * @data: return pointer for read word
  241. *
  242. * Read a register pair. Returns 0 on success or an error code. All
  243. * locking between SCU accesses is handled for the caller.
  244. *
  245. * This function may sleep.
  246. */
  247. int intel_scu_ipc_ioread16(u16 addr, u16 *data)
  248. {
  249. u16 x[2] = {addr, addr + 1};
  250. return pwr_reg_rdwr(x, (u8 *)data, 2, IPCMSG_PCNTRL, IPC_CMD_PCNTRL_R);
  251. }
  252. EXPORT_SYMBOL(intel_scu_ipc_ioread16);
  253. /**
  254. * intel_scu_ipc_ioread32 - read a dword via the SCU
  255. * @addr: register on SCU
  256. * @data: return pointer for read dword
  257. *
  258. * Read four registers. Returns 0 on success or an error code. All
  259. * locking between SCU accesses is handled for the caller.
  260. *
  261. * This function may sleep.
  262. */
  263. int intel_scu_ipc_ioread32(u16 addr, u32 *data)
  264. {
  265. u16 x[4] = {addr, addr + 1, addr + 2, addr + 3};
  266. return pwr_reg_rdwr(x, (u8 *)data, 4, IPCMSG_PCNTRL, IPC_CMD_PCNTRL_R);
  267. }
  268. EXPORT_SYMBOL(intel_scu_ipc_ioread32);
  269. /**
  270. * intel_scu_ipc_iowrite8 - write a byte via the SCU
  271. * @addr: register on SCU
  272. * @data: byte to write
  273. *
  274. * Write a single register. Returns 0 on success or an error code. All
  275. * locking between SCU accesses is handled for the caller.
  276. *
  277. * This function may sleep.
  278. */
  279. int intel_scu_ipc_iowrite8(u16 addr, u8 data)
  280. {
  281. return pwr_reg_rdwr(&addr, &data, 1, IPCMSG_PCNTRL, IPC_CMD_PCNTRL_W);
  282. }
  283. EXPORT_SYMBOL(intel_scu_ipc_iowrite8);
  284. /**
  285. * intel_scu_ipc_iowrite16 - write a word via the SCU
  286. * @addr: register on SCU
  287. * @data: word to write
  288. *
  289. * Write two registers. Returns 0 on success or an error code. All
  290. * locking between SCU accesses is handled for the caller.
  291. *
  292. * This function may sleep.
  293. */
  294. int intel_scu_ipc_iowrite16(u16 addr, u16 data)
  295. {
  296. u16 x[2] = {addr, addr + 1};
  297. return pwr_reg_rdwr(x, (u8 *)&data, 2, IPCMSG_PCNTRL, IPC_CMD_PCNTRL_W);
  298. }
  299. EXPORT_SYMBOL(intel_scu_ipc_iowrite16);
  300. /**
  301. * intel_scu_ipc_iowrite32 - write a dword via the SCU
  302. * @addr: register on SCU
  303. * @data: dword to write
  304. *
  305. * Write four registers. Returns 0 on success or an error code. All
  306. * locking between SCU accesses is handled for the caller.
  307. *
  308. * This function may sleep.
  309. */
  310. int intel_scu_ipc_iowrite32(u16 addr, u32 data)
  311. {
  312. u16 x[4] = {addr, addr + 1, addr + 2, addr + 3};
  313. return pwr_reg_rdwr(x, (u8 *)&data, 4, IPCMSG_PCNTRL, IPC_CMD_PCNTRL_W);
  314. }
  315. EXPORT_SYMBOL(intel_scu_ipc_iowrite32);
  316. /**
  317. * intel_scu_ipc_readvv - read a set of registers
  318. * @addr: register list
  319. * @data: bytes to return
  320. * @len: length of array
  321. *
  322. * Read registers. Returns 0 on success or an error code. All
  323. * locking between SCU accesses is handled for the caller.
  324. *
  325. * The largest array length permitted by the hardware is 5 items.
  326. *
  327. * This function may sleep.
  328. */
  329. int intel_scu_ipc_readv(u16 *addr, u8 *data, int len)
  330. {
  331. return pwr_reg_rdwr(addr, data, len, IPCMSG_PCNTRL, IPC_CMD_PCNTRL_R);
  332. }
  333. EXPORT_SYMBOL(intel_scu_ipc_readv);
  334. /**
  335. * intel_scu_ipc_writev - write a set of registers
  336. * @addr: register list
  337. * @data: bytes to write
  338. * @len: length of array
  339. *
  340. * Write registers. Returns 0 on success or an error code. All
  341. * locking between SCU accesses is handled for the caller.
  342. *
  343. * The largest array length permitted by the hardware is 5 items.
  344. *
  345. * This function may sleep.
  346. *
  347. */
  348. int intel_scu_ipc_writev(u16 *addr, u8 *data, int len)
  349. {
  350. return pwr_reg_rdwr(addr, data, len, IPCMSG_PCNTRL, IPC_CMD_PCNTRL_W);
  351. }
  352. EXPORT_SYMBOL(intel_scu_ipc_writev);
  353. /**
  354. * intel_scu_ipc_update_register - r/m/w a register
  355. * @addr: register address
  356. * @bits: bits to update
  357. * @mask: mask of bits to update
  358. *
  359. * Read-modify-write power control unit register. The first data argument
  360. * must be register value and second is mask value
  361. * mask is a bitmap that indicates which bits to update.
  362. * 0 = masked. Don't modify this bit, 1 = modify this bit.
  363. * returns 0 on success or an error code.
  364. *
  365. * This function may sleep. Locking between SCU accesses is handled
  366. * for the caller.
  367. */
  368. int intel_scu_ipc_update_register(u16 addr, u8 bits, u8 mask)
  369. {
  370. u8 data[2] = { bits, mask };
  371. return pwr_reg_rdwr(&addr, data, 1, IPCMSG_PCNTRL, IPC_CMD_PCNTRL_M);
  372. }
  373. EXPORT_SYMBOL(intel_scu_ipc_update_register);
  374. /**
  375. * intel_scu_ipc_simple_command - send a simple command
  376. * @cmd: command
  377. * @sub: sub type
  378. *
  379. * Issue a simple command to the SCU. Do not use this interface if
  380. * you must then access data as any data values may be overwritten
  381. * by another SCU access by the time this function returns.
  382. *
  383. * This function may sleep. Locking for SCU accesses is handled for
  384. * the caller.
  385. */
  386. int intel_scu_ipc_simple_command(int cmd, int sub)
  387. {
  388. struct intel_scu_ipc_dev *scu = &ipcdev;
  389. int err;
  390. mutex_lock(&ipclock);
  391. if (scu->dev == NULL) {
  392. mutex_unlock(&ipclock);
  393. return -ENODEV;
  394. }
  395. ipc_command(scu, sub << 12 | cmd);
  396. err = intel_scu_ipc_check_status(scu);
  397. mutex_unlock(&ipclock);
  398. return err;
  399. }
  400. EXPORT_SYMBOL(intel_scu_ipc_simple_command);
  401. /**
  402. * intel_scu_ipc_command - command with data
  403. * @cmd: command
  404. * @sub: sub type
  405. * @in: input data
  406. * @inlen: input length in dwords
  407. * @out: output data
  408. * @outlein: output length in dwords
  409. *
  410. * Issue a command to the SCU which involves data transfers. Do the
  411. * data copies under the lock but leave it for the caller to interpret
  412. */
  413. int intel_scu_ipc_command(int cmd, int sub, u32 *in, int inlen,
  414. u32 *out, int outlen)
  415. {
  416. struct intel_scu_ipc_dev *scu = &ipcdev;
  417. int i, err;
  418. mutex_lock(&ipclock);
  419. if (scu->dev == NULL) {
  420. mutex_unlock(&ipclock);
  421. return -ENODEV;
  422. }
  423. for (i = 0; i < inlen; i++)
  424. ipc_data_writel(scu, *in++, 4 * i);
  425. ipc_command(scu, (inlen << 16) | (sub << 12) | cmd);
  426. err = intel_scu_ipc_check_status(scu);
  427. if (!err) {
  428. for (i = 0; i < outlen; i++)
  429. *out++ = ipc_data_readl(scu, 4 * i);
  430. }
  431. mutex_unlock(&ipclock);
  432. return err;
  433. }
  434. EXPORT_SYMBOL(intel_scu_ipc_command);
  435. #define IPC_SPTR 0x08
  436. #define IPC_DPTR 0x0C
  437. /**
  438. * intel_scu_ipc_raw_command() - IPC command with data and pointers
  439. * @cmd: IPC command code.
  440. * @sub: IPC command sub type.
  441. * @in: input data of this IPC command.
  442. * @inlen: input data length in dwords.
  443. * @out: output data of this IPC command.
  444. * @outlen: output data length in dwords.
  445. * @sptr: data writing to SPTR register.
  446. * @dptr: data writing to DPTR register.
  447. *
  448. * Send an IPC command to SCU with input/output data and source/dest pointers.
  449. *
  450. * Return: an IPC error code or 0 on success.
  451. */
  452. int intel_scu_ipc_raw_command(int cmd, int sub, u8 *in, int inlen,
  453. u32 *out, int outlen, u32 dptr, u32 sptr)
  454. {
  455. struct intel_scu_ipc_dev *scu = &ipcdev;
  456. int inbuflen = DIV_ROUND_UP(inlen, 4);
  457. u32 inbuf[4];
  458. int i, err;
  459. /* Up to 16 bytes */
  460. if (inbuflen > 4)
  461. return -EINVAL;
  462. mutex_lock(&ipclock);
  463. if (scu->dev == NULL) {
  464. mutex_unlock(&ipclock);
  465. return -ENODEV;
  466. }
  467. writel(dptr, scu->ipc_base + IPC_DPTR);
  468. writel(sptr, scu->ipc_base + IPC_SPTR);
  469. /*
  470. * SRAM controller doesn't support 8-bit writes, it only
  471. * supports 32-bit writes, so we have to copy input data into
  472. * the temporary buffer, and SCU FW will use the inlen to
  473. * determine the actual input data length in the temporary
  474. * buffer.
  475. */
  476. memcpy(inbuf, in, inlen);
  477. for (i = 0; i < inbuflen; i++)
  478. ipc_data_writel(scu, inbuf[i], 4 * i);
  479. ipc_command(scu, (inlen << 16) | (sub << 12) | cmd);
  480. err = intel_scu_ipc_check_status(scu);
  481. if (!err) {
  482. for (i = 0; i < outlen; i++)
  483. *out++ = ipc_data_readl(scu, 4 * i);
  484. }
  485. mutex_unlock(&ipclock);
  486. return err;
  487. }
  488. EXPORT_SYMBOL_GPL(intel_scu_ipc_raw_command);
  489. /* I2C commands */
  490. #define IPC_I2C_WRITE 1 /* I2C Write command */
  491. #define IPC_I2C_READ 2 /* I2C Read command */
  492. /**
  493. * intel_scu_ipc_i2c_cntrl - I2C read/write operations
  494. * @addr: I2C address + command bits
  495. * @data: data to read/write
  496. *
  497. * Perform an an I2C read/write operation via the SCU. All locking is
  498. * handled for the caller. This function may sleep.
  499. *
  500. * Returns an error code or 0 on success.
  501. *
  502. * This has to be in the IPC driver for the locking.
  503. */
  504. int intel_scu_ipc_i2c_cntrl(u32 addr, u32 *data)
  505. {
  506. struct intel_scu_ipc_dev *scu = &ipcdev;
  507. u32 cmd = 0;
  508. mutex_lock(&ipclock);
  509. if (scu->dev == NULL) {
  510. mutex_unlock(&ipclock);
  511. return -ENODEV;
  512. }
  513. cmd = (addr >> 24) & 0xFF;
  514. if (cmd == IPC_I2C_READ) {
  515. writel(addr, scu->i2c_base + IPC_I2C_CNTRL_ADDR);
  516. /* Write not getting updated without delay */
  517. usleep_range(1000, 2000);
  518. *data = readl(scu->i2c_base + I2C_DATA_ADDR);
  519. } else if (cmd == IPC_I2C_WRITE) {
  520. writel(*data, scu->i2c_base + I2C_DATA_ADDR);
  521. usleep_range(1000, 2000);
  522. writel(addr, scu->i2c_base + IPC_I2C_CNTRL_ADDR);
  523. } else {
  524. dev_err(scu->dev,
  525. "intel_scu_ipc: I2C INVALID_CMD = 0x%x\n", cmd);
  526. mutex_unlock(&ipclock);
  527. return -EIO;
  528. }
  529. mutex_unlock(&ipclock);
  530. return 0;
  531. }
  532. EXPORT_SYMBOL(intel_scu_ipc_i2c_cntrl);
  533. /*
  534. * Interrupt handler gets called when ioc bit of IPC_COMMAND_REG set to 1
  535. * When ioc bit is set to 1, caller api must wait for interrupt handler called
  536. * which in turn unlocks the caller api. Currently this is not used
  537. *
  538. * This is edge triggered so we need take no action to clear anything
  539. */
  540. static irqreturn_t ioc(int irq, void *dev_id)
  541. {
  542. struct intel_scu_ipc_dev *scu = dev_id;
  543. int status = ipc_read_status(scu);
  544. writel(status | IPC_STATUS_IRQ, scu->ipc_base + IPC_STATUS);
  545. complete(&scu->cmd_complete);
  546. return IRQ_HANDLED;
  547. }
  548. /**
  549. * ipc_probe - probe an Intel SCU IPC
  550. * @pdev: the PCI device matching
  551. * @id: entry in the match table
  552. *
  553. * Enable and install an intel SCU IPC. This appears in the PCI space
  554. * but uses some hard coded addresses as well.
  555. */
  556. static int ipc_probe(struct pci_dev *pdev, const struct pci_device_id *id)
  557. {
  558. int err;
  559. struct intel_scu_ipc_dev *scu = &ipcdev;
  560. struct intel_scu_ipc_pdata_t *pdata;
  561. if (scu->dev) /* We support only one SCU */
  562. return -EBUSY;
  563. pdata = (struct intel_scu_ipc_pdata_t *)id->driver_data;
  564. if (!pdata)
  565. return -ENODEV;
  566. err = pcim_enable_device(pdev);
  567. if (err)
  568. return err;
  569. err = pcim_iomap_regions(pdev, 1 << 0, pci_name(pdev));
  570. if (err)
  571. return err;
  572. init_completion(&scu->cmd_complete);
  573. scu->ipc_base = pcim_iomap_table(pdev)[0];
  574. scu->i2c_base = ioremap_nocache(pdata->i2c_base, pdata->i2c_len);
  575. if (!scu->i2c_base)
  576. return -ENOMEM;
  577. err = devm_request_irq(&pdev->dev, pdev->irq, ioc, 0, "intel_scu_ipc",
  578. scu);
  579. if (err)
  580. return err;
  581. /* Assign device at last */
  582. scu->dev = &pdev->dev;
  583. intel_scu_devices_create();
  584. pci_set_drvdata(pdev, scu);
  585. return 0;
  586. }
  587. #define SCU_DEVICE(id, pdata) {PCI_VDEVICE(INTEL, id), (kernel_ulong_t)&pdata}
  588. static const struct pci_device_id pci_ids[] = {
  589. SCU_DEVICE(PCI_DEVICE_ID_LINCROFT, intel_scu_ipc_lincroft_pdata),
  590. SCU_DEVICE(PCI_DEVICE_ID_PENWELL, intel_scu_ipc_penwell_pdata),
  591. SCU_DEVICE(PCI_DEVICE_ID_CLOVERVIEW, intel_scu_ipc_penwell_pdata),
  592. SCU_DEVICE(PCI_DEVICE_ID_TANGIER, intel_scu_ipc_tangier_pdata),
  593. {}
  594. };
  595. static struct pci_driver ipc_driver = {
  596. .driver = {
  597. .suppress_bind_attrs = true,
  598. },
  599. .name = "intel_scu_ipc",
  600. .id_table = pci_ids,
  601. .probe = ipc_probe,
  602. };
  603. builtin_pci_driver(ipc_driver);