iosf_mbi.c 8.8 KB

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