blob: dac0f8ceea1b9583a855b8f908f008df52962dc3 [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
Tom Riniabb9a042024-05-18 20:20:43 -06008#include <common.h>
wdenkb4ad9622005-04-01 00:25:43 +00009#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{
wdenk8d5d28a2005-04-02 22:37:54 +000060 struct sntp_pkt_t *rpktp = (struct sntp_pkt_t *)pkt;
wdenkb4ad9622005-04-01 00:25:43 +000061 struct rtc_time tm;
wdenk8d5d28a2005-04-02 22:37:54 +000062 ulong seconds;
wdenkb4ad9622005-04-01 00:25:43 +000063
Robin Getz9e0a4d62009-07-23 03:01:03 -040064 debug("%s\n", __func__);
wdenkb4ad9622005-04-01 00:25:43 +000065
Joe Hershberger4af0b772015-04-08 01:41:12 -050066 if (dest != sntp_our_port)
Joe Hershberger840fc802012-05-15 08:59:11 +000067 return;
wdenkb4ad9622005-04-01 00:25:43 +000068
wdenk8d5d28a2005-04-02 22:37:54 +000069 /*
Joe Hershberger22498c52015-04-08 01:41:24 -050070 * As the RTC's used in U-Boot support second resolution only
wdenk8d5d28a2005-04-02 22:37:54 +000071 * we simply ignore the sub-second field.
72 */
Joe Hershberger840fc802012-05-15 08:59:11 +000073 memcpy(&seconds, &rpktp->transmit_timestamp, sizeof(ulong));
wdenkb4ad9622005-04-01 00:25:43 +000074
Simon Glass12994662015-04-20 12:37:18 -060075 rtc_to_tm(ntohl(seconds) - 2208988800UL + net_ntp_time_offset, &tm);
Heinrich Schuchardt6875dec2020-10-23 12:58:27 +020076#ifdef CONFIG_DM_RTC
Simon Glass1aa11262015-04-20 12:37:27 -060077 struct udevice *dev;
78 int ret;
79
80 ret = uclass_get_device(UCLASS_RTC, 0, &dev);
81 if (ret)
82 printf("SNTP: cannot find RTC: err=%d\n", ret);
83 else
84 dm_rtc_set(dev, &tm);
Heinrich Schuchardt6875dec2020-10-23 12:58:27 +020085#elif defined(CONFIG_CMD_DATE)
Joe Hershberger840fc802012-05-15 08:59:11 +000086 rtc_set(&tm);
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);
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
Philippe Reynes2829d992020-09-18 14:13:02 +020095/*
96 * SNTP:
97 *
98 * Prerequisites: - own ethernet address
99 * - own IP address
100 * We want: - network time
101 * Next step: none
102 */
103int sntp_prereq(void *data)
104{
105 if (net_ntp_server.s_addr == 0) {
106 puts("*** ERROR: NTP server address not given\n");
107 return 1;
108 }
109
110 return 0;
111}
112
113int sntp_start(void *data)
wdenkb4ad9622005-04-01 00:25:43 +0000114{
Robin Getz9e0a4d62009-07-23 03:01:03 -0400115 debug("%s\n", __func__);
wdenkb4ad9622005-04-01 00:25:43 +0000116
Joe Hershbergerc80b41b02015-04-08 01:41:21 -0500117 net_set_timeout_handler(SNTP_TIMEOUT, sntp_timeout_handler);
Joe Hershberger5874dec2015-04-08 01:41:01 -0500118 net_set_udp_handler(sntp_handler);
Joe Hershberger8ecdbed2015-04-08 01:41:04 -0500119 memset(net_server_ethaddr, 0, sizeof(net_server_ethaddr));
wdenkb4ad9622005-04-01 00:25:43 +0000120
Joe Hershberger4af0b772015-04-08 01:41:12 -0500121 sntp_send();
Philippe Reynes2829d992020-09-18 14:13:02 +0200122
123 return 0;
wdenkb4ad9622005-04-01 00:25:43 +0000124}