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