Merge branch 'master' of git://git.kernel.org/pub/scm/linux/kernel/git/linville/wirel...
[linux-drm-fsl-dcu.git] / arch / powerpc / platforms / powernv / opal-lpc.c
1 /*
2  * PowerNV LPC bus handling.
3  *
4  * Copyright 2013 IBM Corp.
5  *
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.
10  */
11
12 #include <linux/kernel.h>
13 #include <linux/of.h>
14 #include <linux/bug.h>
15
16 #include <asm/machdep.h>
17 #include <asm/firmware.h>
18 #include <asm/xics.h>
19 #include <asm/opal.h>
20 #include <asm/prom.h>
21
22 static int opal_lpc_chip_id = -1;
23
24 static u8 opal_lpc_inb(unsigned long port)
25 {
26         int64_t rc;
27         __be32 data;
28
29         if (opal_lpc_chip_id < 0 || port > 0xffff)
30                 return 0xff;
31         rc = opal_lpc_read(opal_lpc_chip_id, OPAL_LPC_IO, port, &data, 1);
32         return rc ? 0xff : be32_to_cpu(data);
33 }
34
35 static __le16 __opal_lpc_inw(unsigned long port)
36 {
37         int64_t rc;
38         __be32 data;
39
40         if (opal_lpc_chip_id < 0 || port > 0xfffe)
41                 return 0xffff;
42         if (port & 1)
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);
46 }
47 static u16 opal_lpc_inw(unsigned long port)
48 {
49         return le16_to_cpu(__opal_lpc_inw(port));
50 }
51
52 static __le32 __opal_lpc_inl(unsigned long port)
53 {
54         int64_t rc;
55         __be32 data;
56
57         if (opal_lpc_chip_id < 0 || port > 0xfffc)
58                 return 0xffffffff;
59         if (port & 3)
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);
66 }
67
68 static u32 opal_lpc_inl(unsigned long port)
69 {
70         return le32_to_cpu(__opal_lpc_inl(port));
71 }
72
73 static void opal_lpc_outb(u8 val, unsigned long port)
74 {
75         if (opal_lpc_chip_id < 0 || port > 0xffff)
76                 return;
77         opal_lpc_write(opal_lpc_chip_id, OPAL_LPC_IO, port, val, 1);
78 }
79
80 static void __opal_lpc_outw(__le16 val, unsigned long port)
81 {
82         if (opal_lpc_chip_id < 0 || port > 0xfffe)
83                 return;
84         if (port & 1) {
85                 opal_lpc_outb(val >> 8, port);
86                 opal_lpc_outb(val     , port + 1);
87                 return;
88         }
89         opal_lpc_write(opal_lpc_chip_id, OPAL_LPC_IO, port, val, 2);
90 }
91
92 static void opal_lpc_outw(u16 val, unsigned long port)
93 {
94         __opal_lpc_outw(cpu_to_le16(val), port);
95 }
96
97 static void __opal_lpc_outl(__le32 val, unsigned long port)
98 {
99         if (opal_lpc_chip_id < 0 || port > 0xfffc)
100                 return;
101         if (port & 3) {
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);
106                 return;
107         }
108         opal_lpc_write(opal_lpc_chip_id, OPAL_LPC_IO, port, val, 4);
109 }
110
111 static void opal_lpc_outl(u32 val, unsigned long port)
112 {
113         __opal_lpc_outl(cpu_to_le32(val), port);
114 }
115
116 static void opal_lpc_insb(unsigned long p, void *b, unsigned long c)
117 {
118         u8 *ptr = b;
119
120         while(c--)
121                 *(ptr++) = opal_lpc_inb(p);
122 }
123
124 static void opal_lpc_insw(unsigned long p, void *b, unsigned long c)
125 {
126         __le16 *ptr = b;
127
128         while(c--)
129                 *(ptr++) = __opal_lpc_inw(p);
130 }
131
132 static void opal_lpc_insl(unsigned long p, void *b, unsigned long c)
133 {
134         __le32 *ptr = b;
135
136         while(c--)
137                 *(ptr++) = __opal_lpc_inl(p);
138 }
139
140 static void opal_lpc_outsb(unsigned long p, const void *b, unsigned long c)
141 {
142         const u8 *ptr = b;
143
144         while(c--)
145                 opal_lpc_outb(*(ptr++), p);
146 }
147
148 static void opal_lpc_outsw(unsigned long p, const void *b, unsigned long c)
149 {
150         const __le16 *ptr = b;
151
152         while(c--)
153                 __opal_lpc_outw(*(ptr++), p);
154 }
155
156 static void opal_lpc_outsl(unsigned long p, const void *b, unsigned long c)
157 {
158         const __le32 *ptr = b;
159
160         while(c--)
161                 __opal_lpc_outl(*(ptr++), p);
162 }
163
164 static const struct ppc_pci_io opal_lpc_io = {
165         .inb    = opal_lpc_inb,
166         .inw    = opal_lpc_inw,
167         .inl    = opal_lpc_inl,
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,
177 };
178
179 void opal_lpc_init(void)
180 {
181         struct device_node *np;
182
183         /*
184          * Look for a Power8 LPC bus tagged as "primary",
185          * we currently support only one though the OPAL APIs
186          * support any number.
187          */
188         for_each_compatible_node(np, NULL, "ibm,power8-lpc") {
189                 if (!of_device_is_available(np))
190                         continue;
191                 if (!of_get_property(np, "primary", NULL))
192                         continue;
193                 opal_lpc_chip_id = of_get_ibm_chip_id(np);
194                 break;
195         }
196         if (opal_lpc_chip_id < 0)
197                 return;
198
199         /* Setup special IO ops */
200         ppc_pci_io = opal_lpc_io;
201         isa_io_special = true;
202
203         pr_info("OPAL: Power8 LPC bus found, chip ID %d\n", opal_lpc_chip_id);
204 }