Linux 4.1.18
[linux/fpc-iii.git] / arch / x86 / kernel / iosf_mbi.c
blob82f8d02f0df215f658b947b28b027e01a6e46b91
1 /*
2 * IOSF-SB MailBox Interface Driver
3 * Copyright (c) 2013, Intel Corporation.
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.
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.
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.
21 #include <linux/module.h>
22 #include <linux/init.h>
23 #include <linux/spinlock.h>
24 #include <linux/pci.h>
25 #include <linux/debugfs.h>
26 #include <linux/capability.h>
28 #include <asm/iosf_mbi.h>
30 #define PCI_DEVICE_ID_BAYTRAIL 0x0F00
31 #define PCI_DEVICE_ID_BRASWELL 0x2280
32 #define PCI_DEVICE_ID_QUARK_X1000 0x0958
34 static DEFINE_SPINLOCK(iosf_mbi_lock);
36 static inline u32 iosf_mbi_form_mcr(u8 op, u8 port, u8 offset)
38 return (op << 24) | (port << 16) | (offset << 8) | MBI_ENABLE;
41 static struct pci_dev *mbi_pdev; /* one mbi device */
43 static int iosf_mbi_pci_read_mdr(u32 mcrx, u32 mcr, u32 *mdr)
45 int result;
47 if (!mbi_pdev)
48 return -ENODEV;
50 if (mcrx) {
51 result = pci_write_config_dword(mbi_pdev, MBI_MCRX_OFFSET,
52 mcrx);
53 if (result < 0)
54 goto fail_read;
57 result = pci_write_config_dword(mbi_pdev, MBI_MCR_OFFSET, mcr);
58 if (result < 0)
59 goto fail_read;
61 result = pci_read_config_dword(mbi_pdev, MBI_MDR_OFFSET, mdr);
62 if (result < 0)
63 goto fail_read;
65 return 0;
67 fail_read:
68 dev_err(&mbi_pdev->dev, "PCI config access failed with %d\n", result);
69 return result;
72 static int iosf_mbi_pci_write_mdr(u32 mcrx, u32 mcr, u32 mdr)
74 int result;
76 if (!mbi_pdev)
77 return -ENODEV;
79 result = pci_write_config_dword(mbi_pdev, MBI_MDR_OFFSET, mdr);
80 if (result < 0)
81 goto fail_write;
83 if (mcrx) {
84 result = pci_write_config_dword(mbi_pdev, MBI_MCRX_OFFSET,
85 mcrx);
86 if (result < 0)
87 goto fail_write;
90 result = pci_write_config_dword(mbi_pdev, MBI_MCR_OFFSET, mcr);
91 if (result < 0)
92 goto fail_write;
94 return 0;
96 fail_write:
97 dev_err(&mbi_pdev->dev, "PCI config access failed with %d\n", result);
98 return result;
101 int iosf_mbi_read(u8 port, u8 opcode, u32 offset, u32 *mdr)
103 u32 mcr, mcrx;
104 unsigned long flags;
105 int ret;
107 /*Access to the GFX unit is handled by GPU code */
108 if (port == BT_MBI_UNIT_GFX) {
109 WARN_ON(1);
110 return -EPERM;
113 mcr = iosf_mbi_form_mcr(opcode, port, offset & MBI_MASK_LO);
114 mcrx = offset & MBI_MASK_HI;
116 spin_lock_irqsave(&iosf_mbi_lock, flags);
117 ret = iosf_mbi_pci_read_mdr(mcrx, mcr, mdr);
118 spin_unlock_irqrestore(&iosf_mbi_lock, flags);
120 return ret;
122 EXPORT_SYMBOL(iosf_mbi_read);
124 int iosf_mbi_write(u8 port, u8 opcode, u32 offset, u32 mdr)
126 u32 mcr, mcrx;
127 unsigned long flags;
128 int ret;
130 /*Access to the GFX unit is handled by GPU code */
131 if (port == BT_MBI_UNIT_GFX) {
132 WARN_ON(1);
133 return -EPERM;
136 mcr = iosf_mbi_form_mcr(opcode, port, offset & MBI_MASK_LO);
137 mcrx = offset & MBI_MASK_HI;
139 spin_lock_irqsave(&iosf_mbi_lock, flags);
140 ret = iosf_mbi_pci_write_mdr(mcrx, mcr, mdr);
141 spin_unlock_irqrestore(&iosf_mbi_lock, flags);
143 return ret;
145 EXPORT_SYMBOL(iosf_mbi_write);
147 int iosf_mbi_modify(u8 port, u8 opcode, u32 offset, u32 mdr, u32 mask)
149 u32 mcr, mcrx;
150 u32 value;
151 unsigned long flags;
152 int ret;
154 /*Access to the GFX unit is handled by GPU code */
155 if (port == BT_MBI_UNIT_GFX) {
156 WARN_ON(1);
157 return -EPERM;
160 mcr = iosf_mbi_form_mcr(opcode, port, offset & MBI_MASK_LO);
161 mcrx = offset & MBI_MASK_HI;
163 spin_lock_irqsave(&iosf_mbi_lock, flags);
165 /* Read current mdr value */
166 ret = iosf_mbi_pci_read_mdr(mcrx, mcr & MBI_RD_MASK, &value);
167 if (ret < 0) {
168 spin_unlock_irqrestore(&iosf_mbi_lock, flags);
169 return ret;
172 /* Apply mask */
173 value &= ~mask;
174 mdr &= mask;
175 value |= mdr;
177 /* Write back */
178 ret = iosf_mbi_pci_write_mdr(mcrx, mcr | MBI_WR_MASK, value);
180 spin_unlock_irqrestore(&iosf_mbi_lock, flags);
182 return ret;
184 EXPORT_SYMBOL(iosf_mbi_modify);
186 bool iosf_mbi_available(void)
188 /* Mbi isn't hot-pluggable. No remove routine is provided */
189 return mbi_pdev;
191 EXPORT_SYMBOL(iosf_mbi_available);
193 #ifdef CONFIG_IOSF_MBI_DEBUG
194 static u32 dbg_mdr;
195 static u32 dbg_mcr;
196 static u32 dbg_mcrx;
198 static int mcr_get(void *data, u64 *val)
200 *val = *(u32 *)data;
201 return 0;
204 static int mcr_set(void *data, u64 val)
206 u8 command = ((u32)val & 0xFF000000) >> 24,
207 port = ((u32)val & 0x00FF0000) >> 16,
208 offset = ((u32)val & 0x0000FF00) >> 8;
209 int err;
211 *(u32 *)data = val;
213 if (!capable(CAP_SYS_RAWIO))
214 return -EACCES;
216 if (command & 1u)
217 err = iosf_mbi_write(port,
218 command,
219 dbg_mcrx | offset,
220 dbg_mdr);
221 else
222 err = iosf_mbi_read(port,
223 command,
224 dbg_mcrx | offset,
225 &dbg_mdr);
227 return err;
229 DEFINE_SIMPLE_ATTRIBUTE(iosf_mcr_fops, mcr_get, mcr_set , "%llx\n");
231 static struct dentry *iosf_dbg;
233 static void iosf_sideband_debug_init(void)
235 struct dentry *d;
237 iosf_dbg = debugfs_create_dir("iosf_sb", NULL);
238 if (IS_ERR_OR_NULL(iosf_dbg))
239 return;
241 /* mdr */
242 d = debugfs_create_x32("mdr", 0660, iosf_dbg, &dbg_mdr);
243 if (IS_ERR_OR_NULL(d))
244 goto cleanup;
246 /* mcrx */
247 debugfs_create_x32("mcrx", 0660, iosf_dbg, &dbg_mcrx);
248 if (IS_ERR_OR_NULL(d))
249 goto cleanup;
251 /* mcr - initiates mailbox tranaction */
252 debugfs_create_file("mcr", 0660, iosf_dbg, &dbg_mcr, &iosf_mcr_fops);
253 if (IS_ERR_OR_NULL(d))
254 goto cleanup;
256 return;
258 cleanup:
259 debugfs_remove_recursive(d);
262 static void iosf_debugfs_init(void)
264 iosf_sideband_debug_init();
267 static void iosf_debugfs_remove(void)
269 debugfs_remove_recursive(iosf_dbg);
271 #else
272 static inline void iosf_debugfs_init(void) { }
273 static inline void iosf_debugfs_remove(void) { }
274 #endif /* CONFIG_IOSF_MBI_DEBUG */
276 static int iosf_mbi_probe(struct pci_dev *pdev,
277 const struct pci_device_id *unused)
279 int ret;
281 ret = pci_enable_device(pdev);
282 if (ret < 0) {
283 dev_err(&pdev->dev, "error: could not enable device\n");
284 return ret;
287 mbi_pdev = pci_dev_get(pdev);
288 return 0;
291 static const struct pci_device_id iosf_mbi_pci_ids[] = {
292 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_BAYTRAIL) },
293 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_BRASWELL) },
294 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_QUARK_X1000) },
295 { 0, },
297 MODULE_DEVICE_TABLE(pci, iosf_mbi_pci_ids);
299 static struct pci_driver iosf_mbi_pci_driver = {
300 .name = "iosf_mbi_pci",
301 .probe = iosf_mbi_probe,
302 .id_table = iosf_mbi_pci_ids,
305 static int __init iosf_mbi_init(void)
307 iosf_debugfs_init();
309 return pci_register_driver(&iosf_mbi_pci_driver);
312 static void __exit iosf_mbi_exit(void)
314 iosf_debugfs_remove();
316 pci_unregister_driver(&iosf_mbi_pci_driver);
317 if (mbi_pdev) {
318 pci_dev_put(mbi_pdev);
319 mbi_pdev = NULL;
323 module_init(iosf_mbi_init);
324 module_exit(iosf_mbi_exit);
326 MODULE_AUTHOR("David E. Box <david.e.box@linux.intel.com>");
327 MODULE_DESCRIPTION("IOSF Mailbox Interface accessor");
328 MODULE_LICENSE("GPL v2");