blob: 82f2fe6cd1cf45187e529a231f2538f8f2f5be1d [file] [log] [blame]
wdenkea287de2005-04-01 00:25:43 +00001/*
2 * SNTP support driver
3 *
4 * Masami Komiya <mkomiya@sonare.it> 2005
5 *
6 */
7
8#include <common.h>
9#include <command.h>
10#include <net.h>
11#include <rtc.h>
12
13#include "sntp.h"
14
Bartlomiej Sieka49f3bdb2008-10-01 15:26:28 +020015#define SNTP_TIMEOUT 10000UL
wdenkea287de2005-04-01 00:25:43 +000016
17static int SntpOurPort;
18
19static void
20SntpSend (void)
21{
22 struct sntp_pkt_t pkt;
23 int pktlen = SNTP_PACKET_LEN;
24 int sport;
25
Robin Getz0ebf04c2009-07-23 03:01:03 -040026 debug("%s\n", __func__);
wdenkea287de2005-04-01 00:25:43 +000027
28 memset (&pkt, 0, sizeof(pkt));
29
30 pkt.li = NTP_LI_NOLEAP;
31 pkt.vn = NTP_VERSION;
32 pkt.mode = NTP_MODE_CLIENT;
33
34 memcpy ((char *)NetTxPacket + NetEthHdrSize() + IP_HDR_SIZE, (char *)&pkt, pktlen);
35
36 SntpOurPort = 10000 + (get_timer(0) % 4096);
37 sport = NTP_SERVICE_PORT;
38
39 NetSendUDPPacket (NetServerEther, NetNtpServerIP, sport, SntpOurPort, pktlen);
40}
41
42static void
43SntpTimeout (void)
44{
45 puts ("Timeout\n");
46 NetState = NETLOOP_FAIL;
47 return;
48}
49
50static void
Luca Ceresoli03eb1292011-04-18 06:19:50 +000051SntpHandler(uchar *pkt, unsigned dest, IPaddr_t sip, unsigned src,
52 unsigned len)
wdenkea287de2005-04-01 00:25:43 +000053{
wdenk414eec32005-04-02 22:37:54 +000054 struct sntp_pkt_t *rpktp = (struct sntp_pkt_t *)pkt;
wdenkea287de2005-04-01 00:25:43 +000055 struct rtc_time tm;
wdenk414eec32005-04-02 22:37:54 +000056 ulong seconds;
wdenkea287de2005-04-01 00:25:43 +000057
Robin Getz0ebf04c2009-07-23 03:01:03 -040058 debug("%s\n", __func__);
wdenkea287de2005-04-01 00:25:43 +000059
60 if (dest != SntpOurPort) return;
61
wdenk414eec32005-04-02 22:37:54 +000062 /*
63 * As the RTC's used in U-Boot sepport second resolution only
64 * we simply ignore the sub-second field.
65 */
66 memcpy (&seconds, &rpktp->transmit_timestamp, sizeof(ulong));
wdenkea287de2005-04-01 00:25:43 +000067
wdenk414eec32005-04-02 22:37:54 +000068 to_tm(ntohl(seconds) - 2208988800UL + NetTimeOffset, &tm);
Jon Loeliger643d1ab2007-07-09 17:45:14 -050069#if defined(CONFIG_CMD_DATE)
wdenkea287de2005-04-01 00:25:43 +000070 rtc_set (&tm);
71#endif
72 printf ("Date: %4d-%02d-%02d Time: %2d:%02d:%02d\n",
73 tm.tm_year, tm.tm_mon, tm.tm_mday,
74 tm.tm_hour, tm.tm_min, tm.tm_sec);
wdenkea287de2005-04-01 00:25:43 +000075
76 NetState = NETLOOP_SUCCESS;
77}
78
79void
80SntpStart (void)
81{
Robin Getz0ebf04c2009-07-23 03:01:03 -040082 debug("%s\n", __func__);
wdenkea287de2005-04-01 00:25:43 +000083
Bartlomiej Sieka49f3bdb2008-10-01 15:26:28 +020084 NetSetTimeout (SNTP_TIMEOUT, SntpTimeout);
wdenkea287de2005-04-01 00:25:43 +000085 NetSetHandler(SntpHandler);
86 memset (NetServerEther, 0, 6);
87
88 SntpSend ();
89}