]> git.ipfire.org Git - people/ms/u-boot.git/blame - net/sntp.c
Remove CONFIG_SYS_BOOTCOUNT_SINGLEWORD
[people/ms/u-boot.git] / net / sntp.c
CommitLineData
ea287deb
WD
1/*
2 * SNTP support driver
3 *
4 * Masami Komiya <mkomiya@sonare.it> 2005
5 *
6 */
7
8#include <common.h>
9#include <command.h>
17f0ac60 10#include <dm.h>
ea287deb
WD
11#include <net.h>
12#include <rtc.h>
13
14#include "sntp.h"
15
49f3bdbb 16#define SNTP_TIMEOUT 10000UL
ea287deb 17
38ba2558 18static int sntp_our_port;
ea287deb 19
38ba2558 20static void sntp_send(void)
ea287deb
WD
21{
22 struct sntp_pkt_t pkt;
23 int pktlen = SNTP_PACKET_LEN;
24 int sport;
25
0ebf04c6 26 debug("%s\n", __func__);
ea287deb 27
6c3234a3 28 memset(&pkt, 0, sizeof(pkt));
ea287deb
WD
29
30 pkt.li = NTP_LI_NOLEAP;
31 pkt.vn = NTP_VERSION;
32 pkt.mode = NTP_MODE_CLIENT;
33
1203fcce
JH
34 memcpy((char *)net_tx_packet + net_eth_hdr_size() + IP_UDP_HDR_SIZE,
35 (char *)&pkt, pktlen);
ea287deb 36
38ba2558 37 sntp_our_port = 10000 + (get_timer(0) % 4096);
ea287deb
WD
38 sport = NTP_SERVICE_PORT;
39
1203fcce 40 net_send_udp_packet(net_server_ethaddr, net_ntp_server, sport,
38ba2558 41 sntp_our_port, pktlen);
ea287deb
WD
42}
43
38ba2558 44static void sntp_timeout_handler(void)
ea287deb 45{
6c3234a3 46 puts("Timeout\n");
22f6e99d 47 net_set_state(NETLOOP_FAIL);
ea287deb
WD
48 return;
49}
50
049a95a7
JH
51static void sntp_handler(uchar *pkt, unsigned dest, struct in_addr sip,
52 unsigned src, unsigned len)
ea287deb 53{
3c56fb82 54#ifdef CONFIG_TIMESTAMP
414eec35 55 struct sntp_pkt_t *rpktp = (struct sntp_pkt_t *)pkt;
ea287deb 56 struct rtc_time tm;
414eec35 57 ulong seconds;
3c56fb82 58#endif
ea287deb 59
0ebf04c6 60 debug("%s\n", __func__);
ea287deb 61
38ba2558 62 if (dest != sntp_our_port)
6c3234a3 63 return;
ea287deb 64
3c56fb82 65#ifdef CONFIG_TIMESTAMP
414eec35 66 /*
3c56fb82 67 * As the RTC's used in U-Boot support second resolution only
414eec35
WD
68 * we simply ignore the sub-second field.
69 */
6c3234a3 70 memcpy(&seconds, &rpktp->transmit_timestamp, sizeof(ulong));
ea287deb 71
9f9276c3 72 rtc_to_tm(ntohl(seconds) - 2208988800UL + net_ntp_time_offset, &tm);
643d1ab2 73#if defined(CONFIG_CMD_DATE)
17f0ac60
SG
74# 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
6c3234a3 84 rtc_set(&tm);
17f0ac60 85# endif
ea287deb 86#endif
6c3234a3 87 printf("Date: %4d-%02d-%02d Time: %2d:%02d:%02d\n",
38ba2558
JH
88 tm.tm_year, tm.tm_mon, tm.tm_mday,
89 tm.tm_hour, tm.tm_min, tm.tm_sec);
3c56fb82 90#endif
ea287deb 91
22f6e99d 92 net_set_state(NETLOOP_SUCCESS);
ea287deb
WD
93}
94
38ba2558 95void sntp_start(void)
ea287deb 96{
0ebf04c6 97 debug("%s\n", __func__);
ea287deb 98
bc0571fc 99 net_set_timeout_handler(SNTP_TIMEOUT, sntp_timeout_handler);
049a95a7 100 net_set_udp_handler(sntp_handler);
0adb5b76 101 memset(net_server_ethaddr, 0, sizeof(net_server_ethaddr));
ea287deb 102
38ba2558 103 sntp_send();
ea287deb 104}