blob: d5d5671933180a942278912a8e9cd6e3018c2d78 [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>
Simon Glass17f0ac62015-04-20 12:37:27 -060010#include <dm.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060011#include <log.h>
wdenkea287de2005-04-01 00:25:43 +000012#include <net.h>
13#include <rtc.h>
14
Philippe Reynes912ece42020-09-18 14:13:02 +020015#include <net/sntp.h>
wdenkea287de2005-04-01 00:25:43 +000016
Bartlomiej Sieka49f3bdb2008-10-01 15:26:28 +020017#define SNTP_TIMEOUT 10000UL
wdenkea287de2005-04-01 00:25:43 +000018
Joe Hershberger38ba2552015-04-08 01:41:12 -050019static int sntp_our_port;
wdenkea287de2005-04-01 00:25:43 +000020
Philippe Reynes912ece42020-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 Hershberger38ba2552015-04-08 01:41:12 -050026static void sntp_send(void)
wdenkea287de2005-04-01 00:25:43 +000027{
28 struct sntp_pkt_t pkt;
29 int pktlen = SNTP_PACKET_LEN;
30 int sport;
31
Robin Getz0ebf04c2009-07-23 03:01:03 -040032 debug("%s\n", __func__);
wdenkea287de2005-04-01 00:25:43 +000033
Joe Hershberger6c3234a2012-05-15 08:59:11 +000034 memset(&pkt, 0, sizeof(pkt));
wdenkea287de2005-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 Hershberger1203fcc2015-04-08 01:41:05 -050040 memcpy((char *)net_tx_packet + net_eth_hdr_size() + IP_UDP_HDR_SIZE,
41 (char *)&pkt, pktlen);
wdenkea287de2005-04-01 00:25:43 +000042
Joe Hershberger38ba2552015-04-08 01:41:12 -050043 sntp_our_port = 10000 + (get_timer(0) % 4096);
wdenkea287de2005-04-01 00:25:43 +000044 sport = NTP_SERVICE_PORT;
45
Joe Hershberger1203fcc2015-04-08 01:41:05 -050046 net_send_udp_packet(net_server_ethaddr, net_ntp_server, sport,
Joe Hershberger38ba2552015-04-08 01:41:12 -050047 sntp_our_port, pktlen);
wdenkea287de2005-04-01 00:25:43 +000048}
49
Joe Hershberger38ba2552015-04-08 01:41:12 -050050static void sntp_timeout_handler(void)
wdenkea287de2005-04-01 00:25:43 +000051{
Joe Hershberger6c3234a2012-05-15 08:59:11 +000052 puts("Timeout\n");
Joe Hershberger22f6e992012-05-23 07:59:14 +000053 net_set_state(NETLOOP_FAIL);
wdenkea287de2005-04-01 00:25:43 +000054 return;
55}
56
Joe Hershberger049a95a2015-04-08 01:41:01 -050057static void sntp_handler(uchar *pkt, unsigned dest, struct in_addr sip,
58 unsigned src, unsigned len)
wdenkea287de2005-04-01 00:25:43 +000059{
Joe Hershberger3c56fb82015-04-08 01:41:24 -050060#ifdef CONFIG_TIMESTAMP
wdenk414eec32005-04-02 22:37:54 +000061 struct sntp_pkt_t *rpktp = (struct sntp_pkt_t *)pkt;
wdenkea287de2005-04-01 00:25:43 +000062 struct rtc_time tm;
wdenk414eec32005-04-02 22:37:54 +000063 ulong seconds;
Joe Hershberger3c56fb82015-04-08 01:41:24 -050064#endif
wdenkea287de2005-04-01 00:25:43 +000065
Robin Getz0ebf04c2009-07-23 03:01:03 -040066 debug("%s\n", __func__);
wdenkea287de2005-04-01 00:25:43 +000067
Joe Hershberger38ba2552015-04-08 01:41:12 -050068 if (dest != sntp_our_port)
Joe Hershberger6c3234a2012-05-15 08:59:11 +000069 return;
wdenkea287de2005-04-01 00:25:43 +000070
Joe Hershberger3c56fb82015-04-08 01:41:24 -050071#ifdef CONFIG_TIMESTAMP
wdenk414eec32005-04-02 22:37:54 +000072 /*
Joe Hershberger3c56fb82015-04-08 01:41:24 -050073 * As the RTC's used in U-Boot support second resolution only
wdenk414eec32005-04-02 22:37:54 +000074 * we simply ignore the sub-second field.
75 */
Joe Hershberger6c3234a2012-05-15 08:59:11 +000076 memcpy(&seconds, &rpktp->transmit_timestamp, sizeof(ulong));
wdenkea287de2005-04-01 00:25:43 +000077
Simon Glass9f9276c2015-04-20 12:37:18 -060078 rtc_to_tm(ntohl(seconds) - 2208988800UL + net_ntp_time_offset, &tm);
Jon Loeliger643d1ab2007-07-09 17:45:14 -050079#if defined(CONFIG_CMD_DATE)
Simon Glass17f0ac62015-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 Hershberger6c3234a2012-05-15 08:59:11 +000090 rtc_set(&tm);
Simon Glass17f0ac62015-04-20 12:37:27 -060091# endif
wdenkea287de2005-04-01 00:25:43 +000092#endif
Joe Hershberger6c3234a2012-05-15 08:59:11 +000093 printf("Date: %4d-%02d-%02d Time: %2d:%02d:%02d\n",
Joe Hershberger38ba2552015-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 Hershberger3c56fb82015-04-08 01:41:24 -050096#endif
wdenkea287de2005-04-01 00:25:43 +000097
Joe Hershberger22f6e992012-05-23 07:59:14 +000098 net_set_state(NETLOOP_SUCCESS);
wdenkea287de2005-04-01 00:25:43 +000099}
100
Philippe Reynes912ece42020-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)
wdenkea287de2005-04-01 00:25:43 +0000120{
Robin Getz0ebf04c2009-07-23 03:01:03 -0400121 debug("%s\n", __func__);
wdenkea287de2005-04-01 00:25:43 +0000122
Joe Hershbergerbc0571f2015-04-08 01:41:21 -0500123 net_set_timeout_handler(SNTP_TIMEOUT, sntp_timeout_handler);
Joe Hershberger049a95a2015-04-08 01:41:01 -0500124 net_set_udp_handler(sntp_handler);
Joe Hershberger0adb5b72015-04-08 01:41:04 -0500125 memset(net_server_ethaddr, 0, sizeof(net_server_ethaddr));
wdenkea287de2005-04-01 00:25:43 +0000126
Joe Hershberger38ba2552015-04-08 01:41:12 -0500127 sntp_send();
Philippe Reynes912ece42020-09-18 14:13:02 +0200128
129 return 0;
wdenkea287de2005-04-01 00:25:43 +0000130}