2 * PowerNV LPC bus handling.
4 * Copyright 2013 IBM Corp.
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
12 #include <linux/kernel.h>
14 #include <linux/bug.h>
16 #include <asm/machdep.h>
17 #include <asm/firmware.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 : 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 : 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 : 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 void opal_lpc_init(void)
181 struct device_node *np;
184 * Look for a Power8 LPC bus tagged as "primary",
185 * we currently support only one though the OPAL APIs
186 * support any number.
188 for_each_compatible_node(np, NULL, "ibm,power8-lpc") {
189 if (!of_device_is_available(np))
191 if (!of_get_property(np, "primary", NULL))
193 opal_lpc_chip_id = of_get_ibm_chip_id(np);
196 if (opal_lpc_chip_id < 0)
199 /* Setup special IO ops */
200 ppc_pci_io = opal_lpc_io;
201 isa_io_special = true;
203 pr_info("OPAL: Power8 LPC bus found, chip ID %d\n", opal_lpc_chip_id);