blob: d5d5671933180a942278912a8e9cd6e3018c2d78 [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
Philippe Reynes2829d992020-09-18 14:13:02 +020015#include <net/sntp.h>
wdenkb4ad9622005-04-01 00:25:43 +000016
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
Philippe Reynes2829d992020-09-18 14:13:02 +020021/* NTP server IP address */
22struct in_addr net_ntp_server;
23/* offset time from UTC */
24int net_ntp_time_offset;
25
Joe Hershberger4af0b772015-04-08 01:41:12 -050026static void sntp_send(void)
wdenkb4ad9622005-04-01 00:25:43 +000027{
28 struct sntp_pkt_t pkt;
29 int pktlen = SNTP_PACKET_LEN;
30 int sport;
31
Robin Getz9e0a4d62009-07-23 03:01:03 -040032 debug("%s\n", __func__);
wdenkb4ad9622005-04-01 00:25:43 +000033
Joe Hershberger840fc802012-05-15 08:59:11 +000034 memset(&pkt, 0, sizeof(pkt));
wdenkb4ad9622005-04-01 00:25:43 +000035
36 pkt.li = NTP_LI_NOLEAP;
37 pkt.vn = NTP_VERSION;
38 pkt.mode = NTP_MODE_CLIENT;
39
Joe Hershbergera8ca4f62015-04-08 01:41:05 -050040 memcpy((char *)net_tx_packet + net_eth_hdr_size() + IP_UDP_HDR_SIZE,
41 (char *)&pkt, pktlen);
wdenkb4ad9622005-04-01 00:25:43 +000042
Joe Hershberger4af0b772015-04-08 01:41:12 -050043 sntp_our_port = 10000 + (get_timer(0) % 4096);
wdenkb4ad9622005-04-01 00:25:43 +000044 sport = NTP_SERVICE_PORT;
45
Joe Hershbergera8ca4f62015-04-08 01:41:05 -050046 net_send_udp_packet(net_server_ethaddr, net_ntp_server, sport,
Joe Hershberger4af0b772015-04-08 01:41:12 -050047 sntp_our_port, pktlen);
wdenkb4ad9622005-04-01 00:25:43 +000048}
49
Joe Hershberger4af0b772015-04-08 01:41:12 -050050static void sntp_timeout_handler(void)
wdenkb4ad9622005-04-01 00:25:43 +000051{
Joe Hershberger840fc802012-05-15 08:59:11 +000052 puts("Timeout\n");
Joe Hershbergerd4bb76a2012-05-23 07:59:14 +000053 net_set_state(NETLOOP_FAIL);
wdenkb4ad9622005-04-01 00:25:43 +000054 return;
55}
56
Joe Hershberger5874dec2015-04-08 01:41:01 -050057static void sntp_handler(uchar *pkt, unsigned dest, struct in_addr sip,
58 unsigned src, unsigned len)
wdenkb4ad9622005-04-01 00:25:43 +000059{
Joe Hershberger22498c52015-04-08 01:41:24 -050060#ifdef CONFIG_TIMESTAMP
wdenk8d5d28a2005-04-02 22:37:54 +000061 struct sntp_pkt_t *rpktp = (struct sntp_pkt_t *)pkt;
wdenkb4ad9622005-04-01 00:25:43 +000062 struct rtc_time tm;
wdenk8d5d28a2005-04-02 22:37:54 +000063 ulong seconds;
Joe Hershberger22498c52015-04-08 01:41:24 -050064#endif
wdenkb4ad9622005-04-01 00:25:43 +000065
Robin Getz9e0a4d62009-07-23 03:01:03 -040066 debug("%s\n", __func__);
wdenkb4ad9622005-04-01 00:25:43 +000067
Joe Hershberger4af0b772015-04-08 01:41:12 -050068 if (dest != sntp_our_port)
Joe Hershberger840fc802012-05-15 08:59:11 +000069 return;
wdenkb4ad9622005-04-01 00:25:43 +000070
Joe Hershberger22498c52015-04-08 01:41:24 -050071#ifdef CONFIG_TIMESTAMP
wdenk8d5d28a2005-04-02 22:37:54 +000072 /*
Joe Hershberger22498c52015-04-08 01:41:24 -050073 * As the RTC's used in U-Boot support second resolution only
wdenk8d5d28a2005-04-02 22:37:54 +000074 * we simply ignore the sub-second field.
75 */
Joe Hershberger840fc802012-05-15 08:59:11 +000076 memcpy(&seconds, &rpktp->transmit_timestamp, sizeof(ulong));
wdenkb4ad9622005-04-01 00:25:43 +000077
Simon Glass12994662015-04-20 12:37:18 -060078 rtc_to_tm(ntohl(seconds) - 2208988800UL + net_ntp_time_offset, &tm);
Jon Loeliger54f35c22007-07-09 17:45:14 -050079#if defined(CONFIG_CMD_DATE)
Simon Glass1aa11262015-04-20 12:37:27 -060080# ifdef CONFIG_DM_RTC
81 struct udevice *dev;
82 int ret;
83
84 ret = uclass_get_device(UCLASS_RTC, 0, &dev);
85 if (ret)
86 printf("SNTP: cannot find RTC: err=%d\n", ret);
87 else
88 dm_rtc_set(dev, &tm);
89# else
Joe Hershberger840fc802012-05-15 08:59:11 +000090 rtc_set(&tm);
Simon Glass1aa11262015-04-20 12:37:27 -060091# endif
wdenkb4ad9622005-04-01 00:25:43 +000092#endif
Joe Hershberger840fc802012-05-15 08:59:11 +000093 printf("Date: %4d-%02d-%02d Time: %2d:%02d:%02d\n",
Joe Hershberger4af0b772015-04-08 01:41:12 -050094 tm.tm_year, tm.tm_mon, tm.tm_mday,
95 tm.tm_hour, tm.tm_min, tm.tm_sec);
Joe Hershberger22498c52015-04-08 01:41:24 -050096#endif
wdenkb4ad9622005-04-01 00:25:43 +000097
Joe Hershbergerd4bb76a2012-05-23 07:59:14 +000098 net_set_state(NETLOOP_SUCCESS);
wdenkb4ad9622005-04-01 00:25:43 +000099}
100
Philippe Reynes2829d992020-09-18 14:13:02 +0200101/*
102 * SNTP:
103 *
104 * Prerequisites: - own ethernet address
105 * - own IP address
106 * We want: - network time
107 * Next step: none
108 */
109int sntp_prereq(void *data)
110{
111 if (net_ntp_server.s_addr == 0) {
112 puts("*** ERROR: NTP server address not given\n");
113 return 1;
114 }
115
116 return 0;
117}
118
119int sntp_start(void *data)
wdenkb4ad9622005-04-01 00:25:43 +0000120{
Robin Getz9e0a4d62009-07-23 03:01:03 -0400121 debug("%s\n", __func__);
wdenkb4ad9622005-04-01 00:25:43 +0000122
Joe Hershbergerc80b41b02015-04-08 01:41:21 -0500123 net_set_timeout_handler(SNTP_TIMEOUT, sntp_timeout_handler);
Joe Hershberger5874dec2015-04-08 01:41:01 -0500124 net_set_udp_handler(sntp_handler);
Joe Hershberger8ecdbed2015-04-08 01:41:04 -0500125 memset(net_server_ethaddr, 0, sizeof(net_server_ethaddr));
wdenkb4ad9622005-04-01 00:25:43 +0000126
Joe Hershberger4af0b772015-04-08 01:41:12 -0500127 sntp_send();
Philippe Reynes2829d992020-09-18 14:13:02 +0200128
129 return 0;
wdenkb4ad9622005-04-01 00:25:43 +0000130}