blob: 28fb290ca3250f1b6dfda9d9c484137511759d57 [file] [log] [blame]
David E. Box461844152014-01-08 13:27:51 -08001/*
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
21#include <linux/module.h>
22#include <linux/init.h>
23#include <linux/spinlock.h>
24#include <linux/pci.h>
David E. Box8dc12f92014-08-27 14:40:40 -070025#include <linux/debugfs.h>
26#include <linux/capability.h>
David E. Box461844152014-01-08 13:27:51 -080027
28#include <asm/iosf_mbi.h>
29
Ong Boon Leong04725ad2014-05-09 13:44:08 -070030#define PCI_DEVICE_ID_BAYTRAIL 0x0F00
David E. Box849f5d82014-09-17 22:13:49 -070031#define PCI_DEVICE_ID_BRASWELL 0x2280
Ong Boon Leong04725ad2014-05-09 13:44:08 -070032#define PCI_DEVICE_ID_QUARK_X1000 0x0958
33
Andy Shevchenko7e1ff152015-07-08 17:45:08 +030034static struct pci_dev *mbi_pdev;
David E. Box461844152014-01-08 13:27:51 -080035static DEFINE_SPINLOCK(iosf_mbi_lock);
36
37static inline u32 iosf_mbi_form_mcr(u8 op, u8 port, u8 offset)
38{
39 return (op << 24) | (port << 16) | (offset << 8) | MBI_ENABLE;
40}
41
David E. Box461844152014-01-08 13:27:51 -080042static int iosf_mbi_pci_read_mdr(u32 mcrx, u32 mcr, u32 *mdr)
43{
44 int result;
45
46 if (!mbi_pdev)
47 return -ENODEV;
48
49 if (mcrx) {
50 result = pci_write_config_dword(mbi_pdev, MBI_MCRX_OFFSET,
51 mcrx);
52 if (result < 0)
53 goto fail_read;
54 }
55
56 result = pci_write_config_dword(mbi_pdev, MBI_MCR_OFFSET, mcr);
57 if (result < 0)
58 goto fail_read;
59
60 result = pci_read_config_dword(mbi_pdev, MBI_MDR_OFFSET, mdr);
61 if (result < 0)
62 goto fail_read;
63
64 return 0;
65
66fail_read:
67 dev_err(&mbi_pdev->dev, "PCI config access failed with %d\n", result);
68 return result;
69}
70
71static int iosf_mbi_pci_write_mdr(u32 mcrx, u32 mcr, u32 mdr)
72{
73 int result;
74
75 if (!mbi_pdev)
76 return -ENODEV;
77
78 result = pci_write_config_dword(mbi_pdev, MBI_MDR_OFFSET, mdr);
79 if (result < 0)
80 goto fail_write;
81
82 if (mcrx) {
83 result = pci_write_config_dword(mbi_pdev, MBI_MCRX_OFFSET,
84 mcrx);
85 if (result < 0)
86 goto fail_write;
87 }
88
89 result = pci_write_config_dword(mbi_pdev, MBI_MCR_OFFSET, mcr);
90 if (result < 0)
91 goto fail_write;
92
93 return 0;
94
95fail_write:
96 dev_err(&mbi_pdev->dev, "PCI config access failed with %d\n", result);
97 return result;
98}
99
100int iosf_mbi_read(u8 port, u8 opcode, u32 offset, u32 *mdr)
101{
102 u32 mcr, mcrx;
103 unsigned long flags;
104 int ret;
105
Andy Shevchenko7e1ff152015-07-08 17:45:08 +0300106 /* Access to the GFX unit is handled by GPU code */
David E. Box461844152014-01-08 13:27:51 -0800107 if (port == BT_MBI_UNIT_GFX) {
108 WARN_ON(1);
109 return -EPERM;
110 }
111
112 mcr = iosf_mbi_form_mcr(opcode, port, offset & MBI_MASK_LO);
113 mcrx = offset & MBI_MASK_HI;
114
115 spin_lock_irqsave(&iosf_mbi_lock, flags);
116 ret = iosf_mbi_pci_read_mdr(mcrx, mcr, mdr);
117 spin_unlock_irqrestore(&iosf_mbi_lock, flags);
118
119 return ret;
120}
121EXPORT_SYMBOL(iosf_mbi_read);
122
123int iosf_mbi_write(u8 port, u8 opcode, u32 offset, u32 mdr)
124{
125 u32 mcr, mcrx;
126 unsigned long flags;
127 int ret;
128
Andy Shevchenko7e1ff152015-07-08 17:45:08 +0300129 /* Access to the GFX unit is handled by GPU code */
David E. Box461844152014-01-08 13:27:51 -0800130 if (port == BT_MBI_UNIT_GFX) {
131 WARN_ON(1);
132 return -EPERM;
133 }
134
135 mcr = iosf_mbi_form_mcr(opcode, port, offset & MBI_MASK_LO);
136 mcrx = offset & MBI_MASK_HI;
137
138 spin_lock_irqsave(&iosf_mbi_lock, flags);
139 ret = iosf_mbi_pci_write_mdr(mcrx, mcr, mdr);
140 spin_unlock_irqrestore(&iosf_mbi_lock, flags);
141
142 return ret;
143}
144EXPORT_SYMBOL(iosf_mbi_write);
145
146int iosf_mbi_modify(u8 port, u8 opcode, u32 offset, u32 mdr, u32 mask)
147{
148 u32 mcr, mcrx;
149 u32 value;
150 unsigned long flags;
151 int ret;
152
Andy Shevchenko7e1ff152015-07-08 17:45:08 +0300153 /* Access to the GFX unit is handled by GPU code */
David E. Box461844152014-01-08 13:27:51 -0800154 if (port == BT_MBI_UNIT_GFX) {
155 WARN_ON(1);
156 return -EPERM;
157 }
158
159 mcr = iosf_mbi_form_mcr(opcode, port, offset & MBI_MASK_LO);
160 mcrx = offset & MBI_MASK_HI;
161
162 spin_lock_irqsave(&iosf_mbi_lock, flags);
163
164 /* Read current mdr value */
165 ret = iosf_mbi_pci_read_mdr(mcrx, mcr & MBI_RD_MASK, &value);
166 if (ret < 0) {
167 spin_unlock_irqrestore(&iosf_mbi_lock, flags);
168 return ret;
169 }
170
171 /* Apply mask */
172 value &= ~mask;
173 mdr &= mask;
174 value |= mdr;
175
176 /* Write back */
177 ret = iosf_mbi_pci_write_mdr(mcrx, mcr | MBI_WR_MASK, value);
178
179 spin_unlock_irqrestore(&iosf_mbi_lock, flags);
180
181 return ret;
182}
183EXPORT_SYMBOL(iosf_mbi_modify);
184
David E. Box6b8f0c82014-05-09 13:44:05 -0700185bool iosf_mbi_available(void)
186{
187 /* Mbi isn't hot-pluggable. No remove routine is provided */
188 return mbi_pdev;
189}
190EXPORT_SYMBOL(iosf_mbi_available);
191
David E. Boxed2226b2014-09-17 22:13:51 -0700192#ifdef CONFIG_IOSF_MBI_DEBUG
David E. Box8dc12f92014-08-27 14:40:40 -0700193static u32 dbg_mdr;
194static u32 dbg_mcr;
195static u32 dbg_mcrx;
196
197static int mcr_get(void *data, u64 *val)
198{
199 *val = *(u32 *)data;
200 return 0;
201}
202
203static int mcr_set(void *data, u64 val)
204{
205 u8 command = ((u32)val & 0xFF000000) >> 24,
206 port = ((u32)val & 0x00FF0000) >> 16,
207 offset = ((u32)val & 0x0000FF00) >> 8;
208 int err;
209
210 *(u32 *)data = val;
211
212 if (!capable(CAP_SYS_RAWIO))
213 return -EACCES;
214
215 if (command & 1u)
216 err = iosf_mbi_write(port,
217 command,
218 dbg_mcrx | offset,
219 dbg_mdr);
220 else
221 err = iosf_mbi_read(port,
222 command,
223 dbg_mcrx | offset,
224 &dbg_mdr);
225
226 return err;
227}
228DEFINE_SIMPLE_ATTRIBUTE(iosf_mcr_fops, mcr_get, mcr_set , "%llx\n");
229
230static struct dentry *iosf_dbg;
David E. Boxed2226b2014-09-17 22:13:51 -0700231
David E. Box8dc12f92014-08-27 14:40:40 -0700232static void iosf_sideband_debug_init(void)
233{
234 struct dentry *d;
235
236 iosf_dbg = debugfs_create_dir("iosf_sb", NULL);
237 if (IS_ERR_OR_NULL(iosf_dbg))
238 return;
239
240 /* mdr */
241 d = debugfs_create_x32("mdr", 0660, iosf_dbg, &dbg_mdr);
Andy Shevchenko64279c72015-07-08 17:45:06 +0300242 if (!d)
David E. Box8dc12f92014-08-27 14:40:40 -0700243 goto cleanup;
244
245 /* mcrx */
Andy Shevchenko64279c72015-07-08 17:45:06 +0300246 d = debugfs_create_x32("mcrx", 0660, iosf_dbg, &dbg_mcrx);
247 if (!d)
David E. Box8dc12f92014-08-27 14:40:40 -0700248 goto cleanup;
249
250 /* mcr - initiates mailbox tranaction */
Andy Shevchenko64279c72015-07-08 17:45:06 +0300251 d = debugfs_create_file("mcr", 0660, iosf_dbg, &dbg_mcr, &iosf_mcr_fops);
252 if (!d)
David E. Box8dc12f92014-08-27 14:40:40 -0700253 goto cleanup;
254
255 return;
256
257cleanup:
258 debugfs_remove_recursive(d);
259}
David E. Boxed2226b2014-09-17 22:13:51 -0700260
261static void iosf_debugfs_init(void)
262{
263 iosf_sideband_debug_init();
264}
265
266static void iosf_debugfs_remove(void)
267{
268 debugfs_remove_recursive(iosf_dbg);
269}
270#else
271static inline void iosf_debugfs_init(void) { }
272static inline void iosf_debugfs_remove(void) { }
273#endif /* CONFIG_IOSF_MBI_DEBUG */
David E. Box8dc12f92014-08-27 14:40:40 -0700274
David E. Box461844152014-01-08 13:27:51 -0800275static int iosf_mbi_probe(struct pci_dev *pdev,
276 const struct pci_device_id *unused)
277{
278 int ret;
279
280 ret = pci_enable_device(pdev);
281 if (ret < 0) {
282 dev_err(&pdev->dev, "error: could not enable device\n");
283 return ret;
284 }
285
286 mbi_pdev = pci_dev_get(pdev);
287 return 0;
288}
289
Benoit Taine9baa3c32014-08-08 15:56:03 +0200290static const struct pci_device_id iosf_mbi_pci_ids[] = {
Ong Boon Leong04725ad2014-05-09 13:44:08 -0700291 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_BAYTRAIL) },
David E. Box849f5d82014-09-17 22:13:49 -0700292 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_BRASWELL) },
Ong Boon Leong04725ad2014-05-09 13:44:08 -0700293 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_QUARK_X1000) },
David E. Box461844152014-01-08 13:27:51 -0800294 { 0, },
295};
296MODULE_DEVICE_TABLE(pci, iosf_mbi_pci_ids);
297
298static struct pci_driver iosf_mbi_pci_driver = {
299 .name = "iosf_mbi_pci",
300 .probe = iosf_mbi_probe,
301 .id_table = iosf_mbi_pci_ids,
302};
303
304static int __init iosf_mbi_init(void)
305{
David E. Boxed2226b2014-09-17 22:13:51 -0700306 iosf_debugfs_init();
307
David E. Box461844152014-01-08 13:27:51 -0800308 return pci_register_driver(&iosf_mbi_pci_driver);
309}
310
311static void __exit iosf_mbi_exit(void)
312{
David E. Boxed2226b2014-09-17 22:13:51 -0700313 iosf_debugfs_remove();
David E. Box8dc12f92014-08-27 14:40:40 -0700314
David E. Box461844152014-01-08 13:27:51 -0800315 pci_unregister_driver(&iosf_mbi_pci_driver);
Andy Shevchenkob93fb9f2015-07-08 17:45:07 +0300316 pci_dev_put(mbi_pdev);
317 mbi_pdev = NULL;
David E. Box461844152014-01-08 13:27:51 -0800318}
319
320module_init(iosf_mbi_init);
321module_exit(iosf_mbi_exit);
322
323MODULE_AUTHOR("David E. Box <david.e.box@linux.intel.com>");
324MODULE_DESCRIPTION("IOSF Mailbox Interface accessor");
325MODULE_LICENSE("GPL v2");