blob: 76c10ecd3b5d4565c684d155659e5a2036636860 [file] [log] [blame]
wdenkb4ad9622005-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 Sieka56668462008-10-01 15:26:28 +020015#define SNTP_TIMEOUT 10000UL
wdenkb4ad9622005-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 Getz9e0a4d62009-07-23 03:01:03 -040026 debug("%s\n", __func__);
wdenkb4ad9622005-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
51SntpHandler (uchar *pkt, unsigned dest, unsigned src, unsigned len)
52{
wdenk8d5d28a2005-04-02 22:37:54 +000053 struct sntp_pkt_t *rpktp = (struct sntp_pkt_t *)pkt;
wdenkb4ad9622005-04-01 00:25:43 +000054 struct rtc_time tm;
wdenk8d5d28a2005-04-02 22:37:54 +000055 ulong seconds;
wdenkb4ad9622005-04-01 00:25:43 +000056
Robin Getz9e0a4d62009-07-23 03:01:03 -040057 debug("%s\n", __func__);
wdenkb4ad9622005-04-01 00:25:43 +000058
59 if (dest != SntpOurPort) return;
60
wdenk8d5d28a2005-04-02 22:37:54 +000061 /*
62 * As the RTC's used in U-Boot sepport second resolution only
63 * we simply ignore the sub-second field.
64 */
65 memcpy (&seconds, &rpktp->transmit_timestamp, sizeof(ulong));
wdenkb4ad9622005-04-01 00:25:43 +000066
wdenk8d5d28a2005-04-02 22:37:54 +000067 to_tm(ntohl(seconds) - 2208988800UL + NetTimeOffset, &tm);
Jon Loeliger54f35c22007-07-09 17:45:14 -050068#if defined(CONFIG_CMD_DATE)
wdenkb4ad9622005-04-01 00:25:43 +000069 rtc_set (&tm);
70#endif
71 printf ("Date: %4d-%02d-%02d Time: %2d:%02d:%02d\n",
72 tm.tm_year, tm.tm_mon, tm.tm_mday,
73 tm.tm_hour, tm.tm_min, tm.tm_sec);
wdenkb4ad9622005-04-01 00:25:43 +000074
75 NetState = NETLOOP_SUCCESS;
76}
77
78void
79SntpStart (void)
80{
Robin Getz9e0a4d62009-07-23 03:01:03 -040081 debug("%s\n", __func__);
wdenkb4ad9622005-04-01 00:25:43 +000082
Bartlomiej Sieka56668462008-10-01 15:26:28 +020083 NetSetTimeout (SNTP_TIMEOUT, SntpTimeout);
wdenkb4ad9622005-04-01 00:25:43 +000084 NetSetHandler(SntpHandler);
85 memset (NetServerEther, 0, 6);
86
87 SntpSend ();
88}