blob: 78ce536d4a375d8d30a2306e080f773d19ae1cee [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>
Marek Vasut16c3e212022-04-13 04:15:37 +020054#include <dm.h>
Marek Vasut9c5e9ca2022-04-13 04:15:32 +020055#include <malloc.h>
wdenk7ac16102004-08-01 22:48:16 +000056#include <net.h>
57#include <asm/io.h>
Simon Glassdbd79542020-05-10 11:40:11 -060058#include <linux/delay.h>
wdenk7ac16102004-08-01 22:48:16 +000059
wdenk7ac16102004-08-01 22:48:16 +000060#include "dm9000x.h"
61
wdenk7ac16102004-08-01 22:48:16 +000062/* Structure/enum declaration ------------------------------- */
Marek Vasut09a8a9c2022-04-13 04:15:31 +020063struct dm9000_priv {
wdenk7ac16102004-08-01 22:48:16 +000064 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 */
wdenk7ac16102004-08-01 22:48:16 +000074 unsigned char srom[128];
Marek Vasut1b5dd322022-04-13 04:15:34 +020075 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 Vasut16c3e212022-04-13 04:15:37 +020078#ifndef CONFIG_DM_ETH
Marek Vasut9c5e9ca2022-04-13 04:15:32 +020079 struct eth_device dev;
Marek Vasut16c3e212022-04-13 04:15:37 +020080#endif
Marek Vasut1b5dd322022-04-13 04:15:34 +020081 void __iomem *base_io;
82 void __iomem *base_data;
Marek Vasuta4aa0482022-04-13 04:15:29 +020083};
wdenk7ac16102004-08-01 22:48:16 +000084
wdenk7ac16102004-08-01 22:48:16 +000085/* DM9000 network board routine ---------------------------- */
Jason Jinc74c4362011-08-25 15:46:43 +080086#ifndef CONFIG_DM9000_BYTE_SWAPPED
Marek Vasuta4aa0482022-04-13 04:15:29 +020087#define dm9000_outb(d, r) writeb((d), (r))
88#define dm9000_outw(d, r) writew((d), (r))
89#define dm9000_outl(d, r) writel((d), (r))
Marek Vasut7823b232022-04-13 04:15:28 +020090#define dm9000_inb(r) readb(r)
91#define dm9000_inw(r) readw(r)
92#define dm9000_inl(r) readl(r)
Jason Jinc74c4362011-08-25 15:46:43 +080093#else
Marek Vasuteb2749a2022-04-13 04:15:23 +020094#define dm9000_outb(d, r) __raw_writeb(d, r)
95#define dm9000_outw(d, r) __raw_writew(d, r)
96#define dm9000_outl(d, r) __raw_writel(d, r)
97#define dm9000_inb(r) __raw_readb(r)
98#define dm9000_inw(r) __raw_readw(r)
99#define dm9000_inl(r) __raw_readl(r)
Jason Jinc74c4362011-08-25 15:46:43 +0800100#endif
wdenk7ac16102004-08-01 22:48:16 +0000101
Marek Vasut52006d22022-04-13 04:15:27 +0200102#ifdef DEBUG
103static void dm9000_dump_packet(const char *func, u8 *packet, int length)
104{
105 int i;
106
107 printf("%s: length: %d\n", func, length);
108
109 for (i = 0; i < length; i++) {
110 if (i % 8 == 0)
111 printf("\n%s: %02x: ", func, i);
112 printf("%02x ", packet[i]);
113 }
114
115 printf("\n");
116}
117#else
118static void dm9000_dump_packet(const char *func, u8 *packet, int length) {}
119#endif
120
Marek Vasut1b5dd322022-04-13 04:15:34 +0200121static void dm9000_outblk_8bit(struct dm9000_priv *db, void *data_ptr, int count)
Remy Bohmer5f63bf42008-06-03 15:26:21 +0200122{
123 int i;
Marek Vasuta4aa0482022-04-13 04:15:29 +0200124
Remy Bohmer5f63bf42008-06-03 15:26:21 +0200125 for (i = 0; i < count; i++)
Marek Vasut1b5dd322022-04-13 04:15:34 +0200126 dm9000_outb((((u8 *)data_ptr)[i] & 0xff), db->base_data);
Remy Bohmer5f63bf42008-06-03 15:26:21 +0200127}
128
Marek Vasut1b5dd322022-04-13 04:15:34 +0200129static void dm9000_outblk_16bit(struct dm9000_priv *db, void *data_ptr, int count)
Remy Bohmer5f63bf42008-06-03 15:26:21 +0200130{
131 int i;
132 u32 tmplen = (count + 1) / 2;
133
134 for (i = 0; i < tmplen; i++)
Marek Vasut1b5dd322022-04-13 04:15:34 +0200135 dm9000_outw(((u16 *)data_ptr)[i], db->base_data);
Remy Bohmer5f63bf42008-06-03 15:26:21 +0200136}
Marek Vasuta4aa0482022-04-13 04:15:29 +0200137
Marek Vasut1b5dd322022-04-13 04:15:34 +0200138static void dm9000_outblk_32bit(struct dm9000_priv *db, void *data_ptr, int count)
Remy Bohmer5f63bf42008-06-03 15:26:21 +0200139{
140 int i;
141 u32 tmplen = (count + 3) / 4;
142
143 for (i = 0; i < tmplen; i++)
Marek Vasut1b5dd322022-04-13 04:15:34 +0200144 dm9000_outl(((u32 *)data_ptr)[i], db->base_data);
Remy Bohmer5f63bf42008-06-03 15:26:21 +0200145}
146
Marek Vasut1b5dd322022-04-13 04:15:34 +0200147static void dm9000_inblk_8bit(struct dm9000_priv *db, void *data_ptr, int count)
Remy Bohmer5f63bf42008-06-03 15:26:21 +0200148{
149 int i;
Marek Vasuta4aa0482022-04-13 04:15:29 +0200150
Remy Bohmer5f63bf42008-06-03 15:26:21 +0200151 for (i = 0; i < count; i++)
Marek Vasut1b5dd322022-04-13 04:15:34 +0200152 ((u8 *)data_ptr)[i] = dm9000_inb(db->base_data);
Remy Bohmer5f63bf42008-06-03 15:26:21 +0200153}
154
Marek Vasut1b5dd322022-04-13 04:15:34 +0200155static void dm9000_inblk_16bit(struct dm9000_priv *db, void *data_ptr, int count)
Remy Bohmer5f63bf42008-06-03 15:26:21 +0200156{
157 int i;
158 u32 tmplen = (count + 1) / 2;
159
160 for (i = 0; i < tmplen; i++)
Marek Vasut1b5dd322022-04-13 04:15:34 +0200161 ((u16 *)data_ptr)[i] = dm9000_inw(db->base_data);
Remy Bohmer5f63bf42008-06-03 15:26:21 +0200162}
Marek Vasuta4aa0482022-04-13 04:15:29 +0200163
Marek Vasut1b5dd322022-04-13 04:15:34 +0200164static void dm9000_inblk_32bit(struct dm9000_priv *db, void *data_ptr, int count)
Remy Bohmer5f63bf42008-06-03 15:26:21 +0200165{
166 int i;
167 u32 tmplen = (count + 3) / 4;
168
169 for (i = 0; i < tmplen; i++)
Marek Vasut1b5dd322022-04-13 04:15:34 +0200170 ((u32 *)data_ptr)[i] = dm9000_inl(db->base_data);
Remy Bohmer5f63bf42008-06-03 15:26:21 +0200171}
172
Marek Vasut1b5dd322022-04-13 04:15:34 +0200173static void dm9000_rx_status_32bit(struct dm9000_priv *db, u16 *rxstatus, u16 *rxlen)
Remy Bohmer5f63bf42008-06-03 15:26:21 +0200174{
Remy Bohmer2e1604f2008-06-04 10:47:25 +0200175 u32 tmpdata;
Remy Bohmer5f63bf42008-06-03 15:26:21 +0200176
Marek Vasut1b5dd322022-04-13 04:15:34 +0200177 dm9000_outb(DM9000_MRCMD, db->base_io);
Remy Bohmer5f63bf42008-06-03 15:26:21 +0200178
Marek Vasut1b5dd322022-04-13 04:15:34 +0200179 tmpdata = dm9000_inl(db->base_data);
Marek Vasut5248e562022-04-13 04:15:25 +0200180 *rxstatus = __le16_to_cpu(tmpdata);
181 *rxlen = __le16_to_cpu(tmpdata >> 16);
Remy Bohmer5f63bf42008-06-03 15:26:21 +0200182}
183
Marek Vasut1b5dd322022-04-13 04:15:34 +0200184static void dm9000_rx_status_16bit(struct dm9000_priv *db, u16 *rxstatus, u16 *rxlen)
Remy Bohmer5f63bf42008-06-03 15:26:21 +0200185{
Marek Vasut1b5dd322022-04-13 04:15:34 +0200186 dm9000_outb(DM9000_MRCMD, db->base_io);
Remy Bohmer5f63bf42008-06-03 15:26:21 +0200187
Marek Vasut1b5dd322022-04-13 04:15:34 +0200188 *rxstatus = __le16_to_cpu(dm9000_inw(db->base_data));
189 *rxlen = __le16_to_cpu(dm9000_inw(db->base_data));
Remy Bohmer5f63bf42008-06-03 15:26:21 +0200190}
191
Marek Vasut1b5dd322022-04-13 04:15:34 +0200192static void dm9000_rx_status_8bit(struct dm9000_priv *db, u16 *rxstatus, u16 *rxlen)
Remy Bohmer5f63bf42008-06-03 15:26:21 +0200193{
Marek Vasut1b5dd322022-04-13 04:15:34 +0200194 dm9000_outb(DM9000_MRCMD, db->base_io);
Remy Bohmer5f63bf42008-06-03 15:26:21 +0200195
Marek Vasut5248e562022-04-13 04:15:25 +0200196 *rxstatus =
Marek Vasut1b5dd322022-04-13 04:15:34 +0200197 __le16_to_cpu(dm9000_inb(db->base_data) +
198 (dm9000_inb(db->base_data) << 8));
Marek Vasut5248e562022-04-13 04:15:25 +0200199 *rxlen =
Marek Vasut1b5dd322022-04-13 04:15:34 +0200200 __le16_to_cpu(dm9000_inb(db->base_data) +
201 (dm9000_inb(db->base_data) << 8));
Remy Bohmer5f63bf42008-06-03 15:26:21 +0200202}
wdenk7ac16102004-08-01 22:48:16 +0000203
204/*
Marek Vasutc5e9d642022-04-13 04:15:30 +0200205 * Read a byte from I/O port
206 */
Marek Vasut1b5dd322022-04-13 04:15:34 +0200207static u8 dm9000_ior(struct dm9000_priv *db, int reg)
Marek Vasutc5e9d642022-04-13 04:15:30 +0200208{
Marek Vasut1b5dd322022-04-13 04:15:34 +0200209 dm9000_outb(reg, db->base_io);
210 return dm9000_inb(db->base_data);
Marek Vasutc5e9d642022-04-13 04:15:30 +0200211}
212
213/*
214 * Write a byte to I/O port
215 */
Marek Vasut1b5dd322022-04-13 04:15:34 +0200216static void dm9000_iow(struct dm9000_priv *db, int reg, u8 value)
Marek Vasutc5e9d642022-04-13 04:15:30 +0200217{
Marek Vasut1b5dd322022-04-13 04:15:34 +0200218 dm9000_outb(reg, db->base_io);
219 dm9000_outb(value, db->base_data);
Marek Vasutc5e9d642022-04-13 04:15:30 +0200220}
221
222/*
223 * Read a word from phyxcer
224 */
Marek Vasut1b5dd322022-04-13 04:15:34 +0200225static u16 dm9000_phy_read(struct dm9000_priv *db, int reg)
Marek Vasutc5e9d642022-04-13 04:15:30 +0200226{
227 u16 val;
228
229 /* Fill the phyxcer register into REG_0C */
Marek Vasut1b5dd322022-04-13 04:15:34 +0200230 dm9000_iow(db, DM9000_EPAR, DM9000_PHY | reg);
231 dm9000_iow(db, DM9000_EPCR, 0xc); /* Issue phyxcer read command */
Marek Vasutc5e9d642022-04-13 04:15:30 +0200232 udelay(100); /* Wait read complete */
Marek Vasut1b5dd322022-04-13 04:15:34 +0200233 dm9000_iow(db, DM9000_EPCR, 0x0); /* Clear phyxcer read command */
234 val = (dm9000_ior(db, DM9000_EPDRH) << 8) |
235 dm9000_ior(db, DM9000_EPDRL);
Marek Vasutc5e9d642022-04-13 04:15:30 +0200236
237 /* The read data keeps on REG_0D & REG_0E */
238 debug("%s(0x%x): 0x%x\n", __func__, reg, val);
239 return val;
240}
241
242/*
243 * Write a word to phyxcer
244 */
Marek Vasut1b5dd322022-04-13 04:15:34 +0200245static void dm9000_phy_write(struct dm9000_priv *db, int reg, u16 value)
Marek Vasutc5e9d642022-04-13 04:15:30 +0200246{
247 /* Fill the phyxcer register into REG_0C */
Marek Vasut1b5dd322022-04-13 04:15:34 +0200248 dm9000_iow(db, DM9000_EPAR, DM9000_PHY | reg);
Marek Vasutc5e9d642022-04-13 04:15:30 +0200249
250 /* Fill the written data into REG_0D & REG_0E */
Marek Vasut1b5dd322022-04-13 04:15:34 +0200251 dm9000_iow(db, DM9000_EPDRL, (value & 0xff));
252 dm9000_iow(db, DM9000_EPDRH, ((value >> 8) & 0xff));
253 dm9000_iow(db, DM9000_EPCR, 0xa); /* Issue phyxcer write command */
Marek Vasutc5e9d642022-04-13 04:15:30 +0200254 udelay(500); /* Wait write complete */
Marek Vasut1b5dd322022-04-13 04:15:34 +0200255 dm9000_iow(db, DM9000_EPCR, 0x0); /* Clear phyxcer write command */
Marek Vasutc5e9d642022-04-13 04:15:30 +0200256 debug("%s(reg:0x%x, value:0x%x)\n", __func__, reg, value);
257}
258
259/*
Marek Vasuta4aa0482022-04-13 04:15:29 +0200260 * Search DM9000 board, allocate space and register it
261 */
Marek Vasut1b5dd322022-04-13 04:15:34 +0200262static int dm9000_probe(struct dm9000_priv *db)
wdenk7ac16102004-08-01 22:48:16 +0000263{
264 u32 id_val;
Marek Vasuta4aa0482022-04-13 04:15:29 +0200265
Marek Vasut1b5dd322022-04-13 04:15:34 +0200266 id_val = dm9000_ior(db, DM9000_VIDL);
267 id_val |= dm9000_ior(db, DM9000_VIDH) << 8;
268 id_val |= dm9000_ior(db, DM9000_PIDL) << 16;
269 id_val |= dm9000_ior(db, DM9000_PIDH) << 24;
Marek Vasuta4aa0482022-04-13 04:15:29 +0200270 if (id_val != DM9000_ID) {
Marek Vasut1b5dd322022-04-13 04:15:34 +0200271 printf("dm9000 not found at 0x%p id: 0x%08x\n",
272 db->base_io, id_val);
wdenk7ac16102004-08-01 22:48:16 +0000273 return -1;
274 }
Marek Vasuta4aa0482022-04-13 04:15:29 +0200275
Marek Vasut1b5dd322022-04-13 04:15:34 +0200276 printf("dm9000 i/o: 0x%p, id: 0x%x\n", db->base_io, id_val);
Marek Vasuta4aa0482022-04-13 04:15:29 +0200277 return 0;
wdenk7ac16102004-08-01 22:48:16 +0000278}
279
280/* General Purpose dm9000 reset routine */
Marek Vasut1b5dd322022-04-13 04:15:34 +0200281static void dm9000_reset(struct dm9000_priv *db)
wdenk7ac16102004-08-01 22:48:16 +0000282{
Marek Vasuted761222022-04-13 04:15:24 +0200283 debug("resetting DM9000\n");
Remy Bohmer2f13d2c2008-06-03 15:26:24 +0200284
Marek Vasuta4aa0482022-04-13 04:15:29 +0200285 /*
286 * Reset DM9000,
287 * see DM9000 Application Notes V1.22 Jun 11, 2004 page 29
288 */
Remy Bohmer2f13d2c2008-06-03 15:26:24 +0200289
Andrew Dyera62f5d42008-08-26 17:03:38 -0500290 /* DEBUG: Make all GPIO0 outputs, all others inputs */
Marek Vasut1b5dd322022-04-13 04:15:34 +0200291 dm9000_iow(db, DM9000_GPCR, GPCR_GPIO0_OUT);
Remy Bohmer2f13d2c2008-06-03 15:26:24 +0200292 /* Step 1: Power internal PHY by writing 0 to GPIO0 pin */
Marek Vasut1b5dd322022-04-13 04:15:34 +0200293 dm9000_iow(db, DM9000_GPR, 0);
Remy Bohmer2f13d2c2008-06-03 15:26:24 +0200294 /* Step 2: Software reset */
Marek Vasut1b5dd322022-04-13 04:15:34 +0200295 dm9000_iow(db, DM9000_NCR, (NCR_LBK_INT_MAC | NCR_RST));
Remy Bohmer2f13d2c2008-06-03 15:26:24 +0200296
297 do {
Marek Vasuted761222022-04-13 04:15:24 +0200298 debug("resetting the DM9000, 1st reset\n");
Remy Bohmer2f13d2c2008-06-03 15:26:24 +0200299 udelay(25); /* Wait at least 20 us */
Marek Vasut1b5dd322022-04-13 04:15:34 +0200300 } while (dm9000_ior(db, DM9000_NCR) & 1);
Remy Bohmer2f13d2c2008-06-03 15:26:24 +0200301
Marek Vasut1b5dd322022-04-13 04:15:34 +0200302 dm9000_iow(db, DM9000_NCR, 0);
303 dm9000_iow(db, DM9000_NCR, (NCR_LBK_INT_MAC | NCR_RST)); /* Issue a second reset */
Remy Bohmer2f13d2c2008-06-03 15:26:24 +0200304
305 do {
Marek Vasuted761222022-04-13 04:15:24 +0200306 debug("resetting the DM9000, 2nd reset\n");
Remy Bohmer2f13d2c2008-06-03 15:26:24 +0200307 udelay(25); /* Wait at least 20 us */
Marek Vasut1b5dd322022-04-13 04:15:34 +0200308 } while (dm9000_ior(db, DM9000_NCR) & 1);
Remy Bohmer2f13d2c2008-06-03 15:26:24 +0200309
310 /* Check whether the ethernet controller is present */
Marek Vasut1b5dd322022-04-13 04:15:34 +0200311 if ((dm9000_ior(db, DM9000_PIDL) != 0x0) ||
312 (dm9000_ior(db, DM9000_PIDH) != 0x90))
Remy Bohmer2f13d2c2008-06-03 15:26:24 +0200313 printf("ERROR: resetting DM9000 -> not responding\n");
wdenk7ac16102004-08-01 22:48:16 +0000314}
315
Marek Vasuta4aa0482022-04-13 04:15:29 +0200316/* Initialize dm9000 board */
Marek Vasut9b65a962022-04-13 04:15:35 +0200317static int dm9000_init_common(struct dm9000_priv *db, u8 enetaddr[6])
wdenk7ac16102004-08-01 22:48:16 +0000318{
319 int i, oft, lnk;
Remy Bohmer5f63bf42008-06-03 15:26:21 +0200320 u8 io_mode;
Remy Bohmer5f63bf42008-06-03 15:26:21 +0200321
wdenk7ac16102004-08-01 22:48:16 +0000322 /* RESET device */
Marek Vasut1b5dd322022-04-13 04:15:34 +0200323 dm9000_reset(db);
Andrew Dyera62f5d42008-08-26 17:03:38 -0500324
Marek Vasut1b5dd322022-04-13 04:15:34 +0200325 if (dm9000_probe(db) < 0)
Andrew Dyera62f5d42008-08-26 17:03:38 -0500326 return -1;
wdenk7ac16102004-08-01 22:48:16 +0000327
Remy Bohmer5f63bf42008-06-03 15:26:21 +0200328 /* Auto-detect 8/16/32 bit mode, ISR Bit 6+7 indicate bus width */
Marek Vasut1b5dd322022-04-13 04:15:34 +0200329 io_mode = dm9000_ior(db, DM9000_ISR) >> 6;
Remy Bohmer5f63bf42008-06-03 15:26:21 +0200330
331 switch (io_mode) {
332 case 0x0: /* 16-bit mode */
333 printf("DM9000: running in 16 bit mode\n");
334 db->outblk = dm9000_outblk_16bit;
335 db->inblk = dm9000_inblk_16bit;
336 db->rx_status = dm9000_rx_status_16bit;
337 break;
338 case 0x01: /* 32-bit mode */
339 printf("DM9000: running in 32 bit mode\n");
340 db->outblk = dm9000_outblk_32bit;
341 db->inblk = dm9000_inblk_32bit;
342 db->rx_status = dm9000_rx_status_32bit;
343 break;
344 case 0x02: /* 8 bit mode */
345 printf("DM9000: running in 8 bit mode\n");
346 db->outblk = dm9000_outblk_8bit;
347 db->inblk = dm9000_inblk_8bit;
348 db->rx_status = dm9000_rx_status_8bit;
349 break;
350 default:
351 /* Assume 8 bit mode, will probably not work anyway */
352 printf("DM9000: Undefined IO-mode:0x%x\n", io_mode);
353 db->outblk = dm9000_outblk_8bit;
354 db->inblk = dm9000_inblk_8bit;
355 db->rx_status = dm9000_rx_status_8bit;
356 break;
357 }
358
Andrew Dyera62f5d42008-08-26 17:03:38 -0500359 /* Program operating register, only internal phy supported */
Marek Vasut1b5dd322022-04-13 04:15:34 +0200360 dm9000_iow(db, DM9000_NCR, 0x0);
Remy Bohmer61b8dbd2008-06-03 15:26:26 +0200361 /* TX Polling clear */
Marek Vasut1b5dd322022-04-13 04:15:34 +0200362 dm9000_iow(db, DM9000_TCR, 0);
Remy Bohmer61b8dbd2008-06-03 15:26:26 +0200363 /* Less 3Kb, 200us */
Marek Vasut1b5dd322022-04-13 04:15:34 +0200364 dm9000_iow(db, DM9000_BPTR, BPTR_BPHW(3) | BPTR_JPT_600US);
Remy Bohmer61b8dbd2008-06-03 15:26:26 +0200365 /* Flow Control : High/Low Water */
Marek Vasut1b5dd322022-04-13 04:15:34 +0200366 dm9000_iow(db, DM9000_FCTR, FCTR_HWOT(3) | FCTR_LWOT(8));
Remy Bohmer61b8dbd2008-06-03 15:26:26 +0200367 /* SH FIXME: This looks strange! Flow Control */
Marek Vasut1b5dd322022-04-13 04:15:34 +0200368 dm9000_iow(db, DM9000_FCR, 0x0);
Remy Bohmer61b8dbd2008-06-03 15:26:26 +0200369 /* Special Mode */
Marek Vasut1b5dd322022-04-13 04:15:34 +0200370 dm9000_iow(db, DM9000_SMCR, 0);
Remy Bohmer61b8dbd2008-06-03 15:26:26 +0200371 /* clear TX status */
Marek Vasut1b5dd322022-04-13 04:15:34 +0200372 dm9000_iow(db, DM9000_NSR, NSR_WAKEST | NSR_TX2END | NSR_TX1END);
Remy Bohmer61b8dbd2008-06-03 15:26:26 +0200373 /* Clear interrupt status */
Marek Vasut1b5dd322022-04-13 04:15:34 +0200374 dm9000_iow(db, DM9000_ISR, ISR_ROOS | ISR_ROS | ISR_PTS | ISR_PRS);
wdenk7ac16102004-08-01 22:48:16 +0000375
Marek Vasut9b65a962022-04-13 04:15:35 +0200376 printf("MAC: %pM\n", enetaddr);
377 if (!is_valid_ethaddr(enetaddr))
Andrew Ruder1c377d12013-10-22 19:09:02 -0500378 printf("WARNING: Bad MAC address (uninitialized EEPROM?)\n");
Andrew Dyera62f5d42008-08-26 17:03:38 -0500379
380 /* fill device MAC address registers */
381 for (i = 0, oft = DM9000_PAR; i < 6; i++, oft++)
Marek Vasut9b65a962022-04-13 04:15:35 +0200382 dm9000_iow(db, oft, enetaddr[i]);
wdenk7ac16102004-08-01 22:48:16 +0000383 for (i = 0, oft = 0x16; i < 8; i++, oft++)
Marek Vasut1b5dd322022-04-13 04:15:34 +0200384 dm9000_iow(db, oft, 0xff);
wdenk7ac16102004-08-01 22:48:16 +0000385
386 /* read back mac, just to be sure */
387 for (i = 0, oft = 0x10; i < 6; i++, oft++)
Marek Vasut1b5dd322022-04-13 04:15:34 +0200388 debug("%02x:", dm9000_ior(db, oft));
Marek Vasuted761222022-04-13 04:15:24 +0200389 debug("\n");
wdenk7ac16102004-08-01 22:48:16 +0000390
391 /* Activate DM9000 */
Remy Bohmer61b8dbd2008-06-03 15:26:26 +0200392 /* RX enable */
Marek Vasut1b5dd322022-04-13 04:15:34 +0200393 dm9000_iow(db, DM9000_RCR, RCR_DIS_LONG | RCR_DIS_CRC | RCR_RXEN);
Remy Bohmer61b8dbd2008-06-03 15:26:26 +0200394 /* Enable TX/RX interrupt mask */
Marek Vasut1b5dd322022-04-13 04:15:34 +0200395 dm9000_iow(db, DM9000_IMR, IMR_PAR);
Remy Bohmer61b8dbd2008-06-03 15:26:26 +0200396
wdenk7ac16102004-08-01 22:48:16 +0000397 i = 0;
Marek Vasut1b5dd322022-04-13 04:15:34 +0200398 while (!(dm9000_phy_read(db, 1) & 0x20)) { /* autonegation complete bit */
wdenk7ac16102004-08-01 22:48:16 +0000399 udelay(1000);
400 i++;
401 if (i == 10000) {
402 printf("could not establish link\n");
403 return 0;
404 }
405 }
406
407 /* see what we've got */
Marek Vasut1b5dd322022-04-13 04:15:34 +0200408 lnk = dm9000_phy_read(db, 17) >> 12;
wdenk7ac16102004-08-01 22:48:16 +0000409 printf("operating at ");
410 switch (lnk) {
411 case 1:
412 printf("10M half duplex ");
413 break;
414 case 2:
415 printf("10M full duplex ");
416 break;
417 case 4:
418 printf("100M half duplex ");
419 break;
420 case 8:
421 printf("100M full duplex ");
422 break;
423 default:
424 printf("unknown: %d ", lnk);
425 break;
426 }
427 printf("mode\n");
428 return 0;
429}
430
431/*
Marek Vasuta4aa0482022-04-13 04:15:29 +0200432 * Hardware start transmission.
433 * Send a packet to media from the upper layer.
434 */
Marek Vasut9b65a962022-04-13 04:15:35 +0200435static int dm9000_send_common(struct dm9000_priv *db, void *packet, int length)
wdenk7ac16102004-08-01 22:48:16 +0000436{
wdenk7ac16102004-08-01 22:48:16 +0000437 int tmo;
Remy Bohmer5f63bf42008-06-03 15:26:21 +0200438
Marek Vasuta4aa0482022-04-13 04:15:29 +0200439 dm9000_dump_packet(__func__, packet, length);
wdenk7ac16102004-08-01 22:48:16 +0000440
Marek Vasut1b5dd322022-04-13 04:15:34 +0200441 dm9000_iow(db, DM9000_ISR, IMR_PTM); /* Clear Tx bit in ISR */
Remy Bohmer16cb2642008-06-03 15:26:23 +0200442
wdenk7ac16102004-08-01 22:48:16 +0000443 /* Move data to DM9000 TX RAM */
Marek Vasut1b5dd322022-04-13 04:15:34 +0200444 dm9000_outb(DM9000_MWCMD, db->base_io); /* Prepare for TX-data */
wdenk7ac16102004-08-01 22:48:16 +0000445
Remy Bohmer5f63bf42008-06-03 15:26:21 +0200446 /* push the data to the TX-fifo */
Marek Vasut1b5dd322022-04-13 04:15:34 +0200447 db->outblk(db, packet, length);
wdenk7ac16102004-08-01 22:48:16 +0000448
449 /* Set TX length to DM9000 */
Marek Vasut1b5dd322022-04-13 04:15:34 +0200450 dm9000_iow(db, DM9000_TXPLL, length & 0xff);
451 dm9000_iow(db, DM9000_TXPLH, (length >> 8) & 0xff);
wdenk7ac16102004-08-01 22:48:16 +0000452
453 /* Issue TX polling command */
Marek Vasut1b5dd322022-04-13 04:15:34 +0200454 dm9000_iow(db, DM9000_TCR, TCR_TXREQ); /* Cleared after TX complete */
wdenk7ac16102004-08-01 22:48:16 +0000455
456 /* wait for end of transmission */
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200457 tmo = get_timer(0) + 5 * CONFIG_SYS_HZ;
Marek Vasut1b5dd322022-04-13 04:15:34 +0200458 while (!(dm9000_ior(db, DM9000_NSR) & (NSR_TX1END | NSR_TX2END)) ||
459 !(dm9000_ior(db, DM9000_ISR) & IMR_PTM)) {
wdenk7ac16102004-08-01 22:48:16 +0000460 if (get_timer(0) >= tmo) {
461 printf("transmission timeout\n");
462 break;
463 }
464 }
Marek Vasut1b5dd322022-04-13 04:15:34 +0200465 dm9000_iow(db, DM9000_ISR, IMR_PTM); /* Clear Tx bit in ISR */
Remy Bohmer16cb2642008-06-03 15:26:23 +0200466
Marek Vasuted761222022-04-13 04:15:24 +0200467 debug("transmit done\n\n");
wdenk7ac16102004-08-01 22:48:16 +0000468 return 0;
469}
470
471/*
Marek Vasuta4aa0482022-04-13 04:15:29 +0200472 * Stop the interface.
473 * The interface is stopped when it is brought.
474 */
Marek Vasut9b65a962022-04-13 04:15:35 +0200475static void dm9000_halt_common(struct dm9000_priv *db)
wdenk7ac16102004-08-01 22:48:16 +0000476{
Marek Vasuta4aa0482022-04-13 04:15:29 +0200477 /* RESET device */
Marek Vasut1b5dd322022-04-13 04:15:34 +0200478 dm9000_phy_write(db, 0, 0x8000); /* PHY RESET */
479 dm9000_iow(db, DM9000_GPR, 0x01); /* Power-Down PHY */
480 dm9000_iow(db, DM9000_IMR, 0x80); /* Disable all interrupt */
481 dm9000_iow(db, DM9000_RCR, 0x00); /* Disable RX */
wdenk7ac16102004-08-01 22:48:16 +0000482}
483
484/*
Marek Vasuta4aa0482022-04-13 04:15:29 +0200485 * Received a packet and pass to upper layer
486 */
Marek Vasut0beb3392022-04-13 04:15:36 +0200487static int dm9000_recv_common(struct dm9000_priv *db, uchar *rdptr)
wdenk7ac16102004-08-01 22:48:16 +0000488{
Joe Hershberger9f09a362015-04-08 01:41:06 -0500489 u8 rxbyte;
Marek Vasut5248e562022-04-13 04:15:25 +0200490 u16 rxstatus, rxlen = 0;
wdenk7ac16102004-08-01 22:48:16 +0000491
Marek Vasuta4aa0482022-04-13 04:15:29 +0200492 /*
493 * Check packet ready or not, we must check
494 * the ISR status first for DM9000A
495 */
Marek Vasut1b5dd322022-04-13 04:15:34 +0200496 if (!(dm9000_ior(db, DM9000_ISR) & 0x01)) /* Rx-ISR bit must be set. */
wdenk7ac16102004-08-01 22:48:16 +0000497 return 0;
498
Marek Vasut1b5dd322022-04-13 04:15:34 +0200499 dm9000_iow(db, DM9000_ISR, 0x01); /* clear PR status latched in bit 0 */
wdenk7ac16102004-08-01 22:48:16 +0000500
Remy Bohmereec38a12008-06-03 15:26:25 +0200501 /* There is _at least_ 1 package in the fifo, read them all */
Marek Vasut0beb3392022-04-13 04:15:36 +0200502 dm9000_ior(db, DM9000_MRCMDX); /* Dummy read */
wdenk7ac16102004-08-01 22:48:16 +0000503
Marek Vasut0beb3392022-04-13 04:15:36 +0200504 /*
505 * Get most updated data,
506 * only look at bits 0:1, See application notes DM9000
507 */
508 rxbyte = dm9000_inb(db->base_data) & 0x03;
wdenk7ac16102004-08-01 22:48:16 +0000509
Marek Vasut0beb3392022-04-13 04:15:36 +0200510 /* Status check: this byte must be 0 or 1 */
511 if (rxbyte > DM9000_PKT_RDY) {
512 dm9000_iow(db, DM9000_RCR, 0x00); /* Stop Device */
513 dm9000_iow(db, DM9000_ISR, 0x80); /* Stop INT request */
514 printf("DM9000 error: status check fail: 0x%x\n",
515 rxbyte);
516 return -EINVAL;
517 }
wdenk7ac16102004-08-01 22:48:16 +0000518
Marek Vasut0beb3392022-04-13 04:15:36 +0200519 if (rxbyte != DM9000_PKT_RDY)
520 return 0; /* No packet received, ignore */
Remy Bohmereec38a12008-06-03 15:26:25 +0200521
Marek Vasut0beb3392022-04-13 04:15:36 +0200522 debug("receiving packet\n");
wdenk7ac16102004-08-01 22:48:16 +0000523
Marek Vasut0beb3392022-04-13 04:15:36 +0200524 /* A packet ready now & Get status/length */
525 db->rx_status(db, &rxstatus, &rxlen);
Remy Bohmereec38a12008-06-03 15:26:25 +0200526
Marek Vasut0beb3392022-04-13 04:15:36 +0200527 debug("rx status: 0x%04x rx len: %d\n", rxstatus, rxlen);
Remy Bohmereec38a12008-06-03 15:26:25 +0200528
Marek Vasut0beb3392022-04-13 04:15:36 +0200529 /* Move data from DM9000 */
530 /* Read received packet from RX SRAM */
531 db->inblk(db, rdptr, rxlen);
Remy Bohmereec38a12008-06-03 15:26:25 +0200532
Marek Vasut0beb3392022-04-13 04:15:36 +0200533 if (rxstatus & 0xbf00 || rxlen < 0x40 || rxlen > DM9000_PKT_MAX) {
534 if (rxstatus & 0x100)
535 printf("rx fifo error\n");
536 if (rxstatus & 0x200)
537 printf("rx crc error\n");
538 if (rxstatus & 0x8000)
539 printf("rx length error\n");
540 if (rxlen > DM9000_PKT_MAX) {
541 printf("rx length too big\n");
542 dm9000_reset(db);
Remy Bohmereec38a12008-06-03 15:26:25 +0200543 }
Marek Vasut0beb3392022-04-13 04:15:36 +0200544 return -EINVAL;
wdenk7ac16102004-08-01 22:48:16 +0000545 }
Marek Vasut0beb3392022-04-13 04:15:36 +0200546
547 return rxlen;
wdenk7ac16102004-08-01 22:48:16 +0000548}
549
550/*
Marek Vasuta4aa0482022-04-13 04:15:29 +0200551 * Read a word data from SROM
552 */
Remy Bohmercd9a36c2009-05-03 12:11:40 +0200553#if !defined(CONFIG_DM9000_NO_SROM)
Marek Vasut1b5dd322022-04-13 04:15:34 +0200554static void dm9000_read_srom_word(struct dm9000_priv *db, int offset, u8 *to)
wdenk7ac16102004-08-01 22:48:16 +0000555{
Marek Vasut1b5dd322022-04-13 04:15:34 +0200556 dm9000_iow(db, DM9000_EPAR, offset);
557 dm9000_iow(db, DM9000_EPCR, 0x4);
Marek Vasuta4aa0482022-04-13 04:15:29 +0200558 mdelay(8);
Marek Vasut1b5dd322022-04-13 04:15:34 +0200559 dm9000_iow(db, DM9000_EPCR, 0x0);
560 to[0] = dm9000_ior(db, DM9000_EPDRL);
561 to[1] = dm9000_ior(db, DM9000_EPDRH);
wdenk7ac16102004-08-01 22:48:16 +0000562}
563
Marek Vasut9b65a962022-04-13 04:15:35 +0200564static void dm9000_get_enetaddr(struct dm9000_priv *db, u8 *enetaddr)
Ben Warren8707f622009-10-21 21:53:39 -0700565{
Ben Warren8707f622009-10-21 21:53:39 -0700566 int i;
Marek Vasuta4aa0482022-04-13 04:15:29 +0200567
Ben Warren8707f622009-10-21 21:53:39 -0700568 for (i = 0; i < 3; i++)
Marek Vasut9b65a962022-04-13 04:15:35 +0200569 dm9000_read_srom_word(db, i, enetaddr + (2 * i));
Ben Warren8707f622009-10-21 21:53:39 -0700570}
Marek Vasuta4aa0482022-04-13 04:15:29 +0200571#else
Marek Vasut9b65a962022-04-13 04:15:35 +0200572static void dm9000_get_enetaddr(struct dm9000_priv *db, u8 *enetaddr) {}
Marek Vasuta4aa0482022-04-13 04:15:29 +0200573#endif
stefano babic6708a602007-08-30 23:01:49 +0200574
Marek Vasut16c3e212022-04-13 04:15:37 +0200575#ifndef CONFIG_DM_ETH
Marek Vasut9b65a962022-04-13 04:15:35 +0200576static int dm9000_init(struct eth_device *dev, struct bd_info *bd)
577{
578 struct dm9000_priv *db = container_of(dev, struct dm9000_priv, dev);
579
580 return dm9000_init_common(db, dev->enetaddr);
581}
582
583static void dm9000_halt(struct eth_device *dev)
584{
585 struct dm9000_priv *db = container_of(dev, struct dm9000_priv, dev);
586
587 dm9000_halt_common(db);
588}
589
590static int dm9000_send(struct eth_device *dev, void *packet, int length)
591{
592 struct dm9000_priv *db = container_of(dev, struct dm9000_priv, dev);
593
594 return dm9000_send_common(db, packet, length);
595}
596
597static int dm9000_recv(struct eth_device *dev)
598{
599 struct dm9000_priv *db = container_of(dev, struct dm9000_priv, dev);
Marek Vasut0beb3392022-04-13 04:15:36 +0200600 int ret;
601
602 ret = dm9000_recv_common(db, net_rx_packets[0]);
603 if (ret > 0)
604 net_process_received_packet(net_rx_packets[0], ret);
Marek Vasut9b65a962022-04-13 04:15:35 +0200605
Marek Vasut0beb3392022-04-13 04:15:36 +0200606 return ret;
Marek Vasut9b65a962022-04-13 04:15:35 +0200607}
608
Masahiro Yamadaf7ed78b2020-06-26 15:13:33 +0900609int dm9000_initialize(struct bd_info *bis)
Remy Bohmer7eefd922009-05-02 21:49:18 +0200610{
Marek Vasut9c5e9ca2022-04-13 04:15:32 +0200611 struct dm9000_priv *priv;
612 struct eth_device *dev;
613
614 priv = calloc(1, sizeof(*priv));
615 if (!priv)
616 return -ENOMEM;
617
618 dev = &priv->dev;
Remy Bohmer7eefd922009-05-02 21:49:18 +0200619
Marek Vasut1b5dd322022-04-13 04:15:34 +0200620 priv->base_io = (void __iomem *)DM9000_IO;
621 priv->base_data = (void __iomem *)DM9000_DATA;
622
Ben Warren8707f622009-10-21 21:53:39 -0700623 /* Load MAC address from EEPROM */
Marek Vasut9b65a962022-04-13 04:15:35 +0200624 dm9000_get_enetaddr(priv, dev->enetaddr);
Ben Warren8707f622009-10-21 21:53:39 -0700625
Remy Bohmer7eefd922009-05-02 21:49:18 +0200626 dev->init = dm9000_init;
627 dev->halt = dm9000_halt;
628 dev->send = dm9000_send;
Marek Vasut9b65a962022-04-13 04:15:35 +0200629 dev->recv = dm9000_recv;
Ben Whitten34fd6c92015-12-30 13:05:58 +0000630 strcpy(dev->name, "dm9000");
Remy Bohmer7eefd922009-05-02 21:49:18 +0200631
Marek Vasut9c5e9ca2022-04-13 04:15:32 +0200632 eth_register(&priv->dev);
Remy Bohmer7eefd922009-05-02 21:49:18 +0200633
634 return 0;
635}
Marek Vasut16c3e212022-04-13 04:15:37 +0200636#else /* ifdef CONFIG_DM_ETH */
637static int dm9000_start(struct udevice *dev)
638{
639 struct dm9000_priv *db = dev_get_priv(dev);
640 struct eth_pdata *pdata = dev_get_plat(dev);
641
642 return dm9000_init_common(db, pdata->enetaddr);
643}
644
645static void dm9000_stop(struct udevice *dev)
646{
647 struct dm9000_priv *db = dev_get_priv(dev);
648
649 dm9000_halt_common(db);
650}
651
652static int dm9000_send(struct udevice *dev, void *packet, int length)
653{
654 struct dm9000_priv *db = dev_get_priv(dev);
655 int ret;
656
657 ret = dm9000_send_common(db, packet, length);
658
659 return ret ? 0 : -ETIMEDOUT;
660}
661
662static int dm9000_recv(struct udevice *dev, int flags, uchar **packetp)
663{
664 struct dm9000_priv *db = dev_get_priv(dev);
665 uchar *data = net_rx_packets[0];
666 int ret;
667
668 ret = dm9000_recv_common(db, data);
669 if (ret)
670 *packetp = (void *)data;
671
672 return ret ? ret : -EAGAIN;
673}
674
675static int dm9000_write_hwaddr(struct udevice *dev)
676{
677 struct dm9000_priv *db = dev_get_priv(dev);
678 struct eth_pdata *pdata = dev_get_plat(dev);
679 int i, oft;
680
681 /* fill device MAC address registers */
682 for (i = 0, oft = DM9000_PAR; i < 6; i++, oft++)
683 dm9000_iow(db, oft, pdata->enetaddr[i]);
684
685 for (i = 0, oft = 0x16; i < 8; i++, oft++)
686 dm9000_iow(db, oft, 0xff);
687
688 /* read back mac, just to be sure */
689 for (i = 0, oft = 0x10; i < 6; i++, oft++)
690 debug("%02x:", dm9000_ior(db, oft));
691
692 debug("\n");
693
694 return 0;
695}
696
697static int dm9000_read_rom_hwaddr(struct udevice *dev)
698{
699 struct dm9000_priv *db = dev_get_priv(dev);
700 struct eth_pdata *pdata = dev_get_plat(dev);
701
702 dm9000_get_enetaddr(db, pdata->enetaddr);
703
704 return !is_valid_ethaddr(pdata->enetaddr);
705}
706
707static int dm9000_bind(struct udevice *dev)
708{
709 return device_set_name(dev, dev->name);
710}
711
712static int dm9000_of_to_plat(struct udevice *dev)
713{
714 struct dm9000_priv *db = dev_get_priv(dev);
715 struct eth_pdata *pdata = dev_get_plat(dev);
716
717 pdata->iobase = dev_read_addr_index(dev, 0);
718 db->base_io = (void __iomem *)pdata->iobase;
719 db->base_data = (void __iomem *)dev_read_addr_index(dev, 1);
720
721 return 0;
722}
723
724static const struct eth_ops dm9000_ops = {
725 .start = dm9000_start,
726 .stop = dm9000_stop,
727 .send = dm9000_send,
728 .recv = dm9000_recv,
729 .write_hwaddr = dm9000_write_hwaddr,
730 .read_rom_hwaddr = dm9000_read_rom_hwaddr,
731};
732
733static const struct udevice_id dm9000_ids[] = {
734 { .compatible = "davicom,dm9000" },
735 { }
736};
737
738U_BOOT_DRIVER(dm9000) = {
739 .name = "eth_dm9000",
740 .id = UCLASS_ETH,
741 .of_match = dm9000_ids,
742 .bind = dm9000_bind,
743 .of_to_plat = dm9000_of_to_plat,
744 .ops = &dm9000_ops,
745 .priv_auto = sizeof(struct dm9000_priv),
746 .plat_auto = sizeof(struct eth_pdata),
747 .flags = DM_FLAG_ALLOC_PRIV_DMA,
748};
749#endif