delint
[AROS.git] / rom / devs / ata / waitnano.c
blob8b98a9a60f9d5de41e39e85ca4a5adce2113c027
1 /*
2 Copyright © 2013, The AROS Development Team. All rights reserved
3 $Id$
4 */
6 #include <exec/types.h>
7 #include <devices/timer.h>
8 #include <exec/io.h>
9 #include <proto/exec.h>
10 #include <aros/debug.h>
11 #include <proto/timer.h>
13 #include "timer.h"
14 #include "ata.h"
16 BOOL ata_Calibrate(struct IORequest* tmr, struct ataBase *base)
18 register ULONG x;
19 register ULONG scale = 0x8000; // min iterations...
20 volatile register ULONG t = 1;
21 struct timeval t1, t2;
22 struct Device *TimerBase = tmr->io_Device;
24 D(bug("[ATA ] Calibration started\n"));
26 while (scale <= 0x80000000)
28 Forbid();
29 GetSysTime(&t1);
30 for (x = 1; x < scale; x++)
31 t = (((t + x) * t) - x) / x; // add, mul, sub, div, trivial benchmark.
33 GetSysTime(&t2);
34 Permit();
35 SubTime(&t2, &t1);
37 // ok, it's going to be totally insane, if secs > 1.
38 if (t2.tv_secs != 0)
40 bug("[ATA ] micro wait useless.\n");
41 return FALSE;
44 /*
45 * we expect at least 10000 times longer period, which should be 'achievable'
46 * unlikely we will cross the magic boundary here of 4 billion instructions in 10 millisecond (yielding 400'000MIPS?)
47 * on the other side, if we go as low as 1, then 4 iterations of add/sub/mul/div is perfectly fine yielding a bit more than 400ns...
50 if (t2.tv_micro >= 10000)
51 break;
52 scale <<= 1;
55 D(bug("[ATA ] Executed %ld ops in %ldus\n", scale, t2.tv_micro));
57 // always round up to the next value.. so 30.9 -> 31, 5.1 -> 6, etc
58 x = (x + t2.tv_micro - 1) / t2.tv_micro;
59 x = (x+9) / 10;
61 bug("[ATA ] Approximate number of iterations per 100 nanoseconds: %ld\n", x);
62 base->ata_ItersPer100ns = x;
63 return TRUE;
66 void ata_WaitNano(register ULONG ns, struct ataBase *base)
68 volatile register ULONG t = 1;
69 ns = (ns + 99) / 100;
70 ns *= base->ata_ItersPer100ns;
71 while (ns > 0)
73 t = (((t + ns) * t) - ns) / ns; // add, mul, sub, div, trivial benchmark.
74 --ns;