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