blob: 9c8ee34a4ed7838ced1fa01eda84dcc1b9735bcf [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>
Simon Glass1aa11262015-04-20 12:37:27 -060010#include <dm.h>
wdenkb4ad9622005-04-01 00:25:43 +000011#include <net.h>
12#include <rtc.h>
13
14#include "sntp.h"
15
Bartlomiej Sieka56668462008-10-01 15:26:28 +020016#define SNTP_TIMEOUT 10000UL
wdenkb4ad9622005-04-01 00:25:43 +000017
Joe Hershberger4af0b772015-04-08 01:41:12 -050018static int sntp_our_port;
wdenkb4ad9622005-04-01 00:25:43 +000019
Joe Hershberger4af0b772015-04-08 01:41:12 -050020static void sntp_send(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
Joe Hershberger4af0b772015-04-08 01:41:12 -050037 sntp_our_port = 10000 + (get_timer(0) % 4096);
wdenkb4ad9622005-04-01 00:25:43 +000038 sport = NTP_SERVICE_PORT;
39
Joe Hershbergera8ca4f62015-04-08 01:41:05 -050040 net_send_udp_packet(net_server_ethaddr, net_ntp_server, sport,
Joe Hershberger4af0b772015-04-08 01:41:12 -050041 sntp_our_port, pktlen);
wdenkb4ad9622005-04-01 00:25:43 +000042}
43
Joe Hershberger4af0b772015-04-08 01:41:12 -050044static void sntp_timeout_handler(void)
wdenkb4ad9622005-04-01 00:25:43 +000045{
Joe Hershberger840fc802012-05-15 08:59:11 +000046 puts("Timeout\n");
Joe Hershbergerd4bb76a2012-05-23 07:59:14 +000047 net_set_state(NETLOOP_FAIL);
wdenkb4ad9622005-04-01 00:25:43 +000048 return;
49}
50
Joe Hershberger5874dec2015-04-08 01:41:01 -050051static void sntp_handler(uchar *pkt, unsigned dest, struct in_addr sip,
52 unsigned src, unsigned len)
wdenkb4ad9622005-04-01 00:25:43 +000053{
Joe Hershberger22498c52015-04-08 01:41:24 -050054#ifdef CONFIG_TIMESTAMP
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;
Joe Hershberger22498c52015-04-08 01:41:24 -050058#endif
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 Hershberger4af0b772015-04-08 01:41:12 -050062 if (dest != sntp_our_port)
Joe Hershberger840fc802012-05-15 08:59:11 +000063 return;
wdenkb4ad9622005-04-01 00:25:43 +000064
Joe Hershberger22498c52015-04-08 01:41:24 -050065#ifdef CONFIG_TIMESTAMP
wdenk8d5d28a2005-04-02 22:37:54 +000066 /*
Joe Hershberger22498c52015-04-08 01:41:24 -050067 * As the RTC's used in U-Boot support second resolution only
wdenk8d5d28a2005-04-02 22:37:54 +000068 * we simply ignore the sub-second field.
69 */
Joe Hershberger840fc802012-05-15 08:59:11 +000070 memcpy(&seconds, &rpktp->transmit_timestamp, sizeof(ulong));
wdenkb4ad9622005-04-01 00:25:43 +000071
Simon Glass12994662015-04-20 12:37:18 -060072 rtc_to_tm(ntohl(seconds) - 2208988800UL + net_ntp_time_offset, &tm);
Jon Loeliger54f35c22007-07-09 17:45:14 -050073#if defined(CONFIG_CMD_DATE)
Simon Glass1aa11262015-04-20 12:37:27 -060074# ifdef CONFIG_DM_RTC
75 struct udevice *dev;
76 int ret;
77
78 ret = uclass_get_device(UCLASS_RTC, 0, &dev);
79 if (ret)
80 printf("SNTP: cannot find RTC: err=%d\n", ret);
81 else
82 dm_rtc_set(dev, &tm);
83# else
Joe Hershberger840fc802012-05-15 08:59:11 +000084 rtc_set(&tm);
Simon Glass1aa11262015-04-20 12:37:27 -060085# endif
wdenkb4ad9622005-04-01 00:25:43 +000086#endif
Joe Hershberger840fc802012-05-15 08:59:11 +000087 printf("Date: %4d-%02d-%02d Time: %2d:%02d:%02d\n",
Joe Hershberger4af0b772015-04-08 01:41:12 -050088 tm.tm_year, tm.tm_mon, tm.tm_mday,
89 tm.tm_hour, tm.tm_min, tm.tm_sec);
Joe Hershberger22498c52015-04-08 01:41:24 -050090#endif
wdenkb4ad9622005-04-01 00:25:43 +000091
Joe Hershbergerd4bb76a2012-05-23 07:59:14 +000092 net_set_state(NETLOOP_SUCCESS);
wdenkb4ad9622005-04-01 00:25:43 +000093}
94
Joe Hershberger4af0b772015-04-08 01:41:12 -050095void sntp_start(void)
wdenkb4ad9622005-04-01 00:25:43 +000096{
Robin Getz9e0a4d62009-07-23 03:01:03 -040097 debug("%s\n", __func__);
wdenkb4ad9622005-04-01 00:25:43 +000098
Joe Hershbergerc80b41b02015-04-08 01:41:21 -050099 net_set_timeout_handler(SNTP_TIMEOUT, sntp_timeout_handler);
Joe Hershberger5874dec2015-04-08 01:41:01 -0500100 net_set_udp_handler(sntp_handler);
Joe Hershberger8ecdbed2015-04-08 01:41:04 -0500101 memset(net_server_ethaddr, 0, sizeof(net_server_ethaddr));
wdenkb4ad9622005-04-01 00:25:43 +0000102
Joe Hershberger4af0b772015-04-08 01:41:12 -0500103 sntp_send();
wdenkb4ad9622005-04-01 00:25:43 +0000104}