1235676Sadrian/*-
2235676Sadrian * Copyright (c) 2002-2009 Sam Leffler, Errno Consulting
3235676Sadrian * All rights reserved.
4235676Sadrian *
5235676Sadrian * Redistribution and use in source and binary forms, with or without
6235676Sadrian * modification, are permitted provided that the following conditions
7235676Sadrian * are met:
8235676Sadrian * 1. Redistributions of source code must retain the above copyright
9235676Sadrian *    notice, this list of conditions and the following disclaimer,
10235676Sadrian *    without modification.
11235676Sadrian * 2. Redistributions in binary form must reproduce at minimum a disclaimer
12235676Sadrian *    similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any
13235676Sadrian *    redistribution must be conditioned upon including a substantially
14235676Sadrian *    similar Disclaimer requirement for further binary redistribution.
15235676Sadrian *
16235676Sadrian * NO WARRANTY
17235676Sadrian * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18235676Sadrian * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19235676Sadrian * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY
20235676Sadrian * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
21235676Sadrian * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,
22235676Sadrian * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23235676Sadrian * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24235676Sadrian * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25235676Sadrian * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26235676Sadrian * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
27235676Sadrian * THE POSSIBILITY OF SUCH DAMAGES.
28235676Sadrian *
29235676Sadrian * $FreeBSD$
30235676Sadrian */
31235676Sadrian#ifndef	__IF_ATH_TSF_H__
32235676Sadrian#define	__IF_ATH_TSF_H__
33235676Sadrian
34235676Sadrian/*
35235676Sadrian * Extend 15-bit time stamp from rx descriptor to
36235676Sadrian * a full 64-bit TSF using the specified TSF.
37235676Sadrian */
38235676Sadrianstatic __inline u_int64_t
39235676Sadrianath_extend_tsf15(u_int32_t rstamp, u_int64_t tsf)
40235676Sadrian{
41235676Sadrian	if ((tsf & 0x7fff) < rstamp)
42235676Sadrian		tsf -= 0x8000;
43235676Sadrian
44235676Sadrian	return ((tsf &~ 0x7fff) | rstamp);
45235676Sadrian}
46235676Sadrian
47235676Sadrian/*
48235676Sadrian * Extend 32-bit time stamp from rx descriptor to
49235676Sadrian * a full 64-bit TSF using the specified TSF.
50235676Sadrian */
51235676Sadrianstatic __inline u_int64_t
52235676Sadrianath_extend_tsf32(u_int32_t rstamp, u_int64_t tsf)
53235676Sadrian{
54235676Sadrian	u_int32_t tsf_low = tsf & 0xffffffff;
55235676Sadrian	u_int64_t tsf64 = (tsf & ~0xffffffffULL) | rstamp;
56235676Sadrian
57235676Sadrian	if (rstamp > tsf_low && (rstamp - tsf_low > 0x10000000))
58235676Sadrian		tsf64 -= 0x100000000ULL;
59235676Sadrian
60235676Sadrian	if (rstamp < tsf_low && (tsf_low - rstamp > 0x10000000))
61235676Sadrian		tsf64 += 0x100000000ULL;
62235676Sadrian
63235676Sadrian	return tsf64;
64235676Sadrian}
65235676Sadrian
66235676Sadrian/*
67235676Sadrian * Extend the TSF from the RX descriptor to a full 64 bit TSF.
68235676Sadrian * Earlier hardware versions only wrote the low 15 bits of the
69235676Sadrian * TSF into the RX descriptor; later versions (AR5416 and up)
70235676Sadrian * include the 32 bit TSF value.
71235676Sadrian */
72235676Sadrianstatic __inline u_int64_t
73235676Sadrianath_extend_tsf(struct ath_softc *sc, u_int32_t rstamp, u_int64_t tsf)
74235676Sadrian{
75235676Sadrian	if (sc->sc_rxtsf32)
76235676Sadrian		return ath_extend_tsf32(rstamp, tsf);
77235676Sadrian	else
78235676Sadrian		return ath_extend_tsf15(rstamp, tsf);
79235676Sadrian}
80235676Sadrian
81235676Sadrian#endif
82