Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
wdenk | 281e00a | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 2 | /* |
Marek Vasut | a7bebf8 | 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 | 281e00a | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 51 | |
| 52 | #include <common.h> |
| 53 | #include <command.h> |
Marek Vasut | 41e10be | 2022-04-13 04:15:37 +0200 | [diff] [blame] | 54 | #include <dm.h> |
Marek Vasut | 3928055 | 2022-04-13 04:15:32 +0200 | [diff] [blame] | 55 | #include <malloc.h> |
wdenk | 281e00a | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 56 | #include <net.h> |
| 57 | #include <asm/io.h> |
Simon Glass | c05ed00 | 2020-05-10 11:40:11 -0600 | [diff] [blame] | 58 | #include <linux/delay.h> |
wdenk | 281e00a | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 59 | |
wdenk | 281e00a | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 60 | #include "dm9000x.h" |
| 61 | |
wdenk | 281e00a | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 62 | /* Structure/enum declaration ------------------------------- */ |
Marek Vasut | 8371edd | 2022-04-13 04:15:31 +0200 | [diff] [blame] | 63 | struct dm9000_priv { |
wdenk | 281e00a | 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 | 281e00a | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 74 | unsigned char srom[128]; |
Marek Vasut | f0d1a29 | 2022-04-13 04:15:34 +0200 | [diff] [blame] | 75 | void (*outblk)(struct dm9000_priv *db, void *data_ptr, int count); |
| 76 | void (*inblk)(struct dm9000_priv *db, void *data_ptr, int count); |
| 77 | void (*rx_status)(struct dm9000_priv *db, u16 *rxstatus, u16 *rxlen); |
Marek Vasut | f0d1a29 | 2022-04-13 04:15:34 +0200 | [diff] [blame] | 78 | void __iomem *base_io; |
| 79 | void __iomem *base_data; |
Marek Vasut | a7bebf8 | 2022-04-13 04:15:29 +0200 | [diff] [blame] | 80 | }; |
wdenk | 281e00a | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 81 | |
wdenk | 281e00a | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 82 | /* DM9000 network board routine ---------------------------- */ |
Jason Jin | 5c1d082 | 2011-08-25 15:46:43 +0800 | [diff] [blame] | 83 | #ifndef CONFIG_DM9000_BYTE_SWAPPED |
Marek Vasut | a7bebf8 | 2022-04-13 04:15:29 +0200 | [diff] [blame] | 84 | #define dm9000_outb(d, r) writeb((d), (r)) |
| 85 | #define dm9000_outw(d, r) writew((d), (r)) |
| 86 | #define dm9000_outl(d, r) writel((d), (r)) |
Marek Vasut | 6d3de0f | 2022-04-13 04:15:28 +0200 | [diff] [blame] | 87 | #define dm9000_inb(r) readb(r) |
| 88 | #define dm9000_inw(r) readw(r) |
| 89 | #define dm9000_inl(r) readl(r) |
Jason Jin | 5c1d082 | 2011-08-25 15:46:43 +0800 | [diff] [blame] | 90 | #else |
Marek Vasut | ff61d4e | 2022-04-13 04:15:23 +0200 | [diff] [blame] | 91 | #define dm9000_outb(d, r) __raw_writeb(d, r) |
| 92 | #define dm9000_outw(d, r) __raw_writew(d, r) |
| 93 | #define dm9000_outl(d, r) __raw_writel(d, r) |
| 94 | #define dm9000_inb(r) __raw_readb(r) |
| 95 | #define dm9000_inw(r) __raw_readw(r) |
| 96 | #define dm9000_inl(r) __raw_readl(r) |
Jason Jin | 5c1d082 | 2011-08-25 15:46:43 +0800 | [diff] [blame] | 97 | #endif |
wdenk | 281e00a | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 98 | |
Marek Vasut | c7b7ee5 | 2022-04-13 04:15:27 +0200 | [diff] [blame] | 99 | #ifdef DEBUG |
| 100 | static void dm9000_dump_packet(const char *func, u8 *packet, int length) |
| 101 | { |
| 102 | int i; |
| 103 | |
| 104 | printf("%s: length: %d\n", func, length); |
| 105 | |
| 106 | for (i = 0; i < length; i++) { |
| 107 | if (i % 8 == 0) |
| 108 | printf("\n%s: %02x: ", func, i); |
| 109 | printf("%02x ", packet[i]); |
| 110 | } |
| 111 | |
| 112 | printf("\n"); |
| 113 | } |
| 114 | #else |
| 115 | static void dm9000_dump_packet(const char *func, u8 *packet, int length) {} |
| 116 | #endif |
| 117 | |
Marek Vasut | f0d1a29 | 2022-04-13 04:15:34 +0200 | [diff] [blame] | 118 | static void dm9000_outblk_8bit(struct dm9000_priv *db, void *data_ptr, int count) |
Remy Bohmer | a101361 | 2008-06-03 15:26:21 +0200 | [diff] [blame] | 119 | { |
| 120 | int i; |
Marek Vasut | a7bebf8 | 2022-04-13 04:15:29 +0200 | [diff] [blame] | 121 | |
Remy Bohmer | a101361 | 2008-06-03 15:26:21 +0200 | [diff] [blame] | 122 | for (i = 0; i < count; i++) |
Marek Vasut | f0d1a29 | 2022-04-13 04:15:34 +0200 | [diff] [blame] | 123 | dm9000_outb((((u8 *)data_ptr)[i] & 0xff), db->base_data); |
Remy Bohmer | a101361 | 2008-06-03 15:26:21 +0200 | [diff] [blame] | 124 | } |
| 125 | |
Marek Vasut | f0d1a29 | 2022-04-13 04:15:34 +0200 | [diff] [blame] | 126 | static void dm9000_outblk_16bit(struct dm9000_priv *db, void *data_ptr, int count) |
Remy Bohmer | a101361 | 2008-06-03 15:26:21 +0200 | [diff] [blame] | 127 | { |
| 128 | int i; |
| 129 | u32 tmplen = (count + 1) / 2; |
| 130 | |
| 131 | for (i = 0; i < tmplen; i++) |
Marek Vasut | f0d1a29 | 2022-04-13 04:15:34 +0200 | [diff] [blame] | 132 | dm9000_outw(((u16 *)data_ptr)[i], db->base_data); |
Remy Bohmer | a101361 | 2008-06-03 15:26:21 +0200 | [diff] [blame] | 133 | } |
Marek Vasut | a7bebf8 | 2022-04-13 04:15:29 +0200 | [diff] [blame] | 134 | |
Marek Vasut | f0d1a29 | 2022-04-13 04:15:34 +0200 | [diff] [blame] | 135 | static void dm9000_outblk_32bit(struct dm9000_priv *db, void *data_ptr, int count) |
Remy Bohmer | a101361 | 2008-06-03 15:26:21 +0200 | [diff] [blame] | 136 | { |
| 137 | int i; |
| 138 | u32 tmplen = (count + 3) / 4; |
| 139 | |
| 140 | for (i = 0; i < tmplen; i++) |
Marek Vasut | f0d1a29 | 2022-04-13 04:15:34 +0200 | [diff] [blame] | 141 | dm9000_outl(((u32 *)data_ptr)[i], db->base_data); |
Remy Bohmer | a101361 | 2008-06-03 15:26:21 +0200 | [diff] [blame] | 142 | } |
| 143 | |
Marek Vasut | f0d1a29 | 2022-04-13 04:15:34 +0200 | [diff] [blame] | 144 | static void dm9000_inblk_8bit(struct dm9000_priv *db, void *data_ptr, int count) |
Remy Bohmer | a101361 | 2008-06-03 15:26:21 +0200 | [diff] [blame] | 145 | { |
| 146 | int i; |
Marek Vasut | a7bebf8 | 2022-04-13 04:15:29 +0200 | [diff] [blame] | 147 | |
Remy Bohmer | a101361 | 2008-06-03 15:26:21 +0200 | [diff] [blame] | 148 | for (i = 0; i < count; i++) |
Marek Vasut | f0d1a29 | 2022-04-13 04:15:34 +0200 | [diff] [blame] | 149 | ((u8 *)data_ptr)[i] = dm9000_inb(db->base_data); |
Remy Bohmer | a101361 | 2008-06-03 15:26:21 +0200 | [diff] [blame] | 150 | } |
| 151 | |
Marek Vasut | f0d1a29 | 2022-04-13 04:15:34 +0200 | [diff] [blame] | 152 | static void dm9000_inblk_16bit(struct dm9000_priv *db, void *data_ptr, int count) |
Remy Bohmer | a101361 | 2008-06-03 15:26:21 +0200 | [diff] [blame] | 153 | { |
| 154 | int i; |
| 155 | u32 tmplen = (count + 1) / 2; |
| 156 | |
| 157 | for (i = 0; i < tmplen; i++) |
Marek Vasut | f0d1a29 | 2022-04-13 04:15:34 +0200 | [diff] [blame] | 158 | ((u16 *)data_ptr)[i] = dm9000_inw(db->base_data); |
Remy Bohmer | a101361 | 2008-06-03 15:26:21 +0200 | [diff] [blame] | 159 | } |
Marek Vasut | a7bebf8 | 2022-04-13 04:15:29 +0200 | [diff] [blame] | 160 | |
Marek Vasut | f0d1a29 | 2022-04-13 04:15:34 +0200 | [diff] [blame] | 161 | static void dm9000_inblk_32bit(struct dm9000_priv *db, void *data_ptr, int count) |
Remy Bohmer | a101361 | 2008-06-03 15:26:21 +0200 | [diff] [blame] | 162 | { |
| 163 | int i; |
| 164 | u32 tmplen = (count + 3) / 4; |
| 165 | |
| 166 | for (i = 0; i < tmplen; i++) |
Marek Vasut | f0d1a29 | 2022-04-13 04:15:34 +0200 | [diff] [blame] | 167 | ((u32 *)data_ptr)[i] = dm9000_inl(db->base_data); |
Remy Bohmer | a101361 | 2008-06-03 15:26:21 +0200 | [diff] [blame] | 168 | } |
| 169 | |
Marek Vasut | f0d1a29 | 2022-04-13 04:15:34 +0200 | [diff] [blame] | 170 | static void dm9000_rx_status_32bit(struct dm9000_priv *db, u16 *rxstatus, u16 *rxlen) |
Remy Bohmer | a101361 | 2008-06-03 15:26:21 +0200 | [diff] [blame] | 171 | { |
Remy Bohmer | d6ee5fa | 2008-06-04 10:47:25 +0200 | [diff] [blame] | 172 | u32 tmpdata; |
Remy Bohmer | a101361 | 2008-06-03 15:26:21 +0200 | [diff] [blame] | 173 | |
Marek Vasut | f0d1a29 | 2022-04-13 04:15:34 +0200 | [diff] [blame] | 174 | dm9000_outb(DM9000_MRCMD, db->base_io); |
Remy Bohmer | a101361 | 2008-06-03 15:26:21 +0200 | [diff] [blame] | 175 | |
Marek Vasut | f0d1a29 | 2022-04-13 04:15:34 +0200 | [diff] [blame] | 176 | tmpdata = dm9000_inl(db->base_data); |
Marek Vasut | d8f21b2 | 2022-04-13 04:15:25 +0200 | [diff] [blame] | 177 | *rxstatus = __le16_to_cpu(tmpdata); |
| 178 | *rxlen = __le16_to_cpu(tmpdata >> 16); |
Remy Bohmer | a101361 | 2008-06-03 15:26:21 +0200 | [diff] [blame] | 179 | } |
| 180 | |
Marek Vasut | f0d1a29 | 2022-04-13 04:15:34 +0200 | [diff] [blame] | 181 | static void dm9000_rx_status_16bit(struct dm9000_priv *db, u16 *rxstatus, u16 *rxlen) |
Remy Bohmer | a101361 | 2008-06-03 15:26:21 +0200 | [diff] [blame] | 182 | { |
Marek Vasut | f0d1a29 | 2022-04-13 04:15:34 +0200 | [diff] [blame] | 183 | dm9000_outb(DM9000_MRCMD, db->base_io); |
Remy Bohmer | a101361 | 2008-06-03 15:26:21 +0200 | [diff] [blame] | 184 | |
Marek Vasut | f0d1a29 | 2022-04-13 04:15:34 +0200 | [diff] [blame] | 185 | *rxstatus = __le16_to_cpu(dm9000_inw(db->base_data)); |
| 186 | *rxlen = __le16_to_cpu(dm9000_inw(db->base_data)); |
Remy Bohmer | a101361 | 2008-06-03 15:26:21 +0200 | [diff] [blame] | 187 | } |
| 188 | |
Marek Vasut | f0d1a29 | 2022-04-13 04:15:34 +0200 | [diff] [blame] | 189 | static void dm9000_rx_status_8bit(struct dm9000_priv *db, u16 *rxstatus, u16 *rxlen) |
Remy Bohmer | a101361 | 2008-06-03 15:26:21 +0200 | [diff] [blame] | 190 | { |
Marek Vasut | f0d1a29 | 2022-04-13 04:15:34 +0200 | [diff] [blame] | 191 | dm9000_outb(DM9000_MRCMD, db->base_io); |
Remy Bohmer | a101361 | 2008-06-03 15:26:21 +0200 | [diff] [blame] | 192 | |
Marek Vasut | d8f21b2 | 2022-04-13 04:15:25 +0200 | [diff] [blame] | 193 | *rxstatus = |
Marek Vasut | f0d1a29 | 2022-04-13 04:15:34 +0200 | [diff] [blame] | 194 | __le16_to_cpu(dm9000_inb(db->base_data) + |
| 195 | (dm9000_inb(db->base_data) << 8)); |
Marek Vasut | d8f21b2 | 2022-04-13 04:15:25 +0200 | [diff] [blame] | 196 | *rxlen = |
Marek Vasut | f0d1a29 | 2022-04-13 04:15:34 +0200 | [diff] [blame] | 197 | __le16_to_cpu(dm9000_inb(db->base_data) + |
| 198 | (dm9000_inb(db->base_data) << 8)); |
Remy Bohmer | a101361 | 2008-06-03 15:26:21 +0200 | [diff] [blame] | 199 | } |
wdenk | 281e00a | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 200 | |
| 201 | /* |
Marek Vasut | a2e9230 | 2022-04-13 04:15:30 +0200 | [diff] [blame] | 202 | * Read a byte from I/O port |
| 203 | */ |
Marek Vasut | f0d1a29 | 2022-04-13 04:15:34 +0200 | [diff] [blame] | 204 | static u8 dm9000_ior(struct dm9000_priv *db, int reg) |
Marek Vasut | a2e9230 | 2022-04-13 04:15:30 +0200 | [diff] [blame] | 205 | { |
Marek Vasut | f0d1a29 | 2022-04-13 04:15:34 +0200 | [diff] [blame] | 206 | dm9000_outb(reg, db->base_io); |
| 207 | return dm9000_inb(db->base_data); |
Marek Vasut | a2e9230 | 2022-04-13 04:15:30 +0200 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | /* |
| 211 | * Write a byte to I/O port |
| 212 | */ |
Marek Vasut | f0d1a29 | 2022-04-13 04:15:34 +0200 | [diff] [blame] | 213 | static void dm9000_iow(struct dm9000_priv *db, int reg, u8 value) |
Marek Vasut | a2e9230 | 2022-04-13 04:15:30 +0200 | [diff] [blame] | 214 | { |
Marek Vasut | f0d1a29 | 2022-04-13 04:15:34 +0200 | [diff] [blame] | 215 | dm9000_outb(reg, db->base_io); |
| 216 | dm9000_outb(value, db->base_data); |
Marek Vasut | a2e9230 | 2022-04-13 04:15:30 +0200 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | /* |
| 220 | * Read a word from phyxcer |
| 221 | */ |
Marek Vasut | f0d1a29 | 2022-04-13 04:15:34 +0200 | [diff] [blame] | 222 | static u16 dm9000_phy_read(struct dm9000_priv *db, int reg) |
Marek Vasut | a2e9230 | 2022-04-13 04:15:30 +0200 | [diff] [blame] | 223 | { |
| 224 | u16 val; |
| 225 | |
| 226 | /* Fill the phyxcer register into REG_0C */ |
Marek Vasut | f0d1a29 | 2022-04-13 04:15:34 +0200 | [diff] [blame] | 227 | dm9000_iow(db, DM9000_EPAR, DM9000_PHY | reg); |
| 228 | dm9000_iow(db, DM9000_EPCR, 0xc); /* Issue phyxcer read command */ |
Marek Vasut | a2e9230 | 2022-04-13 04:15:30 +0200 | [diff] [blame] | 229 | udelay(100); /* Wait read complete */ |
Marek Vasut | f0d1a29 | 2022-04-13 04:15:34 +0200 | [diff] [blame] | 230 | dm9000_iow(db, DM9000_EPCR, 0x0); /* Clear phyxcer read command */ |
| 231 | val = (dm9000_ior(db, DM9000_EPDRH) << 8) | |
| 232 | dm9000_ior(db, DM9000_EPDRL); |
Marek Vasut | a2e9230 | 2022-04-13 04:15:30 +0200 | [diff] [blame] | 233 | |
| 234 | /* The read data keeps on REG_0D & REG_0E */ |
| 235 | debug("%s(0x%x): 0x%x\n", __func__, reg, val); |
| 236 | return val; |
| 237 | } |
| 238 | |
| 239 | /* |
| 240 | * Write a word to phyxcer |
| 241 | */ |
Marek Vasut | f0d1a29 | 2022-04-13 04:15:34 +0200 | [diff] [blame] | 242 | static void dm9000_phy_write(struct dm9000_priv *db, int reg, u16 value) |
Marek Vasut | a2e9230 | 2022-04-13 04:15:30 +0200 | [diff] [blame] | 243 | { |
| 244 | /* Fill the phyxcer register into REG_0C */ |
Marek Vasut | f0d1a29 | 2022-04-13 04:15:34 +0200 | [diff] [blame] | 245 | dm9000_iow(db, DM9000_EPAR, DM9000_PHY | reg); |
Marek Vasut | a2e9230 | 2022-04-13 04:15:30 +0200 | [diff] [blame] | 246 | |
| 247 | /* Fill the written data into REG_0D & REG_0E */ |
Marek Vasut | f0d1a29 | 2022-04-13 04:15:34 +0200 | [diff] [blame] | 248 | dm9000_iow(db, DM9000_EPDRL, (value & 0xff)); |
| 249 | dm9000_iow(db, DM9000_EPDRH, ((value >> 8) & 0xff)); |
| 250 | dm9000_iow(db, DM9000_EPCR, 0xa); /* Issue phyxcer write command */ |
Marek Vasut | a2e9230 | 2022-04-13 04:15:30 +0200 | [diff] [blame] | 251 | udelay(500); /* Wait write complete */ |
Marek Vasut | f0d1a29 | 2022-04-13 04:15:34 +0200 | [diff] [blame] | 252 | dm9000_iow(db, DM9000_EPCR, 0x0); /* Clear phyxcer write command */ |
Marek Vasut | a2e9230 | 2022-04-13 04:15:30 +0200 | [diff] [blame] | 253 | debug("%s(reg:0x%x, value:0x%x)\n", __func__, reg, value); |
| 254 | } |
| 255 | |
| 256 | /* |
Marek Vasut | a7bebf8 | 2022-04-13 04:15:29 +0200 | [diff] [blame] | 257 | * Search DM9000 board, allocate space and register it |
| 258 | */ |
Marek Vasut | f0d1a29 | 2022-04-13 04:15:34 +0200 | [diff] [blame] | 259 | static int dm9000_probe(struct dm9000_priv *db) |
wdenk | 281e00a | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 260 | { |
| 261 | u32 id_val; |
Marek Vasut | a7bebf8 | 2022-04-13 04:15:29 +0200 | [diff] [blame] | 262 | |
Marek Vasut | f0d1a29 | 2022-04-13 04:15:34 +0200 | [diff] [blame] | 263 | id_val = dm9000_ior(db, DM9000_VIDL); |
| 264 | id_val |= dm9000_ior(db, DM9000_VIDH) << 8; |
| 265 | id_val |= dm9000_ior(db, DM9000_PIDL) << 16; |
| 266 | id_val |= dm9000_ior(db, DM9000_PIDH) << 24; |
Marek Vasut | a7bebf8 | 2022-04-13 04:15:29 +0200 | [diff] [blame] | 267 | if (id_val != DM9000_ID) { |
Marek Vasut | f0d1a29 | 2022-04-13 04:15:34 +0200 | [diff] [blame] | 268 | printf("dm9000 not found at 0x%p id: 0x%08x\n", |
| 269 | db->base_io, id_val); |
wdenk | 281e00a | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 270 | return -1; |
| 271 | } |
Marek Vasut | a7bebf8 | 2022-04-13 04:15:29 +0200 | [diff] [blame] | 272 | |
Marek Vasut | f0d1a29 | 2022-04-13 04:15:34 +0200 | [diff] [blame] | 273 | printf("dm9000 i/o: 0x%p, id: 0x%x\n", db->base_io, id_val); |
Marek Vasut | a7bebf8 | 2022-04-13 04:15:29 +0200 | [diff] [blame] | 274 | return 0; |
wdenk | 281e00a | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 275 | } |
| 276 | |
wdenk | 281e00a | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 277 | /* General Purpose dm9000 reset routine */ |
Marek Vasut | f0d1a29 | 2022-04-13 04:15:34 +0200 | [diff] [blame] | 278 | static void dm9000_reset(struct dm9000_priv *db) |
wdenk | 281e00a | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 279 | { |
Marek Vasut | 42a7e0f | 2022-04-13 04:15:24 +0200 | [diff] [blame] | 280 | debug("resetting DM9000\n"); |
Remy Bohmer | fbcb7ec | 2008-06-03 15:26:24 +0200 | [diff] [blame] | 281 | |
Marek Vasut | a7bebf8 | 2022-04-13 04:15:29 +0200 | [diff] [blame] | 282 | /* |
| 283 | * Reset DM9000, |
| 284 | * see DM9000 Application Notes V1.22 Jun 11, 2004 page 29 |
| 285 | */ |
Remy Bohmer | fbcb7ec | 2008-06-03 15:26:24 +0200 | [diff] [blame] | 286 | |
Andrew Dyer | d26b739 | 2008-08-26 17:03:38 -0500 | [diff] [blame] | 287 | /* DEBUG: Make all GPIO0 outputs, all others inputs */ |
Marek Vasut | f0d1a29 | 2022-04-13 04:15:34 +0200 | [diff] [blame] | 288 | dm9000_iow(db, DM9000_GPCR, GPCR_GPIO0_OUT); |
Remy Bohmer | fbcb7ec | 2008-06-03 15:26:24 +0200 | [diff] [blame] | 289 | /* Step 1: Power internal PHY by writing 0 to GPIO0 pin */ |
Marek Vasut | f0d1a29 | 2022-04-13 04:15:34 +0200 | [diff] [blame] | 290 | dm9000_iow(db, DM9000_GPR, 0); |
Remy Bohmer | fbcb7ec | 2008-06-03 15:26:24 +0200 | [diff] [blame] | 291 | /* Step 2: Software reset */ |
Marek Vasut | f0d1a29 | 2022-04-13 04:15:34 +0200 | [diff] [blame] | 292 | dm9000_iow(db, DM9000_NCR, (NCR_LBK_INT_MAC | NCR_RST)); |
Remy Bohmer | fbcb7ec | 2008-06-03 15:26:24 +0200 | [diff] [blame] | 293 | |
| 294 | do { |
Marek Vasut | 42a7e0f | 2022-04-13 04:15:24 +0200 | [diff] [blame] | 295 | debug("resetting the DM9000, 1st reset\n"); |
Remy Bohmer | fbcb7ec | 2008-06-03 15:26:24 +0200 | [diff] [blame] | 296 | udelay(25); /* Wait at least 20 us */ |
Marek Vasut | f0d1a29 | 2022-04-13 04:15:34 +0200 | [diff] [blame] | 297 | } while (dm9000_ior(db, DM9000_NCR) & 1); |
Remy Bohmer | fbcb7ec | 2008-06-03 15:26:24 +0200 | [diff] [blame] | 298 | |
Marek Vasut | f0d1a29 | 2022-04-13 04:15:34 +0200 | [diff] [blame] | 299 | dm9000_iow(db, DM9000_NCR, 0); |
| 300 | dm9000_iow(db, DM9000_NCR, (NCR_LBK_INT_MAC | NCR_RST)); /* Issue a second reset */ |
Remy Bohmer | fbcb7ec | 2008-06-03 15:26:24 +0200 | [diff] [blame] | 301 | |
| 302 | do { |
Marek Vasut | 42a7e0f | 2022-04-13 04:15:24 +0200 | [diff] [blame] | 303 | debug("resetting the DM9000, 2nd reset\n"); |
Remy Bohmer | fbcb7ec | 2008-06-03 15:26:24 +0200 | [diff] [blame] | 304 | udelay(25); /* Wait at least 20 us */ |
Marek Vasut | f0d1a29 | 2022-04-13 04:15:34 +0200 | [diff] [blame] | 305 | } while (dm9000_ior(db, DM9000_NCR) & 1); |
Remy Bohmer | fbcb7ec | 2008-06-03 15:26:24 +0200 | [diff] [blame] | 306 | |
| 307 | /* Check whether the ethernet controller is present */ |
Marek Vasut | f0d1a29 | 2022-04-13 04:15:34 +0200 | [diff] [blame] | 308 | if ((dm9000_ior(db, DM9000_PIDL) != 0x0) || |
| 309 | (dm9000_ior(db, DM9000_PIDH) != 0x90)) |
Remy Bohmer | fbcb7ec | 2008-06-03 15:26:24 +0200 | [diff] [blame] | 310 | printf("ERROR: resetting DM9000 -> not responding\n"); |
wdenk | 281e00a | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 311 | } |
| 312 | |
Marek Vasut | a7bebf8 | 2022-04-13 04:15:29 +0200 | [diff] [blame] | 313 | /* Initialize dm9000 board */ |
Marek Vasut | 85a7260 | 2022-04-13 04:15:35 +0200 | [diff] [blame] | 314 | static int dm9000_init_common(struct dm9000_priv *db, u8 enetaddr[6]) |
wdenk | 281e00a | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 315 | { |
| 316 | int i, oft, lnk; |
Remy Bohmer | a101361 | 2008-06-03 15:26:21 +0200 | [diff] [blame] | 317 | u8 io_mode; |
Remy Bohmer | a101361 | 2008-06-03 15:26:21 +0200 | [diff] [blame] | 318 | |
wdenk | 281e00a | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 319 | /* RESET device */ |
Marek Vasut | f0d1a29 | 2022-04-13 04:15:34 +0200 | [diff] [blame] | 320 | dm9000_reset(db); |
Andrew Dyer | d26b739 | 2008-08-26 17:03:38 -0500 | [diff] [blame] | 321 | |
Marek Vasut | f0d1a29 | 2022-04-13 04:15:34 +0200 | [diff] [blame] | 322 | if (dm9000_probe(db) < 0) |
Andrew Dyer | d26b739 | 2008-08-26 17:03:38 -0500 | [diff] [blame] | 323 | return -1; |
wdenk | 281e00a | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 324 | |
Remy Bohmer | a101361 | 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 | f0d1a29 | 2022-04-13 04:15:34 +0200 | [diff] [blame] | 326 | io_mode = dm9000_ior(db, DM9000_ISR) >> 6; |
Remy Bohmer | a101361 | 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 | d26b739 | 2008-08-26 17:03:38 -0500 | [diff] [blame] | 356 | /* Program operating register, only internal phy supported */ |
Marek Vasut | f0d1a29 | 2022-04-13 04:15:34 +0200 | [diff] [blame] | 357 | dm9000_iow(db, DM9000_NCR, 0x0); |
Remy Bohmer | 98291e2 | 2008-06-03 15:26:26 +0200 | [diff] [blame] | 358 | /* TX Polling clear */ |
Marek Vasut | f0d1a29 | 2022-04-13 04:15:34 +0200 | [diff] [blame] | 359 | dm9000_iow(db, DM9000_TCR, 0); |
Remy Bohmer | 98291e2 | 2008-06-03 15:26:26 +0200 | [diff] [blame] | 360 | /* Less 3Kb, 200us */ |
Marek Vasut | f0d1a29 | 2022-04-13 04:15:34 +0200 | [diff] [blame] | 361 | dm9000_iow(db, DM9000_BPTR, BPTR_BPHW(3) | BPTR_JPT_600US); |
Remy Bohmer | 98291e2 | 2008-06-03 15:26:26 +0200 | [diff] [blame] | 362 | /* Flow Control : High/Low Water */ |
Marek Vasut | f0d1a29 | 2022-04-13 04:15:34 +0200 | [diff] [blame] | 363 | dm9000_iow(db, DM9000_FCTR, FCTR_HWOT(3) | FCTR_LWOT(8)); |
Remy Bohmer | 98291e2 | 2008-06-03 15:26:26 +0200 | [diff] [blame] | 364 | /* SH FIXME: This looks strange! Flow Control */ |
Marek Vasut | f0d1a29 | 2022-04-13 04:15:34 +0200 | [diff] [blame] | 365 | dm9000_iow(db, DM9000_FCR, 0x0); |
Remy Bohmer | 98291e2 | 2008-06-03 15:26:26 +0200 | [diff] [blame] | 366 | /* Special Mode */ |
Marek Vasut | f0d1a29 | 2022-04-13 04:15:34 +0200 | [diff] [blame] | 367 | dm9000_iow(db, DM9000_SMCR, 0); |
Remy Bohmer | 98291e2 | 2008-06-03 15:26:26 +0200 | [diff] [blame] | 368 | /* clear TX status */ |
Marek Vasut | f0d1a29 | 2022-04-13 04:15:34 +0200 | [diff] [blame] | 369 | dm9000_iow(db, DM9000_NSR, NSR_WAKEST | NSR_TX2END | NSR_TX1END); |
Remy Bohmer | 98291e2 | 2008-06-03 15:26:26 +0200 | [diff] [blame] | 370 | /* Clear interrupt status */ |
Marek Vasut | f0d1a29 | 2022-04-13 04:15:34 +0200 | [diff] [blame] | 371 | dm9000_iow(db, DM9000_ISR, ISR_ROOS | ISR_ROS | ISR_PTS | ISR_PRS); |
wdenk | 281e00a | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 372 | |
Marek Vasut | 85a7260 | 2022-04-13 04:15:35 +0200 | [diff] [blame] | 373 | printf("MAC: %pM\n", enetaddr); |
| 374 | if (!is_valid_ethaddr(enetaddr)) |
Andrew Ruder | c583ee1 | 2013-10-22 19:09:02 -0500 | [diff] [blame] | 375 | printf("WARNING: Bad MAC address (uninitialized EEPROM?)\n"); |
Andrew Dyer | d26b739 | 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 | 85a7260 | 2022-04-13 04:15:35 +0200 | [diff] [blame] | 379 | dm9000_iow(db, oft, enetaddr[i]); |
wdenk | 281e00a | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 380 | for (i = 0, oft = 0x16; i < 8; i++, oft++) |
Marek Vasut | f0d1a29 | 2022-04-13 04:15:34 +0200 | [diff] [blame] | 381 | dm9000_iow(db, oft, 0xff); |
wdenk | 281e00a | 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 | f0d1a29 | 2022-04-13 04:15:34 +0200 | [diff] [blame] | 385 | debug("%02x:", dm9000_ior(db, oft)); |
Marek Vasut | 42a7e0f | 2022-04-13 04:15:24 +0200 | [diff] [blame] | 386 | debug("\n"); |
wdenk | 281e00a | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 387 | |
| 388 | /* Activate DM9000 */ |
Remy Bohmer | 98291e2 | 2008-06-03 15:26:26 +0200 | [diff] [blame] | 389 | /* RX enable */ |
Marek Vasut | f0d1a29 | 2022-04-13 04:15:34 +0200 | [diff] [blame] | 390 | dm9000_iow(db, DM9000_RCR, RCR_DIS_LONG | RCR_DIS_CRC | RCR_RXEN); |
Remy Bohmer | 98291e2 | 2008-06-03 15:26:26 +0200 | [diff] [blame] | 391 | /* Enable TX/RX interrupt mask */ |
Marek Vasut | f0d1a29 | 2022-04-13 04:15:34 +0200 | [diff] [blame] | 392 | dm9000_iow(db, DM9000_IMR, IMR_PAR); |
Remy Bohmer | 98291e2 | 2008-06-03 15:26:26 +0200 | [diff] [blame] | 393 | |
wdenk | 281e00a | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 394 | i = 0; |
Marek Vasut | f0d1a29 | 2022-04-13 04:15:34 +0200 | [diff] [blame] | 395 | while (!(dm9000_phy_read(db, 1) & 0x20)) { /* autonegation complete bit */ |
wdenk | 281e00a | 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 */ |
Marek Vasut | f0d1a29 | 2022-04-13 04:15:34 +0200 | [diff] [blame] | 405 | lnk = dm9000_phy_read(db, 17) >> 12; |
wdenk | 281e00a | 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 | a7bebf8 | 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 | 85a7260 | 2022-04-13 04:15:35 +0200 | [diff] [blame] | 432 | static int dm9000_send_common(struct dm9000_priv *db, void *packet, int length) |
wdenk | 281e00a | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 433 | { |
wdenk | 281e00a | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 434 | int tmo; |
Remy Bohmer | a101361 | 2008-06-03 15:26:21 +0200 | [diff] [blame] | 435 | |
Marek Vasut | a7bebf8 | 2022-04-13 04:15:29 +0200 | [diff] [blame] | 436 | dm9000_dump_packet(__func__, packet, length); |
wdenk | 281e00a | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 437 | |
Marek Vasut | f0d1a29 | 2022-04-13 04:15:34 +0200 | [diff] [blame] | 438 | dm9000_iow(db, DM9000_ISR, IMR_PTM); /* Clear Tx bit in ISR */ |
Remy Bohmer | acba318 | 2008-06-03 15:26:23 +0200 | [diff] [blame] | 439 | |
wdenk | 281e00a | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 440 | /* Move data to DM9000 TX RAM */ |
Marek Vasut | f0d1a29 | 2022-04-13 04:15:34 +0200 | [diff] [blame] | 441 | dm9000_outb(DM9000_MWCMD, db->base_io); /* Prepare for TX-data */ |
wdenk | 281e00a | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 442 | |
Remy Bohmer | a101361 | 2008-06-03 15:26:21 +0200 | [diff] [blame] | 443 | /* push the data to the TX-fifo */ |
Marek Vasut | f0d1a29 | 2022-04-13 04:15:34 +0200 | [diff] [blame] | 444 | db->outblk(db, packet, length); |
wdenk | 281e00a | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 445 | |
| 446 | /* Set TX length to DM9000 */ |
Marek Vasut | f0d1a29 | 2022-04-13 04:15:34 +0200 | [diff] [blame] | 447 | dm9000_iow(db, DM9000_TXPLL, length & 0xff); |
| 448 | dm9000_iow(db, DM9000_TXPLH, (length >> 8) & 0xff); |
wdenk | 281e00a | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 449 | |
| 450 | /* Issue TX polling command */ |
Marek Vasut | f0d1a29 | 2022-04-13 04:15:34 +0200 | [diff] [blame] | 451 | dm9000_iow(db, DM9000_TCR, TCR_TXREQ); /* Cleared after TX complete */ |
wdenk | 281e00a | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 452 | |
| 453 | /* wait for end of transmission */ |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 454 | tmo = get_timer(0) + 5 * CONFIG_SYS_HZ; |
Marek Vasut | f0d1a29 | 2022-04-13 04:15:34 +0200 | [diff] [blame] | 455 | while (!(dm9000_ior(db, DM9000_NSR) & (NSR_TX1END | NSR_TX2END)) || |
| 456 | !(dm9000_ior(db, DM9000_ISR) & IMR_PTM)) { |
wdenk | 281e00a | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 457 | if (get_timer(0) >= tmo) { |
| 458 | printf("transmission timeout\n"); |
| 459 | break; |
| 460 | } |
| 461 | } |
Marek Vasut | f0d1a29 | 2022-04-13 04:15:34 +0200 | [diff] [blame] | 462 | dm9000_iow(db, DM9000_ISR, IMR_PTM); /* Clear Tx bit in ISR */ |
Remy Bohmer | acba318 | 2008-06-03 15:26:23 +0200 | [diff] [blame] | 463 | |
Marek Vasut | 42a7e0f | 2022-04-13 04:15:24 +0200 | [diff] [blame] | 464 | debug("transmit done\n\n"); |
wdenk | 281e00a | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 465 | return 0; |
| 466 | } |
| 467 | |
| 468 | /* |
Marek Vasut | a7bebf8 | 2022-04-13 04:15:29 +0200 | [diff] [blame] | 469 | * Stop the interface. |
| 470 | * The interface is stopped when it is brought. |
| 471 | */ |
Marek Vasut | 85a7260 | 2022-04-13 04:15:35 +0200 | [diff] [blame] | 472 | static void dm9000_halt_common(struct dm9000_priv *db) |
wdenk | 281e00a | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 473 | { |
Marek Vasut | a7bebf8 | 2022-04-13 04:15:29 +0200 | [diff] [blame] | 474 | /* RESET device */ |
Marek Vasut | f0d1a29 | 2022-04-13 04:15:34 +0200 | [diff] [blame] | 475 | dm9000_phy_write(db, 0, 0x8000); /* PHY RESET */ |
| 476 | dm9000_iow(db, DM9000_GPR, 0x01); /* Power-Down PHY */ |
| 477 | dm9000_iow(db, DM9000_IMR, 0x80); /* Disable all interrupt */ |
| 478 | dm9000_iow(db, DM9000_RCR, 0x00); /* Disable RX */ |
wdenk | 281e00a | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 479 | } |
| 480 | |
| 481 | /* |
Marek Vasut | a7bebf8 | 2022-04-13 04:15:29 +0200 | [diff] [blame] | 482 | * Received a packet and pass to upper layer |
| 483 | */ |
Marek Vasut | 84bf20f | 2022-04-13 04:15:36 +0200 | [diff] [blame] | 484 | static int dm9000_recv_common(struct dm9000_priv *db, uchar *rdptr) |
wdenk | 281e00a | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 485 | { |
Joe Hershberger | 1fd92db | 2015-04-08 01:41:06 -0500 | [diff] [blame] | 486 | u8 rxbyte; |
Marek Vasut | d8f21b2 | 2022-04-13 04:15:25 +0200 | [diff] [blame] | 487 | u16 rxstatus, rxlen = 0; |
wdenk | 281e00a | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 488 | |
Marek Vasut | a7bebf8 | 2022-04-13 04:15:29 +0200 | [diff] [blame] | 489 | /* |
| 490 | * Check packet ready or not, we must check |
| 491 | * the ISR status first for DM9000A |
| 492 | */ |
Marek Vasut | f0d1a29 | 2022-04-13 04:15:34 +0200 | [diff] [blame] | 493 | if (!(dm9000_ior(db, DM9000_ISR) & 0x01)) /* Rx-ISR bit must be set. */ |
wdenk | 281e00a | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 494 | return 0; |
| 495 | |
Marek Vasut | f0d1a29 | 2022-04-13 04:15:34 +0200 | [diff] [blame] | 496 | dm9000_iow(db, DM9000_ISR, 0x01); /* clear PR status latched in bit 0 */ |
wdenk | 281e00a | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 497 | |
Remy Bohmer | 850ba75 | 2008-06-03 15:26:25 +0200 | [diff] [blame] | 498 | /* There is _at least_ 1 package in the fifo, read them all */ |
Marek Vasut | 84bf20f | 2022-04-13 04:15:36 +0200 | [diff] [blame] | 499 | dm9000_ior(db, DM9000_MRCMDX); /* Dummy read */ |
wdenk | 281e00a | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 500 | |
Marek Vasut | 84bf20f | 2022-04-13 04:15:36 +0200 | [diff] [blame] | 501 | /* |
| 502 | * Get most updated data, |
| 503 | * only look at bits 0:1, See application notes DM9000 |
| 504 | */ |
| 505 | rxbyte = dm9000_inb(db->base_data) & 0x03; |
wdenk | 281e00a | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 506 | |
Marek Vasut | 84bf20f | 2022-04-13 04:15:36 +0200 | [diff] [blame] | 507 | /* Status check: this byte must be 0 or 1 */ |
| 508 | if (rxbyte > DM9000_PKT_RDY) { |
| 509 | dm9000_iow(db, DM9000_RCR, 0x00); /* Stop Device */ |
| 510 | dm9000_iow(db, DM9000_ISR, 0x80); /* Stop INT request */ |
| 511 | printf("DM9000 error: status check fail: 0x%x\n", |
| 512 | rxbyte); |
| 513 | return -EINVAL; |
wdenk | 281e00a | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 514 | } |
Marek Vasut | 84bf20f | 2022-04-13 04:15:36 +0200 | [diff] [blame] | 515 | |
| 516 | if (rxbyte != DM9000_PKT_RDY) |
| 517 | return 0; /* No packet received, ignore */ |
| 518 | |
| 519 | debug("receiving packet\n"); |
| 520 | |
| 521 | /* A packet ready now & Get status/length */ |
| 522 | db->rx_status(db, &rxstatus, &rxlen); |
| 523 | |
| 524 | debug("rx status: 0x%04x rx len: %d\n", rxstatus, rxlen); |
| 525 | |
| 526 | /* Move data from DM9000 */ |
| 527 | /* Read received packet from RX SRAM */ |
| 528 | db->inblk(db, rdptr, rxlen); |
| 529 | |
| 530 | if (rxstatus & 0xbf00 || rxlen < 0x40 || rxlen > DM9000_PKT_MAX) { |
| 531 | if (rxstatus & 0x100) |
| 532 | printf("rx fifo error\n"); |
| 533 | if (rxstatus & 0x200) |
| 534 | printf("rx crc error\n"); |
| 535 | if (rxstatus & 0x8000) |
| 536 | printf("rx length error\n"); |
| 537 | if (rxlen > DM9000_PKT_MAX) { |
| 538 | printf("rx length too big\n"); |
| 539 | dm9000_reset(db); |
| 540 | } |
| 541 | return -EINVAL; |
| 542 | } |
| 543 | |
| 544 | return rxlen; |
wdenk | 281e00a | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 545 | } |
| 546 | |
| 547 | /* |
Marek Vasut | a7bebf8 | 2022-04-13 04:15:29 +0200 | [diff] [blame] | 548 | * Read a word data from SROM |
| 549 | */ |
Remy Bohmer | e5a3bc2 | 2009-05-03 12:11:40 +0200 | [diff] [blame] | 550 | #if !defined(CONFIG_DM9000_NO_SROM) |
Marek Vasut | f0d1a29 | 2022-04-13 04:15:34 +0200 | [diff] [blame] | 551 | static void dm9000_read_srom_word(struct dm9000_priv *db, int offset, u8 *to) |
wdenk | 281e00a | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 552 | { |
Marek Vasut | f0d1a29 | 2022-04-13 04:15:34 +0200 | [diff] [blame] | 553 | dm9000_iow(db, DM9000_EPAR, offset); |
| 554 | dm9000_iow(db, DM9000_EPCR, 0x4); |
Marek Vasut | a7bebf8 | 2022-04-13 04:15:29 +0200 | [diff] [blame] | 555 | mdelay(8); |
Marek Vasut | f0d1a29 | 2022-04-13 04:15:34 +0200 | [diff] [blame] | 556 | dm9000_iow(db, DM9000_EPCR, 0x0); |
| 557 | to[0] = dm9000_ior(db, DM9000_EPDRL); |
| 558 | to[1] = dm9000_ior(db, DM9000_EPDRH); |
wdenk | 281e00a | 2004-08-01 22:48:16 +0000 | [diff] [blame] | 559 | } |
| 560 | |
Marek Vasut | 85a7260 | 2022-04-13 04:15:35 +0200 | [diff] [blame] | 561 | static void dm9000_get_enetaddr(struct dm9000_priv *db, u8 *enetaddr) |
Ben Warren | 0775437 | 2009-10-21 21:53:39 -0700 | [diff] [blame] | 562 | { |
Ben Warren | 0775437 | 2009-10-21 21:53:39 -0700 | [diff] [blame] | 563 | int i; |
Marek Vasut | a7bebf8 | 2022-04-13 04:15:29 +0200 | [diff] [blame] | 564 | |
Ben Warren | 0775437 | 2009-10-21 21:53:39 -0700 | [diff] [blame] | 565 | for (i = 0; i < 3; i++) |
Marek Vasut | 85a7260 | 2022-04-13 04:15:35 +0200 | [diff] [blame] | 566 | dm9000_read_srom_word(db, i, enetaddr + (2 * i)); |
Ben Warren | 0775437 | 2009-10-21 21:53:39 -0700 | [diff] [blame] | 567 | } |
Marek Vasut | a7bebf8 | 2022-04-13 04:15:29 +0200 | [diff] [blame] | 568 | #else |
Marek Vasut | 85a7260 | 2022-04-13 04:15:35 +0200 | [diff] [blame] | 569 | static void dm9000_get_enetaddr(struct dm9000_priv *db, u8 *enetaddr) {} |
Marek Vasut | a7bebf8 | 2022-04-13 04:15:29 +0200 | [diff] [blame] | 570 | #endif |
Ben Warren | 0775437 | 2009-10-21 21:53:39 -0700 | [diff] [blame] | 571 | |
Marek Vasut | 41e10be | 2022-04-13 04:15:37 +0200 | [diff] [blame] | 572 | static int dm9000_start(struct udevice *dev) |
| 573 | { |
| 574 | struct dm9000_priv *db = dev_get_priv(dev); |
| 575 | struct eth_pdata *pdata = dev_get_plat(dev); |
| 576 | |
| 577 | return dm9000_init_common(db, pdata->enetaddr); |
| 578 | } |
| 579 | |
| 580 | static void dm9000_stop(struct udevice *dev) |
| 581 | { |
| 582 | struct dm9000_priv *db = dev_get_priv(dev); |
| 583 | |
| 584 | dm9000_halt_common(db); |
| 585 | } |
| 586 | |
| 587 | static int dm9000_send(struct udevice *dev, void *packet, int length) |
| 588 | { |
| 589 | struct dm9000_priv *db = dev_get_priv(dev); |
| 590 | int ret; |
| 591 | |
| 592 | ret = dm9000_send_common(db, packet, length); |
| 593 | |
| 594 | return ret ? 0 : -ETIMEDOUT; |
| 595 | } |
| 596 | |
| 597 | static int dm9000_recv(struct udevice *dev, int flags, uchar **packetp) |
| 598 | { |
| 599 | struct dm9000_priv *db = dev_get_priv(dev); |
| 600 | uchar *data = net_rx_packets[0]; |
| 601 | int ret; |
| 602 | |
| 603 | ret = dm9000_recv_common(db, data); |
Marek Vasut | ecd8b03 | 2022-04-25 20:28:05 +0200 | [diff] [blame] | 604 | if (ret > 0) |
Marek Vasut | 41e10be | 2022-04-13 04:15:37 +0200 | [diff] [blame] | 605 | *packetp = (void *)data; |
| 606 | |
Marek Vasut | ecd8b03 | 2022-04-25 20:28:05 +0200 | [diff] [blame] | 607 | return ret >= 0 ? ret : -EAGAIN; |
Marek Vasut | 41e10be | 2022-04-13 04:15:37 +0200 | [diff] [blame] | 608 | } |
| 609 | |
| 610 | static int dm9000_write_hwaddr(struct udevice *dev) |
| 611 | { |
| 612 | struct dm9000_priv *db = dev_get_priv(dev); |
| 613 | struct eth_pdata *pdata = dev_get_plat(dev); |
| 614 | int i, oft; |
| 615 | |
| 616 | /* fill device MAC address registers */ |
| 617 | for (i = 0, oft = DM9000_PAR; i < 6; i++, oft++) |
| 618 | dm9000_iow(db, oft, pdata->enetaddr[i]); |
| 619 | |
| 620 | for (i = 0, oft = 0x16; i < 8; i++, oft++) |
| 621 | dm9000_iow(db, oft, 0xff); |
| 622 | |
| 623 | /* read back mac, just to be sure */ |
| 624 | for (i = 0, oft = 0x10; i < 6; i++, oft++) |
| 625 | debug("%02x:", dm9000_ior(db, oft)); |
| 626 | |
| 627 | debug("\n"); |
| 628 | |
| 629 | return 0; |
| 630 | } |
| 631 | |
| 632 | static int dm9000_read_rom_hwaddr(struct udevice *dev) |
| 633 | { |
| 634 | struct dm9000_priv *db = dev_get_priv(dev); |
| 635 | struct eth_pdata *pdata = dev_get_plat(dev); |
| 636 | |
| 637 | dm9000_get_enetaddr(db, pdata->enetaddr); |
| 638 | |
| 639 | return !is_valid_ethaddr(pdata->enetaddr); |
| 640 | } |
| 641 | |
| 642 | static int dm9000_bind(struct udevice *dev) |
| 643 | { |
| 644 | return device_set_name(dev, dev->name); |
| 645 | } |
| 646 | |
| 647 | static int dm9000_of_to_plat(struct udevice *dev) |
| 648 | { |
| 649 | struct dm9000_priv *db = dev_get_priv(dev); |
| 650 | struct eth_pdata *pdata = dev_get_plat(dev); |
| 651 | |
| 652 | pdata->iobase = dev_read_addr_index(dev, 0); |
| 653 | db->base_io = (void __iomem *)pdata->iobase; |
Johan Jonker | e5822ec | 2023-03-13 01:31:49 +0100 | [diff] [blame] | 654 | db->base_data = dev_read_addr_index_ptr(dev, 1); |
Marek Vasut | 41e10be | 2022-04-13 04:15:37 +0200 | [diff] [blame] | 655 | |
| 656 | return 0; |
| 657 | } |
| 658 | |
| 659 | static const struct eth_ops dm9000_ops = { |
| 660 | .start = dm9000_start, |
| 661 | .stop = dm9000_stop, |
| 662 | .send = dm9000_send, |
| 663 | .recv = dm9000_recv, |
| 664 | .write_hwaddr = dm9000_write_hwaddr, |
| 665 | .read_rom_hwaddr = dm9000_read_rom_hwaddr, |
| 666 | }; |
| 667 | |
| 668 | static const struct udevice_id dm9000_ids[] = { |
| 669 | { .compatible = "davicom,dm9000" }, |
| 670 | { } |
| 671 | }; |
| 672 | |
| 673 | U_BOOT_DRIVER(dm9000) = { |
| 674 | .name = "eth_dm9000", |
| 675 | .id = UCLASS_ETH, |
| 676 | .of_match = dm9000_ids, |
| 677 | .bind = dm9000_bind, |
| 678 | .of_to_plat = dm9000_of_to_plat, |
| 679 | .ops = &dm9000_ops, |
| 680 | .priv_auto = sizeof(struct dm9000_priv), |
| 681 | .plat_auto = sizeof(struct eth_pdata), |
| 682 | .flags = DM_FLAG_ALLOC_PRIV_DMA, |
| 683 | }; |