4 * Masami Komiya <mkomiya@sonare.it> 2005
17 #define SNTP_TIMEOUT 10000UL
19 static int sntp_our_port;
21 /* NTP server IP address */
22 struct in_addr net_ntp_server;
23 /* offset time from UTC */
24 int net_ntp_time_offset;
26 static void sntp_send(void)
28 struct sntp_pkt_t pkt;
29 int pktlen = SNTP_PACKET_LEN;
32 debug("%s\n", __func__);
34 memset(&pkt, 0, sizeof(pkt));
36 pkt.li = NTP_LI_NOLEAP;
38 pkt.mode = NTP_MODE_CLIENT;
40 memcpy((char *)net_tx_packet + net_eth_hdr_size() + IP_UDP_HDR_SIZE,
41 (char *)&pkt, pktlen);
43 sntp_our_port = 10000 + (get_timer(0) % 4096);
44 sport = NTP_SERVICE_PORT;
46 net_send_udp_packet(net_server_ethaddr, net_ntp_server, sport,
47 sntp_our_port, pktlen);
50 static void sntp_timeout_handler(void)
53 net_set_state(NETLOOP_FAIL);
57 static void sntp_handler(uchar *pkt, unsigned dest, struct in_addr sip,
58 unsigned src, unsigned len)
60 #ifdef CONFIG_TIMESTAMP
61 struct sntp_pkt_t *rpktp = (struct sntp_pkt_t *)pkt;
66 debug("%s\n", __func__);
68 if (dest != sntp_our_port)
71 #ifdef CONFIG_TIMESTAMP
73 * As the RTC's used in U-Boot support second resolution only
74 * we simply ignore the sub-second field.
76 memcpy(&seconds, &rpktp->transmit_timestamp, sizeof(ulong));
78 rtc_to_tm(ntohl(seconds) - 2208988800UL + net_ntp_time_offset, &tm);
79 #if defined(CONFIG_CMD_DATE)
84 ret = uclass_get_device(UCLASS_RTC, 0, &dev);
86 printf("SNTP: cannot find RTC: err=%d\n", ret);
93 printf("Date: %4d-%02d-%02d Time: %2d:%02d:%02d\n",
94 tm.tm_year, tm.tm_mon, tm.tm_mday,
95 tm.tm_hour, tm.tm_min, tm.tm_sec);
98 net_set_state(NETLOOP_SUCCESS);
104 * Prerequisites: - own ethernet address
106 * We want: - network time
109 int sntp_prereq(void *data)
111 if (net_ntp_server.s_addr == 0) {
112 puts("*** ERROR: NTP server address not given\n");
119 int sntp_start(void *data)
121 debug("%s\n", __func__);
123 net_set_timeout_handler(SNTP_TIMEOUT, sntp_timeout_handler);
124 net_set_udp_handler(sntp_handler);
125 memset(net_server_ethaddr, 0, sizeof(net_server_ethaddr));