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 | |
Marek Vasut | 3192026 | 2018-01-19 18:57:17 +0100 | [diff] [blame^] | 21 | #ifdef CONFIG_DM_ETH |
| 22 | #include <clk.h> |
| 23 | #include <dm.h> |
| 24 | #include <linux/mii.h> |
| 25 | #include <asm/gpio.h> |
| 26 | #endif |
| 27 | |
Nobuhiro Iwamatsu | 9751ee0 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 28 | #include "sh_eth.h" |
| 29 | |
| 30 | #ifndef CONFIG_SH_ETHER_USE_PORT |
| 31 | # error "Please define CONFIG_SH_ETHER_USE_PORT" |
| 32 | #endif |
| 33 | #ifndef CONFIG_SH_ETHER_PHY_ADDR |
| 34 | # error "Please define CONFIG_SH_ETHER_PHY_ADDR" |
| 35 | #endif |
Nobuhiro Iwamatsu | 870cc23 | 2013-08-22 13:22:01 +0900 | [diff] [blame] | 36 | |
Nobuhiro Iwamatsu | 92f0713 | 2013-08-22 13:22:03 +0900 | [diff] [blame] | 37 | #if defined(CONFIG_SH_ETHER_CACHE_WRITEBACK) && !defined(CONFIG_SYS_DCACHE_OFF) |
| 38 | #define flush_cache_wback(addr, len) \ |
Nobuhiro Iwamatsu | aae5d23 | 2017-12-01 13:56:08 +0900 | [diff] [blame] | 39 | flush_dcache_range((u32)addr, \ |
| 40 | (u32)(addr + ALIGN(len, CONFIG_SH_ETHER_ALIGNE_SIZE))) |
Yoshihiro Shimoda | 68260aa | 2011-01-27 10:06:08 +0900 | [diff] [blame] | 41 | #else |
| 42 | #define flush_cache_wback(...) |
| 43 | #endif |
Nobuhiro Iwamatsu | 9751ee0 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 44 | |
Nobuhiro Iwamatsu | 92f0713 | 2013-08-22 13:22:03 +0900 | [diff] [blame] | 45 | #if defined(CONFIG_SH_ETHER_CACHE_INVALIDATE) && defined(CONFIG_ARM) |
| 46 | #define invalidate_cache(addr, len) \ |
| 47 | { \ |
| 48 | u32 line_size = CONFIG_SH_ETHER_ALIGNE_SIZE; \ |
| 49 | u32 start, end; \ |
| 50 | \ |
| 51 | start = (u32)addr; \ |
| 52 | end = start + len; \ |
| 53 | start &= ~(line_size - 1); \ |
| 54 | end = ((end + line_size - 1) & ~(line_size - 1)); \ |
| 55 | \ |
| 56 | invalidate_dcache_range(start, end); \ |
| 57 | } |
| 58 | #else |
| 59 | #define invalidate_cache(...) |
| 60 | #endif |
| 61 | |
Nobuhiro Iwamatsu | 4ba62c7 | 2012-01-11 10:23:51 +0900 | [diff] [blame] | 62 | #define TIMEOUT_CNT 1000 |
| 63 | |
Marek Vasut | dca221b | 2018-01-21 14:27:51 +0100 | [diff] [blame] | 64 | static int sh_eth_send_common(struct sh_eth_dev *eth, void *packet, int len) |
Nobuhiro Iwamatsu | 9751ee0 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 65 | { |
Nobuhiro Iwamatsu | bd3980c | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 66 | int port = eth->port, ret = 0, timeout; |
| 67 | struct sh_eth_info *port_info = ð->port_info[port]; |
Nobuhiro Iwamatsu | 9751ee0 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 68 | |
| 69 | if (!packet || len > 0xffff) { |
Nobuhiro Iwamatsu | bd3980c | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 70 | printf(SHETHER_NAME ": %s: Invalid argument\n", __func__); |
| 71 | ret = -EINVAL; |
| 72 | goto err; |
Nobuhiro Iwamatsu | 9751ee0 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | /* packet must be a 4 byte boundary */ |
Nobuhiro Iwamatsu | ee6ec5d | 2012-02-02 21:28:49 +0000 | [diff] [blame] | 76 | if ((int)packet & 3) { |
Nobuhiro Iwamatsu | dc14867 | 2017-12-01 08:08:00 +0900 | [diff] [blame] | 77 | printf(SHETHER_NAME ": %s: packet not 4 byte aligned\n" |
Nobuhiro Iwamatsu | e2752db | 2014-01-23 07:52:19 +0900 | [diff] [blame] | 78 | , __func__); |
Nobuhiro Iwamatsu | bd3980c | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 79 | ret = -EFAULT; |
| 80 | goto err; |
Nobuhiro Iwamatsu | 9751ee0 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | /* Update tx descriptor */ |
Yoshihiro Shimoda | 68260aa | 2011-01-27 10:06:08 +0900 | [diff] [blame] | 84 | flush_cache_wback(packet, len); |
Nobuhiro Iwamatsu | 9751ee0 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 85 | port_info->tx_desc_cur->td2 = ADDR_TO_PHY(packet); |
| 86 | port_info->tx_desc_cur->td1 = len << 16; |
| 87 | /* Must preserve the end of descriptor list indication */ |
| 88 | if (port_info->tx_desc_cur->td0 & TD_TDLE) |
| 89 | port_info->tx_desc_cur->td0 = TD_TACT | TD_TFP | TD_TDLE; |
| 90 | else |
| 91 | port_info->tx_desc_cur->td0 = TD_TACT | TD_TFP; |
| 92 | |
Nobuhiro Iwamatsu | f7ca1f7 | 2014-11-04 09:15:48 +0900 | [diff] [blame] | 93 | flush_cache_wback(port_info->tx_desc_cur, sizeof(struct tx_desc_s)); |
| 94 | |
Nobuhiro Iwamatsu | 9751ee0 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 95 | /* Restart the transmitter if disabled */ |
Nobuhiro Iwamatsu | fbfb511 | 2017-12-01 08:10:32 +0900 | [diff] [blame] | 96 | if (!(sh_eth_read(port_info, EDTRR) & EDTRR_TRNS)) |
| 97 | sh_eth_write(port_info, EDTRR_TRNS, EDTRR); |
Nobuhiro Iwamatsu | 9751ee0 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 98 | |
| 99 | /* Wait until packet is transmitted */ |
Nobuhiro Iwamatsu | 4ba62c7 | 2012-01-11 10:23:51 +0900 | [diff] [blame] | 100 | timeout = TIMEOUT_CNT; |
Nobuhiro Iwamatsu | 92f0713 | 2013-08-22 13:22:03 +0900 | [diff] [blame] | 101 | do { |
| 102 | invalidate_cache(port_info->tx_desc_cur, |
| 103 | sizeof(struct tx_desc_s)); |
Nobuhiro Iwamatsu | 9751ee0 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 104 | udelay(100); |
Nobuhiro Iwamatsu | 92f0713 | 2013-08-22 13:22:03 +0900 | [diff] [blame] | 105 | } while (port_info->tx_desc_cur->td0 & TD_TACT && timeout--); |
Nobuhiro Iwamatsu | 9751ee0 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 106 | |
| 107 | if (timeout < 0) { |
Nobuhiro Iwamatsu | bd3980c | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 108 | printf(SHETHER_NAME ": transmit timeout\n"); |
| 109 | ret = -ETIMEDOUT; |
Nobuhiro Iwamatsu | 9751ee0 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 110 | goto err; |
| 111 | } |
| 112 | |
Nobuhiro Iwamatsu | 9751ee0 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 113 | port_info->tx_desc_cur++; |
| 114 | if (port_info->tx_desc_cur >= port_info->tx_desc_base + NUM_TX_DESC) |
| 115 | port_info->tx_desc_cur = port_info->tx_desc_base; |
| 116 | |
Nobuhiro Iwamatsu | bd3980c | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 117 | err: |
| 118 | return ret; |
Nobuhiro Iwamatsu | 9751ee0 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 119 | } |
| 120 | |
Marek Vasut | 52c15e2 | 2018-01-21 15:39:50 +0100 | [diff] [blame] | 121 | static int sh_eth_recv_start(struct sh_eth_dev *eth) |
| 122 | { |
| 123 | int port = eth->port, len = 0; |
| 124 | struct sh_eth_info *port_info = ð->port_info[port]; |
| 125 | |
| 126 | /* Check if the rx descriptor is ready */ |
| 127 | invalidate_cache(port_info->rx_desc_cur, sizeof(struct rx_desc_s)); |
| 128 | if (port_info->rx_desc_cur->rd0 & RD_RACT) |
| 129 | return -EINVAL; |
| 130 | |
| 131 | /* Check for errors */ |
| 132 | if (port_info->rx_desc_cur->rd0 & RD_RFE) |
| 133 | return -EINVAL; |
| 134 | |
| 135 | len = port_info->rx_desc_cur->rd1 & 0xffff; |
| 136 | |
| 137 | return len; |
| 138 | } |
| 139 | |
| 140 | static void sh_eth_recv_finish(struct sh_eth_dev *eth) |
| 141 | { |
| 142 | struct sh_eth_info *port_info = ð->port_info[eth->port]; |
| 143 | |
| 144 | /* Make current descriptor available again */ |
| 145 | if (port_info->rx_desc_cur->rd0 & RD_RDLE) |
| 146 | port_info->rx_desc_cur->rd0 = RD_RACT | RD_RDLE; |
| 147 | else |
| 148 | port_info->rx_desc_cur->rd0 = RD_RACT; |
| 149 | |
| 150 | flush_cache_wback(port_info->rx_desc_cur, |
| 151 | sizeof(struct rx_desc_s)); |
| 152 | |
| 153 | /* Point to the next descriptor */ |
| 154 | port_info->rx_desc_cur++; |
| 155 | if (port_info->rx_desc_cur >= |
| 156 | port_info->rx_desc_base + NUM_RX_DESC) |
| 157 | port_info->rx_desc_cur = port_info->rx_desc_base; |
| 158 | } |
| 159 | |
Nobuhiro Iwamatsu | bd3980c | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 160 | static int sh_eth_reset(struct sh_eth_dev *eth) |
Nobuhiro Iwamatsu | 9751ee0 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 161 | { |
Nobuhiro Iwamatsu | fbfb511 | 2017-12-01 08:10:32 +0900 | [diff] [blame] | 162 | struct sh_eth_info *port_info = ð->port_info[eth->port]; |
Nobuhiro Iwamatsu | 62cbddc | 2014-01-23 07:52:18 +0900 | [diff] [blame] | 163 | #if defined(SH_ETH_TYPE_GETHER) || defined(SH_ETH_TYPE_RZ) |
Nobuhiro Iwamatsu | bd3980c | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 164 | int ret = 0, i; |
Nobuhiro Iwamatsu | 9751ee0 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 165 | |
| 166 | /* Start e-dmac transmitter and receiver */ |
Nobuhiro Iwamatsu | fbfb511 | 2017-12-01 08:10:32 +0900 | [diff] [blame] | 167 | sh_eth_write(port_info, EDSR_ENALL, EDSR); |
Nobuhiro Iwamatsu | 9751ee0 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 168 | |
| 169 | /* Perform a software reset and wait for it to complete */ |
Nobuhiro Iwamatsu | fbfb511 | 2017-12-01 08:10:32 +0900 | [diff] [blame] | 170 | sh_eth_write(port_info, EDMR_SRST, EDMR); |
Nobuhiro Iwamatsu | e2752db | 2014-01-23 07:52:19 +0900 | [diff] [blame] | 171 | for (i = 0; i < TIMEOUT_CNT; i++) { |
Nobuhiro Iwamatsu | fbfb511 | 2017-12-01 08:10:32 +0900 | [diff] [blame] | 172 | if (!(sh_eth_read(port_info, EDMR) & EDMR_SRST)) |
Nobuhiro Iwamatsu | 9751ee0 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 173 | break; |
| 174 | udelay(1000); |
| 175 | } |
| 176 | |
Nobuhiro Iwamatsu | 4ba62c7 | 2012-01-11 10:23:51 +0900 | [diff] [blame] | 177 | if (i == TIMEOUT_CNT) { |
Nobuhiro Iwamatsu | bd3980c | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 178 | printf(SHETHER_NAME ": Software reset timeout\n"); |
| 179 | ret = -EIO; |
Nobuhiro Iwamatsu | 9751ee0 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 180 | } |
Nobuhiro Iwamatsu | bd3980c | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 181 | |
| 182 | return ret; |
Yoshihiro Shimoda | 903de46 | 2011-01-18 17:53:45 +0900 | [diff] [blame] | 183 | #else |
Nobuhiro Iwamatsu | fbfb511 | 2017-12-01 08:10:32 +0900 | [diff] [blame] | 184 | 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] | 185 | udelay(3000); |
Nobuhiro Iwamatsu | fbfb511 | 2017-12-01 08:10:32 +0900 | [diff] [blame] | 186 | sh_eth_write(port_info, |
| 187 | sh_eth_read(port_info, EDMR) & ~EDMR_SRST, EDMR); |
Yoshihiro Shimoda | 903de46 | 2011-01-18 17:53:45 +0900 | [diff] [blame] | 188 | |
| 189 | return 0; |
| 190 | #endif |
Nobuhiro Iwamatsu | 9751ee0 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 191 | } |
| 192 | |
Nobuhiro Iwamatsu | bd3980c | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 193 | static int sh_eth_tx_desc_init(struct sh_eth_dev *eth) |
Nobuhiro Iwamatsu | 9751ee0 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 194 | { |
Nobuhiro Iwamatsu | bd3980c | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 195 | int port = eth->port, i, ret = 0; |
Nobuhiro Iwamatsu | 000889c | 2014-11-04 09:15:47 +0900 | [diff] [blame] | 196 | u32 alloc_desc_size = NUM_TX_DESC * sizeof(struct tx_desc_s); |
Nobuhiro Iwamatsu | bd3980c | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 197 | struct sh_eth_info *port_info = ð->port_info[port]; |
Nobuhiro Iwamatsu | 9751ee0 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 198 | struct tx_desc_s *cur_tx_desc; |
Nobuhiro Iwamatsu | 9751ee0 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 199 | |
Nobuhiro Iwamatsu | bd3980c | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 200 | /* |
Nobuhiro Iwamatsu | 703949e | 2014-11-04 09:15:46 +0900 | [diff] [blame] | 201 | * Allocate rx descriptors. They must be aligned to size of struct |
| 202 | * tx_desc_s. |
Nobuhiro Iwamatsu | bd3980c | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 203 | */ |
Nobuhiro Iwamatsu | 000889c | 2014-11-04 09:15:47 +0900 | [diff] [blame] | 204 | port_info->tx_desc_alloc = |
| 205 | memalign(sizeof(struct tx_desc_s), alloc_desc_size); |
| 206 | if (!port_info->tx_desc_alloc) { |
| 207 | printf(SHETHER_NAME ": memalign failed\n"); |
Nobuhiro Iwamatsu | bd3980c | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 208 | ret = -ENOMEM; |
| 209 | goto err; |
Nobuhiro Iwamatsu | 9751ee0 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 210 | } |
Nobuhiro Iwamatsu | bd3980c | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 211 | |
Nobuhiro Iwamatsu | aae5d23 | 2017-12-01 13:56:08 +0900 | [diff] [blame] | 212 | flush_cache_wback(port_info->tx_desc_alloc, alloc_desc_size); |
Nobuhiro Iwamatsu | 000889c | 2014-11-04 09:15:47 +0900 | [diff] [blame] | 213 | |
Nobuhiro Iwamatsu | 9751ee0 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 214 | /* Make sure we use a P2 address (non-cacheable) */ |
Nobuhiro Iwamatsu | 000889c | 2014-11-04 09:15:47 +0900 | [diff] [blame] | 215 | port_info->tx_desc_base = |
| 216 | (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] | 217 | port_info->tx_desc_cur = port_info->tx_desc_base; |
| 218 | |
| 219 | /* Initialize all descriptors */ |
| 220 | for (cur_tx_desc = port_info->tx_desc_base, i = 0; i < NUM_TX_DESC; |
| 221 | cur_tx_desc++, i++) { |
| 222 | cur_tx_desc->td0 = 0x00; |
| 223 | cur_tx_desc->td1 = 0x00; |
| 224 | cur_tx_desc->td2 = 0x00; |
| 225 | } |
| 226 | |
| 227 | /* Mark the end of the descriptors */ |
| 228 | cur_tx_desc--; |
| 229 | cur_tx_desc->td0 |= TD_TDLE; |
| 230 | |
Nobuhiro Iwamatsu | dc14867 | 2017-12-01 08:08:00 +0900 | [diff] [blame] | 231 | /* |
| 232 | * Point the controller to the tx descriptor list. Must use physical |
| 233 | * addresses |
| 234 | */ |
Nobuhiro Iwamatsu | fbfb511 | 2017-12-01 08:10:32 +0900 | [diff] [blame] | 235 | 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] | 236 | #if defined(SH_ETH_TYPE_GETHER) || defined(SH_ETH_TYPE_RZ) |
Nobuhiro Iwamatsu | fbfb511 | 2017-12-01 08:10:32 +0900 | [diff] [blame] | 237 | sh_eth_write(port_info, ADDR_TO_PHY(port_info->tx_desc_base), TDFAR); |
| 238 | sh_eth_write(port_info, ADDR_TO_PHY(cur_tx_desc), TDFXR); |
| 239 | sh_eth_write(port_info, 0x01, TDFFR);/* Last discriptor bit */ |
Yoshihiro Shimoda | 903de46 | 2011-01-18 17:53:45 +0900 | [diff] [blame] | 240 | #endif |
Nobuhiro Iwamatsu | 9751ee0 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 241 | |
Nobuhiro Iwamatsu | bd3980c | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 242 | err: |
| 243 | return ret; |
Nobuhiro Iwamatsu | 9751ee0 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 244 | } |
| 245 | |
Nobuhiro Iwamatsu | bd3980c | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 246 | static int sh_eth_rx_desc_init(struct sh_eth_dev *eth) |
Nobuhiro Iwamatsu | 9751ee0 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 247 | { |
Nobuhiro Iwamatsu | dc14867 | 2017-12-01 08:08:00 +0900 | [diff] [blame] | 248 | int port = eth->port, i, ret = 0; |
Nobuhiro Iwamatsu | 000889c | 2014-11-04 09:15:47 +0900 | [diff] [blame] | 249 | u32 alloc_desc_size = NUM_RX_DESC * sizeof(struct rx_desc_s); |
Nobuhiro Iwamatsu | bd3980c | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 250 | struct sh_eth_info *port_info = ð->port_info[port]; |
Nobuhiro Iwamatsu | 9751ee0 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 251 | struct rx_desc_s *cur_rx_desc; |
| 252 | u8 *rx_buf; |
Nobuhiro Iwamatsu | 9751ee0 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 253 | |
Nobuhiro Iwamatsu | bd3980c | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 254 | /* |
Nobuhiro Iwamatsu | 703949e | 2014-11-04 09:15:46 +0900 | [diff] [blame] | 255 | * Allocate rx descriptors. They must be aligned to size of struct |
| 256 | * rx_desc_s. |
Nobuhiro Iwamatsu | bd3980c | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 257 | */ |
Nobuhiro Iwamatsu | 000889c | 2014-11-04 09:15:47 +0900 | [diff] [blame] | 258 | port_info->rx_desc_alloc = |
| 259 | memalign(sizeof(struct rx_desc_s), alloc_desc_size); |
| 260 | if (!port_info->rx_desc_alloc) { |
| 261 | printf(SHETHER_NAME ": memalign failed\n"); |
Nobuhiro Iwamatsu | bd3980c | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 262 | ret = -ENOMEM; |
| 263 | goto err; |
Nobuhiro Iwamatsu | 9751ee0 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 264 | } |
Nobuhiro Iwamatsu | bd3980c | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 265 | |
Nobuhiro Iwamatsu | 000889c | 2014-11-04 09:15:47 +0900 | [diff] [blame] | 266 | flush_cache_wback(port_info->rx_desc_alloc, alloc_desc_size); |
| 267 | |
Nobuhiro Iwamatsu | 9751ee0 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 268 | /* Make sure we use a P2 address (non-cacheable) */ |
Nobuhiro Iwamatsu | 000889c | 2014-11-04 09:15:47 +0900 | [diff] [blame] | 269 | port_info->rx_desc_base = |
| 270 | (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] | 271 | |
| 272 | port_info->rx_desc_cur = port_info->rx_desc_base; |
| 273 | |
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 | * Allocate rx data buffers. They must be RX_BUF_ALIGNE_SIZE bytes |
| 276 | * aligned and in P2 area. |
Nobuhiro Iwamatsu | bd3980c | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 277 | */ |
Nobuhiro Iwamatsu | 000889c | 2014-11-04 09:15:47 +0900 | [diff] [blame] | 278 | port_info->rx_buf_alloc = |
| 279 | memalign(RX_BUF_ALIGNE_SIZE, NUM_RX_DESC * MAX_BUF_SIZE); |
| 280 | if (!port_info->rx_buf_alloc) { |
| 281 | printf(SHETHER_NAME ": alloc failed\n"); |
Nobuhiro Iwamatsu | bd3980c | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 282 | ret = -ENOMEM; |
Nobuhiro Iwamatsu | 000889c | 2014-11-04 09:15:47 +0900 | [diff] [blame] | 283 | goto err_buf_alloc; |
Nobuhiro Iwamatsu | 9751ee0 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 284 | } |
Nobuhiro Iwamatsu | bd3980c | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 285 | |
Nobuhiro Iwamatsu | 000889c | 2014-11-04 09:15:47 +0900 | [diff] [blame] | 286 | 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] | 287 | |
| 288 | /* Initialize all descriptors */ |
| 289 | for (cur_rx_desc = port_info->rx_desc_base, |
| 290 | rx_buf = port_info->rx_buf_base, i = 0; |
| 291 | i < NUM_RX_DESC; cur_rx_desc++, rx_buf += MAX_BUF_SIZE, i++) { |
| 292 | cur_rx_desc->rd0 = RD_RACT; |
| 293 | cur_rx_desc->rd1 = MAX_BUF_SIZE << 16; |
Nobuhiro Iwamatsu | dc14867 | 2017-12-01 08:08:00 +0900 | [diff] [blame] | 294 | cur_rx_desc->rd2 = (u32)ADDR_TO_PHY(rx_buf); |
Nobuhiro Iwamatsu | 9751ee0 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 295 | } |
| 296 | |
| 297 | /* Mark the end of the descriptors */ |
| 298 | cur_rx_desc--; |
| 299 | cur_rx_desc->rd0 |= RD_RDLE; |
| 300 | |
| 301 | /* Point the controller to the rx descriptor list */ |
Nobuhiro Iwamatsu | fbfb511 | 2017-12-01 08:10:32 +0900 | [diff] [blame] | 302 | 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] | 303 | #if defined(SH_ETH_TYPE_GETHER) || defined(SH_ETH_TYPE_RZ) |
Nobuhiro Iwamatsu | fbfb511 | 2017-12-01 08:10:32 +0900 | [diff] [blame] | 304 | sh_eth_write(port_info, ADDR_TO_PHY(port_info->rx_desc_base), RDFAR); |
| 305 | sh_eth_write(port_info, ADDR_TO_PHY(cur_rx_desc), RDFXR); |
| 306 | sh_eth_write(port_info, RDFFR_RDLF, RDFFR); |
Yoshihiro Shimoda | 903de46 | 2011-01-18 17:53:45 +0900 | [diff] [blame] | 307 | #endif |
Nobuhiro Iwamatsu | 9751ee0 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 308 | |
Nobuhiro Iwamatsu | bd3980c | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 309 | return ret; |
| 310 | |
Nobuhiro Iwamatsu | 000889c | 2014-11-04 09:15:47 +0900 | [diff] [blame] | 311 | err_buf_alloc: |
| 312 | free(port_info->rx_desc_alloc); |
| 313 | port_info->rx_desc_alloc = NULL; |
Nobuhiro Iwamatsu | bd3980c | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 314 | |
| 315 | err: |
| 316 | return ret; |
Nobuhiro Iwamatsu | 9751ee0 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 317 | } |
| 318 | |
Nobuhiro Iwamatsu | bd3980c | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 319 | static void sh_eth_tx_desc_free(struct sh_eth_dev *eth) |
Nobuhiro Iwamatsu | 9751ee0 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 320 | { |
Nobuhiro Iwamatsu | bd3980c | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 321 | int port = eth->port; |
| 322 | struct sh_eth_info *port_info = ð->port_info[port]; |
Nobuhiro Iwamatsu | 9751ee0 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 323 | |
Nobuhiro Iwamatsu | 000889c | 2014-11-04 09:15:47 +0900 | [diff] [blame] | 324 | if (port_info->tx_desc_alloc) { |
| 325 | free(port_info->tx_desc_alloc); |
| 326 | port_info->tx_desc_alloc = NULL; |
Nobuhiro Iwamatsu | 9751ee0 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 327 | } |
Nobuhiro Iwamatsu | bd3980c | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 328 | } |
| 329 | |
| 330 | static void sh_eth_rx_desc_free(struct sh_eth_dev *eth) |
| 331 | { |
| 332 | int port = eth->port; |
| 333 | struct sh_eth_info *port_info = ð->port_info[port]; |
Nobuhiro Iwamatsu | 9751ee0 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 334 | |
Nobuhiro Iwamatsu | 000889c | 2014-11-04 09:15:47 +0900 | [diff] [blame] | 335 | if (port_info->rx_desc_alloc) { |
| 336 | free(port_info->rx_desc_alloc); |
| 337 | port_info->rx_desc_alloc = NULL; |
Nobuhiro Iwamatsu | 9751ee0 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 338 | } |
| 339 | |
Nobuhiro Iwamatsu | 000889c | 2014-11-04 09:15:47 +0900 | [diff] [blame] | 340 | if (port_info->rx_buf_alloc) { |
| 341 | free(port_info->rx_buf_alloc); |
| 342 | port_info->rx_buf_alloc = NULL; |
Nobuhiro Iwamatsu | 9751ee0 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 343 | } |
| 344 | } |
| 345 | |
Nobuhiro Iwamatsu | bd3980c | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 346 | static int sh_eth_desc_init(struct sh_eth_dev *eth) |
Nobuhiro Iwamatsu | 9751ee0 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 347 | { |
Nobuhiro Iwamatsu | bd3980c | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 348 | int ret = 0; |
Nobuhiro Iwamatsu | 9751ee0 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 349 | |
Nobuhiro Iwamatsu | bd3980c | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 350 | ret = sh_eth_tx_desc_init(eth); |
| 351 | if (ret) |
| 352 | goto err_tx_init; |
Nobuhiro Iwamatsu | 9751ee0 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 353 | |
Nobuhiro Iwamatsu | bd3980c | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 354 | ret = sh_eth_rx_desc_init(eth); |
| 355 | if (ret) |
| 356 | goto err_rx_init; |
| 357 | |
| 358 | return ret; |
| 359 | err_rx_init: |
| 360 | sh_eth_tx_desc_free(eth); |
| 361 | |
| 362 | err_tx_init: |
| 363 | return ret; |
Nobuhiro Iwamatsu | 9751ee0 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 364 | } |
| 365 | |
Marek Vasut | 68ac92e | 2018-01-21 14:55:44 +0100 | [diff] [blame] | 366 | static void sh_eth_write_hwaddr(struct sh_eth_info *port_info, |
| 367 | unsigned char *mac) |
| 368 | { |
| 369 | u32 val; |
| 370 | |
| 371 | val = (mac[0] << 24) | (mac[1] << 16) | (mac[2] << 8) | mac[3]; |
| 372 | sh_eth_write(port_info, val, MAHR); |
| 373 | |
| 374 | val = (mac[4] << 8) | mac[5]; |
| 375 | sh_eth_write(port_info, val, MALR); |
| 376 | } |
| 377 | |
Marek Vasut | 013af64 | 2018-01-21 15:10:21 +0100 | [diff] [blame] | 378 | static void sh_eth_mac_regs_config(struct sh_eth_dev *eth, unsigned char *mac) |
Nobuhiro Iwamatsu | 9751ee0 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 379 | { |
Marek Vasut | 013af64 | 2018-01-21 15:10:21 +0100 | [diff] [blame] | 380 | struct sh_eth_info *port_info = ð->port_info[eth->port]; |
Nobuhiro Iwamatsu | 9751ee0 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 381 | |
| 382 | /* Configure e-dmac registers */ |
Nobuhiro Iwamatsu | fbfb511 | 2017-12-01 08:10:32 +0900 | [diff] [blame] | 383 | 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] | 384 | (EMDR_DESC | EDMR_EL), EDMR); |
| 385 | |
Nobuhiro Iwamatsu | fbfb511 | 2017-12-01 08:10:32 +0900 | [diff] [blame] | 386 | sh_eth_write(port_info, 0, EESIPR); |
| 387 | sh_eth_write(port_info, 0, TRSCER); |
| 388 | sh_eth_write(port_info, 0, TFTR); |
| 389 | sh_eth_write(port_info, (FIFO_SIZE_T | FIFO_SIZE_R), FDR); |
| 390 | sh_eth_write(port_info, RMCR_RST, RMCR); |
Nobuhiro Iwamatsu | 62cbddc | 2014-01-23 07:52:18 +0900 | [diff] [blame] | 391 | #if defined(SH_ETH_TYPE_GETHER) || defined(SH_ETH_TYPE_RZ) |
Nobuhiro Iwamatsu | fbfb511 | 2017-12-01 08:10:32 +0900 | [diff] [blame] | 392 | sh_eth_write(port_info, 0, RPADIR); |
Yoshihiro Shimoda | 903de46 | 2011-01-18 17:53:45 +0900 | [diff] [blame] | 393 | #endif |
Nobuhiro Iwamatsu | fbfb511 | 2017-12-01 08:10:32 +0900 | [diff] [blame] | 394 | 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] | 395 | |
| 396 | /* Configure e-mac registers */ |
Nobuhiro Iwamatsu | fbfb511 | 2017-12-01 08:10:32 +0900 | [diff] [blame] | 397 | sh_eth_write(port_info, 0, ECSIPR); |
Nobuhiro Iwamatsu | 9751ee0 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 398 | |
| 399 | /* Set Mac address */ |
Marek Vasut | 013af64 | 2018-01-21 15:10:21 +0100 | [diff] [blame] | 400 | sh_eth_write_hwaddr(port_info, mac); |
Nobuhiro Iwamatsu | 9751ee0 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 401 | |
Nobuhiro Iwamatsu | fbfb511 | 2017-12-01 08:10:32 +0900 | [diff] [blame] | 402 | sh_eth_write(port_info, RFLR_RFL_MIN, RFLR); |
Yoshihiro Shimoda | 2623509 | 2012-06-26 16:38:06 +0000 | [diff] [blame] | 403 | #if defined(SH_ETH_TYPE_GETHER) |
Nobuhiro Iwamatsu | fbfb511 | 2017-12-01 08:10:32 +0900 | [diff] [blame] | 404 | sh_eth_write(port_info, 0, PIPR); |
Nobuhiro Iwamatsu | 62cbddc | 2014-01-23 07:52:18 +0900 | [diff] [blame] | 405 | #endif |
| 406 | #if defined(SH_ETH_TYPE_GETHER) || defined(SH_ETH_TYPE_RZ) |
Nobuhiro Iwamatsu | fbfb511 | 2017-12-01 08:10:32 +0900 | [diff] [blame] | 407 | sh_eth_write(port_info, APR_AP, APR); |
| 408 | sh_eth_write(port_info, MPR_MP, MPR); |
| 409 | sh_eth_write(port_info, TPAUSER_TPAUSE, TPAUSER); |
Nobuhiro Iwamatsu | 3bb4cc3 | 2011-11-14 16:56:59 +0900 | [diff] [blame] | 410 | #endif |
| 411 | |
Nobuhiro Iwamatsu | dcd5a59 | 2012-08-02 22:08:40 +0000 | [diff] [blame] | 412 | #if defined(CONFIG_CPU_SH7734) || defined(CONFIG_R8A7740) |
Nobuhiro Iwamatsu | fbfb511 | 2017-12-01 08:10:32 +0900 | [diff] [blame] | 413 | sh_eth_write(port_info, CONFIG_SH_ETHER_SH7734_MII, RMII_MII); |
Marek Vasut | effb790 | 2018-01-22 01:42:32 +0100 | [diff] [blame] | 414 | #elif defined(CONFIG_RCAR_GEN2) |
Nobuhiro Iwamatsu | fbfb511 | 2017-12-01 08:10:32 +0900 | [diff] [blame] | 415 | 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] | 416 | #endif |
Marek Vasut | 013af64 | 2018-01-21 15:10:21 +0100 | [diff] [blame] | 417 | } |
Nobuhiro Iwamatsu | 9751ee0 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 418 | |
Marek Vasut | 013af64 | 2018-01-21 15:10:21 +0100 | [diff] [blame] | 419 | static int sh_eth_phy_regs_config(struct sh_eth_dev *eth) |
| 420 | { |
| 421 | struct sh_eth_info *port_info = ð->port_info[eth->port]; |
| 422 | struct phy_device *phy = port_info->phydev; |
| 423 | int ret = 0; |
| 424 | u32 val = 0; |
Nobuhiro Iwamatsu | 3bb4cc3 | 2011-11-14 16:56:59 +0900 | [diff] [blame] | 425 | |
Nobuhiro Iwamatsu | 9751ee0 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 426 | /* Set the transfer speed */ |
Yoshihiro Shimoda | bd1024b | 2011-10-11 18:10:14 +0900 | [diff] [blame] | 427 | if (phy->speed == 100) { |
Nobuhiro Iwamatsu | bd3980c | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 428 | printf(SHETHER_NAME ": 100Base/"); |
Yoshihiro Shimoda | 2623509 | 2012-06-26 16:38:06 +0000 | [diff] [blame] | 429 | #if defined(SH_ETH_TYPE_GETHER) |
Nobuhiro Iwamatsu | fbfb511 | 2017-12-01 08:10:32 +0900 | [diff] [blame] | 430 | sh_eth_write(port_info, GECMR_100B, GECMR); |
Yoshihiro Shimoda | e3bb325 | 2012-11-04 15:54:30 +0000 | [diff] [blame] | 431 | #elif defined(CONFIG_CPU_SH7757) || defined(CONFIG_CPU_SH7752) |
Nobuhiro Iwamatsu | fbfb511 | 2017-12-01 08:10:32 +0900 | [diff] [blame] | 432 | sh_eth_write(port_info, 1, RTRATE); |
Marek Vasut | effb790 | 2018-01-22 01:42:32 +0100 | [diff] [blame] | 433 | #elif defined(CONFIG_CPU_SH7724) || defined(CONFIG_RCAR_GEN2) |
Nobuhiro Iwamatsu | 3bb4cc3 | 2011-11-14 16:56:59 +0900 | [diff] [blame] | 434 | val = ECMR_RTM; |
| 435 | #endif |
Yoshihiro Shimoda | bd1024b | 2011-10-11 18:10:14 +0900 | [diff] [blame] | 436 | } else if (phy->speed == 10) { |
Nobuhiro Iwamatsu | bd3980c | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 437 | printf(SHETHER_NAME ": 10Base/"); |
Yoshihiro Shimoda | 2623509 | 2012-06-26 16:38:06 +0000 | [diff] [blame] | 438 | #if defined(SH_ETH_TYPE_GETHER) |
Nobuhiro Iwamatsu | fbfb511 | 2017-12-01 08:10:32 +0900 | [diff] [blame] | 439 | sh_eth_write(port_info, GECMR_10B, GECMR); |
Yoshihiro Shimoda | e3bb325 | 2012-11-04 15:54:30 +0000 | [diff] [blame] | 440 | #elif defined(CONFIG_CPU_SH7757) || defined(CONFIG_CPU_SH7752) |
Nobuhiro Iwamatsu | fbfb511 | 2017-12-01 08:10:32 +0900 | [diff] [blame] | 441 | sh_eth_write(port_info, 0, RTRATE); |
Yoshihiro Shimoda | 903de46 | 2011-01-18 17:53:45 +0900 | [diff] [blame] | 442 | #endif |
Nobuhiro Iwamatsu | 3bb4cc3 | 2011-11-14 16:56:59 +0900 | [diff] [blame] | 443 | } |
Yoshihiro Shimoda | 2623509 | 2012-06-26 16:38:06 +0000 | [diff] [blame] | 444 | #if defined(SH_ETH_TYPE_GETHER) |
Nobuhiro Iwamatsu | 4398d55 | 2012-05-15 15:49:39 +0000 | [diff] [blame] | 445 | else if (phy->speed == 1000) { |
| 446 | printf(SHETHER_NAME ": 1000Base/"); |
Nobuhiro Iwamatsu | fbfb511 | 2017-12-01 08:10:32 +0900 | [diff] [blame] | 447 | sh_eth_write(port_info, GECMR_1000B, GECMR); |
Nobuhiro Iwamatsu | 4398d55 | 2012-05-15 15:49:39 +0000 | [diff] [blame] | 448 | } |
| 449 | #endif |
Nobuhiro Iwamatsu | 9751ee0 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 450 | |
| 451 | /* Check if full duplex mode is supported by the phy */ |
Yoshihiro Shimoda | bd1024b | 2011-10-11 18:10:14 +0900 | [diff] [blame] | 452 | if (phy->duplex) { |
Nobuhiro Iwamatsu | 9751ee0 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 453 | printf("Full\n"); |
Nobuhiro Iwamatsu | fbfb511 | 2017-12-01 08:10:32 +0900 | [diff] [blame] | 454 | sh_eth_write(port_info, |
Nobuhiro Iwamatsu | dc14867 | 2017-12-01 08:08:00 +0900 | [diff] [blame] | 455 | val | (ECMR_CHG_DM | ECMR_RE | ECMR_TE | ECMR_DM), |
Yoshihiro Shimoda | 49afb8c | 2012-06-26 16:38:09 +0000 | [diff] [blame] | 456 | ECMR); |
Nobuhiro Iwamatsu | 9751ee0 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 457 | } else { |
| 458 | printf("Half\n"); |
Nobuhiro Iwamatsu | fbfb511 | 2017-12-01 08:10:32 +0900 | [diff] [blame] | 459 | sh_eth_write(port_info, |
Nobuhiro Iwamatsu | dc14867 | 2017-12-01 08:08:00 +0900 | [diff] [blame] | 460 | val | (ECMR_CHG_DM | ECMR_RE | ECMR_TE), |
| 461 | ECMR); |
Nobuhiro Iwamatsu | 9751ee0 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 462 | } |
Nobuhiro Iwamatsu | bd3980c | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 463 | |
| 464 | return ret; |
Nobuhiro Iwamatsu | 9751ee0 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 465 | } |
| 466 | |
Nobuhiro Iwamatsu | bd3980c | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 467 | static void sh_eth_start(struct sh_eth_dev *eth) |
Nobuhiro Iwamatsu | 9751ee0 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 468 | { |
Nobuhiro Iwamatsu | fbfb511 | 2017-12-01 08:10:32 +0900 | [diff] [blame] | 469 | struct sh_eth_info *port_info = ð->port_info[eth->port]; |
| 470 | |
Nobuhiro Iwamatsu | 9751ee0 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 471 | /* |
| 472 | * Enable the e-dmac receiver only. The transmitter will be enabled when |
| 473 | * we have something to transmit |
| 474 | */ |
Nobuhiro Iwamatsu | fbfb511 | 2017-12-01 08:10:32 +0900 | [diff] [blame] | 475 | sh_eth_write(port_info, EDRRR_R, EDRRR); |
Nobuhiro Iwamatsu | bd3980c | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 476 | } |
Nobuhiro Iwamatsu | 9751ee0 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 477 | |
Nobuhiro Iwamatsu | bd3980c | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 478 | static void sh_eth_stop(struct sh_eth_dev *eth) |
| 479 | { |
Nobuhiro Iwamatsu | fbfb511 | 2017-12-01 08:10:32 +0900 | [diff] [blame] | 480 | struct sh_eth_info *port_info = ð->port_info[eth->port]; |
| 481 | |
| 482 | sh_eth_write(port_info, ~EDRRR_R, EDRRR); |
Nobuhiro Iwamatsu | 9751ee0 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 483 | } |
| 484 | |
Marek Vasut | 013af64 | 2018-01-21 15:10:21 +0100 | [diff] [blame] | 485 | static int sh_eth_init_common(struct sh_eth_dev *eth, unsigned char *mac) |
Nobuhiro Iwamatsu | 9751ee0 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 486 | { |
Nobuhiro Iwamatsu | bd3980c | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 487 | int ret = 0; |
Nobuhiro Iwamatsu | bd3980c | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 488 | |
| 489 | ret = sh_eth_reset(eth); |
| 490 | if (ret) |
Marek Vasut | 013af64 | 2018-01-21 15:10:21 +0100 | [diff] [blame] | 491 | return ret; |
Nobuhiro Iwamatsu | bd3980c | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 492 | |
| 493 | ret = sh_eth_desc_init(eth); |
| 494 | if (ret) |
Marek Vasut | 013af64 | 2018-01-21 15:10:21 +0100 | [diff] [blame] | 495 | return ret; |
Nobuhiro Iwamatsu | bd3980c | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 496 | |
Marek Vasut | 013af64 | 2018-01-21 15:10:21 +0100 | [diff] [blame] | 497 | sh_eth_mac_regs_config(eth, mac); |
| 498 | |
| 499 | return 0; |
| 500 | } |
| 501 | |
| 502 | static int sh_eth_start_common(struct sh_eth_dev *eth) |
| 503 | { |
| 504 | struct sh_eth_info *port_info = ð->port_info[eth->port]; |
| 505 | int ret; |
| 506 | |
| 507 | ret = phy_startup(port_info->phydev); |
| 508 | if (ret) { |
| 509 | printf(SHETHER_NAME ": phy startup failure\n"); |
| 510 | return ret; |
| 511 | } |
| 512 | |
| 513 | ret = sh_eth_phy_regs_config(eth); |
Nobuhiro Iwamatsu | bd3980c | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 514 | if (ret) |
Marek Vasut | 013af64 | 2018-01-21 15:10:21 +0100 | [diff] [blame] | 515 | return ret; |
Nobuhiro Iwamatsu | bd3980c | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 516 | |
| 517 | sh_eth_start(eth); |
| 518 | |
Marek Vasut | 013af64 | 2018-01-21 15:10:21 +0100 | [diff] [blame] | 519 | return 0; |
| 520 | } |
Nobuhiro Iwamatsu | bd3980c | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 521 | |
Marek Vasut | 3192026 | 2018-01-19 18:57:17 +0100 | [diff] [blame^] | 522 | #ifndef CONFIG_DM_ETH |
Marek Vasut | a220784 | 2018-01-21 15:31:48 +0100 | [diff] [blame] | 523 | static int sh_eth_phy_config_legacy(struct sh_eth_dev *eth) |
| 524 | { |
| 525 | int port = eth->port, ret = 0; |
| 526 | struct sh_eth_info *port_info = ð->port_info[port]; |
| 527 | struct eth_device *dev = port_info->dev; |
| 528 | struct phy_device *phydev; |
| 529 | |
| 530 | phydev = phy_connect( |
| 531 | miiphy_get_dev_by_name(dev->name), |
| 532 | port_info->phy_addr, dev, CONFIG_SH_ETHER_PHY_MODE); |
| 533 | port_info->phydev = phydev; |
| 534 | phy_config(phydev); |
| 535 | |
| 536 | return ret; |
| 537 | } |
| 538 | |
| 539 | static int sh_eth_send_legacy(struct eth_device *dev, void *packet, int len) |
| 540 | { |
| 541 | struct sh_eth_dev *eth = dev->priv; |
| 542 | |
| 543 | return sh_eth_send_common(eth, packet, len); |
| 544 | } |
| 545 | |
| 546 | static int sh_eth_recv_common(struct sh_eth_dev *eth) |
| 547 | { |
| 548 | int port = eth->port, len = 0; |
| 549 | struct sh_eth_info *port_info = ð->port_info[port]; |
| 550 | uchar *packet = (uchar *)ADDR_TO_P2(port_info->rx_desc_cur->rd2); |
| 551 | |
| 552 | len = sh_eth_recv_start(eth); |
| 553 | if (len > 0) { |
| 554 | invalidate_cache(packet, len); |
| 555 | net_process_received_packet(packet, len); |
| 556 | sh_eth_recv_finish(eth); |
| 557 | } else |
| 558 | len = 0; |
| 559 | |
| 560 | /* Restart the receiver if disabled */ |
| 561 | if (!(sh_eth_read(port_info, EDRRR) & EDRRR_R)) |
| 562 | sh_eth_write(port_info, EDRRR_R, EDRRR); |
| 563 | |
| 564 | return len; |
| 565 | } |
| 566 | |
| 567 | static int sh_eth_recv_legacy(struct eth_device *dev) |
| 568 | { |
| 569 | struct sh_eth_dev *eth = dev->priv; |
| 570 | |
| 571 | return sh_eth_recv_common(eth); |
| 572 | } |
| 573 | |
Marek Vasut | 013af64 | 2018-01-21 15:10:21 +0100 | [diff] [blame] | 574 | static int sh_eth_init_legacy(struct eth_device *dev, bd_t *bd) |
| 575 | { |
| 576 | struct sh_eth_dev *eth = dev->priv; |
| 577 | int ret; |
| 578 | |
| 579 | ret = sh_eth_init_common(eth, dev->enetaddr); |
| 580 | if (ret) |
| 581 | return ret; |
| 582 | |
| 583 | ret = sh_eth_phy_config_legacy(eth); |
| 584 | if (ret) { |
| 585 | printf(SHETHER_NAME ": phy config timeout\n"); |
| 586 | goto err_start; |
| 587 | } |
| 588 | |
| 589 | ret = sh_eth_start_common(eth); |
| 590 | if (ret) |
| 591 | goto err_start; |
| 592 | |
| 593 | return 0; |
| 594 | |
| 595 | err_start: |
Nobuhiro Iwamatsu | bd3980c | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 596 | sh_eth_tx_desc_free(eth); |
| 597 | sh_eth_rx_desc_free(eth); |
Nobuhiro Iwamatsu | bd3980c | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 598 | return ret; |
| 599 | } |
| 600 | |
Marek Vasut | 013af64 | 2018-01-21 15:10:21 +0100 | [diff] [blame] | 601 | void sh_eth_halt_legacy(struct eth_device *dev) |
Nobuhiro Iwamatsu | bd3980c | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 602 | { |
| 603 | struct sh_eth_dev *eth = dev->priv; |
Nobuhiro Iwamatsu | dc14867 | 2017-12-01 08:08:00 +0900 | [diff] [blame] | 604 | |
Nobuhiro Iwamatsu | bd3980c | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 605 | sh_eth_stop(eth); |
| 606 | } |
| 607 | |
| 608 | int sh_eth_initialize(bd_t *bd) |
| 609 | { |
Nobuhiro Iwamatsu | e2752db | 2014-01-23 07:52:19 +0900 | [diff] [blame] | 610 | int ret = 0; |
Nobuhiro Iwamatsu | bd3980c | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 611 | struct sh_eth_dev *eth = NULL; |
Nobuhiro Iwamatsu | e2752db | 2014-01-23 07:52:19 +0900 | [diff] [blame] | 612 | struct eth_device *dev = NULL; |
Nobuhiro Iwamatsu | dc14867 | 2017-12-01 08:08:00 +0900 | [diff] [blame] | 613 | struct mii_dev *mdiodev; |
Nobuhiro Iwamatsu | bd3980c | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 614 | |
Nobuhiro Iwamatsu | e2752db | 2014-01-23 07:52:19 +0900 | [diff] [blame] | 615 | eth = (struct sh_eth_dev *)malloc(sizeof(struct sh_eth_dev)); |
Nobuhiro Iwamatsu | bd3980c | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 616 | if (!eth) { |
| 617 | printf(SHETHER_NAME ": %s: malloc failed\n", __func__); |
| 618 | ret = -ENOMEM; |
| 619 | goto err; |
Nobuhiro Iwamatsu | 9751ee0 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 620 | } |
| 621 | |
Nobuhiro Iwamatsu | e2752db | 2014-01-23 07:52:19 +0900 | [diff] [blame] | 622 | dev = (struct eth_device *)malloc(sizeof(struct eth_device)); |
Nobuhiro Iwamatsu | bd3980c | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 623 | if (!dev) { |
| 624 | printf(SHETHER_NAME ": %s: malloc failed\n", __func__); |
| 625 | ret = -ENOMEM; |
| 626 | goto err; |
| 627 | } |
Nobuhiro Iwamatsu | e2752db | 2014-01-23 07:52:19 +0900 | [diff] [blame] | 628 | memset(dev, 0, sizeof(struct eth_device)); |
| 629 | memset(eth, 0, sizeof(struct sh_eth_dev)); |
Nobuhiro Iwamatsu | 9751ee0 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 630 | |
Nobuhiro Iwamatsu | bd3980c | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 631 | eth->port = CONFIG_SH_ETHER_USE_PORT; |
| 632 | eth->port_info[eth->port].phy_addr = CONFIG_SH_ETHER_PHY_ADDR; |
Nobuhiro Iwamatsu | fbfb511 | 2017-12-01 08:10:32 +0900 | [diff] [blame] | 633 | eth->port_info[eth->port].iobase = |
| 634 | (void __iomem *)(BASE_IO_ADDR + 0x800 * eth->port); |
Nobuhiro Iwamatsu | bd3980c | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 635 | |
Nobuhiro Iwamatsu | e2752db | 2014-01-23 07:52:19 +0900 | [diff] [blame] | 636 | dev->priv = (void *)eth; |
| 637 | dev->iobase = 0; |
Marek Vasut | 013af64 | 2018-01-21 15:10:21 +0100 | [diff] [blame] | 638 | dev->init = sh_eth_init_legacy; |
| 639 | dev->halt = sh_eth_halt_legacy; |
Marek Vasut | dca221b | 2018-01-21 14:27:51 +0100 | [diff] [blame] | 640 | dev->send = sh_eth_send_legacy; |
| 641 | dev->recv = sh_eth_recv_legacy; |
Nobuhiro Iwamatsu | e2752db | 2014-01-23 07:52:19 +0900 | [diff] [blame] | 642 | eth->port_info[eth->port].dev = dev; |
Nobuhiro Iwamatsu | bd3980c | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 643 | |
Ben Whitten | 192bc69 | 2015-12-30 13:05:58 +0000 | [diff] [blame] | 644 | strcpy(dev->name, SHETHER_NAME); |
Nobuhiro Iwamatsu | bd3980c | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 645 | |
Nobuhiro Iwamatsu | e2752db | 2014-01-23 07:52:19 +0900 | [diff] [blame] | 646 | /* Register Device to EtherNet subsystem */ |
| 647 | eth_register(dev); |
Nobuhiro Iwamatsu | 9751ee0 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 648 | |
Yoshihiro Shimoda | bd1024b | 2011-10-11 18:10:14 +0900 | [diff] [blame] | 649 | bb_miiphy_buses[0].priv = eth; |
Nobuhiro Iwamatsu | dc14867 | 2017-12-01 08:08:00 +0900 | [diff] [blame] | 650 | mdiodev = mdio_alloc(); |
Joe Hershberger | 5a49f17 | 2016-08-08 11:28:38 -0500 | [diff] [blame] | 651 | if (!mdiodev) |
| 652 | return -ENOMEM; |
| 653 | strncpy(mdiodev->name, dev->name, MDIO_NAME_LEN); |
| 654 | mdiodev->read = bb_miiphy_read; |
| 655 | mdiodev->write = bb_miiphy_write; |
| 656 | |
Nobuhiro Iwamatsu | dc14867 | 2017-12-01 08:08:00 +0900 | [diff] [blame] | 657 | ret = mdio_register(mdiodev); |
| 658 | if (ret < 0) |
| 659 | return ret; |
Yoshihiro Shimoda | bd1024b | 2011-10-11 18:10:14 +0900 | [diff] [blame] | 660 | |
Simon Glass | 35affd7 | 2017-08-03 12:22:14 -0600 | [diff] [blame] | 661 | if (!eth_env_get_enetaddr("ethaddr", dev->enetaddr)) |
Mike Frysinger | c527ce9 | 2009-02-11 19:14:09 -0500 | [diff] [blame] | 662 | puts("Please set MAC address\n"); |
Nobuhiro Iwamatsu | 9751ee0 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 663 | |
Nobuhiro Iwamatsu | bd3980c | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 664 | return ret; |
Nobuhiro Iwamatsu | 9751ee0 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 665 | |
Nobuhiro Iwamatsu | 9751ee0 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 666 | err: |
Nobuhiro Iwamatsu | bd3980c | 2008-11-21 12:04:18 +0900 | [diff] [blame] | 667 | if (dev) |
| 668 | free(dev); |
| 669 | |
| 670 | if (eth) |
| 671 | free(eth); |
| 672 | |
| 673 | printf(SHETHER_NAME ": Failed\n"); |
| 674 | return ret; |
Nobuhiro Iwamatsu | 9751ee0 | 2008-06-11 21:05:00 +0900 | [diff] [blame] | 675 | } |
Yoshihiro Shimoda | bd1024b | 2011-10-11 18:10:14 +0900 | [diff] [blame] | 676 | |
Marek Vasut | 3192026 | 2018-01-19 18:57:17 +0100 | [diff] [blame^] | 677 | #else /* CONFIG_DM_ETH */ |
| 678 | |
| 679 | struct sh_ether_priv { |
| 680 | struct sh_eth_dev shdev; |
| 681 | |
| 682 | struct mii_dev *bus; |
| 683 | void __iomem *iobase; |
| 684 | struct clk clk; |
| 685 | struct gpio_desc reset_gpio; |
| 686 | }; |
| 687 | |
| 688 | static int sh_ether_send(struct udevice *dev, void *packet, int len) |
| 689 | { |
| 690 | struct sh_ether_priv *priv = dev_get_priv(dev); |
| 691 | struct sh_eth_dev *eth = &priv->shdev; |
| 692 | |
| 693 | return sh_eth_send_common(eth, packet, len); |
| 694 | } |
| 695 | |
| 696 | static int sh_ether_recv(struct udevice *dev, int flags, uchar **packetp) |
| 697 | { |
| 698 | struct sh_ether_priv *priv = dev_get_priv(dev); |
| 699 | struct sh_eth_dev *eth = &priv->shdev; |
| 700 | struct sh_eth_info *port_info = ð->port_info[eth->port]; |
| 701 | uchar *packet = (uchar *)ADDR_TO_P2(port_info->rx_desc_cur->rd2); |
| 702 | int len; |
| 703 | |
| 704 | len = sh_eth_recv_start(eth); |
| 705 | if (len > 0) { |
| 706 | invalidate_cache(packet, len); |
| 707 | *packetp = packet; |
| 708 | |
| 709 | return len; |
| 710 | } else { |
| 711 | len = 0; |
| 712 | |
| 713 | /* Restart the receiver if disabled */ |
| 714 | if (!(sh_eth_read(port_info, EDRRR) & EDRRR_R)) |
| 715 | sh_eth_write(port_info, EDRRR_R, EDRRR); |
| 716 | |
| 717 | return -EAGAIN; |
| 718 | } |
| 719 | } |
| 720 | |
| 721 | static int sh_ether_free_pkt(struct udevice *dev, uchar *packet, int length) |
| 722 | { |
| 723 | struct sh_ether_priv *priv = dev_get_priv(dev); |
| 724 | struct sh_eth_dev *eth = &priv->shdev; |
| 725 | struct sh_eth_info *port_info = ð->port_info[eth->port]; |
| 726 | |
| 727 | sh_eth_recv_finish(eth); |
| 728 | |
| 729 | /* Restart the receiver if disabled */ |
| 730 | if (!(sh_eth_read(port_info, EDRRR) & EDRRR_R)) |
| 731 | sh_eth_write(port_info, EDRRR_R, EDRRR); |
| 732 | |
| 733 | return 0; |
| 734 | } |
| 735 | |
| 736 | static int sh_ether_write_hwaddr(struct udevice *dev) |
| 737 | { |
| 738 | struct sh_ether_priv *priv = dev_get_priv(dev); |
| 739 | struct sh_eth_dev *eth = &priv->shdev; |
| 740 | struct sh_eth_info *port_info = ð->port_info[eth->port]; |
| 741 | struct eth_pdata *pdata = dev_get_platdata(dev); |
| 742 | |
| 743 | sh_eth_write_hwaddr(port_info, pdata->enetaddr); |
| 744 | |
| 745 | return 0; |
| 746 | } |
| 747 | |
| 748 | static int sh_eth_phy_config(struct udevice *dev) |
| 749 | { |
| 750 | struct sh_ether_priv *priv = dev_get_priv(dev); |
| 751 | struct eth_pdata *pdata = dev_get_platdata(dev); |
| 752 | struct sh_eth_dev *eth = &priv->shdev; |
| 753 | int port = eth->port, ret = 0; |
| 754 | struct sh_eth_info *port_info = ð->port_info[port]; |
| 755 | struct phy_device *phydev; |
| 756 | int mask = 0xffffffff; |
| 757 | |
| 758 | phydev = phy_find_by_mask(priv->bus, mask, pdata->phy_interface); |
| 759 | if (!phydev) |
| 760 | return -ENODEV; |
| 761 | |
| 762 | phy_connect_dev(phydev, dev); |
| 763 | |
| 764 | port_info->phydev = phydev; |
| 765 | phy_config(phydev); |
| 766 | |
| 767 | return ret; |
| 768 | } |
| 769 | |
| 770 | static int sh_ether_start(struct udevice *dev) |
| 771 | { |
| 772 | struct sh_ether_priv *priv = dev_get_priv(dev); |
| 773 | struct eth_pdata *pdata = dev_get_platdata(dev); |
| 774 | struct sh_eth_dev *eth = &priv->shdev; |
| 775 | int ret; |
| 776 | |
| 777 | ret = clk_enable(&priv->clk); |
| 778 | if (ret) |
| 779 | return ret; |
| 780 | |
| 781 | ret = sh_eth_init_common(eth, pdata->enetaddr); |
| 782 | if (ret) |
| 783 | goto err_clk; |
| 784 | |
| 785 | ret = sh_eth_phy_config(dev); |
| 786 | if (ret) { |
| 787 | printf(SHETHER_NAME ": phy config timeout\n"); |
| 788 | goto err_start; |
| 789 | } |
| 790 | |
| 791 | ret = sh_eth_start_common(eth); |
| 792 | if (ret) |
| 793 | goto err_start; |
| 794 | |
| 795 | return 0; |
| 796 | |
| 797 | err_start: |
| 798 | sh_eth_tx_desc_free(eth); |
| 799 | sh_eth_rx_desc_free(eth); |
| 800 | err_clk: |
| 801 | clk_disable(&priv->clk); |
| 802 | return ret; |
| 803 | } |
| 804 | |
| 805 | static void sh_ether_stop(struct udevice *dev) |
| 806 | { |
| 807 | struct sh_ether_priv *priv = dev_get_priv(dev); |
| 808 | |
| 809 | sh_eth_stop(&priv->shdev); |
| 810 | clk_disable(&priv->clk); |
| 811 | } |
| 812 | |
| 813 | static int sh_ether_probe(struct udevice *udev) |
| 814 | { |
| 815 | struct eth_pdata *pdata = dev_get_platdata(udev); |
| 816 | struct sh_ether_priv *priv = dev_get_priv(udev); |
| 817 | struct sh_eth_dev *eth = &priv->shdev; |
| 818 | struct mii_dev *mdiodev; |
| 819 | void __iomem *iobase; |
| 820 | int ret; |
| 821 | |
| 822 | iobase = map_physmem(pdata->iobase, 0x1000, MAP_NOCACHE); |
| 823 | priv->iobase = iobase; |
| 824 | |
| 825 | ret = clk_get_by_index(udev, 0, &priv->clk); |
| 826 | if (ret < 0) |
| 827 | goto err_mdio_alloc; |
| 828 | |
| 829 | gpio_request_by_name(udev, "reset-gpios", 0, &priv->reset_gpio, |
| 830 | GPIOD_IS_OUT); |
| 831 | |
| 832 | mdiodev = mdio_alloc(); |
| 833 | if (!mdiodev) { |
| 834 | ret = -ENOMEM; |
| 835 | goto err_mdio_alloc; |
| 836 | } |
| 837 | |
| 838 | mdiodev->read = bb_miiphy_read; |
| 839 | mdiodev->write = bb_miiphy_write; |
| 840 | bb_miiphy_buses[0].priv = eth; |
| 841 | snprintf(mdiodev->name, sizeof(mdiodev->name), udev->name); |
| 842 | |
| 843 | ret = mdio_register(mdiodev); |
| 844 | if (ret < 0) |
| 845 | goto err_mdio_register; |
| 846 | |
| 847 | priv->bus = miiphy_get_dev_by_name(udev->name); |
| 848 | |
| 849 | eth->port = CONFIG_SH_ETHER_USE_PORT; |
| 850 | eth->port_info[eth->port].phy_addr = CONFIG_SH_ETHER_PHY_ADDR; |
| 851 | eth->port_info[eth->port].iobase = |
| 852 | (void __iomem *)(BASE_IO_ADDR + 0x800 * eth->port); |
| 853 | |
| 854 | return 0; |
| 855 | |
| 856 | err_mdio_register: |
| 857 | mdio_free(mdiodev); |
| 858 | err_mdio_alloc: |
| 859 | unmap_physmem(priv->iobase, MAP_NOCACHE); |
| 860 | return ret; |
| 861 | } |
| 862 | |
| 863 | static int sh_ether_remove(struct udevice *udev) |
| 864 | { |
| 865 | struct sh_ether_priv *priv = dev_get_priv(udev); |
| 866 | struct sh_eth_dev *eth = &priv->shdev; |
| 867 | struct sh_eth_info *port_info = ð->port_info[eth->port]; |
| 868 | |
| 869 | free(port_info->phydev); |
| 870 | mdio_unregister(priv->bus); |
| 871 | mdio_free(priv->bus); |
| 872 | |
| 873 | if (dm_gpio_is_valid(&priv->reset_gpio)) |
| 874 | dm_gpio_free(udev, &priv->reset_gpio); |
| 875 | |
| 876 | unmap_physmem(priv->iobase, MAP_NOCACHE); |
| 877 | |
| 878 | return 0; |
| 879 | } |
| 880 | |
| 881 | static const struct eth_ops sh_ether_ops = { |
| 882 | .start = sh_ether_start, |
| 883 | .send = sh_ether_send, |
| 884 | .recv = sh_ether_recv, |
| 885 | .free_pkt = sh_ether_free_pkt, |
| 886 | .stop = sh_ether_stop, |
| 887 | .write_hwaddr = sh_ether_write_hwaddr, |
| 888 | }; |
| 889 | |
| 890 | int sh_ether_ofdata_to_platdata(struct udevice *dev) |
| 891 | { |
| 892 | struct eth_pdata *pdata = dev_get_platdata(dev); |
| 893 | const char *phy_mode; |
| 894 | const fdt32_t *cell; |
| 895 | int ret = 0; |
| 896 | |
| 897 | pdata->iobase = devfdt_get_addr(dev); |
| 898 | pdata->phy_interface = -1; |
| 899 | phy_mode = fdt_getprop(gd->fdt_blob, dev_of_offset(dev), "phy-mode", |
| 900 | NULL); |
| 901 | if (phy_mode) |
| 902 | pdata->phy_interface = phy_get_interface_by_name(phy_mode); |
| 903 | if (pdata->phy_interface == -1) { |
| 904 | debug("%s: Invalid PHY interface '%s'\n", __func__, phy_mode); |
| 905 | return -EINVAL; |
| 906 | } |
| 907 | |
| 908 | pdata->max_speed = 1000; |
| 909 | cell = fdt_getprop(gd->fdt_blob, dev_of_offset(dev), "max-speed", NULL); |
| 910 | if (cell) |
| 911 | pdata->max_speed = fdt32_to_cpu(*cell); |
| 912 | |
| 913 | sprintf(bb_miiphy_buses[0].name, dev->name); |
| 914 | |
| 915 | return ret; |
| 916 | } |
| 917 | |
| 918 | static const struct udevice_id sh_ether_ids[] = { |
| 919 | { .compatible = "renesas,ether-r8a7791" }, |
| 920 | { } |
| 921 | }; |
| 922 | |
| 923 | U_BOOT_DRIVER(eth_sh_ether) = { |
| 924 | .name = "sh_ether", |
| 925 | .id = UCLASS_ETH, |
| 926 | .of_match = sh_ether_ids, |
| 927 | .ofdata_to_platdata = sh_ether_ofdata_to_platdata, |
| 928 | .probe = sh_ether_probe, |
| 929 | .remove = sh_ether_remove, |
| 930 | .ops = &sh_ether_ops, |
| 931 | .priv_auto_alloc_size = sizeof(struct sh_ether_priv), |
| 932 | .platdata_auto_alloc_size = sizeof(struct eth_pdata), |
| 933 | .flags = DM_FLAG_ALLOC_PRIV_DMA, |
| 934 | }; |
| 935 | #endif |
| 936 | |
Yoshihiro Shimoda | bd1024b | 2011-10-11 18:10:14 +0900 | [diff] [blame] | 937 | /******* for bb_miiphy *******/ |
| 938 | static int sh_eth_bb_init(struct bb_miiphy_bus *bus) |
| 939 | { |
| 940 | return 0; |
| 941 | } |
| 942 | |
| 943 | static int sh_eth_bb_mdio_active(struct bb_miiphy_bus *bus) |
| 944 | { |
| 945 | struct sh_eth_dev *eth = bus->priv; |
Nobuhiro Iwamatsu | fbfb511 | 2017-12-01 08:10:32 +0900 | [diff] [blame] | 946 | struct sh_eth_info *port_info = ð->port_info[eth->port]; |
Yoshihiro Shimoda | bd1024b | 2011-10-11 18:10:14 +0900 | [diff] [blame] | 947 | |
Nobuhiro Iwamatsu | fbfb511 | 2017-12-01 08:10:32 +0900 | [diff] [blame] | 948 | 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] | 949 | |
| 950 | return 0; |
| 951 | } |
| 952 | |
| 953 | static int sh_eth_bb_mdio_tristate(struct bb_miiphy_bus *bus) |
| 954 | { |
| 955 | struct sh_eth_dev *eth = bus->priv; |
Nobuhiro Iwamatsu | fbfb511 | 2017-12-01 08:10:32 +0900 | [diff] [blame] | 956 | struct sh_eth_info *port_info = ð->port_info[eth->port]; |
Yoshihiro Shimoda | bd1024b | 2011-10-11 18:10:14 +0900 | [diff] [blame] | 957 | |
Nobuhiro Iwamatsu | fbfb511 | 2017-12-01 08:10:32 +0900 | [diff] [blame] | 958 | 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] | 959 | |
| 960 | return 0; |
| 961 | } |
| 962 | |
| 963 | static int sh_eth_bb_set_mdio(struct bb_miiphy_bus *bus, int v) |
| 964 | { |
| 965 | struct sh_eth_dev *eth = bus->priv; |
Nobuhiro Iwamatsu | fbfb511 | 2017-12-01 08:10:32 +0900 | [diff] [blame] | 966 | struct sh_eth_info *port_info = ð->port_info[eth->port]; |
Yoshihiro Shimoda | bd1024b | 2011-10-11 18:10:14 +0900 | [diff] [blame] | 967 | |
| 968 | if (v) |
Nobuhiro Iwamatsu | fbfb511 | 2017-12-01 08:10:32 +0900 | [diff] [blame] | 969 | sh_eth_write(port_info, |
| 970 | sh_eth_read(port_info, PIR) | PIR_MDO, PIR); |
Yoshihiro Shimoda | bd1024b | 2011-10-11 18:10:14 +0900 | [diff] [blame] | 971 | else |
Nobuhiro Iwamatsu | fbfb511 | 2017-12-01 08:10:32 +0900 | [diff] [blame] | 972 | sh_eth_write(port_info, |
| 973 | sh_eth_read(port_info, PIR) & ~PIR_MDO, PIR); |
Yoshihiro Shimoda | bd1024b | 2011-10-11 18:10:14 +0900 | [diff] [blame] | 974 | |
| 975 | return 0; |
| 976 | } |
| 977 | |
| 978 | static int sh_eth_bb_get_mdio(struct bb_miiphy_bus *bus, int *v) |
| 979 | { |
| 980 | struct sh_eth_dev *eth = bus->priv; |
Nobuhiro Iwamatsu | fbfb511 | 2017-12-01 08:10:32 +0900 | [diff] [blame] | 981 | struct sh_eth_info *port_info = ð->port_info[eth->port]; |
Yoshihiro Shimoda | bd1024b | 2011-10-11 18:10:14 +0900 | [diff] [blame] | 982 | |
Nobuhiro Iwamatsu | fbfb511 | 2017-12-01 08:10:32 +0900 | [diff] [blame] | 983 | *v = (sh_eth_read(port_info, PIR) & PIR_MDI) >> 3; |
Yoshihiro Shimoda | bd1024b | 2011-10-11 18:10:14 +0900 | [diff] [blame] | 984 | |
| 985 | return 0; |
| 986 | } |
| 987 | |
| 988 | static int sh_eth_bb_set_mdc(struct bb_miiphy_bus *bus, int v) |
| 989 | { |
| 990 | struct sh_eth_dev *eth = bus->priv; |
Nobuhiro Iwamatsu | fbfb511 | 2017-12-01 08:10:32 +0900 | [diff] [blame] | 991 | struct sh_eth_info *port_info = ð->port_info[eth->port]; |
Yoshihiro Shimoda | bd1024b | 2011-10-11 18:10:14 +0900 | [diff] [blame] | 992 | |
| 993 | if (v) |
Nobuhiro Iwamatsu | fbfb511 | 2017-12-01 08:10:32 +0900 | [diff] [blame] | 994 | sh_eth_write(port_info, |
| 995 | sh_eth_read(port_info, PIR) | PIR_MDC, PIR); |
Yoshihiro Shimoda | bd1024b | 2011-10-11 18:10:14 +0900 | [diff] [blame] | 996 | else |
Nobuhiro Iwamatsu | fbfb511 | 2017-12-01 08:10:32 +0900 | [diff] [blame] | 997 | sh_eth_write(port_info, |
| 998 | sh_eth_read(port_info, PIR) & ~PIR_MDC, PIR); |
Yoshihiro Shimoda | bd1024b | 2011-10-11 18:10:14 +0900 | [diff] [blame] | 999 | |
| 1000 | return 0; |
| 1001 | } |
| 1002 | |
| 1003 | static int sh_eth_bb_delay(struct bb_miiphy_bus *bus) |
| 1004 | { |
| 1005 | udelay(10); |
| 1006 | |
| 1007 | return 0; |
| 1008 | } |
| 1009 | |
| 1010 | struct bb_miiphy_bus bb_miiphy_buses[] = { |
| 1011 | { |
| 1012 | .name = "sh_eth", |
| 1013 | .init = sh_eth_bb_init, |
| 1014 | .mdio_active = sh_eth_bb_mdio_active, |
| 1015 | .mdio_tristate = sh_eth_bb_mdio_tristate, |
| 1016 | .set_mdio = sh_eth_bb_set_mdio, |
| 1017 | .get_mdio = sh_eth_bb_get_mdio, |
| 1018 | .set_mdc = sh_eth_bb_set_mdc, |
| 1019 | .delay = sh_eth_bb_delay, |
| 1020 | } |
| 1021 | }; |
Nobuhiro Iwamatsu | dc14867 | 2017-12-01 08:08:00 +0900 | [diff] [blame] | 1022 | |
Yoshihiro Shimoda | bd1024b | 2011-10-11 18:10:14 +0900 | [diff] [blame] | 1023 | int bb_miiphy_buses_num = ARRAY_SIZE(bb_miiphy_buses); |