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