Update QCA956x GPIO OUT functions list
[oweals/u-boot_mod.git] / u-boot / net / sntp.c
1 /*
2  * SNTP support driver
3  *
4  * Masami Komiya <mkomiya@sonare.it> 2005
5  *
6  */
7 #include <common.h>
8 #include <command.h>
9 #include <net.h>
10 #include <rtc.h>
11 #include "sntp.h"
12
13 #if defined(CONFIG_CMD_NET) && defined(CONFIG_CMD_SNTP)
14
15 DECLARE_GLOBAL_DATA_PTR;
16
17 //#define       DEBUG
18
19 #define SNTP_TIMEOUT    10
20
21 static int SntpOurPort;
22
23 static void SntpSend(void){
24         struct sntp_pkt_t pkt;
25         int pktlen = SNTP_PACKET_LEN;
26         int sport;
27
28 #ifdef DEBUG
29         printf("%s\n", __FUNCTION__);
30 #endif
31
32         memset(&pkt, 0, sizeof(pkt));
33
34         pkt.li = NTP_LI_NOLEAP;
35         pkt.vn = NTP_VERSION;
36         pkt.mode = NTP_MODE_CLIENT;
37
38         memcpy((char *)NetTxPacket + NetEthHdrSize() + IP_HDR_SIZE, (char *)&pkt, pktlen);
39
40         SntpOurPort = 10000 + (get_timer(0) % 4096);
41         sport = NTP_SERVICE_PORT;
42
43         puts("Sending SNTP request...\n");
44
45         NetSendUDPPacket(NetServerEther, NetNtpServerIP, sport, SntpOurPort, pktlen);
46 }
47
48 static void SntpTimeout(void){
49         printf_err("timeout\n");
50         NetState = NETLOOP_FAIL;
51
52         return;
53 }
54
55 static void SntpHandler(uchar *pkt, unsigned dest, unsigned src, unsigned len){
56         struct sntp_pkt_t *rpktp = (struct sntp_pkt_t *)pkt;
57         struct rtc_time tm;
58         ulong seconds;
59
60 #ifdef DEBUG
61         printf("%s\n", __FUNCTION__);
62 #endif
63
64         if(dest != SntpOurPort){
65                 return;
66         }
67
68         /*
69          * As the RTC's used in U-Boot supports second resolution only
70          * we simply ignore the sub-second field.
71          */
72         memcpy(&seconds, &rpktp->transmit_timestamp, sizeof(ulong));
73
74         to_tm(ntohl(seconds) - 2208988800UL + NetTimeOffset, &tm);
75
76 #if defined(CONFIG_CMD_DATE)
77         // TODO: find out how to use RTC on
78         //rtc_set(&tm);
79 #endif
80         printf("Got SNTP response: %4d-%02d-%02d %2d:%02d:%02d\n\n", tm.tm_year, tm.tm_mon, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec);
81         NetState = NETLOOP_SUCCESS;
82 }
83
84 void SntpStart(void){
85         bd_t *bd = gd->bd;
86 #ifdef DEBUG
87         printf("%s\n", __FUNCTION__);
88 #endif
89
90         NetSetTimeout(SNTP_TIMEOUT * CFG_HZ, SntpTimeout);
91         NetSetHandler(SntpHandler);
92
93         memset(NetServerEther, 0, 6);
94
95         SntpSend();
96 }
97
98 #endif /* CONFIG_CMD_NET && CONFIG_CMD_SNTP */