wdenk | b4ad962 | 2005-04-01 00:25:43 +0000 | [diff] [blame] | 1 | /* |
| 2 | * SNTP support driver |
| 3 | * |
| 4 | * Masami Komiya <mkomiya@sonare.it> 2005 |
| 5 | * |
| 6 | */ |
| 7 | |
| 8 | #include <common.h> |
| 9 | #include <command.h> |
Simon Glass | 1aa1126 | 2015-04-20 12:37:27 -0600 | [diff] [blame] | 10 | #include <dm.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 11 | #include <log.h> |
wdenk | b4ad962 | 2005-04-01 00:25:43 +0000 | [diff] [blame] | 12 | #include <net.h> |
| 13 | #include <rtc.h> |
| 14 | |
| 15 | #include "sntp.h" |
| 16 | |
Bartlomiej Sieka | 5666846 | 2008-10-01 15:26:28 +0200 | [diff] [blame] | 17 | #define SNTP_TIMEOUT 10000UL |
wdenk | b4ad962 | 2005-04-01 00:25:43 +0000 | [diff] [blame] | 18 | |
Joe Hershberger | 4af0b77 | 2015-04-08 01:41:12 -0500 | [diff] [blame] | 19 | static int sntp_our_port; |
wdenk | b4ad962 | 2005-04-01 00:25:43 +0000 | [diff] [blame] | 20 | |
Joe Hershberger | 4af0b77 | 2015-04-08 01:41:12 -0500 | [diff] [blame] | 21 | static void sntp_send(void) |
wdenk | b4ad962 | 2005-04-01 00:25:43 +0000 | [diff] [blame] | 22 | { |
| 23 | struct sntp_pkt_t pkt; |
| 24 | int pktlen = SNTP_PACKET_LEN; |
| 25 | int sport; |
| 26 | |
Robin Getz | 9e0a4d6 | 2009-07-23 03:01:03 -0400 | [diff] [blame] | 27 | debug("%s\n", __func__); |
wdenk | b4ad962 | 2005-04-01 00:25:43 +0000 | [diff] [blame] | 28 | |
Joe Hershberger | 840fc80 | 2012-05-15 08:59:11 +0000 | [diff] [blame] | 29 | memset(&pkt, 0, sizeof(pkt)); |
wdenk | b4ad962 | 2005-04-01 00:25:43 +0000 | [diff] [blame] | 30 | |
| 31 | pkt.li = NTP_LI_NOLEAP; |
| 32 | pkt.vn = NTP_VERSION; |
| 33 | pkt.mode = NTP_MODE_CLIENT; |
| 34 | |
Joe Hershberger | a8ca4f6 | 2015-04-08 01:41:05 -0500 | [diff] [blame] | 35 | memcpy((char *)net_tx_packet + net_eth_hdr_size() + IP_UDP_HDR_SIZE, |
| 36 | (char *)&pkt, pktlen); |
wdenk | b4ad962 | 2005-04-01 00:25:43 +0000 | [diff] [blame] | 37 | |
Joe Hershberger | 4af0b77 | 2015-04-08 01:41:12 -0500 | [diff] [blame] | 38 | sntp_our_port = 10000 + (get_timer(0) % 4096); |
wdenk | b4ad962 | 2005-04-01 00:25:43 +0000 | [diff] [blame] | 39 | sport = NTP_SERVICE_PORT; |
| 40 | |
Joe Hershberger | a8ca4f6 | 2015-04-08 01:41:05 -0500 | [diff] [blame] | 41 | net_send_udp_packet(net_server_ethaddr, net_ntp_server, sport, |
Joe Hershberger | 4af0b77 | 2015-04-08 01:41:12 -0500 | [diff] [blame] | 42 | sntp_our_port, pktlen); |
wdenk | b4ad962 | 2005-04-01 00:25:43 +0000 | [diff] [blame] | 43 | } |
| 44 | |
Joe Hershberger | 4af0b77 | 2015-04-08 01:41:12 -0500 | [diff] [blame] | 45 | static void sntp_timeout_handler(void) |
wdenk | b4ad962 | 2005-04-01 00:25:43 +0000 | [diff] [blame] | 46 | { |
Joe Hershberger | 840fc80 | 2012-05-15 08:59:11 +0000 | [diff] [blame] | 47 | puts("Timeout\n"); |
Joe Hershberger | d4bb76a | 2012-05-23 07:59:14 +0000 | [diff] [blame] | 48 | net_set_state(NETLOOP_FAIL); |
wdenk | b4ad962 | 2005-04-01 00:25:43 +0000 | [diff] [blame] | 49 | return; |
| 50 | } |
| 51 | |
Joe Hershberger | 5874dec | 2015-04-08 01:41:01 -0500 | [diff] [blame] | 52 | static void sntp_handler(uchar *pkt, unsigned dest, struct in_addr sip, |
| 53 | unsigned src, unsigned len) |
wdenk | b4ad962 | 2005-04-01 00:25:43 +0000 | [diff] [blame] | 54 | { |
Joe Hershberger | 22498c5 | 2015-04-08 01:41:24 -0500 | [diff] [blame] | 55 | #ifdef CONFIG_TIMESTAMP |
wdenk | 8d5d28a | 2005-04-02 22:37:54 +0000 | [diff] [blame] | 56 | struct sntp_pkt_t *rpktp = (struct sntp_pkt_t *)pkt; |
wdenk | b4ad962 | 2005-04-01 00:25:43 +0000 | [diff] [blame] | 57 | struct rtc_time tm; |
wdenk | 8d5d28a | 2005-04-02 22:37:54 +0000 | [diff] [blame] | 58 | ulong seconds; |
Joe Hershberger | 22498c5 | 2015-04-08 01:41:24 -0500 | [diff] [blame] | 59 | #endif |
wdenk | b4ad962 | 2005-04-01 00:25:43 +0000 | [diff] [blame] | 60 | |
Robin Getz | 9e0a4d6 | 2009-07-23 03:01:03 -0400 | [diff] [blame] | 61 | debug("%s\n", __func__); |
wdenk | b4ad962 | 2005-04-01 00:25:43 +0000 | [diff] [blame] | 62 | |
Joe Hershberger | 4af0b77 | 2015-04-08 01:41:12 -0500 | [diff] [blame] | 63 | if (dest != sntp_our_port) |
Joe Hershberger | 840fc80 | 2012-05-15 08:59:11 +0000 | [diff] [blame] | 64 | return; |
wdenk | b4ad962 | 2005-04-01 00:25:43 +0000 | [diff] [blame] | 65 | |
Joe Hershberger | 22498c5 | 2015-04-08 01:41:24 -0500 | [diff] [blame] | 66 | #ifdef CONFIG_TIMESTAMP |
wdenk | 8d5d28a | 2005-04-02 22:37:54 +0000 | [diff] [blame] | 67 | /* |
Joe Hershberger | 22498c5 | 2015-04-08 01:41:24 -0500 | [diff] [blame] | 68 | * As the RTC's used in U-Boot support second resolution only |
wdenk | 8d5d28a | 2005-04-02 22:37:54 +0000 | [diff] [blame] | 69 | * we simply ignore the sub-second field. |
| 70 | */ |
Joe Hershberger | 840fc80 | 2012-05-15 08:59:11 +0000 | [diff] [blame] | 71 | memcpy(&seconds, &rpktp->transmit_timestamp, sizeof(ulong)); |
wdenk | b4ad962 | 2005-04-01 00:25:43 +0000 | [diff] [blame] | 72 | |
Simon Glass | 1299466 | 2015-04-20 12:37:18 -0600 | [diff] [blame] | 73 | rtc_to_tm(ntohl(seconds) - 2208988800UL + net_ntp_time_offset, &tm); |
Jon Loeliger | 54f35c2 | 2007-07-09 17:45:14 -0500 | [diff] [blame] | 74 | #if defined(CONFIG_CMD_DATE) |
Simon Glass | 1aa1126 | 2015-04-20 12:37:27 -0600 | [diff] [blame] | 75 | # ifdef CONFIG_DM_RTC |
| 76 | struct udevice *dev; |
| 77 | int ret; |
| 78 | |
| 79 | ret = uclass_get_device(UCLASS_RTC, 0, &dev); |
| 80 | if (ret) |
| 81 | printf("SNTP: cannot find RTC: err=%d\n", ret); |
| 82 | else |
| 83 | dm_rtc_set(dev, &tm); |
| 84 | # else |
Joe Hershberger | 840fc80 | 2012-05-15 08:59:11 +0000 | [diff] [blame] | 85 | rtc_set(&tm); |
Simon Glass | 1aa1126 | 2015-04-20 12:37:27 -0600 | [diff] [blame] | 86 | # endif |
wdenk | b4ad962 | 2005-04-01 00:25:43 +0000 | [diff] [blame] | 87 | #endif |
Joe Hershberger | 840fc80 | 2012-05-15 08:59:11 +0000 | [diff] [blame] | 88 | printf("Date: %4d-%02d-%02d Time: %2d:%02d:%02d\n", |
Joe Hershberger | 4af0b77 | 2015-04-08 01:41:12 -0500 | [diff] [blame] | 89 | tm.tm_year, tm.tm_mon, tm.tm_mday, |
| 90 | tm.tm_hour, tm.tm_min, tm.tm_sec); |
Joe Hershberger | 22498c5 | 2015-04-08 01:41:24 -0500 | [diff] [blame] | 91 | #endif |
wdenk | b4ad962 | 2005-04-01 00:25:43 +0000 | [diff] [blame] | 92 | |
Joe Hershberger | d4bb76a | 2012-05-23 07:59:14 +0000 | [diff] [blame] | 93 | net_set_state(NETLOOP_SUCCESS); |
wdenk | b4ad962 | 2005-04-01 00:25:43 +0000 | [diff] [blame] | 94 | } |
| 95 | |
Joe Hershberger | 4af0b77 | 2015-04-08 01:41:12 -0500 | [diff] [blame] | 96 | void sntp_start(void) |
wdenk | b4ad962 | 2005-04-01 00:25:43 +0000 | [diff] [blame] | 97 | { |
Robin Getz | 9e0a4d6 | 2009-07-23 03:01:03 -0400 | [diff] [blame] | 98 | debug("%s\n", __func__); |
wdenk | b4ad962 | 2005-04-01 00:25:43 +0000 | [diff] [blame] | 99 | |
Joe Hershberger | c80b41b0 | 2015-04-08 01:41:21 -0500 | [diff] [blame] | 100 | net_set_timeout_handler(SNTP_TIMEOUT, sntp_timeout_handler); |
Joe Hershberger | 5874dec | 2015-04-08 01:41:01 -0500 | [diff] [blame] | 101 | net_set_udp_handler(sntp_handler); |
Joe Hershberger | 8ecdbed | 2015-04-08 01:41:04 -0500 | [diff] [blame] | 102 | memset(net_server_ethaddr, 0, sizeof(net_server_ethaddr)); |
wdenk | b4ad962 | 2005-04-01 00:25:43 +0000 | [diff] [blame] | 103 | |
Joe Hershberger | 4af0b77 | 2015-04-08 01:41:12 -0500 | [diff] [blame] | 104 | sntp_send(); |
wdenk | b4ad962 | 2005-04-01 00:25:43 +0000 | [diff] [blame] | 105 | } |