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