Forgotten changes that should have been part of the r45368 64-bit fix.
[AROS.git] / rom / devs / ata / timer.c
blob8ce0285f354e3bce66b4a496d77b4b8d8e1e9427
1 /*
2 Copyright © 2009, The AROS Development Team. All rights reserved
3 $Id$
5 Desc:
6 Lang: English
7 */
8 /*
9 * PARTIAL CHANGELOG:
10 * DATE NAME ENTRY
11 * ---------- ------------------ -------------------------------------------------------------------
12 * 2005-03-06 T. Wiszkowski few corrections (thanks, Georg)
13 * 2005-03-05 T. Wiszkowski created file; initial benchmarked nanowait and timer-based micro/sec wait
16 #include <exec/types.h>
17 #include <devices/timer.h>
18 #include <exec/io.h>
19 #include <proto/exec.h>
20 #include <aros/debug.h>
21 #include <proto/timer.h>
23 #include "timer.h"
24 #include "ata.h"
26 static BOOL ata_Calibrate(struct IORequest* tmr, struct ataBase *base)
28 register ULONG x;
29 register ULONG scale = 0x8000; // min iterations...
30 volatile register ULONG t = 1;
31 struct timeval t1, t2;
32 struct Device *TimerBase = base->ata_TimerBase;
34 D(bug("[ATA ] Calibration started\n"));
36 while (scale <= 0x80000000)
38 Forbid();
39 GetSysTime(&t1);
40 for (x = 1; x < scale; x++)
41 t = (((t + x) * t) - x) / x; // add, mul, sub, div, trivial benchmark.
43 GetSysTime(&t2);
44 Permit();
45 SubTime(&t2, &t1);
47 // ok, it's going to be totally insane, if secs > 1.
48 if (t2.tv_secs != 0)
50 bug("[ATA ] micro wait useless.\n");
51 return FALSE;
54 /*
55 * we expect at least 10000 times longer period, which should be 'achievable'
56 * unlikely we will cross the magic boundary here of 4 billion instructions in 10 millisecond (yielding 400'000MIPS?)
57 * 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...
60 if (t2.tv_micro >= 10000)
61 break;
62 scale <<= 1;
65 D(bug("[ATA ] Executed %ld ops in %ldus\n", scale, t2.tv_micro));
67 // always round up to the next value.. so 30.9 -> 31, 5.1 -> 6, etc
68 x = (x + t2.tv_micro - 1) / t2.tv_micro;
69 x = (x+9) / 10;
71 bug("[ATA ] Approximate number of iterations per 100 nanoseconds: %ld\n", x);
72 base->ata_ItersPer100ns = x;
73 return TRUE;
76 struct IORequest *ata_OpenTimer(struct ataBase *base)
78 struct MsgPort *p = CreateMsgPort();
79 if (NULL != p)
81 struct IORequest *io = CreateIORequest(p, sizeof(struct timerequest));
83 if (NULL != io)
86 * ok. ECLOCK does not have too great resolution, either.
87 * we will have to sacrifice our performance a little bit, meaning, the 400ns will turn into (worst case) 2us.
88 * hopefully we won't have to call that TOO often...
90 if (0 == OpenDevice("timer.device", UNIT_MICROHZ, io, 0))
92 if (NULL == base->ata_TimerBase)
94 base->ata_TimerBase = io->io_Device;
95 ata_Calibrate(io, base);
97 return io;
99 else
101 bug("[ATA ] Failed to open timer.device, unit MICROHZ\n");
103 DeleteIORequest(io);
105 else
107 bug("[ATA ] Failed to create timerequest\n");
109 DeleteMsgPort(p);
111 else
113 bug("[ATA ] Failed to create timer port\n");
116 return NULL;
119 void ata_CloseTimer(struct IORequest *tmr)
121 if (NULL != tmr)
123 struct MsgPort *p = tmr->io_Message.mn_ReplyPort;
124 CloseDevice(tmr);
125 DeleteIORequest(tmr);
126 DeleteMsgPort(p);
130 void ata_WaitNano(register ULONG ns, struct ataBase *base)
132 volatile register ULONG t = 1;
133 ns = (ns + 99) / 100;
134 ns *= base->ata_ItersPer100ns;
135 while (ns > 0)
137 t = (((t + ns) * t) - ns) / ns; // add, mul, sub, div, trivial benchmark.
138 --ns;
142 ULONG ata_WaitTO(struct IORequest* tmr, ULONG secs, ULONG micro, ULONG sigs)
144 ULONG sig = 1 << tmr->io_Message.mn_ReplyPort->mp_SigBit;
146 //D(bug("[ATA--] Timed wait %lds %ldu\n", secs, micro));
148 tmr->io_Command = TR_ADDREQUEST;
149 ((struct timerequest*)tmr)->tr_time.tv_secs = secs;
150 ((struct timerequest*)tmr)->tr_time.tv_micro = micro;
152 SendIO(tmr);
153 sigs = Wait(sigs | sig);
154 if (0 == (sigs & sig))
156 if (!CheckIO(tmr))
157 AbortIO(tmr);
159 WaitIO(tmr);
161 SetSignal(0, sig);
163 return sigs &~ sig;