blob: 69cddb16b5c053ae278ade14bf412161eb8016ff [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
Joe Hershberger6c3234a2012-05-15 08:59:11 +000020SntpSend(void)
wdenkea287de2005-04-01 00:25:43 +000021{
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
Joe Hershberger6c3234a2012-05-15 08:59:11 +000028 memset(&pkt, 0, sizeof(pkt));
wdenkea287de2005-04-01 00:25:43 +000029
30 pkt.li = NTP_LI_NOLEAP;
31 pkt.vn = NTP_VERSION;
32 pkt.mode = NTP_MODE_CLIENT;
33
Joe Hershberger6c3234a2012-05-15 08:59:11 +000034 memcpy((char *)NetTxPacket + NetEthHdrSize() + IP_HDR_SIZE,
35 (char *)&pkt, pktlen);
wdenkea287de2005-04-01 00:25:43 +000036
37 SntpOurPort = 10000 + (get_timer(0) % 4096);
38 sport = NTP_SERVICE_PORT;
39
Joe Hershberger6c3234a2012-05-15 08:59:11 +000040 NetSendUDPPacket(NetServerEther, NetNtpServerIP, sport, SntpOurPort,
41 pktlen);
wdenkea287de2005-04-01 00:25:43 +000042}
43
44static void
Joe Hershberger6c3234a2012-05-15 08:59:11 +000045SntpTimeout(void)
wdenkea287de2005-04-01 00:25:43 +000046{
Joe Hershberger6c3234a2012-05-15 08:59:11 +000047 puts("Timeout\n");
wdenkea287de2005-04-01 00:25:43 +000048 NetState = NETLOOP_FAIL;
49 return;
50}
51
52static void
Luca Ceresoli03eb1292011-04-18 06:19:50 +000053SntpHandler(uchar *pkt, unsigned dest, IPaddr_t sip, unsigned src,
54 unsigned len)
wdenkea287de2005-04-01 00:25:43 +000055{
wdenk414eec32005-04-02 22:37:54 +000056 struct sntp_pkt_t *rpktp = (struct sntp_pkt_t *)pkt;
wdenkea287de2005-04-01 00:25:43 +000057 struct rtc_time tm;
wdenk414eec32005-04-02 22:37:54 +000058 ulong seconds;
wdenkea287de2005-04-01 00:25:43 +000059
Robin Getz0ebf04c2009-07-23 03:01:03 -040060 debug("%s\n", __func__);
wdenkea287de2005-04-01 00:25:43 +000061
Joe Hershberger6c3234a2012-05-15 08:59:11 +000062 if (dest != SntpOurPort)
63 return;
wdenkea287de2005-04-01 00:25:43 +000064
wdenk414eec32005-04-02 22:37:54 +000065 /*
66 * As the RTC's used in U-Boot sepport second resolution only
67 * we simply ignore the sub-second field.
68 */
Joe Hershberger6c3234a2012-05-15 08:59:11 +000069 memcpy(&seconds, &rpktp->transmit_timestamp, sizeof(ulong));
wdenkea287de2005-04-01 00:25:43 +000070
wdenk414eec32005-04-02 22:37:54 +000071 to_tm(ntohl(seconds) - 2208988800UL + NetTimeOffset, &tm);
Jon Loeliger643d1ab2007-07-09 17:45:14 -050072#if defined(CONFIG_CMD_DATE)
Joe Hershberger6c3234a2012-05-15 08:59:11 +000073 rtc_set(&tm);
wdenkea287de2005-04-01 00:25:43 +000074#endif
Joe Hershberger6c3234a2012-05-15 08:59:11 +000075 printf("Date: %4d-%02d-%02d Time: %2d:%02d:%02d\n",
wdenkea287de2005-04-01 00:25:43 +000076 tm.tm_year, tm.tm_mon, tm.tm_mday,
77 tm.tm_hour, tm.tm_min, tm.tm_sec);
wdenkea287de2005-04-01 00:25:43 +000078
79 NetState = NETLOOP_SUCCESS;
80}
81
82void
Joe Hershberger6c3234a2012-05-15 08:59:11 +000083SntpStart(void)
wdenkea287de2005-04-01 00:25:43 +000084{
Robin Getz0ebf04c2009-07-23 03:01:03 -040085 debug("%s\n", __func__);
wdenkea287de2005-04-01 00:25:43 +000086
Joe Hershberger6c3234a2012-05-15 08:59:11 +000087 NetSetTimeout(SNTP_TIMEOUT, SntpTimeout);
wdenkea287de2005-04-01 00:25:43 +000088 NetSetHandler(SntpHandler);
Joe Hershberger6c3234a2012-05-15 08:59:11 +000089 memset(NetServerEther, 0, sizeof(NetServerEther));
wdenkea287de2005-04-01 00:25:43 +000090
Joe Hershberger6c3234a2012-05-15 08:59:11 +000091 SntpSend();
wdenkea287de2005-04-01 00:25:43 +000092}