intel_scu_ipc.c 19 KB

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