blob: 5de19526e6394b213cd2204d594026d6201a5175 [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
Joe Hershberger840fc802012-05-15 08:59:11 +000020SntpSend(void)
wdenkb4ad9622005-04-01 00:25:43 +000021{
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
Joe Hershberger840fc802012-05-15 08:59:11 +000028 memset(&pkt, 0, sizeof(pkt));
wdenkb4ad9622005-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 Hershberger6fe8b452012-05-23 07:58:04 +000034 memcpy((char *)NetTxPacket + NetEthHdrSize() + IP_UDP_HDR_SIZE,
Joe Hershberger840fc802012-05-15 08:59:11 +000035 (char *)&pkt, pktlen);
wdenkb4ad9622005-04-01 00:25:43 +000036
37 SntpOurPort = 10000 + (get_timer(0) % 4096);
38 sport = NTP_SERVICE_PORT;
39
Joe Hershberger840fc802012-05-15 08:59:11 +000040 NetSendUDPPacket(NetServerEther, NetNtpServerIP, sport, SntpOurPort,
41 pktlen);
wdenkb4ad9622005-04-01 00:25:43 +000042}
43
44static void
Joe Hershberger840fc802012-05-15 08:59:11 +000045SntpTimeout(void)
wdenkb4ad9622005-04-01 00:25:43 +000046{
Joe Hershberger840fc802012-05-15 08:59:11 +000047 puts("Timeout\n");
Joe Hershbergerd4bb76a2012-05-23 07:59:14 +000048 net_set_state(NETLOOP_FAIL);
wdenkb4ad9622005-04-01 00:25:43 +000049 return;
50}
51
52static void
Luca Ceresoli428ab362011-04-18 06:19:50 +000053SntpHandler(uchar *pkt, unsigned dest, IPaddr_t sip, unsigned src,
54 unsigned len)
wdenkb4ad9622005-04-01 00:25:43 +000055{
wdenk8d5d28a2005-04-02 22:37:54 +000056 struct sntp_pkt_t *rpktp = (struct sntp_pkt_t *)pkt;
wdenkb4ad9622005-04-01 00:25:43 +000057 struct rtc_time tm;
wdenk8d5d28a2005-04-02 22:37:54 +000058 ulong seconds;
wdenkb4ad9622005-04-01 00:25:43 +000059
Robin Getz9e0a4d62009-07-23 03:01:03 -040060 debug("%s\n", __func__);
wdenkb4ad9622005-04-01 00:25:43 +000061
Joe Hershberger840fc802012-05-15 08:59:11 +000062 if (dest != SntpOurPort)
63 return;
wdenkb4ad9622005-04-01 00:25:43 +000064
wdenk8d5d28a2005-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 Hershberger840fc802012-05-15 08:59:11 +000069 memcpy(&seconds, &rpktp->transmit_timestamp, sizeof(ulong));
wdenkb4ad9622005-04-01 00:25:43 +000070
wdenk8d5d28a2005-04-02 22:37:54 +000071 to_tm(ntohl(seconds) - 2208988800UL + NetTimeOffset, &tm);
Jon Loeliger54f35c22007-07-09 17:45:14 -050072#if defined(CONFIG_CMD_DATE)
Joe Hershberger840fc802012-05-15 08:59:11 +000073 rtc_set(&tm);
wdenkb4ad9622005-04-01 00:25:43 +000074#endif
Joe Hershberger840fc802012-05-15 08:59:11 +000075 printf("Date: %4d-%02d-%02d Time: %2d:%02d:%02d\n",
wdenkb4ad9622005-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);
wdenkb4ad9622005-04-01 00:25:43 +000078
Joe Hershbergerd4bb76a2012-05-23 07:59:14 +000079 net_set_state(NETLOOP_SUCCESS);
wdenkb4ad9622005-04-01 00:25:43 +000080}
81
82void
Joe Hershberger840fc802012-05-15 08:59:11 +000083SntpStart(void)
wdenkb4ad9622005-04-01 00:25:43 +000084{
Robin Getz9e0a4d62009-07-23 03:01:03 -040085 debug("%s\n", __func__);
wdenkb4ad9622005-04-01 00:25:43 +000086
Joe Hershberger840fc802012-05-15 08:59:11 +000087 NetSetTimeout(SNTP_TIMEOUT, SntpTimeout);
Joe Hershbergerf50357b2012-05-23 07:59:15 +000088 net_set_udp_handler(SntpHandler);
Joe Hershberger840fc802012-05-15 08:59:11 +000089 memset(NetServerEther, 0, sizeof(NetServerEther));
wdenkb4ad9622005-04-01 00:25:43 +000090
Joe Hershberger840fc802012-05-15 08:59:11 +000091 SntpSend();
wdenkb4ad9622005-04-01 00:25:43 +000092}