2 * Copyright (c) 2015 Landon Fuller <landon@landonf.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer,
10 * without modification.
11 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
12 * similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any
13 * redistribution must be conditioned upon including a substantially
14 * similar Disclaimer requirement for further binary redistribution.
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY
20 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
21 * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,
22 * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
27 * THE POSSIBILITY OF SUCH DAMAGES.
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
33 #include <sys/param.h>
35 #include <sys/kernel.h>
36 #include <sys/limits.h>
37 #include <sys/systm.h>
39 #include <machine/bus.h>
40 #include <machine/resource.h>
42 #include <dev/bhnd/bhndvar.h>
48 * Map a siba(4) OCP vendor code to its corresponding JEDEC JEP-106 vendor
51 * @param ocp_vendor An OCP vendor code.
52 * @return The BHND_MFGID constant corresponding to @p ocp_vendor, or
53 * BHND_MFGID_INVALID if the OCP vendor is unknown.
56 siba_get_bhnd_mfgid(uint16_t ocp_vendor)
60 return (BHND_MFGID_BCM);
62 return (BHND_MFGID_INVALID);
67 * Parse the SIBA_IDH_* fields from the per-core identification
68 * registers, returning a siba_core_id representation.
70 * @param idhigh The SIBA_R0_IDHIGH register.
71 * @param idlow The SIBA_R0_IDLOW register.
72 * @param core_id The core id (index) to include in the result.
73 * @param unit The unit number to include in the result.
76 siba_parse_core_id(uint32_t idhigh, uint32_t idlow, u_int core_idx, int unit)
81 uint8_t num_addrspace;
84 ocp_vendor = SIBA_REG_GET(idhigh, IDH_VENDOR);
85 sonics_rev = SIBA_REG_GET(idlow, IDL_SBREV);
86 num_addrspace = SIBA_REG_GET(idlow, IDL_NRADDR) + 1 /* + enum block */;
88 /* Determine the number of sonics config register blocks */
89 num_cfg = SIBA_CFG_NUM_2_2;
90 if (sonics_rev >= SIBA_IDL_SBREV_2_3)
91 num_cfg = SIBA_CFG_NUM_2_3;
93 return (struct siba_core_id) {
95 .vendor = siba_get_bhnd_mfgid(ocp_vendor),
96 .device = SIBA_REG_GET(idhigh, IDH_DEVICE),
97 .hwrev = SIBA_IDH_CORE_REV(idhigh),
101 .sonics_vendor = ocp_vendor,
102 .sonics_rev = sonics_rev,
103 .num_addrspace = num_addrspace,
104 .num_cfg_blocks = num_cfg
109 * Allocate and return a new empty device info structure.
111 * @param bus The requesting bus device.
113 * @retval NULL if allocation failed.
115 struct siba_devinfo *
116 siba_alloc_dinfo(device_t bus)
118 struct siba_devinfo *dinfo;
120 dinfo = malloc(sizeof(struct siba_devinfo), M_BHND, M_NOWAIT|M_ZERO);
124 for (u_int i = 0; i < nitems(dinfo->cfg); i++) {
125 dinfo->cfg[i] = NULL;
126 dinfo->cfg_rid[i] = -1;
129 resource_list_init(&dinfo->resources);
135 * Initialize a device info structure previously allocated via
136 * siba_alloc_dinfo, copying the provided core id.
138 * @param dev The requesting bus device.
139 * @param dinfo The device info instance.
140 * @param core Device core info.
143 * @retval non-zero initialization failed.
146 siba_init_dinfo(device_t dev, struct siba_devinfo *dinfo,
147 const struct siba_core_id *core_id)
149 dinfo->core_id = *core_id;
154 * Map an addrspace index to its corresponding bhnd(4) port number.
156 * @param addrspace Address space index.
159 siba_addrspace_port(u_int addrspace)
161 /* The first addrspace is always mapped to device0; the remainder
162 * are mapped to device1 */
170 * Map an addrspace index to its corresponding bhnd(4) region number.
172 * @param addrspace Address space index.
175 siba_addrspace_region(u_int addrspace)
177 /* The first addrspace is always mapped to device0.0; the remainder
178 * are mapped to device1.0 + (n - 1) */
182 return (addrspace - 1);
186 * Return the number of bhnd(4) ports to advertise for the given
189 * @param num_addrspace The number of siba address spaces.
192 siba_addrspace_port_count(u_int num_addrspace)
194 /* 0, 1, or 2 ports */
195 return min(num_addrspace, 2);
199 * Return the number of bhnd(4) regions to advertise on @p port
200 * given the provided @p num_addrspace address space count.
202 * @param num_addrspace The number of core-mapped siba(4) Sonics/OCP address
206 siba_addrspace_region_count(u_int num_addrspace, u_int port)
208 /* The first address space, if any, is mapped to device0.0 */
210 return (min(num_addrspace, 1));
212 /* All remaining address spaces are mapped to device0.(n - 1) */
213 if (port == 1 && num_addrspace >= 2)
214 return (num_addrspace - 1);
216 /* No region mapping */
221 * Return true if @p port is defined given an address space count
222 * of @p num_addrspace, false otherwise.
224 * Refer to the siba_find_addrspace() function for information on siba's
225 * mapping of bhnd(4) port and region identifiers.
227 * @param num_addrspace The number of address spaces to verify the port against.
228 * @param type The bhnd(4) port type.
229 * @param port The bhnd(4) port number.
232 siba_is_port_valid(u_int num_addrspace, bhnd_port_type type, u_int port)
234 /* Only device ports are supported */
235 if (type != BHND_PORT_DEVICE)
238 /* Verify the index against the port count */
239 if (siba_addrspace_port_count(num_addrspace) <= port)
246 * Map a bhnd(4) type/port/region triplet to its associated address space
249 * For compatibility with bcma(4), we map address spaces to port/region
250 * identifiers as follows:
258 * The only supported port type is BHND_PORT_DEVICE.
260 * @param num_addrspace The number of available siba address spaces.
261 * @param type The bhnd(4) port type.
262 * @param port The bhnd(4) port number.
263 * @param region The bhnd(4) port region.
264 * @param addridx On success, the corresponding addrspace index.
267 * @retval ENOENT if the given type/port/region cannot be mapped to a
268 * siba address space.
271 siba_addrspace_index(u_int num_addrspace, bhnd_port_type type, u_int port,
272 u_int region, u_int *addridx)
276 if (!siba_is_port_valid(num_addrspace, type, port))
286 if (idx >= num_addrspace)
295 * Map an bhnd(4) type/port/region triplet to its associated address space
298 * The only supported port type is BHND_PORT_DEVICE.
300 * @param dinfo The device info to search for a matching address space.
301 * @param type The bhnd(4) port type.
302 * @param port The bhnd(4) port number.
303 * @param region The bhnd(4) port region.
305 struct siba_addrspace *
306 siba_find_addrspace(struct siba_devinfo *dinfo, bhnd_port_type type, u_int port,
312 /* Map to addrspace index */
313 error = siba_addrspace_index(dinfo->core_id.num_addrspace, type, port,
319 if (addridx >= SIBA_MAX_ADDRSPACE)
322 return (&dinfo->addrspace[addridx]);
326 * Append an address space entry to @p dinfo.
328 * @param dinfo The device info entry to update.
329 * @param addridx The address space index.
330 * @param base The mapping's base address.
331 * @param size The mapping size.
332 * @param bus_reserved Number of bytes to reserve in @p size for bus use
333 * when registering the resource list entry. This is used to reserve bus
334 * access to the core's SIBA_CFG* register blocks.
337 * @retval non-zero An error occurred appending the entry.
340 siba_append_dinfo_region(struct siba_devinfo *dinfo, uint8_t addridx,
341 uint32_t base, uint32_t size, uint32_t bus_reserved)
343 struct siba_addrspace *sa;
346 /* Verify that base + size will not overflow */
347 if (size > 0 && UINT32_MAX - (size - 1) < base)
350 /* Verify that size - bus_reserved will not underflow */
351 if (size < bus_reserved)
354 /* Must not be 0-length */
358 /* Must not exceed addrspace array size */
359 if (addridx >= nitems(dinfo->addrspace))
362 /* Initialize new addrspace entry */
363 sa = &dinfo->addrspace[addridx];
366 sa->sa_bus_reserved = bus_reserved;
368 /* Populate the resource list */
369 r_size = size - bus_reserved;
370 sa->sa_rid = resource_list_add_next(&dinfo->resources, SYS_RES_MEMORY,
371 base, base + (r_size - 1), r_size);
377 * Deallocate the given device info structure and any associated resources.
379 * @param dev The requesting bus device.
380 * @param dinfo Device info to be deallocated.
383 siba_free_dinfo(device_t dev, struct siba_devinfo *dinfo)
385 resource_list_free(&dinfo->resources);
387 /* Free all mapped configuration blocks */
388 for (u_int i = 0; i < nitems(dinfo->cfg); i++) {
389 if (dinfo->cfg[i] == NULL)
392 bhnd_release_resource(dev, SYS_RES_MEMORY, dinfo->cfg_rid[i],
395 dinfo->cfg[i] = NULL;
396 dinfo->cfg_rid[i] = -1;
403 * Return the core-enumeration-relative offset for the @p addrspace
404 * SIBA_R0_ADMATCH* register.
406 * @param addrspace The address space index.
408 * @retval non-zero success
409 * @retval 0 the given @p addrspace index is not supported.
412 siba_admatch_offset(uint8_t addrspace)
416 return SB0_REG_ABS(SIBA_CFG0_ADMATCH0);
418 return SB0_REG_ABS(SIBA_CFG0_ADMATCH1);
420 return SB0_REG_ABS(SIBA_CFG0_ADMATCH2);
422 return SB0_REG_ABS(SIBA_CFG0_ADMATCH3);
429 * Parse a SIBA_R0_ADMATCH* register.
431 * @param addrspace The address space index.
432 * @param am The address match register value to be parsed.
433 * @param[out] addr The parsed address.
434 * @param[out] size The parsed size.
437 * @retval non-zero a parse error occurred.
440 siba_parse_admatch(uint32_t am, uint32_t *addr, uint32_t *size)
444 /* Negative encoding is not supported. This is not used on any
445 * currently known devices*/
446 if (am & SIBA_AM_ADNEG)
449 /* Extract the base address and size */
450 am_type = SIBA_REG_GET(am, AM_TYPE);
453 *addr = am & SIBA_AM_BASE0_MASK;
454 *size = 1 << (SIBA_REG_GET(am, AM_ADINT0) + 1);
457 *addr = am & SIBA_AM_BASE1_MASK;
458 *size = 1 << (SIBA_REG_GET(am, AM_ADINT1) + 1);
461 *addr = am & SIBA_AM_BASE2_MASK;
462 *size = 1 << (SIBA_REG_GET(am, AM_ADINT2) + 1);
472 * Write @p value to @p dev's CFG0 target/initiator state register and
473 * wait for completion.
475 * @param dev The siba(4) child device.
476 * @param reg The state register to write (e.g. SIBA_CFG0_TMSTATELOW,
478 * @param value The value to write to @p reg.
479 * @param mask The mask of bits to be included from @p value.
482 * @retval ENODEV if SIBA_CFG0 is not mapped by @p dinfo.
483 * @retval ETIMEDOUT if a timeout occurs prior to SIBA_TMH_BUSY clearing.
486 siba_write_target_state(device_t dev, struct siba_devinfo *dinfo,
487 bus_size_t reg, uint32_t value, uint32_t mask)
489 struct bhnd_resource *r;
492 /* Must have a CFG0 block */
493 if ((r = dinfo->cfg[0]) == NULL)
496 /* Verify the register offset falls within CFG register block */
497 if (reg > SIBA_CFG_SIZE-4)
500 for (int i = 0; i < 300; i += 10) {
501 rval = bhnd_bus_read_4(r, reg);
503 rval |= (value & mask);
505 bhnd_bus_write_4(r, reg, rval);
506 bhnd_bus_read_4(r, reg); /* read-back */
509 /* If the write has completed, wait for target busy state
511 rval = bhnd_bus_read_4(r, reg);
512 if ((rval & mask) == (value & mask))
513 return (siba_wait_target_busy(dev, dinfo, 100000));
522 * Spin for up to @p usec waiting for SIBA_TMH_BUSY to clear in
523 * @p dev's SIBA_CFG0_TMSTATEHIGH register.
525 * @param dev The siba(4) child device to wait on.
526 * @param dinfo The @p dev's device info
528 * @retval 0 if SIBA_TMH_BUSY is cleared prior to the @p usec timeout.
529 * @retval ENODEV if SIBA_CFG0 is not mapped by @p dinfo.
530 * @retval ETIMEDOUT if a timeout occurs prior to SIBA_TMH_BUSY clearing.
533 siba_wait_target_busy(device_t dev, struct siba_devinfo *dinfo, int usec)
535 struct bhnd_resource *r;
538 if ((r = dinfo->cfg[0]) == NULL)
541 for (int i = 0; i < usec; i += 10) {
542 ts_high = bhnd_bus_read_4(r, SIBA_CFG0_TMSTATEHIGH);
543 if (!(ts_high & SIBA_TMH_BUSY))
549 device_printf(dev, "SIBA_TMH_BUSY wait timeout\n");