iosf_mbi.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  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_BRASWELL 0x2280
  29. #define PCI_DEVICE_ID_QUARK_X1000 0x0958
  30. static DEFINE_SPINLOCK(iosf_mbi_lock);
  31. static inline u32 iosf_mbi_form_mcr(u8 op, u8 port, u8 offset)
  32. {
  33. return (op << 24) | (port << 16) | (offset << 8) | MBI_ENABLE;
  34. }
  35. static struct pci_dev *mbi_pdev; /* one mbi device */
  36. static int iosf_mbi_pci_read_mdr(u32 mcrx, u32 mcr, u32 *mdr)
  37. {
  38. int result;
  39. if (!mbi_pdev)
  40. return -ENODEV;
  41. if (mcrx) {
  42. result = pci_write_config_dword(mbi_pdev, MBI_MCRX_OFFSET,
  43. mcrx);
  44. if (result < 0)
  45. goto fail_read;
  46. }
  47. result = pci_write_config_dword(mbi_pdev, MBI_MCR_OFFSET, mcr);
  48. if (result < 0)
  49. goto fail_read;
  50. result = pci_read_config_dword(mbi_pdev, MBI_MDR_OFFSET, mdr);
  51. if (result < 0)
  52. goto fail_read;
  53. return 0;
  54. fail_read:
  55. dev_err(&mbi_pdev->dev, "PCI config access failed with %d\n", result);
  56. return result;
  57. }
  58. static int iosf_mbi_pci_write_mdr(u32 mcrx, u32 mcr, u32 mdr)
  59. {
  60. int result;
  61. if (!mbi_pdev)
  62. return -ENODEV;
  63. result = pci_write_config_dword(mbi_pdev, MBI_MDR_OFFSET, mdr);
  64. if (result < 0)
  65. goto fail_write;
  66. if (mcrx) {
  67. result = pci_write_config_dword(mbi_pdev, MBI_MCRX_OFFSET,
  68. mcrx);
  69. if (result < 0)
  70. goto fail_write;
  71. }
  72. result = pci_write_config_dword(mbi_pdev, MBI_MCR_OFFSET, mcr);
  73. if (result < 0)
  74. goto fail_write;
  75. return 0;
  76. fail_write:
  77. dev_err(&mbi_pdev->dev, "PCI config access failed with %d\n", result);
  78. return result;
  79. }
  80. int iosf_mbi_read(u8 port, u8 opcode, u32 offset, u32 *mdr)
  81. {
  82. u32 mcr, mcrx;
  83. unsigned long flags;
  84. int ret;
  85. /*Access to the GFX unit is handled by GPU code */
  86. if (port == BT_MBI_UNIT_GFX) {
  87. WARN_ON(1);
  88. return -EPERM;
  89. }
  90. mcr = iosf_mbi_form_mcr(opcode, port, offset & MBI_MASK_LO);
  91. mcrx = offset & MBI_MASK_HI;
  92. spin_lock_irqsave(&iosf_mbi_lock, flags);
  93. ret = iosf_mbi_pci_read_mdr(mcrx, mcr, mdr);
  94. spin_unlock_irqrestore(&iosf_mbi_lock, flags);
  95. return ret;
  96. }
  97. EXPORT_SYMBOL(iosf_mbi_read);
  98. int iosf_mbi_write(u8 port, u8 opcode, u32 offset, u32 mdr)
  99. {
  100. u32 mcr, mcrx;
  101. unsigned long flags;
  102. int ret;
  103. /*Access to the GFX unit is handled by GPU code */
  104. if (port == BT_MBI_UNIT_GFX) {
  105. WARN_ON(1);
  106. return -EPERM;
  107. }
  108. mcr = iosf_mbi_form_mcr(opcode, port, offset & MBI_MASK_LO);
  109. mcrx = offset & MBI_MASK_HI;
  110. spin_lock_irqsave(&iosf_mbi_lock, flags);
  111. ret = iosf_mbi_pci_write_mdr(mcrx, mcr, mdr);
  112. spin_unlock_irqrestore(&iosf_mbi_lock, flags);
  113. return ret;
  114. }
  115. EXPORT_SYMBOL(iosf_mbi_write);
  116. int iosf_mbi_modify(u8 port, u8 opcode, u32 offset, u32 mdr, u32 mask)
  117. {
  118. u32 mcr, mcrx;
  119. u32 value;
  120. unsigned long flags;
  121. int ret;
  122. /*Access to the GFX unit is handled by GPU code */
  123. if (port == BT_MBI_UNIT_GFX) {
  124. WARN_ON(1);
  125. return -EPERM;
  126. }
  127. mcr = iosf_mbi_form_mcr(opcode, port, offset & MBI_MASK_LO);
  128. mcrx = offset & MBI_MASK_HI;
  129. spin_lock_irqsave(&iosf_mbi_lock, flags);
  130. /* Read current mdr value */
  131. ret = iosf_mbi_pci_read_mdr(mcrx, mcr & MBI_RD_MASK, &value);
  132. if (ret < 0) {
  133. spin_unlock_irqrestore(&iosf_mbi_lock, flags);
  134. return ret;
  135. }
  136. /* Apply mask */
  137. value &= ~mask;
  138. mdr &= mask;
  139. value |= mdr;
  140. /* Write back */
  141. ret = iosf_mbi_pci_write_mdr(mcrx, mcr | MBI_WR_MASK, value);
  142. spin_unlock_irqrestore(&iosf_mbi_lock, flags);
  143. return ret;
  144. }
  145. EXPORT_SYMBOL(iosf_mbi_modify);
  146. bool iosf_mbi_available(void)
  147. {
  148. /* Mbi isn't hot-pluggable. No remove routine is provided */
  149. return mbi_pdev;
  150. }
  151. EXPORT_SYMBOL(iosf_mbi_available);
  152. #ifdef CONFIG_IOSF_MBI_DEBUG
  153. static u32 dbg_mdr;
  154. static u32 dbg_mcr;
  155. static u32 dbg_mcrx;
  156. static int mcr_get(void *data, u64 *val)
  157. {
  158. *val = *(u32 *)data;
  159. return 0;
  160. }
  161. static int mcr_set(void *data, u64 val)
  162. {
  163. u8 command = ((u32)val & 0xFF000000) >> 24,
  164. port = ((u32)val & 0x00FF0000) >> 16,
  165. offset = ((u32)val & 0x0000FF00) >> 8;
  166. int err;
  167. *(u32 *)data = val;
  168. if (!capable(CAP_SYS_RAWIO))
  169. return -EACCES;
  170. if (command & 1u)
  171. err = iosf_mbi_write(port,
  172. command,
  173. dbg_mcrx | offset,
  174. dbg_mdr);
  175. else
  176. err = iosf_mbi_read(port,
  177. command,
  178. dbg_mcrx | offset,
  179. &dbg_mdr);
  180. return err;
  181. }
  182. DEFINE_SIMPLE_ATTRIBUTE(iosf_mcr_fops, mcr_get, mcr_set , "%llx\n");
  183. static struct dentry *iosf_dbg;
  184. static void iosf_sideband_debug_init(void)
  185. {
  186. struct dentry *d;
  187. iosf_dbg = debugfs_create_dir("iosf_sb", NULL);
  188. if (IS_ERR_OR_NULL(iosf_dbg))
  189. return;
  190. /* mdr */
  191. d = debugfs_create_x32("mdr", 0660, iosf_dbg, &dbg_mdr);
  192. if (!d)
  193. goto cleanup;
  194. /* mcrx */
  195. d = debugfs_create_x32("mcrx", 0660, iosf_dbg, &dbg_mcrx);
  196. if (!d)
  197. goto cleanup;
  198. /* mcr - initiates mailbox tranaction */
  199. d = debugfs_create_file("mcr", 0660, iosf_dbg, &dbg_mcr, &iosf_mcr_fops);
  200. if (!d)
  201. goto cleanup;
  202. return;
  203. cleanup:
  204. debugfs_remove_recursive(d);
  205. }
  206. static void iosf_debugfs_init(void)
  207. {
  208. iosf_sideband_debug_init();
  209. }
  210. static void iosf_debugfs_remove(void)
  211. {
  212. debugfs_remove_recursive(iosf_dbg);
  213. }
  214. #else
  215. static inline void iosf_debugfs_init(void) { }
  216. static inline void iosf_debugfs_remove(void) { }
  217. #endif /* CONFIG_IOSF_MBI_DEBUG */
  218. static int iosf_mbi_probe(struct pci_dev *pdev,
  219. const struct pci_device_id *unused)
  220. {
  221. int ret;
  222. ret = pci_enable_device(pdev);
  223. if (ret < 0) {
  224. dev_err(&pdev->dev, "error: could not enable device\n");
  225. return ret;
  226. }
  227. mbi_pdev = pci_dev_get(pdev);
  228. return 0;
  229. }
  230. static const struct pci_device_id iosf_mbi_pci_ids[] = {
  231. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_BAYTRAIL) },
  232. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_BRASWELL) },
  233. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_QUARK_X1000) },
  234. { 0, },
  235. };
  236. MODULE_DEVICE_TABLE(pci, iosf_mbi_pci_ids);
  237. static struct pci_driver iosf_mbi_pci_driver = {
  238. .name = "iosf_mbi_pci",
  239. .probe = iosf_mbi_probe,
  240. .id_table = iosf_mbi_pci_ids,
  241. };
  242. static int __init iosf_mbi_init(void)
  243. {
  244. iosf_debugfs_init();
  245. return pci_register_driver(&iosf_mbi_pci_driver);
  246. }
  247. static void __exit iosf_mbi_exit(void)
  248. {
  249. iosf_debugfs_remove();
  250. pci_unregister_driver(&iosf_mbi_pci_driver);
  251. pci_dev_put(mbi_pdev);
  252. mbi_pdev = NULL;
  253. }
  254. module_init(iosf_mbi_init);
  255. module_exit(iosf_mbi_exit);
  256. MODULE_AUTHOR("David E. Box <david.e.box@linux.intel.com>");
  257. MODULE_DESCRIPTION("IOSF Mailbox Interface accessor");
  258. MODULE_LICENSE("GPL v2");