xref: /linux/drivers/mcb/mcb-parse.c (revision a1ff5a7d78a036d6c2178ee5acd6ba4946243800)
1457c8996SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
23764e82eSJohannes Thumshirn #include <linux/types.h>
33764e82eSJohannes Thumshirn #include <linux/ioport.h>
43764e82eSJohannes Thumshirn #include <linux/slab.h>
53764e82eSJohannes Thumshirn #include <linux/export.h>
63764e82eSJohannes Thumshirn #include <linux/io.h>
73764e82eSJohannes Thumshirn #include <linux/mcb.h>
83764e82eSJohannes Thumshirn 
93764e82eSJohannes Thumshirn #include "mcb-internal.h"
103764e82eSJohannes Thumshirn 
113764e82eSJohannes Thumshirn #define for_each_chameleon_cell(dtype, p)	\
123764e82eSJohannes Thumshirn 	for ((dtype) = get_next_dtype((p));	\
133764e82eSJohannes Thumshirn 	     (dtype) != CHAMELEON_DTYPE_END;	\
143764e82eSJohannes Thumshirn 	     (dtype) = get_next_dtype((p)))
153764e82eSJohannes Thumshirn 
get_next_dtype(void __iomem * p)163764e82eSJohannes Thumshirn static inline uint32_t get_next_dtype(void __iomem *p)
173764e82eSJohannes Thumshirn {
183764e82eSJohannes Thumshirn 	uint32_t dtype;
193764e82eSJohannes Thumshirn 
203764e82eSJohannes Thumshirn 	dtype = readl(p);
213764e82eSJohannes Thumshirn 	return dtype >> 28;
223764e82eSJohannes Thumshirn }
233764e82eSJohannes Thumshirn 
chameleon_parse_bdd(struct mcb_bus * bus,struct chameleon_bar * cb,void __iomem * base)243764e82eSJohannes Thumshirn static int chameleon_parse_bdd(struct mcb_bus *bus,
25ffc7bb38SAndreas Werner 			struct chameleon_bar *cb,
263764e82eSJohannes Thumshirn 			void __iomem *base)
273764e82eSJohannes Thumshirn {
283764e82eSJohannes Thumshirn 	return 0;
293764e82eSJohannes Thumshirn }
303764e82eSJohannes Thumshirn 
chameleon_parse_gdd(struct mcb_bus * bus,struct chameleon_bar * cb,void __iomem * base,int bar_count)313764e82eSJohannes Thumshirn static int chameleon_parse_gdd(struct mcb_bus *bus,
32ffc7bb38SAndreas Werner 			struct chameleon_bar *cb,
33ffc7bb38SAndreas Werner 			void __iomem *base, int bar_count)
343764e82eSJohannes Thumshirn {
353764e82eSJohannes Thumshirn 	struct chameleon_gdd __iomem *gdd =
363764e82eSJohannes Thumshirn 		(struct chameleon_gdd __iomem *) base;
373764e82eSJohannes Thumshirn 	struct mcb_device *mdev;
38ffc7bb38SAndreas Werner 	u32 dev_mapbase;
393764e82eSJohannes Thumshirn 	u32 offset;
403764e82eSJohannes Thumshirn 	u32 size;
413764e82eSJohannes Thumshirn 	int ret;
423764e82eSJohannes Thumshirn 	__le32 reg1;
433764e82eSJohannes Thumshirn 	__le32 reg2;
443764e82eSJohannes Thumshirn 
453764e82eSJohannes Thumshirn 	mdev = mcb_alloc_dev(bus);
463764e82eSJohannes Thumshirn 	if (!mdev)
473764e82eSJohannes Thumshirn 		return -ENOMEM;
483764e82eSJohannes Thumshirn 
493764e82eSJohannes Thumshirn 	reg1 = readl(&gdd->reg1);
503764e82eSJohannes Thumshirn 	reg2 = readl(&gdd->reg2);
513764e82eSJohannes Thumshirn 	offset = readl(&gdd->offset);
523764e82eSJohannes Thumshirn 	size = readl(&gdd->size);
533764e82eSJohannes Thumshirn 
543764e82eSJohannes Thumshirn 	mdev->id = GDD_DEV(reg1);
553764e82eSJohannes Thumshirn 	mdev->rev = GDD_REV(reg1);
563764e82eSJohannes Thumshirn 	mdev->var = GDD_VAR(reg1);
57f75564d3SAndreas Werner 	mdev->bar = GDD_BAR(reg2);
583764e82eSJohannes Thumshirn 	mdev->group = GDD_GRP(reg2);
593764e82eSJohannes Thumshirn 	mdev->inst = GDD_INS(reg2);
603764e82eSJohannes Thumshirn 
61ffc7bb38SAndreas Werner 	/*
62ffc7bb38SAndreas Werner 	 * If the BAR is missing, dev_mapbase is zero, or if the
63ffc7bb38SAndreas Werner 	 * device is IO mapped we just print a warning and go on with the
64ffc7bb38SAndreas Werner 	 * next device, instead of completely stop the gdd parser
65ffc7bb38SAndreas Werner 	 */
66ffc7bb38SAndreas Werner 	if (mdev->bar > bar_count - 1) {
67ffc7bb38SAndreas Werner 		pr_info("No BAR for 16z%03d\n", mdev->id);
68ffc7bb38SAndreas Werner 		ret = 0;
69ffc7bb38SAndreas Werner 		goto err;
70ffc7bb38SAndreas Werner 	}
71ffc7bb38SAndreas Werner 
72ffc7bb38SAndreas Werner 	dev_mapbase = cb[mdev->bar].addr;
73ffc7bb38SAndreas Werner 	if (!dev_mapbase) {
74ffc7bb38SAndreas Werner 		pr_info("BAR not assigned for 16z%03d\n", mdev->id);
75ffc7bb38SAndreas Werner 		ret = 0;
76ffc7bb38SAndreas Werner 		goto err;
77ffc7bb38SAndreas Werner 	}
78ffc7bb38SAndreas Werner 
79ffc7bb38SAndreas Werner 	if (dev_mapbase & 0x01) {
80ffc7bb38SAndreas Werner 		pr_info("IO mapped Device (16z%03d) not yet supported\n",
81ffc7bb38SAndreas Werner 			mdev->id);
82ffc7bb38SAndreas Werner 		ret = 0;
83ffc7bb38SAndreas Werner 		goto err;
84ffc7bb38SAndreas Werner 	}
85ffc7bb38SAndreas Werner 
863764e82eSJohannes Thumshirn 	pr_debug("Found a 16z%03d\n", mdev->id);
873764e82eSJohannes Thumshirn 
883764e82eSJohannes Thumshirn 	mdev->irq.start = GDD_IRQ(reg1);
893764e82eSJohannes Thumshirn 	mdev->irq.end = GDD_IRQ(reg1);
903764e82eSJohannes Thumshirn 	mdev->irq.flags = IORESOURCE_IRQ;
913764e82eSJohannes Thumshirn 
92ffc7bb38SAndreas Werner 	mdev->mem.start = dev_mapbase + offset;
93ffc7bb38SAndreas Werner 
943764e82eSJohannes Thumshirn 	mdev->mem.end = mdev->mem.start + size - 1;
953764e82eSJohannes Thumshirn 	mdev->mem.flags = IORESOURCE_MEM;
963764e82eSJohannes Thumshirn 
973764e82eSJohannes Thumshirn 	ret = mcb_device_register(bus, mdev);
983764e82eSJohannes Thumshirn 	if (ret < 0)
993764e82eSJohannes Thumshirn 		goto err;
1003764e82eSJohannes Thumshirn 
1013764e82eSJohannes Thumshirn 	return 0;
1023764e82eSJohannes Thumshirn 
1033764e82eSJohannes Thumshirn err:
104*63ba2d07SSanjuán García, Jorge 	mcb_free_dev(mdev);
1053764e82eSJohannes Thumshirn 
1063764e82eSJohannes Thumshirn 	return ret;
1073764e82eSJohannes Thumshirn }
1083764e82eSJohannes Thumshirn 
chameleon_parse_bar(void __iomem * base,struct chameleon_bar * cb,int bar_count)109ffc7bb38SAndreas Werner static void chameleon_parse_bar(void __iomem *base,
110ffc7bb38SAndreas Werner 				struct chameleon_bar *cb, int bar_count)
111ffc7bb38SAndreas Werner {
112ffc7bb38SAndreas Werner 	char __iomem *p = base;
113ffc7bb38SAndreas Werner 	int i;
114ffc7bb38SAndreas Werner 
115ffc7bb38SAndreas Werner 	/* skip reg1 */
116ffc7bb38SAndreas Werner 	p += sizeof(__le32);
117ffc7bb38SAndreas Werner 
118ffc7bb38SAndreas Werner 	for (i = 0; i < bar_count; i++) {
119ffc7bb38SAndreas Werner 		cb[i].addr = readl(p);
120ffc7bb38SAndreas Werner 		cb[i].size = readl(p + 4);
121ffc7bb38SAndreas Werner 
122ffc7bb38SAndreas Werner 		p += sizeof(struct chameleon_bar);
123ffc7bb38SAndreas Werner 	}
124ffc7bb38SAndreas Werner }
125ffc7bb38SAndreas Werner 
chameleon_get_bar(void __iomem ** base,phys_addr_t mapbase,struct chameleon_bar ** cb)126a889c276SRodríguez Barbarin, José Javier static int chameleon_get_bar(void __iomem **base, phys_addr_t mapbase,
127ffc7bb38SAndreas Werner 			     struct chameleon_bar **cb)
128ffc7bb38SAndreas Werner {
129ffc7bb38SAndreas Werner 	struct chameleon_bar *c;
130ffc7bb38SAndreas Werner 	int bar_count;
131ffc7bb38SAndreas Werner 	__le32 reg;
132ffc7bb38SAndreas Werner 	u32 dtype;
133ffc7bb38SAndreas Werner 
134ffc7bb38SAndreas Werner 	/*
135ffc7bb38SAndreas Werner 	 * For those devices which are not connected
136ffc7bb38SAndreas Werner 	 * to the PCI Bus (e.g. LPC) there is a bar
137ffc7bb38SAndreas Werner 	 * descriptor located directly after the
138ffc7bb38SAndreas Werner 	 * chameleon header. This header is comparable
139ffc7bb38SAndreas Werner 	 * to a PCI header.
140ffc7bb38SAndreas Werner 	 */
141ffc7bb38SAndreas Werner 	dtype = get_next_dtype(*base);
142ffc7bb38SAndreas Werner 	if (dtype == CHAMELEON_DTYPE_BAR) {
143ffc7bb38SAndreas Werner 		reg = readl(*base);
144ffc7bb38SAndreas Werner 
145ffc7bb38SAndreas Werner 		bar_count = BAR_CNT(reg);
146c836790aSMichael Moese 		if (bar_count <= 0 || bar_count > CHAMELEON_BAR_MAX)
147ffc7bb38SAndreas Werner 			return -ENODEV;
148ffc7bb38SAndreas Werner 
149ffc7bb38SAndreas Werner 		c = kcalloc(bar_count, sizeof(struct chameleon_bar),
150ffc7bb38SAndreas Werner 			    GFP_KERNEL);
151ffc7bb38SAndreas Werner 		if (!c)
152ffc7bb38SAndreas Werner 			return -ENOMEM;
153ffc7bb38SAndreas Werner 
154ffc7bb38SAndreas Werner 		chameleon_parse_bar(*base, c, bar_count);
155ffc7bb38SAndreas Werner 		*base += BAR_DESC_SIZE(bar_count);
156ffc7bb38SAndreas Werner 	} else {
157ffc7bb38SAndreas Werner 		c = kzalloc(sizeof(struct chameleon_bar), GFP_KERNEL);
158ffc7bb38SAndreas Werner 		if (!c)
159ffc7bb38SAndreas Werner 			return -ENOMEM;
160ffc7bb38SAndreas Werner 
161ffc7bb38SAndreas Werner 		bar_count = 1;
162ffc7bb38SAndreas Werner 		c->addr = mapbase;
163ffc7bb38SAndreas Werner 	}
164ffc7bb38SAndreas Werner 
165ffc7bb38SAndreas Werner 	*cb = c;
166ffc7bb38SAndreas Werner 
167ffc7bb38SAndreas Werner 	return bar_count;
168ffc7bb38SAndreas Werner }
169ffc7bb38SAndreas Werner 
chameleon_parse_cells(struct mcb_bus * bus,phys_addr_t mapbase,void __iomem * base)1703764e82eSJohannes Thumshirn int chameleon_parse_cells(struct mcb_bus *bus, phys_addr_t mapbase,
1713764e82eSJohannes Thumshirn 			void __iomem *base)
1723764e82eSJohannes Thumshirn {
1733764e82eSJohannes Thumshirn 	struct chameleon_fpga_header *header;
174ffc7bb38SAndreas Werner 	struct chameleon_bar *cb;
175a889c276SRodríguez Barbarin, José Javier 	void __iomem *p = base;
1763764e82eSJohannes Thumshirn 	int num_cells = 0;
177ffc7bb38SAndreas Werner 	uint32_t dtype;
178ffc7bb38SAndreas Werner 	int bar_count;
1795ec6bff1SChristophe JAILLET 	int ret;
1803764e82eSJohannes Thumshirn 	u32 hsize;
181a889c276SRodríguez Barbarin, José Javier 	u32 table_size;
1823764e82eSJohannes Thumshirn 
1833764e82eSJohannes Thumshirn 	hsize = sizeof(struct chameleon_fpga_header);
1843764e82eSJohannes Thumshirn 
1853764e82eSJohannes Thumshirn 	header = kzalloc(hsize, GFP_KERNEL);
1863764e82eSJohannes Thumshirn 	if (!header)
1873764e82eSJohannes Thumshirn 		return -ENOMEM;
1883764e82eSJohannes Thumshirn 
1893764e82eSJohannes Thumshirn 	/* Extract header information */
1903764e82eSJohannes Thumshirn 	memcpy_fromio(header, p, hsize);
1913764e82eSJohannes Thumshirn 	/* We only support chameleon v2 at the moment */
1923764e82eSJohannes Thumshirn 	header->magic = le16_to_cpu(header->magic);
1933764e82eSJohannes Thumshirn 	if (header->magic != CHAMELEONV2_MAGIC) {
1943764e82eSJohannes Thumshirn 		pr_err("Unsupported chameleon version 0x%x\n",
1953764e82eSJohannes Thumshirn 				header->magic);
196ffc7bb38SAndreas Werner 		ret = -ENODEV;
197ffc7bb38SAndreas Werner 		goto free_header;
1983764e82eSJohannes Thumshirn 	}
1993764e82eSJohannes Thumshirn 	p += hsize;
2003764e82eSJohannes Thumshirn 
201803f1ca6SJohannes Thumshirn 	bus->revision = header->revision;
202803f1ca6SJohannes Thumshirn 	bus->model = header->model;
203803f1ca6SJohannes Thumshirn 	bus->minor = header->minor;
204803f1ca6SJohannes Thumshirn 	snprintf(bus->name, CHAMELEON_FILENAME_LEN + 1, "%s",
2053764e82eSJohannes Thumshirn 		 header->filename);
2063764e82eSJohannes Thumshirn 
207ffc7bb38SAndreas Werner 	bar_count = chameleon_get_bar(&p, mapbase, &cb);
2085ec6bff1SChristophe JAILLET 	if (bar_count < 0) {
2095ec6bff1SChristophe JAILLET 		ret = bar_count;
210ffc7bb38SAndreas Werner 		goto free_header;
2115ec6bff1SChristophe JAILLET 	}
212ffc7bb38SAndreas Werner 
2133764e82eSJohannes Thumshirn 	for_each_chameleon_cell(dtype, p) {
2143764e82eSJohannes Thumshirn 		switch (dtype) {
2153764e82eSJohannes Thumshirn 		case CHAMELEON_DTYPE_GENERAL:
216ffc7bb38SAndreas Werner 			ret = chameleon_parse_gdd(bus, cb, p, bar_count);
2173764e82eSJohannes Thumshirn 			if (ret < 0)
218ffc7bb38SAndreas Werner 				goto free_bar;
2193764e82eSJohannes Thumshirn 			p += sizeof(struct chameleon_gdd);
2203764e82eSJohannes Thumshirn 			break;
2213764e82eSJohannes Thumshirn 		case CHAMELEON_DTYPE_BRIDGE:
222ffc7bb38SAndreas Werner 			chameleon_parse_bdd(bus, cb, p);
2233764e82eSJohannes Thumshirn 			p += sizeof(struct chameleon_bdd);
2243764e82eSJohannes Thumshirn 			break;
2253764e82eSJohannes Thumshirn 		case CHAMELEON_DTYPE_END:
2263764e82eSJohannes Thumshirn 			break;
2273764e82eSJohannes Thumshirn 		default:
2283764e82eSJohannes Thumshirn 			pr_err("Invalid chameleon descriptor type 0x%x\n",
2293764e82eSJohannes Thumshirn 				dtype);
230ffc7bb38SAndreas Werner 			ret = -EINVAL;
231ffc7bb38SAndreas Werner 			goto free_bar;
2323764e82eSJohannes Thumshirn 		}
2333764e82eSJohannes Thumshirn 		num_cells++;
2343764e82eSJohannes Thumshirn 	}
2353764e82eSJohannes Thumshirn 
236a889c276SRodríguez Barbarin, José Javier 	if (num_cells == 0) {
237a889c276SRodríguez Barbarin, José Javier 		ret = -EINVAL;
238a889c276SRodríguez Barbarin, José Javier 		goto free_bar;
239a889c276SRodríguez Barbarin, José Javier 	}
2403764e82eSJohannes Thumshirn 
241a889c276SRodríguez Barbarin, José Javier 	table_size = p - base;
242a889c276SRodríguez Barbarin, José Javier 	pr_debug("%d cell(s) found. Chameleon table size: 0x%04x bytes\n", num_cells, table_size);
243ffc7bb38SAndreas Werner 	kfree(cb);
2443764e82eSJohannes Thumshirn 	kfree(header);
245a889c276SRodríguez Barbarin, José Javier 	return table_size;
2463764e82eSJohannes Thumshirn 
247ffc7bb38SAndreas Werner free_bar:
248ffc7bb38SAndreas Werner 	kfree(cb);
249ffc7bb38SAndreas Werner free_header:
2503764e82eSJohannes Thumshirn 	kfree(header);
251ffc7bb38SAndreas Werner 
2523764e82eSJohannes Thumshirn 	return ret;
2533764e82eSJohannes Thumshirn }
254891e6036SJohannes Thumshirn EXPORT_SYMBOL_NS_GPL(chameleon_parse_cells, MCB);
255