iosf_mbi.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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 <asm/iosf_mbi.h>
  25. static DEFINE_SPINLOCK(iosf_mbi_lock);
  26. static inline u32 iosf_mbi_form_mcr(u8 op, u8 port, u8 offset)
  27. {
  28. return (op << 24) | (port << 16) | (offset << 8) | MBI_ENABLE;
  29. }
  30. static struct pci_dev *mbi_pdev; /* one mbi device */
  31. static int iosf_mbi_pci_read_mdr(u32 mcrx, u32 mcr, u32 *mdr)
  32. {
  33. int result;
  34. if (!mbi_pdev)
  35. return -ENODEV;
  36. if (mcrx) {
  37. result = pci_write_config_dword(mbi_pdev, MBI_MCRX_OFFSET,
  38. mcrx);
  39. if (result < 0)
  40. goto fail_read;
  41. }
  42. result = pci_write_config_dword(mbi_pdev, MBI_MCR_OFFSET, mcr);
  43. if (result < 0)
  44. goto fail_read;
  45. result = pci_read_config_dword(mbi_pdev, MBI_MDR_OFFSET, mdr);
  46. if (result < 0)
  47. goto fail_read;
  48. return 0;
  49. fail_read:
  50. dev_err(&mbi_pdev->dev, "PCI config access failed with %d\n", result);
  51. return result;
  52. }
  53. static int iosf_mbi_pci_write_mdr(u32 mcrx, u32 mcr, u32 mdr)
  54. {
  55. int result;
  56. if (!mbi_pdev)
  57. return -ENODEV;
  58. result = pci_write_config_dword(mbi_pdev, MBI_MDR_OFFSET, mdr);
  59. if (result < 0)
  60. goto fail_write;
  61. if (mcrx) {
  62. result = pci_write_config_dword(mbi_pdev, MBI_MCRX_OFFSET,
  63. mcrx);
  64. if (result < 0)
  65. goto fail_write;
  66. }
  67. result = pci_write_config_dword(mbi_pdev, MBI_MCR_OFFSET, mcr);
  68. if (result < 0)
  69. goto fail_write;
  70. return 0;
  71. fail_write:
  72. dev_err(&mbi_pdev->dev, "PCI config access failed with %d\n", result);
  73. return result;
  74. }
  75. int iosf_mbi_read(u8 port, u8 opcode, u32 offset, u32 *mdr)
  76. {
  77. u32 mcr, mcrx;
  78. unsigned long flags;
  79. int ret;
  80. /*Access to the GFX unit is handled by GPU code */
  81. if (port == BT_MBI_UNIT_GFX) {
  82. WARN_ON(1);
  83. return -EPERM;
  84. }
  85. mcr = iosf_mbi_form_mcr(opcode, port, offset & MBI_MASK_LO);
  86. mcrx = offset & MBI_MASK_HI;
  87. spin_lock_irqsave(&iosf_mbi_lock, flags);
  88. ret = iosf_mbi_pci_read_mdr(mcrx, mcr, mdr);
  89. spin_unlock_irqrestore(&iosf_mbi_lock, flags);
  90. return ret;
  91. }
  92. EXPORT_SYMBOL(iosf_mbi_read);
  93. int iosf_mbi_write(u8 port, u8 opcode, u32 offset, u32 mdr)
  94. {
  95. u32 mcr, mcrx;
  96. unsigned long flags;
  97. int ret;
  98. /*Access to the GFX unit is handled by GPU code */
  99. if (port == BT_MBI_UNIT_GFX) {
  100. WARN_ON(1);
  101. return -EPERM;
  102. }
  103. mcr = iosf_mbi_form_mcr(opcode, port, offset & MBI_MASK_LO);
  104. mcrx = offset & MBI_MASK_HI;
  105. spin_lock_irqsave(&iosf_mbi_lock, flags);
  106. ret = iosf_mbi_pci_write_mdr(mcrx, mcr, mdr);
  107. spin_unlock_irqrestore(&iosf_mbi_lock, flags);
  108. return ret;
  109. }
  110. EXPORT_SYMBOL(iosf_mbi_write);
  111. int iosf_mbi_modify(u8 port, u8 opcode, u32 offset, u32 mdr, u32 mask)
  112. {
  113. u32 mcr, mcrx;
  114. u32 value;
  115. unsigned long flags;
  116. int ret;
  117. /*Access to the GFX unit is handled by GPU code */
  118. if (port == BT_MBI_UNIT_GFX) {
  119. WARN_ON(1);
  120. return -EPERM;
  121. }
  122. mcr = iosf_mbi_form_mcr(opcode, port, offset & MBI_MASK_LO);
  123. mcrx = offset & MBI_MASK_HI;
  124. spin_lock_irqsave(&iosf_mbi_lock, flags);
  125. /* Read current mdr value */
  126. ret = iosf_mbi_pci_read_mdr(mcrx, mcr & MBI_RD_MASK, &value);
  127. if (ret < 0) {
  128. spin_unlock_irqrestore(&iosf_mbi_lock, flags);
  129. return ret;
  130. }
  131. /* Apply mask */
  132. value &= ~mask;
  133. mdr &= mask;
  134. value |= mdr;
  135. /* Write back */
  136. ret = iosf_mbi_pci_write_mdr(mcrx, mcr | MBI_WR_MASK, value);
  137. spin_unlock_irqrestore(&iosf_mbi_lock, flags);
  138. return ret;
  139. }
  140. EXPORT_SYMBOL(iosf_mbi_modify);
  141. static int iosf_mbi_probe(struct pci_dev *pdev,
  142. const struct pci_device_id *unused)
  143. {
  144. int ret;
  145. ret = pci_enable_device(pdev);
  146. if (ret < 0) {
  147. dev_err(&pdev->dev, "error: could not enable device\n");
  148. return ret;
  149. }
  150. mbi_pdev = pci_dev_get(pdev);
  151. return 0;
  152. }
  153. static DEFINE_PCI_DEVICE_TABLE(iosf_mbi_pci_ids) = {
  154. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x0F00) },
  155. { 0, },
  156. };
  157. MODULE_DEVICE_TABLE(pci, iosf_mbi_pci_ids);
  158. static struct pci_driver iosf_mbi_pci_driver = {
  159. .name = "iosf_mbi_pci",
  160. .probe = iosf_mbi_probe,
  161. .id_table = iosf_mbi_pci_ids,
  162. };
  163. static int __init iosf_mbi_init(void)
  164. {
  165. return pci_register_driver(&iosf_mbi_pci_driver);
  166. }
  167. static void __exit iosf_mbi_exit(void)
  168. {
  169. pci_unregister_driver(&iosf_mbi_pci_driver);
  170. if (mbi_pdev) {
  171. pci_dev_put(mbi_pdev);
  172. mbi_pdev = NULL;
  173. }
  174. }
  175. module_init(iosf_mbi_init);
  176. module_exit(iosf_mbi_exit);
  177. MODULE_AUTHOR("David E. Box <david.e.box@linux.intel.com>");
  178. MODULE_DESCRIPTION("IOSF Mailbox Interface accessor");
  179. MODULE_LICENSE("GPL v2");