blob: becf7aec828d6bce462e9faad77f023a3db1d8b3 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
wdenk7ac16102004-08-01 22:48:16 +00002/*
Marek Vasuta4aa0482022-04-13 04:15:29 +02003 * 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 */
wdenk7ac16102004-08-01 22:48:16 +000051
52#include <common.h>
53#include <command.h>
54#include <net.h>
55#include <asm/io.h>
Remy Bohmercd9a36c2009-05-03 12:11:40 +020056#include <dm9000.h>
Simon Glassdbd79542020-05-10 11:40:11 -060057#include <linux/delay.h>
wdenk7ac16102004-08-01 22:48:16 +000058
wdenk7ac16102004-08-01 22:48:16 +000059#include "dm9000x.h"
60
wdenk7ac16102004-08-01 22:48:16 +000061/* Structure/enum declaration ------------------------------- */
Marek Vasuta4aa0482022-04-13 04:15:29 +020062struct board_info {
wdenk7ac16102004-08-01 22:48:16 +000063 u32 runt_length_counter; /* counter: RX length < 64byte */
64 u32 long_length_counter; /* counter: RX length > 1514byte */
65 u32 reset_counter; /* counter: RESET */
66 u32 reset_tx_timeout; /* RESET caused by TX Timeout */
67 u32 reset_rx_status; /* RESET caused by RX Statsus wrong */
68 u16 tx_pkt_cnt;
69 u16 queue_start_addr;
70 u16 dbug_cnt;
71 u8 phy_addr;
72 u8 device_wait_reset; /* device state */
wdenk7ac16102004-08-01 22:48:16 +000073 unsigned char srom[128];
Marek Vasut7823b232022-04-13 04:15:28 +020074 void (*outblk)(void *data_ptr, int count);
Remy Bohmer5f63bf42008-06-03 15:26:21 +020075 void (*inblk)(void *data_ptr, int count);
Marek Vasut5248e562022-04-13 04:15:25 +020076 void (*rx_status)(u16 *rxstatus, u16 *rxlen);
Remy Bohmer7eefd922009-05-02 21:49:18 +020077 struct eth_device netdev;
Marek Vasuta4aa0482022-04-13 04:15:29 +020078};
wdenk7ac16102004-08-01 22:48:16 +000079
Marek Vasuta4aa0482022-04-13 04:15:29 +020080static struct board_info dm9000_info;
Remy Bohmer7eefd922009-05-02 21:49:18 +020081
wdenk7ac16102004-08-01 22:48:16 +000082/* function declaration ------------------------------------- */
wdenk7ac16102004-08-01 22:48:16 +000083static int dm9000_probe(void);
Andy Fleming0d2df962011-03-22 22:49:13 -050084static u16 dm9000_phy_read(int);
85static void dm9000_phy_write(int, u16);
Marek Vasuteb2749a2022-04-13 04:15:23 +020086static u8 dm9000_ior(int);
87static void dm9000_iow(int reg, u8 value);
wdenk7ac16102004-08-01 22:48:16 +000088
89/* DM9000 network board routine ---------------------------- */
Jason Jinc74c4362011-08-25 15:46:43 +080090#ifndef CONFIG_DM9000_BYTE_SWAPPED
Marek Vasuta4aa0482022-04-13 04:15:29 +020091#define dm9000_outb(d, r) writeb((d), (r))
92#define dm9000_outw(d, r) writew((d), (r))
93#define dm9000_outl(d, r) writel((d), (r))
Marek Vasut7823b232022-04-13 04:15:28 +020094#define dm9000_inb(r) readb(r)
95#define dm9000_inw(r) readw(r)
96#define dm9000_inl(r) readl(r)
Jason Jinc74c4362011-08-25 15:46:43 +080097#else
Marek Vasuteb2749a2022-04-13 04:15:23 +020098#define dm9000_outb(d, r) __raw_writeb(d, r)
99#define dm9000_outw(d, r) __raw_writew(d, r)
100#define dm9000_outl(d, r) __raw_writel(d, r)
101#define dm9000_inb(r) __raw_readb(r)
102#define dm9000_inw(r) __raw_readw(r)
103#define dm9000_inl(r) __raw_readl(r)
Jason Jinc74c4362011-08-25 15:46:43 +0800104#endif
wdenk7ac16102004-08-01 22:48:16 +0000105
Marek Vasut52006d22022-04-13 04:15:27 +0200106#ifdef DEBUG
107static void dm9000_dump_packet(const char *func, u8 *packet, int length)
108{
109 int i;
110
111 printf("%s: length: %d\n", func, length);
112
113 for (i = 0; i < length; i++) {
114 if (i % 8 == 0)
115 printf("\n%s: %02x: ", func, i);
116 printf("%02x ", packet[i]);
117 }
118
119 printf("\n");
120}
121#else
122static void dm9000_dump_packet(const char *func, u8 *packet, int length) {}
123#endif
124
Marek Vasut7823b232022-04-13 04:15:28 +0200125static void dm9000_outblk_8bit(void *data_ptr, int count)
Remy Bohmer5f63bf42008-06-03 15:26:21 +0200126{
127 int i;
Marek Vasuta4aa0482022-04-13 04:15:29 +0200128
Remy Bohmer5f63bf42008-06-03 15:26:21 +0200129 for (i = 0; i < count; i++)
Marek Vasuta4aa0482022-04-13 04:15:29 +0200130 dm9000_outb((((u8 *)data_ptr)[i] & 0xff), DM9000_DATA);
Remy Bohmer5f63bf42008-06-03 15:26:21 +0200131}
132
Marek Vasut7823b232022-04-13 04:15:28 +0200133static void dm9000_outblk_16bit(void *data_ptr, int count)
Remy Bohmer5f63bf42008-06-03 15:26:21 +0200134{
135 int i;
136 u32 tmplen = (count + 1) / 2;
137
138 for (i = 0; i < tmplen; i++)
Marek Vasuta4aa0482022-04-13 04:15:29 +0200139 dm9000_outw(((u16 *)data_ptr)[i], DM9000_DATA);
Remy Bohmer5f63bf42008-06-03 15:26:21 +0200140}
Marek Vasuta4aa0482022-04-13 04:15:29 +0200141
Marek Vasut7823b232022-04-13 04:15:28 +0200142static void dm9000_outblk_32bit(void *data_ptr, int count)
Remy Bohmer5f63bf42008-06-03 15:26:21 +0200143{
144 int i;
145 u32 tmplen = (count + 3) / 4;
146
147 for (i = 0; i < tmplen; i++)
Marek Vasuta4aa0482022-04-13 04:15:29 +0200148 dm9000_outl(((u32 *)data_ptr)[i], DM9000_DATA);
Remy Bohmer5f63bf42008-06-03 15:26:21 +0200149}
150
151static void dm9000_inblk_8bit(void *data_ptr, int count)
152{
153 int i;
Marek Vasuta4aa0482022-04-13 04:15:29 +0200154
Remy Bohmer5f63bf42008-06-03 15:26:21 +0200155 for (i = 0; i < count; i++)
Marek Vasuta4aa0482022-04-13 04:15:29 +0200156 ((u8 *)data_ptr)[i] = dm9000_inb(DM9000_DATA);
Remy Bohmer5f63bf42008-06-03 15:26:21 +0200157}
158
159static void dm9000_inblk_16bit(void *data_ptr, int count)
160{
161 int i;
162 u32 tmplen = (count + 1) / 2;
163
164 for (i = 0; i < tmplen; i++)
Marek Vasuta4aa0482022-04-13 04:15:29 +0200165 ((u16 *)data_ptr)[i] = dm9000_inw(DM9000_DATA);
Remy Bohmer5f63bf42008-06-03 15:26:21 +0200166}
Marek Vasuta4aa0482022-04-13 04:15:29 +0200167
Remy Bohmer5f63bf42008-06-03 15:26:21 +0200168static void dm9000_inblk_32bit(void *data_ptr, int count)
169{
170 int i;
171 u32 tmplen = (count + 3) / 4;
172
173 for (i = 0; i < tmplen; i++)
Marek Vasuta4aa0482022-04-13 04:15:29 +0200174 ((u32 *)data_ptr)[i] = dm9000_inl(DM9000_DATA);
Remy Bohmer5f63bf42008-06-03 15:26:21 +0200175}
176
Marek Vasut5248e562022-04-13 04:15:25 +0200177static void dm9000_rx_status_32bit(u16 *rxstatus, u16 *rxlen)
Remy Bohmer5f63bf42008-06-03 15:26:21 +0200178{
Remy Bohmer2e1604f2008-06-04 10:47:25 +0200179 u32 tmpdata;
Remy Bohmer5f63bf42008-06-03 15:26:21 +0200180
Marek Vasuteb2749a2022-04-13 04:15:23 +0200181 dm9000_outb(DM9000_MRCMD, DM9000_IO);
Remy Bohmer5f63bf42008-06-03 15:26:21 +0200182
Marek Vasuteb2749a2022-04-13 04:15:23 +0200183 tmpdata = dm9000_inl(DM9000_DATA);
Marek Vasut5248e562022-04-13 04:15:25 +0200184 *rxstatus = __le16_to_cpu(tmpdata);
185 *rxlen = __le16_to_cpu(tmpdata >> 16);
Remy Bohmer5f63bf42008-06-03 15:26:21 +0200186}
187
Marek Vasut5248e562022-04-13 04:15:25 +0200188static void dm9000_rx_status_16bit(u16 *rxstatus, u16 *rxlen)
Remy Bohmer5f63bf42008-06-03 15:26:21 +0200189{
Marek Vasuteb2749a2022-04-13 04:15:23 +0200190 dm9000_outb(DM9000_MRCMD, DM9000_IO);
Remy Bohmer5f63bf42008-06-03 15:26:21 +0200191
Marek Vasut5248e562022-04-13 04:15:25 +0200192 *rxstatus = __le16_to_cpu(dm9000_inw(DM9000_DATA));
193 *rxlen = __le16_to_cpu(dm9000_inw(DM9000_DATA));
Remy Bohmer5f63bf42008-06-03 15:26:21 +0200194}
195
Marek Vasut5248e562022-04-13 04:15:25 +0200196static void dm9000_rx_status_8bit(u16 *rxstatus, u16 *rxlen)
Remy Bohmer5f63bf42008-06-03 15:26:21 +0200197{
Marek Vasuteb2749a2022-04-13 04:15:23 +0200198 dm9000_outb(DM9000_MRCMD, DM9000_IO);
Remy Bohmer5f63bf42008-06-03 15:26:21 +0200199
Marek Vasut5248e562022-04-13 04:15:25 +0200200 *rxstatus =
Marek Vasuteb2749a2022-04-13 04:15:23 +0200201 __le16_to_cpu(dm9000_inb(DM9000_DATA) +
202 (dm9000_inb(DM9000_DATA) << 8));
Marek Vasut5248e562022-04-13 04:15:25 +0200203 *rxlen =
Marek Vasuteb2749a2022-04-13 04:15:23 +0200204 __le16_to_cpu(dm9000_inb(DM9000_DATA) +
205 (dm9000_inb(DM9000_DATA) << 8));
Remy Bohmer5f63bf42008-06-03 15:26:21 +0200206}
wdenk7ac16102004-08-01 22:48:16 +0000207
208/*
Marek Vasuta4aa0482022-04-13 04:15:29 +0200209 * Search DM9000 board, allocate space and register it
210 */
wdenk7ac16102004-08-01 22:48:16 +0000211int
212dm9000_probe(void)
213{
214 u32 id_val;
Marek Vasuta4aa0482022-04-13 04:15:29 +0200215
Marek Vasuteb2749a2022-04-13 04:15:23 +0200216 id_val = dm9000_ior(DM9000_VIDL);
217 id_val |= dm9000_ior(DM9000_VIDH) << 8;
218 id_val |= dm9000_ior(DM9000_PIDL) << 16;
219 id_val |= dm9000_ior(DM9000_PIDH) << 24;
Marek Vasuta4aa0482022-04-13 04:15:29 +0200220 if (id_val != DM9000_ID) {
wdenk7ac16102004-08-01 22:48:16 +0000221 printf("dm9000 not found at 0x%08x id: 0x%08x\n",
222 CONFIG_DM9000_BASE, id_val);
223 return -1;
224 }
Marek Vasuta4aa0482022-04-13 04:15:29 +0200225
226 printf("dm9000 i/o: 0x%x, id: 0x%x\n", CONFIG_DM9000_BASE, id_val);
227 return 0;
wdenk7ac16102004-08-01 22:48:16 +0000228}
229
230/* General Purpose dm9000 reset routine */
231static void
232dm9000_reset(void)
233{
Marek Vasuted761222022-04-13 04:15:24 +0200234 debug("resetting DM9000\n");
Remy Bohmer2f13d2c2008-06-03 15:26:24 +0200235
Marek Vasuta4aa0482022-04-13 04:15:29 +0200236 /*
237 * Reset DM9000,
238 * see DM9000 Application Notes V1.22 Jun 11, 2004 page 29
239 */
Remy Bohmer2f13d2c2008-06-03 15:26:24 +0200240
Andrew Dyera62f5d42008-08-26 17:03:38 -0500241 /* DEBUG: Make all GPIO0 outputs, all others inputs */
Marek Vasuteb2749a2022-04-13 04:15:23 +0200242 dm9000_iow(DM9000_GPCR, GPCR_GPIO0_OUT);
Remy Bohmer2f13d2c2008-06-03 15:26:24 +0200243 /* Step 1: Power internal PHY by writing 0 to GPIO0 pin */
Marek Vasuteb2749a2022-04-13 04:15:23 +0200244 dm9000_iow(DM9000_GPR, 0);
Remy Bohmer2f13d2c2008-06-03 15:26:24 +0200245 /* Step 2: Software reset */
Marek Vasuteb2749a2022-04-13 04:15:23 +0200246 dm9000_iow(DM9000_NCR, (NCR_LBK_INT_MAC | NCR_RST));
Remy Bohmer2f13d2c2008-06-03 15:26:24 +0200247
248 do {
Marek Vasuted761222022-04-13 04:15:24 +0200249 debug("resetting the DM9000, 1st reset\n");
Remy Bohmer2f13d2c2008-06-03 15:26:24 +0200250 udelay(25); /* Wait at least 20 us */
Marek Vasuteb2749a2022-04-13 04:15:23 +0200251 } while (dm9000_ior(DM9000_NCR) & 1);
Remy Bohmer2f13d2c2008-06-03 15:26:24 +0200252
Marek Vasuteb2749a2022-04-13 04:15:23 +0200253 dm9000_iow(DM9000_NCR, 0);
254 dm9000_iow(DM9000_NCR, (NCR_LBK_INT_MAC | NCR_RST)); /* Issue a second reset */
Remy Bohmer2f13d2c2008-06-03 15:26:24 +0200255
256 do {
Marek Vasuted761222022-04-13 04:15:24 +0200257 debug("resetting the DM9000, 2nd reset\n");
Remy Bohmer2f13d2c2008-06-03 15:26:24 +0200258 udelay(25); /* Wait at least 20 us */
Marek Vasuteb2749a2022-04-13 04:15:23 +0200259 } while (dm9000_ior(DM9000_NCR) & 1);
Remy Bohmer2f13d2c2008-06-03 15:26:24 +0200260
261 /* Check whether the ethernet controller is present */
Marek Vasuteb2749a2022-04-13 04:15:23 +0200262 if ((dm9000_ior(DM9000_PIDL) != 0x0) ||
263 (dm9000_ior(DM9000_PIDH) != 0x90))
Remy Bohmer2f13d2c2008-06-03 15:26:24 +0200264 printf("ERROR: resetting DM9000 -> not responding\n");
wdenk7ac16102004-08-01 22:48:16 +0000265}
266
Marek Vasuta4aa0482022-04-13 04:15:29 +0200267/* Initialize dm9000 board */
Masahiro Yamadaf7ed78b2020-06-26 15:13:33 +0900268static int dm9000_init(struct eth_device *dev, struct bd_info *bd)
wdenk7ac16102004-08-01 22:48:16 +0000269{
270 int i, oft, lnk;
Remy Bohmer5f63bf42008-06-03 15:26:21 +0200271 u8 io_mode;
272 struct board_info *db = &dm9000_info;
273
wdenk7ac16102004-08-01 22:48:16 +0000274 /* RESET device */
275 dm9000_reset();
Andrew Dyera62f5d42008-08-26 17:03:38 -0500276
277 if (dm9000_probe() < 0)
278 return -1;
wdenk7ac16102004-08-01 22:48:16 +0000279
Remy Bohmer5f63bf42008-06-03 15:26:21 +0200280 /* Auto-detect 8/16/32 bit mode, ISR Bit 6+7 indicate bus width */
Marek Vasuteb2749a2022-04-13 04:15:23 +0200281 io_mode = dm9000_ior(DM9000_ISR) >> 6;
Remy Bohmer5f63bf42008-06-03 15:26:21 +0200282
283 switch (io_mode) {
284 case 0x0: /* 16-bit mode */
285 printf("DM9000: running in 16 bit mode\n");
286 db->outblk = dm9000_outblk_16bit;
287 db->inblk = dm9000_inblk_16bit;
288 db->rx_status = dm9000_rx_status_16bit;
289 break;
290 case 0x01: /* 32-bit mode */
291 printf("DM9000: running in 32 bit mode\n");
292 db->outblk = dm9000_outblk_32bit;
293 db->inblk = dm9000_inblk_32bit;
294 db->rx_status = dm9000_rx_status_32bit;
295 break;
296 case 0x02: /* 8 bit mode */
297 printf("DM9000: running in 8 bit mode\n");
298 db->outblk = dm9000_outblk_8bit;
299 db->inblk = dm9000_inblk_8bit;
300 db->rx_status = dm9000_rx_status_8bit;
301 break;
302 default:
303 /* Assume 8 bit mode, will probably not work anyway */
304 printf("DM9000: Undefined IO-mode:0x%x\n", io_mode);
305 db->outblk = dm9000_outblk_8bit;
306 db->inblk = dm9000_inblk_8bit;
307 db->rx_status = dm9000_rx_status_8bit;
308 break;
309 }
310
Andrew Dyera62f5d42008-08-26 17:03:38 -0500311 /* Program operating register, only internal phy supported */
Marek Vasuteb2749a2022-04-13 04:15:23 +0200312 dm9000_iow(DM9000_NCR, 0x0);
Remy Bohmer61b8dbd2008-06-03 15:26:26 +0200313 /* TX Polling clear */
Marek Vasuteb2749a2022-04-13 04:15:23 +0200314 dm9000_iow(DM9000_TCR, 0);
Remy Bohmer61b8dbd2008-06-03 15:26:26 +0200315 /* Less 3Kb, 200us */
Marek Vasuteb2749a2022-04-13 04:15:23 +0200316 dm9000_iow(DM9000_BPTR, BPTR_BPHW(3) | BPTR_JPT_600US);
Remy Bohmer61b8dbd2008-06-03 15:26:26 +0200317 /* Flow Control : High/Low Water */
Marek Vasuteb2749a2022-04-13 04:15:23 +0200318 dm9000_iow(DM9000_FCTR, FCTR_HWOT(3) | FCTR_LWOT(8));
Remy Bohmer61b8dbd2008-06-03 15:26:26 +0200319 /* SH FIXME: This looks strange! Flow Control */
Marek Vasuteb2749a2022-04-13 04:15:23 +0200320 dm9000_iow(DM9000_FCR, 0x0);
Remy Bohmer61b8dbd2008-06-03 15:26:26 +0200321 /* Special Mode */
Marek Vasuteb2749a2022-04-13 04:15:23 +0200322 dm9000_iow(DM9000_SMCR, 0);
Remy Bohmer61b8dbd2008-06-03 15:26:26 +0200323 /* clear TX status */
Marek Vasuteb2749a2022-04-13 04:15:23 +0200324 dm9000_iow(DM9000_NSR, NSR_WAKEST | NSR_TX2END | NSR_TX1END);
Remy Bohmer61b8dbd2008-06-03 15:26:26 +0200325 /* Clear interrupt status */
Marek Vasuteb2749a2022-04-13 04:15:23 +0200326 dm9000_iow(DM9000_ISR, ISR_ROOS | ISR_ROS | ISR_PTS | ISR_PRS);
wdenk7ac16102004-08-01 22:48:16 +0000327
Ben Warren8707f622009-10-21 21:53:39 -0700328 printf("MAC: %pM\n", dev->enetaddr);
Marek Vasuta4aa0482022-04-13 04:15:29 +0200329 if (!is_valid_ethaddr(dev->enetaddr))
Andrew Ruder1c377d12013-10-22 19:09:02 -0500330 printf("WARNING: Bad MAC address (uninitialized EEPROM?)\n");
Andrew Dyera62f5d42008-08-26 17:03:38 -0500331
332 /* fill device MAC address registers */
333 for (i = 0, oft = DM9000_PAR; i < 6; i++, oft++)
Marek Vasuteb2749a2022-04-13 04:15:23 +0200334 dm9000_iow(oft, dev->enetaddr[i]);
wdenk7ac16102004-08-01 22:48:16 +0000335 for (i = 0, oft = 0x16; i < 8; i++, oft++)
Marek Vasuteb2749a2022-04-13 04:15:23 +0200336 dm9000_iow(oft, 0xff);
wdenk7ac16102004-08-01 22:48:16 +0000337
338 /* read back mac, just to be sure */
339 for (i = 0, oft = 0x10; i < 6; i++, oft++)
Marek Vasuted761222022-04-13 04:15:24 +0200340 debug("%02x:", dm9000_ior(oft));
341 debug("\n");
wdenk7ac16102004-08-01 22:48:16 +0000342
343 /* Activate DM9000 */
Remy Bohmer61b8dbd2008-06-03 15:26:26 +0200344 /* RX enable */
Marek Vasuteb2749a2022-04-13 04:15:23 +0200345 dm9000_iow(DM9000_RCR, RCR_DIS_LONG | RCR_DIS_CRC | RCR_RXEN);
Remy Bohmer61b8dbd2008-06-03 15:26:26 +0200346 /* Enable TX/RX interrupt mask */
Marek Vasuteb2749a2022-04-13 04:15:23 +0200347 dm9000_iow(DM9000_IMR, IMR_PAR);
Remy Bohmer61b8dbd2008-06-03 15:26:26 +0200348
wdenk7ac16102004-08-01 22:48:16 +0000349 i = 0;
Andy Fleming0d2df962011-03-22 22:49:13 -0500350 while (!(dm9000_phy_read(1) & 0x20)) { /* autonegation complete bit */
wdenk7ac16102004-08-01 22:48:16 +0000351 udelay(1000);
352 i++;
353 if (i == 10000) {
354 printf("could not establish link\n");
355 return 0;
356 }
357 }
358
359 /* see what we've got */
Andy Fleming0d2df962011-03-22 22:49:13 -0500360 lnk = dm9000_phy_read(17) >> 12;
wdenk7ac16102004-08-01 22:48:16 +0000361 printf("operating at ");
362 switch (lnk) {
363 case 1:
364 printf("10M half duplex ");
365 break;
366 case 2:
367 printf("10M full duplex ");
368 break;
369 case 4:
370 printf("100M half duplex ");
371 break;
372 case 8:
373 printf("100M full duplex ");
374 break;
375 default:
376 printf("unknown: %d ", lnk);
377 break;
378 }
379 printf("mode\n");
380 return 0;
381}
382
383/*
Marek Vasuta4aa0482022-04-13 04:15:29 +0200384 * Hardware start transmission.
385 * Send a packet to media from the upper layer.
386 */
Joe Hershbergerab1ac412012-05-21 14:45:23 +0000387static int dm9000_send(struct eth_device *netdev, void *packet, int length)
wdenk7ac16102004-08-01 22:48:16 +0000388{
wdenk7ac16102004-08-01 22:48:16 +0000389 int tmo;
Remy Bohmer5f63bf42008-06-03 15:26:21 +0200390 struct board_info *db = &dm9000_info;
391
Marek Vasuta4aa0482022-04-13 04:15:29 +0200392 dm9000_dump_packet(__func__, packet, length);
wdenk7ac16102004-08-01 22:48:16 +0000393
Marek Vasuteb2749a2022-04-13 04:15:23 +0200394 dm9000_iow(DM9000_ISR, IMR_PTM); /* Clear Tx bit in ISR */
Remy Bohmer16cb2642008-06-03 15:26:23 +0200395
wdenk7ac16102004-08-01 22:48:16 +0000396 /* Move data to DM9000 TX RAM */
Marek Vasuteb2749a2022-04-13 04:15:23 +0200397 dm9000_outb(DM9000_MWCMD, DM9000_IO); /* Prepare for TX-data */
wdenk7ac16102004-08-01 22:48:16 +0000398
Remy Bohmer5f63bf42008-06-03 15:26:21 +0200399 /* push the data to the TX-fifo */
Marek Vasuta4aa0482022-04-13 04:15:29 +0200400 db->outblk(packet, length);
wdenk7ac16102004-08-01 22:48:16 +0000401
402 /* Set TX length to DM9000 */
Marek Vasuteb2749a2022-04-13 04:15:23 +0200403 dm9000_iow(DM9000_TXPLL, length & 0xff);
404 dm9000_iow(DM9000_TXPLH, (length >> 8) & 0xff);
wdenk7ac16102004-08-01 22:48:16 +0000405
406 /* Issue TX polling command */
Marek Vasuteb2749a2022-04-13 04:15:23 +0200407 dm9000_iow(DM9000_TCR, TCR_TXREQ); /* Cleared after TX complete */
wdenk7ac16102004-08-01 22:48:16 +0000408
409 /* wait for end of transmission */
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200410 tmo = get_timer(0) + 5 * CONFIG_SYS_HZ;
Marek Vasuta4aa0482022-04-13 04:15:29 +0200411 while (!(dm9000_ior(DM9000_NSR) & (NSR_TX1END | NSR_TX2END)) ||
412 !(dm9000_ior(DM9000_ISR) & IMR_PTM)) {
wdenk7ac16102004-08-01 22:48:16 +0000413 if (get_timer(0) >= tmo) {
414 printf("transmission timeout\n");
415 break;
416 }
417 }
Marek Vasuteb2749a2022-04-13 04:15:23 +0200418 dm9000_iow(DM9000_ISR, IMR_PTM); /* Clear Tx bit in ISR */
Remy Bohmer16cb2642008-06-03 15:26:23 +0200419
Marek Vasuted761222022-04-13 04:15:24 +0200420 debug("transmit done\n\n");
wdenk7ac16102004-08-01 22:48:16 +0000421 return 0;
422}
423
424/*
Marek Vasuta4aa0482022-04-13 04:15:29 +0200425 * Stop the interface.
426 * The interface is stopped when it is brought.
427 */
Remy Bohmer7eefd922009-05-02 21:49:18 +0200428static void dm9000_halt(struct eth_device *netdev)
wdenk7ac16102004-08-01 22:48:16 +0000429{
Marek Vasuta4aa0482022-04-13 04:15:29 +0200430 /* RESET device */
Andy Fleming0d2df962011-03-22 22:49:13 -0500431 dm9000_phy_write(0, 0x8000); /* PHY RESET */
Marek Vasuteb2749a2022-04-13 04:15:23 +0200432 dm9000_iow(DM9000_GPR, 0x01); /* Power-Down PHY */
433 dm9000_iow(DM9000_IMR, 0x80); /* Disable all interrupt */
434 dm9000_iow(DM9000_RCR, 0x00); /* Disable RX */
wdenk7ac16102004-08-01 22:48:16 +0000435}
436
437/*
Marek Vasuta4aa0482022-04-13 04:15:29 +0200438 * Received a packet and pass to upper layer
439 */
Remy Bohmer7eefd922009-05-02 21:49:18 +0200440static int dm9000_rx(struct eth_device *netdev)
wdenk7ac16102004-08-01 22:48:16 +0000441{
Joe Hershberger9f09a362015-04-08 01:41:06 -0500442 u8 rxbyte;
443 u8 *rdptr = (u8 *)net_rx_packets[0];
Marek Vasut5248e562022-04-13 04:15:25 +0200444 u16 rxstatus, rxlen = 0;
Remy Bohmer5f63bf42008-06-03 15:26:21 +0200445 struct board_info *db = &dm9000_info;
wdenk7ac16102004-08-01 22:48:16 +0000446
Marek Vasuta4aa0482022-04-13 04:15:29 +0200447 /*
448 * Check packet ready or not, we must check
449 * the ISR status first for DM9000A
450 */
Marek Vasuteb2749a2022-04-13 04:15:23 +0200451 if (!(dm9000_ior(DM9000_ISR) & 0x01)) /* Rx-ISR bit must be set. */
wdenk7ac16102004-08-01 22:48:16 +0000452 return 0;
453
Marek Vasuteb2749a2022-04-13 04:15:23 +0200454 dm9000_iow(DM9000_ISR, 0x01); /* clear PR status latched in bit 0 */
wdenk7ac16102004-08-01 22:48:16 +0000455
Remy Bohmereec38a12008-06-03 15:26:25 +0200456 /* There is _at least_ 1 package in the fifo, read them all */
457 for (;;) {
Marek Vasuteb2749a2022-04-13 04:15:23 +0200458 dm9000_ior(DM9000_MRCMDX); /* Dummy read */
wdenk7ac16102004-08-01 22:48:16 +0000459
Marek Vasuta4aa0482022-04-13 04:15:29 +0200460 /*
461 * Get most updated data,
462 * only look at bits 0:1, See application notes DM9000
463 */
Marek Vasuteb2749a2022-04-13 04:15:23 +0200464 rxbyte = dm9000_inb(DM9000_DATA) & 0x03;
wdenk7ac16102004-08-01 22:48:16 +0000465
Remy Bohmereec38a12008-06-03 15:26:25 +0200466 /* Status check: this byte must be 0 or 1 */
467 if (rxbyte > DM9000_PKT_RDY) {
Marek Vasuteb2749a2022-04-13 04:15:23 +0200468 dm9000_iow(DM9000_RCR, 0x00); /* Stop Device */
469 dm9000_iow(DM9000_ISR, 0x80); /* Stop INT request */
Remy Bohmereec38a12008-06-03 15:26:25 +0200470 printf("DM9000 error: status check fail: 0x%x\n",
Marek Vasuta4aa0482022-04-13 04:15:29 +0200471 rxbyte);
Remy Bohmereec38a12008-06-03 15:26:25 +0200472 return 0;
473 }
wdenk7ac16102004-08-01 22:48:16 +0000474
Remy Bohmereec38a12008-06-03 15:26:25 +0200475 if (rxbyte != DM9000_PKT_RDY)
476 return 0; /* No packet received, ignore */
wdenk7ac16102004-08-01 22:48:16 +0000477
Marek Vasuted761222022-04-13 04:15:24 +0200478 debug("receiving packet\n");
Remy Bohmereec38a12008-06-03 15:26:25 +0200479
480 /* A packet ready now & Get status/length */
Marek Vasuta4aa0482022-04-13 04:15:29 +0200481 db->rx_status(&rxstatus, &rxlen);
wdenk7ac16102004-08-01 22:48:16 +0000482
Marek Vasut5248e562022-04-13 04:15:25 +0200483 debug("rx status: 0x%04x rx len: %d\n", rxstatus, rxlen);
Remy Bohmereec38a12008-06-03 15:26:25 +0200484
485 /* Move data from DM9000 */
486 /* Read received packet from RX SRAM */
Marek Vasuta4aa0482022-04-13 04:15:29 +0200487 db->inblk(rdptr, rxlen);
Remy Bohmereec38a12008-06-03 15:26:25 +0200488
Marek Vasuta4aa0482022-04-13 04:15:29 +0200489 if (rxstatus & 0xbf00 || rxlen < 0x40 ||
490 rxlen > DM9000_PKT_MAX) {
491 if (rxstatus & 0x100)
Remy Bohmereec38a12008-06-03 15:26:25 +0200492 printf("rx fifo error\n");
Marek Vasuta4aa0482022-04-13 04:15:29 +0200493 if (rxstatus & 0x200)
Remy Bohmereec38a12008-06-03 15:26:25 +0200494 printf("rx crc error\n");
Marek Vasuta4aa0482022-04-13 04:15:29 +0200495 if (rxstatus & 0x8000)
Remy Bohmereec38a12008-06-03 15:26:25 +0200496 printf("rx length error\n");
Marek Vasut5248e562022-04-13 04:15:25 +0200497 if (rxlen > DM9000_PKT_MAX) {
Remy Bohmereec38a12008-06-03 15:26:25 +0200498 printf("rx length too big\n");
499 dm9000_reset();
500 }
501 } else {
Marek Vasuta4aa0482022-04-13 04:15:29 +0200502 dm9000_dump_packet(__func__, rdptr, rxlen);
Remy Bohmereec38a12008-06-03 15:26:25 +0200503
Marek Vasuted761222022-04-13 04:15:24 +0200504 debug("passing packet to upper layer\n");
Marek Vasut5248e562022-04-13 04:15:25 +0200505 net_process_received_packet(net_rx_packets[0], rxlen);
Remy Bohmereec38a12008-06-03 15:26:25 +0200506 }
wdenk7ac16102004-08-01 22:48:16 +0000507 }
508 return 0;
509}
510
511/*
Marek Vasuta4aa0482022-04-13 04:15:29 +0200512 * Read a word data from SROM
513 */
Remy Bohmercd9a36c2009-05-03 12:11:40 +0200514#if !defined(CONFIG_DM9000_NO_SROM)
515void dm9000_read_srom_word(int offset, u8 *to)
wdenk7ac16102004-08-01 22:48:16 +0000516{
Marek Vasuteb2749a2022-04-13 04:15:23 +0200517 dm9000_iow(DM9000_EPAR, offset);
518 dm9000_iow(DM9000_EPCR, 0x4);
Marek Vasuta4aa0482022-04-13 04:15:29 +0200519 mdelay(8);
Marek Vasuteb2749a2022-04-13 04:15:23 +0200520 dm9000_iow(DM9000_EPCR, 0x0);
521 to[0] = dm9000_ior(DM9000_EPDRL);
522 to[1] = dm9000_ior(DM9000_EPDRH);
wdenk7ac16102004-08-01 22:48:16 +0000523}
524
Remy Bohmercd9a36c2009-05-03 12:11:40 +0200525void dm9000_write_srom_word(int offset, u16 val)
stefano babic6708a602007-08-30 23:01:49 +0200526{
Marek Vasuteb2749a2022-04-13 04:15:23 +0200527 dm9000_iow(DM9000_EPAR, offset);
528 dm9000_iow(DM9000_EPDRH, ((val >> 8) & 0xff));
529 dm9000_iow(DM9000_EPDRL, (val & 0xff));
530 dm9000_iow(DM9000_EPCR, 0x12);
Marek Vasuta4aa0482022-04-13 04:15:29 +0200531 mdelay(8);
Marek Vasuteb2749a2022-04-13 04:15:23 +0200532 dm9000_iow(DM9000_EPCR, 0);
stefano babic6708a602007-08-30 23:01:49 +0200533}
Ben Warren8707f622009-10-21 21:53:39 -0700534
535static void dm9000_get_enetaddr(struct eth_device *dev)
536{
Ben Warren8707f622009-10-21 21:53:39 -0700537 int i;
Marek Vasuta4aa0482022-04-13 04:15:29 +0200538
Ben Warren8707f622009-10-21 21:53:39 -0700539 for (i = 0; i < 3; i++)
540 dm9000_read_srom_word(i, dev->enetaddr + (2 * i));
Ben Warren8707f622009-10-21 21:53:39 -0700541}
Marek Vasuta4aa0482022-04-13 04:15:29 +0200542#else
543static void dm9000_get_enetaddr(struct eth_device *dev) {}
544#endif
stefano babic6708a602007-08-30 23:01:49 +0200545
wdenk7ac16102004-08-01 22:48:16 +0000546/*
Marek Vasuta4aa0482022-04-13 04:15:29 +0200547 * Read a byte from I/O port
548 */
wdenk7ac16102004-08-01 22:48:16 +0000549static u8
Marek Vasuteb2749a2022-04-13 04:15:23 +0200550dm9000_ior(int reg)
wdenk7ac16102004-08-01 22:48:16 +0000551{
Marek Vasuteb2749a2022-04-13 04:15:23 +0200552 dm9000_outb(reg, DM9000_IO);
553 return dm9000_inb(DM9000_DATA);
wdenk7ac16102004-08-01 22:48:16 +0000554}
555
556/*
Marek Vasuta4aa0482022-04-13 04:15:29 +0200557 * Write a byte to I/O port
558 */
wdenk7ac16102004-08-01 22:48:16 +0000559static void
Marek Vasuteb2749a2022-04-13 04:15:23 +0200560dm9000_iow(int reg, u8 value)
wdenk7ac16102004-08-01 22:48:16 +0000561{
Marek Vasuteb2749a2022-04-13 04:15:23 +0200562 dm9000_outb(reg, DM9000_IO);
563 dm9000_outb(value, DM9000_DATA);
wdenk7ac16102004-08-01 22:48:16 +0000564}
565
566/*
Marek Vasuta4aa0482022-04-13 04:15:29 +0200567 * Read a word from phyxcer
568 */
wdenk7ac16102004-08-01 22:48:16 +0000569static u16
Andy Fleming0d2df962011-03-22 22:49:13 -0500570dm9000_phy_read(int reg)
wdenk7ac16102004-08-01 22:48:16 +0000571{
572 u16 val;
573
574 /* Fill the phyxcer register into REG_0C */
Marek Vasuteb2749a2022-04-13 04:15:23 +0200575 dm9000_iow(DM9000_EPAR, DM9000_PHY | reg);
576 dm9000_iow(DM9000_EPCR, 0xc); /* Issue phyxcer read command */
Remy Bohmer61b8dbd2008-06-03 15:26:26 +0200577 udelay(100); /* Wait read complete */
Marek Vasuteb2749a2022-04-13 04:15:23 +0200578 dm9000_iow(DM9000_EPCR, 0x0); /* Clear phyxcer read command */
579 val = (dm9000_ior(DM9000_EPDRH) << 8) | dm9000_ior(DM9000_EPDRL);
wdenk7ac16102004-08-01 22:48:16 +0000580
581 /* The read data keeps on REG_0D & REG_0E */
Marek Vasuta4aa0482022-04-13 04:15:29 +0200582 debug("%s(0x%x): 0x%x\n", __func__, reg, val);
wdenk7ac16102004-08-01 22:48:16 +0000583 return val;
584}
585
586/*
Marek Vasuta4aa0482022-04-13 04:15:29 +0200587 * Write a word to phyxcer
588 */
wdenk7ac16102004-08-01 22:48:16 +0000589static void
Andy Fleming0d2df962011-03-22 22:49:13 -0500590dm9000_phy_write(int reg, u16 value)
wdenk7ac16102004-08-01 22:48:16 +0000591{
wdenk7ac16102004-08-01 22:48:16 +0000592 /* Fill the phyxcer register into REG_0C */
Marek Vasuteb2749a2022-04-13 04:15:23 +0200593 dm9000_iow(DM9000_EPAR, DM9000_PHY | reg);
wdenk7ac16102004-08-01 22:48:16 +0000594
595 /* Fill the written data into REG_0D & REG_0E */
Marek Vasuteb2749a2022-04-13 04:15:23 +0200596 dm9000_iow(DM9000_EPDRL, (value & 0xff));
597 dm9000_iow(DM9000_EPDRH, ((value >> 8) & 0xff));
598 dm9000_iow(DM9000_EPCR, 0xa); /* Issue phyxcer write command */
Remy Bohmer61b8dbd2008-06-03 15:26:26 +0200599 udelay(500); /* Wait write complete */
Marek Vasuteb2749a2022-04-13 04:15:23 +0200600 dm9000_iow(DM9000_EPCR, 0x0); /* Clear phyxcer write command */
Marek Vasuta4aa0482022-04-13 04:15:29 +0200601 debug("%s(reg:0x%x, value:0x%x)\n", __func__, reg, value);
wdenk7ac16102004-08-01 22:48:16 +0000602}
Remy Bohmer7eefd922009-05-02 21:49:18 +0200603
Masahiro Yamadaf7ed78b2020-06-26 15:13:33 +0900604int dm9000_initialize(struct bd_info *bis)
Remy Bohmer7eefd922009-05-02 21:49:18 +0200605{
Marek Vasuta4aa0482022-04-13 04:15:29 +0200606 struct eth_device *dev = &dm9000_info.netdev;
Remy Bohmer7eefd922009-05-02 21:49:18 +0200607
Ben Warren8707f622009-10-21 21:53:39 -0700608 /* Load MAC address from EEPROM */
609 dm9000_get_enetaddr(dev);
610
Remy Bohmer7eefd922009-05-02 21:49:18 +0200611 dev->init = dm9000_init;
612 dev->halt = dm9000_halt;
613 dev->send = dm9000_send;
614 dev->recv = dm9000_rx;
Ben Whitten34fd6c92015-12-30 13:05:58 +0000615 strcpy(dev->name, "dm9000");
Remy Bohmer7eefd922009-05-02 21:49:18 +0200616
617 eth_register(dev);
618
619 return 0;
620}