blob: 39d7664a2248942cf890038c9d2f1465743cf210 [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>
Simon Glass0f2af882020-05-10 11:40:05 -060011#include <log.h>
wdenkb4ad9622005-04-01 00:25:43 +000012#include <net.h>
13#include <rtc.h>
14
15#include "sntp.h"
16
Bartlomiej Sieka56668462008-10-01 15:26:28 +020017#define SNTP_TIMEOUT 10000UL
wdenkb4ad9622005-04-01 00:25:43 +000018
Joe Hershberger4af0b772015-04-08 01:41:12 -050019static int sntp_our_port;
wdenkb4ad9622005-04-01 00:25:43 +000020
Joe Hershberger4af0b772015-04-08 01:41:12 -050021static void sntp_send(void)
wdenkb4ad9622005-04-01 00:25:43 +000022{
23 struct sntp_pkt_t pkt;
24 int pktlen = SNTP_PACKET_LEN;
25 int sport;
26
Robin Getz9e0a4d62009-07-23 03:01:03 -040027 debug("%s\n", __func__);
wdenkb4ad9622005-04-01 00:25:43 +000028
Joe Hershberger840fc802012-05-15 08:59:11 +000029 memset(&pkt, 0, sizeof(pkt));
wdenkb4ad9622005-04-01 00:25:43 +000030
31 pkt.li = NTP_LI_NOLEAP;
32 pkt.vn = NTP_VERSION;
33 pkt.mode = NTP_MODE_CLIENT;
34
Joe Hershbergera8ca4f62015-04-08 01:41:05 -050035 memcpy((char *)net_tx_packet + net_eth_hdr_size() + IP_UDP_HDR_SIZE,
36 (char *)&pkt, pktlen);
wdenkb4ad9622005-04-01 00:25:43 +000037
Joe Hershberger4af0b772015-04-08 01:41:12 -050038 sntp_our_port = 10000 + (get_timer(0) % 4096);
wdenkb4ad9622005-04-01 00:25:43 +000039 sport = NTP_SERVICE_PORT;
40
Joe Hershbergera8ca4f62015-04-08 01:41:05 -050041 net_send_udp_packet(net_server_ethaddr, net_ntp_server, sport,
Joe Hershberger4af0b772015-04-08 01:41:12 -050042 sntp_our_port, pktlen);
wdenkb4ad9622005-04-01 00:25:43 +000043}
44
Joe Hershberger4af0b772015-04-08 01:41:12 -050045static void sntp_timeout_handler(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{
Joe Hershberger22498c52015-04-08 01:41:24 -050055#ifdef CONFIG_TIMESTAMP
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;
Joe Hershberger22498c52015-04-08 01:41:24 -050059#endif
wdenkb4ad9622005-04-01 00:25:43 +000060
Robin Getz9e0a4d62009-07-23 03:01:03 -040061 debug("%s\n", __func__);
wdenkb4ad9622005-04-01 00:25:43 +000062
Joe Hershberger4af0b772015-04-08 01:41:12 -050063 if (dest != sntp_our_port)
Joe Hershberger840fc802012-05-15 08:59:11 +000064 return;
wdenkb4ad9622005-04-01 00:25:43 +000065
Joe Hershberger22498c52015-04-08 01:41:24 -050066#ifdef CONFIG_TIMESTAMP
wdenk8d5d28a2005-04-02 22:37:54 +000067 /*
Joe Hershberger22498c52015-04-08 01:41:24 -050068 * As the RTC's used in U-Boot support second resolution only
wdenk8d5d28a2005-04-02 22:37:54 +000069 * we simply ignore the sub-second field.
70 */
Joe Hershberger840fc802012-05-15 08:59:11 +000071 memcpy(&seconds, &rpktp->transmit_timestamp, sizeof(ulong));
wdenkb4ad9622005-04-01 00:25:43 +000072
Simon Glass12994662015-04-20 12:37:18 -060073 rtc_to_tm(ntohl(seconds) - 2208988800UL + net_ntp_time_offset, &tm);
Jon Loeliger54f35c22007-07-09 17:45:14 -050074#if defined(CONFIG_CMD_DATE)
Simon Glass1aa11262015-04-20 12:37:27 -060075# ifdef CONFIG_DM_RTC
76 struct udevice *dev;
77 int ret;
78
79 ret = uclass_get_device(UCLASS_RTC, 0, &dev);
80 if (ret)
81 printf("SNTP: cannot find RTC: err=%d\n", ret);
82 else
83 dm_rtc_set(dev, &tm);
84# else
Joe Hershberger840fc802012-05-15 08:59:11 +000085 rtc_set(&tm);
Simon Glass1aa11262015-04-20 12:37:27 -060086# endif
wdenkb4ad9622005-04-01 00:25:43 +000087#endif
Joe Hershberger840fc802012-05-15 08:59:11 +000088 printf("Date: %4d-%02d-%02d Time: %2d:%02d:%02d\n",
Joe Hershberger4af0b772015-04-08 01:41:12 -050089 tm.tm_year, tm.tm_mon, tm.tm_mday,
90 tm.tm_hour, tm.tm_min, tm.tm_sec);
Joe Hershberger22498c52015-04-08 01:41:24 -050091#endif
wdenkb4ad9622005-04-01 00:25:43 +000092
Joe Hershbergerd4bb76a2012-05-23 07:59:14 +000093 net_set_state(NETLOOP_SUCCESS);
wdenkb4ad9622005-04-01 00:25:43 +000094}
95
Joe Hershberger4af0b772015-04-08 01:41:12 -050096void sntp_start(void)
wdenkb4ad9622005-04-01 00:25:43 +000097{
Robin Getz9e0a4d62009-07-23 03:01:03 -040098 debug("%s\n", __func__);
wdenkb4ad9622005-04-01 00:25:43 +000099
Joe Hershbergerc80b41b02015-04-08 01:41:21 -0500100 net_set_timeout_handler(SNTP_TIMEOUT, sntp_timeout_handler);
Joe Hershberger5874dec2015-04-08 01:41:01 -0500101 net_set_udp_handler(sntp_handler);
Joe Hershberger8ecdbed2015-04-08 01:41:04 -0500102 memset(net_server_ethaddr, 0, sizeof(net_server_ethaddr));
wdenkb4ad9622005-04-01 00:25:43 +0000103
Joe Hershberger4af0b772015-04-08 01:41:12 -0500104 sntp_send();
wdenkb4ad9622005-04-01 00:25:43 +0000105}