blob: 5de19526e6394b213cd2204d594026d6201a5175 [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 Hershberger594c26f2012-05-23 07:58:04 +000034 memcpy((char *)NetTxPacket + NetEthHdrSize() + IP_UDP_HDR_SIZE,
Joe Hershberger6c3234a2012-05-15 08:59:11 +000035 (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");
Joe Hershberger22f6e992012-05-23 07:59:14 +000048 net_set_state(NETLOOP_FAIL);
wdenkea287de2005-04-01 00:25:43 +000049 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
Joe Hershberger22f6e992012-05-23 07:59:14 +000079 net_set_state(NETLOOP_SUCCESS);
wdenkea287de2005-04-01 00:25:43 +000080}
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);
Joe Hershbergerece223b2012-05-23 07:59:15 +000088 net_set_udp_handler(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}