iosf_mbi.c 7.1 KB

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