blob: 9e17f0b9c2809113e09b8e6e43fce99394c57a16 [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
wdenk7ac16102004-08-01 22:48:16 +000052#include <command.h>
Marek Vasut16c3e212022-04-13 04:15:37 +020053#include <dm.h>
Marek Vasut9c5e9ca2022-04-13 04:15:32 +020054#include <malloc.h>
wdenk7ac16102004-08-01 22:48:16 +000055#include <net.h>
56#include <asm/io.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 Vasut09a8a9c2022-04-13 04:15:31 +020062struct dm9000_priv {
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 Vasut1b5dd322022-04-13 04:15:34 +020074 void (*outblk)(struct dm9000_priv *db, void *data_ptr, int count);
75 void (*inblk)(struct dm9000_priv *db, void *data_ptr, int count);
76 void (*rx_status)(struct dm9000_priv *db, u16 *rxstatus, u16 *rxlen);
Marek Vasut1b5dd322022-04-13 04:15:34 +020077 void __iomem *base_io;
78 void __iomem *base_data;
Marek Vasuta4aa0482022-04-13 04:15:29 +020079};
wdenk7ac16102004-08-01 22:48:16 +000080
wdenk7ac16102004-08-01 22:48:16 +000081/* DM9000 network board routine ---------------------------- */
Jason Jinc74c4362011-08-25 15:46:43 +080082#ifndef CONFIG_DM9000_BYTE_SWAPPED
Marek Vasuta4aa0482022-04-13 04:15:29 +020083#define dm9000_outb(d, r) writeb((d), (r))
84#define dm9000_outw(d, r) writew((d), (r))
85#define dm9000_outl(d, r) writel((d), (r))
Marek Vasut7823b232022-04-13 04:15:28 +020086#define dm9000_inb(r) readb(r)
87#define dm9000_inw(r) readw(r)
88#define dm9000_inl(r) readl(r)
Jason Jinc74c4362011-08-25 15:46:43 +080089#else
Marek Vasuteb2749a2022-04-13 04:15:23 +020090#define dm9000_outb(d, r) __raw_writeb(d, r)
91#define dm9000_outw(d, r) __raw_writew(d, r)
92#define dm9000_outl(d, r) __raw_writel(d, r)
93#define dm9000_inb(r) __raw_readb(r)
94#define dm9000_inw(r) __raw_readw(r)
95#define dm9000_inl(r) __raw_readl(r)
Jason Jinc74c4362011-08-25 15:46:43 +080096#endif
wdenk7ac16102004-08-01 22:48:16 +000097
Marek Vasut52006d22022-04-13 04:15:27 +020098#ifdef DEBUG
99static void dm9000_dump_packet(const char *func, u8 *packet, int length)
100{
101 int i;
102
103 printf("%s: length: %d\n", func, length);
104
105 for (i = 0; i < length; i++) {
106 if (i % 8 == 0)
107 printf("\n%s: %02x: ", func, i);
108 printf("%02x ", packet[i]);
109 }
110
111 printf("\n");
112}
113#else
114static void dm9000_dump_packet(const char *func, u8 *packet, int length) {}
115#endif
116
Marek Vasut1b5dd322022-04-13 04:15:34 +0200117static void dm9000_outblk_8bit(struct dm9000_priv *db, void *data_ptr, int count)
Remy Bohmer5f63bf42008-06-03 15:26:21 +0200118{
119 int i;
Marek Vasuta4aa0482022-04-13 04:15:29 +0200120
Remy Bohmer5f63bf42008-06-03 15:26:21 +0200121 for (i = 0; i < count; i++)
Marek Vasut1b5dd322022-04-13 04:15:34 +0200122 dm9000_outb((((u8 *)data_ptr)[i] & 0xff), db->base_data);
Remy Bohmer5f63bf42008-06-03 15:26:21 +0200123}
124
Marek Vasut1b5dd322022-04-13 04:15:34 +0200125static void dm9000_outblk_16bit(struct dm9000_priv *db, void *data_ptr, int count)
Remy Bohmer5f63bf42008-06-03 15:26:21 +0200126{
127 int i;
128 u32 tmplen = (count + 1) / 2;
129
130 for (i = 0; i < tmplen; i++)
Marek Vasut1b5dd322022-04-13 04:15:34 +0200131 dm9000_outw(((u16 *)data_ptr)[i], db->base_data);
Remy Bohmer5f63bf42008-06-03 15:26:21 +0200132}
Marek Vasuta4aa0482022-04-13 04:15:29 +0200133
Marek Vasut1b5dd322022-04-13 04:15:34 +0200134static void dm9000_outblk_32bit(struct dm9000_priv *db, void *data_ptr, int count)
Remy Bohmer5f63bf42008-06-03 15:26:21 +0200135{
136 int i;
137 u32 tmplen = (count + 3) / 4;
138
139 for (i = 0; i < tmplen; i++)
Marek Vasut1b5dd322022-04-13 04:15:34 +0200140 dm9000_outl(((u32 *)data_ptr)[i], db->base_data);
Remy Bohmer5f63bf42008-06-03 15:26:21 +0200141}
142
Marek Vasut1b5dd322022-04-13 04:15:34 +0200143static void dm9000_inblk_8bit(struct dm9000_priv *db, void *data_ptr, int count)
Remy Bohmer5f63bf42008-06-03 15:26:21 +0200144{
145 int i;
Marek Vasuta4aa0482022-04-13 04:15:29 +0200146
Remy Bohmer5f63bf42008-06-03 15:26:21 +0200147 for (i = 0; i < count; i++)
Marek Vasut1b5dd322022-04-13 04:15:34 +0200148 ((u8 *)data_ptr)[i] = dm9000_inb(db->base_data);
Remy Bohmer5f63bf42008-06-03 15:26:21 +0200149}
150
Marek Vasut1b5dd322022-04-13 04:15:34 +0200151static void dm9000_inblk_16bit(struct dm9000_priv *db, void *data_ptr, int count)
Remy Bohmer5f63bf42008-06-03 15:26:21 +0200152{
153 int i;
154 u32 tmplen = (count + 1) / 2;
155
156 for (i = 0; i < tmplen; i++)
Marek Vasut1b5dd322022-04-13 04:15:34 +0200157 ((u16 *)data_ptr)[i] = dm9000_inw(db->base_data);
Remy Bohmer5f63bf42008-06-03 15:26:21 +0200158}
Marek Vasuta4aa0482022-04-13 04:15:29 +0200159
Marek Vasut1b5dd322022-04-13 04:15:34 +0200160static void dm9000_inblk_32bit(struct dm9000_priv *db, void *data_ptr, int count)
Remy Bohmer5f63bf42008-06-03 15:26:21 +0200161{
162 int i;
163 u32 tmplen = (count + 3) / 4;
164
165 for (i = 0; i < tmplen; i++)
Marek Vasut1b5dd322022-04-13 04:15:34 +0200166 ((u32 *)data_ptr)[i] = dm9000_inl(db->base_data);
Remy Bohmer5f63bf42008-06-03 15:26:21 +0200167}
168
Marek Vasut1b5dd322022-04-13 04:15:34 +0200169static void dm9000_rx_status_32bit(struct dm9000_priv *db, u16 *rxstatus, u16 *rxlen)
Remy Bohmer5f63bf42008-06-03 15:26:21 +0200170{
Remy Bohmer2e1604f2008-06-04 10:47:25 +0200171 u32 tmpdata;
Remy Bohmer5f63bf42008-06-03 15:26:21 +0200172
Marek Vasut1b5dd322022-04-13 04:15:34 +0200173 dm9000_outb(DM9000_MRCMD, db->base_io);
Remy Bohmer5f63bf42008-06-03 15:26:21 +0200174
Marek Vasut1b5dd322022-04-13 04:15:34 +0200175 tmpdata = dm9000_inl(db->base_data);
Marek Vasut5248e562022-04-13 04:15:25 +0200176 *rxstatus = __le16_to_cpu(tmpdata);
177 *rxlen = __le16_to_cpu(tmpdata >> 16);
Remy Bohmer5f63bf42008-06-03 15:26:21 +0200178}
179
Marek Vasut1b5dd322022-04-13 04:15:34 +0200180static void dm9000_rx_status_16bit(struct dm9000_priv *db, u16 *rxstatus, u16 *rxlen)
Remy Bohmer5f63bf42008-06-03 15:26:21 +0200181{
Marek Vasut1b5dd322022-04-13 04:15:34 +0200182 dm9000_outb(DM9000_MRCMD, db->base_io);
Remy Bohmer5f63bf42008-06-03 15:26:21 +0200183
Marek Vasut1b5dd322022-04-13 04:15:34 +0200184 *rxstatus = __le16_to_cpu(dm9000_inw(db->base_data));
185 *rxlen = __le16_to_cpu(dm9000_inw(db->base_data));
Remy Bohmer5f63bf42008-06-03 15:26:21 +0200186}
187
Marek Vasut1b5dd322022-04-13 04:15:34 +0200188static void dm9000_rx_status_8bit(struct dm9000_priv *db, u16 *rxstatus, u16 *rxlen)
Remy Bohmer5f63bf42008-06-03 15:26:21 +0200189{
Marek Vasut1b5dd322022-04-13 04:15:34 +0200190 dm9000_outb(DM9000_MRCMD, db->base_io);
Remy Bohmer5f63bf42008-06-03 15:26:21 +0200191
Marek Vasut5248e562022-04-13 04:15:25 +0200192 *rxstatus =
Marek Vasut1b5dd322022-04-13 04:15:34 +0200193 __le16_to_cpu(dm9000_inb(db->base_data) +
194 (dm9000_inb(db->base_data) << 8));
Marek Vasut5248e562022-04-13 04:15:25 +0200195 *rxlen =
Marek Vasut1b5dd322022-04-13 04:15:34 +0200196 __le16_to_cpu(dm9000_inb(db->base_data) +
197 (dm9000_inb(db->base_data) << 8));
Remy Bohmer5f63bf42008-06-03 15:26:21 +0200198}
wdenk7ac16102004-08-01 22:48:16 +0000199
200/*
Marek Vasutc5e9d642022-04-13 04:15:30 +0200201 * Read a byte from I/O port
202 */
Marek Vasut1b5dd322022-04-13 04:15:34 +0200203static u8 dm9000_ior(struct dm9000_priv *db, int reg)
Marek Vasutc5e9d642022-04-13 04:15:30 +0200204{
Marek Vasut1b5dd322022-04-13 04:15:34 +0200205 dm9000_outb(reg, db->base_io);
206 return dm9000_inb(db->base_data);
Marek Vasutc5e9d642022-04-13 04:15:30 +0200207}
208
209/*
210 * Write a byte to I/O port
211 */
Marek Vasut1b5dd322022-04-13 04:15:34 +0200212static void dm9000_iow(struct dm9000_priv *db, int reg, u8 value)
Marek Vasutc5e9d642022-04-13 04:15:30 +0200213{
Marek Vasut1b5dd322022-04-13 04:15:34 +0200214 dm9000_outb(reg, db->base_io);
215 dm9000_outb(value, db->base_data);
Marek Vasutc5e9d642022-04-13 04:15:30 +0200216}
217
218/*
219 * Read a word from phyxcer
220 */
Marek Vasut1b5dd322022-04-13 04:15:34 +0200221static u16 dm9000_phy_read(struct dm9000_priv *db, int reg)
Marek Vasutc5e9d642022-04-13 04:15:30 +0200222{
223 u16 val;
224
225 /* Fill the phyxcer register into REG_0C */
Marek Vasut1b5dd322022-04-13 04:15:34 +0200226 dm9000_iow(db, DM9000_EPAR, DM9000_PHY | reg);
227 dm9000_iow(db, DM9000_EPCR, 0xc); /* Issue phyxcer read command */
Marek Vasutc5e9d642022-04-13 04:15:30 +0200228 udelay(100); /* Wait read complete */
Marek Vasut1b5dd322022-04-13 04:15:34 +0200229 dm9000_iow(db, DM9000_EPCR, 0x0); /* Clear phyxcer read command */
230 val = (dm9000_ior(db, DM9000_EPDRH) << 8) |
231 dm9000_ior(db, DM9000_EPDRL);
Marek Vasutc5e9d642022-04-13 04:15:30 +0200232
233 /* The read data keeps on REG_0D & REG_0E */
234 debug("%s(0x%x): 0x%x\n", __func__, reg, val);
235 return val;
236}
237
238/*
239 * Write a word to phyxcer
240 */
Marek Vasut1b5dd322022-04-13 04:15:34 +0200241static void dm9000_phy_write(struct dm9000_priv *db, int reg, u16 value)
Marek Vasutc5e9d642022-04-13 04:15:30 +0200242{
243 /* Fill the phyxcer register into REG_0C */
Marek Vasut1b5dd322022-04-13 04:15:34 +0200244 dm9000_iow(db, DM9000_EPAR, DM9000_PHY | reg);
Marek Vasutc5e9d642022-04-13 04:15:30 +0200245
246 /* Fill the written data into REG_0D & REG_0E */
Marek Vasut1b5dd322022-04-13 04:15:34 +0200247 dm9000_iow(db, DM9000_EPDRL, (value & 0xff));
248 dm9000_iow(db, DM9000_EPDRH, ((value >> 8) & 0xff));
249 dm9000_iow(db, DM9000_EPCR, 0xa); /* Issue phyxcer write command */
Marek Vasutc5e9d642022-04-13 04:15:30 +0200250 udelay(500); /* Wait write complete */
Marek Vasut1b5dd322022-04-13 04:15:34 +0200251 dm9000_iow(db, DM9000_EPCR, 0x0); /* Clear phyxcer write command */
Marek Vasutc5e9d642022-04-13 04:15:30 +0200252 debug("%s(reg:0x%x, value:0x%x)\n", __func__, reg, value);
253}
254
255/*
Marek Vasuta4aa0482022-04-13 04:15:29 +0200256 * Search DM9000 board, allocate space and register it
257 */
Marek Vasut1b5dd322022-04-13 04:15:34 +0200258static int dm9000_probe(struct dm9000_priv *db)
wdenk7ac16102004-08-01 22:48:16 +0000259{
260 u32 id_val;
Marek Vasuta4aa0482022-04-13 04:15:29 +0200261
Marek Vasut1b5dd322022-04-13 04:15:34 +0200262 id_val = dm9000_ior(db, DM9000_VIDL);
263 id_val |= dm9000_ior(db, DM9000_VIDH) << 8;
264 id_val |= dm9000_ior(db, DM9000_PIDL) << 16;
265 id_val |= dm9000_ior(db, DM9000_PIDH) << 24;
Marek Vasuta4aa0482022-04-13 04:15:29 +0200266 if (id_val != DM9000_ID) {
Marek Vasut1b5dd322022-04-13 04:15:34 +0200267 printf("dm9000 not found at 0x%p id: 0x%08x\n",
268 db->base_io, id_val);
wdenk7ac16102004-08-01 22:48:16 +0000269 return -1;
270 }
Marek Vasuta4aa0482022-04-13 04:15:29 +0200271
Marek Vasut1b5dd322022-04-13 04:15:34 +0200272 printf("dm9000 i/o: 0x%p, id: 0x%x\n", db->base_io, id_val);
Marek Vasuta4aa0482022-04-13 04:15:29 +0200273 return 0;
wdenk7ac16102004-08-01 22:48:16 +0000274}
275
276/* General Purpose dm9000 reset routine */
Marek Vasut1b5dd322022-04-13 04:15:34 +0200277static void dm9000_reset(struct dm9000_priv *db)
wdenk7ac16102004-08-01 22:48:16 +0000278{
Marek Vasuted761222022-04-13 04:15:24 +0200279 debug("resetting DM9000\n");
Remy Bohmer2f13d2c2008-06-03 15:26:24 +0200280
Marek Vasuta4aa0482022-04-13 04:15:29 +0200281 /*
282 * Reset DM9000,
283 * see DM9000 Application Notes V1.22 Jun 11, 2004 page 29
284 */
Remy Bohmer2f13d2c2008-06-03 15:26:24 +0200285
Andrew Dyera62f5d42008-08-26 17:03:38 -0500286 /* DEBUG: Make all GPIO0 outputs, all others inputs */
Marek Vasut1b5dd322022-04-13 04:15:34 +0200287 dm9000_iow(db, DM9000_GPCR, GPCR_GPIO0_OUT);
Remy Bohmer2f13d2c2008-06-03 15:26:24 +0200288 /* Step 1: Power internal PHY by writing 0 to GPIO0 pin */
Marek Vasut1b5dd322022-04-13 04:15:34 +0200289 dm9000_iow(db, DM9000_GPR, 0);
Remy Bohmer2f13d2c2008-06-03 15:26:24 +0200290 /* Step 2: Software reset */
Marek Vasut1b5dd322022-04-13 04:15:34 +0200291 dm9000_iow(db, DM9000_NCR, (NCR_LBK_INT_MAC | NCR_RST));
Remy Bohmer2f13d2c2008-06-03 15:26:24 +0200292
293 do {
Marek Vasuted761222022-04-13 04:15:24 +0200294 debug("resetting the DM9000, 1st reset\n");
Remy Bohmer2f13d2c2008-06-03 15:26:24 +0200295 udelay(25); /* Wait at least 20 us */
Marek Vasut1b5dd322022-04-13 04:15:34 +0200296 } while (dm9000_ior(db, DM9000_NCR) & 1);
Remy Bohmer2f13d2c2008-06-03 15:26:24 +0200297
Marek Vasut1b5dd322022-04-13 04:15:34 +0200298 dm9000_iow(db, DM9000_NCR, 0);
299 dm9000_iow(db, DM9000_NCR, (NCR_LBK_INT_MAC | NCR_RST)); /* Issue a second reset */
Remy Bohmer2f13d2c2008-06-03 15:26:24 +0200300
301 do {
Marek Vasuted761222022-04-13 04:15:24 +0200302 debug("resetting the DM9000, 2nd reset\n");
Remy Bohmer2f13d2c2008-06-03 15:26:24 +0200303 udelay(25); /* Wait at least 20 us */
Marek Vasut1b5dd322022-04-13 04:15:34 +0200304 } while (dm9000_ior(db, DM9000_NCR) & 1);
Remy Bohmer2f13d2c2008-06-03 15:26:24 +0200305
306 /* Check whether the ethernet controller is present */
Marek Vasut1b5dd322022-04-13 04:15:34 +0200307 if ((dm9000_ior(db, DM9000_PIDL) != 0x0) ||
308 (dm9000_ior(db, DM9000_PIDH) != 0x90))
Remy Bohmer2f13d2c2008-06-03 15:26:24 +0200309 printf("ERROR: resetting DM9000 -> not responding\n");
wdenk7ac16102004-08-01 22:48:16 +0000310}
311
Marek Vasuta4aa0482022-04-13 04:15:29 +0200312/* Initialize dm9000 board */
Marek Vasut9b65a962022-04-13 04:15:35 +0200313static int dm9000_init_common(struct dm9000_priv *db, u8 enetaddr[6])
wdenk7ac16102004-08-01 22:48:16 +0000314{
315 int i, oft, lnk;
Remy Bohmer5f63bf42008-06-03 15:26:21 +0200316 u8 io_mode;
Remy Bohmer5f63bf42008-06-03 15:26:21 +0200317
wdenk7ac16102004-08-01 22:48:16 +0000318 /* RESET device */
Marek Vasut1b5dd322022-04-13 04:15:34 +0200319 dm9000_reset(db);
Andrew Dyera62f5d42008-08-26 17:03:38 -0500320
Marek Vasut1b5dd322022-04-13 04:15:34 +0200321 if (dm9000_probe(db) < 0)
Andrew Dyera62f5d42008-08-26 17:03:38 -0500322 return -1;
wdenk7ac16102004-08-01 22:48:16 +0000323
Remy Bohmer5f63bf42008-06-03 15:26:21 +0200324 /* Auto-detect 8/16/32 bit mode, ISR Bit 6+7 indicate bus width */
Marek Vasut1b5dd322022-04-13 04:15:34 +0200325 io_mode = dm9000_ior(db, DM9000_ISR) >> 6;
Remy Bohmer5f63bf42008-06-03 15:26:21 +0200326
327 switch (io_mode) {
328 case 0x0: /* 16-bit mode */
329 printf("DM9000: running in 16 bit mode\n");
330 db->outblk = dm9000_outblk_16bit;
331 db->inblk = dm9000_inblk_16bit;
332 db->rx_status = dm9000_rx_status_16bit;
333 break;
334 case 0x01: /* 32-bit mode */
335 printf("DM9000: running in 32 bit mode\n");
336 db->outblk = dm9000_outblk_32bit;
337 db->inblk = dm9000_inblk_32bit;
338 db->rx_status = dm9000_rx_status_32bit;
339 break;
340 case 0x02: /* 8 bit mode */
341 printf("DM9000: running in 8 bit mode\n");
342 db->outblk = dm9000_outblk_8bit;
343 db->inblk = dm9000_inblk_8bit;
344 db->rx_status = dm9000_rx_status_8bit;
345 break;
346 default:
347 /* Assume 8 bit mode, will probably not work anyway */
348 printf("DM9000: Undefined IO-mode:0x%x\n", io_mode);
349 db->outblk = dm9000_outblk_8bit;
350 db->inblk = dm9000_inblk_8bit;
351 db->rx_status = dm9000_rx_status_8bit;
352 break;
353 }
354
Andrew Dyera62f5d42008-08-26 17:03:38 -0500355 /* Program operating register, only internal phy supported */
Marek Vasut1b5dd322022-04-13 04:15:34 +0200356 dm9000_iow(db, DM9000_NCR, 0x0);
Remy Bohmer61b8dbd2008-06-03 15:26:26 +0200357 /* TX Polling clear */
Marek Vasut1b5dd322022-04-13 04:15:34 +0200358 dm9000_iow(db, DM9000_TCR, 0);
Remy Bohmer61b8dbd2008-06-03 15:26:26 +0200359 /* Less 3Kb, 200us */
Marek Vasut1b5dd322022-04-13 04:15:34 +0200360 dm9000_iow(db, DM9000_BPTR, BPTR_BPHW(3) | BPTR_JPT_600US);
Remy Bohmer61b8dbd2008-06-03 15:26:26 +0200361 /* Flow Control : High/Low Water */
Marek Vasut1b5dd322022-04-13 04:15:34 +0200362 dm9000_iow(db, DM9000_FCTR, FCTR_HWOT(3) | FCTR_LWOT(8));
Remy Bohmer61b8dbd2008-06-03 15:26:26 +0200363 /* SH FIXME: This looks strange! Flow Control */
Marek Vasut1b5dd322022-04-13 04:15:34 +0200364 dm9000_iow(db, DM9000_FCR, 0x0);
Remy Bohmer61b8dbd2008-06-03 15:26:26 +0200365 /* Special Mode */
Marek Vasut1b5dd322022-04-13 04:15:34 +0200366 dm9000_iow(db, DM9000_SMCR, 0);
Remy Bohmer61b8dbd2008-06-03 15:26:26 +0200367 /* clear TX status */
Marek Vasut1b5dd322022-04-13 04:15:34 +0200368 dm9000_iow(db, DM9000_NSR, NSR_WAKEST | NSR_TX2END | NSR_TX1END);
Remy Bohmer61b8dbd2008-06-03 15:26:26 +0200369 /* Clear interrupt status */
Marek Vasut1b5dd322022-04-13 04:15:34 +0200370 dm9000_iow(db, DM9000_ISR, ISR_ROOS | ISR_ROS | ISR_PTS | ISR_PRS);
wdenk7ac16102004-08-01 22:48:16 +0000371
Marek Vasut9b65a962022-04-13 04:15:35 +0200372 printf("MAC: %pM\n", enetaddr);
373 if (!is_valid_ethaddr(enetaddr))
Andrew Ruder1c377d12013-10-22 19:09:02 -0500374 printf("WARNING: Bad MAC address (uninitialized EEPROM?)\n");
Andrew Dyera62f5d42008-08-26 17:03:38 -0500375
376 /* fill device MAC address registers */
377 for (i = 0, oft = DM9000_PAR; i < 6; i++, oft++)
Marek Vasut9b65a962022-04-13 04:15:35 +0200378 dm9000_iow(db, oft, enetaddr[i]);
wdenk7ac16102004-08-01 22:48:16 +0000379 for (i = 0, oft = 0x16; i < 8; i++, oft++)
Marek Vasut1b5dd322022-04-13 04:15:34 +0200380 dm9000_iow(db, oft, 0xff);
wdenk7ac16102004-08-01 22:48:16 +0000381
382 /* read back mac, just to be sure */
383 for (i = 0, oft = 0x10; i < 6; i++, oft++)
Marek Vasut1b5dd322022-04-13 04:15:34 +0200384 debug("%02x:", dm9000_ior(db, oft));
Marek Vasuted761222022-04-13 04:15:24 +0200385 debug("\n");
wdenk7ac16102004-08-01 22:48:16 +0000386
387 /* Activate DM9000 */
Remy Bohmer61b8dbd2008-06-03 15:26:26 +0200388 /* RX enable */
Marek Vasut1b5dd322022-04-13 04:15:34 +0200389 dm9000_iow(db, DM9000_RCR, RCR_DIS_LONG | RCR_DIS_CRC | RCR_RXEN);
Remy Bohmer61b8dbd2008-06-03 15:26:26 +0200390 /* Enable TX/RX interrupt mask */
Marek Vasut1b5dd322022-04-13 04:15:34 +0200391 dm9000_iow(db, DM9000_IMR, IMR_PAR);
Remy Bohmer61b8dbd2008-06-03 15:26:26 +0200392
wdenk7ac16102004-08-01 22:48:16 +0000393 i = 0;
Marek Vasut1b5dd322022-04-13 04:15:34 +0200394 while (!(dm9000_phy_read(db, 1) & 0x20)) { /* autonegation complete bit */
wdenk7ac16102004-08-01 22:48:16 +0000395 udelay(1000);
396 i++;
397 if (i == 10000) {
398 printf("could not establish link\n");
399 return 0;
400 }
401 }
402
403 /* see what we've got */
Marek Vasut1b5dd322022-04-13 04:15:34 +0200404 lnk = dm9000_phy_read(db, 17) >> 12;
wdenk7ac16102004-08-01 22:48:16 +0000405 printf("operating at ");
406 switch (lnk) {
407 case 1:
408 printf("10M half duplex ");
409 break;
410 case 2:
411 printf("10M full duplex ");
412 break;
413 case 4:
414 printf("100M half duplex ");
415 break;
416 case 8:
417 printf("100M full duplex ");
418 break;
419 default:
420 printf("unknown: %d ", lnk);
421 break;
422 }
423 printf("mode\n");
424 return 0;
425}
426
427/*
Marek Vasuta4aa0482022-04-13 04:15:29 +0200428 * Hardware start transmission.
429 * Send a packet to media from the upper layer.
430 */
Marek Vasut9b65a962022-04-13 04:15:35 +0200431static int dm9000_send_common(struct dm9000_priv *db, void *packet, int length)
wdenk7ac16102004-08-01 22:48:16 +0000432{
wdenk7ac16102004-08-01 22:48:16 +0000433 int tmo;
Remy Bohmer5f63bf42008-06-03 15:26:21 +0200434
Marek Vasuta4aa0482022-04-13 04:15:29 +0200435 dm9000_dump_packet(__func__, packet, length);
wdenk7ac16102004-08-01 22:48:16 +0000436
Marek Vasut1b5dd322022-04-13 04:15:34 +0200437 dm9000_iow(db, DM9000_ISR, IMR_PTM); /* Clear Tx bit in ISR */
Remy Bohmer16cb2642008-06-03 15:26:23 +0200438
wdenk7ac16102004-08-01 22:48:16 +0000439 /* Move data to DM9000 TX RAM */
Marek Vasut1b5dd322022-04-13 04:15:34 +0200440 dm9000_outb(DM9000_MWCMD, db->base_io); /* Prepare for TX-data */
wdenk7ac16102004-08-01 22:48:16 +0000441
Remy Bohmer5f63bf42008-06-03 15:26:21 +0200442 /* push the data to the TX-fifo */
Marek Vasut1b5dd322022-04-13 04:15:34 +0200443 db->outblk(db, packet, length);
wdenk7ac16102004-08-01 22:48:16 +0000444
445 /* Set TX length to DM9000 */
Marek Vasut1b5dd322022-04-13 04:15:34 +0200446 dm9000_iow(db, DM9000_TXPLL, length & 0xff);
447 dm9000_iow(db, DM9000_TXPLH, (length >> 8) & 0xff);
wdenk7ac16102004-08-01 22:48:16 +0000448
449 /* Issue TX polling command */
Marek Vasut1b5dd322022-04-13 04:15:34 +0200450 dm9000_iow(db, DM9000_TCR, TCR_TXREQ); /* Cleared after TX complete */
wdenk7ac16102004-08-01 22:48:16 +0000451
452 /* wait for end of transmission */
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200453 tmo = get_timer(0) + 5 * CONFIG_SYS_HZ;
Marek Vasut1b5dd322022-04-13 04:15:34 +0200454 while (!(dm9000_ior(db, DM9000_NSR) & (NSR_TX1END | NSR_TX2END)) ||
455 !(dm9000_ior(db, DM9000_ISR) & IMR_PTM)) {
wdenk7ac16102004-08-01 22:48:16 +0000456 if (get_timer(0) >= tmo) {
457 printf("transmission timeout\n");
458 break;
459 }
460 }
Marek Vasut1b5dd322022-04-13 04:15:34 +0200461 dm9000_iow(db, DM9000_ISR, IMR_PTM); /* Clear Tx bit in ISR */
Remy Bohmer16cb2642008-06-03 15:26:23 +0200462
Marek Vasuted761222022-04-13 04:15:24 +0200463 debug("transmit done\n\n");
wdenk7ac16102004-08-01 22:48:16 +0000464 return 0;
465}
466
467/*
Marek Vasuta4aa0482022-04-13 04:15:29 +0200468 * Stop the interface.
469 * The interface is stopped when it is brought.
470 */
Marek Vasut9b65a962022-04-13 04:15:35 +0200471static void dm9000_halt_common(struct dm9000_priv *db)
wdenk7ac16102004-08-01 22:48:16 +0000472{
Marek Vasuta4aa0482022-04-13 04:15:29 +0200473 /* RESET device */
Marek Vasut1b5dd322022-04-13 04:15:34 +0200474 dm9000_phy_write(db, 0, 0x8000); /* PHY RESET */
475 dm9000_iow(db, DM9000_GPR, 0x01); /* Power-Down PHY */
476 dm9000_iow(db, DM9000_IMR, 0x80); /* Disable all interrupt */
477 dm9000_iow(db, DM9000_RCR, 0x00); /* Disable RX */
wdenk7ac16102004-08-01 22:48:16 +0000478}
479
480/*
Marek Vasuta4aa0482022-04-13 04:15:29 +0200481 * Received a packet and pass to upper layer
482 */
Marek Vasut0beb3392022-04-13 04:15:36 +0200483static int dm9000_recv_common(struct dm9000_priv *db, uchar *rdptr)
wdenk7ac16102004-08-01 22:48:16 +0000484{
Joe Hershberger9f09a362015-04-08 01:41:06 -0500485 u8 rxbyte;
Marek Vasut5248e562022-04-13 04:15:25 +0200486 u16 rxstatus, rxlen = 0;
wdenk7ac16102004-08-01 22:48:16 +0000487
Marek Vasuta4aa0482022-04-13 04:15:29 +0200488 /*
489 * Check packet ready or not, we must check
490 * the ISR status first for DM9000A
491 */
Marek Vasut1b5dd322022-04-13 04:15:34 +0200492 if (!(dm9000_ior(db, DM9000_ISR) & 0x01)) /* Rx-ISR bit must be set. */
wdenk7ac16102004-08-01 22:48:16 +0000493 return 0;
494
Marek Vasut1b5dd322022-04-13 04:15:34 +0200495 dm9000_iow(db, DM9000_ISR, 0x01); /* clear PR status latched in bit 0 */
wdenk7ac16102004-08-01 22:48:16 +0000496
Remy Bohmereec38a12008-06-03 15:26:25 +0200497 /* There is _at least_ 1 package in the fifo, read them all */
Marek Vasut0beb3392022-04-13 04:15:36 +0200498 dm9000_ior(db, DM9000_MRCMDX); /* Dummy read */
wdenk7ac16102004-08-01 22:48:16 +0000499
Marek Vasut0beb3392022-04-13 04:15:36 +0200500 /*
501 * Get most updated data,
502 * only look at bits 0:1, See application notes DM9000
503 */
504 rxbyte = dm9000_inb(db->base_data) & 0x03;
wdenk7ac16102004-08-01 22:48:16 +0000505
Marek Vasut0beb3392022-04-13 04:15:36 +0200506 /* Status check: this byte must be 0 or 1 */
507 if (rxbyte > DM9000_PKT_RDY) {
508 dm9000_iow(db, DM9000_RCR, 0x00); /* Stop Device */
509 dm9000_iow(db, DM9000_ISR, 0x80); /* Stop INT request */
510 printf("DM9000 error: status check fail: 0x%x\n",
511 rxbyte);
512 return -EINVAL;
513 }
wdenk7ac16102004-08-01 22:48:16 +0000514
Marek Vasut0beb3392022-04-13 04:15:36 +0200515 if (rxbyte != DM9000_PKT_RDY)
516 return 0; /* No packet received, ignore */
Remy Bohmereec38a12008-06-03 15:26:25 +0200517
Marek Vasut0beb3392022-04-13 04:15:36 +0200518 debug("receiving packet\n");
wdenk7ac16102004-08-01 22:48:16 +0000519
Marek Vasut0beb3392022-04-13 04:15:36 +0200520 /* A packet ready now & Get status/length */
521 db->rx_status(db, &rxstatus, &rxlen);
Remy Bohmereec38a12008-06-03 15:26:25 +0200522
Marek Vasut0beb3392022-04-13 04:15:36 +0200523 debug("rx status: 0x%04x rx len: %d\n", rxstatus, rxlen);
Remy Bohmereec38a12008-06-03 15:26:25 +0200524
Marek Vasut0beb3392022-04-13 04:15:36 +0200525 /* Move data from DM9000 */
526 /* Read received packet from RX SRAM */
527 db->inblk(db, rdptr, rxlen);
Remy Bohmereec38a12008-06-03 15:26:25 +0200528
Marek Vasut0beb3392022-04-13 04:15:36 +0200529 if (rxstatus & 0xbf00 || rxlen < 0x40 || rxlen > DM9000_PKT_MAX) {
530 if (rxstatus & 0x100)
531 printf("rx fifo error\n");
532 if (rxstatus & 0x200)
533 printf("rx crc error\n");
534 if (rxstatus & 0x8000)
535 printf("rx length error\n");
536 if (rxlen > DM9000_PKT_MAX) {
537 printf("rx length too big\n");
538 dm9000_reset(db);
Remy Bohmereec38a12008-06-03 15:26:25 +0200539 }
Marek Vasut0beb3392022-04-13 04:15:36 +0200540 return -EINVAL;
wdenk7ac16102004-08-01 22:48:16 +0000541 }
Marek Vasut0beb3392022-04-13 04:15:36 +0200542
543 return rxlen;
wdenk7ac16102004-08-01 22:48:16 +0000544}
545
546/*
Marek Vasuta4aa0482022-04-13 04:15:29 +0200547 * Read a word data from SROM
548 */
Remy Bohmercd9a36c2009-05-03 12:11:40 +0200549#if !defined(CONFIG_DM9000_NO_SROM)
Marek Vasut1b5dd322022-04-13 04:15:34 +0200550static void dm9000_read_srom_word(struct dm9000_priv *db, int offset, u8 *to)
wdenk7ac16102004-08-01 22:48:16 +0000551{
Marek Vasut1b5dd322022-04-13 04:15:34 +0200552 dm9000_iow(db, DM9000_EPAR, offset);
553 dm9000_iow(db, DM9000_EPCR, 0x4);
Marek Vasuta4aa0482022-04-13 04:15:29 +0200554 mdelay(8);
Marek Vasut1b5dd322022-04-13 04:15:34 +0200555 dm9000_iow(db, DM9000_EPCR, 0x0);
556 to[0] = dm9000_ior(db, DM9000_EPDRL);
557 to[1] = dm9000_ior(db, DM9000_EPDRH);
wdenk7ac16102004-08-01 22:48:16 +0000558}
559
Marek Vasut9b65a962022-04-13 04:15:35 +0200560static void dm9000_get_enetaddr(struct dm9000_priv *db, u8 *enetaddr)
Ben Warren8707f622009-10-21 21:53:39 -0700561{
Ben Warren8707f622009-10-21 21:53:39 -0700562 int i;
Marek Vasuta4aa0482022-04-13 04:15:29 +0200563
Ben Warren8707f622009-10-21 21:53:39 -0700564 for (i = 0; i < 3; i++)
Marek Vasut9b65a962022-04-13 04:15:35 +0200565 dm9000_read_srom_word(db, i, enetaddr + (2 * i));
Ben Warren8707f622009-10-21 21:53:39 -0700566}
Marek Vasuta4aa0482022-04-13 04:15:29 +0200567#else
Marek Vasut9b65a962022-04-13 04:15:35 +0200568static void dm9000_get_enetaddr(struct dm9000_priv *db, u8 *enetaddr) {}
Marek Vasuta4aa0482022-04-13 04:15:29 +0200569#endif
stefano babic6708a602007-08-30 23:01:49 +0200570
Marek Vasut16c3e212022-04-13 04:15:37 +0200571static int dm9000_start(struct udevice *dev)
572{
573 struct dm9000_priv *db = dev_get_priv(dev);
574 struct eth_pdata *pdata = dev_get_plat(dev);
575
576 return dm9000_init_common(db, pdata->enetaddr);
577}
578
579static void dm9000_stop(struct udevice *dev)
580{
581 struct dm9000_priv *db = dev_get_priv(dev);
582
583 dm9000_halt_common(db);
584}
585
586static int dm9000_send(struct udevice *dev, void *packet, int length)
587{
588 struct dm9000_priv *db = dev_get_priv(dev);
589 int ret;
590
591 ret = dm9000_send_common(db, packet, length);
592
593 return ret ? 0 : -ETIMEDOUT;
594}
595
596static int dm9000_recv(struct udevice *dev, int flags, uchar **packetp)
597{
598 struct dm9000_priv *db = dev_get_priv(dev);
599 uchar *data = net_rx_packets[0];
600 int ret;
601
602 ret = dm9000_recv_common(db, data);
Marek Vasutff35f4c2022-04-25 20:28:05 +0200603 if (ret > 0)
Marek Vasut16c3e212022-04-13 04:15:37 +0200604 *packetp = (void *)data;
605
Marek Vasutff35f4c2022-04-25 20:28:05 +0200606 return ret >= 0 ? ret : -EAGAIN;
Marek Vasut16c3e212022-04-13 04:15:37 +0200607}
608
609static int dm9000_write_hwaddr(struct udevice *dev)
610{
611 struct dm9000_priv *db = dev_get_priv(dev);
612 struct eth_pdata *pdata = dev_get_plat(dev);
613 int i, oft;
614
615 /* fill device MAC address registers */
616 for (i = 0, oft = DM9000_PAR; i < 6; i++, oft++)
617 dm9000_iow(db, oft, pdata->enetaddr[i]);
618
619 for (i = 0, oft = 0x16; i < 8; i++, oft++)
620 dm9000_iow(db, oft, 0xff);
621
622 /* read back mac, just to be sure */
623 for (i = 0, oft = 0x10; i < 6; i++, oft++)
624 debug("%02x:", dm9000_ior(db, oft));
625
626 debug("\n");
627
628 return 0;
629}
630
631static int dm9000_read_rom_hwaddr(struct udevice *dev)
632{
633 struct dm9000_priv *db = dev_get_priv(dev);
634 struct eth_pdata *pdata = dev_get_plat(dev);
635
636 dm9000_get_enetaddr(db, pdata->enetaddr);
637
638 return !is_valid_ethaddr(pdata->enetaddr);
639}
640
641static int dm9000_bind(struct udevice *dev)
642{
643 return device_set_name(dev, dev->name);
644}
645
646static int dm9000_of_to_plat(struct udevice *dev)
647{
648 struct dm9000_priv *db = dev_get_priv(dev);
649 struct eth_pdata *pdata = dev_get_plat(dev);
650
651 pdata->iobase = dev_read_addr_index(dev, 0);
652 db->base_io = (void __iomem *)pdata->iobase;
Johan Jonker5ff88122023-03-13 01:31:49 +0100653 db->base_data = dev_read_addr_index_ptr(dev, 1);
Marek Vasut16c3e212022-04-13 04:15:37 +0200654
655 return 0;
656}
657
658static const struct eth_ops dm9000_ops = {
659 .start = dm9000_start,
660 .stop = dm9000_stop,
661 .send = dm9000_send,
662 .recv = dm9000_recv,
663 .write_hwaddr = dm9000_write_hwaddr,
664 .read_rom_hwaddr = dm9000_read_rom_hwaddr,
665};
666
667static const struct udevice_id dm9000_ids[] = {
668 { .compatible = "davicom,dm9000" },
669 { }
670};
671
672U_BOOT_DRIVER(dm9000) = {
673 .name = "eth_dm9000",
674 .id = UCLASS_ETH,
675 .of_match = dm9000_ids,
676 .bind = dm9000_bind,
677 .of_to_plat = dm9000_of_to_plat,
678 .ops = &dm9000_ops,
679 .priv_auto = sizeof(struct dm9000_priv),
680 .plat_auto = sizeof(struct eth_pdata),
681 .flags = DM_FLAG_ALLOC_PRIV_DMA,
682};