intel_punit_ipc.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. /*
  2. * Driver for the Intel P-Unit Mailbox IPC mechanism
  3. *
  4. * (C) Copyright 2015 Intel Corporation
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. *
  10. * The heart of the P-Unit is the Foxton microcontroller and its firmware,
  11. * which provide mailbox interface for power management usage.
  12. */
  13. #include <linux/module.h>
  14. #include <linux/acpi.h>
  15. #include <linux/delay.h>
  16. #include <linux/bitops.h>
  17. #include <linux/device.h>
  18. #include <linux/interrupt.h>
  19. #include <linux/platform_device.h>
  20. #include <asm/intel_punit_ipc.h>
  21. /* IPC Mailbox registers */
  22. #define OFFSET_DATA_LOW 0x0
  23. #define OFFSET_DATA_HIGH 0x4
  24. /* bit field of interface register */
  25. #define CMD_RUN BIT(31)
  26. #define CMD_ERRCODE_MASK GENMASK(7, 0)
  27. #define CMD_PARA1_SHIFT 8
  28. #define CMD_PARA2_SHIFT 16
  29. #define CMD_TIMEOUT_SECONDS 1
  30. enum {
  31. BASE_DATA = 0,
  32. BASE_IFACE,
  33. BASE_MAX,
  34. };
  35. typedef struct {
  36. struct device *dev;
  37. struct mutex lock;
  38. int irq;
  39. struct completion cmd_complete;
  40. /* base of interface and data registers */
  41. void __iomem *base[RESERVED_IPC][BASE_MAX];
  42. IPC_TYPE type;
  43. } IPC_DEV;
  44. static IPC_DEV *punit_ipcdev;
  45. static inline u32 ipc_read_status(IPC_DEV *ipcdev, IPC_TYPE type)
  46. {
  47. return readl(ipcdev->base[type][BASE_IFACE]);
  48. }
  49. static inline void ipc_write_cmd(IPC_DEV *ipcdev, IPC_TYPE type, u32 cmd)
  50. {
  51. writel(cmd, ipcdev->base[type][BASE_IFACE]);
  52. }
  53. static inline u32 ipc_read_data_low(IPC_DEV *ipcdev, IPC_TYPE type)
  54. {
  55. return readl(ipcdev->base[type][BASE_DATA] + OFFSET_DATA_LOW);
  56. }
  57. static inline u32 ipc_read_data_high(IPC_DEV *ipcdev, IPC_TYPE type)
  58. {
  59. return readl(ipcdev->base[type][BASE_DATA] + OFFSET_DATA_HIGH);
  60. }
  61. static inline void ipc_write_data_low(IPC_DEV *ipcdev, IPC_TYPE type, u32 data)
  62. {
  63. writel(data, ipcdev->base[type][BASE_DATA] + OFFSET_DATA_LOW);
  64. }
  65. static inline void ipc_write_data_high(IPC_DEV *ipcdev, IPC_TYPE type, u32 data)
  66. {
  67. writel(data, ipcdev->base[type][BASE_DATA] + OFFSET_DATA_HIGH);
  68. }
  69. static const char *ipc_err_string(int error)
  70. {
  71. if (error == IPC_PUNIT_ERR_SUCCESS)
  72. return "no error";
  73. else if (error == IPC_PUNIT_ERR_INVALID_CMD)
  74. return "invalid command";
  75. else if (error == IPC_PUNIT_ERR_INVALID_PARAMETER)
  76. return "invalid parameter";
  77. else if (error == IPC_PUNIT_ERR_CMD_TIMEOUT)
  78. return "command timeout";
  79. else if (error == IPC_PUNIT_ERR_CMD_LOCKED)
  80. return "command locked";
  81. else if (error == IPC_PUNIT_ERR_INVALID_VR_ID)
  82. return "invalid vr id";
  83. else if (error == IPC_PUNIT_ERR_VR_ERR)
  84. return "vr error";
  85. else
  86. return "unknown error";
  87. }
  88. static int intel_punit_ipc_check_status(IPC_DEV *ipcdev, IPC_TYPE type)
  89. {
  90. int loops = CMD_TIMEOUT_SECONDS * USEC_PER_SEC;
  91. int errcode;
  92. int status;
  93. if (ipcdev->irq) {
  94. if (!wait_for_completion_timeout(&ipcdev->cmd_complete,
  95. CMD_TIMEOUT_SECONDS * HZ)) {
  96. dev_err(ipcdev->dev, "IPC timed out\n");
  97. return -ETIMEDOUT;
  98. }
  99. } else {
  100. while ((ipc_read_status(ipcdev, type) & CMD_RUN) && --loops)
  101. udelay(1);
  102. if (!loops) {
  103. dev_err(ipcdev->dev, "IPC timed out\n");
  104. return -ETIMEDOUT;
  105. }
  106. }
  107. status = ipc_read_status(ipcdev, type);
  108. errcode = status & CMD_ERRCODE_MASK;
  109. if (errcode) {
  110. dev_err(ipcdev->dev, "IPC failed: %s, IPC_STS=0x%x\n",
  111. ipc_err_string(errcode), status);
  112. return -EIO;
  113. }
  114. return 0;
  115. }
  116. /**
  117. * intel_punit_ipc_simple_command() - Simple IPC command
  118. * @cmd: IPC command code.
  119. * @para1: First 8bit parameter, set 0 if not used.
  120. * @para2: Second 8bit parameter, set 0 if not used.
  121. *
  122. * Send a IPC command to P-Unit when there is no data transaction
  123. *
  124. * Return: IPC error code or 0 on success.
  125. */
  126. int intel_punit_ipc_simple_command(int cmd, int para1, int para2)
  127. {
  128. IPC_DEV *ipcdev = punit_ipcdev;
  129. IPC_TYPE type;
  130. u32 val;
  131. int ret;
  132. mutex_lock(&ipcdev->lock);
  133. reinit_completion(&ipcdev->cmd_complete);
  134. type = (cmd & IPC_PUNIT_CMD_TYPE_MASK) >> IPC_TYPE_OFFSET;
  135. val = cmd & ~IPC_PUNIT_CMD_TYPE_MASK;
  136. val |= CMD_RUN | para2 << CMD_PARA2_SHIFT | para1 << CMD_PARA1_SHIFT;
  137. ipc_write_cmd(ipcdev, type, val);
  138. ret = intel_punit_ipc_check_status(ipcdev, type);
  139. mutex_unlock(&ipcdev->lock);
  140. return ret;
  141. }
  142. EXPORT_SYMBOL(intel_punit_ipc_simple_command);
  143. /**
  144. * intel_punit_ipc_command() - IPC command with data and pointers
  145. * @cmd: IPC command code.
  146. * @para1: First 8bit parameter, set 0 if not used.
  147. * @para2: Second 8bit parameter, set 0 if not used.
  148. * @in: Input data, 32bit for BIOS cmd, two 32bit for GTD and ISPD.
  149. * @out: Output data.
  150. *
  151. * Send a IPC command to P-Unit with data transaction
  152. *
  153. * Return: IPC error code or 0 on success.
  154. */
  155. int intel_punit_ipc_command(u32 cmd, u32 para1, u32 para2, u32 *in, u32 *out)
  156. {
  157. IPC_DEV *ipcdev = punit_ipcdev;
  158. IPC_TYPE type;
  159. u32 val;
  160. int ret;
  161. mutex_lock(&ipcdev->lock);
  162. reinit_completion(&ipcdev->cmd_complete);
  163. type = (cmd & IPC_PUNIT_CMD_TYPE_MASK) >> IPC_TYPE_OFFSET;
  164. ipc_write_data_low(ipcdev, type, *in);
  165. if (type == GTDRIVER_IPC || type == ISPDRIVER_IPC)
  166. ipc_write_data_high(ipcdev, type, *++in);
  167. val = cmd & ~IPC_PUNIT_CMD_TYPE_MASK;
  168. val |= CMD_RUN | para2 << CMD_PARA2_SHIFT | para1 << CMD_PARA1_SHIFT;
  169. ipc_write_cmd(ipcdev, type, val);
  170. ret = intel_punit_ipc_check_status(ipcdev, type);
  171. if (ret)
  172. goto out;
  173. *out = ipc_read_data_low(ipcdev, type);
  174. if (type == GTDRIVER_IPC || type == ISPDRIVER_IPC)
  175. *++out = ipc_read_data_high(ipcdev, type);
  176. out:
  177. mutex_unlock(&ipcdev->lock);
  178. return ret;
  179. }
  180. EXPORT_SYMBOL_GPL(intel_punit_ipc_command);
  181. static irqreturn_t intel_punit_ioc(int irq, void *dev_id)
  182. {
  183. IPC_DEV *ipcdev = dev_id;
  184. complete(&ipcdev->cmd_complete);
  185. return IRQ_HANDLED;
  186. }
  187. static int intel_punit_get_bars(struct platform_device *pdev)
  188. {
  189. struct resource *res;
  190. void __iomem *addr;
  191. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  192. addr = devm_ioremap_resource(&pdev->dev, res);
  193. if (IS_ERR(addr))
  194. return PTR_ERR(addr);
  195. punit_ipcdev->base[BIOS_IPC][BASE_DATA] = addr;
  196. res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  197. addr = devm_ioremap_resource(&pdev->dev, res);
  198. if (IS_ERR(addr))
  199. return PTR_ERR(addr);
  200. punit_ipcdev->base[BIOS_IPC][BASE_IFACE] = addr;
  201. res = platform_get_resource(pdev, IORESOURCE_MEM, 2);
  202. addr = devm_ioremap_resource(&pdev->dev, res);
  203. if (IS_ERR(addr))
  204. return PTR_ERR(addr);
  205. punit_ipcdev->base[ISPDRIVER_IPC][BASE_DATA] = addr;
  206. res = platform_get_resource(pdev, IORESOURCE_MEM, 3);
  207. addr = devm_ioremap_resource(&pdev->dev, res);
  208. if (IS_ERR(addr))
  209. return PTR_ERR(addr);
  210. punit_ipcdev->base[ISPDRIVER_IPC][BASE_IFACE] = addr;
  211. res = platform_get_resource(pdev, IORESOURCE_MEM, 4);
  212. addr = devm_ioremap_resource(&pdev->dev, res);
  213. if (IS_ERR(addr))
  214. return PTR_ERR(addr);
  215. punit_ipcdev->base[GTDRIVER_IPC][BASE_DATA] = addr;
  216. res = platform_get_resource(pdev, IORESOURCE_MEM, 5);
  217. addr = devm_ioremap_resource(&pdev->dev, res);
  218. if (IS_ERR(addr))
  219. return PTR_ERR(addr);
  220. punit_ipcdev->base[GTDRIVER_IPC][BASE_IFACE] = addr;
  221. return 0;
  222. }
  223. static int intel_punit_ipc_probe(struct platform_device *pdev)
  224. {
  225. int irq, ret;
  226. punit_ipcdev = devm_kzalloc(&pdev->dev,
  227. sizeof(*punit_ipcdev), GFP_KERNEL);
  228. if (!punit_ipcdev)
  229. return -ENOMEM;
  230. platform_set_drvdata(pdev, punit_ipcdev);
  231. irq = platform_get_irq(pdev, 0);
  232. if (irq < 0) {
  233. punit_ipcdev->irq = 0;
  234. dev_warn(&pdev->dev, "Invalid IRQ, using polling mode\n");
  235. } else {
  236. ret = devm_request_irq(&pdev->dev, irq, intel_punit_ioc,
  237. IRQF_NO_SUSPEND, "intel_punit_ipc",
  238. &punit_ipcdev);
  239. if (ret) {
  240. dev_err(&pdev->dev, "Failed to request irq: %d\n", irq);
  241. return ret;
  242. }
  243. punit_ipcdev->irq = irq;
  244. }
  245. ret = intel_punit_get_bars(pdev);
  246. if (ret)
  247. goto out;
  248. punit_ipcdev->dev = &pdev->dev;
  249. mutex_init(&punit_ipcdev->lock);
  250. init_completion(&punit_ipcdev->cmd_complete);
  251. out:
  252. return ret;
  253. }
  254. static int intel_punit_ipc_remove(struct platform_device *pdev)
  255. {
  256. return 0;
  257. }
  258. static const struct acpi_device_id punit_ipc_acpi_ids[] = {
  259. { "INT34D4", 0 },
  260. { }
  261. };
  262. static struct platform_driver intel_punit_ipc_driver = {
  263. .probe = intel_punit_ipc_probe,
  264. .remove = intel_punit_ipc_remove,
  265. .driver = {
  266. .name = "intel_punit_ipc",
  267. .acpi_match_table = ACPI_PTR(punit_ipc_acpi_ids),
  268. },
  269. };
  270. static int __init intel_punit_ipc_init(void)
  271. {
  272. return platform_driver_register(&intel_punit_ipc_driver);
  273. }
  274. static void __exit intel_punit_ipc_exit(void)
  275. {
  276. platform_driver_unregister(&intel_punit_ipc_driver);
  277. }
  278. MODULE_AUTHOR("Zha Qipeng <qipeng.zha@intel.com>");
  279. MODULE_DESCRIPTION("Intel P-Unit IPC driver");
  280. MODULE_LICENSE("GPL v2");
  281. /* Some modules are dependent on this, so init earlier */
  282. fs_initcall(intel_punit_ipc_init);
  283. module_exit(intel_punit_ipc_exit);