1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * PowerNV LPC bus handling.
5 * Copyright 2013 IBM Corp.
8 #include <linux/kernel.h>
10 #include <linux/bug.h>
12 #include <linux/slab.h>
14 #include <asm/machdep.h>
15 #include <asm/firmware.h>
18 #include <linux/uaccess.h>
19 #include <asm/debugfs.h>
20 #include <asm/isa-bridge.h>
22 static int opal_lpc_chip_id = -1;
24 static u8 opal_lpc_inb(unsigned long port)
29 if (opal_lpc_chip_id < 0 || port > 0xffff)
31 rc = opal_lpc_read(opal_lpc_chip_id, OPAL_LPC_IO, port, &data, 1);
32 return rc ? 0xff : be32_to_cpu(data);
35 static __le16 __opal_lpc_inw(unsigned long port)
40 if (opal_lpc_chip_id < 0 || port > 0xfffe)
43 return (__le16)opal_lpc_inb(port) << 8 | opal_lpc_inb(port + 1);
44 rc = opal_lpc_read(opal_lpc_chip_id, OPAL_LPC_IO, port, &data, 2);
45 return rc ? 0xffff : be32_to_cpu(data);
47 static u16 opal_lpc_inw(unsigned long port)
49 return le16_to_cpu(__opal_lpc_inw(port));
52 static __le32 __opal_lpc_inl(unsigned long port)
57 if (opal_lpc_chip_id < 0 || port > 0xfffc)
60 return (__le32)opal_lpc_inb(port ) << 24 |
61 (__le32)opal_lpc_inb(port + 1) << 16 |
62 (__le32)opal_lpc_inb(port + 2) << 8 |
63 opal_lpc_inb(port + 3);
64 rc = opal_lpc_read(opal_lpc_chip_id, OPAL_LPC_IO, port, &data, 4);
65 return rc ? 0xffffffff : be32_to_cpu(data);
68 static u32 opal_lpc_inl(unsigned long port)
70 return le32_to_cpu(__opal_lpc_inl(port));
73 static void opal_lpc_outb(u8 val, unsigned long port)
75 if (opal_lpc_chip_id < 0 || port > 0xffff)
77 opal_lpc_write(opal_lpc_chip_id, OPAL_LPC_IO, port, val, 1);
80 static void __opal_lpc_outw(__le16 val, unsigned long port)
82 if (opal_lpc_chip_id < 0 || port > 0xfffe)
85 opal_lpc_outb(val >> 8, port);
86 opal_lpc_outb(val , port + 1);
89 opal_lpc_write(opal_lpc_chip_id, OPAL_LPC_IO, port, val, 2);
92 static void opal_lpc_outw(u16 val, unsigned long port)
94 __opal_lpc_outw(cpu_to_le16(val), port);
97 static void __opal_lpc_outl(__le32 val, unsigned long port)
99 if (opal_lpc_chip_id < 0 || port > 0xfffc)
102 opal_lpc_outb(val >> 24, port);
103 opal_lpc_outb(val >> 16, port + 1);
104 opal_lpc_outb(val >> 8, port + 2);
105 opal_lpc_outb(val , port + 3);
108 opal_lpc_write(opal_lpc_chip_id, OPAL_LPC_IO, port, val, 4);
111 static void opal_lpc_outl(u32 val, unsigned long port)
113 __opal_lpc_outl(cpu_to_le32(val), port);
116 static void opal_lpc_insb(unsigned long p, void *b, unsigned long c)
121 *(ptr++) = opal_lpc_inb(p);
124 static void opal_lpc_insw(unsigned long p, void *b, unsigned long c)
129 *(ptr++) = __opal_lpc_inw(p);
132 static void opal_lpc_insl(unsigned long p, void *b, unsigned long c)
137 *(ptr++) = __opal_lpc_inl(p);
140 static void opal_lpc_outsb(unsigned long p, const void *b, unsigned long c)
145 opal_lpc_outb(*(ptr++), p);
148 static void opal_lpc_outsw(unsigned long p, const void *b, unsigned long c)
150 const __le16 *ptr = b;
153 __opal_lpc_outw(*(ptr++), p);
156 static void opal_lpc_outsl(unsigned long p, const void *b, unsigned long c)
158 const __le32 *ptr = b;
161 __opal_lpc_outl(*(ptr++), p);
164 static const struct ppc_pci_io opal_lpc_io = {
168 .outb = opal_lpc_outb,
169 .outw = opal_lpc_outw,
170 .outl = opal_lpc_outl,
171 .insb = opal_lpc_insb,
172 .insw = opal_lpc_insw,
173 .insl = opal_lpc_insl,
174 .outsb = opal_lpc_outsb,
175 .outsw = opal_lpc_outsw,
176 .outsl = opal_lpc_outsl,
179 #ifdef CONFIG_DEBUG_FS
180 struct lpc_debugfs_entry {
181 enum OpalLPCAddressType lpc_type;
184 static ssize_t lpc_debug_read(struct file *filp, char __user *ubuf,
185 size_t count, loff_t *ppos)
187 struct lpc_debugfs_entry *lpc = filp->private_data;
188 u32 data, pos, len, todo;
191 if (!access_ok(ubuf, count))
199 * Select access size based on count and alignment and
200 * access type. IO and MEM only support byte acceses,
204 if (lpc->lpc_type == OPAL_LPC_FW) {
205 if (todo > 3 && (pos & 3) == 0)
207 else if (todo > 1 && (pos & 1) == 0)
210 rc = opal_lpc_read(opal_lpc_chip_id, lpc->lpc_type, pos,
216 * Now there is some trickery with the data returned by OPAL
217 * as it's the desired data right justified in a 32-bit BE
220 * This is a very bad interface and I'm to blame for it :-(
222 * So we can't just apply a 32-bit swap to what comes from OPAL,
223 * because user space expects the *bytes* to be in their proper
224 * respective positions (ie, LPC position).
226 * So what we really want to do here is to shift data right
227 * appropriately on a LE kernel.
229 * IE. If the LPC transaction has bytes B0, B1, B2 and B3 in that
230 * order, we have in memory written to by OPAL at the "data"
233 * Bytes: OPAL "data" LE "data"
234 * 32-bit: B0 B1 B2 B3 B0B1B2B3 B3B2B1B0
235 * 16-bit: B0 B1 0000B0B1 B1B00000
236 * 8-bit: B0 000000B0 B0000000
238 * So a BE kernel will have the leftmost of the above in the MSB
239 * and rightmost in the LSB and can just then "cast" the u32 "data"
240 * down to the appropriate quantity and write it.
242 * However, an LE kernel can't. It doesn't need to swap because a
243 * load from data followed by a store to user are going to preserve
244 * the byte ordering which is the wire byte order which is what the
245 * user wants, but in order to "crop" to the right size, we need to
250 rc = __put_user((u32)data, (u32 __user *)ubuf);
253 #ifdef __LITTLE_ENDIAN__
256 rc = __put_user((u16)data, (u16 __user *)ubuf);
259 #ifdef __LITTLE_ENDIAN__
262 rc = __put_user((u8)data, (u8 __user *)ubuf);
275 static ssize_t lpc_debug_write(struct file *filp, const char __user *ubuf,
276 size_t count, loff_t *ppos)
278 struct lpc_debugfs_entry *lpc = filp->private_data;
279 u32 data, pos, len, todo;
282 if (!access_ok(ubuf, count))
290 * Select access size based on count and alignment and
291 * access type. IO and MEM only support byte acceses,
295 if (lpc->lpc_type == OPAL_LPC_FW) {
296 if (todo > 3 && (pos & 3) == 0)
298 else if (todo > 1 && (pos & 1) == 0)
303 * Similarly to the read case, we have some trickery here but
304 * it's different to handle. We need to pass the value to OPAL in
305 * a register whose layout depends on the access size. We want
306 * to reproduce the memory layout of the user, however we aren't
307 * doing a load from user and a store to another memory location
308 * which would achieve that. Here we pass the value to OPAL via
309 * a register which is expected to contain the "BE" interpretation
310 * of the byte sequence. IE: for a 32-bit access, byte 0 should be
311 * in the MSB. So here we *do* need to byteswap on LE.
313 * User bytes: LE "data" OPAL "data"
314 * 32-bit: B0 B1 B2 B3 B3B2B1B0 B0B1B2B3
315 * 16-bit: B0 B1 0000B1B0 0000B0B1
316 * 8-bit: B0 000000B0 000000B0
320 rc = __get_user(data, (u32 __user *)ubuf);
321 data = cpu_to_be32(data);
324 rc = __get_user(data, (u16 __user *)ubuf);
325 data = cpu_to_be16(data);
328 rc = __get_user(data, (u8 __user *)ubuf);
334 rc = opal_lpc_write(opal_lpc_chip_id, lpc->lpc_type, pos,
346 static const struct file_operations lpc_fops = {
347 .read = lpc_debug_read,
348 .write = lpc_debug_write,
350 .llseek = default_llseek,
353 static int opal_lpc_debugfs_create_type(struct dentry *folder,
355 enum OpalLPCAddressType type)
357 struct lpc_debugfs_entry *entry;
358 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
361 entry->lpc_type = type;
362 debugfs_create_file(fname, 0600, folder, entry, &lpc_fops);
366 static int opal_lpc_init_debugfs(void)
371 if (opal_lpc_chip_id < 0)
374 root = debugfs_create_dir("lpc", powerpc_debugfs_root);
376 rc |= opal_lpc_debugfs_create_type(root, "io", OPAL_LPC_IO);
377 rc |= opal_lpc_debugfs_create_type(root, "mem", OPAL_LPC_MEM);
378 rc |= opal_lpc_debugfs_create_type(root, "fw", OPAL_LPC_FW);
381 machine_device_initcall(powernv, opal_lpc_init_debugfs);
382 #endif /* CONFIG_DEBUG_FS */
384 void __init opal_lpc_init(void)
386 struct device_node *np;
389 * Look for a Power8 LPC bus tagged as "primary",
390 * we currently support only one though the OPAL APIs
391 * support any number.
393 for_each_compatible_node(np, NULL, "ibm,power8-lpc") {
394 if (!of_device_is_available(np))
396 if (!of_get_property(np, "primary", NULL))
398 opal_lpc_chip_id = of_get_ibm_chip_id(np);
401 if (opal_lpc_chip_id < 0)
404 /* Does it support direct mapping ? */
405 if (of_get_property(np, "ranges", NULL)) {
406 pr_info("OPAL: Found memory mapped LPC bus on chip %d\n",
408 isa_bridge_init_non_pci(np);
410 pr_info("OPAL: Found non-mapped LPC bus on chip %d\n",
413 /* Setup special IO ops */
414 ppc_pci_io = opal_lpc_io;
415 isa_io_special = true;