1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (C) 2012 CERN (www.cern.ch)
4 * Author: Alessandro Rubini <rubini@gnudd.com>
6 * This work is part of the White Rabbit project, a research effort led
7 * by CERN, the European Institute for Nuclear Research.
9 #include <linux/module.h>
10 #include <linux/slab.h>
11 #include <linux/fmc.h>
12 #include <linux/sdb.h>
13 #include <linux/err.h>
14 #include <linux/fmc-sdb.h>
15 #include <asm/byteorder.h>
17 static uint32_t __sdb_rd(struct fmc_device
*fmc
, unsigned long address
,
20 uint32_t res
= fmc_readl(fmc
, address
);
22 return __be32_to_cpu(res
);
26 static struct sdb_array
*__fmc_scan_sdb_tree(struct fmc_device
*fmc
,
27 unsigned long sdb_addr
,
28 unsigned long reg_base
, int level
)
31 int i
, j
, n
, convert
= 0;
32 struct sdb_array
*arr
, *sub
;
34 onew
= fmc_readl(fmc
, sdb_addr
);
35 if (onew
== SDB_MAGIC
) {
36 /* Uh! If we are little-endian, we must convert */
37 if (SDB_MAGIC
!= __be32_to_cpu(SDB_MAGIC
))
39 } else if (onew
== __be32_to_cpu(SDB_MAGIC
)) {
40 /* ok, don't convert */
42 return ERR_PTR(-ENOENT
);
44 /* So, the magic was there: get the count from offset 4*/
45 onew
= __sdb_rd(fmc
, sdb_addr
+ 4, convert
);
46 n
= __be16_to_cpu(*(uint16_t *)&onew
);
47 arr
= kzalloc(sizeof(*arr
), GFP_KERNEL
);
49 return ERR_PTR(-ENOMEM
);
50 arr
->record
= kcalloc(n
, sizeof(arr
->record
[0]), GFP_KERNEL
);
51 arr
->subtree
= kcalloc(n
, sizeof(arr
->subtree
[0]), GFP_KERNEL
);
52 if (!arr
->record
|| !arr
->subtree
) {
56 return ERR_PTR(-ENOMEM
);
62 for (i
= 0; i
< n
; i
++) {
65 for (j
= 0; j
< sizeof(arr
->record
[0]); j
+= 4) {
66 *(uint32_t *)((void *)(arr
->record
+ i
) + j
) =
67 __sdb_rd(fmc
, sdb_addr
+ (i
* 64) + j
, convert
);
70 arr
->subtree
[i
] = ERR_PTR(-ENODEV
);
71 if (r
->empty
.record_type
== sdb_type_bridge
) {
72 struct sdb_component
*c
= &r
->bridge
.sdb_component
;
73 uint64_t subaddr
= __be64_to_cpu(r
->bridge
.sdb_child
);
74 uint64_t newbase
= __be64_to_cpu(c
->addr_first
);
78 sub
= __fmc_scan_sdb_tree(fmc
, subaddr
, newbase
,
80 arr
->subtree
[i
] = sub
; /* may be error */
84 sub
->baseaddr
= newbase
;
90 int fmc_scan_sdb_tree(struct fmc_device
*fmc
, unsigned long address
)
92 struct sdb_array
*ret
;
95 ret
= __fmc_scan_sdb_tree(fmc
, address
, 0 /* regs */, 0);
101 EXPORT_SYMBOL(fmc_scan_sdb_tree
);
103 static void __fmc_sdb_free(struct sdb_array
*arr
)
110 for (i
= 0; i
< n
; i
++) {
111 if (IS_ERR(arr
->subtree
[i
]))
113 __fmc_sdb_free(arr
->subtree
[i
]);
120 int fmc_free_sdb_tree(struct fmc_device
*fmc
)
122 __fmc_sdb_free(fmc
->sdb
);
126 EXPORT_SYMBOL(fmc_free_sdb_tree
);
128 /* This helper calls reprogram and inizialized sdb as well */
129 int fmc_reprogram_raw(struct fmc_device
*fmc
, struct fmc_driver
*d
,
130 void *gw
, unsigned long len
, int sdb_entry
)
134 ret
= fmc
->op
->reprogram_raw(fmc
, d
, gw
, len
);
140 /* We are required to find SDB at a given offset */
141 ret
= fmc_scan_sdb_tree(fmc
, sdb_entry
);
143 dev_err(&fmc
->dev
, "Can't find SDB at address 0x%x\n",
150 EXPORT_SYMBOL(fmc_reprogram_raw
);
152 /* This helper calls reprogram and inizialized sdb as well */
153 int fmc_reprogram(struct fmc_device
*fmc
, struct fmc_driver
*d
, char *gw
,
158 ret
= fmc
->op
->reprogram(fmc
, d
, gw
);
164 /* We are required to find SDB at a given offset */
165 ret
= fmc_scan_sdb_tree(fmc
, sdb_entry
);
167 dev_err(&fmc
->dev
, "Can't find SDB at address 0x%x\n",
174 EXPORT_SYMBOL(fmc_reprogram
);
176 void fmc_show_sdb_tree(const struct fmc_device
*fmc
)
178 pr_err("%s: not supported anymore, use debugfs to dump SDB\n",
181 EXPORT_SYMBOL(fmc_show_sdb_tree
);
183 signed long fmc_find_sdb_device(struct sdb_array
*tree
,
184 uint64_t vid
, uint32_t did
, unsigned long *sz
)
186 signed long res
= -ENODEV
;
188 struct sdb_product
*p
;
189 struct sdb_component
*c
;
190 int i
, n
= tree
->len
;
191 uint64_t last
, first
;
193 /* FIXME: what if the first interconnect is not at zero? */
194 for (i
= 0; i
< n
; i
++) {
195 r
= &tree
->record
[i
];
196 c
= &r
->dev
.sdb_component
;
199 if (!IS_ERR(tree
->subtree
[i
]))
200 res
= fmc_find_sdb_device(tree
->subtree
[i
],
203 return res
+ tree
->baseaddr
;
204 if (r
->empty
.record_type
!= sdb_type_device
)
206 if (__be64_to_cpu(p
->vendor_id
) != vid
)
208 if (__be32_to_cpu(p
->device_id
) != did
)
211 last
= __be64_to_cpu(c
->addr_last
);
212 first
= __be64_to_cpu(c
->addr_first
);
214 *sz
= (typeof(*sz
))(last
+ 1 - first
);
215 return first
+ tree
->baseaddr
;
219 EXPORT_SYMBOL(fmc_find_sdb_device
);