Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Po-Yu Chuang | 750326e | 2009-08-10 11:00:00 +0800 | [diff] [blame] | 2 | /* |
| 3 | * Faraday FTMAC100 Ethernet |
| 4 | * |
| 5 | * (C) Copyright 2009 Faraday Technology |
| 6 | * Po-Yu Chuang <ratbert@faraday-tech.com> |
Po-Yu Chuang | 750326e | 2009-08-10 11:00:00 +0800 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | #include <config.h> |
| 10 | #include <common.h> |
Simon Glass | 1eb69ae | 2019-11-14 12:57:39 -0700 | [diff] [blame] | 11 | #include <cpu_func.h> |
Simon Glass | 7b51b57 | 2019-08-01 09:46:52 -0600 | [diff] [blame] | 12 | #include <env.h> |
Po-Yu Chuang | 750326e | 2009-08-10 11:00:00 +0800 | [diff] [blame] | 13 | #include <malloc.h> |
| 14 | #include <net.h> |
Simon Glass | 401d1c4 | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 15 | #include <asm/global_data.h> |
Simon Glass | c05ed00 | 2020-05-10 11:40:11 -0600 | [diff] [blame] | 16 | #include <linux/delay.h> |
rick | be71a17 | 2017-05-23 13:48:27 +0800 | [diff] [blame] | 17 | #include <linux/io.h> |
Po-Yu Chuang | 750326e | 2009-08-10 11:00:00 +0800 | [diff] [blame] | 18 | |
| 19 | #include "ftmac100.h" |
rick | be71a17 | 2017-05-23 13:48:27 +0800 | [diff] [blame] | 20 | #ifdef CONFIG_DM_ETH |
| 21 | #include <dm.h> |
| 22 | DECLARE_GLOBAL_DATA_PTR; |
| 23 | #endif |
Po-Yu Chuang | 750326e | 2009-08-10 11:00:00 +0800 | [diff] [blame] | 24 | #define ETH_ZLEN 60 |
| 25 | |
| 26 | struct ftmac100_data { |
Po-Yu Chuang | 6f6e6e0 | 2011-01-21 15:50:37 +0800 | [diff] [blame] | 27 | struct ftmac100_txdes txdes[1]; |
| 28 | struct ftmac100_rxdes rxdes[PKTBUFSRX]; |
Po-Yu Chuang | 750326e | 2009-08-10 11:00:00 +0800 | [diff] [blame] | 29 | int rx_index; |
rick | be71a17 | 2017-05-23 13:48:27 +0800 | [diff] [blame] | 30 | const char *name; |
| 31 | phys_addr_t iobase; |
Po-Yu Chuang | 750326e | 2009-08-10 11:00:00 +0800 | [diff] [blame] | 32 | }; |
| 33 | |
| 34 | /* |
| 35 | * Reset MAC |
| 36 | */ |
rick | be71a17 | 2017-05-23 13:48:27 +0800 | [diff] [blame] | 37 | static void ftmac100_reset(struct ftmac100_data *priv) |
Po-Yu Chuang | 750326e | 2009-08-10 11:00:00 +0800 | [diff] [blame] | 38 | { |
Bin Meng | 673625c | 2021-01-31 20:36:02 +0800 | [diff] [blame] | 39 | struct ftmac100 *ftmac100 = (struct ftmac100 *)(uintptr_t)priv->iobase; |
Po-Yu Chuang | 750326e | 2009-08-10 11:00:00 +0800 | [diff] [blame] | 40 | |
| 41 | debug ("%s()\n", __func__); |
| 42 | |
| 43 | writel (FTMAC100_MACCR_SW_RST, &ftmac100->maccr); |
| 44 | |
| 45 | while (readl (&ftmac100->maccr) & FTMAC100_MACCR_SW_RST) |
rick | 1341494 | 2017-08-29 10:15:05 +0800 | [diff] [blame] | 46 | mdelay(1); |
| 47 | /* |
| 48 | * When soft reset complete, write mac address immediately maybe fail somehow |
| 49 | * Wait for a while can avoid this problem |
| 50 | */ |
| 51 | mdelay(1); |
Po-Yu Chuang | 750326e | 2009-08-10 11:00:00 +0800 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | /* |
| 55 | * Set MAC address |
| 56 | */ |
rick | be71a17 | 2017-05-23 13:48:27 +0800 | [diff] [blame] | 57 | static void ftmac100_set_mac(struct ftmac100_data *priv , |
| 58 | const unsigned char *mac) |
Po-Yu Chuang | 750326e | 2009-08-10 11:00:00 +0800 | [diff] [blame] | 59 | { |
Bin Meng | 673625c | 2021-01-31 20:36:02 +0800 | [diff] [blame] | 60 | struct ftmac100 *ftmac100 = (struct ftmac100 *)(uintptr_t)priv->iobase; |
Po-Yu Chuang | 750326e | 2009-08-10 11:00:00 +0800 | [diff] [blame] | 61 | unsigned int maddr = mac[0] << 8 | mac[1]; |
| 62 | unsigned int laddr = mac[2] << 24 | mac[3] << 16 | mac[4] << 8 | mac[5]; |
| 63 | |
| 64 | debug ("%s(%x %x)\n", __func__, maddr, laddr); |
| 65 | |
| 66 | writel (maddr, &ftmac100->mac_madr); |
| 67 | writel (laddr, &ftmac100->mac_ladr); |
| 68 | } |
| 69 | |
Po-Yu Chuang | 750326e | 2009-08-10 11:00:00 +0800 | [diff] [blame] | 70 | /* |
rick | be71a17 | 2017-05-23 13:48:27 +0800 | [diff] [blame] | 71 | * Disable MAC |
Po-Yu Chuang | 750326e | 2009-08-10 11:00:00 +0800 | [diff] [blame] | 72 | */ |
rick | be71a17 | 2017-05-23 13:48:27 +0800 | [diff] [blame] | 73 | static void _ftmac100_halt(struct ftmac100_data *priv) |
Po-Yu Chuang | 750326e | 2009-08-10 11:00:00 +0800 | [diff] [blame] | 74 | { |
Bin Meng | 673625c | 2021-01-31 20:36:02 +0800 | [diff] [blame] | 75 | struct ftmac100 *ftmac100 = (struct ftmac100 *)(uintptr_t)priv->iobase; |
Po-Yu Chuang | 750326e | 2009-08-10 11:00:00 +0800 | [diff] [blame] | 76 | debug ("%s()\n", __func__); |
Po-Yu Chuang | 750326e | 2009-08-10 11:00:00 +0800 | [diff] [blame] | 77 | writel (0, &ftmac100->maccr); |
| 78 | } |
| 79 | |
rick | be71a17 | 2017-05-23 13:48:27 +0800 | [diff] [blame] | 80 | /* |
| 81 | * Initialize MAC |
| 82 | */ |
| 83 | static int _ftmac100_init(struct ftmac100_data *priv, unsigned char enetaddr[6]) |
Po-Yu Chuang | 750326e | 2009-08-10 11:00:00 +0800 | [diff] [blame] | 84 | { |
Bin Meng | 673625c | 2021-01-31 20:36:02 +0800 | [diff] [blame] | 85 | struct ftmac100 *ftmac100 = (struct ftmac100 *)(uintptr_t)priv->iobase; |
Po-Yu Chuang | 6f6e6e0 | 2011-01-21 15:50:37 +0800 | [diff] [blame] | 86 | struct ftmac100_txdes *txdes = priv->txdes; |
| 87 | struct ftmac100_rxdes *rxdes = priv->rxdes; |
Po-Yu Chuang | 750326e | 2009-08-10 11:00:00 +0800 | [diff] [blame] | 88 | unsigned int maccr; |
| 89 | int i; |
Po-Yu Chuang | 750326e | 2009-08-10 11:00:00 +0800 | [diff] [blame] | 90 | debug ("%s()\n", __func__); |
| 91 | |
rick | be71a17 | 2017-05-23 13:48:27 +0800 | [diff] [blame] | 92 | ftmac100_reset(priv); |
Po-Yu Chuang | 750326e | 2009-08-10 11:00:00 +0800 | [diff] [blame] | 93 | |
| 94 | /* set the ethernet address */ |
rick | be71a17 | 2017-05-23 13:48:27 +0800 | [diff] [blame] | 95 | ftmac100_set_mac(priv, enetaddr); |
Po-Yu Chuang | 750326e | 2009-08-10 11:00:00 +0800 | [diff] [blame] | 96 | |
Po-Yu Chuang | 750326e | 2009-08-10 11:00:00 +0800 | [diff] [blame] | 97 | |
| 98 | /* disable all interrupts */ |
| 99 | |
| 100 | writel (0, &ftmac100->imr); |
| 101 | |
| 102 | /* initialize descriptors */ |
| 103 | |
| 104 | priv->rx_index = 0; |
| 105 | |
| 106 | txdes[0].txdes1 = FTMAC100_TXDES1_EDOTR; |
| 107 | rxdes[PKTBUFSRX - 1].rxdes1 = FTMAC100_RXDES1_EDORR; |
| 108 | |
| 109 | for (i = 0; i < PKTBUFSRX; i++) { |
| 110 | /* RXBUF_BADR */ |
Rick Chen | 28b52a6 | 2018-05-29 10:42:19 +0800 | [diff] [blame] | 111 | rxdes[i].rxdes2 = (unsigned int)(unsigned long)net_rx_packets[i]; |
Po-Yu Chuang | 750326e | 2009-08-10 11:00:00 +0800 | [diff] [blame] | 112 | rxdes[i].rxdes1 |= FTMAC100_RXDES1_RXBUF_SIZE (PKTSIZE_ALIGN); |
| 113 | rxdes[i].rxdes0 = FTMAC100_RXDES0_RXDMA_OWN; |
| 114 | } |
| 115 | |
| 116 | /* transmit ring */ |
| 117 | |
Rick Chen | 28b52a6 | 2018-05-29 10:42:19 +0800 | [diff] [blame] | 118 | writel ((unsigned long)txdes, &ftmac100->txr_badr); |
Po-Yu Chuang | 750326e | 2009-08-10 11:00:00 +0800 | [diff] [blame] | 119 | |
| 120 | /* receive ring */ |
| 121 | |
Rick Chen | 28b52a6 | 2018-05-29 10:42:19 +0800 | [diff] [blame] | 122 | writel ((unsigned long)rxdes, &ftmac100->rxr_badr); |
Po-Yu Chuang | 750326e | 2009-08-10 11:00:00 +0800 | [diff] [blame] | 123 | |
| 124 | /* poll receive descriptor automatically */ |
| 125 | |
| 126 | writel (FTMAC100_APTC_RXPOLL_CNT (1), &ftmac100->aptc); |
| 127 | |
| 128 | /* enable transmitter, receiver */ |
| 129 | |
| 130 | maccr = FTMAC100_MACCR_XMT_EN | |
| 131 | FTMAC100_MACCR_RCV_EN | |
| 132 | FTMAC100_MACCR_XDMA_EN | |
| 133 | FTMAC100_MACCR_RDMA_EN | |
| 134 | FTMAC100_MACCR_CRC_APD | |
| 135 | FTMAC100_MACCR_ENRX_IN_HALFTX | |
| 136 | FTMAC100_MACCR_RX_RUNT | |
| 137 | FTMAC100_MACCR_RX_BROADPKT; |
| 138 | |
| 139 | writel (maccr, &ftmac100->maccr); |
| 140 | |
| 141 | return 0; |
| 142 | } |
| 143 | |
| 144 | /* |
rick | be71a17 | 2017-05-23 13:48:27 +0800 | [diff] [blame] | 145 | * Free receiving buffer |
Po-Yu Chuang | 750326e | 2009-08-10 11:00:00 +0800 | [diff] [blame] | 146 | */ |
rick | be71a17 | 2017-05-23 13:48:27 +0800 | [diff] [blame] | 147 | static int _ftmac100_free_pkt(struct ftmac100_data *priv) |
Po-Yu Chuang | 750326e | 2009-08-10 11:00:00 +0800 | [diff] [blame] | 148 | { |
rick | be71a17 | 2017-05-23 13:48:27 +0800 | [diff] [blame] | 149 | struct ftmac100_rxdes *curr_des; |
| 150 | curr_des = &priv->rxdes[priv->rx_index]; |
| 151 | /* release buffer to DMA */ |
| 152 | curr_des->rxdes0 |= FTMAC100_RXDES0_RXDMA_OWN; |
| 153 | priv->rx_index = (priv->rx_index + 1) % PKTBUFSRX; |
| 154 | return 0; |
| 155 | } |
| 156 | |
| 157 | /* |
| 158 | * Receive a data block via Ethernet |
| 159 | */ |
| 160 | static int __ftmac100_recv(struct ftmac100_data *priv) |
| 161 | { |
Po-Yu Chuang | 6f6e6e0 | 2011-01-21 15:50:37 +0800 | [diff] [blame] | 162 | struct ftmac100_rxdes *curr_des; |
Po-Yu Chuang | 750326e | 2009-08-10 11:00:00 +0800 | [diff] [blame] | 163 | unsigned short rxlen; |
| 164 | |
| 165 | curr_des = &priv->rxdes[priv->rx_index]; |
Po-Yu Chuang | 750326e | 2009-08-10 11:00:00 +0800 | [diff] [blame] | 166 | if (curr_des->rxdes0 & FTMAC100_RXDES0_RXDMA_OWN) |
rick | be71a17 | 2017-05-23 13:48:27 +0800 | [diff] [blame] | 167 | return 0; |
Po-Yu Chuang | 750326e | 2009-08-10 11:00:00 +0800 | [diff] [blame] | 168 | |
| 169 | if (curr_des->rxdes0 & (FTMAC100_RXDES0_RX_ERR | |
| 170 | FTMAC100_RXDES0_CRC_ERR | |
| 171 | FTMAC100_RXDES0_FTL | |
| 172 | FTMAC100_RXDES0_RUNT | |
| 173 | FTMAC100_RXDES0_RX_ODD_NB)) { |
rick | be71a17 | 2017-05-23 13:48:27 +0800 | [diff] [blame] | 174 | return 0; |
Po-Yu Chuang | 750326e | 2009-08-10 11:00:00 +0800 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | rxlen = FTMAC100_RXDES0_RFL (curr_des->rxdes0); |
rick | ce4e237 | 2017-08-29 10:09:00 +0800 | [diff] [blame] | 178 | invalidate_dcache_range(curr_des->rxdes2,curr_des->rxdes2+rxlen); |
Po-Yu Chuang | 750326e | 2009-08-10 11:00:00 +0800 | [diff] [blame] | 179 | debug ("%s(): RX buffer %d, %x received\n", |
| 180 | __func__, priv->rx_index, rxlen); |
| 181 | |
rick | be71a17 | 2017-05-23 13:48:27 +0800 | [diff] [blame] | 182 | return rxlen; |
Po-Yu Chuang | 750326e | 2009-08-10 11:00:00 +0800 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | /* |
| 186 | * Send a data block via Ethernet |
| 187 | */ |
rick | be71a17 | 2017-05-23 13:48:27 +0800 | [diff] [blame] | 188 | static int _ftmac100_send(struct ftmac100_data *priv, void *packet, int length) |
Po-Yu Chuang | 750326e | 2009-08-10 11:00:00 +0800 | [diff] [blame] | 189 | { |
Bin Meng | 673625c | 2021-01-31 20:36:02 +0800 | [diff] [blame] | 190 | struct ftmac100 *ftmac100 = (struct ftmac100 *)(uintptr_t)priv->iobase; |
Po-Yu Chuang | 6f6e6e0 | 2011-01-21 15:50:37 +0800 | [diff] [blame] | 191 | struct ftmac100_txdes *curr_des = priv->txdes; |
Po-Yu Chuang | 8d8fd5b | 2011-01-21 15:51:25 +0800 | [diff] [blame] | 192 | ulong start; |
Po-Yu Chuang | 750326e | 2009-08-10 11:00:00 +0800 | [diff] [blame] | 193 | |
| 194 | if (curr_des->txdes0 & FTMAC100_TXDES0_TXDMA_OWN) { |
| 195 | debug ("%s(): no TX descriptor available\n", __func__); |
| 196 | return -1; |
| 197 | } |
| 198 | |
Rick Chen | 28b52a6 | 2018-05-29 10:42:19 +0800 | [diff] [blame] | 199 | debug ("%s(%lx, %x)\n", __func__, (unsigned long)packet, length); |
Po-Yu Chuang | 750326e | 2009-08-10 11:00:00 +0800 | [diff] [blame] | 200 | |
| 201 | length = (length < ETH_ZLEN) ? ETH_ZLEN : length; |
| 202 | |
| 203 | /* initiate a transmit sequence */ |
| 204 | |
Rick Chen | 28b52a6 | 2018-05-29 10:42:19 +0800 | [diff] [blame] | 205 | flush_dcache_range((unsigned long)packet,(unsigned long)packet+length); |
| 206 | curr_des->txdes2 = (unsigned int)(unsigned long)packet; /* TXBUF_BADR */ |
Po-Yu Chuang | 750326e | 2009-08-10 11:00:00 +0800 | [diff] [blame] | 207 | |
| 208 | curr_des->txdes1 &= FTMAC100_TXDES1_EDOTR; |
| 209 | curr_des->txdes1 |= FTMAC100_TXDES1_FTS | |
| 210 | FTMAC100_TXDES1_LTS | |
| 211 | FTMAC100_TXDES1_TXBUF_SIZE (length); |
| 212 | |
| 213 | curr_des->txdes0 = FTMAC100_TXDES0_TXDMA_OWN; |
| 214 | |
| 215 | /* start transmit */ |
| 216 | |
| 217 | writel (1, &ftmac100->txpd); |
| 218 | |
| 219 | /* wait for transfer to succeed */ |
| 220 | |
Po-Yu Chuang | 8d8fd5b | 2011-01-21 15:51:25 +0800 | [diff] [blame] | 221 | start = get_timer(0); |
Po-Yu Chuang | 750326e | 2009-08-10 11:00:00 +0800 | [diff] [blame] | 222 | while (curr_des->txdes0 & FTMAC100_TXDES0_TXDMA_OWN) { |
Po-Yu Chuang | 8d8fd5b | 2011-01-21 15:51:25 +0800 | [diff] [blame] | 223 | if (get_timer(start) >= 5) { |
Po-Yu Chuang | 750326e | 2009-08-10 11:00:00 +0800 | [diff] [blame] | 224 | debug ("%s(): timed out\n", __func__); |
| 225 | return -1; |
| 226 | } |
| 227 | } |
| 228 | |
| 229 | debug ("%s(): packet sent\n", __func__); |
| 230 | |
| 231 | return 0; |
| 232 | } |
| 233 | |
rick | be71a17 | 2017-05-23 13:48:27 +0800 | [diff] [blame] | 234 | #ifndef CONFIG_DM_ETH |
| 235 | /* |
| 236 | * disable transmitter, receiver |
| 237 | */ |
| 238 | static void ftmac100_halt(struct eth_device *dev) |
| 239 | { |
| 240 | struct ftmac100_data *priv = dev->priv; |
| 241 | return _ftmac100_halt(priv); |
| 242 | } |
| 243 | |
Masahiro Yamada | b75d8dc | 2020-06-26 15:13:33 +0900 | [diff] [blame] | 244 | static int ftmac100_init(struct eth_device *dev, struct bd_info *bd) |
rick | be71a17 | 2017-05-23 13:48:27 +0800 | [diff] [blame] | 245 | { |
| 246 | struct ftmac100_data *priv = dev->priv; |
| 247 | return _ftmac100_init(priv , dev->enetaddr); |
| 248 | } |
| 249 | |
| 250 | static int _ftmac100_recv(struct ftmac100_data *priv) |
| 251 | { |
| 252 | struct ftmac100_rxdes *curr_des; |
| 253 | unsigned short len; |
| 254 | curr_des = &priv->rxdes[priv->rx_index]; |
| 255 | len = __ftmac100_recv(priv); |
| 256 | if (len) { |
| 257 | /* pass the packet up to the protocol layers. */ |
| 258 | net_process_received_packet((void *)curr_des->rxdes2, len); |
| 259 | _ftmac100_free_pkt(priv); |
| 260 | } |
| 261 | return len ? 1 : 0; |
| 262 | } |
| 263 | |
| 264 | /* |
| 265 | * Get a data block via Ethernet |
| 266 | */ |
| 267 | static int ftmac100_recv(struct eth_device *dev) |
| 268 | { |
| 269 | struct ftmac100_data *priv = dev->priv; |
| 270 | return _ftmac100_recv(priv); |
| 271 | } |
| 272 | |
| 273 | /* |
| 274 | * Send a data block via Ethernet |
| 275 | */ |
| 276 | static int ftmac100_send(struct eth_device *dev, void *packet, int length) |
| 277 | { |
| 278 | struct ftmac100_data *priv = dev->priv; |
| 279 | return _ftmac100_send(priv , packet , length); |
| 280 | } |
| 281 | |
Masahiro Yamada | b75d8dc | 2020-06-26 15:13:33 +0900 | [diff] [blame] | 282 | int ftmac100_initialize (struct bd_info *bd) |
Po-Yu Chuang | 750326e | 2009-08-10 11:00:00 +0800 | [diff] [blame] | 283 | { |
| 284 | struct eth_device *dev; |
| 285 | struct ftmac100_data *priv; |
Po-Yu Chuang | 750326e | 2009-08-10 11:00:00 +0800 | [diff] [blame] | 286 | dev = malloc (sizeof *dev); |
| 287 | if (!dev) { |
| 288 | printf ("%s(): failed to allocate dev\n", __func__); |
| 289 | goto out; |
| 290 | } |
Po-Yu Chuang | 750326e | 2009-08-10 11:00:00 +0800 | [diff] [blame] | 291 | /* Transmit and receive descriptors should align to 16 bytes */ |
Po-Yu Chuang | 750326e | 2009-08-10 11:00:00 +0800 | [diff] [blame] | 292 | priv = memalign (16, sizeof (struct ftmac100_data)); |
| 293 | if (!priv) { |
| 294 | printf ("%s(): failed to allocate priv\n", __func__); |
| 295 | goto free_dev; |
| 296 | } |
Po-Yu Chuang | 750326e | 2009-08-10 11:00:00 +0800 | [diff] [blame] | 297 | memset (dev, 0, sizeof (*dev)); |
| 298 | memset (priv, 0, sizeof (*priv)); |
| 299 | |
Ben Whitten | 192bc69 | 2015-12-30 13:05:58 +0000 | [diff] [blame] | 300 | strcpy(dev->name, "FTMAC100"); |
Po-Yu Chuang | 750326e | 2009-08-10 11:00:00 +0800 | [diff] [blame] | 301 | dev->iobase = CONFIG_FTMAC100_BASE; |
| 302 | dev->init = ftmac100_init; |
| 303 | dev->halt = ftmac100_halt; |
| 304 | dev->send = ftmac100_send; |
| 305 | dev->recv = ftmac100_recv; |
| 306 | dev->priv = priv; |
rick | be71a17 | 2017-05-23 13:48:27 +0800 | [diff] [blame] | 307 | priv->iobase = dev->iobase; |
Po-Yu Chuang | 750326e | 2009-08-10 11:00:00 +0800 | [diff] [blame] | 308 | eth_register (dev); |
| 309 | |
| 310 | return 1; |
| 311 | |
| 312 | free_dev: |
| 313 | free (dev); |
| 314 | out: |
| 315 | return 0; |
| 316 | } |
rick | be71a17 | 2017-05-23 13:48:27 +0800 | [diff] [blame] | 317 | #endif |
| 318 | |
| 319 | #ifdef CONFIG_DM_ETH |
| 320 | static int ftmac100_start(struct udevice *dev) |
| 321 | { |
Simon Glass | c69cda2 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 322 | struct eth_pdata *plat = dev_get_plat(dev); |
rick | be71a17 | 2017-05-23 13:48:27 +0800 | [diff] [blame] | 323 | struct ftmac100_data *priv = dev_get_priv(dev); |
| 324 | |
| 325 | return _ftmac100_init(priv, plat->enetaddr); |
| 326 | } |
| 327 | |
| 328 | static void ftmac100_stop(struct udevice *dev) |
| 329 | { |
| 330 | struct ftmac100_data *priv = dev_get_priv(dev); |
| 331 | _ftmac100_halt(priv); |
| 332 | } |
| 333 | |
| 334 | static int ftmac100_send(struct udevice *dev, void *packet, int length) |
| 335 | { |
| 336 | struct ftmac100_data *priv = dev_get_priv(dev); |
| 337 | int ret; |
| 338 | ret = _ftmac100_send(priv , packet , length); |
| 339 | return ret ? 0 : -ETIMEDOUT; |
| 340 | } |
| 341 | |
| 342 | static int ftmac100_recv(struct udevice *dev, int flags, uchar **packetp) |
| 343 | { |
| 344 | struct ftmac100_data *priv = dev_get_priv(dev); |
| 345 | struct ftmac100_rxdes *curr_des; |
| 346 | curr_des = &priv->rxdes[priv->rx_index]; |
| 347 | int len; |
| 348 | len = __ftmac100_recv(priv); |
| 349 | if (len) |
Rick Chen | 28b52a6 | 2018-05-29 10:42:19 +0800 | [diff] [blame] | 350 | *packetp = (uchar *)(unsigned long)curr_des->rxdes2; |
rick | be71a17 | 2017-05-23 13:48:27 +0800 | [diff] [blame] | 351 | |
| 352 | return len ? len : -EAGAIN; |
| 353 | } |
| 354 | |
| 355 | static int ftmac100_free_pkt(struct udevice *dev, uchar *packet, int length) |
| 356 | { |
| 357 | struct ftmac100_data *priv = dev_get_priv(dev); |
| 358 | _ftmac100_free_pkt(priv); |
| 359 | return 0; |
| 360 | } |
| 361 | |
| 362 | int ftmac100_read_rom_hwaddr(struct udevice *dev) |
| 363 | { |
Simon Glass | c69cda2 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 364 | struct eth_pdata *pdata = dev_get_plat(dev); |
Simon Glass | 35affd7 | 2017-08-03 12:22:14 -0600 | [diff] [blame] | 365 | eth_env_get_enetaddr("ethaddr", pdata->enetaddr); |
rick | be71a17 | 2017-05-23 13:48:27 +0800 | [diff] [blame] | 366 | return 0; |
| 367 | } |
| 368 | |
| 369 | static const char *dtbmacaddr(u32 ifno) |
| 370 | { |
| 371 | int node, len; |
| 372 | char enet[16]; |
| 373 | const char *mac; |
| 374 | const char *path; |
| 375 | if (gd->fdt_blob == NULL) { |
| 376 | printf("%s: don't have a valid gd->fdt_blob!\n", __func__); |
| 377 | return NULL; |
| 378 | } |
| 379 | node = fdt_path_offset(gd->fdt_blob, "/aliases"); |
| 380 | if (node < 0) |
| 381 | return NULL; |
| 382 | |
| 383 | sprintf(enet, "ethernet%d", ifno); |
| 384 | path = fdt_getprop(gd->fdt_blob, node, enet, NULL); |
| 385 | if (!path) { |
| 386 | printf("no alias for %s\n", enet); |
| 387 | return NULL; |
| 388 | } |
| 389 | node = fdt_path_offset(gd->fdt_blob, path); |
| 390 | mac = fdt_getprop(gd->fdt_blob, node, "mac-address", &len); |
| 391 | if (mac && is_valid_ethaddr((u8 *)mac)) |
| 392 | return mac; |
| 393 | |
| 394 | return NULL; |
| 395 | } |
| 396 | |
Simon Glass | d1998a9 | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 397 | static int ftmac100_of_to_plat(struct udevice *dev) |
rick | be71a17 | 2017-05-23 13:48:27 +0800 | [diff] [blame] | 398 | { |
| 399 | struct ftmac100_data *priv = dev_get_priv(dev); |
Simon Glass | c69cda2 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 400 | struct eth_pdata *pdata = dev_get_plat(dev); |
rick | be71a17 | 2017-05-23 13:48:27 +0800 | [diff] [blame] | 401 | const char *mac; |
Masahiro Yamada | 2548493 | 2020-07-17 14:36:48 +0900 | [diff] [blame] | 402 | pdata->iobase = dev_read_addr(dev); |
rick | be71a17 | 2017-05-23 13:48:27 +0800 | [diff] [blame] | 403 | priv->iobase = pdata->iobase; |
| 404 | mac = dtbmacaddr(0); |
| 405 | if (mac) |
| 406 | memcpy(pdata->enetaddr , mac , 6); |
| 407 | |
| 408 | return 0; |
| 409 | } |
| 410 | |
| 411 | static int ftmac100_probe(struct udevice *dev) |
| 412 | { |
| 413 | struct ftmac100_data *priv = dev_get_priv(dev); |
| 414 | priv->name = dev->name; |
| 415 | return 0; |
| 416 | } |
| 417 | |
| 418 | static int ftmac100_bind(struct udevice *dev) |
| 419 | { |
| 420 | return device_set_name(dev, dev->name); |
| 421 | } |
| 422 | |
| 423 | static const struct eth_ops ftmac100_ops = { |
| 424 | .start = ftmac100_start, |
| 425 | .send = ftmac100_send, |
| 426 | .recv = ftmac100_recv, |
| 427 | .stop = ftmac100_stop, |
| 428 | .free_pkt = ftmac100_free_pkt, |
| 429 | }; |
| 430 | |
| 431 | static const struct udevice_id ftmac100_ids[] = { |
| 432 | { .compatible = "andestech,atmac100" }, |
| 433 | { } |
| 434 | }; |
| 435 | |
| 436 | U_BOOT_DRIVER(ftmac100) = { |
| 437 | .name = "nds32_mac", |
| 438 | .id = UCLASS_ETH, |
| 439 | .of_match = ftmac100_ids, |
| 440 | .bind = ftmac100_bind, |
Simon Glass | d1998a9 | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 441 | .of_to_plat = ftmac100_of_to_plat, |
rick | be71a17 | 2017-05-23 13:48:27 +0800 | [diff] [blame] | 442 | .probe = ftmac100_probe, |
| 443 | .ops = &ftmac100_ops, |
Simon Glass | 41575d8 | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 444 | .priv_auto = sizeof(struct ftmac100_data), |
Simon Glass | caa4daa | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 445 | .plat_auto = sizeof(struct eth_pdata), |
rick | be71a17 | 2017-05-23 13:48:27 +0800 | [diff] [blame] | 446 | .flags = DM_FLAG_ALLOC_PRIV_DMA, |
| 447 | }; |
| 448 | #endif |