blob: 1e2b67840f36b9ff828f9d02c280eab01ac7244d [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 Hershbergera8ca4f62015-04-08 01:41:05 -050034 memcpy((char *)net_tx_packet + net_eth_hdr_size() + IP_UDP_HDR_SIZE,
35 (char *)&pkt, pktlen);
wdenkb4ad9622005-04-01 00:25:43 +000036
37 SntpOurPort = 10000 + (get_timer(0) % 4096);
38 sport = NTP_SERVICE_PORT;
39
Joe Hershbergera8ca4f62015-04-08 01:41:05 -050040 net_send_udp_packet(net_server_ethaddr, net_ntp_server, sport,
41 SntpOurPort, 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
Joe Hershberger5874dec2015-04-08 01:41:01 -050052static void sntp_handler(uchar *pkt, unsigned dest, struct in_addr sip,
53 unsigned src, unsigned len)
wdenkb4ad9622005-04-01 00:25:43 +000054{
wdenk8d5d28a2005-04-02 22:37:54 +000055 struct sntp_pkt_t *rpktp = (struct sntp_pkt_t *)pkt;
wdenkb4ad9622005-04-01 00:25:43 +000056 struct rtc_time tm;
wdenk8d5d28a2005-04-02 22:37:54 +000057 ulong seconds;
wdenkb4ad9622005-04-01 00:25:43 +000058
Robin Getz9e0a4d62009-07-23 03:01:03 -040059 debug("%s\n", __func__);
wdenkb4ad9622005-04-01 00:25:43 +000060
Joe Hershberger840fc802012-05-15 08:59:11 +000061 if (dest != SntpOurPort)
62 return;
wdenkb4ad9622005-04-01 00:25:43 +000063
wdenk8d5d28a2005-04-02 22:37:54 +000064 /*
65 * As the RTC's used in U-Boot sepport second resolution only
66 * we simply ignore the sub-second field.
67 */
Joe Hershberger840fc802012-05-15 08:59:11 +000068 memcpy(&seconds, &rpktp->transmit_timestamp, sizeof(ulong));
wdenkb4ad9622005-04-01 00:25:43 +000069
wdenk8d5d28a2005-04-02 22:37:54 +000070 to_tm(ntohl(seconds) - 2208988800UL + NetTimeOffset, &tm);
Jon Loeliger54f35c22007-07-09 17:45:14 -050071#if defined(CONFIG_CMD_DATE)
Joe Hershberger840fc802012-05-15 08:59:11 +000072 rtc_set(&tm);
wdenkb4ad9622005-04-01 00:25:43 +000073#endif
Joe Hershberger840fc802012-05-15 08:59:11 +000074 printf("Date: %4d-%02d-%02d Time: %2d:%02d:%02d\n",
wdenkb4ad9622005-04-01 00:25:43 +000075 tm.tm_year, tm.tm_mon, tm.tm_mday,
76 tm.tm_hour, tm.tm_min, tm.tm_sec);
wdenkb4ad9622005-04-01 00:25:43 +000077
Joe Hershbergerd4bb76a2012-05-23 07:59:14 +000078 net_set_state(NETLOOP_SUCCESS);
wdenkb4ad9622005-04-01 00:25:43 +000079}
80
81void
Joe Hershberger840fc802012-05-15 08:59:11 +000082SntpStart(void)
wdenkb4ad9622005-04-01 00:25:43 +000083{
Robin Getz9e0a4d62009-07-23 03:01:03 -040084 debug("%s\n", __func__);
wdenkb4ad9622005-04-01 00:25:43 +000085
Joe Hershberger840fc802012-05-15 08:59:11 +000086 NetSetTimeout(SNTP_TIMEOUT, SntpTimeout);
Joe Hershberger5874dec2015-04-08 01:41:01 -050087 net_set_udp_handler(sntp_handler);
Joe Hershberger8ecdbed2015-04-08 01:41:04 -050088 memset(net_server_ethaddr, 0, sizeof(net_server_ethaddr));
wdenkb4ad9622005-04-01 00:25:43 +000089
Joe Hershberger840fc802012-05-15 08:59:11 +000090 SntpSend();
wdenkb4ad9622005-04-01 00:25:43 +000091}