wdenk | ea287de | 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 | |
wdenk | ea287de | 2005-04-01 00:25:43 +0000 | [diff] [blame] | 8 | #include <command.h> |
Simon Glass | 17f0ac6 | 2015-04-20 12:37:27 -0600 | [diff] [blame] | 9 | #include <dm.h> |
Simon Glass | f7ae49f | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 10 | #include <log.h> |
wdenk | ea287de | 2005-04-01 00:25:43 +0000 | [diff] [blame] | 11 | #include <net.h> |
| 12 | #include <rtc.h> |
| 13 | |
Philippe Reynes | 912ece4 | 2020-09-18 14:13:02 +0200 | [diff] [blame] | 14 | #include <net/sntp.h> |
wdenk | ea287de | 2005-04-01 00:25:43 +0000 | [diff] [blame] | 15 | |
Bartlomiej Sieka | 49f3bdb | 2008-10-01 15:26:28 +0200 | [diff] [blame] | 16 | #define SNTP_TIMEOUT 10000UL |
wdenk | ea287de | 2005-04-01 00:25:43 +0000 | [diff] [blame] | 17 | |
Joe Hershberger | 38ba255 | 2015-04-08 01:41:12 -0500 | [diff] [blame] | 18 | static int sntp_our_port; |
wdenk | ea287de | 2005-04-01 00:25:43 +0000 | [diff] [blame] | 19 | |
Philippe Reynes | 912ece4 | 2020-09-18 14:13:02 +0200 | [diff] [blame] | 20 | /* NTP server IP address */ |
| 21 | struct in_addr net_ntp_server; |
| 22 | /* offset time from UTC */ |
| 23 | int net_ntp_time_offset; |
| 24 | |
Joe Hershberger | 38ba255 | 2015-04-08 01:41:12 -0500 | [diff] [blame] | 25 | static void sntp_send(void) |
wdenk | ea287de | 2005-04-01 00:25:43 +0000 | [diff] [blame] | 26 | { |
| 27 | struct sntp_pkt_t pkt; |
| 28 | int pktlen = SNTP_PACKET_LEN; |
| 29 | int sport; |
| 30 | |
Robin Getz | 0ebf04c | 2009-07-23 03:01:03 -0400 | [diff] [blame] | 31 | debug("%s\n", __func__); |
wdenk | ea287de | 2005-04-01 00:25:43 +0000 | [diff] [blame] | 32 | |
Joe Hershberger | 6c3234a | 2012-05-15 08:59:11 +0000 | [diff] [blame] | 33 | memset(&pkt, 0, sizeof(pkt)); |
wdenk | ea287de | 2005-04-01 00:25:43 +0000 | [diff] [blame] | 34 | |
| 35 | pkt.li = NTP_LI_NOLEAP; |
| 36 | pkt.vn = NTP_VERSION; |
| 37 | pkt.mode = NTP_MODE_CLIENT; |
| 38 | |
Joe Hershberger | 1203fcc | 2015-04-08 01:41:05 -0500 | [diff] [blame] | 39 | memcpy((char *)net_tx_packet + net_eth_hdr_size() + IP_UDP_HDR_SIZE, |
| 40 | (char *)&pkt, pktlen); |
wdenk | ea287de | 2005-04-01 00:25:43 +0000 | [diff] [blame] | 41 | |
Joe Hershberger | 38ba255 | 2015-04-08 01:41:12 -0500 | [diff] [blame] | 42 | sntp_our_port = 10000 + (get_timer(0) % 4096); |
wdenk | ea287de | 2005-04-01 00:25:43 +0000 | [diff] [blame] | 43 | sport = NTP_SERVICE_PORT; |
| 44 | |
Joe Hershberger | 1203fcc | 2015-04-08 01:41:05 -0500 | [diff] [blame] | 45 | net_send_udp_packet(net_server_ethaddr, net_ntp_server, sport, |
Joe Hershberger | 38ba255 | 2015-04-08 01:41:12 -0500 | [diff] [blame] | 46 | sntp_our_port, pktlen); |
wdenk | ea287de | 2005-04-01 00:25:43 +0000 | [diff] [blame] | 47 | } |
| 48 | |
Joe Hershberger | 38ba255 | 2015-04-08 01:41:12 -0500 | [diff] [blame] | 49 | static void sntp_timeout_handler(void) |
wdenk | ea287de | 2005-04-01 00:25:43 +0000 | [diff] [blame] | 50 | { |
Joe Hershberger | 6c3234a | 2012-05-15 08:59:11 +0000 | [diff] [blame] | 51 | puts("Timeout\n"); |
Joe Hershberger | 22f6e99 | 2012-05-23 07:59:14 +0000 | [diff] [blame] | 52 | net_set_state(NETLOOP_FAIL); |
wdenk | ea287de | 2005-04-01 00:25:43 +0000 | [diff] [blame] | 53 | return; |
| 54 | } |
| 55 | |
Joe Hershberger | 049a95a | 2015-04-08 01:41:01 -0500 | [diff] [blame] | 56 | static void sntp_handler(uchar *pkt, unsigned dest, struct in_addr sip, |
| 57 | unsigned src, unsigned len) |
wdenk | ea287de | 2005-04-01 00:25:43 +0000 | [diff] [blame] | 58 | { |
wdenk | 414eec3 | 2005-04-02 22:37:54 +0000 | [diff] [blame] | 59 | struct sntp_pkt_t *rpktp = (struct sntp_pkt_t *)pkt; |
wdenk | ea287de | 2005-04-01 00:25:43 +0000 | [diff] [blame] | 60 | struct rtc_time tm; |
wdenk | 414eec3 | 2005-04-02 22:37:54 +0000 | [diff] [blame] | 61 | ulong seconds; |
wdenk | ea287de | 2005-04-01 00:25:43 +0000 | [diff] [blame] | 62 | |
Robin Getz | 0ebf04c | 2009-07-23 03:01:03 -0400 | [diff] [blame] | 63 | debug("%s\n", __func__); |
wdenk | ea287de | 2005-04-01 00:25:43 +0000 | [diff] [blame] | 64 | |
Joe Hershberger | 38ba255 | 2015-04-08 01:41:12 -0500 | [diff] [blame] | 65 | if (dest != sntp_our_port) |
Joe Hershberger | 6c3234a | 2012-05-15 08:59:11 +0000 | [diff] [blame] | 66 | return; |
wdenk | ea287de | 2005-04-01 00:25:43 +0000 | [diff] [blame] | 67 | |
wdenk | 414eec3 | 2005-04-02 22:37:54 +0000 | [diff] [blame] | 68 | /* |
Joe Hershberger | 3c56fb8 | 2015-04-08 01:41:24 -0500 | [diff] [blame] | 69 | * As the RTC's used in U-Boot support second resolution only |
wdenk | 414eec3 | 2005-04-02 22:37:54 +0000 | [diff] [blame] | 70 | * we simply ignore the sub-second field. |
| 71 | */ |
Joe Hershberger | 6c3234a | 2012-05-15 08:59:11 +0000 | [diff] [blame] | 72 | memcpy(&seconds, &rpktp->transmit_timestamp, sizeof(ulong)); |
wdenk | ea287de | 2005-04-01 00:25:43 +0000 | [diff] [blame] | 73 | |
Simon Glass | 9f9276c | 2015-04-20 12:37:18 -0600 | [diff] [blame] | 74 | rtc_to_tm(ntohl(seconds) - 2208988800UL + net_ntp_time_offset, &tm); |
Heinrich Schuchardt | e1864db | 2020-10-23 12:58:27 +0200 | [diff] [blame] | 75 | #ifdef CONFIG_DM_RTC |
Simon Glass | 17f0ac6 | 2015-04-20 12:37:27 -0600 | [diff] [blame] | 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); |
Heinrich Schuchardt | e1864db | 2020-10-23 12:58:27 +0200 | [diff] [blame] | 84 | #elif defined(CONFIG_CMD_DATE) |
Joe Hershberger | 6c3234a | 2012-05-15 08:59:11 +0000 | [diff] [blame] | 85 | rtc_set(&tm); |
wdenk | ea287de | 2005-04-01 00:25:43 +0000 | [diff] [blame] | 86 | #endif |
Joe Hershberger | 6c3234a | 2012-05-15 08:59:11 +0000 | [diff] [blame] | 87 | printf("Date: %4d-%02d-%02d Time: %2d:%02d:%02d\n", |
Joe Hershberger | 38ba255 | 2015-04-08 01:41:12 -0500 | [diff] [blame] | 88 | tm.tm_year, tm.tm_mon, tm.tm_mday, |
| 89 | tm.tm_hour, tm.tm_min, tm.tm_sec); |
wdenk | ea287de | 2005-04-01 00:25:43 +0000 | [diff] [blame] | 90 | |
Joe Hershberger | 22f6e99 | 2012-05-23 07:59:14 +0000 | [diff] [blame] | 91 | net_set_state(NETLOOP_SUCCESS); |
wdenk | ea287de | 2005-04-01 00:25:43 +0000 | [diff] [blame] | 92 | } |
| 93 | |
Philippe Reynes | 912ece4 | 2020-09-18 14:13:02 +0200 | [diff] [blame] | 94 | /* |
| 95 | * SNTP: |
| 96 | * |
| 97 | * Prerequisites: - own ethernet address |
| 98 | * - own IP address |
| 99 | * We want: - network time |
| 100 | * Next step: none |
| 101 | */ |
| 102 | int sntp_prereq(void *data) |
| 103 | { |
| 104 | if (net_ntp_server.s_addr == 0) { |
| 105 | puts("*** ERROR: NTP server address not given\n"); |
| 106 | return 1; |
| 107 | } |
| 108 | |
| 109 | return 0; |
| 110 | } |
| 111 | |
| 112 | int sntp_start(void *data) |
wdenk | ea287de | 2005-04-01 00:25:43 +0000 | [diff] [blame] | 113 | { |
Robin Getz | 0ebf04c | 2009-07-23 03:01:03 -0400 | [diff] [blame] | 114 | debug("%s\n", __func__); |
wdenk | ea287de | 2005-04-01 00:25:43 +0000 | [diff] [blame] | 115 | |
Joe Hershberger | bc0571f | 2015-04-08 01:41:21 -0500 | [diff] [blame] | 116 | net_set_timeout_handler(SNTP_TIMEOUT, sntp_timeout_handler); |
Joe Hershberger | 049a95a | 2015-04-08 01:41:01 -0500 | [diff] [blame] | 117 | net_set_udp_handler(sntp_handler); |
Joe Hershberger | 0adb5b7 | 2015-04-08 01:41:04 -0500 | [diff] [blame] | 118 | memset(net_server_ethaddr, 0, sizeof(net_server_ethaddr)); |
wdenk | ea287de | 2005-04-01 00:25:43 +0000 | [diff] [blame] | 119 | |
Joe Hershberger | 38ba255 | 2015-04-08 01:41:12 -0500 | [diff] [blame] | 120 | sntp_send(); |
Philippe Reynes | 912ece4 | 2020-09-18 14:13:02 +0200 | [diff] [blame] | 121 | |
| 122 | return 0; |
wdenk | ea287de | 2005-04-01 00:25:43 +0000 | [diff] [blame] | 123 | } |