iosf_mbi.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. /*
  2. * IOSF-SB MailBox Interface Driver
  3. * Copyright (c) 2013, Intel Corporation.
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms and conditions of the GNU General Public License,
  7. * version 2, as published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  12. * more details.
  13. *
  14. *
  15. * The IOSF-SB is a fabric bus available on Atom based SOC's that uses a
  16. * mailbox interface (MBI) to communicate with mutiple devices. This
  17. * driver implements access to this interface for those platforms that can
  18. * enumerate the device using PCI.
  19. */
  20. #include <linux/module.h>
  21. #include <linux/init.h>
  22. #include <linux/spinlock.h>
  23. #include <linux/pci.h>
  24. #include <linux/debugfs.h>
  25. #include <linux/capability.h>
  26. #include <asm/iosf_mbi.h>
  27. #define PCI_DEVICE_ID_BAYTRAIL 0x0F00
  28. #define PCI_DEVICE_ID_QUARK_X1000 0x0958
  29. static DEFINE_SPINLOCK(iosf_mbi_lock);
  30. static inline u32 iosf_mbi_form_mcr(u8 op, u8 port, u8 offset)
  31. {
  32. return (op << 24) | (port << 16) | (offset << 8) | MBI_ENABLE;
  33. }
  34. static struct pci_dev *mbi_pdev; /* one mbi device */
  35. static int iosf_mbi_pci_read_mdr(u32 mcrx, u32 mcr, u32 *mdr)
  36. {
  37. int result;
  38. if (!mbi_pdev)
  39. return -ENODEV;
  40. if (mcrx) {
  41. result = pci_write_config_dword(mbi_pdev, MBI_MCRX_OFFSET,
  42. mcrx);
  43. if (result < 0)
  44. goto fail_read;
  45. }
  46. result = pci_write_config_dword(mbi_pdev, MBI_MCR_OFFSET, mcr);
  47. if (result < 0)
  48. goto fail_read;
  49. result = pci_read_config_dword(mbi_pdev, MBI_MDR_OFFSET, mdr);
  50. if (result < 0)
  51. goto fail_read;
  52. return 0;
  53. fail_read:
  54. dev_err(&mbi_pdev->dev, "PCI config access failed with %d\n", result);
  55. return result;
  56. }
  57. static int iosf_mbi_pci_write_mdr(u32 mcrx, u32 mcr, u32 mdr)
  58. {
  59. int result;
  60. if (!mbi_pdev)
  61. return -ENODEV;
  62. result = pci_write_config_dword(mbi_pdev, MBI_MDR_OFFSET, mdr);
  63. if (result < 0)
  64. goto fail_write;
  65. if (mcrx) {
  66. result = pci_write_config_dword(mbi_pdev, MBI_MCRX_OFFSET,
  67. mcrx);
  68. if (result < 0)
  69. goto fail_write;
  70. }
  71. result = pci_write_config_dword(mbi_pdev, MBI_MCR_OFFSET, mcr);
  72. if (result < 0)
  73. goto fail_write;
  74. return 0;
  75. fail_write:
  76. dev_err(&mbi_pdev->dev, "PCI config access failed with %d\n", result);
  77. return result;
  78. }
  79. int iosf_mbi_read(u8 port, u8 opcode, u32 offset, u32 *mdr)
  80. {
  81. u32 mcr, mcrx;
  82. unsigned long flags;
  83. int ret;
  84. /*Access to the GFX unit is handled by GPU code */
  85. if (port == BT_MBI_UNIT_GFX) {
  86. WARN_ON(1);
  87. return -EPERM;
  88. }
  89. mcr = iosf_mbi_form_mcr(opcode, port, offset & MBI_MASK_LO);
  90. mcrx = offset & MBI_MASK_HI;
  91. spin_lock_irqsave(&iosf_mbi_lock, flags);
  92. ret = iosf_mbi_pci_read_mdr(mcrx, mcr, mdr);
  93. spin_unlock_irqrestore(&iosf_mbi_lock, flags);
  94. return ret;
  95. }
  96. EXPORT_SYMBOL(iosf_mbi_read);
  97. int iosf_mbi_write(u8 port, u8 opcode, u32 offset, u32 mdr)
  98. {
  99. u32 mcr, mcrx;
  100. unsigned long flags;
  101. int ret;
  102. /*Access to the GFX unit is handled by GPU code */
  103. if (port == BT_MBI_UNIT_GFX) {
  104. WARN_ON(1);
  105. return -EPERM;
  106. }
  107. mcr = iosf_mbi_form_mcr(opcode, port, offset & MBI_MASK_LO);
  108. mcrx = offset & MBI_MASK_HI;
  109. spin_lock_irqsave(&iosf_mbi_lock, flags);
  110. ret = iosf_mbi_pci_write_mdr(mcrx, mcr, mdr);
  111. spin_unlock_irqrestore(&iosf_mbi_lock, flags);
  112. return ret;
  113. }
  114. EXPORT_SYMBOL(iosf_mbi_write);
  115. int iosf_mbi_modify(u8 port, u8 opcode, u32 offset, u32 mdr, u32 mask)
  116. {
  117. u32 mcr, mcrx;
  118. u32 value;
  119. unsigned long flags;
  120. int ret;
  121. /*Access to the GFX unit is handled by GPU code */
  122. if (port == BT_MBI_UNIT_GFX) {
  123. WARN_ON(1);
  124. return -EPERM;
  125. }
  126. mcr = iosf_mbi_form_mcr(opcode, port, offset & MBI_MASK_LO);
  127. mcrx = offset & MBI_MASK_HI;
  128. spin_lock_irqsave(&iosf_mbi_lock, flags);
  129. /* Read current mdr value */
  130. ret = iosf_mbi_pci_read_mdr(mcrx, mcr & MBI_RD_MASK, &value);
  131. if (ret < 0) {
  132. spin_unlock_irqrestore(&iosf_mbi_lock, flags);
  133. return ret;
  134. }
  135. /* Apply mask */
  136. value &= ~mask;
  137. mdr &= mask;
  138. value |= mdr;
  139. /* Write back */
  140. ret = iosf_mbi_pci_write_mdr(mcrx, mcr | MBI_WR_MASK, value);
  141. spin_unlock_irqrestore(&iosf_mbi_lock, flags);
  142. return ret;
  143. }
  144. EXPORT_SYMBOL(iosf_mbi_modify);
  145. bool iosf_mbi_available(void)
  146. {
  147. /* Mbi isn't hot-pluggable. No remove routine is provided */
  148. return mbi_pdev;
  149. }
  150. EXPORT_SYMBOL(iosf_mbi_available);
  151. /********************** debugfs begin ****************************/
  152. static u32 dbg_mdr;
  153. static u32 dbg_mcr;
  154. static u32 dbg_mcrx;
  155. static int mcr_get(void *data, u64 *val)
  156. {
  157. *val = *(u32 *)data;
  158. return 0;
  159. }
  160. static int mcr_set(void *data, u64 val)
  161. {
  162. u8 command = ((u32)val & 0xFF000000) >> 24,
  163. port = ((u32)val & 0x00FF0000) >> 16,
  164. offset = ((u32)val & 0x0000FF00) >> 8;
  165. int err;
  166. *(u32 *)data = val;
  167. if (!capable(CAP_SYS_RAWIO))
  168. return -EACCES;
  169. if (command & 1u)
  170. err = iosf_mbi_write(port,
  171. command,
  172. dbg_mcrx | offset,
  173. dbg_mdr);
  174. else
  175. err = iosf_mbi_read(port,
  176. command,
  177. dbg_mcrx | offset,
  178. &dbg_mdr);
  179. return err;
  180. }
  181. DEFINE_SIMPLE_ATTRIBUTE(iosf_mcr_fops, mcr_get, mcr_set , "%llx\n");
  182. static struct dentry *iosf_dbg;
  183. static void iosf_sideband_debug_init(void)
  184. {
  185. struct dentry *d;
  186. iosf_dbg = debugfs_create_dir("iosf_sb", NULL);
  187. if (IS_ERR_OR_NULL(iosf_dbg))
  188. return;
  189. /* mdr */
  190. d = debugfs_create_x32("mdr", 0660, iosf_dbg, &dbg_mdr);
  191. if (IS_ERR_OR_NULL(d))
  192. goto cleanup;
  193. /* mcrx */
  194. debugfs_create_x32("mcrx", 0660, iosf_dbg, &dbg_mcrx);
  195. if (IS_ERR_OR_NULL(d))
  196. goto cleanup;
  197. /* mcr - initiates mailbox tranaction */
  198. debugfs_create_file("mcr", 0660, iosf_dbg, &dbg_mcr, &iosf_mcr_fops);
  199. if (IS_ERR_OR_NULL(d))
  200. goto cleanup;
  201. return;
  202. cleanup:
  203. debugfs_remove_recursive(d);
  204. }
  205. /********************** debugfs end ****************************/
  206. static int iosf_mbi_probe(struct pci_dev *pdev,
  207. const struct pci_device_id *unused)
  208. {
  209. int ret;
  210. ret = pci_enable_device(pdev);
  211. if (ret < 0) {
  212. dev_err(&pdev->dev, "error: could not enable device\n");
  213. return ret;
  214. }
  215. mbi_pdev = pci_dev_get(pdev);
  216. return 0;
  217. }
  218. static const struct pci_device_id iosf_mbi_pci_ids[] = {
  219. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_BAYTRAIL) },
  220. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_QUARK_X1000) },
  221. { 0, },
  222. };
  223. MODULE_DEVICE_TABLE(pci, iosf_mbi_pci_ids);
  224. static struct pci_driver iosf_mbi_pci_driver = {
  225. .name = "iosf_mbi_pci",
  226. .probe = iosf_mbi_probe,
  227. .id_table = iosf_mbi_pci_ids,
  228. };
  229. static int __init iosf_mbi_init(void)
  230. {
  231. iosf_sideband_debug_init();
  232. return pci_register_driver(&iosf_mbi_pci_driver);
  233. }
  234. static void __exit iosf_mbi_exit(void)
  235. {
  236. debugfs_remove_recursive(iosf_dbg);
  237. pci_unregister_driver(&iosf_mbi_pci_driver);
  238. if (mbi_pdev) {
  239. pci_dev_put(mbi_pdev);
  240. mbi_pdev = NULL;
  241. }
  242. }
  243. module_init(iosf_mbi_init);
  244. module_exit(iosf_mbi_exit);
  245. MODULE_AUTHOR("David E. Box <david.e.box@linux.intel.com>");
  246. MODULE_DESCRIPTION("IOSF Mailbox Interface accessor");
  247. MODULE_LICENSE("GPL v2");