intel_baytrail.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. /*
  2. * Baytrail 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 BayTrail-specific access to this interface.
  18. */
  19. #include <linux/module.h>
  20. #include <linux/init.h>
  21. #include <linux/spinlock.h>
  22. #include <linux/pci.h>
  23. #include "intel_baytrail.h"
  24. static DEFINE_SPINLOCK(iosf_mbi_lock);
  25. static inline u32 iosf_mbi_form_mcr(u8 op, u8 port, u8 offset)
  26. {
  27. return (op << 24) | (port << 16) | (offset << 8) | BT_MBI_ENABLE;
  28. }
  29. static struct pci_dev *mbi_pdev; /* one mbi device */
  30. /* Hold lock before calling */
  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,
  38. BT_MBI_MCRX_OFFSET, mcrx);
  39. if (result < 0)
  40. goto iosf_mbi_read_err;
  41. }
  42. result = pci_write_config_dword(mbi_pdev,
  43. BT_MBI_MCR_OFFSET, mcr);
  44. if (result < 0)
  45. goto iosf_mbi_read_err;
  46. result = pci_read_config_dword(mbi_pdev,
  47. BT_MBI_MDR_OFFSET, mdr);
  48. if (result < 0)
  49. goto iosf_mbi_read_err;
  50. return 0;
  51. iosf_mbi_read_err:
  52. dev_err(&mbi_pdev->dev, "error: PCI config operation returned %d\n",
  53. result);
  54. return result;
  55. }
  56. /* Hold lock before calling */
  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,
  63. BT_MBI_MDR_OFFSET, mdr);
  64. if (result < 0)
  65. goto iosf_mbi_write_err;
  66. if (mcrx) {
  67. result = pci_write_config_dword(mbi_pdev,
  68. BT_MBI_MCRX_OFFSET, mcrx);
  69. if (result < 0)
  70. goto iosf_mbi_write_err;
  71. }
  72. result = pci_write_config_dword(mbi_pdev,
  73. BT_MBI_MCR_OFFSET, mcr);
  74. if (result < 0)
  75. goto iosf_mbi_write_err;
  76. return 0;
  77. iosf_mbi_write_err:
  78. dev_err(&mbi_pdev->dev, "error: PCI config operation returned %d\n",
  79. result);
  80. return result;
  81. }
  82. int bt_mbi_read(u8 port, u8 opcode, u32 offset, u32 *mdr)
  83. {
  84. u32 mcr, mcrx;
  85. unsigned long flags;
  86. int ret;
  87. /*Access to the GFX unit is handled by GPU code */
  88. BUG_ON(port == BT_MBI_UNIT_GFX);
  89. mcr = iosf_mbi_form_mcr(opcode, port, offset & BT_MBI_MASK_LO);
  90. mcrx = offset & BT_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(bt_mbi_read);
  97. int bt_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. BUG_ON(port == BT_MBI_UNIT_GFX);
  104. mcr = iosf_mbi_form_mcr(opcode, port, offset & BT_MBI_MASK_LO);
  105. mcrx = offset & BT_MBI_MASK_HI;
  106. spin_lock_irqsave(&iosf_mbi_lock, flags);
  107. ret = iosf_mbi_pci_write_mdr(mcrx, mcr, mdr);
  108. spin_unlock_irqrestore(&iosf_mbi_lock, flags);
  109. return ret;
  110. }
  111. EXPORT_SYMBOL(bt_mbi_write);
  112. int bt_mbi_modify(u8 port, u8 opcode, u32 offset, u32 mdr, u32 mask)
  113. {
  114. u32 mcr, mcrx;
  115. u32 value;
  116. unsigned long flags;
  117. int ret;
  118. /*Access to the GFX unit is handled by GPU code */
  119. BUG_ON(port == BT_MBI_UNIT_GFX);
  120. mcr = iosf_mbi_form_mcr(opcode, port, offset & BT_MBI_MASK_LO);
  121. mcrx = offset & BT_MBI_MASK_HI;
  122. spin_lock_irqsave(&iosf_mbi_lock, flags);
  123. /* Read current mdr value */
  124. ret = iosf_mbi_pci_read_mdr(mcrx, mcr & BT_MBI_RD_MASK, &value);
  125. if (ret < 0) {
  126. spin_unlock_irqrestore(&iosf_mbi_lock, flags);
  127. return ret;
  128. }
  129. /* Apply mask */
  130. value &= ~mask;
  131. mdr &= mask;
  132. value |= mdr;
  133. /* Write back */
  134. ret = iosf_mbi_pci_write_mdr(mcrx, mcr | BT_MBI_WR_MASK, value);
  135. spin_unlock_irqrestore(&iosf_mbi_lock, flags);
  136. return ret;
  137. }
  138. EXPORT_SYMBOL(bt_mbi_modify);
  139. static int iosf_mbi_probe(struct pci_dev *pdev,
  140. const struct pci_device_id *unused)
  141. {
  142. int ret;
  143. ret = pci_enable_device(pdev);
  144. if (ret < 0) {
  145. dev_err(&pdev->dev, "error: could not enable device\n");
  146. return ret;
  147. }
  148. mbi_pdev = pci_dev_get(pdev);
  149. return 0;
  150. }
  151. static DEFINE_PCI_DEVICE_TABLE(iosf_mbi_pci_ids) = {
  152. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x0F00) },
  153. { 0, },
  154. };
  155. MODULE_DEVICE_TABLE(pci, iosf_mbi_pci_ids);
  156. static struct pci_driver iosf_mbi_pci_driver = {
  157. .name = "iosf_mbi_pci",
  158. .probe = iosf_mbi_probe,
  159. .id_table = iosf_mbi_pci_ids,
  160. };
  161. static int __init bt_mbi_init(void)
  162. {
  163. return pci_register_driver(&iosf_mbi_pci_driver);
  164. }
  165. static void __exit bt_mbi_exit(void)
  166. {
  167. pci_unregister_driver(&iosf_mbi_pci_driver);
  168. if (mbi_pdev) {
  169. pci_dev_put(mbi_pdev);
  170. mbi_pdev = NULL;
  171. }
  172. }
  173. module_init(bt_mbi_init);
  174. module_exit(bt_mbi_exit);
  175. MODULE_AUTHOR("David E. Box <david.e.box@linux.intel.com>");
  176. MODULE_DESCRIPTION("BayTrail Mailbox Interface accessor");
  177. MODULE_LICENSE("GPL v2");