blob: dac0f8ceea1b9583a855b8f908f008df52962dc3 [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
Tom Rinid678a592024-05-18 20:20:43 -06008#include <common.h>
wdenkea287de2005-04-01 00:25:43 +00009#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{
wdenk414eec32005-04-02 22:37:54 +000060 struct sntp_pkt_t *rpktp = (struct sntp_pkt_t *)pkt;
wdenkea287de2005-04-01 00:25:43 +000061 struct rtc_time tm;
wdenk414eec32005-04-02 22:37:54 +000062 ulong seconds;
wdenkea287de2005-04-01 00:25:43 +000063
Robin Getz0ebf04c2009-07-23 03:01:03 -040064 debug("%s\n", __func__);
wdenkea287de2005-04-01 00:25:43 +000065
Joe Hershberger38ba2552015-04-08 01:41:12 -050066 if (dest != sntp_our_port)
Joe Hershberger6c3234a2012-05-15 08:59:11 +000067 return;
wdenkea287de2005-04-01 00:25:43 +000068
wdenk414eec32005-04-02 22:37:54 +000069 /*
Joe Hershberger3c56fb82015-04-08 01:41:24 -050070 * As the RTC's used in U-Boot support second resolution only
wdenk414eec32005-04-02 22:37:54 +000071 * we simply ignore the sub-second field.
72 */
Joe Hershberger6c3234a2012-05-15 08:59:11 +000073 memcpy(&seconds, &rpktp->transmit_timestamp, sizeof(ulong));
wdenkea287de2005-04-01 00:25:43 +000074
Simon Glass9f9276c2015-04-20 12:37:18 -060075 rtc_to_tm(ntohl(seconds) - 2208988800UL + net_ntp_time_offset, &tm);
Heinrich Schuchardte1864db2020-10-23 12:58:27 +020076#ifdef CONFIG_DM_RTC
Simon Glass17f0ac62015-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 Schuchardte1864db2020-10-23 12:58:27 +020085#elif defined(CONFIG_CMD_DATE)
Joe Hershberger6c3234a2012-05-15 08:59:11 +000086 rtc_set(&tm);
wdenkea287de2005-04-01 00:25:43 +000087#endif
Joe Hershberger6c3234a2012-05-15 08:59:11 +000088 printf("Date: %4d-%02d-%02d Time: %2d:%02d:%02d\n",
Joe Hershberger38ba2552015-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);
wdenkea287de2005-04-01 00:25:43 +000091
Joe Hershberger22f6e992012-05-23 07:59:14 +000092 net_set_state(NETLOOP_SUCCESS);
wdenkea287de2005-04-01 00:25:43 +000093}
94
Philippe Reynes912ece42020-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)
wdenkea287de2005-04-01 00:25:43 +0000114{
Robin Getz0ebf04c2009-07-23 03:01:03 -0400115 debug("%s\n", __func__);
wdenkea287de2005-04-01 00:25:43 +0000116
Joe Hershbergerbc0571f2015-04-08 01:41:21 -0500117 net_set_timeout_handler(SNTP_TIMEOUT, sntp_timeout_handler);
Joe Hershberger049a95a2015-04-08 01:41:01 -0500118 net_set_udp_handler(sntp_handler);
Joe Hershberger0adb5b72015-04-08 01:41:04 -0500119 memset(net_server_ethaddr, 0, sizeof(net_server_ethaddr));
wdenkea287de2005-04-01 00:25:43 +0000120
Joe Hershberger38ba2552015-04-08 01:41:12 -0500121 sntp_send();
Philippe Reynes912ece42020-09-18 14:13:02 +0200122
123 return 0;
wdenkea287de2005-04-01 00:25:43 +0000124}