blob: 9c8ee34a4ed7838ced1fa01eda84dcc1b9735bcf [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>
wdenkea287de2005-04-01 00:25:43 +000011#include <net.h>
12#include <rtc.h>
13
14#include "sntp.h"
15
Bartlomiej Sieka49f3bdb2008-10-01 15:26:28 +020016#define SNTP_TIMEOUT 10000UL
wdenkea287de2005-04-01 00:25:43 +000017
Joe Hershberger38ba2552015-04-08 01:41:12 -050018static int sntp_our_port;
wdenkea287de2005-04-01 00:25:43 +000019
Joe Hershberger38ba2552015-04-08 01:41:12 -050020static void sntp_send(void)
wdenkea287de2005-04-01 00:25:43 +000021{
22 struct sntp_pkt_t pkt;
23 int pktlen = SNTP_PACKET_LEN;
24 int sport;
25
Robin Getz0ebf04c2009-07-23 03:01:03 -040026 debug("%s\n", __func__);
wdenkea287de2005-04-01 00:25:43 +000027
Joe Hershberger6c3234a2012-05-15 08:59:11 +000028 memset(&pkt, 0, sizeof(pkt));
wdenkea287de2005-04-01 00:25:43 +000029
30 pkt.li = NTP_LI_NOLEAP;
31 pkt.vn = NTP_VERSION;
32 pkt.mode = NTP_MODE_CLIENT;
33
Joe Hershberger1203fcc2015-04-08 01:41:05 -050034 memcpy((char *)net_tx_packet + net_eth_hdr_size() + IP_UDP_HDR_SIZE,
35 (char *)&pkt, pktlen);
wdenkea287de2005-04-01 00:25:43 +000036
Joe Hershberger38ba2552015-04-08 01:41:12 -050037 sntp_our_port = 10000 + (get_timer(0) % 4096);
wdenkea287de2005-04-01 00:25:43 +000038 sport = NTP_SERVICE_PORT;
39
Joe Hershberger1203fcc2015-04-08 01:41:05 -050040 net_send_udp_packet(net_server_ethaddr, net_ntp_server, sport,
Joe Hershberger38ba2552015-04-08 01:41:12 -050041 sntp_our_port, pktlen);
wdenkea287de2005-04-01 00:25:43 +000042}
43
Joe Hershberger38ba2552015-04-08 01:41:12 -050044static void sntp_timeout_handler(void)
wdenkea287de2005-04-01 00:25:43 +000045{
Joe Hershberger6c3234a2012-05-15 08:59:11 +000046 puts("Timeout\n");
Joe Hershberger22f6e992012-05-23 07:59:14 +000047 net_set_state(NETLOOP_FAIL);
wdenkea287de2005-04-01 00:25:43 +000048 return;
49}
50
Joe Hershberger049a95a2015-04-08 01:41:01 -050051static void sntp_handler(uchar *pkt, unsigned dest, struct in_addr sip,
52 unsigned src, unsigned len)
wdenkea287de2005-04-01 00:25:43 +000053{
Joe Hershberger3c56fb82015-04-08 01:41:24 -050054#ifdef CONFIG_TIMESTAMP
wdenk414eec32005-04-02 22:37:54 +000055 struct sntp_pkt_t *rpktp = (struct sntp_pkt_t *)pkt;
wdenkea287de2005-04-01 00:25:43 +000056 struct rtc_time tm;
wdenk414eec32005-04-02 22:37:54 +000057 ulong seconds;
Joe Hershberger3c56fb82015-04-08 01:41:24 -050058#endif
wdenkea287de2005-04-01 00:25:43 +000059
Robin Getz0ebf04c2009-07-23 03:01:03 -040060 debug("%s\n", __func__);
wdenkea287de2005-04-01 00:25:43 +000061
Joe Hershberger38ba2552015-04-08 01:41:12 -050062 if (dest != sntp_our_port)
Joe Hershberger6c3234a2012-05-15 08:59:11 +000063 return;
wdenkea287de2005-04-01 00:25:43 +000064
Joe Hershberger3c56fb82015-04-08 01:41:24 -050065#ifdef CONFIG_TIMESTAMP
wdenk414eec32005-04-02 22:37:54 +000066 /*
Joe Hershberger3c56fb82015-04-08 01:41:24 -050067 * As the RTC's used in U-Boot support second resolution only
wdenk414eec32005-04-02 22:37:54 +000068 * we simply ignore the sub-second field.
69 */
Joe Hershberger6c3234a2012-05-15 08:59:11 +000070 memcpy(&seconds, &rpktp->transmit_timestamp, sizeof(ulong));
wdenkea287de2005-04-01 00:25:43 +000071
Simon Glass9f9276c2015-04-20 12:37:18 -060072 rtc_to_tm(ntohl(seconds) - 2208988800UL + net_ntp_time_offset, &tm);
Jon Loeliger643d1ab2007-07-09 17:45:14 -050073#if defined(CONFIG_CMD_DATE)
Simon Glass17f0ac62015-04-20 12:37:27 -060074# ifdef CONFIG_DM_RTC
75 struct udevice *dev;
76 int ret;
77
78 ret = uclass_get_device(UCLASS_RTC, 0, &dev);
79 if (ret)
80 printf("SNTP: cannot find RTC: err=%d\n", ret);
81 else
82 dm_rtc_set(dev, &tm);
83# else
Joe Hershberger6c3234a2012-05-15 08:59:11 +000084 rtc_set(&tm);
Simon Glass17f0ac62015-04-20 12:37:27 -060085# endif
wdenkea287de2005-04-01 00:25:43 +000086#endif
Joe Hershberger6c3234a2012-05-15 08:59:11 +000087 printf("Date: %4d-%02d-%02d Time: %2d:%02d:%02d\n",
Joe Hershberger38ba2552015-04-08 01:41:12 -050088 tm.tm_year, tm.tm_mon, tm.tm_mday,
89 tm.tm_hour, tm.tm_min, tm.tm_sec);
Joe Hershberger3c56fb82015-04-08 01:41:24 -050090#endif
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
Joe Hershberger38ba2552015-04-08 01:41:12 -050095void sntp_start(void)
wdenkea287de2005-04-01 00:25:43 +000096{
Robin Getz0ebf04c2009-07-23 03:01:03 -040097 debug("%s\n", __func__);
wdenkea287de2005-04-01 00:25:43 +000098
Joe Hershbergerbc0571f2015-04-08 01:41:21 -050099 net_set_timeout_handler(SNTP_TIMEOUT, sntp_timeout_handler);
Joe Hershberger049a95a2015-04-08 01:41:01 -0500100 net_set_udp_handler(sntp_handler);
Joe Hershberger0adb5b72015-04-08 01:41:04 -0500101 memset(net_server_ethaddr, 0, sizeof(net_server_ethaddr));
wdenkea287de2005-04-01 00:25:43 +0000102
Joe Hershberger38ba2552015-04-08 01:41:12 -0500103 sntp_send();
wdenkea287de2005-04-01 00:25:43 +0000104}