1 /*
2  * SNTP support driver
3  *
4  * Masami Komiya <mkomiya@sonare.it> 2005
5  *
6  */
7 
8 #include <common.h>
9 #include <command.h>
10 #include <dm.h>
11 #include <log.h>
12 #include <net.h>
13 #include <rtc.h>
14 
15 #include <net/sntp.h>
16 
17 #define SNTP_TIMEOUT 10000UL
18 
19 static int sntp_our_port;
20 
21 /* NTP server IP address */
22 struct in_addr	net_ntp_server;
23 /* offset time from UTC */
24 int		net_ntp_time_offset;
25 
sntp_send(void)26 static void sntp_send(void)
27 {
28 	struct sntp_pkt_t pkt;
29 	int pktlen = SNTP_PACKET_LEN;
30 	int sport;
31 
32 	debug("%s\n", __func__);
33 
34 	memset(&pkt, 0, sizeof(pkt));
35 
36 	pkt.li = NTP_LI_NOLEAP;
37 	pkt.vn = NTP_VERSION;
38 	pkt.mode = NTP_MODE_CLIENT;
39 
40 	memcpy((char *)net_tx_packet + net_eth_hdr_size() + IP_UDP_HDR_SIZE,
41 	       (char *)&pkt, pktlen);
42 
43 	sntp_our_port = 10000 + (get_timer(0) % 4096);
44 	sport = NTP_SERVICE_PORT;
45 
46 	net_send_udp_packet(net_server_ethaddr, net_ntp_server, sport,
47 			    sntp_our_port, pktlen);
48 }
49 
sntp_timeout_handler(void)50 static void sntp_timeout_handler(void)
51 {
52 	puts("Timeout\n");
53 	net_set_state(NETLOOP_FAIL);
54 	return;
55 }
56 
sntp_handler(uchar * pkt,unsigned dest,struct in_addr sip,unsigned src,unsigned len)57 static void sntp_handler(uchar *pkt, unsigned dest, struct in_addr sip,
58 			 unsigned src, unsigned len)
59 {
60 	struct sntp_pkt_t *rpktp = (struct sntp_pkt_t *)pkt;
61 	struct rtc_time tm;
62 	ulong seconds;
63 
64 	debug("%s\n", __func__);
65 
66 	if (dest != sntp_our_port)
67 		return;
68 
69 	/*
70 	 * As the RTC's used in U-Boot support second resolution only
71 	 * we simply ignore the sub-second field.
72 	 */
73 	memcpy(&seconds, &rpktp->transmit_timestamp, sizeof(ulong));
74 
75 	rtc_to_tm(ntohl(seconds) - 2208988800UL + net_ntp_time_offset, &tm);
76 #ifdef CONFIG_DM_RTC
77 	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);
85 #elif defined(CONFIG_CMD_DATE)
86 	rtc_set(&tm);
87 #endif
88 	printf("Date: %4d-%02d-%02d Time: %2d:%02d:%02d\n",
89 	       tm.tm_year, tm.tm_mon, tm.tm_mday,
90 	       tm.tm_hour, tm.tm_min, tm.tm_sec);
91 
92 	net_set_state(NETLOOP_SUCCESS);
93 }
94 
95 /*
96  * SNTP:
97  *
98  *	Prerequisites:	- own ethernet address
99  *			- own IP address
100  *	We want:	- network time
101  *	Next step:	none
102  */
sntp_prereq(void * data)103 int 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 
sntp_start(void * data)113 int sntp_start(void *data)
114 {
115 	debug("%s\n", __func__);
116 
117 	net_set_timeout_handler(SNTP_TIMEOUT, sntp_timeout_handler);
118 	net_set_udp_handler(sntp_handler);
119 	memset(net_server_ethaddr, 0, sizeof(net_server_ethaddr));
120 
121 	sntp_send();
122 
123 	return 0;
124 }
125