Nobuhiro Iwamatsu | 9751ee0 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 1 | /* |
Robert P. J. Day | 1cc0a9f | 2016-05-04 04:47:31 -0400 | [diff] [blame] | 2 | * sh_eth.c - Driver for Renesas ethernet controller. |
Nobuhiro Iwamatsu | 9751ee0 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 3 | * |
Nobuhiro Iwamatsu | 3bb4cc3 | 2011-11-14 16:56:59 +0900 | [diff] [blame] | 4 | * Copyright (C) 2008, 2011 Renesas Solutions Corp. |
Nobuhiro Iwamatsu | f7ca1f7 | 2014-11-04 09:15:48 +0900 | [diff] [blame] | 5 | * Copyright (c) 2008, 2011, 2014 2014 Nobuhiro Iwamatsu |
Nobuhiro Iwamatsu | 9751ee0 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 6 | * Copyright (c) 2007 Carlos Munoz <carlos@kenati.com> |
Nobuhiro Iwamatsu | f7ca1f7 | 2014-11-04 09:15:48 +0900 | [diff] [blame] | 7 | * Copyright (C) 2013, 2014 Renesas Electronics Corporation |
Nobuhiro Iwamatsu | 9751ee0 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 8 | * |
Wolfgang Denk | 1a45966 | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 9 | * SPDX-License-Identifier: GPL-2.0+ |
Nobuhiro Iwamatsu | 9751ee0 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 10 | */ |
| 11 | |
| 12 | #include <config.h> |
| 13 | #include <common.h> |
| 14 | #include <malloc.h> |
| 15 | #include <net.h> |
Nobuhiro Iwamatsu | bd3980c | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 16 | #include <netdev.h> |
Yoshihiro Shimoda | bd1024b | 2011-10-11 18:10:14 +0900 | [diff] [blame] | 17 | #include <miiphy.h> |
Masahiro Yamada | 1221ce4 | 2016-09-21 11:28:55 +0900 | [diff] [blame] | 18 | #include <linux/errno.h> |
Nobuhiro Iwamatsu | 9751ee0 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 19 | #include <asm/io.h> |
| 20 | |
| 21 | #include "sh_eth.h" |
| 22 | |
| 23 | #ifndef CONFIG_SH_ETHER_USE_PORT |
| 24 | # error "Please define CONFIG_SH_ETHER_USE_PORT" |
| 25 | #endif |
| 26 | #ifndef CONFIG_SH_ETHER_PHY_ADDR |
| 27 | # error "Please define CONFIG_SH_ETHER_PHY_ADDR" |
| 28 | #endif |
Nobuhiro Iwamatsu | 870cc23 | 2013-08-22 13:22:01 +0900 | [diff] [blame] | 29 | |
Nobuhiro Iwamatsu | 92f0713 | 2013-08-22 13:22:03 +0900 | [diff] [blame] | 30 | #if defined(CONFIG_SH_ETHER_CACHE_WRITEBACK) && !defined(CONFIG_SYS_DCACHE_OFF) |
| 31 | #define flush_cache_wback(addr, len) \ |
Nobuhiro Iwamatsu | aae5d23 | 2017-12-01 13:56:08 +0900 | [diff] [blame] | 32 | flush_dcache_range((u32)addr, \ |
| 33 | (u32)(addr + ALIGN(len, CONFIG_SH_ETHER_ALIGNE_SIZE))) |
Yoshihiro Shimoda | 68260aa | 2011-01-27 10:06:08 +0900 | [diff] [blame] | 34 | #else |
| 35 | #define flush_cache_wback(...) |
| 36 | #endif |
Nobuhiro Iwamatsu | 9751ee0 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 37 | |
Nobuhiro Iwamatsu | 92f0713 | 2013-08-22 13:22:03 +0900 | [diff] [blame] | 38 | #if defined(CONFIG_SH_ETHER_CACHE_INVALIDATE) && defined(CONFIG_ARM) |
| 39 | #define invalidate_cache(addr, len) \ |
| 40 | { \ |
| 41 | u32 line_size = CONFIG_SH_ETHER_ALIGNE_SIZE; \ |
| 42 | u32 start, end; \ |
| 43 | \ |
| 44 | start = (u32)addr; \ |
| 45 | end = start + len; \ |
| 46 | start &= ~(line_size - 1); \ |
| 47 | end = ((end + line_size - 1) & ~(line_size - 1)); \ |
| 48 | \ |
| 49 | invalidate_dcache_range(start, end); \ |
| 50 | } |
| 51 | #else |
| 52 | #define invalidate_cache(...) |
| 53 | #endif |
| 54 | |
Nobuhiro Iwamatsu | 4ba62c7 | 2012-01-11 10:23:51 +0900 | [diff] [blame] | 55 | #define TIMEOUT_CNT 1000 |
| 56 | |
Joe Hershberger | 10cbe3b | 2012-05-22 18:36:19 +0000 | [diff] [blame] | 57 | int sh_eth_send(struct eth_device *dev, void *packet, int len) |
Nobuhiro Iwamatsu | 9751ee0 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 58 | { |
Nobuhiro Iwamatsu | bd3980c | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 59 | struct sh_eth_dev *eth = dev->priv; |
| 60 | int port = eth->port, ret = 0, timeout; |
| 61 | struct sh_eth_info *port_info = ð->port_info[port]; |
Nobuhiro Iwamatsu | 9751ee0 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 62 | |
| 63 | if (!packet || len > 0xffff) { |
Nobuhiro Iwamatsu | bd3980c | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 64 | printf(SHETHER_NAME ": %s: Invalid argument\n", __func__); |
| 65 | ret = -EINVAL; |
| 66 | goto err; |
Nobuhiro Iwamatsu | 9751ee0 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | /* packet must be a 4 byte boundary */ |
Nobuhiro Iwamatsu | ee6ec5d | 2012-02-02 21:28:49 +0000 | [diff] [blame] | 70 | if ((int)packet & 3) { |
Nobuhiro Iwamatsu | dc14867 | 2017-12-01 08:08:00 +0900 | [diff] [blame] | 71 | printf(SHETHER_NAME ": %s: packet not 4 byte aligned\n" |
Nobuhiro Iwamatsu | e2752db | 2014-01-23 07:52:19 +0900 | [diff] [blame] | 72 | , __func__); |
Nobuhiro Iwamatsu | bd3980c | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 73 | ret = -EFAULT; |
| 74 | goto err; |
Nobuhiro Iwamatsu | 9751ee0 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | /* Update tx descriptor */ |
Yoshihiro Shimoda | 68260aa | 2011-01-27 10:06:08 +0900 | [diff] [blame] | 78 | flush_cache_wback(packet, len); |
Nobuhiro Iwamatsu | 9751ee0 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 79 | port_info->tx_desc_cur->td2 = ADDR_TO_PHY(packet); |
| 80 | port_info->tx_desc_cur->td1 = len << 16; |
| 81 | /* Must preserve the end of descriptor list indication */ |
| 82 | if (port_info->tx_desc_cur->td0 & TD_TDLE) |
| 83 | port_info->tx_desc_cur->td0 = TD_TACT | TD_TFP | TD_TDLE; |
| 84 | else |
| 85 | port_info->tx_desc_cur->td0 = TD_TACT | TD_TFP; |
| 86 | |
Nobuhiro Iwamatsu | f7ca1f7 | 2014-11-04 09:15:48 +0900 | [diff] [blame] | 87 | flush_cache_wback(port_info->tx_desc_cur, sizeof(struct tx_desc_s)); |
| 88 | |
Nobuhiro Iwamatsu | 9751ee0 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 89 | /* Restart the transmitter if disabled */ |
Nobuhiro Iwamatsu | fbfb511 | 2017-12-01 08:10:32 +0900 | [diff] [blame] | 90 | if (!(sh_eth_read(port_info, EDTRR) & EDTRR_TRNS)) |
| 91 | sh_eth_write(port_info, EDTRR_TRNS, EDTRR); |
Nobuhiro Iwamatsu | 9751ee0 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 92 | |
| 93 | /* Wait until packet is transmitted */ |
Nobuhiro Iwamatsu | 4ba62c7 | 2012-01-11 10:23:51 +0900 | [diff] [blame] | 94 | timeout = TIMEOUT_CNT; |
Nobuhiro Iwamatsu | 92f0713 | 2013-08-22 13:22:03 +0900 | [diff] [blame] | 95 | do { |
| 96 | invalidate_cache(port_info->tx_desc_cur, |
| 97 | sizeof(struct tx_desc_s)); |
Nobuhiro Iwamatsu | 9751ee0 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 98 | udelay(100); |
Nobuhiro Iwamatsu | 92f0713 | 2013-08-22 13:22:03 +0900 | [diff] [blame] | 99 | } while (port_info->tx_desc_cur->td0 & TD_TACT && timeout--); |
Nobuhiro Iwamatsu | 9751ee0 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 100 | |
| 101 | if (timeout < 0) { |
Nobuhiro Iwamatsu | bd3980c | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 102 | printf(SHETHER_NAME ": transmit timeout\n"); |
| 103 | ret = -ETIMEDOUT; |
Nobuhiro Iwamatsu | 9751ee0 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 104 | goto err; |
| 105 | } |
| 106 | |
Nobuhiro Iwamatsu | 9751ee0 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 107 | port_info->tx_desc_cur++; |
| 108 | if (port_info->tx_desc_cur >= port_info->tx_desc_base + NUM_TX_DESC) |
| 109 | port_info->tx_desc_cur = port_info->tx_desc_base; |
| 110 | |
Nobuhiro Iwamatsu | bd3980c | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 111 | err: |
| 112 | return ret; |
Nobuhiro Iwamatsu | 9751ee0 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 113 | } |
| 114 | |
Nobuhiro Iwamatsu | bd3980c | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 115 | int sh_eth_recv(struct eth_device *dev) |
Nobuhiro Iwamatsu | 9751ee0 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 116 | { |
Nobuhiro Iwamatsu | bd3980c | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 117 | struct sh_eth_dev *eth = dev->priv; |
| 118 | int port = eth->port, len = 0; |
| 119 | struct sh_eth_info *port_info = ð->port_info[port]; |
Joe Hershberger | 10cbe3b | 2012-05-22 18:36:19 +0000 | [diff] [blame] | 120 | uchar *packet; |
Nobuhiro Iwamatsu | 9751ee0 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 121 | |
| 122 | /* Check if the rx descriptor is ready */ |
Nobuhiro Iwamatsu | 92f0713 | 2013-08-22 13:22:03 +0900 | [diff] [blame] | 123 | invalidate_cache(port_info->rx_desc_cur, sizeof(struct rx_desc_s)); |
Nobuhiro Iwamatsu | 9751ee0 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 124 | if (!(port_info->rx_desc_cur->rd0 & RD_RACT)) { |
| 125 | /* Check for errors */ |
| 126 | if (!(port_info->rx_desc_cur->rd0 & RD_RFE)) { |
| 127 | len = port_info->rx_desc_cur->rd1 & 0xffff; |
Joe Hershberger | 10cbe3b | 2012-05-22 18:36:19 +0000 | [diff] [blame] | 128 | packet = (uchar *) |
| 129 | ADDR_TO_P2(port_info->rx_desc_cur->rd2); |
Nobuhiro Iwamatsu | 92f0713 | 2013-08-22 13:22:03 +0900 | [diff] [blame] | 130 | invalidate_cache(packet, len); |
Joe Hershberger | 1fd92db | 2015-04-08 01:41:06 -0500 | [diff] [blame] | 131 | net_process_received_packet(packet, len); |
Nobuhiro Iwamatsu | 9751ee0 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | /* Make current descriptor available again */ |
| 135 | if (port_info->rx_desc_cur->rd0 & RD_RDLE) |
| 136 | port_info->rx_desc_cur->rd0 = RD_RACT | RD_RDLE; |
| 137 | else |
| 138 | port_info->rx_desc_cur->rd0 = RD_RACT; |
Nobuhiro Iwamatsu | f7ca1f7 | 2014-11-04 09:15:48 +0900 | [diff] [blame] | 139 | |
| 140 | flush_cache_wback(port_info->rx_desc_cur, |
| 141 | sizeof(struct rx_desc_s)); |
| 142 | |
Nobuhiro Iwamatsu | 9751ee0 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 143 | /* Point to the next descriptor */ |
| 144 | port_info->rx_desc_cur++; |
| 145 | if (port_info->rx_desc_cur >= |
| 146 | port_info->rx_desc_base + NUM_RX_DESC) |
| 147 | port_info->rx_desc_cur = port_info->rx_desc_base; |
| 148 | } |
| 149 | |
| 150 | /* Restart the receiver if disabled */ |
Nobuhiro Iwamatsu | fbfb511 | 2017-12-01 08:10:32 +0900 | [diff] [blame] | 151 | if (!(sh_eth_read(port_info, EDRRR) & EDRRR_R)) |
| 152 | sh_eth_write(port_info, EDRRR_R, EDRRR); |
Nobuhiro Iwamatsu | 9751ee0 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 153 | |
| 154 | return len; |
| 155 | } |
| 156 | |
Nobuhiro Iwamatsu | bd3980c | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 157 | static int sh_eth_reset(struct sh_eth_dev *eth) |
Nobuhiro Iwamatsu | 9751ee0 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 158 | { |
Nobuhiro Iwamatsu | fbfb511 | 2017-12-01 08:10:32 +0900 | [diff] [blame] | 159 | struct sh_eth_info *port_info = ð->port_info[eth->port]; |
Nobuhiro Iwamatsu | 62cbddc | 2014-01-23 07:52:18 +0900 | [diff] [blame] | 160 | #if defined(SH_ETH_TYPE_GETHER) || defined(SH_ETH_TYPE_RZ) |
Nobuhiro Iwamatsu | bd3980c | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 161 | int ret = 0, i; |
Nobuhiro Iwamatsu | 9751ee0 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 162 | |
| 163 | /* Start e-dmac transmitter and receiver */ |
Nobuhiro Iwamatsu | fbfb511 | 2017-12-01 08:10:32 +0900 | [diff] [blame] | 164 | sh_eth_write(port_info, EDSR_ENALL, EDSR); |
Nobuhiro Iwamatsu | 9751ee0 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 165 | |
| 166 | /* Perform a software reset and wait for it to complete */ |
Nobuhiro Iwamatsu | fbfb511 | 2017-12-01 08:10:32 +0900 | [diff] [blame] | 167 | sh_eth_write(port_info, EDMR_SRST, EDMR); |
Nobuhiro Iwamatsu | e2752db | 2014-01-23 07:52:19 +0900 | [diff] [blame] | 168 | for (i = 0; i < TIMEOUT_CNT; i++) { |
Nobuhiro Iwamatsu | fbfb511 | 2017-12-01 08:10:32 +0900 | [diff] [blame] | 169 | if (!(sh_eth_read(port_info, EDMR) & EDMR_SRST)) |
Nobuhiro Iwamatsu | 9751ee0 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 170 | break; |
| 171 | udelay(1000); |
| 172 | } |
| 173 | |
Nobuhiro Iwamatsu | 4ba62c7 | 2012-01-11 10:23:51 +0900 | [diff] [blame] | 174 | if (i == TIMEOUT_CNT) { |
Nobuhiro Iwamatsu | bd3980c | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 175 | printf(SHETHER_NAME ": Software reset timeout\n"); |
| 176 | ret = -EIO; |
Nobuhiro Iwamatsu | 9751ee0 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 177 | } |
Nobuhiro Iwamatsu | bd3980c | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 178 | |
| 179 | return ret; |
Yoshihiro Shimoda | 903de46 | 2011-01-18 17:53:45 +0900 | [diff] [blame] | 180 | #else |
Nobuhiro Iwamatsu | fbfb511 | 2017-12-01 08:10:32 +0900 | [diff] [blame] | 181 | sh_eth_write(port_info, sh_eth_read(port_info, EDMR) | EDMR_SRST, EDMR); |
Yoshihiro Shimoda | 903de46 | 2011-01-18 17:53:45 +0900 | [diff] [blame] | 182 | udelay(3000); |
Nobuhiro Iwamatsu | fbfb511 | 2017-12-01 08:10:32 +0900 | [diff] [blame] | 183 | sh_eth_write(port_info, |
| 184 | sh_eth_read(port_info, EDMR) & ~EDMR_SRST, EDMR); |
Yoshihiro Shimoda | 903de46 | 2011-01-18 17:53:45 +0900 | [diff] [blame] | 185 | |
| 186 | return 0; |
| 187 | #endif |
Nobuhiro Iwamatsu | 9751ee0 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 188 | } |
| 189 | |
Nobuhiro Iwamatsu | bd3980c | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 190 | static int sh_eth_tx_desc_init(struct sh_eth_dev *eth) |
Nobuhiro Iwamatsu | 9751ee0 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 191 | { |
Nobuhiro Iwamatsu | bd3980c | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 192 | int port = eth->port, i, ret = 0; |
Nobuhiro Iwamatsu | 000889c | 2014-11-04 09:15:47 +0900 | [diff] [blame] | 193 | u32 alloc_desc_size = NUM_TX_DESC * sizeof(struct tx_desc_s); |
Nobuhiro Iwamatsu | bd3980c | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 194 | struct sh_eth_info *port_info = ð->port_info[port]; |
Nobuhiro Iwamatsu | 9751ee0 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 195 | struct tx_desc_s *cur_tx_desc; |
Nobuhiro Iwamatsu | 9751ee0 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 196 | |
Nobuhiro Iwamatsu | bd3980c | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 197 | /* |
Nobuhiro Iwamatsu | 703949e | 2014-11-04 09:15:46 +0900 | [diff] [blame] | 198 | * Allocate rx descriptors. They must be aligned to size of struct |
| 199 | * tx_desc_s. |
Nobuhiro Iwamatsu | bd3980c | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 200 | */ |
Nobuhiro Iwamatsu | 000889c | 2014-11-04 09:15:47 +0900 | [diff] [blame] | 201 | port_info->tx_desc_alloc = |
| 202 | memalign(sizeof(struct tx_desc_s), alloc_desc_size); |
| 203 | if (!port_info->tx_desc_alloc) { |
| 204 | printf(SHETHER_NAME ": memalign failed\n"); |
Nobuhiro Iwamatsu | bd3980c | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 205 | ret = -ENOMEM; |
| 206 | goto err; |
Nobuhiro Iwamatsu | 9751ee0 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 207 | } |
Nobuhiro Iwamatsu | bd3980c | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 208 | |
Nobuhiro Iwamatsu | aae5d23 | 2017-12-01 13:56:08 +0900 | [diff] [blame] | 209 | flush_cache_wback(port_info->tx_desc_alloc, alloc_desc_size); |
Nobuhiro Iwamatsu | 000889c | 2014-11-04 09:15:47 +0900 | [diff] [blame] | 210 | |
Nobuhiro Iwamatsu | 9751ee0 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 211 | /* Make sure we use a P2 address (non-cacheable) */ |
Nobuhiro Iwamatsu | 000889c | 2014-11-04 09:15:47 +0900 | [diff] [blame] | 212 | port_info->tx_desc_base = |
| 213 | (struct tx_desc_s *)ADDR_TO_P2((u32)port_info->tx_desc_alloc); |
Nobuhiro Iwamatsu | 9751ee0 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 214 | port_info->tx_desc_cur = port_info->tx_desc_base; |
| 215 | |
| 216 | /* Initialize all descriptors */ |
| 217 | for (cur_tx_desc = port_info->tx_desc_base, i = 0; i < NUM_TX_DESC; |
| 218 | cur_tx_desc++, i++) { |
| 219 | cur_tx_desc->td0 = 0x00; |
| 220 | cur_tx_desc->td1 = 0x00; |
| 221 | cur_tx_desc->td2 = 0x00; |
| 222 | } |
| 223 | |
| 224 | /* Mark the end of the descriptors */ |
| 225 | cur_tx_desc--; |
| 226 | cur_tx_desc->td0 |= TD_TDLE; |
| 227 | |
Nobuhiro Iwamatsu | dc14867 | 2017-12-01 08:08:00 +0900 | [diff] [blame] | 228 | /* |
| 229 | * Point the controller to the tx descriptor list. Must use physical |
| 230 | * addresses |
| 231 | */ |
Nobuhiro Iwamatsu | fbfb511 | 2017-12-01 08:10:32 +0900 | [diff] [blame] | 232 | sh_eth_write(port_info, ADDR_TO_PHY(port_info->tx_desc_base), TDLAR); |
Nobuhiro Iwamatsu | 62cbddc | 2014-01-23 07:52:18 +0900 | [diff] [blame] | 233 | #if defined(SH_ETH_TYPE_GETHER) || defined(SH_ETH_TYPE_RZ) |
Nobuhiro Iwamatsu | fbfb511 | 2017-12-01 08:10:32 +0900 | [diff] [blame] | 234 | sh_eth_write(port_info, ADDR_TO_PHY(port_info->tx_desc_base), TDFAR); |
| 235 | sh_eth_write(port_info, ADDR_TO_PHY(cur_tx_desc), TDFXR); |
| 236 | sh_eth_write(port_info, 0x01, TDFFR);/* Last discriptor bit */ |
Yoshihiro Shimoda | 903de46 | 2011-01-18 17:53:45 +0900 | [diff] [blame] | 237 | #endif |
Nobuhiro Iwamatsu | 9751ee0 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 238 | |
Nobuhiro Iwamatsu | bd3980c | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 239 | err: |
| 240 | return ret; |
Nobuhiro Iwamatsu | 9751ee0 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 241 | } |
| 242 | |
Nobuhiro Iwamatsu | bd3980c | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 243 | static int sh_eth_rx_desc_init(struct sh_eth_dev *eth) |
Nobuhiro Iwamatsu | 9751ee0 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 244 | { |
Nobuhiro Iwamatsu | dc14867 | 2017-12-01 08:08:00 +0900 | [diff] [blame] | 245 | int port = eth->port, i, ret = 0; |
Nobuhiro Iwamatsu | 000889c | 2014-11-04 09:15:47 +0900 | [diff] [blame] | 246 | u32 alloc_desc_size = NUM_RX_DESC * sizeof(struct rx_desc_s); |
Nobuhiro Iwamatsu | bd3980c | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 247 | struct sh_eth_info *port_info = ð->port_info[port]; |
Nobuhiro Iwamatsu | 9751ee0 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 248 | struct rx_desc_s *cur_rx_desc; |
| 249 | u8 *rx_buf; |
Nobuhiro Iwamatsu | 9751ee0 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 250 | |
Nobuhiro Iwamatsu | bd3980c | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 251 | /* |
Nobuhiro Iwamatsu | 703949e | 2014-11-04 09:15:46 +0900 | [diff] [blame] | 252 | * Allocate rx descriptors. They must be aligned to size of struct |
| 253 | * rx_desc_s. |
Nobuhiro Iwamatsu | bd3980c | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 254 | */ |
Nobuhiro Iwamatsu | 000889c | 2014-11-04 09:15:47 +0900 | [diff] [blame] | 255 | port_info->rx_desc_alloc = |
| 256 | memalign(sizeof(struct rx_desc_s), alloc_desc_size); |
| 257 | if (!port_info->rx_desc_alloc) { |
| 258 | printf(SHETHER_NAME ": memalign failed\n"); |
Nobuhiro Iwamatsu | bd3980c | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 259 | ret = -ENOMEM; |
| 260 | goto err; |
Nobuhiro Iwamatsu | 9751ee0 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 261 | } |
Nobuhiro Iwamatsu | bd3980c | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 262 | |
Nobuhiro Iwamatsu | 000889c | 2014-11-04 09:15:47 +0900 | [diff] [blame] | 263 | flush_cache_wback(port_info->rx_desc_alloc, alloc_desc_size); |
| 264 | |
Nobuhiro Iwamatsu | 9751ee0 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 265 | /* Make sure we use a P2 address (non-cacheable) */ |
Nobuhiro Iwamatsu | 000889c | 2014-11-04 09:15:47 +0900 | [diff] [blame] | 266 | port_info->rx_desc_base = |
| 267 | (struct rx_desc_s *)ADDR_TO_P2((u32)port_info->rx_desc_alloc); |
Nobuhiro Iwamatsu | 9751ee0 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 268 | |
| 269 | port_info->rx_desc_cur = port_info->rx_desc_base; |
| 270 | |
Nobuhiro Iwamatsu | bd3980c | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 271 | /* |
Nobuhiro Iwamatsu | 000889c | 2014-11-04 09:15:47 +0900 | [diff] [blame] | 272 | * Allocate rx data buffers. They must be RX_BUF_ALIGNE_SIZE bytes |
| 273 | * aligned and in P2 area. |
Nobuhiro Iwamatsu | bd3980c | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 274 | */ |
Nobuhiro Iwamatsu | 000889c | 2014-11-04 09:15:47 +0900 | [diff] [blame] | 275 | port_info->rx_buf_alloc = |
| 276 | memalign(RX_BUF_ALIGNE_SIZE, NUM_RX_DESC * MAX_BUF_SIZE); |
| 277 | if (!port_info->rx_buf_alloc) { |
| 278 | printf(SHETHER_NAME ": alloc failed\n"); |
Nobuhiro Iwamatsu | bd3980c | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 279 | ret = -ENOMEM; |
Nobuhiro Iwamatsu | 000889c | 2014-11-04 09:15:47 +0900 | [diff] [blame] | 280 | goto err_buf_alloc; |
Nobuhiro Iwamatsu | 9751ee0 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 281 | } |
Nobuhiro Iwamatsu | bd3980c | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 282 | |
Nobuhiro Iwamatsu | 000889c | 2014-11-04 09:15:47 +0900 | [diff] [blame] | 283 | port_info->rx_buf_base = (u8 *)ADDR_TO_P2((u32)port_info->rx_buf_alloc); |
Nobuhiro Iwamatsu | 9751ee0 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 284 | |
| 285 | /* Initialize all descriptors */ |
| 286 | for (cur_rx_desc = port_info->rx_desc_base, |
| 287 | rx_buf = port_info->rx_buf_base, i = 0; |
| 288 | i < NUM_RX_DESC; cur_rx_desc++, rx_buf += MAX_BUF_SIZE, i++) { |
| 289 | cur_rx_desc->rd0 = RD_RACT; |
| 290 | cur_rx_desc->rd1 = MAX_BUF_SIZE << 16; |
Nobuhiro Iwamatsu | dc14867 | 2017-12-01 08:08:00 +0900 | [diff] [blame] | 291 | cur_rx_desc->rd2 = (u32)ADDR_TO_PHY(rx_buf); |
Nobuhiro Iwamatsu | 9751ee0 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 292 | } |
| 293 | |
| 294 | /* Mark the end of the descriptors */ |
| 295 | cur_rx_desc--; |
| 296 | cur_rx_desc->rd0 |= RD_RDLE; |
| 297 | |
| 298 | /* Point the controller to the rx descriptor list */ |
Nobuhiro Iwamatsu | fbfb511 | 2017-12-01 08:10:32 +0900 | [diff] [blame] | 299 | sh_eth_write(port_info, ADDR_TO_PHY(port_info->rx_desc_base), RDLAR); |
Nobuhiro Iwamatsu | 62cbddc | 2014-01-23 07:52:18 +0900 | [diff] [blame] | 300 | #if defined(SH_ETH_TYPE_GETHER) || defined(SH_ETH_TYPE_RZ) |
Nobuhiro Iwamatsu | fbfb511 | 2017-12-01 08:10:32 +0900 | [diff] [blame] | 301 | sh_eth_write(port_info, ADDR_TO_PHY(port_info->rx_desc_base), RDFAR); |
| 302 | sh_eth_write(port_info, ADDR_TO_PHY(cur_rx_desc), RDFXR); |
| 303 | sh_eth_write(port_info, RDFFR_RDLF, RDFFR); |
Yoshihiro Shimoda | 903de46 | 2011-01-18 17:53:45 +0900 | [diff] [blame] | 304 | #endif |
Nobuhiro Iwamatsu | 9751ee0 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 305 | |
Nobuhiro Iwamatsu | bd3980c | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 306 | return ret; |
| 307 | |
Nobuhiro Iwamatsu | 000889c | 2014-11-04 09:15:47 +0900 | [diff] [blame] | 308 | err_buf_alloc: |
| 309 | free(port_info->rx_desc_alloc); |
| 310 | port_info->rx_desc_alloc = NULL; |
Nobuhiro Iwamatsu | bd3980c | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 311 | |
| 312 | err: |
| 313 | return ret; |
Nobuhiro Iwamatsu | 9751ee0 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 314 | } |
| 315 | |
Nobuhiro Iwamatsu | bd3980c | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 316 | static void sh_eth_tx_desc_free(struct sh_eth_dev *eth) |
Nobuhiro Iwamatsu | 9751ee0 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 317 | { |
Nobuhiro Iwamatsu | bd3980c | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 318 | int port = eth->port; |
| 319 | struct sh_eth_info *port_info = ð->port_info[port]; |
Nobuhiro Iwamatsu | 9751ee0 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 320 | |
Nobuhiro Iwamatsu | 000889c | 2014-11-04 09:15:47 +0900 | [diff] [blame] | 321 | if (port_info->tx_desc_alloc) { |
| 322 | free(port_info->tx_desc_alloc); |
| 323 | port_info->tx_desc_alloc = NULL; |
Nobuhiro Iwamatsu | 9751ee0 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 324 | } |
Nobuhiro Iwamatsu | bd3980c | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 325 | } |
| 326 | |
| 327 | static void sh_eth_rx_desc_free(struct sh_eth_dev *eth) |
| 328 | { |
| 329 | int port = eth->port; |
| 330 | struct sh_eth_info *port_info = ð->port_info[port]; |
Nobuhiro Iwamatsu | 9751ee0 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 331 | |
Nobuhiro Iwamatsu | 000889c | 2014-11-04 09:15:47 +0900 | [diff] [blame] | 332 | if (port_info->rx_desc_alloc) { |
| 333 | free(port_info->rx_desc_alloc); |
| 334 | port_info->rx_desc_alloc = NULL; |
Nobuhiro Iwamatsu | 9751ee0 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 335 | } |
| 336 | |
Nobuhiro Iwamatsu | 000889c | 2014-11-04 09:15:47 +0900 | [diff] [blame] | 337 | if (port_info->rx_buf_alloc) { |
| 338 | free(port_info->rx_buf_alloc); |
| 339 | port_info->rx_buf_alloc = NULL; |
Nobuhiro Iwamatsu | 9751ee0 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 340 | } |
| 341 | } |
| 342 | |
Nobuhiro Iwamatsu | bd3980c | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 343 | static int sh_eth_desc_init(struct sh_eth_dev *eth) |
Nobuhiro Iwamatsu | 9751ee0 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 344 | { |
Nobuhiro Iwamatsu | bd3980c | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 345 | int ret = 0; |
Nobuhiro Iwamatsu | 9751ee0 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 346 | |
Nobuhiro Iwamatsu | bd3980c | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 347 | ret = sh_eth_tx_desc_init(eth); |
| 348 | if (ret) |
| 349 | goto err_tx_init; |
Nobuhiro Iwamatsu | 9751ee0 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 350 | |
Nobuhiro Iwamatsu | bd3980c | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 351 | ret = sh_eth_rx_desc_init(eth); |
| 352 | if (ret) |
| 353 | goto err_rx_init; |
| 354 | |
| 355 | return ret; |
| 356 | err_rx_init: |
| 357 | sh_eth_tx_desc_free(eth); |
| 358 | |
| 359 | err_tx_init: |
| 360 | return ret; |
Nobuhiro Iwamatsu | 9751ee0 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 361 | } |
| 362 | |
Nobuhiro Iwamatsu | bd3980c | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 363 | static int sh_eth_phy_config(struct sh_eth_dev *eth) |
Nobuhiro Iwamatsu | 9751ee0 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 364 | { |
Yoshihiro Shimoda | bd1024b | 2011-10-11 18:10:14 +0900 | [diff] [blame] | 365 | int port = eth->port, ret = 0; |
Nobuhiro Iwamatsu | bd3980c | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 366 | struct sh_eth_info *port_info = ð->port_info[port]; |
Yoshihiro Shimoda | bd1024b | 2011-10-11 18:10:14 +0900 | [diff] [blame] | 367 | struct eth_device *dev = port_info->dev; |
| 368 | struct phy_device *phydev; |
Nobuhiro Iwamatsu | bd3980c | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 369 | |
Nobuhiro Iwamatsu | ee6ec5d | 2012-02-02 21:28:49 +0000 | [diff] [blame] | 370 | phydev = phy_connect( |
| 371 | miiphy_get_dev_by_name(dev->name), |
Nobuhiro Iwamatsu | 4398d55 | 2012-05-15 15:49:39 +0000 | [diff] [blame] | 372 | port_info->phy_addr, dev, CONFIG_SH_ETHER_PHY_MODE); |
Yoshihiro Shimoda | bd1024b | 2011-10-11 18:10:14 +0900 | [diff] [blame] | 373 | port_info->phydev = phydev; |
| 374 | phy_config(phydev); |
Nobuhiro Iwamatsu | bd3980c | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 375 | |
Nobuhiro Iwamatsu | bd3980c | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 376 | return ret; |
Nobuhiro Iwamatsu | 9751ee0 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 377 | } |
| 378 | |
Nobuhiro Iwamatsu | 9b5f9ec | 2017-12-01 08:08:47 +0900 | [diff] [blame] | 379 | static int sh_eth_config(struct sh_eth_dev *eth) |
Nobuhiro Iwamatsu | 9751ee0 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 380 | { |
Nobuhiro Iwamatsu | bd3980c | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 381 | int port = eth->port, ret = 0; |
Yoshihiro Shimoda | bd1024b | 2011-10-11 18:10:14 +0900 | [diff] [blame] | 382 | u32 val; |
Nobuhiro Iwamatsu | bd3980c | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 383 | struct sh_eth_info *port_info = ð->port_info[port]; |
Mike Frysinger | c527ce9 | 2009-02-11 19:14:09 -0500 | [diff] [blame] | 384 | struct eth_device *dev = port_info->dev; |
Yoshihiro Shimoda | bd1024b | 2011-10-11 18:10:14 +0900 | [diff] [blame] | 385 | struct phy_device *phy; |
Nobuhiro Iwamatsu | 9751ee0 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 386 | |
| 387 | /* Configure e-dmac registers */ |
Nobuhiro Iwamatsu | fbfb511 | 2017-12-01 08:10:32 +0900 | [diff] [blame] | 388 | sh_eth_write(port_info, (sh_eth_read(port_info, EDMR) & ~EMDR_DESC_R) | |
Nobuhiro Iwamatsu | f8b7507 | 2013-08-22 13:22:02 +0900 | [diff] [blame] | 389 | (EMDR_DESC | EDMR_EL), EDMR); |
| 390 | |
Nobuhiro Iwamatsu | fbfb511 | 2017-12-01 08:10:32 +0900 | [diff] [blame] | 391 | sh_eth_write(port_info, 0, EESIPR); |
| 392 | sh_eth_write(port_info, 0, TRSCER); |
| 393 | sh_eth_write(port_info, 0, TFTR); |
| 394 | sh_eth_write(port_info, (FIFO_SIZE_T | FIFO_SIZE_R), FDR); |
| 395 | sh_eth_write(port_info, RMCR_RST, RMCR); |
Nobuhiro Iwamatsu | 62cbddc | 2014-01-23 07:52:18 +0900 | [diff] [blame] | 396 | #if defined(SH_ETH_TYPE_GETHER) || defined(SH_ETH_TYPE_RZ) |
Nobuhiro Iwamatsu | fbfb511 | 2017-12-01 08:10:32 +0900 | [diff] [blame] | 397 | sh_eth_write(port_info, 0, RPADIR); |
Yoshihiro Shimoda | 903de46 | 2011-01-18 17:53:45 +0900 | [diff] [blame] | 398 | #endif |
Nobuhiro Iwamatsu | fbfb511 | 2017-12-01 08:10:32 +0900 | [diff] [blame] | 399 | sh_eth_write(port_info, (FIFO_F_D_RFF | FIFO_F_D_RFD), FCFTR); |
Nobuhiro Iwamatsu | 9751ee0 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 400 | |
| 401 | /* Configure e-mac registers */ |
Nobuhiro Iwamatsu | fbfb511 | 2017-12-01 08:10:32 +0900 | [diff] [blame] | 402 | sh_eth_write(port_info, 0, ECSIPR); |
Nobuhiro Iwamatsu | 9751ee0 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 403 | |
| 404 | /* Set Mac address */ |
Mike Frysinger | c527ce9 | 2009-02-11 19:14:09 -0500 | [diff] [blame] | 405 | val = dev->enetaddr[0] << 24 | dev->enetaddr[1] << 16 | |
| 406 | dev->enetaddr[2] << 8 | dev->enetaddr[3]; |
Nobuhiro Iwamatsu | fbfb511 | 2017-12-01 08:10:32 +0900 | [diff] [blame] | 407 | sh_eth_write(port_info, val, MAHR); |
Nobuhiro Iwamatsu | 9751ee0 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 408 | |
Mike Frysinger | c527ce9 | 2009-02-11 19:14:09 -0500 | [diff] [blame] | 409 | val = dev->enetaddr[4] << 8 | dev->enetaddr[5]; |
Nobuhiro Iwamatsu | fbfb511 | 2017-12-01 08:10:32 +0900 | [diff] [blame] | 410 | sh_eth_write(port_info, val, MALR); |
Nobuhiro Iwamatsu | 9751ee0 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 411 | |
Nobuhiro Iwamatsu | fbfb511 | 2017-12-01 08:10:32 +0900 | [diff] [blame] | 412 | sh_eth_write(port_info, RFLR_RFL_MIN, RFLR); |
Yoshihiro Shimoda | 2623509 | 2012-06-26 16:38:06 +0000 | [diff] [blame] | 413 | #if defined(SH_ETH_TYPE_GETHER) |
Nobuhiro Iwamatsu | fbfb511 | 2017-12-01 08:10:32 +0900 | [diff] [blame] | 414 | sh_eth_write(port_info, 0, PIPR); |
Nobuhiro Iwamatsu | 62cbddc | 2014-01-23 07:52:18 +0900 | [diff] [blame] | 415 | #endif |
| 416 | #if defined(SH_ETH_TYPE_GETHER) || defined(SH_ETH_TYPE_RZ) |
Nobuhiro Iwamatsu | fbfb511 | 2017-12-01 08:10:32 +0900 | [diff] [blame] | 417 | sh_eth_write(port_info, APR_AP, APR); |
| 418 | sh_eth_write(port_info, MPR_MP, MPR); |
| 419 | sh_eth_write(port_info, TPAUSER_TPAUSE, TPAUSER); |
Nobuhiro Iwamatsu | 3bb4cc3 | 2011-11-14 16:56:59 +0900 | [diff] [blame] | 420 | #endif |
| 421 | |
Nobuhiro Iwamatsu | dcd5a59 | 2012-08-02 22:08:40 +0000 | [diff] [blame] | 422 | #if defined(CONFIG_CPU_SH7734) || defined(CONFIG_R8A7740) |
Nobuhiro Iwamatsu | fbfb511 | 2017-12-01 08:10:32 +0900 | [diff] [blame] | 423 | sh_eth_write(port_info, CONFIG_SH_ETHER_SH7734_MII, RMII_MII); |
Marek Vasut | effb790 | 2018-01-22 01:42:32 +0100 | [diff] [blame^] | 424 | #elif defined(CONFIG_RCAR_GEN2) |
Nobuhiro Iwamatsu | fbfb511 | 2017-12-01 08:10:32 +0900 | [diff] [blame] | 425 | sh_eth_write(port_info, sh_eth_read(port_info, RMIIMR) | 0x1, RMIIMR); |
Nobuhiro Iwamatsu | 4398d55 | 2012-05-15 15:49:39 +0000 | [diff] [blame] | 426 | #endif |
Nobuhiro Iwamatsu | 9751ee0 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 427 | /* Configure phy */ |
Nobuhiro Iwamatsu | bd3980c | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 428 | ret = sh_eth_phy_config(eth); |
| 429 | if (ret) { |
Nobuhiro Iwamatsu | 88a4c2e | 2009-06-25 16:33:04 +0900 | [diff] [blame] | 430 | printf(SHETHER_NAME ": phy config timeout\n"); |
Nobuhiro Iwamatsu | bd3980c | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 431 | goto err_phy_cfg; |
| 432 | } |
Yoshihiro Shimoda | bd1024b | 2011-10-11 18:10:14 +0900 | [diff] [blame] | 433 | phy = port_info->phydev; |
Timur Tabi | 11af8d6 | 2012-07-09 08:52:43 +0000 | [diff] [blame] | 434 | ret = phy_startup(phy); |
| 435 | if (ret) { |
| 436 | printf(SHETHER_NAME ": phy startup failure\n"); |
| 437 | return ret; |
| 438 | } |
Nobuhiro Iwamatsu | 9751ee0 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 439 | |
Nobuhiro Iwamatsu | 3bb4cc3 | 2011-11-14 16:56:59 +0900 | [diff] [blame] | 440 | val = 0; |
| 441 | |
Nobuhiro Iwamatsu | 9751ee0 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 442 | /* Set the transfer speed */ |
Yoshihiro Shimoda | bd1024b | 2011-10-11 18:10:14 +0900 | [diff] [blame] | 443 | if (phy->speed == 100) { |
Nobuhiro Iwamatsu | bd3980c | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 444 | printf(SHETHER_NAME ": 100Base/"); |
Yoshihiro Shimoda | 2623509 | 2012-06-26 16:38:06 +0000 | [diff] [blame] | 445 | #if defined(SH_ETH_TYPE_GETHER) |
Nobuhiro Iwamatsu | fbfb511 | 2017-12-01 08:10:32 +0900 | [diff] [blame] | 446 | sh_eth_write(port_info, GECMR_100B, GECMR); |
Yoshihiro Shimoda | e3bb325 | 2012-11-04 15:54:30 +0000 | [diff] [blame] | 447 | #elif defined(CONFIG_CPU_SH7757) || defined(CONFIG_CPU_SH7752) |
Nobuhiro Iwamatsu | fbfb511 | 2017-12-01 08:10:32 +0900 | [diff] [blame] | 448 | sh_eth_write(port_info, 1, RTRATE); |
Marek Vasut | effb790 | 2018-01-22 01:42:32 +0100 | [diff] [blame^] | 449 | #elif defined(CONFIG_CPU_SH7724) || defined(CONFIG_RCAR_GEN2) |
Nobuhiro Iwamatsu | 3bb4cc3 | 2011-11-14 16:56:59 +0900 | [diff] [blame] | 450 | val = ECMR_RTM; |
| 451 | #endif |
Yoshihiro Shimoda | bd1024b | 2011-10-11 18:10:14 +0900 | [diff] [blame] | 452 | } else if (phy->speed == 10) { |
Nobuhiro Iwamatsu | bd3980c | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 453 | printf(SHETHER_NAME ": 10Base/"); |
Yoshihiro Shimoda | 2623509 | 2012-06-26 16:38:06 +0000 | [diff] [blame] | 454 | #if defined(SH_ETH_TYPE_GETHER) |
Nobuhiro Iwamatsu | fbfb511 | 2017-12-01 08:10:32 +0900 | [diff] [blame] | 455 | sh_eth_write(port_info, GECMR_10B, GECMR); |
Yoshihiro Shimoda | e3bb325 | 2012-11-04 15:54:30 +0000 | [diff] [blame] | 456 | #elif defined(CONFIG_CPU_SH7757) || defined(CONFIG_CPU_SH7752) |
Nobuhiro Iwamatsu | fbfb511 | 2017-12-01 08:10:32 +0900 | [diff] [blame] | 457 | sh_eth_write(port_info, 0, RTRATE); |
Yoshihiro Shimoda | 903de46 | 2011-01-18 17:53:45 +0900 | [diff] [blame] | 458 | #endif |
Nobuhiro Iwamatsu | 3bb4cc3 | 2011-11-14 16:56:59 +0900 | [diff] [blame] | 459 | } |
Yoshihiro Shimoda | 2623509 | 2012-06-26 16:38:06 +0000 | [diff] [blame] | 460 | #if defined(SH_ETH_TYPE_GETHER) |
Nobuhiro Iwamatsu | 4398d55 | 2012-05-15 15:49:39 +0000 | [diff] [blame] | 461 | else if (phy->speed == 1000) { |
| 462 | printf(SHETHER_NAME ": 1000Base/"); |
Nobuhiro Iwamatsu | fbfb511 | 2017-12-01 08:10:32 +0900 | [diff] [blame] | 463 | sh_eth_write(port_info, GECMR_1000B, GECMR); |
Nobuhiro Iwamatsu | 4398d55 | 2012-05-15 15:49:39 +0000 | [diff] [blame] | 464 | } |
| 465 | #endif |
Nobuhiro Iwamatsu | 9751ee0 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 466 | |
| 467 | /* Check if full duplex mode is supported by the phy */ |
Yoshihiro Shimoda | bd1024b | 2011-10-11 18:10:14 +0900 | [diff] [blame] | 468 | if (phy->duplex) { |
Nobuhiro Iwamatsu | 9751ee0 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 469 | printf("Full\n"); |
Nobuhiro Iwamatsu | fbfb511 | 2017-12-01 08:10:32 +0900 | [diff] [blame] | 470 | sh_eth_write(port_info, |
Nobuhiro Iwamatsu | dc14867 | 2017-12-01 08:08:00 +0900 | [diff] [blame] | 471 | val | (ECMR_CHG_DM | ECMR_RE | ECMR_TE | ECMR_DM), |
Yoshihiro Shimoda | 49afb8c | 2012-06-26 16:38:09 +0000 | [diff] [blame] | 472 | ECMR); |
Nobuhiro Iwamatsu | 9751ee0 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 473 | } else { |
| 474 | printf("Half\n"); |
Nobuhiro Iwamatsu | fbfb511 | 2017-12-01 08:10:32 +0900 | [diff] [blame] | 475 | sh_eth_write(port_info, |
Nobuhiro Iwamatsu | dc14867 | 2017-12-01 08:08:00 +0900 | [diff] [blame] | 476 | val | (ECMR_CHG_DM | ECMR_RE | ECMR_TE), |
| 477 | ECMR); |
Nobuhiro Iwamatsu | 9751ee0 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 478 | } |
Nobuhiro Iwamatsu | bd3980c | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 479 | |
| 480 | return ret; |
| 481 | |
| 482 | err_phy_cfg: |
| 483 | return ret; |
Nobuhiro Iwamatsu | 9751ee0 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 484 | } |
| 485 | |
Nobuhiro Iwamatsu | bd3980c | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 486 | static void sh_eth_start(struct sh_eth_dev *eth) |
Nobuhiro Iwamatsu | 9751ee0 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 487 | { |
Nobuhiro Iwamatsu | fbfb511 | 2017-12-01 08:10:32 +0900 | [diff] [blame] | 488 | struct sh_eth_info *port_info = ð->port_info[eth->port]; |
| 489 | |
Nobuhiro Iwamatsu | 9751ee0 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 490 | /* |
| 491 | * Enable the e-dmac receiver only. The transmitter will be enabled when |
| 492 | * we have something to transmit |
| 493 | */ |
Nobuhiro Iwamatsu | fbfb511 | 2017-12-01 08:10:32 +0900 | [diff] [blame] | 494 | sh_eth_write(port_info, EDRRR_R, EDRRR); |
Nobuhiro Iwamatsu | bd3980c | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 495 | } |
Nobuhiro Iwamatsu | 9751ee0 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 496 | |
Nobuhiro Iwamatsu | bd3980c | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 497 | static void sh_eth_stop(struct sh_eth_dev *eth) |
| 498 | { |
Nobuhiro Iwamatsu | fbfb511 | 2017-12-01 08:10:32 +0900 | [diff] [blame] | 499 | struct sh_eth_info *port_info = ð->port_info[eth->port]; |
| 500 | |
| 501 | sh_eth_write(port_info, ~EDRRR_R, EDRRR); |
Nobuhiro Iwamatsu | 9751ee0 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 502 | } |
| 503 | |
Nobuhiro Iwamatsu | bd3980c | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 504 | int sh_eth_init(struct eth_device *dev, bd_t *bd) |
Nobuhiro Iwamatsu | 9751ee0 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 505 | { |
Nobuhiro Iwamatsu | bd3980c | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 506 | int ret = 0; |
| 507 | struct sh_eth_dev *eth = dev->priv; |
| 508 | |
| 509 | ret = sh_eth_reset(eth); |
| 510 | if (ret) |
| 511 | goto err; |
| 512 | |
| 513 | ret = sh_eth_desc_init(eth); |
| 514 | if (ret) |
| 515 | goto err; |
| 516 | |
Nobuhiro Iwamatsu | 9b5f9ec | 2017-12-01 08:08:47 +0900 | [diff] [blame] | 517 | ret = sh_eth_config(eth); |
Nobuhiro Iwamatsu | bd3980c | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 518 | if (ret) |
| 519 | goto err_config; |
| 520 | |
| 521 | sh_eth_start(eth); |
| 522 | |
| 523 | return ret; |
| 524 | |
| 525 | err_config: |
| 526 | sh_eth_tx_desc_free(eth); |
| 527 | sh_eth_rx_desc_free(eth); |
| 528 | |
| 529 | err: |
| 530 | return ret; |
| 531 | } |
| 532 | |
| 533 | void sh_eth_halt(struct eth_device *dev) |
| 534 | { |
| 535 | struct sh_eth_dev *eth = dev->priv; |
Nobuhiro Iwamatsu | dc14867 | 2017-12-01 08:08:00 +0900 | [diff] [blame] | 536 | |
Nobuhiro Iwamatsu | bd3980c | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 537 | sh_eth_stop(eth); |
| 538 | } |
| 539 | |
| 540 | int sh_eth_initialize(bd_t *bd) |
| 541 | { |
Nobuhiro Iwamatsu | e2752db | 2014-01-23 07:52:19 +0900 | [diff] [blame] | 542 | int ret = 0; |
Nobuhiro Iwamatsu | bd3980c | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 543 | struct sh_eth_dev *eth = NULL; |
Nobuhiro Iwamatsu | e2752db | 2014-01-23 07:52:19 +0900 | [diff] [blame] | 544 | struct eth_device *dev = NULL; |
Nobuhiro Iwamatsu | dc14867 | 2017-12-01 08:08:00 +0900 | [diff] [blame] | 545 | struct mii_dev *mdiodev; |
Nobuhiro Iwamatsu | bd3980c | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 546 | |
Nobuhiro Iwamatsu | e2752db | 2014-01-23 07:52:19 +0900 | [diff] [blame] | 547 | eth = (struct sh_eth_dev *)malloc(sizeof(struct sh_eth_dev)); |
Nobuhiro Iwamatsu | bd3980c | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 548 | if (!eth) { |
| 549 | printf(SHETHER_NAME ": %s: malloc failed\n", __func__); |
| 550 | ret = -ENOMEM; |
| 551 | goto err; |
Nobuhiro Iwamatsu | 9751ee0 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 552 | } |
| 553 | |
Nobuhiro Iwamatsu | e2752db | 2014-01-23 07:52:19 +0900 | [diff] [blame] | 554 | dev = (struct eth_device *)malloc(sizeof(struct eth_device)); |
Nobuhiro Iwamatsu | bd3980c | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 555 | if (!dev) { |
| 556 | printf(SHETHER_NAME ": %s: malloc failed\n", __func__); |
| 557 | ret = -ENOMEM; |
| 558 | goto err; |
| 559 | } |
Nobuhiro Iwamatsu | e2752db | 2014-01-23 07:52:19 +0900 | [diff] [blame] | 560 | memset(dev, 0, sizeof(struct eth_device)); |
| 561 | memset(eth, 0, sizeof(struct sh_eth_dev)); |
Nobuhiro Iwamatsu | 9751ee0 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 562 | |
Nobuhiro Iwamatsu | bd3980c | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 563 | eth->port = CONFIG_SH_ETHER_USE_PORT; |
| 564 | eth->port_info[eth->port].phy_addr = CONFIG_SH_ETHER_PHY_ADDR; |
Nobuhiro Iwamatsu | fbfb511 | 2017-12-01 08:10:32 +0900 | [diff] [blame] | 565 | eth->port_info[eth->port].iobase = |
| 566 | (void __iomem *)(BASE_IO_ADDR + 0x800 * eth->port); |
Nobuhiro Iwamatsu | bd3980c | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 567 | |
Nobuhiro Iwamatsu | e2752db | 2014-01-23 07:52:19 +0900 | [diff] [blame] | 568 | dev->priv = (void *)eth; |
| 569 | dev->iobase = 0; |
| 570 | dev->init = sh_eth_init; |
| 571 | dev->halt = sh_eth_halt; |
| 572 | dev->send = sh_eth_send; |
| 573 | dev->recv = sh_eth_recv; |
| 574 | eth->port_info[eth->port].dev = dev; |
Nobuhiro Iwamatsu | bd3980c | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 575 | |
Ben Whitten | 192bc69 | 2015-12-30 13:05:58 +0000 | [diff] [blame] | 576 | strcpy(dev->name, SHETHER_NAME); |
Nobuhiro Iwamatsu | bd3980c | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 577 | |
Nobuhiro Iwamatsu | e2752db | 2014-01-23 07:52:19 +0900 | [diff] [blame] | 578 | /* Register Device to EtherNet subsystem */ |
| 579 | eth_register(dev); |
Nobuhiro Iwamatsu | 9751ee0 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 580 | |
Yoshihiro Shimoda | bd1024b | 2011-10-11 18:10:14 +0900 | [diff] [blame] | 581 | bb_miiphy_buses[0].priv = eth; |
Nobuhiro Iwamatsu | dc14867 | 2017-12-01 08:08:00 +0900 | [diff] [blame] | 582 | mdiodev = mdio_alloc(); |
Joe Hershberger | 5a49f17 | 2016-08-08 11:28:38 -0500 | [diff] [blame] | 583 | if (!mdiodev) |
| 584 | return -ENOMEM; |
| 585 | strncpy(mdiodev->name, dev->name, MDIO_NAME_LEN); |
| 586 | mdiodev->read = bb_miiphy_read; |
| 587 | mdiodev->write = bb_miiphy_write; |
| 588 | |
Nobuhiro Iwamatsu | dc14867 | 2017-12-01 08:08:00 +0900 | [diff] [blame] | 589 | ret = mdio_register(mdiodev); |
| 590 | if (ret < 0) |
| 591 | return ret; |
Yoshihiro Shimoda | bd1024b | 2011-10-11 18:10:14 +0900 | [diff] [blame] | 592 | |
Simon Glass | 35affd7 | 2017-08-03 12:22:14 -0600 | [diff] [blame] | 593 | if (!eth_env_get_enetaddr("ethaddr", dev->enetaddr)) |
Mike Frysinger | c527ce9 | 2009-02-11 19:14:09 -0500 | [diff] [blame] | 594 | puts("Please set MAC address\n"); |
Nobuhiro Iwamatsu | 9751ee0 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 595 | |
Nobuhiro Iwamatsu | bd3980c | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 596 | return ret; |
Nobuhiro Iwamatsu | 9751ee0 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 597 | |
Nobuhiro Iwamatsu | 9751ee0 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 598 | err: |
Nobuhiro Iwamatsu | bd3980c | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 599 | if (dev) |
| 600 | free(dev); |
| 601 | |
| 602 | if (eth) |
| 603 | free(eth); |
| 604 | |
| 605 | printf(SHETHER_NAME ": Failed\n"); |
| 606 | return ret; |
Nobuhiro Iwamatsu | 9751ee0 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 607 | } |
Yoshihiro Shimoda | bd1024b | 2011-10-11 18:10:14 +0900 | [diff] [blame] | 608 | |
| 609 | /******* for bb_miiphy *******/ |
| 610 | static int sh_eth_bb_init(struct bb_miiphy_bus *bus) |
| 611 | { |
| 612 | return 0; |
| 613 | } |
| 614 | |
| 615 | static int sh_eth_bb_mdio_active(struct bb_miiphy_bus *bus) |
| 616 | { |
| 617 | struct sh_eth_dev *eth = bus->priv; |
Nobuhiro Iwamatsu | fbfb511 | 2017-12-01 08:10:32 +0900 | [diff] [blame] | 618 | struct sh_eth_info *port_info = ð->port_info[eth->port]; |
Yoshihiro Shimoda | bd1024b | 2011-10-11 18:10:14 +0900 | [diff] [blame] | 619 | |
Nobuhiro Iwamatsu | fbfb511 | 2017-12-01 08:10:32 +0900 | [diff] [blame] | 620 | sh_eth_write(port_info, sh_eth_read(port_info, PIR) | PIR_MMD, PIR); |
Yoshihiro Shimoda | bd1024b | 2011-10-11 18:10:14 +0900 | [diff] [blame] | 621 | |
| 622 | return 0; |
| 623 | } |
| 624 | |
| 625 | static int sh_eth_bb_mdio_tristate(struct bb_miiphy_bus *bus) |
| 626 | { |
| 627 | struct sh_eth_dev *eth = bus->priv; |
Nobuhiro Iwamatsu | fbfb511 | 2017-12-01 08:10:32 +0900 | [diff] [blame] | 628 | struct sh_eth_info *port_info = ð->port_info[eth->port]; |
Yoshihiro Shimoda | bd1024b | 2011-10-11 18:10:14 +0900 | [diff] [blame] | 629 | |
Nobuhiro Iwamatsu | fbfb511 | 2017-12-01 08:10:32 +0900 | [diff] [blame] | 630 | sh_eth_write(port_info, sh_eth_read(port_info, PIR) & ~PIR_MMD, PIR); |
Yoshihiro Shimoda | bd1024b | 2011-10-11 18:10:14 +0900 | [diff] [blame] | 631 | |
| 632 | return 0; |
| 633 | } |
| 634 | |
| 635 | static int sh_eth_bb_set_mdio(struct bb_miiphy_bus *bus, int v) |
| 636 | { |
| 637 | struct sh_eth_dev *eth = bus->priv; |
Nobuhiro Iwamatsu | fbfb511 | 2017-12-01 08:10:32 +0900 | [diff] [blame] | 638 | struct sh_eth_info *port_info = ð->port_info[eth->port]; |
Yoshihiro Shimoda | bd1024b | 2011-10-11 18:10:14 +0900 | [diff] [blame] | 639 | |
| 640 | if (v) |
Nobuhiro Iwamatsu | fbfb511 | 2017-12-01 08:10:32 +0900 | [diff] [blame] | 641 | sh_eth_write(port_info, |
| 642 | sh_eth_read(port_info, PIR) | PIR_MDO, PIR); |
Yoshihiro Shimoda | bd1024b | 2011-10-11 18:10:14 +0900 | [diff] [blame] | 643 | else |
Nobuhiro Iwamatsu | fbfb511 | 2017-12-01 08:10:32 +0900 | [diff] [blame] | 644 | sh_eth_write(port_info, |
| 645 | sh_eth_read(port_info, PIR) & ~PIR_MDO, PIR); |
Yoshihiro Shimoda | bd1024b | 2011-10-11 18:10:14 +0900 | [diff] [blame] | 646 | |
| 647 | return 0; |
| 648 | } |
| 649 | |
| 650 | static int sh_eth_bb_get_mdio(struct bb_miiphy_bus *bus, int *v) |
| 651 | { |
| 652 | struct sh_eth_dev *eth = bus->priv; |
Nobuhiro Iwamatsu | fbfb511 | 2017-12-01 08:10:32 +0900 | [diff] [blame] | 653 | struct sh_eth_info *port_info = ð->port_info[eth->port]; |
Yoshihiro Shimoda | bd1024b | 2011-10-11 18:10:14 +0900 | [diff] [blame] | 654 | |
Nobuhiro Iwamatsu | fbfb511 | 2017-12-01 08:10:32 +0900 | [diff] [blame] | 655 | *v = (sh_eth_read(port_info, PIR) & PIR_MDI) >> 3; |
Yoshihiro Shimoda | bd1024b | 2011-10-11 18:10:14 +0900 | [diff] [blame] | 656 | |
| 657 | return 0; |
| 658 | } |
| 659 | |
| 660 | static int sh_eth_bb_set_mdc(struct bb_miiphy_bus *bus, int v) |
| 661 | { |
| 662 | struct sh_eth_dev *eth = bus->priv; |
Nobuhiro Iwamatsu | fbfb511 | 2017-12-01 08:10:32 +0900 | [diff] [blame] | 663 | struct sh_eth_info *port_info = ð->port_info[eth->port]; |
Yoshihiro Shimoda | bd1024b | 2011-10-11 18:10:14 +0900 | [diff] [blame] | 664 | |
| 665 | if (v) |
Nobuhiro Iwamatsu | fbfb511 | 2017-12-01 08:10:32 +0900 | [diff] [blame] | 666 | sh_eth_write(port_info, |
| 667 | sh_eth_read(port_info, PIR) | PIR_MDC, PIR); |
Yoshihiro Shimoda | bd1024b | 2011-10-11 18:10:14 +0900 | [diff] [blame] | 668 | else |
Nobuhiro Iwamatsu | fbfb511 | 2017-12-01 08:10:32 +0900 | [diff] [blame] | 669 | sh_eth_write(port_info, |
| 670 | sh_eth_read(port_info, PIR) & ~PIR_MDC, PIR); |
Yoshihiro Shimoda | bd1024b | 2011-10-11 18:10:14 +0900 | [diff] [blame] | 671 | |
| 672 | return 0; |
| 673 | } |
| 674 | |
| 675 | static int sh_eth_bb_delay(struct bb_miiphy_bus *bus) |
| 676 | { |
| 677 | udelay(10); |
| 678 | |
| 679 | return 0; |
| 680 | } |
| 681 | |
| 682 | struct bb_miiphy_bus bb_miiphy_buses[] = { |
| 683 | { |
| 684 | .name = "sh_eth", |
| 685 | .init = sh_eth_bb_init, |
| 686 | .mdio_active = sh_eth_bb_mdio_active, |
| 687 | .mdio_tristate = sh_eth_bb_mdio_tristate, |
| 688 | .set_mdio = sh_eth_bb_set_mdio, |
| 689 | .get_mdio = sh_eth_bb_get_mdio, |
| 690 | .set_mdc = sh_eth_bb_set_mdc, |
| 691 | .delay = sh_eth_bb_delay, |
| 692 | } |
| 693 | }; |
Nobuhiro Iwamatsu | dc14867 | 2017-12-01 08:08:00 +0900 | [diff] [blame] | 694 | |
Yoshihiro Shimoda | bd1024b | 2011-10-11 18:10:14 +0900 | [diff] [blame] | 695 | int bb_miiphy_buses_num = ARRAY_SIZE(bb_miiphy_buses); |