blob: 6422eef72ef2b694177115a7634364b1fd05b34b [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
Joe Hershberger4af0b772015-04-08 01:41:12 -050017static int sntp_our_port;
wdenkb4ad9622005-04-01 00:25:43 +000018
Joe Hershberger4af0b772015-04-08 01:41:12 -050019static void sntp_send(void)
wdenkb4ad9622005-04-01 00:25:43 +000020{
21 struct sntp_pkt_t pkt;
22 int pktlen = SNTP_PACKET_LEN;
23 int sport;
24
Robin Getz9e0a4d62009-07-23 03:01:03 -040025 debug("%s\n", __func__);
wdenkb4ad9622005-04-01 00:25:43 +000026
Joe Hershberger840fc802012-05-15 08:59:11 +000027 memset(&pkt, 0, sizeof(pkt));
wdenkb4ad9622005-04-01 00:25:43 +000028
29 pkt.li = NTP_LI_NOLEAP;
30 pkt.vn = NTP_VERSION;
31 pkt.mode = NTP_MODE_CLIENT;
32
Joe Hershbergera8ca4f62015-04-08 01:41:05 -050033 memcpy((char *)net_tx_packet + net_eth_hdr_size() + IP_UDP_HDR_SIZE,
34 (char *)&pkt, pktlen);
wdenkb4ad9622005-04-01 00:25:43 +000035
Joe Hershberger4af0b772015-04-08 01:41:12 -050036 sntp_our_port = 10000 + (get_timer(0) % 4096);
wdenkb4ad9622005-04-01 00:25:43 +000037 sport = NTP_SERVICE_PORT;
38
Joe Hershbergera8ca4f62015-04-08 01:41:05 -050039 net_send_udp_packet(net_server_ethaddr, net_ntp_server, sport,
Joe Hershberger4af0b772015-04-08 01:41:12 -050040 sntp_our_port, pktlen);
wdenkb4ad9622005-04-01 00:25:43 +000041}
42
Joe Hershberger4af0b772015-04-08 01:41:12 -050043static void sntp_timeout_handler(void)
wdenkb4ad9622005-04-01 00:25:43 +000044{
Joe Hershberger840fc802012-05-15 08:59:11 +000045 puts("Timeout\n");
Joe Hershbergerd4bb76a2012-05-23 07:59:14 +000046 net_set_state(NETLOOP_FAIL);
wdenkb4ad9622005-04-01 00:25:43 +000047 return;
48}
49
Joe Hershberger5874dec2015-04-08 01:41:01 -050050static void sntp_handler(uchar *pkt, unsigned dest, struct in_addr sip,
51 unsigned src, unsigned len)
wdenkb4ad9622005-04-01 00:25:43 +000052{
Joe Hershberger22498c52015-04-08 01:41:24 -050053#ifdef CONFIG_TIMESTAMP
wdenk8d5d28a2005-04-02 22:37:54 +000054 struct sntp_pkt_t *rpktp = (struct sntp_pkt_t *)pkt;
wdenkb4ad9622005-04-01 00:25:43 +000055 struct rtc_time tm;
wdenk8d5d28a2005-04-02 22:37:54 +000056 ulong seconds;
Joe Hershberger22498c52015-04-08 01:41:24 -050057#endif
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 Hershberger4af0b772015-04-08 01:41:12 -050061 if (dest != sntp_our_port)
Joe Hershberger840fc802012-05-15 08:59:11 +000062 return;
wdenkb4ad9622005-04-01 00:25:43 +000063
Joe Hershberger22498c52015-04-08 01:41:24 -050064#ifdef CONFIG_TIMESTAMP
wdenk8d5d28a2005-04-02 22:37:54 +000065 /*
Joe Hershberger22498c52015-04-08 01:41:24 -050066 * As the RTC's used in U-Boot support second resolution only
wdenk8d5d28a2005-04-02 22:37:54 +000067 * 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
Joe Hershbergerc80b41b02015-04-08 01:41:21 -050071 to_tm(ntohl(seconds) - 2208988800UL + net_ntp_time_offset, &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",
Joe Hershberger4af0b772015-04-08 01:41:12 -050076 tm.tm_year, tm.tm_mon, tm.tm_mday,
77 tm.tm_hour, tm.tm_min, tm.tm_sec);
Joe Hershberger22498c52015-04-08 01:41:24 -050078#endif
wdenkb4ad9622005-04-01 00:25:43 +000079
Joe Hershbergerd4bb76a2012-05-23 07:59:14 +000080 net_set_state(NETLOOP_SUCCESS);
wdenkb4ad9622005-04-01 00:25:43 +000081}
82
Joe Hershberger4af0b772015-04-08 01:41:12 -050083void sntp_start(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 Hershbergerc80b41b02015-04-08 01:41:21 -050087 net_set_timeout_handler(SNTP_TIMEOUT, sntp_timeout_handler);
Joe Hershberger5874dec2015-04-08 01:41:01 -050088 net_set_udp_handler(sntp_handler);
Joe Hershberger8ecdbed2015-04-08 01:41:04 -050089 memset(net_server_ethaddr, 0, sizeof(net_server_ethaddr));
wdenkb4ad9622005-04-01 00:25:43 +000090
Joe Hershberger4af0b772015-04-08 01:41:12 -050091 sntp_send();
wdenkb4ad9622005-04-01 00:25:43 +000092}