Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
TsiChungLiew | 95e8787 | 2008-01-15 14:00:25 -0600 | [diff] [blame] | 2 | /* |
| 3 | * (C) Copyright 2000-2004 |
| 4 | * Wolfgang Denk, DENX Software Engineering, wd@denx.de. |
| 5 | * |
| 6 | * (C) Copyright 2007 Freescale Semiconductor, Inc. |
| 7 | * TsiChung Liew (Tsi-Chung.Liew@freescale.com) |
Angelo Durgehello | f0a0c88 | 2019-11-15 23:54:19 +0100 | [diff] [blame] | 8 | * |
| 9 | * Conversion to DM |
| 10 | * (C) 2019 Angelo Dureghello <angelo.dureghello@timesys.com> |
TsiChungLiew | 95e8787 | 2008-01-15 14:00:25 -0600 | [diff] [blame] | 11 | */ |
| 12 | |
| 13 | #include <common.h> |
Simon Glass | 0af6e2d | 2019-08-01 09:46:52 -0600 | [diff] [blame] | 14 | #include <env.h> |
Simon Glass | f11478f | 2019-12-28 10:45:07 -0700 | [diff] [blame] | 15 | #include <hang.h> |
TsiChungLiew | 95e8787 | 2008-01-15 14:00:25 -0600 | [diff] [blame] | 16 | #include <malloc.h> |
| 17 | #include <command.h> |
| 18 | #include <config.h> |
| 19 | #include <net.h> |
| 20 | #include <miiphy.h> |
Simon Glass | caefa32 | 2019-11-14 12:57:31 -0700 | [diff] [blame] | 21 | #include <linux/mii.h> |
Angelo Durgehello | f0a0c88 | 2019-11-15 23:54:19 +0100 | [diff] [blame] | 22 | #include <asm/immap.h> |
| 23 | #include <asm/fsl_mcdmafec.h> |
| 24 | |
| 25 | #include "MCD_dma.h" |
TsiChungLiew | 95e8787 | 2008-01-15 14:00:25 -0600 | [diff] [blame] | 26 | |
TsiChungLiew | 95e8787 | 2008-01-15 14:00:25 -0600 | [diff] [blame] | 27 | #undef ET_DEBUG |
| 28 | #undef MII_DEBUG |
| 29 | |
| 30 | /* Ethernet Transmit and Receive Buffers */ |
| 31 | #define DBUF_LENGTH 1520 |
| 32 | #define PKT_MAXBUF_SIZE 1518 |
TsiChungLiew | 95e8787 | 2008-01-15 14:00:25 -0600 | [diff] [blame] | 33 | #define FIFO_ERRSTAT (FIFO_STAT_RXW | FIFO_STAT_UF | FIFO_STAT_OF) |
| 34 | |
| 35 | /* RxBD bits definitions */ |
| 36 | #define BD_ENET_RX_ERR (BD_ENET_RX_LG | BD_ENET_RX_NO | BD_ENET_RX_CR | \ |
| 37 | BD_ENET_RX_OV | BD_ENET_RX_TR) |
| 38 | |
Angelo Durgehello | f0a0c88 | 2019-11-15 23:54:19 +0100 | [diff] [blame] | 39 | DECLARE_GLOBAL_DATA_PTR; |
TsiChungLiew | 95e8787 | 2008-01-15 14:00:25 -0600 | [diff] [blame] | 40 | |
Angelo Durgehello | f0a0c88 | 2019-11-15 23:54:19 +0100 | [diff] [blame] | 41 | static void init_eth_info(struct fec_info_dma *info) |
| 42 | { |
| 43 | /* setup Receive and Transmit buffer descriptor */ |
| 44 | #ifdef CONFIG_SYS_FEC_BUF_USE_SRAM |
| 45 | static u32 tmp; |
TsiChungLiew | 95e8787 | 2008-01-15 14:00:25 -0600 | [diff] [blame] | 46 | |
Angelo Durgehello | f0a0c88 | 2019-11-15 23:54:19 +0100 | [diff] [blame] | 47 | if (info->index == 0) |
| 48 | tmp = CONFIG_SYS_INIT_RAM_ADDR + 0x1000; |
| 49 | else |
| 50 | info->rxbd = (cbd_t *)DBUF_LENGTH; |
| 51 | |
| 52 | info->rxbd = (cbd_t *)((u32)info->rxbd + tmp); |
| 53 | tmp = (u32)info->rxbd; |
| 54 | info->txbd = |
| 55 | (cbd_t *)((u32)info->txbd + tmp + |
| 56 | (PKTBUFSRX * sizeof(cbd_t))); |
| 57 | tmp = (u32)info->txbd; |
| 58 | info->txbuf = |
| 59 | (char *)((u32)info->txbuf + tmp + |
| 60 | (CONFIG_SYS_TX_ETH_BUFFER * sizeof(cbd_t))); |
| 61 | tmp = (u32)info->txbuf; |
TsiChung Liew | 1844b8f | 2008-04-30 12:11:19 -0500 | [diff] [blame] | 62 | #else |
Angelo Durgehello | f0a0c88 | 2019-11-15 23:54:19 +0100 | [diff] [blame] | 63 | info->rxbd = |
| 64 | (cbd_t *)memalign(CONFIG_SYS_CACHELINE_SIZE, |
| 65 | (PKTBUFSRX * sizeof(cbd_t))); |
| 66 | info->txbd = |
| 67 | (cbd_t *)memalign(CONFIG_SYS_CACHELINE_SIZE, |
| 68 | (CONFIG_SYS_TX_ETH_BUFFER * sizeof(cbd_t))); |
| 69 | info->txbuf = |
| 70 | (char *)memalign(CONFIG_SYS_CACHELINE_SIZE, DBUF_LENGTH); |
TsiChung Liew | 1844b8f | 2008-04-30 12:11:19 -0500 | [diff] [blame] | 71 | #endif |
Angelo Durgehello | f0a0c88 | 2019-11-15 23:54:19 +0100 | [diff] [blame] | 72 | |
| 73 | #ifdef ET_DEBUG |
| 74 | printf("rxbd %x txbd %x\n", (int)info->rxbd, (int)info->txbd); |
TsiChungLiew | 95e8787 | 2008-01-15 14:00:25 -0600 | [diff] [blame] | 75 | #endif |
Angelo Durgehello | f0a0c88 | 2019-11-15 23:54:19 +0100 | [diff] [blame] | 76 | info->phy_name = (char *)memalign(CONFIG_SYS_CACHELINE_SIZE, 32); |
| 77 | } |
| 78 | |
| 79 | static void fec_halt(struct udevice *dev) |
| 80 | { |
| 81 | struct fec_info_dma *info = dev->priv; |
| 82 | volatile fecdma_t *fecp = (fecdma_t *)info->iobase; |
| 83 | int counter = 0xffff; |
| 84 | |
| 85 | /* issue graceful stop command to the FEC transmitter if necessary */ |
| 86 | fecp->tcr |= FEC_TCR_GTS; |
| 87 | |
| 88 | /* wait for graceful stop to register */ |
| 89 | while ((counter--) && (!(fecp->eir & FEC_EIR_GRA))) |
| 90 | ; |
| 91 | |
| 92 | /* Disable DMA tasks */ |
| 93 | MCD_killDma(info->tx_task); |
| 94 | MCD_killDma(info->rx_task); |
| 95 | |
| 96 | /* Disable the Ethernet Controller */ |
| 97 | fecp->ecr &= ~FEC_ECR_ETHER_EN; |
| 98 | |
| 99 | /* Clear FIFO status registers */ |
| 100 | fecp->rfsr &= FIFO_ERRSTAT; |
| 101 | fecp->tfsr &= FIFO_ERRSTAT; |
| 102 | |
| 103 | fecp->frst = 0x01000000; |
TsiChungLiew | 95e8787 | 2008-01-15 14:00:25 -0600 | [diff] [blame] | 104 | |
Angelo Durgehello | f0a0c88 | 2019-11-15 23:54:19 +0100 | [diff] [blame] | 105 | /* Issue a reset command to the FEC chip */ |
| 106 | fecp->ecr |= FEC_ECR_RESET; |
| 107 | |
| 108 | /* wait at least 20 clock cycles */ |
| 109 | mdelay(10); |
| 110 | |
| 111 | #ifdef ET_DEBUG |
| 112 | printf("Ethernet task stopped\n"); |
| 113 | #endif |
| 114 | } |
TsiChungLiew | 95e8787 | 2008-01-15 14:00:25 -0600 | [diff] [blame] | 115 | |
| 116 | #ifdef ET_DEBUG |
| 117 | static void dbg_fec_regs(struct eth_device *dev) |
| 118 | { |
| 119 | struct fec_info_dma *info = dev->priv; |
Angelo Durgehello | f0a0c88 | 2019-11-15 23:54:19 +0100 | [diff] [blame] | 120 | volatile fecdma_t *fecp = (fecdma_t *)info->iobase; |
TsiChungLiew | 95e8787 | 2008-01-15 14:00:25 -0600 | [diff] [blame] | 121 | |
| 122 | printf("=====\n"); |
| 123 | printf("ievent %x - %x\n", (int)&fecp->eir, fecp->eir); |
| 124 | printf("imask %x - %x\n", (int)&fecp->eimr, fecp->eimr); |
| 125 | printf("ecntrl %x - %x\n", (int)&fecp->ecr, fecp->ecr); |
| 126 | printf("mii_mframe %x - %x\n", (int)&fecp->mmfr, fecp->mmfr); |
| 127 | printf("mii_speed %x - %x\n", (int)&fecp->mscr, fecp->mscr); |
| 128 | printf("mii_ctrlstat %x - %x\n", (int)&fecp->mibc, fecp->mibc); |
| 129 | printf("r_cntrl %x - %x\n", (int)&fecp->rcr, fecp->rcr); |
| 130 | printf("r hash %x - %x\n", (int)&fecp->rhr, fecp->rhr); |
| 131 | printf("x_cntrl %x - %x\n", (int)&fecp->tcr, fecp->tcr); |
| 132 | printf("padr_l %x - %x\n", (int)&fecp->palr, fecp->palr); |
| 133 | printf("padr_u %x - %x\n", (int)&fecp->paur, fecp->paur); |
| 134 | printf("op_pause %x - %x\n", (int)&fecp->opd, fecp->opd); |
| 135 | printf("iadr_u %x - %x\n", (int)&fecp->iaur, fecp->iaur); |
| 136 | printf("iadr_l %x - %x\n", (int)&fecp->ialr, fecp->ialr); |
| 137 | printf("gadr_u %x - %x\n", (int)&fecp->gaur, fecp->gaur); |
| 138 | printf("gadr_l %x - %x\n", (int)&fecp->galr, fecp->galr); |
| 139 | printf("x_wmrk %x - %x\n", (int)&fecp->tfwr, fecp->tfwr); |
| 140 | printf("r_fdata %x - %x\n", (int)&fecp->rfdr, fecp->rfdr); |
| 141 | printf("r_fstat %x - %x\n", (int)&fecp->rfsr, fecp->rfsr); |
| 142 | printf("r_fctrl %x - %x\n", (int)&fecp->rfcr, fecp->rfcr); |
| 143 | printf("r_flrfp %x - %x\n", (int)&fecp->rlrfp, fecp->rlrfp); |
| 144 | printf("r_flwfp %x - %x\n", (int)&fecp->rlwfp, fecp->rlwfp); |
| 145 | printf("r_frfar %x - %x\n", (int)&fecp->rfar, fecp->rfar); |
| 146 | printf("r_frfrp %x - %x\n", (int)&fecp->rfrp, fecp->rfrp); |
| 147 | printf("r_frfwp %x - %x\n", (int)&fecp->rfwp, fecp->rfwp); |
| 148 | printf("t_fdata %x - %x\n", (int)&fecp->tfdr, fecp->tfdr); |
| 149 | printf("t_fstat %x - %x\n", (int)&fecp->tfsr, fecp->tfsr); |
| 150 | printf("t_fctrl %x - %x\n", (int)&fecp->tfcr, fecp->tfcr); |
| 151 | printf("t_flrfp %x - %x\n", (int)&fecp->tlrfp, fecp->tlrfp); |
| 152 | printf("t_flwfp %x - %x\n", (int)&fecp->tlwfp, fecp->tlwfp); |
| 153 | printf("t_ftfar %x - %x\n", (int)&fecp->tfar, fecp->tfar); |
| 154 | printf("t_ftfrp %x - %x\n", (int)&fecp->tfrp, fecp->tfrp); |
| 155 | printf("t_ftfwp %x - %x\n", (int)&fecp->tfwp, fecp->tfwp); |
| 156 | printf("frst %x - %x\n", (int)&fecp->frst, fecp->frst); |
| 157 | printf("ctcwr %x - %x\n", (int)&fecp->ctcwr, fecp->ctcwr); |
| 158 | } |
| 159 | #endif |
| 160 | |
Angelo Durgehello | f0a0c88 | 2019-11-15 23:54:19 +0100 | [diff] [blame] | 161 | static void set_fec_duplex_speed(volatile fecdma_t *fecp, int dup_spd) |
TsiChungLiew | 95e8787 | 2008-01-15 14:00:25 -0600 | [diff] [blame] | 162 | { |
Angelo Durgehello | f0a0c88 | 2019-11-15 23:54:19 +0100 | [diff] [blame] | 163 | bd_t *bd = gd->bd; |
| 164 | |
TsiChungLiew | 95e8787 | 2008-01-15 14:00:25 -0600 | [diff] [blame] | 165 | if ((dup_spd >> 16) == FULL) { |
| 166 | /* Set maximum frame length */ |
| 167 | fecp->rcr = FEC_RCR_MAX_FL(PKT_MAXBUF_SIZE) | FEC_RCR_MII_MODE | |
| 168 | FEC_RCR_PROM | 0x100; |
| 169 | fecp->tcr = FEC_TCR_FDEN; |
| 170 | } else { |
| 171 | /* Half duplex mode */ |
| 172 | fecp->rcr = FEC_RCR_MAX_FL(PKT_MAXBUF_SIZE) | |
| 173 | FEC_RCR_MII_MODE | FEC_RCR_DRT; |
| 174 | fecp->tcr &= ~FEC_TCR_FDEN; |
| 175 | } |
| 176 | |
| 177 | if ((dup_spd & 0xFFFF) == _100BASET) { |
| 178 | #ifdef MII_DEBUG |
| 179 | printf("100Mbps\n"); |
| 180 | #endif |
| 181 | bd->bi_ethspeed = 100; |
| 182 | } else { |
| 183 | #ifdef MII_DEBUG |
| 184 | printf("10Mbps\n"); |
| 185 | #endif |
| 186 | bd->bi_ethspeed = 10; |
| 187 | } |
TsiChungLiew | 95e8787 | 2008-01-15 14:00:25 -0600 | [diff] [blame] | 188 | } |
| 189 | |
Angelo Durgehello | f0a0c88 | 2019-11-15 23:54:19 +0100 | [diff] [blame] | 190 | static void fec_set_hwaddr(volatile fecdma_t *fecp, u8 *mac) |
TsiChungLiew | 95e8787 | 2008-01-15 14:00:25 -0600 | [diff] [blame] | 191 | { |
Angelo Durgehello | f0a0c88 | 2019-11-15 23:54:19 +0100 | [diff] [blame] | 192 | u8 curr_byte; /* byte for which to compute the CRC */ |
TsiChungLiew | 95e8787 | 2008-01-15 14:00:25 -0600 | [diff] [blame] | 193 | int byte; /* loop - counter */ |
| 194 | int bit; /* loop - counter */ |
| 195 | u32 crc = 0xffffffff; /* initial value */ |
| 196 | |
| 197 | for (byte = 0; byte < 6; byte++) { |
Angelo Durgehello | f0a0c88 | 2019-11-15 23:54:19 +0100 | [diff] [blame] | 198 | curr_byte = mac[byte]; |
TsiChungLiew | 95e8787 | 2008-01-15 14:00:25 -0600 | [diff] [blame] | 199 | for (bit = 0; bit < 8; bit++) { |
Angelo Durgehello | f0a0c88 | 2019-11-15 23:54:19 +0100 | [diff] [blame] | 200 | if ((curr_byte & 0x01) ^ (crc & 0x01)) { |
TsiChungLiew | 95e8787 | 2008-01-15 14:00:25 -0600 | [diff] [blame] | 201 | crc >>= 1; |
| 202 | crc = crc ^ 0xedb88320; |
| 203 | } else { |
| 204 | crc >>= 1; |
| 205 | } |
Angelo Durgehello | f0a0c88 | 2019-11-15 23:54:19 +0100 | [diff] [blame] | 206 | curr_byte >>= 1; |
TsiChungLiew | 95e8787 | 2008-01-15 14:00:25 -0600 | [diff] [blame] | 207 | } |
| 208 | } |
| 209 | |
| 210 | crc = crc >> 26; |
| 211 | |
| 212 | /* Set individual hash table register */ |
| 213 | if (crc >= 32) { |
| 214 | fecp->ialr = (1 << (crc - 32)); |
| 215 | fecp->iaur = 0; |
| 216 | } else { |
| 217 | fecp->ialr = 0; |
| 218 | fecp->iaur = (1 << crc); |
| 219 | } |
| 220 | |
| 221 | /* Set physical address */ |
| 222 | fecp->palr = (mac[0] << 24) + (mac[1] << 16) + (mac[2] << 8) + mac[3]; |
| 223 | fecp->paur = (mac[4] << 24) + (mac[5] << 16) + 0x8808; |
| 224 | |
| 225 | /* Clear multicast address hash table */ |
| 226 | fecp->gaur = 0; |
| 227 | fecp->galr = 0; |
| 228 | } |
| 229 | |
Angelo Durgehello | f0a0c88 | 2019-11-15 23:54:19 +0100 | [diff] [blame] | 230 | static int fec_init(struct udevice *dev) |
TsiChungLiew | 95e8787 | 2008-01-15 14:00:25 -0600 | [diff] [blame] | 231 | { |
| 232 | struct fec_info_dma *info = dev->priv; |
Angelo Durgehello | f0a0c88 | 2019-11-15 23:54:19 +0100 | [diff] [blame] | 233 | volatile fecdma_t *fecp = (fecdma_t *)info->iobase; |
| 234 | int rval, i; |
Mike Frysinger | b203965 | 2009-02-11 19:01:26 -0500 | [diff] [blame] | 235 | uchar enetaddr[6]; |
TsiChungLiew | 95e8787 | 2008-01-15 14:00:25 -0600 | [diff] [blame] | 236 | |
| 237 | #ifdef ET_DEBUG |
| 238 | printf("fec_init: iobase 0x%08x ...\n", info->iobase); |
| 239 | #endif |
| 240 | |
Angelo Durgehello | f0a0c88 | 2019-11-15 23:54:19 +0100 | [diff] [blame] | 241 | fecpin_setclear(info, 1); |
TsiChungLiew | 95e8787 | 2008-01-15 14:00:25 -0600 | [diff] [blame] | 242 | fec_halt(dev); |
| 243 | |
| 244 | #if defined(CONFIG_CMD_MII) || defined (CONFIG_MII) || \ |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 245 | defined (CONFIG_SYS_DISCOVER_PHY) |
TsiChungLiew | 95e8787 | 2008-01-15 14:00:25 -0600 | [diff] [blame] | 246 | |
| 247 | mii_init(); |
Angelo Durgehello | f0a0c88 | 2019-11-15 23:54:19 +0100 | [diff] [blame] | 248 | set_fec_duplex_speed(fecp, info->dup_spd); |
TsiChungLiew | 95e8787 | 2008-01-15 14:00:25 -0600 | [diff] [blame] | 249 | #else |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 250 | #ifndef CONFIG_SYS_DISCOVER_PHY |
Angelo Durgehello | f0a0c88 | 2019-11-15 23:54:19 +0100 | [diff] [blame] | 251 | set_fec_duplex_speed(fecp, (FECDUPLEX << 16) | FECSPEED); |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 252 | #endif /* ifndef CONFIG_SYS_DISCOVER_PHY */ |
TsiChungLiew | 95e8787 | 2008-01-15 14:00:25 -0600 | [diff] [blame] | 253 | #endif /* CONFIG_CMD_MII || CONFIG_MII */ |
| 254 | |
| 255 | /* We use strictly polling mode only */ |
| 256 | fecp->eimr = 0; |
| 257 | |
| 258 | /* Clear any pending interrupt */ |
| 259 | fecp->eir = 0xffffffff; |
| 260 | |
| 261 | /* Set station address */ |
Angelo Durgehello | f0a0c88 | 2019-11-15 23:54:19 +0100 | [diff] [blame] | 262 | if (info->index == 0) |
| 263 | rval = eth_env_get_enetaddr("ethaddr", enetaddr); |
Mike Frysinger | b203965 | 2009-02-11 19:01:26 -0500 | [diff] [blame] | 264 | else |
Angelo Durgehello | f0a0c88 | 2019-11-15 23:54:19 +0100 | [diff] [blame] | 265 | rval = eth_env_get_enetaddr("eth1addr", enetaddr); |
| 266 | |
| 267 | if (!rval) { |
| 268 | puts("Please set a valid MAC address\n"); |
| 269 | return -EINVAL; |
| 270 | } |
| 271 | |
Mike Frysinger | b203965 | 2009-02-11 19:01:26 -0500 | [diff] [blame] | 272 | fec_set_hwaddr(fecp, enetaddr); |
TsiChungLiew | 95e8787 | 2008-01-15 14:00:25 -0600 | [diff] [blame] | 273 | |
| 274 | /* Set Opcode/Pause Duration Register */ |
| 275 | fecp->opd = 0x00010020; |
| 276 | |
Heinrich Schuchardt | 4237696 | 2017-08-29 18:44:37 +0200 | [diff] [blame] | 277 | /* Setup Buffers and Buffer Descriptors */ |
Angelo Durgehello | f0a0c88 | 2019-11-15 23:54:19 +0100 | [diff] [blame] | 278 | info->rx_idx = 0; |
| 279 | info->tx_idx = 0; |
TsiChungLiew | 95e8787 | 2008-01-15 14:00:25 -0600 | [diff] [blame] | 280 | |
| 281 | /* Setup Receiver Buffer Descriptors (13.14.24.18) |
| 282 | * Settings: Empty, Wrap */ |
| 283 | for (i = 0; i < PKTBUFSRX; i++) { |
| 284 | info->rxbd[i].cbd_sc = BD_ENET_RX_EMPTY; |
| 285 | info->rxbd[i].cbd_datlen = PKTSIZE_ALIGN; |
Joe Hershberger | 9f09a36 | 2015-04-08 01:41:06 -0500 | [diff] [blame] | 286 | info->rxbd[i].cbd_bufaddr = (uint) net_rx_packets[i]; |
TsiChungLiew | 95e8787 | 2008-01-15 14:00:25 -0600 | [diff] [blame] | 287 | } |
| 288 | info->rxbd[PKTBUFSRX - 1].cbd_sc |= BD_ENET_RX_WRAP; |
| 289 | |
| 290 | /* Setup Ethernet Transmitter Buffer Descriptors (13.14.24.19) |
| 291 | * Settings: Last, Tx CRC */ |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 292 | for (i = 0; i < CONFIG_SYS_TX_ETH_BUFFER; i++) { |
TsiChungLiew | 95e8787 | 2008-01-15 14:00:25 -0600 | [diff] [blame] | 293 | info->txbd[i].cbd_sc = 0; |
| 294 | info->txbd[i].cbd_datlen = 0; |
| 295 | info->txbd[i].cbd_bufaddr = (uint) (&info->txbuf[0]); |
| 296 | } |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 297 | info->txbd[CONFIG_SYS_TX_ETH_BUFFER - 1].cbd_sc |= BD_ENET_TX_WRAP; |
TsiChungLiew | 95e8787 | 2008-01-15 14:00:25 -0600 | [diff] [blame] | 298 | |
Angelo Durgehello | f0a0c88 | 2019-11-15 23:54:19 +0100 | [diff] [blame] | 299 | info->used_tbd_idx = 0; |
| 300 | info->clean_tbd_num = CONFIG_SYS_TX_ETH_BUFFER; |
TsiChungLiew | 95e8787 | 2008-01-15 14:00:25 -0600 | [diff] [blame] | 301 | |
| 302 | /* Set Rx FIFO alarm and granularity value */ |
| 303 | fecp->rfcr = 0x0c000000; |
| 304 | fecp->rfar = 0x0000030c; |
| 305 | |
| 306 | /* Set Tx FIFO granularity value */ |
| 307 | fecp->tfcr = FIFO_CTRL_FRAME | FIFO_CTRL_GR(6) | 0x00040000; |
| 308 | fecp->tfar = 0x00000080; |
| 309 | |
| 310 | fecp->tfwr = 0x2; |
| 311 | fecp->ctcwr = 0x03000000; |
| 312 | |
| 313 | /* Enable DMA receive task */ |
Angelo Durgehello | f0a0c88 | 2019-11-15 23:54:19 +0100 | [diff] [blame] | 314 | MCD_startDma(info->rx_task, |
| 315 | (s8 *)info->rxbd, |
| 316 | 0, |
| 317 | (s8 *)&fecp->rfdr, |
| 318 | 4, |
| 319 | 0, |
| 320 | 4, |
| 321 | info->rx_init, |
| 322 | info->rx_pri, |
| 323 | (MCD_FECRX_DMA | MCD_TT_FLAGS_DEF), |
| 324 | (MCD_NO_CSUM | MCD_NO_BYTE_SWAP) |
TsiChungLiew | 95e8787 | 2008-01-15 14:00:25 -0600 | [diff] [blame] | 325 | ); |
| 326 | |
| 327 | /* Enable DMA tx task with no ready buffer descriptors */ |
Angelo Durgehello | f0a0c88 | 2019-11-15 23:54:19 +0100 | [diff] [blame] | 328 | MCD_startDma(info->tx_task, |
| 329 | (s8 *)info->txbd, |
| 330 | 0, |
| 331 | (s8 *)&fecp->tfdr, |
| 332 | 4, |
| 333 | 0, |
| 334 | 4, |
| 335 | info->tx_init, |
| 336 | info->tx_pri, |
| 337 | (MCD_FECTX_DMA | MCD_TT_FLAGS_DEF), |
| 338 | (MCD_NO_CSUM | MCD_NO_BYTE_SWAP) |
TsiChungLiew | 95e8787 | 2008-01-15 14:00:25 -0600 | [diff] [blame] | 339 | ); |
| 340 | |
| 341 | /* Now enable the transmit and receive processing */ |
| 342 | fecp->ecr |= FEC_ECR_ETHER_EN; |
| 343 | |
Angelo Durgehello | f0a0c88 | 2019-11-15 23:54:19 +0100 | [diff] [blame] | 344 | return 0; |
| 345 | } |
| 346 | |
| 347 | static int mcdmafec_init(struct udevice *dev) |
| 348 | { |
| 349 | return fec_init(dev); |
TsiChungLiew | 95e8787 | 2008-01-15 14:00:25 -0600 | [diff] [blame] | 350 | } |
| 351 | |
Angelo Durgehello | f0a0c88 | 2019-11-15 23:54:19 +0100 | [diff] [blame] | 352 | static int mcdmafec_send(struct udevice *dev, void *packet, int length) |
TsiChungLiew | 95e8787 | 2008-01-15 14:00:25 -0600 | [diff] [blame] | 353 | { |
| 354 | struct fec_info_dma *info = dev->priv; |
Angelo Durgehello | f0a0c88 | 2019-11-15 23:54:19 +0100 | [diff] [blame] | 355 | cbd_t *p_tbd, *p_used_tbd; |
| 356 | u16 phy_status; |
TsiChungLiew | 95e8787 | 2008-01-15 14:00:25 -0600 | [diff] [blame] | 357 | |
Angelo Durgehello | f0a0c88 | 2019-11-15 23:54:19 +0100 | [diff] [blame] | 358 | miiphy_read(dev->name, info->phy_addr, MII_BMSR, &phy_status); |
TsiChungLiew | 95e8787 | 2008-01-15 14:00:25 -0600 | [diff] [blame] | 359 | |
Angelo Durgehello | f0a0c88 | 2019-11-15 23:54:19 +0100 | [diff] [blame] | 360 | /* process all the consumed TBDs */ |
| 361 | while (info->clean_tbd_num < CONFIG_SYS_TX_ETH_BUFFER) { |
| 362 | p_used_tbd = &info->txbd[info->used_tbd_idx]; |
| 363 | if (p_used_tbd->cbd_sc & BD_ENET_TX_READY) { |
| 364 | #ifdef ET_DEBUG |
| 365 | printf("Cannot clean TBD %d, in use\n", |
| 366 | info->clean_tbd_num); |
| 367 | #endif |
| 368 | return 0; |
| 369 | } |
TsiChungLiew | 95e8787 | 2008-01-15 14:00:25 -0600 | [diff] [blame] | 370 | |
Angelo Durgehello | f0a0c88 | 2019-11-15 23:54:19 +0100 | [diff] [blame] | 371 | /* clean this buffer descriptor */ |
| 372 | if (info->used_tbd_idx == (CONFIG_SYS_TX_ETH_BUFFER - 1)) |
| 373 | p_used_tbd->cbd_sc = BD_ENET_TX_WRAP; |
| 374 | else |
| 375 | p_used_tbd->cbd_sc = 0; |
TsiChungLiew | 95e8787 | 2008-01-15 14:00:25 -0600 | [diff] [blame] | 376 | |
Angelo Durgehello | f0a0c88 | 2019-11-15 23:54:19 +0100 | [diff] [blame] | 377 | /* update some indeces for a correct handling of TBD ring */ |
| 378 | info->clean_tbd_num++; |
| 379 | info->used_tbd_idx = (info->used_tbd_idx + 1) |
| 380 | % CONFIG_SYS_TX_ETH_BUFFER; |
| 381 | } |
TsiChungLiew | 95e8787 | 2008-01-15 14:00:25 -0600 | [diff] [blame] | 382 | |
Angelo Durgehello | f0a0c88 | 2019-11-15 23:54:19 +0100 | [diff] [blame] | 383 | /* Check for valid length of data. */ |
| 384 | if (length > 1500 || length <= 0) |
| 385 | return -1; |
TsiChungLiew | 95e8787 | 2008-01-15 14:00:25 -0600 | [diff] [blame] | 386 | |
Angelo Durgehello | f0a0c88 | 2019-11-15 23:54:19 +0100 | [diff] [blame] | 387 | /* Check the number of vacant TxBDs. */ |
| 388 | if (info->clean_tbd_num < 1) { |
| 389 | printf("No available TxBDs ...\n"); |
| 390 | return -1; |
| 391 | } |
TsiChungLiew | 95e8787 | 2008-01-15 14:00:25 -0600 | [diff] [blame] | 392 | |
Angelo Durgehello | f0a0c88 | 2019-11-15 23:54:19 +0100 | [diff] [blame] | 393 | /* Get the first TxBD to send the mac header */ |
| 394 | p_tbd = &info->txbd[info->tx_idx]; |
| 395 | p_tbd->cbd_datlen = length; |
| 396 | p_tbd->cbd_bufaddr = (u32)packet; |
| 397 | p_tbd->cbd_sc |= BD_ENET_TX_LAST | BD_ENET_TX_TC | BD_ENET_TX_READY; |
| 398 | info->tx_idx = (info->tx_idx + 1) % CONFIG_SYS_TX_ETH_BUFFER; |
TsiChungLiew | 95e8787 | 2008-01-15 14:00:25 -0600 | [diff] [blame] | 399 | |
Angelo Durgehello | f0a0c88 | 2019-11-15 23:54:19 +0100 | [diff] [blame] | 400 | /* Enable DMA transmit task */ |
| 401 | MCD_continDma(info->tx_task); |
TsiChungLiew | 95e8787 | 2008-01-15 14:00:25 -0600 | [diff] [blame] | 402 | |
Angelo Durgehello | f0a0c88 | 2019-11-15 23:54:19 +0100 | [diff] [blame] | 403 | info->clean_tbd_num -= 1; |
| 404 | |
| 405 | /* wait until frame is sent . */ |
| 406 | while (p_tbd->cbd_sc & BD_ENET_TX_READY) |
| 407 | udelay(10); |
| 408 | |
| 409 | return (int)(info->txbd[info->tx_idx].cbd_sc & BD_ENET_TX_STATS); |
TsiChungLiew | 95e8787 | 2008-01-15 14:00:25 -0600 | [diff] [blame] | 410 | } |
| 411 | |
Angelo Durgehello | f0a0c88 | 2019-11-15 23:54:19 +0100 | [diff] [blame] | 412 | static int mcdmafec_recv(struct udevice *dev, int flags, uchar **packetp) |
TsiChungLiew | 95e8787 | 2008-01-15 14:00:25 -0600 | [diff] [blame] | 413 | { |
Angelo Durgehello | f0a0c88 | 2019-11-15 23:54:19 +0100 | [diff] [blame] | 414 | struct fec_info_dma *info = dev->priv; |
| 415 | volatile fecdma_t *fecp = (fecdma_t *)info->iobase; |
TsiChungLiew | 95e8787 | 2008-01-15 14:00:25 -0600 | [diff] [blame] | 416 | |
Angelo Durgehello | f0a0c88 | 2019-11-15 23:54:19 +0100 | [diff] [blame] | 417 | cbd_t *prbd = &info->rxbd[info->rx_idx]; |
| 418 | u32 ievent; |
| 419 | int frame_length, len = 0; |
TsiChungLiew | 95e8787 | 2008-01-15 14:00:25 -0600 | [diff] [blame] | 420 | |
Angelo Durgehello | f0a0c88 | 2019-11-15 23:54:19 +0100 | [diff] [blame] | 421 | /* Check if any critical events have happened */ |
| 422 | ievent = fecp->eir; |
| 423 | if (ievent != 0) { |
| 424 | fecp->eir = ievent; |
TsiChungLiew | 95e8787 | 2008-01-15 14:00:25 -0600 | [diff] [blame] | 425 | |
Angelo Durgehello | f0a0c88 | 2019-11-15 23:54:19 +0100 | [diff] [blame] | 426 | if (ievent & (FEC_EIR_BABT | FEC_EIR_TXERR | FEC_EIR_RXERR)) { |
| 427 | printf("fec_recv: error\n"); |
| 428 | fec_halt(dev); |
| 429 | fec_init(dev); |
| 430 | return 0; |
| 431 | } |
TsiChungLiew | 95e8787 | 2008-01-15 14:00:25 -0600 | [diff] [blame] | 432 | |
Angelo Durgehello | f0a0c88 | 2019-11-15 23:54:19 +0100 | [diff] [blame] | 433 | if (ievent & FEC_EIR_HBERR) { |
| 434 | /* Heartbeat error */ |
| 435 | fecp->tcr |= FEC_TCR_GTS; |
| 436 | } |
TsiChungLiew | 95e8787 | 2008-01-15 14:00:25 -0600 | [diff] [blame] | 437 | |
Angelo Durgehello | f0a0c88 | 2019-11-15 23:54:19 +0100 | [diff] [blame] | 438 | if (ievent & FEC_EIR_GRA) { |
| 439 | /* Graceful stop complete */ |
| 440 | if (fecp->tcr & FEC_TCR_GTS) { |
| 441 | printf("fec_recv: tcr_gts\n"); |
| 442 | fec_halt(dev); |
| 443 | fecp->tcr &= ~FEC_TCR_GTS; |
| 444 | fec_init(dev); |
| 445 | } |
| 446 | } |
| 447 | } |
TsiChungLiew | 95e8787 | 2008-01-15 14:00:25 -0600 | [diff] [blame] | 448 | |
Angelo Durgehello | f0a0c88 | 2019-11-15 23:54:19 +0100 | [diff] [blame] | 449 | if (!(prbd->cbd_sc & BD_ENET_RX_EMPTY)) { |
| 450 | if ((prbd->cbd_sc & BD_ENET_RX_LAST) && |
| 451 | !(prbd->cbd_sc & BD_ENET_RX_ERR) && |
| 452 | ((prbd->cbd_datlen - 4) > 14)) { |
| 453 | /* Get buffer address and size */ |
| 454 | frame_length = prbd->cbd_datlen - 4; |
TsiChungLiew | 95e8787 | 2008-01-15 14:00:25 -0600 | [diff] [blame] | 455 | |
Angelo Durgehello | f0a0c88 | 2019-11-15 23:54:19 +0100 | [diff] [blame] | 456 | /* Fill the buffer and pass it to upper layers */ |
| 457 | net_process_received_packet((uchar *)prbd->cbd_bufaddr, |
| 458 | frame_length); |
| 459 | len = frame_length; |
| 460 | } |
TsiChungLiew | 95e8787 | 2008-01-15 14:00:25 -0600 | [diff] [blame] | 461 | |
Angelo Durgehello | f0a0c88 | 2019-11-15 23:54:19 +0100 | [diff] [blame] | 462 | /* Reset buffer descriptor as empty */ |
| 463 | if (info->rx_idx == (PKTBUFSRX - 1)) |
| 464 | prbd->cbd_sc = (BD_ENET_RX_WRAP | BD_ENET_RX_EMPTY); |
| 465 | else |
| 466 | prbd->cbd_sc = BD_ENET_RX_EMPTY; |
| 467 | |
| 468 | prbd->cbd_datlen = PKTSIZE_ALIGN; |
TsiChungLiew | 95e8787 | 2008-01-15 14:00:25 -0600 | [diff] [blame] | 469 | |
Angelo Durgehello | f0a0c88 | 2019-11-15 23:54:19 +0100 | [diff] [blame] | 470 | /* Now, we have an empty RxBD, restart the DMA receive task */ |
| 471 | MCD_continDma(info->rx_task); |
| 472 | |
| 473 | /* Increment BD count */ |
| 474 | info->rx_idx = (info->rx_idx + 1) % PKTBUFSRX; |
| 475 | } |
| 476 | |
| 477 | return len; |
| 478 | } |
| 479 | |
| 480 | static void mcdmafec_halt(struct udevice *dev) |
| 481 | { |
| 482 | fec_halt(dev); |
| 483 | } |
| 484 | |
| 485 | static const struct eth_ops mcdmafec_ops = { |
| 486 | .start = mcdmafec_init, |
| 487 | .send = mcdmafec_send, |
| 488 | .recv = mcdmafec_recv, |
| 489 | .stop = mcdmafec_halt, |
| 490 | }; |
| 491 | |
| 492 | /* |
| 493 | * Boot sequence, called just after mcffec_ofdata_to_platdata, |
| 494 | * as DM way, it replaces old mcffec_initialize. |
| 495 | */ |
| 496 | static int mcdmafec_probe(struct udevice *dev) |
| 497 | { |
| 498 | struct fec_info_dma *info = dev->priv; |
| 499 | struct eth_pdata *pdata = dev_get_platdata(dev); |
| 500 | int node = dev_of_offset(dev); |
| 501 | int retval; |
| 502 | const u32 *val; |
| 503 | |
| 504 | info->index = dev->seq; |
| 505 | info->iobase = pdata->iobase; |
| 506 | info->miibase = pdata->iobase; |
| 507 | info->phy_addr = -1; |
| 508 | |
| 509 | val = fdt_getprop(gd->fdt_blob, node, "rx-task", NULL); |
| 510 | if (val) |
| 511 | info->rx_task = fdt32_to_cpu(*val); |
| 512 | |
| 513 | val = fdt_getprop(gd->fdt_blob, node, "tx-task", NULL); |
| 514 | if (val) |
| 515 | info->tx_task = fdt32_to_cpu(*val); |
| 516 | |
| 517 | val = fdt_getprop(gd->fdt_blob, node, "rx-prioprity", NULL); |
| 518 | if (val) |
| 519 | info->rx_pri = fdt32_to_cpu(*val); |
| 520 | |
| 521 | val = fdt_getprop(gd->fdt_blob, node, "tx-prioprity", NULL); |
| 522 | if (val) |
| 523 | info->tx_pri = fdt32_to_cpu(*val); |
| 524 | |
| 525 | val = fdt_getprop(gd->fdt_blob, node, "rx-init", NULL); |
| 526 | if (val) |
| 527 | info->rx_init = fdt32_to_cpu(*val); |
| 528 | |
| 529 | val = fdt_getprop(gd->fdt_blob, node, "tx-init", NULL); |
| 530 | if (val) |
| 531 | info->tx_init = fdt32_to_cpu(*val); |
| 532 | |
| 533 | #ifdef CONFIG_SYS_FEC_BUF_USE_SRAM |
| 534 | u32 tmp = CONFIG_SYS_INIT_RAM_ADDR + 0x1000; |
| 535 | #endif |
| 536 | init_eth_info(info); |
TsiChungLiew | 95e8787 | 2008-01-15 14:00:25 -0600 | [diff] [blame] | 537 | |
| 538 | #if defined(CONFIG_MII) || defined(CONFIG_CMD_MII) |
Angelo Durgehello | f0a0c88 | 2019-11-15 23:54:19 +0100 | [diff] [blame] | 539 | info->bus = mdio_alloc(); |
| 540 | if (!info->bus) |
| 541 | return -ENOMEM; |
| 542 | strncpy(info->bus->name, dev->name, MDIO_NAME_LEN); |
| 543 | info->bus->read = mcffec_miiphy_read; |
| 544 | info->bus->write = mcffec_miiphy_write; |
Joe Hershberger | 1fbcbed | 2016-08-08 11:28:38 -0500 | [diff] [blame] | 545 | |
Angelo Durgehello | f0a0c88 | 2019-11-15 23:54:19 +0100 | [diff] [blame] | 546 | retval = mdio_register(info->bus); |
| 547 | if (retval < 0) |
| 548 | return retval; |
TsiChungLiew | 95e8787 | 2008-01-15 14:00:25 -0600 | [diff] [blame] | 549 | #endif |
| 550 | |
Angelo Durgehello | f0a0c88 | 2019-11-15 23:54:19 +0100 | [diff] [blame] | 551 | return 0; |
| 552 | } |
TsiChungLiew | 95e8787 | 2008-01-15 14:00:25 -0600 | [diff] [blame] | 553 | |
Angelo Durgehello | f0a0c88 | 2019-11-15 23:54:19 +0100 | [diff] [blame] | 554 | static int mcdmafec_remove(struct udevice *dev) |
| 555 | { |
| 556 | struct fec_info_dma *priv = dev_get_priv(dev); |
| 557 | |
| 558 | mdio_unregister(priv->bus); |
| 559 | mdio_free(priv->bus); |
TsiChungLiew | 95e8787 | 2008-01-15 14:00:25 -0600 | [diff] [blame] | 560 | |
Ben Warren | e7edd4e | 2008-08-26 22:12:36 -0700 | [diff] [blame] | 561 | return 0; |
TsiChungLiew | 95e8787 | 2008-01-15 14:00:25 -0600 | [diff] [blame] | 562 | } |
Angelo Durgehello | f0a0c88 | 2019-11-15 23:54:19 +0100 | [diff] [blame] | 563 | |
| 564 | /* |
| 565 | * Boot sequence, called 1st |
| 566 | */ |
| 567 | static int mcdmafec_ofdata_to_platdata(struct udevice *dev) |
| 568 | { |
| 569 | struct eth_pdata *pdata = dev_get_platdata(dev); |
| 570 | const u32 *val; |
| 571 | |
| 572 | pdata->iobase = (phys_addr_t)devfdt_get_addr(dev); |
| 573 | /* Default to 10Mbit/s */ |
| 574 | pdata->max_speed = 10; |
| 575 | |
| 576 | val = fdt_getprop(gd->fdt_blob, dev_of_offset(dev), "max-speed", NULL); |
| 577 | if (val) |
| 578 | pdata->max_speed = fdt32_to_cpu(*val); |
| 579 | |
| 580 | return 0; |
| 581 | } |
| 582 | |
| 583 | static const struct udevice_id mcdmafec_ids[] = { |
| 584 | { .compatible = "fsl,mcf-dma-fec" }, |
| 585 | { } |
| 586 | }; |
| 587 | |
| 588 | U_BOOT_DRIVER(mcffec) = { |
| 589 | .name = "mcdmafec", |
| 590 | .id = UCLASS_ETH, |
| 591 | .of_match = mcdmafec_ids, |
| 592 | .ofdata_to_platdata = mcdmafec_ofdata_to_platdata, |
| 593 | .probe = mcdmafec_probe, |
| 594 | .remove = mcdmafec_remove, |
| 595 | .ops = &mcdmafec_ops, |
| 596 | .priv_auto_alloc_size = sizeof(struct fec_info_dma), |
| 597 | .platdata_auto_alloc_size = sizeof(struct eth_pdata), |
| 598 | }; |