Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
wdenk | 7ac1610 | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 2 | /* |
Marek Vasut | a4aa048 | 2022-04-13 04:15:29 +0200 | [diff] [blame] | 3 | * dm9000.c: Version 1.2 12/15/2003 |
| 4 | * |
| 5 | * A Davicom DM9000 ISA NIC fast Ethernet driver for Linux. |
| 6 | * Copyright (C) 1997 Sten Wang |
| 7 | * |
| 8 | * (C)Copyright 1997-1998 DAVICOM Semiconductor,Inc. All Rights Reserved. |
| 9 | * |
| 10 | * V0.11 06/20/2001 REG_0A bit3=1, default enable BP with DA match |
| 11 | * 06/22/2001 Support DM9801 progrmming |
| 12 | * E3: R25 = ((R24 + NF) & 0x00ff) | 0xf000 |
| 13 | * E4: R25 = ((R24 + NF) & 0x00ff) | 0xc200 |
| 14 | * R17 = (R17 & 0xfff0) | NF + 3 |
| 15 | * E5: R25 = ((R24 + NF - 3) & 0x00ff) | 0xc200 |
| 16 | * R17 = (R17 & 0xfff0) | NF |
| 17 | * |
| 18 | * v1.00 modify by simon 2001.9.5 |
| 19 | * change for kernel 2.4.x |
| 20 | * |
| 21 | * v1.1 11/09/2001 fix force mode bug |
| 22 | * |
| 23 | * v1.2 03/18/2003 Weilun Huang <weilun_huang@davicom.com.tw>: |
| 24 | * Fixed phy reset. |
| 25 | * Added tx/rx 32 bit mode. |
| 26 | * Cleaned up for kernel merge. |
| 27 | * |
| 28 | * -------------------------------------- |
| 29 | * |
| 30 | * 12/15/2003 Initial port to u-boot by |
| 31 | * Sascha Hauer <saschahauer@web.de> |
| 32 | * |
| 33 | * 06/03/2008 Remy Bohmer <linux@bohmer.net> |
| 34 | * - Fixed the driver to work with DM9000A. |
| 35 | * (check on ISR receive status bit before reading the |
| 36 | * FIFO as described in DM9000 programming guide and |
| 37 | * application notes) |
| 38 | * - Added autodetect of databus width. |
| 39 | * - Made debug code compile again. |
| 40 | * - Adapt eth_send such that it matches the DM9000* |
| 41 | * application notes. Needed to make it work properly |
| 42 | * for DM9000A. |
| 43 | * - Adapted reset procedure to match DM9000 application |
| 44 | * notes (i.e. double reset) |
| 45 | * - some minor code cleanups |
| 46 | * These changes are tested with DM9000{A,EP,E} together |
| 47 | * with a 200MHz Atmel AT91SAM9261 core |
| 48 | * |
| 49 | * TODO: external MII is not functional, only internal at the moment. |
| 50 | */ |
wdenk | 7ac1610 | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 51 | |
| 52 | #include <common.h> |
| 53 | #include <command.h> |
Marek Vasut | 9c5e9ca | 2022-04-13 04:15:32 +0200 | [diff] [blame^] | 54 | #include <malloc.h> |
wdenk | 7ac1610 | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 55 | #include <net.h> |
| 56 | #include <asm/io.h> |
Remy Bohmer | cd9a36c | 2009-05-03 12:11:40 +0200 | [diff] [blame] | 57 | #include <dm9000.h> |
Simon Glass | dbd7954 | 2020-05-10 11:40:11 -0600 | [diff] [blame] | 58 | #include <linux/delay.h> |
wdenk | 7ac1610 | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 59 | |
wdenk | 7ac1610 | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 60 | #include "dm9000x.h" |
| 61 | |
wdenk | 7ac1610 | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 62 | /* Structure/enum declaration ------------------------------- */ |
Marek Vasut | 09a8a9c | 2022-04-13 04:15:31 +0200 | [diff] [blame] | 63 | struct dm9000_priv { |
wdenk | 7ac1610 | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 64 | u32 runt_length_counter; /* counter: RX length < 64byte */ |
| 65 | u32 long_length_counter; /* counter: RX length > 1514byte */ |
| 66 | u32 reset_counter; /* counter: RESET */ |
| 67 | u32 reset_tx_timeout; /* RESET caused by TX Timeout */ |
| 68 | u32 reset_rx_status; /* RESET caused by RX Statsus wrong */ |
| 69 | u16 tx_pkt_cnt; |
| 70 | u16 queue_start_addr; |
| 71 | u16 dbug_cnt; |
| 72 | u8 phy_addr; |
| 73 | u8 device_wait_reset; /* device state */ |
wdenk | 7ac1610 | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 74 | unsigned char srom[128]; |
Marek Vasut | 7823b23 | 2022-04-13 04:15:28 +0200 | [diff] [blame] | 75 | void (*outblk)(void *data_ptr, int count); |
Remy Bohmer | 5f63bf4 | 2008-06-03 15:26:21 +0200 | [diff] [blame] | 76 | void (*inblk)(void *data_ptr, int count); |
Marek Vasut | 5248e56 | 2022-04-13 04:15:25 +0200 | [diff] [blame] | 77 | void (*rx_status)(u16 *rxstatus, u16 *rxlen); |
Marek Vasut | 9c5e9ca | 2022-04-13 04:15:32 +0200 | [diff] [blame^] | 78 | struct eth_device dev; |
Marek Vasut | a4aa048 | 2022-04-13 04:15:29 +0200 | [diff] [blame] | 79 | }; |
wdenk | 7ac1610 | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 80 | |
wdenk | 7ac1610 | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 81 | /* DM9000 network board routine ---------------------------- */ |
Jason Jin | c74c436 | 2011-08-25 15:46:43 +0800 | [diff] [blame] | 82 | #ifndef CONFIG_DM9000_BYTE_SWAPPED |
Marek Vasut | a4aa048 | 2022-04-13 04:15:29 +0200 | [diff] [blame] | 83 | #define dm9000_outb(d, r) writeb((d), (r)) |
| 84 | #define dm9000_outw(d, r) writew((d), (r)) |
| 85 | #define dm9000_outl(d, r) writel((d), (r)) |
Marek Vasut | 7823b23 | 2022-04-13 04:15:28 +0200 | [diff] [blame] | 86 | #define dm9000_inb(r) readb(r) |
| 87 | #define dm9000_inw(r) readw(r) |
| 88 | #define dm9000_inl(r) readl(r) |
Jason Jin | c74c436 | 2011-08-25 15:46:43 +0800 | [diff] [blame] | 89 | #else |
Marek Vasut | eb2749a | 2022-04-13 04:15:23 +0200 | [diff] [blame] | 90 | #define dm9000_outb(d, r) __raw_writeb(d, r) |
| 91 | #define dm9000_outw(d, r) __raw_writew(d, r) |
| 92 | #define dm9000_outl(d, r) __raw_writel(d, r) |
| 93 | #define dm9000_inb(r) __raw_readb(r) |
| 94 | #define dm9000_inw(r) __raw_readw(r) |
| 95 | #define dm9000_inl(r) __raw_readl(r) |
Jason Jin | c74c436 | 2011-08-25 15:46:43 +0800 | [diff] [blame] | 96 | #endif |
wdenk | 7ac1610 | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 97 | |
Marek Vasut | 52006d2 | 2022-04-13 04:15:27 +0200 | [diff] [blame] | 98 | #ifdef DEBUG |
| 99 | static void dm9000_dump_packet(const char *func, u8 *packet, int length) |
| 100 | { |
| 101 | int i; |
| 102 | |
| 103 | printf("%s: length: %d\n", func, length); |
| 104 | |
| 105 | for (i = 0; i < length; i++) { |
| 106 | if (i % 8 == 0) |
| 107 | printf("\n%s: %02x: ", func, i); |
| 108 | printf("%02x ", packet[i]); |
| 109 | } |
| 110 | |
| 111 | printf("\n"); |
| 112 | } |
| 113 | #else |
| 114 | static void dm9000_dump_packet(const char *func, u8 *packet, int length) {} |
| 115 | #endif |
| 116 | |
Marek Vasut | 7823b23 | 2022-04-13 04:15:28 +0200 | [diff] [blame] | 117 | static void dm9000_outblk_8bit(void *data_ptr, int count) |
Remy Bohmer | 5f63bf4 | 2008-06-03 15:26:21 +0200 | [diff] [blame] | 118 | { |
| 119 | int i; |
Marek Vasut | a4aa048 | 2022-04-13 04:15:29 +0200 | [diff] [blame] | 120 | |
Remy Bohmer | 5f63bf4 | 2008-06-03 15:26:21 +0200 | [diff] [blame] | 121 | for (i = 0; i < count; i++) |
Marek Vasut | a4aa048 | 2022-04-13 04:15:29 +0200 | [diff] [blame] | 122 | dm9000_outb((((u8 *)data_ptr)[i] & 0xff), DM9000_DATA); |
Remy Bohmer | 5f63bf4 | 2008-06-03 15:26:21 +0200 | [diff] [blame] | 123 | } |
| 124 | |
Marek Vasut | 7823b23 | 2022-04-13 04:15:28 +0200 | [diff] [blame] | 125 | static void dm9000_outblk_16bit(void *data_ptr, int count) |
Remy Bohmer | 5f63bf4 | 2008-06-03 15:26:21 +0200 | [diff] [blame] | 126 | { |
| 127 | int i; |
| 128 | u32 tmplen = (count + 1) / 2; |
| 129 | |
| 130 | for (i = 0; i < tmplen; i++) |
Marek Vasut | a4aa048 | 2022-04-13 04:15:29 +0200 | [diff] [blame] | 131 | dm9000_outw(((u16 *)data_ptr)[i], DM9000_DATA); |
Remy Bohmer | 5f63bf4 | 2008-06-03 15:26:21 +0200 | [diff] [blame] | 132 | } |
Marek Vasut | a4aa048 | 2022-04-13 04:15:29 +0200 | [diff] [blame] | 133 | |
Marek Vasut | 7823b23 | 2022-04-13 04:15:28 +0200 | [diff] [blame] | 134 | static void dm9000_outblk_32bit(void *data_ptr, int count) |
Remy Bohmer | 5f63bf4 | 2008-06-03 15:26:21 +0200 | [diff] [blame] | 135 | { |
| 136 | int i; |
| 137 | u32 tmplen = (count + 3) / 4; |
| 138 | |
| 139 | for (i = 0; i < tmplen; i++) |
Marek Vasut | a4aa048 | 2022-04-13 04:15:29 +0200 | [diff] [blame] | 140 | dm9000_outl(((u32 *)data_ptr)[i], DM9000_DATA); |
Remy Bohmer | 5f63bf4 | 2008-06-03 15:26:21 +0200 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | static void dm9000_inblk_8bit(void *data_ptr, int count) |
| 144 | { |
| 145 | int i; |
Marek Vasut | a4aa048 | 2022-04-13 04:15:29 +0200 | [diff] [blame] | 146 | |
Remy Bohmer | 5f63bf4 | 2008-06-03 15:26:21 +0200 | [diff] [blame] | 147 | for (i = 0; i < count; i++) |
Marek Vasut | a4aa048 | 2022-04-13 04:15:29 +0200 | [diff] [blame] | 148 | ((u8 *)data_ptr)[i] = dm9000_inb(DM9000_DATA); |
Remy Bohmer | 5f63bf4 | 2008-06-03 15:26:21 +0200 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | static void dm9000_inblk_16bit(void *data_ptr, int count) |
| 152 | { |
| 153 | int i; |
| 154 | u32 tmplen = (count + 1) / 2; |
| 155 | |
| 156 | for (i = 0; i < tmplen; i++) |
Marek Vasut | a4aa048 | 2022-04-13 04:15:29 +0200 | [diff] [blame] | 157 | ((u16 *)data_ptr)[i] = dm9000_inw(DM9000_DATA); |
Remy Bohmer | 5f63bf4 | 2008-06-03 15:26:21 +0200 | [diff] [blame] | 158 | } |
Marek Vasut | a4aa048 | 2022-04-13 04:15:29 +0200 | [diff] [blame] | 159 | |
Remy Bohmer | 5f63bf4 | 2008-06-03 15:26:21 +0200 | [diff] [blame] | 160 | static void dm9000_inblk_32bit(void *data_ptr, int count) |
| 161 | { |
| 162 | int i; |
| 163 | u32 tmplen = (count + 3) / 4; |
| 164 | |
| 165 | for (i = 0; i < tmplen; i++) |
Marek Vasut | a4aa048 | 2022-04-13 04:15:29 +0200 | [diff] [blame] | 166 | ((u32 *)data_ptr)[i] = dm9000_inl(DM9000_DATA); |
Remy Bohmer | 5f63bf4 | 2008-06-03 15:26:21 +0200 | [diff] [blame] | 167 | } |
| 168 | |
Marek Vasut | 5248e56 | 2022-04-13 04:15:25 +0200 | [diff] [blame] | 169 | static void dm9000_rx_status_32bit(u16 *rxstatus, u16 *rxlen) |
Remy Bohmer | 5f63bf4 | 2008-06-03 15:26:21 +0200 | [diff] [blame] | 170 | { |
Remy Bohmer | 2e1604f | 2008-06-04 10:47:25 +0200 | [diff] [blame] | 171 | u32 tmpdata; |
Remy Bohmer | 5f63bf4 | 2008-06-03 15:26:21 +0200 | [diff] [blame] | 172 | |
Marek Vasut | eb2749a | 2022-04-13 04:15:23 +0200 | [diff] [blame] | 173 | dm9000_outb(DM9000_MRCMD, DM9000_IO); |
Remy Bohmer | 5f63bf4 | 2008-06-03 15:26:21 +0200 | [diff] [blame] | 174 | |
Marek Vasut | eb2749a | 2022-04-13 04:15:23 +0200 | [diff] [blame] | 175 | tmpdata = dm9000_inl(DM9000_DATA); |
Marek Vasut | 5248e56 | 2022-04-13 04:15:25 +0200 | [diff] [blame] | 176 | *rxstatus = __le16_to_cpu(tmpdata); |
| 177 | *rxlen = __le16_to_cpu(tmpdata >> 16); |
Remy Bohmer | 5f63bf4 | 2008-06-03 15:26:21 +0200 | [diff] [blame] | 178 | } |
| 179 | |
Marek Vasut | 5248e56 | 2022-04-13 04:15:25 +0200 | [diff] [blame] | 180 | static void dm9000_rx_status_16bit(u16 *rxstatus, u16 *rxlen) |
Remy Bohmer | 5f63bf4 | 2008-06-03 15:26:21 +0200 | [diff] [blame] | 181 | { |
Marek Vasut | eb2749a | 2022-04-13 04:15:23 +0200 | [diff] [blame] | 182 | dm9000_outb(DM9000_MRCMD, DM9000_IO); |
Remy Bohmer | 5f63bf4 | 2008-06-03 15:26:21 +0200 | [diff] [blame] | 183 | |
Marek Vasut | 5248e56 | 2022-04-13 04:15:25 +0200 | [diff] [blame] | 184 | *rxstatus = __le16_to_cpu(dm9000_inw(DM9000_DATA)); |
| 185 | *rxlen = __le16_to_cpu(dm9000_inw(DM9000_DATA)); |
Remy Bohmer | 5f63bf4 | 2008-06-03 15:26:21 +0200 | [diff] [blame] | 186 | } |
| 187 | |
Marek Vasut | 5248e56 | 2022-04-13 04:15:25 +0200 | [diff] [blame] | 188 | static void dm9000_rx_status_8bit(u16 *rxstatus, u16 *rxlen) |
Remy Bohmer | 5f63bf4 | 2008-06-03 15:26:21 +0200 | [diff] [blame] | 189 | { |
Marek Vasut | eb2749a | 2022-04-13 04:15:23 +0200 | [diff] [blame] | 190 | dm9000_outb(DM9000_MRCMD, DM9000_IO); |
Remy Bohmer | 5f63bf4 | 2008-06-03 15:26:21 +0200 | [diff] [blame] | 191 | |
Marek Vasut | 5248e56 | 2022-04-13 04:15:25 +0200 | [diff] [blame] | 192 | *rxstatus = |
Marek Vasut | eb2749a | 2022-04-13 04:15:23 +0200 | [diff] [blame] | 193 | __le16_to_cpu(dm9000_inb(DM9000_DATA) + |
| 194 | (dm9000_inb(DM9000_DATA) << 8)); |
Marek Vasut | 5248e56 | 2022-04-13 04:15:25 +0200 | [diff] [blame] | 195 | *rxlen = |
Marek Vasut | eb2749a | 2022-04-13 04:15:23 +0200 | [diff] [blame] | 196 | __le16_to_cpu(dm9000_inb(DM9000_DATA) + |
| 197 | (dm9000_inb(DM9000_DATA) << 8)); |
Remy Bohmer | 5f63bf4 | 2008-06-03 15:26:21 +0200 | [diff] [blame] | 198 | } |
wdenk | 7ac1610 | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 199 | |
| 200 | /* |
Marek Vasut | c5e9d64 | 2022-04-13 04:15:30 +0200 | [diff] [blame] | 201 | * Read a byte from I/O port |
| 202 | */ |
| 203 | static u8 dm9000_ior(int reg) |
| 204 | { |
| 205 | dm9000_outb(reg, DM9000_IO); |
| 206 | return dm9000_inb(DM9000_DATA); |
| 207 | } |
| 208 | |
| 209 | /* |
| 210 | * Write a byte to I/O port |
| 211 | */ |
| 212 | static void dm9000_iow(int reg, u8 value) |
| 213 | { |
| 214 | dm9000_outb(reg, DM9000_IO); |
| 215 | dm9000_outb(value, DM9000_DATA); |
| 216 | } |
| 217 | |
| 218 | /* |
| 219 | * Read a word from phyxcer |
| 220 | */ |
| 221 | static u16 dm9000_phy_read(int reg) |
| 222 | { |
| 223 | u16 val; |
| 224 | |
| 225 | /* Fill the phyxcer register into REG_0C */ |
| 226 | dm9000_iow(DM9000_EPAR, DM9000_PHY | reg); |
| 227 | dm9000_iow(DM9000_EPCR, 0xc); /* Issue phyxcer read command */ |
| 228 | udelay(100); /* Wait read complete */ |
| 229 | dm9000_iow(DM9000_EPCR, 0x0); /* Clear phyxcer read command */ |
| 230 | val = (dm9000_ior(DM9000_EPDRH) << 8) | dm9000_ior(DM9000_EPDRL); |
| 231 | |
| 232 | /* The read data keeps on REG_0D & REG_0E */ |
| 233 | debug("%s(0x%x): 0x%x\n", __func__, reg, val); |
| 234 | return val; |
| 235 | } |
| 236 | |
| 237 | /* |
| 238 | * Write a word to phyxcer |
| 239 | */ |
| 240 | static void dm9000_phy_write(int reg, u16 value) |
| 241 | { |
| 242 | /* Fill the phyxcer register into REG_0C */ |
| 243 | dm9000_iow(DM9000_EPAR, DM9000_PHY | reg); |
| 244 | |
| 245 | /* Fill the written data into REG_0D & REG_0E */ |
| 246 | dm9000_iow(DM9000_EPDRL, (value & 0xff)); |
| 247 | dm9000_iow(DM9000_EPDRH, ((value >> 8) & 0xff)); |
| 248 | dm9000_iow(DM9000_EPCR, 0xa); /* Issue phyxcer write command */ |
| 249 | udelay(500); /* Wait write complete */ |
| 250 | dm9000_iow(DM9000_EPCR, 0x0); /* Clear phyxcer write command */ |
| 251 | debug("%s(reg:0x%x, value:0x%x)\n", __func__, reg, value); |
| 252 | } |
| 253 | |
| 254 | /* |
Marek Vasut | a4aa048 | 2022-04-13 04:15:29 +0200 | [diff] [blame] | 255 | * Search DM9000 board, allocate space and register it |
| 256 | */ |
Marek Vasut | c5e9d64 | 2022-04-13 04:15:30 +0200 | [diff] [blame] | 257 | static int dm9000_probe(void) |
wdenk | 7ac1610 | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 258 | { |
| 259 | u32 id_val; |
Marek Vasut | a4aa048 | 2022-04-13 04:15:29 +0200 | [diff] [blame] | 260 | |
Marek Vasut | eb2749a | 2022-04-13 04:15:23 +0200 | [diff] [blame] | 261 | id_val = dm9000_ior(DM9000_VIDL); |
| 262 | id_val |= dm9000_ior(DM9000_VIDH) << 8; |
| 263 | id_val |= dm9000_ior(DM9000_PIDL) << 16; |
| 264 | id_val |= dm9000_ior(DM9000_PIDH) << 24; |
Marek Vasut | a4aa048 | 2022-04-13 04:15:29 +0200 | [diff] [blame] | 265 | if (id_val != DM9000_ID) { |
wdenk | 7ac1610 | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 266 | printf("dm9000 not found at 0x%08x id: 0x%08x\n", |
| 267 | CONFIG_DM9000_BASE, id_val); |
| 268 | return -1; |
| 269 | } |
Marek Vasut | a4aa048 | 2022-04-13 04:15:29 +0200 | [diff] [blame] | 270 | |
| 271 | printf("dm9000 i/o: 0x%x, id: 0x%x\n", CONFIG_DM9000_BASE, id_val); |
| 272 | return 0; |
wdenk | 7ac1610 | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 273 | } |
| 274 | |
| 275 | /* General Purpose dm9000 reset routine */ |
| 276 | static void |
| 277 | dm9000_reset(void) |
| 278 | { |
Marek Vasut | ed76122 | 2022-04-13 04:15:24 +0200 | [diff] [blame] | 279 | debug("resetting DM9000\n"); |
Remy Bohmer | 2f13d2c | 2008-06-03 15:26:24 +0200 | [diff] [blame] | 280 | |
Marek Vasut | a4aa048 | 2022-04-13 04:15:29 +0200 | [diff] [blame] | 281 | /* |
| 282 | * Reset DM9000, |
| 283 | * see DM9000 Application Notes V1.22 Jun 11, 2004 page 29 |
| 284 | */ |
Remy Bohmer | 2f13d2c | 2008-06-03 15:26:24 +0200 | [diff] [blame] | 285 | |
Andrew Dyer | a62f5d4 | 2008-08-26 17:03:38 -0500 | [diff] [blame] | 286 | /* DEBUG: Make all GPIO0 outputs, all others inputs */ |
Marek Vasut | eb2749a | 2022-04-13 04:15:23 +0200 | [diff] [blame] | 287 | dm9000_iow(DM9000_GPCR, GPCR_GPIO0_OUT); |
Remy Bohmer | 2f13d2c | 2008-06-03 15:26:24 +0200 | [diff] [blame] | 288 | /* Step 1: Power internal PHY by writing 0 to GPIO0 pin */ |
Marek Vasut | eb2749a | 2022-04-13 04:15:23 +0200 | [diff] [blame] | 289 | dm9000_iow(DM9000_GPR, 0); |
Remy Bohmer | 2f13d2c | 2008-06-03 15:26:24 +0200 | [diff] [blame] | 290 | /* Step 2: Software reset */ |
Marek Vasut | eb2749a | 2022-04-13 04:15:23 +0200 | [diff] [blame] | 291 | dm9000_iow(DM9000_NCR, (NCR_LBK_INT_MAC | NCR_RST)); |
Remy Bohmer | 2f13d2c | 2008-06-03 15:26:24 +0200 | [diff] [blame] | 292 | |
| 293 | do { |
Marek Vasut | ed76122 | 2022-04-13 04:15:24 +0200 | [diff] [blame] | 294 | debug("resetting the DM9000, 1st reset\n"); |
Remy Bohmer | 2f13d2c | 2008-06-03 15:26:24 +0200 | [diff] [blame] | 295 | udelay(25); /* Wait at least 20 us */ |
Marek Vasut | eb2749a | 2022-04-13 04:15:23 +0200 | [diff] [blame] | 296 | } while (dm9000_ior(DM9000_NCR) & 1); |
Remy Bohmer | 2f13d2c | 2008-06-03 15:26:24 +0200 | [diff] [blame] | 297 | |
Marek Vasut | eb2749a | 2022-04-13 04:15:23 +0200 | [diff] [blame] | 298 | dm9000_iow(DM9000_NCR, 0); |
| 299 | dm9000_iow(DM9000_NCR, (NCR_LBK_INT_MAC | NCR_RST)); /* Issue a second reset */ |
Remy Bohmer | 2f13d2c | 2008-06-03 15:26:24 +0200 | [diff] [blame] | 300 | |
| 301 | do { |
Marek Vasut | ed76122 | 2022-04-13 04:15:24 +0200 | [diff] [blame] | 302 | debug("resetting the DM9000, 2nd reset\n"); |
Remy Bohmer | 2f13d2c | 2008-06-03 15:26:24 +0200 | [diff] [blame] | 303 | udelay(25); /* Wait at least 20 us */ |
Marek Vasut | eb2749a | 2022-04-13 04:15:23 +0200 | [diff] [blame] | 304 | } while (dm9000_ior(DM9000_NCR) & 1); |
Remy Bohmer | 2f13d2c | 2008-06-03 15:26:24 +0200 | [diff] [blame] | 305 | |
| 306 | /* Check whether the ethernet controller is present */ |
Marek Vasut | eb2749a | 2022-04-13 04:15:23 +0200 | [diff] [blame] | 307 | if ((dm9000_ior(DM9000_PIDL) != 0x0) || |
| 308 | (dm9000_ior(DM9000_PIDH) != 0x90)) |
Remy Bohmer | 2f13d2c | 2008-06-03 15:26:24 +0200 | [diff] [blame] | 309 | printf("ERROR: resetting DM9000 -> not responding\n"); |
wdenk | 7ac1610 | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 310 | } |
| 311 | |
Marek Vasut | a4aa048 | 2022-04-13 04:15:29 +0200 | [diff] [blame] | 312 | /* Initialize dm9000 board */ |
Masahiro Yamada | f7ed78b | 2020-06-26 15:13:33 +0900 | [diff] [blame] | 313 | static int dm9000_init(struct eth_device *dev, struct bd_info *bd) |
wdenk | 7ac1610 | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 314 | { |
Marek Vasut | 9c5e9ca | 2022-04-13 04:15:32 +0200 | [diff] [blame^] | 315 | struct dm9000_priv *db = container_of(dev, struct dm9000_priv, dev); |
wdenk | 7ac1610 | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 316 | int i, oft, lnk; |
Remy Bohmer | 5f63bf4 | 2008-06-03 15:26:21 +0200 | [diff] [blame] | 317 | u8 io_mode; |
Remy Bohmer | 5f63bf4 | 2008-06-03 15:26:21 +0200 | [diff] [blame] | 318 | |
wdenk | 7ac1610 | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 319 | /* RESET device */ |
| 320 | dm9000_reset(); |
Andrew Dyer | a62f5d4 | 2008-08-26 17:03:38 -0500 | [diff] [blame] | 321 | |
| 322 | if (dm9000_probe() < 0) |
| 323 | return -1; |
wdenk | 7ac1610 | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 324 | |
Remy Bohmer | 5f63bf4 | 2008-06-03 15:26:21 +0200 | [diff] [blame] | 325 | /* Auto-detect 8/16/32 bit mode, ISR Bit 6+7 indicate bus width */ |
Marek Vasut | eb2749a | 2022-04-13 04:15:23 +0200 | [diff] [blame] | 326 | io_mode = dm9000_ior(DM9000_ISR) >> 6; |
Remy Bohmer | 5f63bf4 | 2008-06-03 15:26:21 +0200 | [diff] [blame] | 327 | |
| 328 | switch (io_mode) { |
| 329 | case 0x0: /* 16-bit mode */ |
| 330 | printf("DM9000: running in 16 bit mode\n"); |
| 331 | db->outblk = dm9000_outblk_16bit; |
| 332 | db->inblk = dm9000_inblk_16bit; |
| 333 | db->rx_status = dm9000_rx_status_16bit; |
| 334 | break; |
| 335 | case 0x01: /* 32-bit mode */ |
| 336 | printf("DM9000: running in 32 bit mode\n"); |
| 337 | db->outblk = dm9000_outblk_32bit; |
| 338 | db->inblk = dm9000_inblk_32bit; |
| 339 | db->rx_status = dm9000_rx_status_32bit; |
| 340 | break; |
| 341 | case 0x02: /* 8 bit mode */ |
| 342 | printf("DM9000: running in 8 bit mode\n"); |
| 343 | db->outblk = dm9000_outblk_8bit; |
| 344 | db->inblk = dm9000_inblk_8bit; |
| 345 | db->rx_status = dm9000_rx_status_8bit; |
| 346 | break; |
| 347 | default: |
| 348 | /* Assume 8 bit mode, will probably not work anyway */ |
| 349 | printf("DM9000: Undefined IO-mode:0x%x\n", io_mode); |
| 350 | db->outblk = dm9000_outblk_8bit; |
| 351 | db->inblk = dm9000_inblk_8bit; |
| 352 | db->rx_status = dm9000_rx_status_8bit; |
| 353 | break; |
| 354 | } |
| 355 | |
Andrew Dyer | a62f5d4 | 2008-08-26 17:03:38 -0500 | [diff] [blame] | 356 | /* Program operating register, only internal phy supported */ |
Marek Vasut | eb2749a | 2022-04-13 04:15:23 +0200 | [diff] [blame] | 357 | dm9000_iow(DM9000_NCR, 0x0); |
Remy Bohmer | 61b8dbd | 2008-06-03 15:26:26 +0200 | [diff] [blame] | 358 | /* TX Polling clear */ |
Marek Vasut | eb2749a | 2022-04-13 04:15:23 +0200 | [diff] [blame] | 359 | dm9000_iow(DM9000_TCR, 0); |
Remy Bohmer | 61b8dbd | 2008-06-03 15:26:26 +0200 | [diff] [blame] | 360 | /* Less 3Kb, 200us */ |
Marek Vasut | eb2749a | 2022-04-13 04:15:23 +0200 | [diff] [blame] | 361 | dm9000_iow(DM9000_BPTR, BPTR_BPHW(3) | BPTR_JPT_600US); |
Remy Bohmer | 61b8dbd | 2008-06-03 15:26:26 +0200 | [diff] [blame] | 362 | /* Flow Control : High/Low Water */ |
Marek Vasut | eb2749a | 2022-04-13 04:15:23 +0200 | [diff] [blame] | 363 | dm9000_iow(DM9000_FCTR, FCTR_HWOT(3) | FCTR_LWOT(8)); |
Remy Bohmer | 61b8dbd | 2008-06-03 15:26:26 +0200 | [diff] [blame] | 364 | /* SH FIXME: This looks strange! Flow Control */ |
Marek Vasut | eb2749a | 2022-04-13 04:15:23 +0200 | [diff] [blame] | 365 | dm9000_iow(DM9000_FCR, 0x0); |
Remy Bohmer | 61b8dbd | 2008-06-03 15:26:26 +0200 | [diff] [blame] | 366 | /* Special Mode */ |
Marek Vasut | eb2749a | 2022-04-13 04:15:23 +0200 | [diff] [blame] | 367 | dm9000_iow(DM9000_SMCR, 0); |
Remy Bohmer | 61b8dbd | 2008-06-03 15:26:26 +0200 | [diff] [blame] | 368 | /* clear TX status */ |
Marek Vasut | eb2749a | 2022-04-13 04:15:23 +0200 | [diff] [blame] | 369 | dm9000_iow(DM9000_NSR, NSR_WAKEST | NSR_TX2END | NSR_TX1END); |
Remy Bohmer | 61b8dbd | 2008-06-03 15:26:26 +0200 | [diff] [blame] | 370 | /* Clear interrupt status */ |
Marek Vasut | eb2749a | 2022-04-13 04:15:23 +0200 | [diff] [blame] | 371 | dm9000_iow(DM9000_ISR, ISR_ROOS | ISR_ROS | ISR_PTS | ISR_PRS); |
wdenk | 7ac1610 | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 372 | |
Ben Warren | 8707f62 | 2009-10-21 21:53:39 -0700 | [diff] [blame] | 373 | printf("MAC: %pM\n", dev->enetaddr); |
Marek Vasut | a4aa048 | 2022-04-13 04:15:29 +0200 | [diff] [blame] | 374 | if (!is_valid_ethaddr(dev->enetaddr)) |
Andrew Ruder | 1c377d1 | 2013-10-22 19:09:02 -0500 | [diff] [blame] | 375 | printf("WARNING: Bad MAC address (uninitialized EEPROM?)\n"); |
Andrew Dyer | a62f5d4 | 2008-08-26 17:03:38 -0500 | [diff] [blame] | 376 | |
| 377 | /* fill device MAC address registers */ |
| 378 | for (i = 0, oft = DM9000_PAR; i < 6; i++, oft++) |
Marek Vasut | eb2749a | 2022-04-13 04:15:23 +0200 | [diff] [blame] | 379 | dm9000_iow(oft, dev->enetaddr[i]); |
wdenk | 7ac1610 | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 380 | for (i = 0, oft = 0x16; i < 8; i++, oft++) |
Marek Vasut | eb2749a | 2022-04-13 04:15:23 +0200 | [diff] [blame] | 381 | dm9000_iow(oft, 0xff); |
wdenk | 7ac1610 | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 382 | |
| 383 | /* read back mac, just to be sure */ |
| 384 | for (i = 0, oft = 0x10; i < 6; i++, oft++) |
Marek Vasut | ed76122 | 2022-04-13 04:15:24 +0200 | [diff] [blame] | 385 | debug("%02x:", dm9000_ior(oft)); |
| 386 | debug("\n"); |
wdenk | 7ac1610 | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 387 | |
| 388 | /* Activate DM9000 */ |
Remy Bohmer | 61b8dbd | 2008-06-03 15:26:26 +0200 | [diff] [blame] | 389 | /* RX enable */ |
Marek Vasut | eb2749a | 2022-04-13 04:15:23 +0200 | [diff] [blame] | 390 | dm9000_iow(DM9000_RCR, RCR_DIS_LONG | RCR_DIS_CRC | RCR_RXEN); |
Remy Bohmer | 61b8dbd | 2008-06-03 15:26:26 +0200 | [diff] [blame] | 391 | /* Enable TX/RX interrupt mask */ |
Marek Vasut | eb2749a | 2022-04-13 04:15:23 +0200 | [diff] [blame] | 392 | dm9000_iow(DM9000_IMR, IMR_PAR); |
Remy Bohmer | 61b8dbd | 2008-06-03 15:26:26 +0200 | [diff] [blame] | 393 | |
wdenk | 7ac1610 | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 394 | i = 0; |
Andy Fleming | 0d2df96 | 2011-03-22 22:49:13 -0500 | [diff] [blame] | 395 | while (!(dm9000_phy_read(1) & 0x20)) { /* autonegation complete bit */ |
wdenk | 7ac1610 | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 396 | udelay(1000); |
| 397 | i++; |
| 398 | if (i == 10000) { |
| 399 | printf("could not establish link\n"); |
| 400 | return 0; |
| 401 | } |
| 402 | } |
| 403 | |
| 404 | /* see what we've got */ |
Andy Fleming | 0d2df96 | 2011-03-22 22:49:13 -0500 | [diff] [blame] | 405 | lnk = dm9000_phy_read(17) >> 12; |
wdenk | 7ac1610 | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 406 | printf("operating at "); |
| 407 | switch (lnk) { |
| 408 | case 1: |
| 409 | printf("10M half duplex "); |
| 410 | break; |
| 411 | case 2: |
| 412 | printf("10M full duplex "); |
| 413 | break; |
| 414 | case 4: |
| 415 | printf("100M half duplex "); |
| 416 | break; |
| 417 | case 8: |
| 418 | printf("100M full duplex "); |
| 419 | break; |
| 420 | default: |
| 421 | printf("unknown: %d ", lnk); |
| 422 | break; |
| 423 | } |
| 424 | printf("mode\n"); |
| 425 | return 0; |
| 426 | } |
| 427 | |
| 428 | /* |
Marek Vasut | a4aa048 | 2022-04-13 04:15:29 +0200 | [diff] [blame] | 429 | * Hardware start transmission. |
| 430 | * Send a packet to media from the upper layer. |
| 431 | */ |
Marek Vasut | 9c5e9ca | 2022-04-13 04:15:32 +0200 | [diff] [blame^] | 432 | static int dm9000_send(struct eth_device *dev, void *packet, int length) |
wdenk | 7ac1610 | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 433 | { |
Marek Vasut | 9c5e9ca | 2022-04-13 04:15:32 +0200 | [diff] [blame^] | 434 | struct dm9000_priv *db = container_of(dev, struct dm9000_priv, dev); |
wdenk | 7ac1610 | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 435 | int tmo; |
Remy Bohmer | 5f63bf4 | 2008-06-03 15:26:21 +0200 | [diff] [blame] | 436 | |
Marek Vasut | a4aa048 | 2022-04-13 04:15:29 +0200 | [diff] [blame] | 437 | dm9000_dump_packet(__func__, packet, length); |
wdenk | 7ac1610 | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 438 | |
Marek Vasut | eb2749a | 2022-04-13 04:15:23 +0200 | [diff] [blame] | 439 | dm9000_iow(DM9000_ISR, IMR_PTM); /* Clear Tx bit in ISR */ |
Remy Bohmer | 16cb264 | 2008-06-03 15:26:23 +0200 | [diff] [blame] | 440 | |
wdenk | 7ac1610 | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 441 | /* Move data to DM9000 TX RAM */ |
Marek Vasut | eb2749a | 2022-04-13 04:15:23 +0200 | [diff] [blame] | 442 | dm9000_outb(DM9000_MWCMD, DM9000_IO); /* Prepare for TX-data */ |
wdenk | 7ac1610 | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 443 | |
Remy Bohmer | 5f63bf4 | 2008-06-03 15:26:21 +0200 | [diff] [blame] | 444 | /* push the data to the TX-fifo */ |
Marek Vasut | a4aa048 | 2022-04-13 04:15:29 +0200 | [diff] [blame] | 445 | db->outblk(packet, length); |
wdenk | 7ac1610 | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 446 | |
| 447 | /* Set TX length to DM9000 */ |
Marek Vasut | eb2749a | 2022-04-13 04:15:23 +0200 | [diff] [blame] | 448 | dm9000_iow(DM9000_TXPLL, length & 0xff); |
| 449 | dm9000_iow(DM9000_TXPLH, (length >> 8) & 0xff); |
wdenk | 7ac1610 | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 450 | |
| 451 | /* Issue TX polling command */ |
Marek Vasut | eb2749a | 2022-04-13 04:15:23 +0200 | [diff] [blame] | 452 | dm9000_iow(DM9000_TCR, TCR_TXREQ); /* Cleared after TX complete */ |
wdenk | 7ac1610 | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 453 | |
| 454 | /* wait for end of transmission */ |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 455 | tmo = get_timer(0) + 5 * CONFIG_SYS_HZ; |
Marek Vasut | a4aa048 | 2022-04-13 04:15:29 +0200 | [diff] [blame] | 456 | while (!(dm9000_ior(DM9000_NSR) & (NSR_TX1END | NSR_TX2END)) || |
| 457 | !(dm9000_ior(DM9000_ISR) & IMR_PTM)) { |
wdenk | 7ac1610 | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 458 | if (get_timer(0) >= tmo) { |
| 459 | printf("transmission timeout\n"); |
| 460 | break; |
| 461 | } |
| 462 | } |
Marek Vasut | eb2749a | 2022-04-13 04:15:23 +0200 | [diff] [blame] | 463 | dm9000_iow(DM9000_ISR, IMR_PTM); /* Clear Tx bit in ISR */ |
Remy Bohmer | 16cb264 | 2008-06-03 15:26:23 +0200 | [diff] [blame] | 464 | |
Marek Vasut | ed76122 | 2022-04-13 04:15:24 +0200 | [diff] [blame] | 465 | debug("transmit done\n\n"); |
wdenk | 7ac1610 | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 466 | return 0; |
| 467 | } |
| 468 | |
| 469 | /* |
Marek Vasut | a4aa048 | 2022-04-13 04:15:29 +0200 | [diff] [blame] | 470 | * Stop the interface. |
| 471 | * The interface is stopped when it is brought. |
| 472 | */ |
Remy Bohmer | 7eefd92 | 2009-05-02 21:49:18 +0200 | [diff] [blame] | 473 | static void dm9000_halt(struct eth_device *netdev) |
wdenk | 7ac1610 | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 474 | { |
Marek Vasut | a4aa048 | 2022-04-13 04:15:29 +0200 | [diff] [blame] | 475 | /* RESET device */ |
Andy Fleming | 0d2df96 | 2011-03-22 22:49:13 -0500 | [diff] [blame] | 476 | dm9000_phy_write(0, 0x8000); /* PHY RESET */ |
Marek Vasut | eb2749a | 2022-04-13 04:15:23 +0200 | [diff] [blame] | 477 | dm9000_iow(DM9000_GPR, 0x01); /* Power-Down PHY */ |
| 478 | dm9000_iow(DM9000_IMR, 0x80); /* Disable all interrupt */ |
| 479 | dm9000_iow(DM9000_RCR, 0x00); /* Disable RX */ |
wdenk | 7ac1610 | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 480 | } |
| 481 | |
| 482 | /* |
Marek Vasut | a4aa048 | 2022-04-13 04:15:29 +0200 | [diff] [blame] | 483 | * Received a packet and pass to upper layer |
| 484 | */ |
Marek Vasut | 9c5e9ca | 2022-04-13 04:15:32 +0200 | [diff] [blame^] | 485 | static int dm9000_rx(struct eth_device *dev) |
wdenk | 7ac1610 | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 486 | { |
Marek Vasut | 9c5e9ca | 2022-04-13 04:15:32 +0200 | [diff] [blame^] | 487 | struct dm9000_priv *db = container_of(dev, struct dm9000_priv, dev); |
Joe Hershberger | 9f09a36 | 2015-04-08 01:41:06 -0500 | [diff] [blame] | 488 | u8 rxbyte; |
| 489 | u8 *rdptr = (u8 *)net_rx_packets[0]; |
Marek Vasut | 5248e56 | 2022-04-13 04:15:25 +0200 | [diff] [blame] | 490 | u16 rxstatus, rxlen = 0; |
wdenk | 7ac1610 | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 491 | |
Marek Vasut | a4aa048 | 2022-04-13 04:15:29 +0200 | [diff] [blame] | 492 | /* |
| 493 | * Check packet ready or not, we must check |
| 494 | * the ISR status first for DM9000A |
| 495 | */ |
Marek Vasut | eb2749a | 2022-04-13 04:15:23 +0200 | [diff] [blame] | 496 | if (!(dm9000_ior(DM9000_ISR) & 0x01)) /* Rx-ISR bit must be set. */ |
wdenk | 7ac1610 | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 497 | return 0; |
| 498 | |
Marek Vasut | eb2749a | 2022-04-13 04:15:23 +0200 | [diff] [blame] | 499 | dm9000_iow(DM9000_ISR, 0x01); /* clear PR status latched in bit 0 */ |
wdenk | 7ac1610 | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 500 | |
Remy Bohmer | eec38a1 | 2008-06-03 15:26:25 +0200 | [diff] [blame] | 501 | /* There is _at least_ 1 package in the fifo, read them all */ |
| 502 | for (;;) { |
Marek Vasut | eb2749a | 2022-04-13 04:15:23 +0200 | [diff] [blame] | 503 | dm9000_ior(DM9000_MRCMDX); /* Dummy read */ |
wdenk | 7ac1610 | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 504 | |
Marek Vasut | a4aa048 | 2022-04-13 04:15:29 +0200 | [diff] [blame] | 505 | /* |
| 506 | * Get most updated data, |
| 507 | * only look at bits 0:1, See application notes DM9000 |
| 508 | */ |
Marek Vasut | eb2749a | 2022-04-13 04:15:23 +0200 | [diff] [blame] | 509 | rxbyte = dm9000_inb(DM9000_DATA) & 0x03; |
wdenk | 7ac1610 | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 510 | |
Remy Bohmer | eec38a1 | 2008-06-03 15:26:25 +0200 | [diff] [blame] | 511 | /* Status check: this byte must be 0 or 1 */ |
| 512 | if (rxbyte > DM9000_PKT_RDY) { |
Marek Vasut | eb2749a | 2022-04-13 04:15:23 +0200 | [diff] [blame] | 513 | dm9000_iow(DM9000_RCR, 0x00); /* Stop Device */ |
| 514 | dm9000_iow(DM9000_ISR, 0x80); /* Stop INT request */ |
Remy Bohmer | eec38a1 | 2008-06-03 15:26:25 +0200 | [diff] [blame] | 515 | printf("DM9000 error: status check fail: 0x%x\n", |
Marek Vasut | a4aa048 | 2022-04-13 04:15:29 +0200 | [diff] [blame] | 516 | rxbyte); |
Remy Bohmer | eec38a1 | 2008-06-03 15:26:25 +0200 | [diff] [blame] | 517 | return 0; |
| 518 | } |
wdenk | 7ac1610 | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 519 | |
Remy Bohmer | eec38a1 | 2008-06-03 15:26:25 +0200 | [diff] [blame] | 520 | if (rxbyte != DM9000_PKT_RDY) |
| 521 | return 0; /* No packet received, ignore */ |
wdenk | 7ac1610 | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 522 | |
Marek Vasut | ed76122 | 2022-04-13 04:15:24 +0200 | [diff] [blame] | 523 | debug("receiving packet\n"); |
Remy Bohmer | eec38a1 | 2008-06-03 15:26:25 +0200 | [diff] [blame] | 524 | |
| 525 | /* A packet ready now & Get status/length */ |
Marek Vasut | a4aa048 | 2022-04-13 04:15:29 +0200 | [diff] [blame] | 526 | db->rx_status(&rxstatus, &rxlen); |
wdenk | 7ac1610 | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 527 | |
Marek Vasut | 5248e56 | 2022-04-13 04:15:25 +0200 | [diff] [blame] | 528 | debug("rx status: 0x%04x rx len: %d\n", rxstatus, rxlen); |
Remy Bohmer | eec38a1 | 2008-06-03 15:26:25 +0200 | [diff] [blame] | 529 | |
| 530 | /* Move data from DM9000 */ |
| 531 | /* Read received packet from RX SRAM */ |
Marek Vasut | a4aa048 | 2022-04-13 04:15:29 +0200 | [diff] [blame] | 532 | db->inblk(rdptr, rxlen); |
Remy Bohmer | eec38a1 | 2008-06-03 15:26:25 +0200 | [diff] [blame] | 533 | |
Marek Vasut | a4aa048 | 2022-04-13 04:15:29 +0200 | [diff] [blame] | 534 | if (rxstatus & 0xbf00 || rxlen < 0x40 || |
| 535 | rxlen > DM9000_PKT_MAX) { |
| 536 | if (rxstatus & 0x100) |
Remy Bohmer | eec38a1 | 2008-06-03 15:26:25 +0200 | [diff] [blame] | 537 | printf("rx fifo error\n"); |
Marek Vasut | a4aa048 | 2022-04-13 04:15:29 +0200 | [diff] [blame] | 538 | if (rxstatus & 0x200) |
Remy Bohmer | eec38a1 | 2008-06-03 15:26:25 +0200 | [diff] [blame] | 539 | printf("rx crc error\n"); |
Marek Vasut | a4aa048 | 2022-04-13 04:15:29 +0200 | [diff] [blame] | 540 | if (rxstatus & 0x8000) |
Remy Bohmer | eec38a1 | 2008-06-03 15:26:25 +0200 | [diff] [blame] | 541 | printf("rx length error\n"); |
Marek Vasut | 5248e56 | 2022-04-13 04:15:25 +0200 | [diff] [blame] | 542 | if (rxlen > DM9000_PKT_MAX) { |
Remy Bohmer | eec38a1 | 2008-06-03 15:26:25 +0200 | [diff] [blame] | 543 | printf("rx length too big\n"); |
| 544 | dm9000_reset(); |
| 545 | } |
| 546 | } else { |
Marek Vasut | a4aa048 | 2022-04-13 04:15:29 +0200 | [diff] [blame] | 547 | dm9000_dump_packet(__func__, rdptr, rxlen); |
Remy Bohmer | eec38a1 | 2008-06-03 15:26:25 +0200 | [diff] [blame] | 548 | |
Marek Vasut | ed76122 | 2022-04-13 04:15:24 +0200 | [diff] [blame] | 549 | debug("passing packet to upper layer\n"); |
Marek Vasut | 5248e56 | 2022-04-13 04:15:25 +0200 | [diff] [blame] | 550 | net_process_received_packet(net_rx_packets[0], rxlen); |
Remy Bohmer | eec38a1 | 2008-06-03 15:26:25 +0200 | [diff] [blame] | 551 | } |
wdenk | 7ac1610 | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 552 | } |
| 553 | return 0; |
| 554 | } |
| 555 | |
| 556 | /* |
Marek Vasut | a4aa048 | 2022-04-13 04:15:29 +0200 | [diff] [blame] | 557 | * Read a word data from SROM |
| 558 | */ |
Remy Bohmer | cd9a36c | 2009-05-03 12:11:40 +0200 | [diff] [blame] | 559 | #if !defined(CONFIG_DM9000_NO_SROM) |
| 560 | void dm9000_read_srom_word(int offset, u8 *to) |
wdenk | 7ac1610 | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 561 | { |
Marek Vasut | eb2749a | 2022-04-13 04:15:23 +0200 | [diff] [blame] | 562 | dm9000_iow(DM9000_EPAR, offset); |
| 563 | dm9000_iow(DM9000_EPCR, 0x4); |
Marek Vasut | a4aa048 | 2022-04-13 04:15:29 +0200 | [diff] [blame] | 564 | mdelay(8); |
Marek Vasut | eb2749a | 2022-04-13 04:15:23 +0200 | [diff] [blame] | 565 | dm9000_iow(DM9000_EPCR, 0x0); |
| 566 | to[0] = dm9000_ior(DM9000_EPDRL); |
| 567 | to[1] = dm9000_ior(DM9000_EPDRH); |
wdenk | 7ac1610 | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 568 | } |
| 569 | |
Remy Bohmer | cd9a36c | 2009-05-03 12:11:40 +0200 | [diff] [blame] | 570 | void dm9000_write_srom_word(int offset, u16 val) |
stefano babic | 6708a60 | 2007-08-30 23:01:49 +0200 | [diff] [blame] | 571 | { |
Marek Vasut | eb2749a | 2022-04-13 04:15:23 +0200 | [diff] [blame] | 572 | dm9000_iow(DM9000_EPAR, offset); |
| 573 | dm9000_iow(DM9000_EPDRH, ((val >> 8) & 0xff)); |
| 574 | dm9000_iow(DM9000_EPDRL, (val & 0xff)); |
| 575 | dm9000_iow(DM9000_EPCR, 0x12); |
Marek Vasut | a4aa048 | 2022-04-13 04:15:29 +0200 | [diff] [blame] | 576 | mdelay(8); |
Marek Vasut | eb2749a | 2022-04-13 04:15:23 +0200 | [diff] [blame] | 577 | dm9000_iow(DM9000_EPCR, 0); |
stefano babic | 6708a60 | 2007-08-30 23:01:49 +0200 | [diff] [blame] | 578 | } |
Ben Warren | 8707f62 | 2009-10-21 21:53:39 -0700 | [diff] [blame] | 579 | |
| 580 | static void dm9000_get_enetaddr(struct eth_device *dev) |
| 581 | { |
Ben Warren | 8707f62 | 2009-10-21 21:53:39 -0700 | [diff] [blame] | 582 | int i; |
Marek Vasut | a4aa048 | 2022-04-13 04:15:29 +0200 | [diff] [blame] | 583 | |
Ben Warren | 8707f62 | 2009-10-21 21:53:39 -0700 | [diff] [blame] | 584 | for (i = 0; i < 3; i++) |
| 585 | dm9000_read_srom_word(i, dev->enetaddr + (2 * i)); |
Ben Warren | 8707f62 | 2009-10-21 21:53:39 -0700 | [diff] [blame] | 586 | } |
Marek Vasut | a4aa048 | 2022-04-13 04:15:29 +0200 | [diff] [blame] | 587 | #else |
| 588 | static void dm9000_get_enetaddr(struct eth_device *dev) {} |
| 589 | #endif |
stefano babic | 6708a60 | 2007-08-30 23:01:49 +0200 | [diff] [blame] | 590 | |
Masahiro Yamada | f7ed78b | 2020-06-26 15:13:33 +0900 | [diff] [blame] | 591 | int dm9000_initialize(struct bd_info *bis) |
Remy Bohmer | 7eefd92 | 2009-05-02 21:49:18 +0200 | [diff] [blame] | 592 | { |
Marek Vasut | 9c5e9ca | 2022-04-13 04:15:32 +0200 | [diff] [blame^] | 593 | struct dm9000_priv *priv; |
| 594 | struct eth_device *dev; |
| 595 | |
| 596 | priv = calloc(1, sizeof(*priv)); |
| 597 | if (!priv) |
| 598 | return -ENOMEM; |
| 599 | |
| 600 | dev = &priv->dev; |
Remy Bohmer | 7eefd92 | 2009-05-02 21:49:18 +0200 | [diff] [blame] | 601 | |
Ben Warren | 8707f62 | 2009-10-21 21:53:39 -0700 | [diff] [blame] | 602 | /* Load MAC address from EEPROM */ |
Marek Vasut | 9c5e9ca | 2022-04-13 04:15:32 +0200 | [diff] [blame^] | 603 | dm9000_get_enetaddr(&priv->dev); |
Ben Warren | 8707f62 | 2009-10-21 21:53:39 -0700 | [diff] [blame] | 604 | |
Remy Bohmer | 7eefd92 | 2009-05-02 21:49:18 +0200 | [diff] [blame] | 605 | dev->init = dm9000_init; |
| 606 | dev->halt = dm9000_halt; |
| 607 | dev->send = dm9000_send; |
| 608 | dev->recv = dm9000_rx; |
Ben Whitten | 34fd6c9 | 2015-12-30 13:05:58 +0000 | [diff] [blame] | 609 | strcpy(dev->name, "dm9000"); |
Remy Bohmer | 7eefd92 | 2009-05-02 21:49:18 +0200 | [diff] [blame] | 610 | |
Marek Vasut | 9c5e9ca | 2022-04-13 04:15:32 +0200 | [diff] [blame^] | 611 | eth_register(&priv->dev); |
Remy Bohmer | 7eefd92 | 2009-05-02 21:49:18 +0200 | [diff] [blame] | 612 | |
| 613 | return 0; |
| 614 | } |