Added 400ns delay in ata_WaitBusyTO before read of device status.
[tangerine.git] / arch / common / hidd.radeon / bitmap.c
blobe1422ac9f9e998fc04977e1fd477ed88d07bb079
1 /*
2 Copyright © 2003-2007, The AROS Development Team. All rights reserved.
3 $Id$
4 */
6 #define DEBUG 0
7 #include "bitmap.h"
8 #include <exec/types.h>
9 #include <proto/exec.h>
10 #include <aros/debug.h>
12 LONG bfffo(ULONG val, UBYTE bitoffset)
14 val &= (0xffffffff >> bitoffset);
15 if (val)
16 return __builtin_clz(val);
17 else
18 return 32;
21 LONG bfflo(ULONG val, UBYTE bitoffset)
23 val &= (0xffffffff << (31-bitoffset));
24 if (val)
25 return 31-__builtin_ctz(val);
26 else
27 return -1;
30 LONG bfffz(ULONG val, UBYTE bitoffset)
32 return bfffo(~val, bitoffset);
35 LONG bfflz(ULONG val, UBYTE bitoffset)
37 return bfflo(~val, bitoffset);
40 ULONG bfset(ULONG data, UBYTE bitoffset, UBYTE bits)
42 ULONG mask = ~((1 << (32 - bits)) - 1);
43 mask >>= bitoffset;
44 return data | mask;
47 ULONG bfclr(ULONG data, UBYTE bitoffset, UBYTE bits)
49 ULONG mask = ~((1 << (32 - bits)) - 1);
50 mask >>= bitoffset;
51 return data & ~mask;
54 LONG bmffo(ULONG *bitmap, ULONG longs, LONG bitoffset)
56 ULONG *scan = bitmap;
57 ULONG err = 32*longs;
58 int longoffset, bit;
60 longoffset = bitoffset >> 5;
61 longs -= longoffset;
62 scan += longoffset;
64 bitoffset = bitoffset & 0x1F;
66 if (bitoffset != 0) {
67 if ((bit = bfffo(*scan, bitoffset)) < 32) {
68 return (bit + ((scan - bitmap) << 5));
70 scan++;
71 longs--;
74 while (longs-- > 0) {
75 if (*scan++ != 0) {
76 return (bfffo(*--scan,0) + ((scan - bitmap) << 5));
80 return (err);
83 LONG bmffz(ULONG *bitmap, ULONG longs, LONG bitoffset)
85 ULONG *scan = bitmap;
86 ULONG err = 32*longs;
87 int longoffset, bit;
89 longoffset = bitoffset >> 5;
90 longs -= longoffset;
91 scan += longoffset;
93 bitoffset = bitoffset & 0x1F;
95 if (bitoffset != 0) {
96 if ((bit = bfffz(*scan, bitoffset)) < 32) {
97 return (bit + ((scan - bitmap) << 5));
99 scan++;
100 longs--;
103 while (longs-- > 0) {
104 if (*scan++ != 0xFFFFFFFF) {
105 return (bfffz(*--scan,0) + ((scan - bitmap) << 5));
109 return (err);
112 LONG bmclr(ULONG *bitmap, ULONG longs, LONG bitoffset, LONG bits)
114 ULONG *scan = bitmap;
115 int longoffset;
116 int orgbits = bits;
118 longoffset = bitoffset >> 5;
119 longs -= longoffset;
120 scan += longoffset;
122 bitoffset = bitoffset & 0x1F;
124 if (bitoffset != 0) {
125 if (bits < 32) {
126 *scan = bfclr(*scan, bitoffset, bits);
127 } else {
128 *scan = bfclr(*scan, bitoffset, 32);
130 scan++;
131 longs--;
132 bits -= 32 - bitoffset;
135 while (bits > 0 && longs-- > 0) {
136 if (bits > 31) {
137 *scan++ = 0;
138 } else {
139 *scan = bfclr(*scan, 0, bits);
141 bits -= 32;
144 if (bits <= 0) {
145 return (orgbits);
147 return (orgbits - bits);
150 LONG bmset(ULONG *bitmap, ULONG longs, LONG bitoffset, LONG bits)
152 ULONG *scan = bitmap;
153 int longoffset;
154 int orgbits = bits;
156 longoffset = bitoffset >> 5;
157 longs -= longoffset;
158 scan += longoffset;
160 bitoffset = bitoffset & 0x1F;
162 if (bitoffset != 0) {
163 if (bits < 32) {
164 *scan = (bfset((*scan), bitoffset, bits));
165 } else {
166 *scan = (bfset((*scan), bitoffset, 32));
168 scan++;
169 longs--;
170 bits -= 32 - bitoffset;
173 while (bits > 0 && longs-- > 0) {
174 if (bits > 31) {
175 *scan++ = 0xFFFFFFFF;
176 } else {
177 *scan = (bfset((*scan), 0, bits));
179 bits -= 32;
182 if (bits <= 0) {
183 return (orgbits);
185 return (orgbits - bits);
188 int bmtstz(ULONG *bitmap, ULONG bitoffset, LONG bits)
190 LONG longoffset = bitoffset >> 5;
191 ULONG *scan = bitmap;
192 ULONG mask;
194 scan += longoffset;
195 bitoffset &= 0x1f;
197 if (bitoffset != 0)
199 if ((bits + bitoffset) < 32)
201 mask = (0xffffffff >> bitoffset) & (0xffffffff << (32 - (bits+bitoffset)));
202 bits=0;
204 else
206 mask = (0xffffffff >> bitoffset);
207 bits -= (32-bitoffset);
209 if ((mask & (*scan++)) != 0)
210 return 0;
213 while (bits > 0)
215 if (bits >= 32)
217 mask=0xffffffff;
218 bits -= 32;
220 else
222 mask = 0xffffffff << (32-bits);
223 bits = 0;
225 if ((mask & (*scan++)) != 0)
226 return 0;
229 return 1;
232 void BitmapInit(struct ati_staticdata *sd)
235 * If Radeon chip has some video memory, create a bitmap representing all allocations.
236 * Divide whole memory into 1KB chunks
238 if (sd->Card.FbUsableSize)
239 sd->CardMemBmp = AllocPooled(sd->memPool, sd->Card.FbUsableSize >> 13);
241 /* Number of ULONG's in bitmap */
242 sd->CardMemSize = sd->Card.FbUsableSize >> 15;
244 bug("[ATIBMP] Bitmap at %p, size %d bytes (%d bits)\n", sd->CardMemBmp, sd->CardMemSize << 2, sd->Card.FbUsableSize >> 10);
247 void BitmapFree(struct ati_staticdata *sd, ULONG ptr, ULONG size)
249 if (ptr + size < sd->Card.FbUsableSize)
251 bmclr(sd->CardMemBmp, sd->CardMemSize, ptr >> 10, (size + 1023) >> 10);
255 ULONG BitmapAlloc(struct ati_staticdata *sd, ULONG size)
257 ULONG ptr;
258 size = (size + 1023) >> 10;
260 ptr = bmffz(sd->CardMemBmp, sd->CardMemSize, 0);
262 D(bug("[ATIBMP] BitmapAlloc(%d)\n", size));
264 while (ptr <= (sd->CardMemSize << 5) + size)
266 D(bug("[ATIBMP] ptr=%08x\n", ptr));
268 if (bmtstz(sd->CardMemBmp, ptr, size))
270 bmset(sd->CardMemBmp, sd->CardMemSize, ptr, size);
271 break;
274 ptr = bmffo(sd->CardMemBmp, sd->CardMemSize, ptr);
275 ptr = bmffz(sd->CardMemBmp, sd->CardMemSize, ptr);
278 if (ptr > (sd->CardMemSize << 5) - size)
279 ptr = 0xffffffff;
280 else
281 ptr <<= 10;
283 return ptr;