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