iosf_mbi.c 5.3 KB

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