module: Convert symbol namespace to string literal
[linux.git] / drivers / mcb / mcb-parse.c
blob02a680c73979b933e6321823cd6666d7f0aff7d0
1 // SPDX-License-Identifier: GPL-2.0-only
2 #include <linux/types.h>
3 #include <linux/ioport.h>
4 #include <linux/slab.h>
5 #include <linux/export.h>
6 #include <linux/io.h>
7 #include <linux/mcb.h>
9 #include "mcb-internal.h"
11 #define for_each_chameleon_cell(dtype, p) \
12 for ((dtype) = get_next_dtype((p)); \
13 (dtype) != CHAMELEON_DTYPE_END; \
14 (dtype) = get_next_dtype((p)))
16 static inline uint32_t get_next_dtype(void __iomem *p)
18 uint32_t dtype;
20 dtype = readl(p);
21 return dtype >> 28;
24 static int chameleon_parse_bdd(struct mcb_bus *bus,
25 struct chameleon_bar *cb,
26 void __iomem *base)
28 return 0;
31 static int chameleon_parse_gdd(struct mcb_bus *bus,
32 struct chameleon_bar *cb,
33 void __iomem *base, int bar_count)
35 struct chameleon_gdd __iomem *gdd =
36 (struct chameleon_gdd __iomem *) base;
37 struct mcb_device *mdev;
38 u32 dev_mapbase;
39 u32 offset;
40 u32 size;
41 int ret;
42 __le32 reg1;
43 __le32 reg2;
45 mdev = mcb_alloc_dev(bus);
46 if (!mdev)
47 return -ENOMEM;
49 reg1 = readl(&gdd->reg1);
50 reg2 = readl(&gdd->reg2);
51 offset = readl(&gdd->offset);
52 size = readl(&gdd->size);
54 mdev->id = GDD_DEV(reg1);
55 mdev->rev = GDD_REV(reg1);
56 mdev->var = GDD_VAR(reg1);
57 mdev->bar = GDD_BAR(reg2);
58 mdev->group = GDD_GRP(reg2);
59 mdev->inst = GDD_INS(reg2);
62 * If the BAR is missing, dev_mapbase is zero, or if the
63 * device is IO mapped we just print a warning and go on with the
64 * next device, instead of completely stop the gdd parser
66 if (mdev->bar > bar_count - 1) {
67 pr_info("No BAR for 16z%03d\n", mdev->id);
68 ret = 0;
69 goto err;
72 dev_mapbase = cb[mdev->bar].addr;
73 if (!dev_mapbase) {
74 pr_info("BAR not assigned for 16z%03d\n", mdev->id);
75 ret = 0;
76 goto err;
79 if (dev_mapbase & 0x01) {
80 pr_info("IO mapped Device (16z%03d) not yet supported\n",
81 mdev->id);
82 ret = 0;
83 goto err;
86 pr_debug("Found a 16z%03d\n", mdev->id);
88 mdev->irq.start = GDD_IRQ(reg1);
89 mdev->irq.end = GDD_IRQ(reg1);
90 mdev->irq.flags = IORESOURCE_IRQ;
92 mdev->mem.start = dev_mapbase + offset;
94 mdev->mem.end = mdev->mem.start + size - 1;
95 mdev->mem.flags = IORESOURCE_MEM;
97 ret = mcb_device_register(bus, mdev);
98 if (ret < 0)
99 goto err;
101 return 0;
103 err:
104 mcb_free_dev(mdev);
106 return ret;
109 static void chameleon_parse_bar(void __iomem *base,
110 struct chameleon_bar *cb, int bar_count)
112 char __iomem *p = base;
113 int i;
115 /* skip reg1 */
116 p += sizeof(__le32);
118 for (i = 0; i < bar_count; i++) {
119 cb[i].addr = readl(p);
120 cb[i].size = readl(p + 4);
122 p += sizeof(struct chameleon_bar);
126 static int chameleon_get_bar(void __iomem **base, phys_addr_t mapbase,
127 struct chameleon_bar **cb)
129 struct chameleon_bar *c;
130 int bar_count;
131 __le32 reg;
132 u32 dtype;
135 * For those devices which are not connected
136 * to the PCI Bus (e.g. LPC) there is a bar
137 * descriptor located directly after the
138 * chameleon header. This header is comparable
139 * to a PCI header.
141 dtype = get_next_dtype(*base);
142 if (dtype == CHAMELEON_DTYPE_BAR) {
143 reg = readl(*base);
145 bar_count = BAR_CNT(reg);
146 if (bar_count <= 0 || bar_count > CHAMELEON_BAR_MAX)
147 return -ENODEV;
149 c = kcalloc(bar_count, sizeof(struct chameleon_bar),
150 GFP_KERNEL);
151 if (!c)
152 return -ENOMEM;
154 chameleon_parse_bar(*base, c, bar_count);
155 *base += BAR_DESC_SIZE(bar_count);
156 } else {
157 c = kzalloc(sizeof(struct chameleon_bar), GFP_KERNEL);
158 if (!c)
159 return -ENOMEM;
161 bar_count = 1;
162 c->addr = mapbase;
165 *cb = c;
167 return bar_count;
170 int chameleon_parse_cells(struct mcb_bus *bus, phys_addr_t mapbase,
171 void __iomem *base)
173 struct chameleon_fpga_header *header;
174 struct chameleon_bar *cb;
175 void __iomem *p = base;
176 int num_cells = 0;
177 uint32_t dtype;
178 int bar_count;
179 int ret;
180 u32 hsize;
181 u32 table_size;
183 hsize = sizeof(struct chameleon_fpga_header);
185 header = kzalloc(hsize, GFP_KERNEL);
186 if (!header)
187 return -ENOMEM;
189 /* Extract header information */
190 memcpy_fromio(header, p, hsize);
191 /* We only support chameleon v2 at the moment */
192 header->magic = le16_to_cpu(header->magic);
193 if (header->magic != CHAMELEONV2_MAGIC) {
194 pr_err("Unsupported chameleon version 0x%x\n",
195 header->magic);
196 ret = -ENODEV;
197 goto free_header;
199 p += hsize;
201 bus->revision = header->revision;
202 bus->model = header->model;
203 bus->minor = header->minor;
204 snprintf(bus->name, CHAMELEON_FILENAME_LEN + 1, "%s",
205 header->filename);
207 bar_count = chameleon_get_bar(&p, mapbase, &cb);
208 if (bar_count < 0) {
209 ret = bar_count;
210 goto free_header;
213 for_each_chameleon_cell(dtype, p) {
214 switch (dtype) {
215 case CHAMELEON_DTYPE_GENERAL:
216 ret = chameleon_parse_gdd(bus, cb, p, bar_count);
217 if (ret < 0)
218 goto free_bar;
219 p += sizeof(struct chameleon_gdd);
220 break;
221 case CHAMELEON_DTYPE_BRIDGE:
222 chameleon_parse_bdd(bus, cb, p);
223 p += sizeof(struct chameleon_bdd);
224 break;
225 case CHAMELEON_DTYPE_END:
226 break;
227 default:
228 pr_err("Invalid chameleon descriptor type 0x%x\n",
229 dtype);
230 ret = -EINVAL;
231 goto free_bar;
233 num_cells++;
236 if (num_cells == 0) {
237 ret = -EINVAL;
238 goto free_bar;
241 table_size = p - base;
242 pr_debug("%d cell(s) found. Chameleon table size: 0x%04x bytes\n", num_cells, table_size);
243 kfree(cb);
244 kfree(header);
245 return table_size;
247 free_bar:
248 kfree(cb);
249 free_header:
250 kfree(header);
252 return ret;
254 EXPORT_SYMBOL_NS_GPL(chameleon_parse_cells, "MCB");