blob: d7b9e5563a7da1fdbfc1d407995d27a3080ee5bf [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
Joe Hershberger38ba2552015-04-08 01:41:12 -050017static int sntp_our_port;
wdenkea287de2005-04-01 00:25:43 +000018
Joe Hershberger38ba2552015-04-08 01:41:12 -050019static void sntp_send(void)
wdenkea287de2005-04-01 00:25:43 +000020{
21 struct sntp_pkt_t pkt;
22 int pktlen = SNTP_PACKET_LEN;
23 int sport;
24
Robin Getz0ebf04c2009-07-23 03:01:03 -040025 debug("%s\n", __func__);
wdenkea287de2005-04-01 00:25:43 +000026
Joe Hershberger6c3234a2012-05-15 08:59:11 +000027 memset(&pkt, 0, sizeof(pkt));
wdenkea287de2005-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 Hershberger1203fcc2015-04-08 01:41:05 -050033 memcpy((char *)net_tx_packet + net_eth_hdr_size() + IP_UDP_HDR_SIZE,
34 (char *)&pkt, pktlen);
wdenkea287de2005-04-01 00:25:43 +000035
Joe Hershberger38ba2552015-04-08 01:41:12 -050036 sntp_our_port = 10000 + (get_timer(0) % 4096);
wdenkea287de2005-04-01 00:25:43 +000037 sport = NTP_SERVICE_PORT;
38
Joe Hershberger1203fcc2015-04-08 01:41:05 -050039 net_send_udp_packet(net_server_ethaddr, net_ntp_server, sport,
Joe Hershberger38ba2552015-04-08 01:41:12 -050040 sntp_our_port, pktlen);
wdenkea287de2005-04-01 00:25:43 +000041}
42
Joe Hershberger38ba2552015-04-08 01:41:12 -050043static void sntp_timeout_handler(void)
wdenkea287de2005-04-01 00:25:43 +000044{
Joe Hershberger6c3234a2012-05-15 08:59:11 +000045 puts("Timeout\n");
Joe Hershberger22f6e992012-05-23 07:59:14 +000046 net_set_state(NETLOOP_FAIL);
wdenkea287de2005-04-01 00:25:43 +000047 return;
48}
49
Joe Hershberger049a95a2015-04-08 01:41:01 -050050static void sntp_handler(uchar *pkt, unsigned dest, struct in_addr sip,
51 unsigned src, unsigned len)
wdenkea287de2005-04-01 00:25:43 +000052{
Joe Hershberger3c56fb82015-04-08 01:41:24 -050053#ifdef CONFIG_TIMESTAMP
wdenk414eec32005-04-02 22:37:54 +000054 struct sntp_pkt_t *rpktp = (struct sntp_pkt_t *)pkt;
wdenkea287de2005-04-01 00:25:43 +000055 struct rtc_time tm;
wdenk414eec32005-04-02 22:37:54 +000056 ulong seconds;
Joe Hershberger3c56fb82015-04-08 01:41:24 -050057#endif
wdenkea287de2005-04-01 00:25:43 +000058
Robin Getz0ebf04c2009-07-23 03:01:03 -040059 debug("%s\n", __func__);
wdenkea287de2005-04-01 00:25:43 +000060
Joe Hershberger38ba2552015-04-08 01:41:12 -050061 if (dest != sntp_our_port)
Joe Hershberger6c3234a2012-05-15 08:59:11 +000062 return;
wdenkea287de2005-04-01 00:25:43 +000063
Joe Hershberger3c56fb82015-04-08 01:41:24 -050064#ifdef CONFIG_TIMESTAMP
wdenk414eec32005-04-02 22:37:54 +000065 /*
Joe Hershberger3c56fb82015-04-08 01:41:24 -050066 * As the RTC's used in U-Boot support second resolution only
wdenk414eec32005-04-02 22:37:54 +000067 * 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
Simon Glass9f9276c2015-04-20 12:37:18 -060071 rtc_to_tm(ntohl(seconds) - 2208988800UL + net_ntp_time_offset, &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",
Joe Hershberger38ba2552015-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 Hershberger3c56fb82015-04-08 01:41:24 -050078#endif
wdenkea287de2005-04-01 00:25:43 +000079
Joe Hershberger22f6e992012-05-23 07:59:14 +000080 net_set_state(NETLOOP_SUCCESS);
wdenkea287de2005-04-01 00:25:43 +000081}
82
Joe Hershberger38ba2552015-04-08 01:41:12 -050083void sntp_start(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 Hershbergerbc0571f2015-04-08 01:41:21 -050087 net_set_timeout_handler(SNTP_TIMEOUT, sntp_timeout_handler);
Joe Hershberger049a95a2015-04-08 01:41:01 -050088 net_set_udp_handler(sntp_handler);
Joe Hershberger0adb5b72015-04-08 01:41:04 -050089 memset(net_server_ethaddr, 0, sizeof(net_server_ethaddr));
wdenkea287de2005-04-01 00:25:43 +000090
Joe Hershberger38ba2552015-04-08 01:41:12 -050091 sntp_send();
wdenkea287de2005-04-01 00:25:43 +000092}