blob: 39d7664a2248942cf890038c9d2f1465743cf210 [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
15#include "sntp.h"
16
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
Joe Hershberger38ba2552015-04-08 01:41:12 -050021static void sntp_send(void)
wdenkea287de2005-04-01 00:25:43 +000022{
23 struct sntp_pkt_t pkt;
24 int pktlen = SNTP_PACKET_LEN;
25 int sport;
26
Robin Getz0ebf04c2009-07-23 03:01:03 -040027 debug("%s\n", __func__);
wdenkea287de2005-04-01 00:25:43 +000028
Joe Hershberger6c3234a2012-05-15 08:59:11 +000029 memset(&pkt, 0, sizeof(pkt));
wdenkea287de2005-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 Hershberger1203fcc2015-04-08 01:41:05 -050035 memcpy((char *)net_tx_packet + net_eth_hdr_size() + IP_UDP_HDR_SIZE,
36 (char *)&pkt, pktlen);
wdenkea287de2005-04-01 00:25:43 +000037
Joe Hershberger38ba2552015-04-08 01:41:12 -050038 sntp_our_port = 10000 + (get_timer(0) % 4096);
wdenkea287de2005-04-01 00:25:43 +000039 sport = NTP_SERVICE_PORT;
40
Joe Hershberger1203fcc2015-04-08 01:41:05 -050041 net_send_udp_packet(net_server_ethaddr, net_ntp_server, sport,
Joe Hershberger38ba2552015-04-08 01:41:12 -050042 sntp_our_port, pktlen);
wdenkea287de2005-04-01 00:25:43 +000043}
44
Joe Hershberger38ba2552015-04-08 01:41:12 -050045static void sntp_timeout_handler(void)
wdenkea287de2005-04-01 00:25:43 +000046{
Joe Hershberger6c3234a2012-05-15 08:59:11 +000047 puts("Timeout\n");
Joe Hershberger22f6e992012-05-23 07:59:14 +000048 net_set_state(NETLOOP_FAIL);
wdenkea287de2005-04-01 00:25:43 +000049 return;
50}
51
Joe Hershberger049a95a2015-04-08 01:41:01 -050052static void sntp_handler(uchar *pkt, unsigned dest, struct in_addr sip,
53 unsigned src, unsigned len)
wdenkea287de2005-04-01 00:25:43 +000054{
Joe Hershberger3c56fb82015-04-08 01:41:24 -050055#ifdef CONFIG_TIMESTAMP
wdenk414eec32005-04-02 22:37:54 +000056 struct sntp_pkt_t *rpktp = (struct sntp_pkt_t *)pkt;
wdenkea287de2005-04-01 00:25:43 +000057 struct rtc_time tm;
wdenk414eec32005-04-02 22:37:54 +000058 ulong seconds;
Joe Hershberger3c56fb82015-04-08 01:41:24 -050059#endif
wdenkea287de2005-04-01 00:25:43 +000060
Robin Getz0ebf04c2009-07-23 03:01:03 -040061 debug("%s\n", __func__);
wdenkea287de2005-04-01 00:25:43 +000062
Joe Hershberger38ba2552015-04-08 01:41:12 -050063 if (dest != sntp_our_port)
Joe Hershberger6c3234a2012-05-15 08:59:11 +000064 return;
wdenkea287de2005-04-01 00:25:43 +000065
Joe Hershberger3c56fb82015-04-08 01:41:24 -050066#ifdef CONFIG_TIMESTAMP
wdenk414eec32005-04-02 22:37:54 +000067 /*
Joe Hershberger3c56fb82015-04-08 01:41:24 -050068 * As the RTC's used in U-Boot support second resolution only
wdenk414eec32005-04-02 22:37:54 +000069 * we simply ignore the sub-second field.
70 */
Joe Hershberger6c3234a2012-05-15 08:59:11 +000071 memcpy(&seconds, &rpktp->transmit_timestamp, sizeof(ulong));
wdenkea287de2005-04-01 00:25:43 +000072
Simon Glass9f9276c2015-04-20 12:37:18 -060073 rtc_to_tm(ntohl(seconds) - 2208988800UL + net_ntp_time_offset, &tm);
Jon Loeliger643d1ab2007-07-09 17:45:14 -050074#if defined(CONFIG_CMD_DATE)
Simon Glass17f0ac62015-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 Hershberger6c3234a2012-05-15 08:59:11 +000085 rtc_set(&tm);
Simon Glass17f0ac62015-04-20 12:37:27 -060086# endif
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);
Joe Hershberger3c56fb82015-04-08 01:41:24 -050091#endif
wdenkea287de2005-04-01 00:25:43 +000092
Joe Hershberger22f6e992012-05-23 07:59:14 +000093 net_set_state(NETLOOP_SUCCESS);
wdenkea287de2005-04-01 00:25:43 +000094}
95
Joe Hershberger38ba2552015-04-08 01:41:12 -050096void sntp_start(void)
wdenkea287de2005-04-01 00:25:43 +000097{
Robin Getz0ebf04c2009-07-23 03:01:03 -040098 debug("%s\n", __func__);
wdenkea287de2005-04-01 00:25:43 +000099
Joe Hershbergerbc0571f2015-04-08 01:41:21 -0500100 net_set_timeout_handler(SNTP_TIMEOUT, sntp_timeout_handler);
Joe Hershberger049a95a2015-04-08 01:41:01 -0500101 net_set_udp_handler(sntp_handler);
Joe Hershberger0adb5b72015-04-08 01:41:04 -0500102 memset(net_server_ethaddr, 0, sizeof(net_server_ethaddr));
wdenkea287de2005-04-01 00:25:43 +0000103
Joe Hershberger38ba2552015-04-08 01:41:12 -0500104 sntp_send();
wdenkea287de2005-04-01 00:25:43 +0000105}