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>
21 static int opal_lpc_chip_id
= -1;
23 static u8
opal_lpc_inb(unsigned long port
)
28 if (opal_lpc_chip_id
< 0 || port
> 0xffff)
30 rc
= opal_lpc_read(opal_lpc_chip_id
, OPAL_LPC_IO
, port
, &data
, 1);
31 return rc
? 0xff : data
;
34 static __le16
__opal_lpc_inw(unsigned long port
)
39 if (opal_lpc_chip_id
< 0 || port
> 0xfffe)
42 return (__le16
)opal_lpc_inb(port
) << 8 | opal_lpc_inb(port
+ 1);
43 rc
= opal_lpc_read(opal_lpc_chip_id
, OPAL_LPC_IO
, port
, &data
, 2);
44 return rc
? 0xffff : data
;
46 static u16
opal_lpc_inw(unsigned long port
)
48 return le16_to_cpu(__opal_lpc_inw(port
));
51 static __le32
__opal_lpc_inl(unsigned long port
)
56 if (opal_lpc_chip_id
< 0 || port
> 0xfffc)
59 return (__le32
)opal_lpc_inb(port
) << 24 |
60 (__le32
)opal_lpc_inb(port
+ 1) << 16 |
61 (__le32
)opal_lpc_inb(port
+ 2) << 8 |
62 opal_lpc_inb(port
+ 3);
63 rc
= opal_lpc_read(opal_lpc_chip_id
, OPAL_LPC_IO
, port
, &data
, 4);
64 return rc
? 0xffffffff : data
;
67 static u32
opal_lpc_inl(unsigned long port
)
69 return le32_to_cpu(__opal_lpc_inl(port
));
72 static void opal_lpc_outb(u8 val
, unsigned long port
)
74 if (opal_lpc_chip_id
< 0 || port
> 0xffff)
76 opal_lpc_write(opal_lpc_chip_id
, OPAL_LPC_IO
, port
, val
, 1);
79 static void __opal_lpc_outw(__le16 val
, unsigned long port
)
81 if (opal_lpc_chip_id
< 0 || port
> 0xfffe)
84 opal_lpc_outb(val
>> 8, port
);
85 opal_lpc_outb(val
, port
+ 1);
88 opal_lpc_write(opal_lpc_chip_id
, OPAL_LPC_IO
, port
, val
, 2);
91 static void opal_lpc_outw(u16 val
, unsigned long port
)
93 __opal_lpc_outw(cpu_to_le16(val
), port
);
96 static void __opal_lpc_outl(__le32 val
, unsigned long port
)
98 if (opal_lpc_chip_id
< 0 || port
> 0xfffc)
101 opal_lpc_outb(val
>> 24, port
);
102 opal_lpc_outb(val
>> 16, port
+ 1);
103 opal_lpc_outb(val
>> 8, port
+ 2);
104 opal_lpc_outb(val
, port
+ 3);
107 opal_lpc_write(opal_lpc_chip_id
, OPAL_LPC_IO
, port
, val
, 4);
110 static void opal_lpc_outl(u32 val
, unsigned long port
)
112 __opal_lpc_outl(cpu_to_le32(val
), port
);
115 static void opal_lpc_insb(unsigned long p
, void *b
, unsigned long c
)
120 *(ptr
++) = opal_lpc_inb(p
);
123 static void opal_lpc_insw(unsigned long p
, void *b
, unsigned long c
)
128 *(ptr
++) = __opal_lpc_inw(p
);
131 static void opal_lpc_insl(unsigned long p
, void *b
, unsigned long c
)
136 *(ptr
++) = __opal_lpc_inl(p
);
139 static void opal_lpc_outsb(unsigned long p
, const void *b
, unsigned long c
)
144 opal_lpc_outb(*(ptr
++), p
);
147 static void opal_lpc_outsw(unsigned long p
, const void *b
, unsigned long c
)
149 const __le16
*ptr
= b
;
152 __opal_lpc_outw(*(ptr
++), p
);
155 static void opal_lpc_outsl(unsigned long p
, const void *b
, unsigned long c
)
157 const __le32
*ptr
= b
;
160 __opal_lpc_outl(*(ptr
++), p
);
163 static const struct ppc_pci_io opal_lpc_io
= {
167 .outb
= opal_lpc_outb
,
168 .outw
= opal_lpc_outw
,
169 .outl
= opal_lpc_outl
,
170 .insb
= opal_lpc_insb
,
171 .insw
= opal_lpc_insw
,
172 .insl
= opal_lpc_insl
,
173 .outsb
= opal_lpc_outsb
,
174 .outsw
= opal_lpc_outsw
,
175 .outsl
= opal_lpc_outsl
,
178 void opal_lpc_init(void)
180 struct device_node
*np
;
183 * Look for a Power8 LPC bus tagged as "primary",
184 * we currently support only one though the OPAL APIs
185 * support any number.
187 for_each_compatible_node(np
, NULL
, "ibm,power8-lpc") {
188 if (!of_device_is_available(np
))
190 if (!of_get_property(np
, "primary", NULL
))
192 opal_lpc_chip_id
= of_get_ibm_chip_id(np
);
195 if (opal_lpc_chip_id
< 0)
198 /* Setup special IO ops */
199 ppc_pci_io
= opal_lpc_io
;
200 isa_io_special
= true;
202 pr_info("OPAL: Power8 LPC bus found, chip ID %d\n", opal_lpc_chip_id
);