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