Sascha Hauer | a136527 | 2005-05-05 15:14:15 -0700 | [diff] [blame] | 1 | /* |
Ben Dooks | 41c340f | 2008-02-05 00:02:15 +0000 | [diff] [blame] | 2 | * Davicom DM9000 Fast Ethernet driver for Linux. |
Sascha Hauer | a136527 | 2005-05-05 15:14:15 -0700 | [diff] [blame] | 3 | * Copyright (C) 1997 Sten Wang |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or |
| 6 | * modify it under the terms of the GNU General Public License |
| 7 | * as published by the Free Software Foundation; either version 2 |
| 8 | * of the License, or (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
Ben Dooks | 41c340f | 2008-02-05 00:02:15 +0000 | [diff] [blame] | 15 | * (C) Copyright 1997-1998 DAVICOM Semiconductor,Inc. All Rights Reserved. |
Sascha Hauer | a136527 | 2005-05-05 15:14:15 -0700 | [diff] [blame] | 16 | * |
Ben Dooks | 41c340f | 2008-02-05 00:02:15 +0000 | [diff] [blame] | 17 | * Additional updates, Copyright: |
| 18 | * Ben Dooks <ben@simtec.co.uk> |
| 19 | * Sascha Hauer <s.hauer@pengutronix.de> |
Sascha Hauer | a136527 | 2005-05-05 15:14:15 -0700 | [diff] [blame] | 20 | */ |
| 21 | |
| 22 | #include <linux/module.h> |
| 23 | #include <linux/ioport.h> |
| 24 | #include <linux/netdevice.h> |
| 25 | #include <linux/etherdevice.h> |
| 26 | #include <linux/init.h> |
| 27 | #include <linux/skbuff.h> |
Sascha Hauer | a136527 | 2005-05-05 15:14:15 -0700 | [diff] [blame] | 28 | #include <linux/spinlock.h> |
| 29 | #include <linux/crc32.h> |
| 30 | #include <linux/mii.h> |
Ben Dooks | 7da9985 | 2008-02-05 00:02:06 +0000 | [diff] [blame] | 31 | #include <linux/ethtool.h> |
Sascha Hauer | a136527 | 2005-05-05 15:14:15 -0700 | [diff] [blame] | 32 | #include <linux/dm9000.h> |
| 33 | #include <linux/delay.h> |
Russell King | d052d1b | 2005-10-29 19:07:23 +0100 | [diff] [blame] | 34 | #include <linux/platform_device.h> |
Daniel Mack | 4e4fc05 | 2008-01-23 14:54:50 +0100 | [diff] [blame] | 35 | #include <linux/irq.h> |
Sascha Hauer | a136527 | 2005-05-05 15:14:15 -0700 | [diff] [blame] | 36 | |
| 37 | #include <asm/delay.h> |
| 38 | #include <asm/irq.h> |
| 39 | #include <asm/io.h> |
| 40 | |
| 41 | #include "dm9000.h" |
| 42 | |
| 43 | /* Board/System/Debug information/definition ---------------- */ |
| 44 | |
| 45 | #define DM9000_PHY 0x40 /* PHY address 0x01 */ |
| 46 | |
Sascha Hauer | a136527 | 2005-05-05 15:14:15 -0700 | [diff] [blame] | 47 | #define CARDNAME "dm9000" |
| 48 | #define PFX CARDNAME ": " |
Ben Dooks | 7da9985 | 2008-02-05 00:02:06 +0000 | [diff] [blame] | 49 | #define DRV_VERSION "1.30" |
Sascha Hauer | a136527 | 2005-05-05 15:14:15 -0700 | [diff] [blame] | 50 | |
Alex Landau | f40d24d | 2007-07-12 12:11:48 +0800 | [diff] [blame] | 51 | #ifdef CONFIG_BLACKFIN |
| 52 | #define readsb insb |
| 53 | #define readsw insw |
| 54 | #define readsl insl |
| 55 | #define writesb outsb |
| 56 | #define writesw outsw |
| 57 | #define writesl outsl |
Ben Dooks | 1a5f1c4 | 2008-02-05 00:02:04 +0000 | [diff] [blame] | 58 | #define DEFAULT_TRIGGER IRQF_TRIGGER_HIGH |
Alex Landau | f40d24d | 2007-07-12 12:11:48 +0800 | [diff] [blame] | 59 | #else |
Ben Dooks | 1a5f1c4 | 2008-02-05 00:02:04 +0000 | [diff] [blame] | 60 | #define DEFAULT_TRIGGER (0) |
Alex Landau | f40d24d | 2007-07-12 12:11:48 +0800 | [diff] [blame] | 61 | #endif |
| 62 | |
Sascha Hauer | a136527 | 2005-05-05 15:14:15 -0700 | [diff] [blame] | 63 | /* |
| 64 | * Transmit timeout, default 5 seconds. |
| 65 | */ |
| 66 | static int watchdog = 5000; |
| 67 | module_param(watchdog, int, 0400); |
| 68 | MODULE_PARM_DESC(watchdog, "transmit timeout in milliseconds"); |
| 69 | |
Ben Dooks | 9a2f037 | 2008-02-05 00:02:10 +0000 | [diff] [blame] | 70 | /* DM9000 register address locking. |
| 71 | * |
| 72 | * The DM9000 uses an address register to control where data written |
| 73 | * to the data register goes. This means that the address register |
| 74 | * must be preserved over interrupts or similar calls. |
| 75 | * |
| 76 | * During interrupt and other critical calls, a spinlock is used to |
| 77 | * protect the system, but the calls themselves save the address |
| 78 | * in the address register in case they are interrupting another |
| 79 | * access to the device. |
| 80 | * |
| 81 | * For general accesses a lock is provided so that calls which are |
| 82 | * allowed to sleep are serialised so that the address register does |
| 83 | * not need to be saved. This lock also serves to serialise access |
| 84 | * to the EEPROM and PHY access registers which are shared between |
| 85 | * these two devices. |
| 86 | */ |
| 87 | |
Sascha Hauer | a136527 | 2005-05-05 15:14:15 -0700 | [diff] [blame] | 88 | /* Structure/enum declaration ------------------------------- */ |
| 89 | typedef struct board_info { |
| 90 | |
| 91 | void __iomem *io_addr; /* Register I/O base address */ |
| 92 | void __iomem *io_data; /* Data I/O address */ |
| 93 | u16 irq; /* IRQ */ |
| 94 | |
| 95 | u16 tx_pkt_cnt; |
| 96 | u16 queue_pkt_len; |
| 97 | u16 queue_start_addr; |
| 98 | u16 dbug_cnt; |
| 99 | u8 io_mode; /* 0:word, 2:byte */ |
| 100 | u8 phy_addr; |
Ben Dooks | 33ba509 | 2008-02-05 00:02:01 +0000 | [diff] [blame] | 101 | unsigned int flags; |
Ben Dooks | 321f69a | 2008-02-05 00:02:08 +0000 | [diff] [blame] | 102 | unsigned int in_suspend :1; |
Sascha Hauer | a136527 | 2005-05-05 15:14:15 -0700 | [diff] [blame] | 103 | |
Ben Dooks | 5b2b4ff | 2008-02-05 00:02:03 +0000 | [diff] [blame] | 104 | int debug_level; |
| 105 | |
Sascha Hauer | a136527 | 2005-05-05 15:14:15 -0700 | [diff] [blame] | 106 | void (*inblk)(void __iomem *port, void *data, int length); |
| 107 | void (*outblk)(void __iomem *port, void *data, int length); |
| 108 | void (*dumpblk)(void __iomem *port, int length); |
| 109 | |
Ben Dooks | a76836f | 2008-02-05 00:02:02 +0000 | [diff] [blame] | 110 | struct device *dev; /* parent device */ |
| 111 | |
Sascha Hauer | a136527 | 2005-05-05 15:14:15 -0700 | [diff] [blame] | 112 | struct resource *addr_res; /* resources found */ |
| 113 | struct resource *data_res; |
| 114 | struct resource *addr_req; /* resources requested */ |
| 115 | struct resource *data_req; |
| 116 | struct resource *irq_res; |
| 117 | |
Ben Dooks | 9a2f037 | 2008-02-05 00:02:10 +0000 | [diff] [blame] | 118 | struct mutex addr_lock; /* phy and eeprom access lock */ |
| 119 | |
Sascha Hauer | a136527 | 2005-05-05 15:14:15 -0700 | [diff] [blame] | 120 | spinlock_t lock; |
| 121 | |
| 122 | struct mii_if_info mii; |
| 123 | u32 msg_enable; |
| 124 | } board_info_t; |
| 125 | |
Ben Dooks | 5b2b4ff | 2008-02-05 00:02:03 +0000 | [diff] [blame] | 126 | /* debug code */ |
| 127 | |
| 128 | #define dm9000_dbg(db, lev, msg...) do { \ |
| 129 | if ((lev) < CONFIG_DM9000_DEBUGLEVEL && \ |
| 130 | (lev) < db->debug_level) { \ |
| 131 | dev_dbg(db->dev, msg); \ |
| 132 | } \ |
| 133 | } while (0) |
| 134 | |
Ben Dooks | 7da9985 | 2008-02-05 00:02:06 +0000 | [diff] [blame] | 135 | static inline board_info_t *to_dm9000_board(struct net_device *dev) |
| 136 | { |
| 137 | return dev->priv; |
| 138 | } |
| 139 | |
Sascha Hauer | a136527 | 2005-05-05 15:14:15 -0700 | [diff] [blame] | 140 | /* function declaration ------------------------------------- */ |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 141 | static int dm9000_probe(struct platform_device *); |
Sascha Hauer | a136527 | 2005-05-05 15:14:15 -0700 | [diff] [blame] | 142 | static int dm9000_open(struct net_device *); |
| 143 | static int dm9000_start_xmit(struct sk_buff *, struct net_device *); |
| 144 | static int dm9000_stop(struct net_device *); |
Sascha Hauer | a136527 | 2005-05-05 15:14:15 -0700 | [diff] [blame] | 145 | |
Sascha Hauer | a136527 | 2005-05-05 15:14:15 -0700 | [diff] [blame] | 146 | static void dm9000_init_dm9000(struct net_device *); |
| 147 | |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 148 | static irqreturn_t dm9000_interrupt(int, void *); |
Sascha Hauer | a136527 | 2005-05-05 15:14:15 -0700 | [diff] [blame] | 149 | |
| 150 | static int dm9000_phy_read(struct net_device *dev, int phyaddr_unsused, int reg); |
| 151 | static void dm9000_phy_write(struct net_device *dev, int phyaddr_unused, int reg, |
| 152 | int value); |
Ben Dooks | 86c62fa | 2008-02-05 00:02:09 +0000 | [diff] [blame] | 153 | |
Ben Dooks | 29d52e5 | 2008-02-05 00:02:11 +0000 | [diff] [blame] | 154 | static void dm9000_read_eeprom(board_info_t *, int addr, u8 *to); |
| 155 | static void dm9000_write_eeprom(board_info_t *, int addr, u8 *dp); |
Sascha Hauer | a136527 | 2005-05-05 15:14:15 -0700 | [diff] [blame] | 156 | static void dm9000_rx(struct net_device *); |
| 157 | static void dm9000_hash_table(struct net_device *); |
| 158 | |
Sascha Hauer | a136527 | 2005-05-05 15:14:15 -0700 | [diff] [blame] | 159 | /* DM9000 network board routine ---------------------------- */ |
| 160 | |
| 161 | static void |
| 162 | dm9000_reset(board_info_t * db) |
| 163 | { |
Ben Dooks | a76836f | 2008-02-05 00:02:02 +0000 | [diff] [blame] | 164 | dev_dbg(db->dev, "resetting device\n"); |
| 165 | |
Sascha Hauer | a136527 | 2005-05-05 15:14:15 -0700 | [diff] [blame] | 166 | /* RESET device */ |
| 167 | writeb(DM9000_NCR, db->io_addr); |
| 168 | udelay(200); |
| 169 | writeb(NCR_RST, db->io_data); |
| 170 | udelay(200); |
| 171 | } |
| 172 | |
| 173 | /* |
| 174 | * Read a byte from I/O port |
| 175 | */ |
| 176 | static u8 |
| 177 | ior(board_info_t * db, int reg) |
| 178 | { |
| 179 | writeb(reg, db->io_addr); |
| 180 | return readb(db->io_data); |
| 181 | } |
| 182 | |
| 183 | /* |
| 184 | * Write a byte to I/O port |
| 185 | */ |
| 186 | |
| 187 | static void |
| 188 | iow(board_info_t * db, int reg, int value) |
| 189 | { |
| 190 | writeb(reg, db->io_addr); |
| 191 | writeb(value, db->io_data); |
| 192 | } |
| 193 | |
| 194 | /* routines for sending block to chip */ |
| 195 | |
| 196 | static void dm9000_outblk_8bit(void __iomem *reg, void *data, int count) |
| 197 | { |
| 198 | writesb(reg, data, count); |
| 199 | } |
| 200 | |
| 201 | static void dm9000_outblk_16bit(void __iomem *reg, void *data, int count) |
| 202 | { |
| 203 | writesw(reg, data, (count+1) >> 1); |
| 204 | } |
| 205 | |
| 206 | static void dm9000_outblk_32bit(void __iomem *reg, void *data, int count) |
| 207 | { |
| 208 | writesl(reg, data, (count+3) >> 2); |
| 209 | } |
| 210 | |
| 211 | /* input block from chip to memory */ |
| 212 | |
| 213 | static void dm9000_inblk_8bit(void __iomem *reg, void *data, int count) |
| 214 | { |
Sascha Hauer | 5f6b551 | 2005-06-20 15:32:51 -0700 | [diff] [blame] | 215 | readsb(reg, data, count); |
Sascha Hauer | a136527 | 2005-05-05 15:14:15 -0700 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | |
| 219 | static void dm9000_inblk_16bit(void __iomem *reg, void *data, int count) |
| 220 | { |
| 221 | readsw(reg, data, (count+1) >> 1); |
| 222 | } |
| 223 | |
| 224 | static void dm9000_inblk_32bit(void __iomem *reg, void *data, int count) |
| 225 | { |
| 226 | readsl(reg, data, (count+3) >> 2); |
| 227 | } |
| 228 | |
| 229 | /* dump block from chip to null */ |
| 230 | |
| 231 | static void dm9000_dumpblk_8bit(void __iomem *reg, int count) |
| 232 | { |
| 233 | int i; |
| 234 | int tmp; |
| 235 | |
| 236 | for (i = 0; i < count; i++) |
| 237 | tmp = readb(reg); |
| 238 | } |
| 239 | |
| 240 | static void dm9000_dumpblk_16bit(void __iomem *reg, int count) |
| 241 | { |
| 242 | int i; |
| 243 | int tmp; |
| 244 | |
| 245 | count = (count + 1) >> 1; |
| 246 | |
| 247 | for (i = 0; i < count; i++) |
| 248 | tmp = readw(reg); |
| 249 | } |
| 250 | |
| 251 | static void dm9000_dumpblk_32bit(void __iomem *reg, int count) |
| 252 | { |
| 253 | int i; |
| 254 | int tmp; |
| 255 | |
| 256 | count = (count + 3) >> 2; |
| 257 | |
| 258 | for (i = 0; i < count; i++) |
| 259 | tmp = readl(reg); |
| 260 | } |
| 261 | |
| 262 | /* dm9000_set_io |
| 263 | * |
| 264 | * select the specified set of io routines to use with the |
| 265 | * device |
| 266 | */ |
| 267 | |
| 268 | static void dm9000_set_io(struct board_info *db, int byte_width) |
| 269 | { |
| 270 | /* use the size of the data resource to work out what IO |
| 271 | * routines we want to use |
| 272 | */ |
| 273 | |
| 274 | switch (byte_width) { |
| 275 | case 1: |
| 276 | db->dumpblk = dm9000_dumpblk_8bit; |
| 277 | db->outblk = dm9000_outblk_8bit; |
| 278 | db->inblk = dm9000_inblk_8bit; |
| 279 | break; |
| 280 | |
Sascha Hauer | a136527 | 2005-05-05 15:14:15 -0700 | [diff] [blame] | 281 | |
| 282 | case 3: |
Ben Dooks | a76836f | 2008-02-05 00:02:02 +0000 | [diff] [blame] | 283 | dev_dbg(db->dev, ": 3 byte IO, falling back to 16bit\n"); |
| 284 | case 2: |
Sascha Hauer | a136527 | 2005-05-05 15:14:15 -0700 | [diff] [blame] | 285 | db->dumpblk = dm9000_dumpblk_16bit; |
| 286 | db->outblk = dm9000_outblk_16bit; |
| 287 | db->inblk = dm9000_inblk_16bit; |
| 288 | break; |
| 289 | |
| 290 | case 4: |
| 291 | default: |
| 292 | db->dumpblk = dm9000_dumpblk_32bit; |
| 293 | db->outblk = dm9000_outblk_32bit; |
| 294 | db->inblk = dm9000_inblk_32bit; |
| 295 | break; |
| 296 | } |
| 297 | } |
| 298 | |
| 299 | |
| 300 | /* Our watchdog timed out. Called by the networking layer */ |
| 301 | static void dm9000_timeout(struct net_device *dev) |
| 302 | { |
| 303 | board_info_t *db = (board_info_t *) dev->priv; |
| 304 | u8 reg_save; |
| 305 | unsigned long flags; |
| 306 | |
| 307 | /* Save previous register address */ |
| 308 | reg_save = readb(db->io_addr); |
Ben Dooks | 9ef9ac5 | 2005-07-23 17:25:18 +0100 | [diff] [blame] | 309 | spin_lock_irqsave(&db->lock,flags); |
Sascha Hauer | a136527 | 2005-05-05 15:14:15 -0700 | [diff] [blame] | 310 | |
| 311 | netif_stop_queue(dev); |
| 312 | dm9000_reset(db); |
| 313 | dm9000_init_dm9000(dev); |
| 314 | /* We can accept TX packets again */ |
| 315 | dev->trans_start = jiffies; |
| 316 | netif_wake_queue(dev); |
| 317 | |
| 318 | /* Restore previous register address */ |
| 319 | writeb(reg_save, db->io_addr); |
Ben Dooks | 9ef9ac5 | 2005-07-23 17:25:18 +0100 | [diff] [blame] | 320 | spin_unlock_irqrestore(&db->lock,flags); |
Sascha Hauer | a136527 | 2005-05-05 15:14:15 -0700 | [diff] [blame] | 321 | } |
| 322 | |
Kevin Hao | 2fd0e33 | 2006-08-14 23:00:15 -0700 | [diff] [blame] | 323 | #ifdef CONFIG_NET_POLL_CONTROLLER |
| 324 | /* |
| 325 | *Used by netconsole |
| 326 | */ |
| 327 | static void dm9000_poll_controller(struct net_device *dev) |
| 328 | { |
| 329 | disable_irq(dev->irq); |
Al Viro | 2843114 | 2006-10-08 15:00:12 +0100 | [diff] [blame] | 330 | dm9000_interrupt(dev->irq,dev); |
Kevin Hao | 2fd0e33 | 2006-08-14 23:00:15 -0700 | [diff] [blame] | 331 | enable_irq(dev->irq); |
| 332 | } |
| 333 | #endif |
Sascha Hauer | a136527 | 2005-05-05 15:14:15 -0700 | [diff] [blame] | 334 | |
Ben Dooks | 7da9985 | 2008-02-05 00:02:06 +0000 | [diff] [blame] | 335 | /* ethtool ops */ |
| 336 | |
| 337 | static void dm9000_get_drvinfo(struct net_device *dev, |
| 338 | struct ethtool_drvinfo *info) |
| 339 | { |
| 340 | board_info_t *dm = to_dm9000_board(dev); |
| 341 | |
| 342 | strcpy(info->driver, CARDNAME); |
| 343 | strcpy(info->version, DRV_VERSION); |
| 344 | strcpy(info->bus_info, to_platform_device(dm->dev)->name); |
| 345 | } |
| 346 | |
Ben Dooks | e662ee0 | 2008-02-05 00:02:12 +0000 | [diff] [blame] | 347 | static u32 dm9000_get_msglevel(struct net_device *dev) |
| 348 | { |
| 349 | board_info_t *dm = to_dm9000_board(dev); |
| 350 | |
| 351 | return dm->msg_enable; |
| 352 | } |
| 353 | |
| 354 | static void dm9000_set_msglevel(struct net_device *dev, u32 value) |
| 355 | { |
| 356 | board_info_t *dm = to_dm9000_board(dev); |
| 357 | |
| 358 | dm->msg_enable = value; |
| 359 | } |
| 360 | |
Ben Dooks | 7da9985 | 2008-02-05 00:02:06 +0000 | [diff] [blame] | 361 | static int dm9000_get_settings(struct net_device *dev, struct ethtool_cmd *cmd) |
| 362 | { |
| 363 | board_info_t *dm = to_dm9000_board(dev); |
Ben Dooks | 7da9985 | 2008-02-05 00:02:06 +0000 | [diff] [blame] | 364 | |
Ben Dooks | 7da9985 | 2008-02-05 00:02:06 +0000 | [diff] [blame] | 365 | mii_ethtool_gset(&dm->mii, cmd); |
Ben Dooks | 7da9985 | 2008-02-05 00:02:06 +0000 | [diff] [blame] | 366 | return 0; |
| 367 | } |
| 368 | |
| 369 | static int dm9000_set_settings(struct net_device *dev, struct ethtool_cmd *cmd) |
| 370 | { |
| 371 | board_info_t *dm = to_dm9000_board(dev); |
Ben Dooks | 7da9985 | 2008-02-05 00:02:06 +0000 | [diff] [blame] | 372 | |
Ben Dooks | 9a2f037 | 2008-02-05 00:02:10 +0000 | [diff] [blame] | 373 | return mii_ethtool_sset(&dm->mii, cmd); |
Ben Dooks | 7da9985 | 2008-02-05 00:02:06 +0000 | [diff] [blame] | 374 | } |
| 375 | |
| 376 | static int dm9000_nway_reset(struct net_device *dev) |
| 377 | { |
| 378 | board_info_t *dm = to_dm9000_board(dev); |
| 379 | return mii_nway_restart(&dm->mii); |
| 380 | } |
| 381 | |
| 382 | static u32 dm9000_get_link(struct net_device *dev) |
| 383 | { |
| 384 | board_info_t *dm = to_dm9000_board(dev); |
| 385 | return mii_link_ok(&dm->mii); |
| 386 | } |
| 387 | |
Ben Dooks | 29d52e5 | 2008-02-05 00:02:11 +0000 | [diff] [blame] | 388 | #define DM_EEPROM_MAGIC (0x444D394B) |
| 389 | |
| 390 | static int dm9000_get_eeprom_len(struct net_device *dev) |
| 391 | { |
| 392 | return 128; |
| 393 | } |
| 394 | |
| 395 | static int dm9000_get_eeprom(struct net_device *dev, |
| 396 | struct ethtool_eeprom *ee, u8 *data) |
| 397 | { |
| 398 | board_info_t *dm = to_dm9000_board(dev); |
| 399 | int offset = ee->offset; |
| 400 | int len = ee->len; |
| 401 | int i; |
| 402 | |
| 403 | /* EEPROM access is aligned to two bytes */ |
| 404 | |
| 405 | if ((len & 1) != 0 || (offset & 1) != 0) |
| 406 | return -EINVAL; |
| 407 | |
| 408 | ee->magic = DM_EEPROM_MAGIC; |
| 409 | |
| 410 | for (i = 0; i < len; i += 2) |
| 411 | dm9000_read_eeprom(dm, (offset + i) / 2, data + i); |
| 412 | |
| 413 | return 0; |
| 414 | } |
| 415 | |
| 416 | static int dm9000_set_eeprom(struct net_device *dev, |
| 417 | struct ethtool_eeprom *ee, u8 *data) |
| 418 | { |
| 419 | board_info_t *dm = to_dm9000_board(dev); |
| 420 | int offset = ee->offset; |
| 421 | int len = ee->len; |
| 422 | int i; |
| 423 | |
| 424 | /* EEPROM access is aligned to two bytes */ |
| 425 | |
| 426 | if ((len & 1) != 0 || (offset & 1) != 0) |
| 427 | return -EINVAL; |
| 428 | |
| 429 | if (ee->magic != DM_EEPROM_MAGIC) |
| 430 | return -EINVAL; |
| 431 | |
| 432 | for (i = 0; i < len; i += 2) |
| 433 | dm9000_write_eeprom(dm, (offset + i) / 2, data + i); |
| 434 | |
| 435 | return 0; |
| 436 | } |
| 437 | |
Ben Dooks | 7da9985 | 2008-02-05 00:02:06 +0000 | [diff] [blame] | 438 | static const struct ethtool_ops dm9000_ethtool_ops = { |
| 439 | .get_drvinfo = dm9000_get_drvinfo, |
| 440 | .get_settings = dm9000_get_settings, |
| 441 | .set_settings = dm9000_set_settings, |
Ben Dooks | e662ee0 | 2008-02-05 00:02:12 +0000 | [diff] [blame] | 442 | .get_msglevel = dm9000_get_msglevel, |
| 443 | .set_msglevel = dm9000_set_msglevel, |
Ben Dooks | 7da9985 | 2008-02-05 00:02:06 +0000 | [diff] [blame] | 444 | .nway_reset = dm9000_nway_reset, |
| 445 | .get_link = dm9000_get_link, |
Ben Dooks | 29d52e5 | 2008-02-05 00:02:11 +0000 | [diff] [blame] | 446 | .get_eeprom_len = dm9000_get_eeprom_len, |
| 447 | .get_eeprom = dm9000_get_eeprom, |
| 448 | .set_eeprom = dm9000_set_eeprom, |
Ben Dooks | 7da9985 | 2008-02-05 00:02:06 +0000 | [diff] [blame] | 449 | }; |
| 450 | |
| 451 | |
Sascha Hauer | a136527 | 2005-05-05 15:14:15 -0700 | [diff] [blame] | 452 | /* dm9000_release_board |
| 453 | * |
| 454 | * release a board, and any mapped resources |
| 455 | */ |
| 456 | |
| 457 | static void |
| 458 | dm9000_release_board(struct platform_device *pdev, struct board_info *db) |
| 459 | { |
| 460 | if (db->data_res == NULL) { |
| 461 | if (db->addr_res != NULL) |
| 462 | release_mem_region((unsigned long)db->io_addr, 4); |
| 463 | return; |
| 464 | } |
| 465 | |
| 466 | /* unmap our resources */ |
| 467 | |
| 468 | iounmap(db->io_addr); |
| 469 | iounmap(db->io_data); |
| 470 | |
| 471 | /* release the resources */ |
| 472 | |
| 473 | if (db->data_req != NULL) { |
| 474 | release_resource(db->data_req); |
| 475 | kfree(db->data_req); |
| 476 | } |
| 477 | |
Dirk Opfer | 5198548 | 2006-09-06 19:53:32 +0200 | [diff] [blame] | 478 | if (db->addr_req != NULL) { |
| 479 | release_resource(db->addr_req); |
Sascha Hauer | a136527 | 2005-05-05 15:14:15 -0700 | [diff] [blame] | 480 | kfree(db->addr_req); |
| 481 | } |
| 482 | } |
| 483 | |
| 484 | #define res_size(_r) (((_r)->end - (_r)->start) + 1) |
| 485 | |
| 486 | /* |
| 487 | * Search DM9000 board, allocate space and register it |
| 488 | */ |
| 489 | static int |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 490 | dm9000_probe(struct platform_device *pdev) |
Sascha Hauer | a136527 | 2005-05-05 15:14:15 -0700 | [diff] [blame] | 491 | { |
Sascha Hauer | a136527 | 2005-05-05 15:14:15 -0700 | [diff] [blame] | 492 | struct dm9000_plat_data *pdata = pdev->dev.platform_data; |
| 493 | struct board_info *db; /* Point a board information structure */ |
| 494 | struct net_device *ndev; |
| 495 | unsigned long base; |
| 496 | int ret = 0; |
| 497 | int iosize; |
| 498 | int i; |
| 499 | u32 id_val; |
| 500 | |
Sascha Hauer | a136527 | 2005-05-05 15:14:15 -0700 | [diff] [blame] | 501 | /* Init network device */ |
| 502 | ndev = alloc_etherdev(sizeof (struct board_info)); |
| 503 | if (!ndev) { |
Ben Dooks | a76836f | 2008-02-05 00:02:02 +0000 | [diff] [blame] | 504 | dev_err(&pdev->dev, "could not allocate device.\n"); |
Sascha Hauer | a136527 | 2005-05-05 15:14:15 -0700 | [diff] [blame] | 505 | return -ENOMEM; |
| 506 | } |
| 507 | |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 508 | SET_NETDEV_DEV(ndev, &pdev->dev); |
Sascha Hauer | a136527 | 2005-05-05 15:14:15 -0700 | [diff] [blame] | 509 | |
Ben Dooks | a76836f | 2008-02-05 00:02:02 +0000 | [diff] [blame] | 510 | dev_dbg(&pdev->dev, "dm9000_probe()"); |
Sascha Hauer | a136527 | 2005-05-05 15:14:15 -0700 | [diff] [blame] | 511 | |
| 512 | /* setup board info structure */ |
| 513 | db = (struct board_info *) ndev->priv; |
| 514 | memset(db, 0, sizeof (*db)); |
| 515 | |
Ben Dooks | a76836f | 2008-02-05 00:02:02 +0000 | [diff] [blame] | 516 | db->dev = &pdev->dev; |
| 517 | |
Ben Dooks | 9ef9ac5 | 2005-07-23 17:25:18 +0100 | [diff] [blame] | 518 | spin_lock_init(&db->lock); |
Ben Dooks | 9a2f037 | 2008-02-05 00:02:10 +0000 | [diff] [blame] | 519 | mutex_init(&db->addr_lock); |
Ben Dooks | 9ef9ac5 | 2005-07-23 17:25:18 +0100 | [diff] [blame] | 520 | |
Sascha Hauer | a136527 | 2005-05-05 15:14:15 -0700 | [diff] [blame] | 521 | if (pdev->num_resources < 2) { |
| 522 | ret = -ENODEV; |
| 523 | goto out; |
Ben Dooks | b4ed03f | 2006-06-13 23:47:19 +0100 | [diff] [blame] | 524 | } else if (pdev->num_resources == 2) { |
Sascha Hauer | a136527 | 2005-05-05 15:14:15 -0700 | [diff] [blame] | 525 | base = pdev->resource[0].start; |
| 526 | |
| 527 | if (!request_mem_region(base, 4, ndev->name)) { |
| 528 | ret = -EBUSY; |
| 529 | goto out; |
| 530 | } |
| 531 | |
| 532 | ndev->base_addr = base; |
| 533 | ndev->irq = pdev->resource[1].start; |
Ben Dooks | b4ed03f | 2006-06-13 23:47:19 +0100 | [diff] [blame] | 534 | db->io_addr = (void __iomem *)base; |
| 535 | db->io_data = (void __iomem *)(base + 4); |
Sascha Hauer | a136527 | 2005-05-05 15:14:15 -0700 | [diff] [blame] | 536 | |
Alex Landau | f40d24d | 2007-07-12 12:11:48 +0800 | [diff] [blame] | 537 | /* ensure at least we have a default set of IO routines */ |
| 538 | dm9000_set_io(db, 2); |
| 539 | |
Ben Dooks | b4ed03f | 2006-06-13 23:47:19 +0100 | [diff] [blame] | 540 | } else { |
Sascha Hauer | a136527 | 2005-05-05 15:14:15 -0700 | [diff] [blame] | 541 | db->addr_res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 542 | db->data_res = platform_get_resource(pdev, IORESOURCE_MEM, 1); |
| 543 | db->irq_res = platform_get_resource(pdev, IORESOURCE_IRQ, 0); |
| 544 | |
Ben Dooks | b4ed03f | 2006-06-13 23:47:19 +0100 | [diff] [blame] | 545 | if (db->addr_res == NULL || db->data_res == NULL || |
| 546 | db->irq_res == NULL) { |
Ben Dooks | a76836f | 2008-02-05 00:02:02 +0000 | [diff] [blame] | 547 | dev_err(db->dev, "insufficient resources\n"); |
Sascha Hauer | a136527 | 2005-05-05 15:14:15 -0700 | [diff] [blame] | 548 | ret = -ENOENT; |
| 549 | goto out; |
| 550 | } |
| 551 | |
| 552 | i = res_size(db->addr_res); |
| 553 | db->addr_req = request_mem_region(db->addr_res->start, i, |
| 554 | pdev->name); |
| 555 | |
| 556 | if (db->addr_req == NULL) { |
Ben Dooks | a76836f | 2008-02-05 00:02:02 +0000 | [diff] [blame] | 557 | dev_err(db->dev, "cannot claim address reg area\n"); |
Sascha Hauer | a136527 | 2005-05-05 15:14:15 -0700 | [diff] [blame] | 558 | ret = -EIO; |
| 559 | goto out; |
| 560 | } |
| 561 | |
| 562 | db->io_addr = ioremap(db->addr_res->start, i); |
| 563 | |
| 564 | if (db->io_addr == NULL) { |
Ben Dooks | a76836f | 2008-02-05 00:02:02 +0000 | [diff] [blame] | 565 | dev_err(db->dev, "failed to ioremap address reg\n"); |
Sascha Hauer | a136527 | 2005-05-05 15:14:15 -0700 | [diff] [blame] | 566 | ret = -EINVAL; |
| 567 | goto out; |
| 568 | } |
| 569 | |
| 570 | iosize = res_size(db->data_res); |
| 571 | db->data_req = request_mem_region(db->data_res->start, iosize, |
| 572 | pdev->name); |
| 573 | |
| 574 | if (db->data_req == NULL) { |
Ben Dooks | a76836f | 2008-02-05 00:02:02 +0000 | [diff] [blame] | 575 | dev_err(db->dev, "cannot claim data reg area\n"); |
Sascha Hauer | a136527 | 2005-05-05 15:14:15 -0700 | [diff] [blame] | 576 | ret = -EIO; |
| 577 | goto out; |
| 578 | } |
| 579 | |
| 580 | db->io_data = ioremap(db->data_res->start, iosize); |
| 581 | |
| 582 | if (db->io_data == NULL) { |
Ben Dooks | a76836f | 2008-02-05 00:02:02 +0000 | [diff] [blame] | 583 | dev_err(db->dev,"failed to ioremap data reg\n"); |
Sascha Hauer | a136527 | 2005-05-05 15:14:15 -0700 | [diff] [blame] | 584 | ret = -EINVAL; |
| 585 | goto out; |
| 586 | } |
| 587 | |
| 588 | /* fill in parameters for net-dev structure */ |
| 589 | |
| 590 | ndev->base_addr = (unsigned long)db->io_addr; |
| 591 | ndev->irq = db->irq_res->start; |
| 592 | |
| 593 | /* ensure at least we have a default set of IO routines */ |
| 594 | dm9000_set_io(db, iosize); |
Sascha Hauer | a136527 | 2005-05-05 15:14:15 -0700 | [diff] [blame] | 595 | } |
| 596 | |
| 597 | /* check to see if anything is being over-ridden */ |
| 598 | if (pdata != NULL) { |
| 599 | /* check to see if the driver wants to over-ride the |
| 600 | * default IO width */ |
| 601 | |
| 602 | if (pdata->flags & DM9000_PLATF_8BITONLY) |
| 603 | dm9000_set_io(db, 1); |
| 604 | |
| 605 | if (pdata->flags & DM9000_PLATF_16BITONLY) |
| 606 | dm9000_set_io(db, 2); |
| 607 | |
| 608 | if (pdata->flags & DM9000_PLATF_32BITONLY) |
| 609 | dm9000_set_io(db, 4); |
| 610 | |
| 611 | /* check to see if there are any IO routine |
| 612 | * over-rides */ |
| 613 | |
| 614 | if (pdata->inblk != NULL) |
| 615 | db->inblk = pdata->inblk; |
| 616 | |
| 617 | if (pdata->outblk != NULL) |
| 618 | db->outblk = pdata->outblk; |
| 619 | |
| 620 | if (pdata->dumpblk != NULL) |
| 621 | db->dumpblk = pdata->dumpblk; |
Ben Dooks | 33ba509 | 2008-02-05 00:02:01 +0000 | [diff] [blame] | 622 | |
| 623 | db->flags = pdata->flags; |
Sascha Hauer | a136527 | 2005-05-05 15:14:15 -0700 | [diff] [blame] | 624 | } |
| 625 | |
| 626 | dm9000_reset(db); |
| 627 | |
| 628 | /* try two times, DM9000 sometimes gets the first read wrong */ |
| 629 | for (i = 0; i < 2; i++) { |
| 630 | id_val = ior(db, DM9000_VIDL); |
| 631 | id_val |= (u32)ior(db, DM9000_VIDH) << 8; |
| 632 | id_val |= (u32)ior(db, DM9000_PIDL) << 16; |
| 633 | id_val |= (u32)ior(db, DM9000_PIDH) << 24; |
| 634 | |
| 635 | if (id_val == DM9000_ID) |
| 636 | break; |
Ben Dooks | a76836f | 2008-02-05 00:02:02 +0000 | [diff] [blame] | 637 | dev_err(db->dev, "read wrong id 0x%08x\n", id_val); |
Sascha Hauer | a136527 | 2005-05-05 15:14:15 -0700 | [diff] [blame] | 638 | } |
| 639 | |
| 640 | if (id_val != DM9000_ID) { |
Ben Dooks | a76836f | 2008-02-05 00:02:02 +0000 | [diff] [blame] | 641 | dev_err(db->dev, "wrong id: 0x%08x\n", id_val); |
Mike Rapoport | 418d6f8 | 2007-10-18 09:23:11 +0200 | [diff] [blame] | 642 | ret = -ENODEV; |
| 643 | goto out; |
Sascha Hauer | a136527 | 2005-05-05 15:14:15 -0700 | [diff] [blame] | 644 | } |
| 645 | |
| 646 | /* from this point we assume that we have found a DM9000 */ |
| 647 | |
| 648 | /* driver system function */ |
| 649 | ether_setup(ndev); |
| 650 | |
| 651 | ndev->open = &dm9000_open; |
| 652 | ndev->hard_start_xmit = &dm9000_start_xmit; |
| 653 | ndev->tx_timeout = &dm9000_timeout; |
| 654 | ndev->watchdog_timeo = msecs_to_jiffies(watchdog); |
| 655 | ndev->stop = &dm9000_stop; |
Sascha Hauer | a136527 | 2005-05-05 15:14:15 -0700 | [diff] [blame] | 656 | ndev->set_multicast_list = &dm9000_hash_table; |
Ben Dooks | 7da9985 | 2008-02-05 00:02:06 +0000 | [diff] [blame] | 657 | ndev->ethtool_ops = &dm9000_ethtool_ops; |
| 658 | |
Kevin Hao | 2fd0e33 | 2006-08-14 23:00:15 -0700 | [diff] [blame] | 659 | #ifdef CONFIG_NET_POLL_CONTROLLER |
| 660 | ndev->poll_controller = &dm9000_poll_controller; |
| 661 | #endif |
Sascha Hauer | a136527 | 2005-05-05 15:14:15 -0700 | [diff] [blame] | 662 | |
Sascha Hauer | a136527 | 2005-05-05 15:14:15 -0700 | [diff] [blame] | 663 | db->msg_enable = NETIF_MSG_LINK; |
| 664 | db->mii.phy_id_mask = 0x1f; |
| 665 | db->mii.reg_num_mask = 0x1f; |
| 666 | db->mii.force_media = 0; |
| 667 | db->mii.full_duplex = 0; |
| 668 | db->mii.dev = ndev; |
| 669 | db->mii.mdio_read = dm9000_phy_read; |
| 670 | db->mii.mdio_write = dm9000_phy_write; |
| 671 | |
Ben Dooks | 86c62fa | 2008-02-05 00:02:09 +0000 | [diff] [blame] | 672 | /* try reading the node address from the attached EEPROM */ |
| 673 | for (i = 0; i < 6; i += 2) |
| 674 | dm9000_read_eeprom(db, i / 2, ndev->dev_addr+i); |
Sascha Hauer | a136527 | 2005-05-05 15:14:15 -0700 | [diff] [blame] | 675 | |
Ben Dooks | 5b55dda | 2006-06-13 23:50:15 +0100 | [diff] [blame] | 676 | if (!is_valid_ether_addr(ndev->dev_addr)) { |
| 677 | /* try reading from mac */ |
| 678 | |
| 679 | for (i = 0; i < 6; i++) |
| 680 | ndev->dev_addr[i] = ior(db, i+DM9000_PAR); |
| 681 | } |
| 682 | |
Sascha Hauer | a136527 | 2005-05-05 15:14:15 -0700 | [diff] [blame] | 683 | if (!is_valid_ether_addr(ndev->dev_addr)) |
Ben Dooks | a76836f | 2008-02-05 00:02:02 +0000 | [diff] [blame] | 684 | dev_warn(db->dev, "%s: Invalid ethernet MAC address. Please " |
| 685 | "set using ifconfig\n", ndev->name); |
Sascha Hauer | a136527 | 2005-05-05 15:14:15 -0700 | [diff] [blame] | 686 | |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 687 | platform_set_drvdata(pdev, ndev); |
Sascha Hauer | a136527 | 2005-05-05 15:14:15 -0700 | [diff] [blame] | 688 | ret = register_netdev(ndev); |
| 689 | |
| 690 | if (ret == 0) { |
Joe Perches | 0795af5 | 2007-10-03 17:59:30 -0700 | [diff] [blame] | 691 | DECLARE_MAC_BUF(mac); |
| 692 | printk("%s: dm9000 at %p,%p IRQ %d MAC: %s\n", |
| 693 | ndev->name, db->io_addr, db->io_data, ndev->irq, |
| 694 | print_mac(mac, ndev->dev_addr)); |
Sascha Hauer | a136527 | 2005-05-05 15:14:15 -0700 | [diff] [blame] | 695 | } |
| 696 | return 0; |
| 697 | |
Mike Rapoport | 418d6f8 | 2007-10-18 09:23:11 +0200 | [diff] [blame] | 698 | out: |
Ben Dooks | a76836f | 2008-02-05 00:02:02 +0000 | [diff] [blame] | 699 | dev_err(db->dev, "not found (%d).\n", ret); |
Sascha Hauer | a136527 | 2005-05-05 15:14:15 -0700 | [diff] [blame] | 700 | |
| 701 | dm9000_release_board(pdev, db); |
Ben Dooks | 9fd9f9b | 2007-05-07 11:13:25 +0000 | [diff] [blame] | 702 | free_netdev(ndev); |
Sascha Hauer | a136527 | 2005-05-05 15:14:15 -0700 | [diff] [blame] | 703 | |
| 704 | return ret; |
| 705 | } |
| 706 | |
| 707 | /* |
| 708 | * Open the interface. |
| 709 | * The interface is opened whenever "ifconfig" actives it. |
| 710 | */ |
| 711 | static int |
| 712 | dm9000_open(struct net_device *dev) |
| 713 | { |
| 714 | board_info_t *db = (board_info_t *) dev->priv; |
Ben Dooks | 1a5f1c4 | 2008-02-05 00:02:04 +0000 | [diff] [blame] | 715 | unsigned long irqflags = db->irq_res->flags & IRQF_TRIGGER_MASK; |
Sascha Hauer | a136527 | 2005-05-05 15:14:15 -0700 | [diff] [blame] | 716 | |
Ben Dooks | c991d16 | 2008-02-05 00:02:16 +0000 | [diff] [blame] | 717 | if (netif_msg_ifup(db)) |
| 718 | dev_dbg(db->dev, "enabling %s\n", dev->name); |
Sascha Hauer | a136527 | 2005-05-05 15:14:15 -0700 | [diff] [blame] | 719 | |
Ben Dooks | 1a5f1c4 | 2008-02-05 00:02:04 +0000 | [diff] [blame] | 720 | /* If there is no IRQ type specified, default to something that |
| 721 | * may work, and tell the user that this is a problem */ |
| 722 | |
| 723 | if (irqflags == IRQF_TRIGGER_NONE) { |
| 724 | dev_warn(db->dev, "WARNING: no IRQ resource flags set.\n"); |
| 725 | irqflags = DEFAULT_TRIGGER; |
| 726 | } |
| 727 | |
| 728 | irqflags |= IRQF_SHARED; |
| 729 | |
| 730 | if (request_irq(dev->irq, &dm9000_interrupt, irqflags, dev->name, dev)) |
Sascha Hauer | a136527 | 2005-05-05 15:14:15 -0700 | [diff] [blame] | 731 | return -EAGAIN; |
| 732 | |
| 733 | /* Initialize DM9000 board */ |
| 734 | dm9000_reset(db); |
| 735 | dm9000_init_dm9000(dev); |
| 736 | |
| 737 | /* Init driver variable */ |
| 738 | db->dbug_cnt = 0; |
| 739 | |
Sascha Hauer | a136527 | 2005-05-05 15:14:15 -0700 | [diff] [blame] | 740 | mii_check_media(&db->mii, netif_msg_link(db), 1); |
| 741 | netif_start_queue(dev); |
| 742 | |
| 743 | return 0; |
| 744 | } |
| 745 | |
| 746 | /* |
| 747 | * Initilize dm9000 board |
| 748 | */ |
| 749 | static void |
| 750 | dm9000_init_dm9000(struct net_device *dev) |
| 751 | { |
| 752 | board_info_t *db = (board_info_t *) dev->priv; |
| 753 | |
Ben Dooks | 5b2b4ff | 2008-02-05 00:02:03 +0000 | [diff] [blame] | 754 | dm9000_dbg(db, 1, "entering %s\n", __func__); |
Sascha Hauer | a136527 | 2005-05-05 15:14:15 -0700 | [diff] [blame] | 755 | |
| 756 | /* I/O mode */ |
| 757 | db->io_mode = ior(db, DM9000_ISR) >> 6; /* ISR bit7:6 keeps I/O mode */ |
| 758 | |
| 759 | /* GPIO0 on pre-activate PHY */ |
| 760 | iow(db, DM9000_GPR, 0); /* REG_1F bit0 activate phyxcer */ |
| 761 | iow(db, DM9000_GPCR, GPCR_GEP_CNTL); /* Let GPIO0 output */ |
| 762 | iow(db, DM9000_GPR, 0); /* Enable PHY */ |
| 763 | |
Ben Dooks | 33ba509 | 2008-02-05 00:02:01 +0000 | [diff] [blame] | 764 | if (db->flags & DM9000_PLATF_EXT_PHY) |
| 765 | iow(db, DM9000_NCR, NCR_EXT_PHY); |
| 766 | |
Sascha Hauer | a136527 | 2005-05-05 15:14:15 -0700 | [diff] [blame] | 767 | /* Program operating register */ |
| 768 | iow(db, DM9000_TCR, 0); /* TX Polling clear */ |
| 769 | iow(db, DM9000_BPTR, 0x3f); /* Less 3Kb, 200us */ |
| 770 | iow(db, DM9000_FCR, 0xff); /* Flow Control */ |
| 771 | iow(db, DM9000_SMCR, 0); /* Special Mode */ |
| 772 | /* clear TX status */ |
| 773 | iow(db, DM9000_NSR, NSR_WAKEST | NSR_TX2END | NSR_TX1END); |
| 774 | iow(db, DM9000_ISR, ISR_CLR_STATUS); /* Clear interrupt status */ |
| 775 | |
| 776 | /* Set address filter table */ |
| 777 | dm9000_hash_table(dev); |
| 778 | |
| 779 | /* Activate DM9000 */ |
| 780 | iow(db, DM9000_RCR, RCR_DIS_LONG | RCR_DIS_CRC | RCR_RXEN); |
| 781 | /* Enable TX/RX interrupt mask */ |
| 782 | iow(db, DM9000_IMR, IMR_PAR | IMR_PTM | IMR_PRM); |
| 783 | |
| 784 | /* Init Driver variable */ |
| 785 | db->tx_pkt_cnt = 0; |
| 786 | db->queue_pkt_len = 0; |
| 787 | dev->trans_start = 0; |
Sascha Hauer | a136527 | 2005-05-05 15:14:15 -0700 | [diff] [blame] | 788 | } |
| 789 | |
| 790 | /* |
| 791 | * Hardware start transmission. |
| 792 | * Send a packet to media from the upper layer. |
| 793 | */ |
| 794 | static int |
| 795 | dm9000_start_xmit(struct sk_buff *skb, struct net_device *dev) |
| 796 | { |
Florian Westphal | c46ac94 | 2007-08-21 01:33:42 +0200 | [diff] [blame] | 797 | unsigned long flags; |
Sascha Hauer | a136527 | 2005-05-05 15:14:15 -0700 | [diff] [blame] | 798 | board_info_t *db = (board_info_t *) dev->priv; |
| 799 | |
Ben Dooks | 5b2b4ff | 2008-02-05 00:02:03 +0000 | [diff] [blame] | 800 | dm9000_dbg(db, 3, "%s:\n", __func__); |
Sascha Hauer | a136527 | 2005-05-05 15:14:15 -0700 | [diff] [blame] | 801 | |
| 802 | if (db->tx_pkt_cnt > 1) |
| 803 | return 1; |
| 804 | |
Florian Westphal | c46ac94 | 2007-08-21 01:33:42 +0200 | [diff] [blame] | 805 | spin_lock_irqsave(&db->lock, flags); |
Sascha Hauer | a136527 | 2005-05-05 15:14:15 -0700 | [diff] [blame] | 806 | |
| 807 | /* Move data to DM9000 TX RAM */ |
| 808 | writeb(DM9000_MWCMD, db->io_addr); |
| 809 | |
| 810 | (db->outblk)(db->io_data, skb->data, skb->len); |
Jeff Garzik | 09f75cd | 2007-10-03 17:41:50 -0700 | [diff] [blame] | 811 | dev->stats.tx_bytes += skb->len; |
Sascha Hauer | a136527 | 2005-05-05 15:14:15 -0700 | [diff] [blame] | 812 | |
Florian Westphal | c46ac94 | 2007-08-21 01:33:42 +0200 | [diff] [blame] | 813 | db->tx_pkt_cnt++; |
Sascha Hauer | a136527 | 2005-05-05 15:14:15 -0700 | [diff] [blame] | 814 | /* TX control: First packet immediately send, second packet queue */ |
Florian Westphal | c46ac94 | 2007-08-21 01:33:42 +0200 | [diff] [blame] | 815 | if (db->tx_pkt_cnt == 1) { |
Sascha Hauer | a136527 | 2005-05-05 15:14:15 -0700 | [diff] [blame] | 816 | /* Set TX length to DM9000 */ |
| 817 | iow(db, DM9000_TXPLL, skb->len & 0xff); |
| 818 | iow(db, DM9000_TXPLH, (skb->len >> 8) & 0xff); |
| 819 | |
| 820 | /* Issue TX polling command */ |
| 821 | iow(db, DM9000_TCR, TCR_TXREQ); /* Cleared after TX complete */ |
| 822 | |
| 823 | dev->trans_start = jiffies; /* save the time stamp */ |
Sascha Hauer | a136527 | 2005-05-05 15:14:15 -0700 | [diff] [blame] | 824 | } else { |
| 825 | /* Second packet */ |
Sascha Hauer | a136527 | 2005-05-05 15:14:15 -0700 | [diff] [blame] | 826 | db->queue_pkt_len = skb->len; |
Florian Westphal | c46ac94 | 2007-08-21 01:33:42 +0200 | [diff] [blame] | 827 | netif_stop_queue(dev); |
Sascha Hauer | a136527 | 2005-05-05 15:14:15 -0700 | [diff] [blame] | 828 | } |
| 829 | |
Florian Westphal | c46ac94 | 2007-08-21 01:33:42 +0200 | [diff] [blame] | 830 | spin_unlock_irqrestore(&db->lock, flags); |
| 831 | |
Sascha Hauer | a136527 | 2005-05-05 15:14:15 -0700 | [diff] [blame] | 832 | /* free this SKB */ |
| 833 | dev_kfree_skb(skb); |
| 834 | |
Sascha Hauer | a136527 | 2005-05-05 15:14:15 -0700 | [diff] [blame] | 835 | return 0; |
| 836 | } |
| 837 | |
| 838 | static void |
| 839 | dm9000_shutdown(struct net_device *dev) |
| 840 | { |
| 841 | board_info_t *db = (board_info_t *) dev->priv; |
| 842 | |
| 843 | /* RESET device */ |
| 844 | dm9000_phy_write(dev, 0, MII_BMCR, BMCR_RESET); /* PHY RESET */ |
| 845 | iow(db, DM9000_GPR, 0x01); /* Power-Down PHY */ |
| 846 | iow(db, DM9000_IMR, IMR_PAR); /* Disable all interrupt */ |
| 847 | iow(db, DM9000_RCR, 0x00); /* Disable RX */ |
| 848 | } |
| 849 | |
| 850 | /* |
| 851 | * Stop the interface. |
| 852 | * The interface is stopped when it is brought. |
| 853 | */ |
| 854 | static int |
| 855 | dm9000_stop(struct net_device *ndev) |
| 856 | { |
| 857 | board_info_t *db = (board_info_t *) ndev->priv; |
| 858 | |
Ben Dooks | c991d16 | 2008-02-05 00:02:16 +0000 | [diff] [blame] | 859 | if (netif_msg_ifdown(db)) |
| 860 | dev_dbg(db->dev, "shutting down %s\n", ndev->name); |
Sascha Hauer | a136527 | 2005-05-05 15:14:15 -0700 | [diff] [blame] | 861 | |
Sascha Hauer | a136527 | 2005-05-05 15:14:15 -0700 | [diff] [blame] | 862 | netif_stop_queue(ndev); |
| 863 | netif_carrier_off(ndev); |
| 864 | |
| 865 | /* free interrupt */ |
| 866 | free_irq(ndev->irq, ndev); |
| 867 | |
| 868 | dm9000_shutdown(ndev); |
| 869 | |
| 870 | return 0; |
| 871 | } |
| 872 | |
| 873 | /* |
| 874 | * DM9000 interrupt handler |
| 875 | * receive the packet to upper layer, free the transmitted packet |
| 876 | */ |
| 877 | |
Ben Dooks | 5d22a31 | 2006-06-14 00:09:17 +0100 | [diff] [blame] | 878 | static void |
Sascha Hauer | a136527 | 2005-05-05 15:14:15 -0700 | [diff] [blame] | 879 | dm9000_tx_done(struct net_device *dev, board_info_t * db) |
| 880 | { |
| 881 | int tx_status = ior(db, DM9000_NSR); /* Got TX status */ |
| 882 | |
| 883 | if (tx_status & (NSR_TX2END | NSR_TX1END)) { |
| 884 | /* One packet sent complete */ |
| 885 | db->tx_pkt_cnt--; |
Jeff Garzik | 09f75cd | 2007-10-03 17:41:50 -0700 | [diff] [blame] | 886 | dev->stats.tx_packets++; |
Sascha Hauer | a136527 | 2005-05-05 15:14:15 -0700 | [diff] [blame] | 887 | |
Ben Dooks | c991d16 | 2008-02-05 00:02:16 +0000 | [diff] [blame] | 888 | if (netif_msg_tx_done(db)) |
| 889 | dev_dbg(db->dev, "tx done, NSR %02x\n", tx_status); |
| 890 | |
Sascha Hauer | a136527 | 2005-05-05 15:14:15 -0700 | [diff] [blame] | 891 | /* Queue packet check & send */ |
| 892 | if (db->tx_pkt_cnt > 0) { |
| 893 | iow(db, DM9000_TXPLL, db->queue_pkt_len & 0xff); |
| 894 | iow(db, DM9000_TXPLH, (db->queue_pkt_len >> 8) & 0xff); |
| 895 | iow(db, DM9000_TCR, TCR_TXREQ); |
| 896 | dev->trans_start = jiffies; |
| 897 | } |
| 898 | netif_wake_queue(dev); |
| 899 | } |
| 900 | } |
| 901 | |
| 902 | static irqreturn_t |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 903 | dm9000_interrupt(int irq, void *dev_id) |
Sascha Hauer | a136527 | 2005-05-05 15:14:15 -0700 | [diff] [blame] | 904 | { |
| 905 | struct net_device *dev = dev_id; |
Ben Dooks | 5b2b4ff | 2008-02-05 00:02:03 +0000 | [diff] [blame] | 906 | board_info_t *db = (board_info_t *) dev->priv; |
Sascha Hauer | a136527 | 2005-05-05 15:14:15 -0700 | [diff] [blame] | 907 | int int_status; |
| 908 | u8 reg_save; |
| 909 | |
Ben Dooks | 5b2b4ff | 2008-02-05 00:02:03 +0000 | [diff] [blame] | 910 | dm9000_dbg(db, 3, "entering %s\n", __func__); |
Sascha Hauer | a136527 | 2005-05-05 15:14:15 -0700 | [diff] [blame] | 911 | |
| 912 | /* A real interrupt coming */ |
Ben Dooks | 5b2b4ff | 2008-02-05 00:02:03 +0000 | [diff] [blame] | 913 | |
Sascha Hauer | a136527 | 2005-05-05 15:14:15 -0700 | [diff] [blame] | 914 | spin_lock(&db->lock); |
| 915 | |
| 916 | /* Save previous register address */ |
| 917 | reg_save = readb(db->io_addr); |
| 918 | |
| 919 | /* Disable all interrupts */ |
| 920 | iow(db, DM9000_IMR, IMR_PAR); |
| 921 | |
| 922 | /* Got DM9000 interrupt status */ |
| 923 | int_status = ior(db, DM9000_ISR); /* Got ISR */ |
| 924 | iow(db, DM9000_ISR, int_status); /* Clear ISR status */ |
| 925 | |
Ben Dooks | c991d16 | 2008-02-05 00:02:16 +0000 | [diff] [blame] | 926 | if (netif_msg_intr(db)) |
| 927 | dev_dbg(db->dev, "interrupt status %02x\n", int_status); |
| 928 | |
Sascha Hauer | a136527 | 2005-05-05 15:14:15 -0700 | [diff] [blame] | 929 | /* Received the coming packet */ |
| 930 | if (int_status & ISR_PRS) |
| 931 | dm9000_rx(dev); |
| 932 | |
| 933 | /* Trnasmit Interrupt check */ |
| 934 | if (int_status & ISR_PTS) |
| 935 | dm9000_tx_done(dev, db); |
| 936 | |
| 937 | /* Re-enable interrupt mask */ |
| 938 | iow(db, DM9000_IMR, IMR_PAR | IMR_PTM | IMR_PRM); |
| 939 | |
| 940 | /* Restore previous register address */ |
| 941 | writeb(reg_save, db->io_addr); |
| 942 | |
| 943 | spin_unlock(&db->lock); |
| 944 | |
| 945 | return IRQ_HANDLED; |
| 946 | } |
| 947 | |
Sascha Hauer | a136527 | 2005-05-05 15:14:15 -0700 | [diff] [blame] | 948 | struct dm9000_rxhdr { |
Ben Dooks | 9311657 | 2008-02-05 00:02:00 +0000 | [diff] [blame] | 949 | u8 RxPktReady; |
| 950 | u8 RxStatus; |
Sascha Hauer | a136527 | 2005-05-05 15:14:15 -0700 | [diff] [blame] | 951 | u16 RxLen; |
| 952 | } __attribute__((__packed__)); |
| 953 | |
| 954 | /* |
| 955 | * Received a packet and pass to upper layer |
| 956 | */ |
| 957 | static void |
| 958 | dm9000_rx(struct net_device *dev) |
| 959 | { |
| 960 | board_info_t *db = (board_info_t *) dev->priv; |
| 961 | struct dm9000_rxhdr rxhdr; |
| 962 | struct sk_buff *skb; |
| 963 | u8 rxbyte, *rdptr; |
Richard Knutsson | 6478fac | 2007-05-01 18:43:27 +0200 | [diff] [blame] | 964 | bool GoodPacket; |
Sascha Hauer | a136527 | 2005-05-05 15:14:15 -0700 | [diff] [blame] | 965 | int RxLen; |
| 966 | |
| 967 | /* Check packet ready or not */ |
| 968 | do { |
| 969 | ior(db, DM9000_MRCMDX); /* Dummy read */ |
| 970 | |
| 971 | /* Get most updated data */ |
| 972 | rxbyte = readb(db->io_data); |
| 973 | |
| 974 | /* Status check: this byte must be 0 or 1 */ |
| 975 | if (rxbyte > DM9000_PKT_RDY) { |
Ben Dooks | a76836f | 2008-02-05 00:02:02 +0000 | [diff] [blame] | 976 | dev_warn(db->dev, "status check fail: %d\n", rxbyte); |
Sascha Hauer | a136527 | 2005-05-05 15:14:15 -0700 | [diff] [blame] | 977 | iow(db, DM9000_RCR, 0x00); /* Stop Device */ |
| 978 | iow(db, DM9000_ISR, IMR_PAR); /* Stop INT request */ |
| 979 | return; |
| 980 | } |
| 981 | |
| 982 | if (rxbyte != DM9000_PKT_RDY) |
| 983 | return; |
| 984 | |
| 985 | /* A packet ready now & Get status/length */ |
Richard Knutsson | 6478fac | 2007-05-01 18:43:27 +0200 | [diff] [blame] | 986 | GoodPacket = true; |
Sascha Hauer | a136527 | 2005-05-05 15:14:15 -0700 | [diff] [blame] | 987 | writeb(DM9000_MRCMD, db->io_addr); |
| 988 | |
| 989 | (db->inblk)(db->io_data, &rxhdr, sizeof(rxhdr)); |
| 990 | |
Ben Dooks | 9311657 | 2008-02-05 00:02:00 +0000 | [diff] [blame] | 991 | RxLen = le16_to_cpu(rxhdr.RxLen); |
Sascha Hauer | a136527 | 2005-05-05 15:14:15 -0700 | [diff] [blame] | 992 | |
Ben Dooks | c991d16 | 2008-02-05 00:02:16 +0000 | [diff] [blame] | 993 | if (netif_msg_rx_status(db)) |
| 994 | dev_dbg(db->dev, "RX: status %02x, length %04x\n", |
| 995 | rxhdr.RxStatus, RxLen); |
| 996 | |
Sascha Hauer | a136527 | 2005-05-05 15:14:15 -0700 | [diff] [blame] | 997 | /* Packet Status check */ |
| 998 | if (RxLen < 0x40) { |
Richard Knutsson | 6478fac | 2007-05-01 18:43:27 +0200 | [diff] [blame] | 999 | GoodPacket = false; |
Ben Dooks | c991d16 | 2008-02-05 00:02:16 +0000 | [diff] [blame] | 1000 | if (netif_msg_rx_err(db)) |
| 1001 | dev_dbg(db->dev, "RX: Bad Packet (runt)\n"); |
Sascha Hauer | a136527 | 2005-05-05 15:14:15 -0700 | [diff] [blame] | 1002 | } |
| 1003 | |
| 1004 | if (RxLen > DM9000_PKT_MAX) { |
Ben Dooks | a76836f | 2008-02-05 00:02:02 +0000 | [diff] [blame] | 1005 | dev_dbg(db->dev, "RST: RX Len:%x\n", RxLen); |
Sascha Hauer | a136527 | 2005-05-05 15:14:15 -0700 | [diff] [blame] | 1006 | } |
| 1007 | |
Ben Dooks | 9311657 | 2008-02-05 00:02:00 +0000 | [diff] [blame] | 1008 | if (rxhdr.RxStatus & 0xbf) { |
Richard Knutsson | 6478fac | 2007-05-01 18:43:27 +0200 | [diff] [blame] | 1009 | GoodPacket = false; |
Ben Dooks | 9311657 | 2008-02-05 00:02:00 +0000 | [diff] [blame] | 1010 | if (rxhdr.RxStatus & 0x01) { |
Ben Dooks | c991d16 | 2008-02-05 00:02:16 +0000 | [diff] [blame] | 1011 | if (netif_msg_rx_err(db)) |
| 1012 | dev_dbg(db->dev, "fifo error\n"); |
Jeff Garzik | 09f75cd | 2007-10-03 17:41:50 -0700 | [diff] [blame] | 1013 | dev->stats.rx_fifo_errors++; |
Sascha Hauer | a136527 | 2005-05-05 15:14:15 -0700 | [diff] [blame] | 1014 | } |
Ben Dooks | 9311657 | 2008-02-05 00:02:00 +0000 | [diff] [blame] | 1015 | if (rxhdr.RxStatus & 0x02) { |
Ben Dooks | c991d16 | 2008-02-05 00:02:16 +0000 | [diff] [blame] | 1016 | if (netif_msg_rx_err(db)) |
| 1017 | dev_dbg(db->dev, "crc error\n"); |
Jeff Garzik | 09f75cd | 2007-10-03 17:41:50 -0700 | [diff] [blame] | 1018 | dev->stats.rx_crc_errors++; |
Sascha Hauer | a136527 | 2005-05-05 15:14:15 -0700 | [diff] [blame] | 1019 | } |
Ben Dooks | 9311657 | 2008-02-05 00:02:00 +0000 | [diff] [blame] | 1020 | if (rxhdr.RxStatus & 0x80) { |
Ben Dooks | c991d16 | 2008-02-05 00:02:16 +0000 | [diff] [blame] | 1021 | if (netif_msg_rx_err(db)) |
| 1022 | dev_dbg(db->dev, "length error\n"); |
Jeff Garzik | 09f75cd | 2007-10-03 17:41:50 -0700 | [diff] [blame] | 1023 | dev->stats.rx_length_errors++; |
Sascha Hauer | a136527 | 2005-05-05 15:14:15 -0700 | [diff] [blame] | 1024 | } |
| 1025 | } |
| 1026 | |
| 1027 | /* Move data from DM9000 */ |
| 1028 | if (GoodPacket |
| 1029 | && ((skb = dev_alloc_skb(RxLen + 4)) != NULL)) { |
Sascha Hauer | a136527 | 2005-05-05 15:14:15 -0700 | [diff] [blame] | 1030 | skb_reserve(skb, 2); |
| 1031 | rdptr = (u8 *) skb_put(skb, RxLen - 4); |
| 1032 | |
| 1033 | /* Read received packet from RX SRAM */ |
| 1034 | |
| 1035 | (db->inblk)(db->io_data, rdptr, RxLen); |
Jeff Garzik | 09f75cd | 2007-10-03 17:41:50 -0700 | [diff] [blame] | 1036 | dev->stats.rx_bytes += RxLen; |
Sascha Hauer | a136527 | 2005-05-05 15:14:15 -0700 | [diff] [blame] | 1037 | |
| 1038 | /* Pass to upper layer */ |
| 1039 | skb->protocol = eth_type_trans(skb, dev); |
| 1040 | netif_rx(skb); |
Jeff Garzik | 09f75cd | 2007-10-03 17:41:50 -0700 | [diff] [blame] | 1041 | dev->stats.rx_packets++; |
Sascha Hauer | a136527 | 2005-05-05 15:14:15 -0700 | [diff] [blame] | 1042 | |
| 1043 | } else { |
| 1044 | /* need to dump the packet's data */ |
| 1045 | |
| 1046 | (db->dumpblk)(db->io_data, RxLen); |
| 1047 | } |
| 1048 | } while (rxbyte == DM9000_PKT_RDY); |
| 1049 | } |
| 1050 | |
Ben Dooks | 39c341a | 2008-02-05 00:02:17 +0000 | [diff] [blame^] | 1051 | static unsigned int |
| 1052 | dm9000_read_locked(board_info_t *db, int reg) |
| 1053 | { |
| 1054 | unsigned long flags; |
| 1055 | unsigned int ret; |
| 1056 | |
| 1057 | spin_lock_irqsave(&db->lock, flags); |
| 1058 | ret = ior(db, reg); |
| 1059 | spin_unlock_irqrestore(&db->lock, flags); |
| 1060 | |
| 1061 | return ret; |
| 1062 | } |
| 1063 | |
| 1064 | static int dm9000_wait_eeprom(board_info_t *db) |
| 1065 | { |
| 1066 | unsigned int status; |
| 1067 | int timeout = 8; /* wait max 8msec */ |
| 1068 | |
| 1069 | /* The DM9000 data sheets say we should be able to |
| 1070 | * poll the ERRE bit in EPCR to wait for the EEPROM |
| 1071 | * operation. From testing several chips, this bit |
| 1072 | * does not seem to work. |
| 1073 | * |
| 1074 | * We attempt to use the bit, but fall back to the |
| 1075 | * timeout (which is why we do not return an error |
| 1076 | * on expiry) to say that the EEPROM operation has |
| 1077 | * completed. |
| 1078 | */ |
| 1079 | |
| 1080 | while (1) { |
| 1081 | status = dm9000_read_locked(db, DM9000_EPCR); |
| 1082 | |
| 1083 | if ((status & EPCR_ERRE) == 0) |
| 1084 | break; |
| 1085 | |
| 1086 | if (timeout-- < 0) { |
| 1087 | dev_dbg(db->dev, "timeout waiting EEPROM\n"); |
| 1088 | break; |
| 1089 | } |
| 1090 | } |
| 1091 | |
| 1092 | return 0; |
| 1093 | } |
| 1094 | |
Sascha Hauer | a136527 | 2005-05-05 15:14:15 -0700 | [diff] [blame] | 1095 | /* |
Ben Dooks | 86c62fa | 2008-02-05 00:02:09 +0000 | [diff] [blame] | 1096 | * Read a word data from EEPROM |
Sascha Hauer | a136527 | 2005-05-05 15:14:15 -0700 | [diff] [blame] | 1097 | */ |
Ben Dooks | 86c62fa | 2008-02-05 00:02:09 +0000 | [diff] [blame] | 1098 | static void |
Ben Dooks | 29d52e5 | 2008-02-05 00:02:11 +0000 | [diff] [blame] | 1099 | dm9000_read_eeprom(board_info_t *db, int offset, u8 *to) |
Sascha Hauer | a136527 | 2005-05-05 15:14:15 -0700 | [diff] [blame] | 1100 | { |
Ben Dooks | 621ddcb | 2008-02-05 00:02:14 +0000 | [diff] [blame] | 1101 | unsigned long flags; |
| 1102 | |
Ben Dooks | 9a2f037 | 2008-02-05 00:02:10 +0000 | [diff] [blame] | 1103 | mutex_lock(&db->addr_lock); |
| 1104 | |
Ben Dooks | 621ddcb | 2008-02-05 00:02:14 +0000 | [diff] [blame] | 1105 | spin_lock_irqsave(&db->lock, flags); |
| 1106 | |
Sascha Hauer | a136527 | 2005-05-05 15:14:15 -0700 | [diff] [blame] | 1107 | iow(db, DM9000_EPAR, offset); |
| 1108 | iow(db, DM9000_EPCR, EPCR_ERPRR); |
Ben Dooks | 621ddcb | 2008-02-05 00:02:14 +0000 | [diff] [blame] | 1109 | |
| 1110 | spin_unlock_irqrestore(&db->lock, flags); |
| 1111 | |
Ben Dooks | 39c341a | 2008-02-05 00:02:17 +0000 | [diff] [blame^] | 1112 | dm9000_wait_eeprom(db); |
| 1113 | |
| 1114 | /* delay for at-least 150uS */ |
| 1115 | msleep(1); |
Ben Dooks | 621ddcb | 2008-02-05 00:02:14 +0000 | [diff] [blame] | 1116 | |
| 1117 | spin_lock_irqsave(&db->lock, flags); |
| 1118 | |
Sascha Hauer | a136527 | 2005-05-05 15:14:15 -0700 | [diff] [blame] | 1119 | iow(db, DM9000_EPCR, 0x0); |
Ben Dooks | 86c62fa | 2008-02-05 00:02:09 +0000 | [diff] [blame] | 1120 | |
| 1121 | to[0] = ior(db, DM9000_EPDRL); |
| 1122 | to[1] = ior(db, DM9000_EPDRH); |
Ben Dooks | 9a2f037 | 2008-02-05 00:02:10 +0000 | [diff] [blame] | 1123 | |
Ben Dooks | 621ddcb | 2008-02-05 00:02:14 +0000 | [diff] [blame] | 1124 | spin_unlock_irqrestore(&db->lock, flags); |
| 1125 | |
Ben Dooks | 9a2f037 | 2008-02-05 00:02:10 +0000 | [diff] [blame] | 1126 | mutex_unlock(&db->addr_lock); |
Sascha Hauer | a136527 | 2005-05-05 15:14:15 -0700 | [diff] [blame] | 1127 | } |
| 1128 | |
Sascha Hauer | a136527 | 2005-05-05 15:14:15 -0700 | [diff] [blame] | 1129 | /* |
| 1130 | * Write a word data to SROM |
| 1131 | */ |
| 1132 | static void |
Ben Dooks | 29d52e5 | 2008-02-05 00:02:11 +0000 | [diff] [blame] | 1133 | dm9000_write_eeprom(board_info_t *db, int offset, u8 *data) |
Sascha Hauer | a136527 | 2005-05-05 15:14:15 -0700 | [diff] [blame] | 1134 | { |
Ben Dooks | 621ddcb | 2008-02-05 00:02:14 +0000 | [diff] [blame] | 1135 | unsigned long flags; |
| 1136 | |
Ben Dooks | 9a2f037 | 2008-02-05 00:02:10 +0000 | [diff] [blame] | 1137 | mutex_lock(&db->addr_lock); |
| 1138 | |
Ben Dooks | 621ddcb | 2008-02-05 00:02:14 +0000 | [diff] [blame] | 1139 | spin_lock_irqsave(&db->lock, flags); |
Sascha Hauer | a136527 | 2005-05-05 15:14:15 -0700 | [diff] [blame] | 1140 | iow(db, DM9000_EPAR, offset); |
Ben Dooks | 29d52e5 | 2008-02-05 00:02:11 +0000 | [diff] [blame] | 1141 | iow(db, DM9000_EPDRH, data[1]); |
| 1142 | iow(db, DM9000_EPDRL, data[0]); |
Sascha Hauer | a136527 | 2005-05-05 15:14:15 -0700 | [diff] [blame] | 1143 | iow(db, DM9000_EPCR, EPCR_WEP | EPCR_ERPRW); |
Ben Dooks | 621ddcb | 2008-02-05 00:02:14 +0000 | [diff] [blame] | 1144 | spin_unlock_irqrestore(&db->lock, flags); |
| 1145 | |
Ben Dooks | 39c341a | 2008-02-05 00:02:17 +0000 | [diff] [blame^] | 1146 | dm9000_wait_eeprom(db); |
| 1147 | |
| 1148 | mdelay(1); /* wait at least 150uS to clear */ |
Ben Dooks | 621ddcb | 2008-02-05 00:02:14 +0000 | [diff] [blame] | 1149 | |
| 1150 | spin_lock_irqsave(&db->lock, flags); |
Sascha Hauer | a136527 | 2005-05-05 15:14:15 -0700 | [diff] [blame] | 1151 | iow(db, DM9000_EPCR, 0); |
Ben Dooks | 621ddcb | 2008-02-05 00:02:14 +0000 | [diff] [blame] | 1152 | spin_unlock_irqrestore(&db->lock, flags); |
Ben Dooks | 9a2f037 | 2008-02-05 00:02:10 +0000 | [diff] [blame] | 1153 | |
| 1154 | mutex_unlock(&db->addr_lock); |
Sascha Hauer | a136527 | 2005-05-05 15:14:15 -0700 | [diff] [blame] | 1155 | } |
| 1156 | |
Sascha Hauer | a136527 | 2005-05-05 15:14:15 -0700 | [diff] [blame] | 1157 | /* |
| 1158 | * Calculate the CRC valude of the Rx packet |
| 1159 | * flag = 1 : return the reverse CRC (for the received packet CRC) |
| 1160 | * 0 : return the normal CRC (for Hash Table index) |
| 1161 | */ |
| 1162 | |
| 1163 | static unsigned long |
| 1164 | cal_CRC(unsigned char *Data, unsigned int Len, u8 flag) |
| 1165 | { |
| 1166 | |
| 1167 | u32 crc = ether_crc_le(Len, Data); |
| 1168 | |
| 1169 | if (flag) |
| 1170 | return ~crc; |
| 1171 | |
| 1172 | return crc; |
| 1173 | } |
| 1174 | |
| 1175 | /* |
| 1176 | * Set DM9000 multicast address |
| 1177 | */ |
| 1178 | static void |
| 1179 | dm9000_hash_table(struct net_device *dev) |
| 1180 | { |
| 1181 | board_info_t *db = (board_info_t *) dev->priv; |
| 1182 | struct dev_mc_list *mcptr = dev->mc_list; |
| 1183 | int mc_cnt = dev->mc_count; |
| 1184 | u32 hash_val; |
| 1185 | u16 i, oft, hash_table[4]; |
| 1186 | unsigned long flags; |
| 1187 | |
Ben Dooks | 5b2b4ff | 2008-02-05 00:02:03 +0000 | [diff] [blame] | 1188 | dm9000_dbg(db, 1, "entering %s\n", __func__); |
Sascha Hauer | a136527 | 2005-05-05 15:14:15 -0700 | [diff] [blame] | 1189 | |
| 1190 | spin_lock_irqsave(&db->lock,flags); |
| 1191 | |
| 1192 | for (i = 0, oft = 0x10; i < 6; i++, oft++) |
| 1193 | iow(db, oft, dev->dev_addr[i]); |
| 1194 | |
| 1195 | /* Clear Hash Table */ |
| 1196 | for (i = 0; i < 4; i++) |
| 1197 | hash_table[i] = 0x0; |
| 1198 | |
| 1199 | /* broadcast address */ |
| 1200 | hash_table[3] = 0x8000; |
| 1201 | |
| 1202 | /* the multicast address in Hash Table : 64 bits */ |
| 1203 | for (i = 0; i < mc_cnt; i++, mcptr = mcptr->next) { |
| 1204 | hash_val = cal_CRC((char *) mcptr->dmi_addr, 6, 0) & 0x3f; |
| 1205 | hash_table[hash_val / 16] |= (u16) 1 << (hash_val % 16); |
| 1206 | } |
| 1207 | |
| 1208 | /* Write the hash table to MAC MD table */ |
| 1209 | for (i = 0, oft = 0x16; i < 4; i++) { |
| 1210 | iow(db, oft++, hash_table[i] & 0xff); |
| 1211 | iow(db, oft++, (hash_table[i] >> 8) & 0xff); |
| 1212 | } |
| 1213 | |
| 1214 | spin_unlock_irqrestore(&db->lock,flags); |
| 1215 | } |
| 1216 | |
| 1217 | |
| 1218 | /* |
Ben Dooks | 321f69a | 2008-02-05 00:02:08 +0000 | [diff] [blame] | 1219 | * Sleep, either by using msleep() or if we are suspending, then |
| 1220 | * use mdelay() to sleep. |
| 1221 | */ |
| 1222 | static void dm9000_msleep(board_info_t *db, unsigned int ms) |
| 1223 | { |
| 1224 | if (db->in_suspend) |
| 1225 | mdelay(ms); |
| 1226 | else |
| 1227 | msleep(ms); |
| 1228 | } |
| 1229 | |
| 1230 | /* |
Sascha Hauer | a136527 | 2005-05-05 15:14:15 -0700 | [diff] [blame] | 1231 | * Read a word from phyxcer |
| 1232 | */ |
| 1233 | static int |
| 1234 | dm9000_phy_read(struct net_device *dev, int phy_reg_unused, int reg) |
| 1235 | { |
| 1236 | board_info_t *db = (board_info_t *) dev->priv; |
| 1237 | unsigned long flags; |
Ben Dooks | 9ef9ac5 | 2005-07-23 17:25:18 +0100 | [diff] [blame] | 1238 | unsigned int reg_save; |
Sascha Hauer | a136527 | 2005-05-05 15:14:15 -0700 | [diff] [blame] | 1239 | int ret; |
| 1240 | |
Ben Dooks | 9a2f037 | 2008-02-05 00:02:10 +0000 | [diff] [blame] | 1241 | mutex_lock(&db->addr_lock); |
| 1242 | |
Sascha Hauer | a136527 | 2005-05-05 15:14:15 -0700 | [diff] [blame] | 1243 | spin_lock_irqsave(&db->lock,flags); |
Ben Dooks | 9ef9ac5 | 2005-07-23 17:25:18 +0100 | [diff] [blame] | 1244 | |
| 1245 | /* Save previous register address */ |
| 1246 | reg_save = readb(db->io_addr); |
| 1247 | |
Sascha Hauer | a136527 | 2005-05-05 15:14:15 -0700 | [diff] [blame] | 1248 | /* Fill the phyxcer register into REG_0C */ |
| 1249 | iow(db, DM9000_EPAR, DM9000_PHY | reg); |
| 1250 | |
| 1251 | iow(db, DM9000_EPCR, 0xc); /* Issue phyxcer read command */ |
Ben Dooks | 89c8b0e | 2008-02-05 00:02:07 +0000 | [diff] [blame] | 1252 | |
| 1253 | writeb(reg_save, db->io_addr); |
| 1254 | spin_unlock_irqrestore(&db->lock,flags); |
| 1255 | |
Ben Dooks | 321f69a | 2008-02-05 00:02:08 +0000 | [diff] [blame] | 1256 | dm9000_msleep(db, 1); /* Wait read complete */ |
Ben Dooks | 89c8b0e | 2008-02-05 00:02:07 +0000 | [diff] [blame] | 1257 | |
| 1258 | spin_lock_irqsave(&db->lock,flags); |
| 1259 | reg_save = readb(db->io_addr); |
| 1260 | |
Sascha Hauer | a136527 | 2005-05-05 15:14:15 -0700 | [diff] [blame] | 1261 | iow(db, DM9000_EPCR, 0x0); /* Clear phyxcer read command */ |
| 1262 | |
| 1263 | /* The read data keeps on REG_0D & REG_0E */ |
| 1264 | ret = (ior(db, DM9000_EPDRH) << 8) | ior(db, DM9000_EPDRL); |
| 1265 | |
Ben Dooks | 9ef9ac5 | 2005-07-23 17:25:18 +0100 | [diff] [blame] | 1266 | /* restore the previous address */ |
| 1267 | writeb(reg_save, db->io_addr); |
Sascha Hauer | a136527 | 2005-05-05 15:14:15 -0700 | [diff] [blame] | 1268 | spin_unlock_irqrestore(&db->lock,flags); |
| 1269 | |
Ben Dooks | 9a2f037 | 2008-02-05 00:02:10 +0000 | [diff] [blame] | 1270 | mutex_unlock(&db->addr_lock); |
Sascha Hauer | a136527 | 2005-05-05 15:14:15 -0700 | [diff] [blame] | 1271 | return ret; |
| 1272 | } |
| 1273 | |
| 1274 | /* |
| 1275 | * Write a word to phyxcer |
| 1276 | */ |
| 1277 | static void |
| 1278 | dm9000_phy_write(struct net_device *dev, int phyaddr_unused, int reg, int value) |
| 1279 | { |
| 1280 | board_info_t *db = (board_info_t *) dev->priv; |
| 1281 | unsigned long flags; |
Ben Dooks | 9ef9ac5 | 2005-07-23 17:25:18 +0100 | [diff] [blame] | 1282 | unsigned long reg_save; |
Sascha Hauer | a136527 | 2005-05-05 15:14:15 -0700 | [diff] [blame] | 1283 | |
Ben Dooks | 9a2f037 | 2008-02-05 00:02:10 +0000 | [diff] [blame] | 1284 | mutex_lock(&db->addr_lock); |
| 1285 | |
Sascha Hauer | a136527 | 2005-05-05 15:14:15 -0700 | [diff] [blame] | 1286 | spin_lock_irqsave(&db->lock,flags); |
| 1287 | |
Ben Dooks | 9ef9ac5 | 2005-07-23 17:25:18 +0100 | [diff] [blame] | 1288 | /* Save previous register address */ |
| 1289 | reg_save = readb(db->io_addr); |
| 1290 | |
Sascha Hauer | a136527 | 2005-05-05 15:14:15 -0700 | [diff] [blame] | 1291 | /* Fill the phyxcer register into REG_0C */ |
| 1292 | iow(db, DM9000_EPAR, DM9000_PHY | reg); |
| 1293 | |
| 1294 | /* Fill the written data into REG_0D & REG_0E */ |
| 1295 | iow(db, DM9000_EPDRL, (value & 0xff)); |
| 1296 | iow(db, DM9000_EPDRH, ((value >> 8) & 0xff)); |
| 1297 | |
| 1298 | iow(db, DM9000_EPCR, 0xa); /* Issue phyxcer write command */ |
Ben Dooks | 89c8b0e | 2008-02-05 00:02:07 +0000 | [diff] [blame] | 1299 | |
| 1300 | writeb(reg_save, db->io_addr); |
Ben Dooks | 9a2f037 | 2008-02-05 00:02:10 +0000 | [diff] [blame] | 1301 | spin_unlock_irqrestore(&db->lock, flags); |
Ben Dooks | 89c8b0e | 2008-02-05 00:02:07 +0000 | [diff] [blame] | 1302 | |
Ben Dooks | 321f69a | 2008-02-05 00:02:08 +0000 | [diff] [blame] | 1303 | dm9000_msleep(db, 1); /* Wait write complete */ |
Ben Dooks | 89c8b0e | 2008-02-05 00:02:07 +0000 | [diff] [blame] | 1304 | |
| 1305 | spin_lock_irqsave(&db->lock,flags); |
| 1306 | reg_save = readb(db->io_addr); |
| 1307 | |
Sascha Hauer | a136527 | 2005-05-05 15:14:15 -0700 | [diff] [blame] | 1308 | iow(db, DM9000_EPCR, 0x0); /* Clear phyxcer write command */ |
| 1309 | |
Ben Dooks | 9ef9ac5 | 2005-07-23 17:25:18 +0100 | [diff] [blame] | 1310 | /* restore the previous address */ |
| 1311 | writeb(reg_save, db->io_addr); |
| 1312 | |
Ben Dooks | 9a2f037 | 2008-02-05 00:02:10 +0000 | [diff] [blame] | 1313 | spin_unlock_irqrestore(&db->lock, flags); |
| 1314 | mutex_unlock(&db->addr_lock); |
Sascha Hauer | a136527 | 2005-05-05 15:14:15 -0700 | [diff] [blame] | 1315 | } |
| 1316 | |
| 1317 | static int |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 1318 | dm9000_drv_suspend(struct platform_device *dev, pm_message_t state) |
Sascha Hauer | a136527 | 2005-05-05 15:14:15 -0700 | [diff] [blame] | 1319 | { |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 1320 | struct net_device *ndev = platform_get_drvdata(dev); |
Ben Dooks | 321f69a | 2008-02-05 00:02:08 +0000 | [diff] [blame] | 1321 | board_info_t *db; |
Sascha Hauer | a136527 | 2005-05-05 15:14:15 -0700 | [diff] [blame] | 1322 | |
Russell King | 9480e30 | 2005-10-28 09:52:56 -0700 | [diff] [blame] | 1323 | if (ndev) { |
Ben Dooks | 321f69a | 2008-02-05 00:02:08 +0000 | [diff] [blame] | 1324 | db = (board_info_t *) ndev->priv; |
| 1325 | db->in_suspend = 1; |
| 1326 | |
Sascha Hauer | a136527 | 2005-05-05 15:14:15 -0700 | [diff] [blame] | 1327 | if (netif_running(ndev)) { |
| 1328 | netif_device_detach(ndev); |
| 1329 | dm9000_shutdown(ndev); |
| 1330 | } |
| 1331 | } |
| 1332 | return 0; |
| 1333 | } |
| 1334 | |
| 1335 | static int |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 1336 | dm9000_drv_resume(struct platform_device *dev) |
Sascha Hauer | a136527 | 2005-05-05 15:14:15 -0700 | [diff] [blame] | 1337 | { |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 1338 | struct net_device *ndev = platform_get_drvdata(dev); |
Sascha Hauer | a136527 | 2005-05-05 15:14:15 -0700 | [diff] [blame] | 1339 | board_info_t *db = (board_info_t *) ndev->priv; |
| 1340 | |
Russell King | 9480e30 | 2005-10-28 09:52:56 -0700 | [diff] [blame] | 1341 | if (ndev) { |
Sascha Hauer | a136527 | 2005-05-05 15:14:15 -0700 | [diff] [blame] | 1342 | |
| 1343 | if (netif_running(ndev)) { |
| 1344 | dm9000_reset(db); |
| 1345 | dm9000_init_dm9000(ndev); |
| 1346 | |
| 1347 | netif_device_attach(ndev); |
| 1348 | } |
Ben Dooks | 321f69a | 2008-02-05 00:02:08 +0000 | [diff] [blame] | 1349 | |
| 1350 | db->in_suspend = 0; |
Sascha Hauer | a136527 | 2005-05-05 15:14:15 -0700 | [diff] [blame] | 1351 | } |
| 1352 | return 0; |
| 1353 | } |
| 1354 | |
| 1355 | static int |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 1356 | dm9000_drv_remove(struct platform_device *pdev) |
Sascha Hauer | a136527 | 2005-05-05 15:14:15 -0700 | [diff] [blame] | 1357 | { |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 1358 | struct net_device *ndev = platform_get_drvdata(pdev); |
Sascha Hauer | a136527 | 2005-05-05 15:14:15 -0700 | [diff] [blame] | 1359 | |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 1360 | platform_set_drvdata(pdev, NULL); |
Sascha Hauer | a136527 | 2005-05-05 15:14:15 -0700 | [diff] [blame] | 1361 | |
| 1362 | unregister_netdev(ndev); |
| 1363 | dm9000_release_board(pdev, (board_info_t *) ndev->priv); |
Ben Dooks | 9fd9f9b | 2007-05-07 11:13:25 +0000 | [diff] [blame] | 1364 | free_netdev(ndev); /* free device structure */ |
Sascha Hauer | a136527 | 2005-05-05 15:14:15 -0700 | [diff] [blame] | 1365 | |
Ben Dooks | a76836f | 2008-02-05 00:02:02 +0000 | [diff] [blame] | 1366 | dev_dbg(&pdev->dev, "released and freed device\n"); |
Sascha Hauer | a136527 | 2005-05-05 15:14:15 -0700 | [diff] [blame] | 1367 | return 0; |
| 1368 | } |
| 1369 | |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 1370 | static struct platform_driver dm9000_driver = { |
Ben Dooks | 5d22a31 | 2006-06-14 00:09:17 +0100 | [diff] [blame] | 1371 | .driver = { |
| 1372 | .name = "dm9000", |
| 1373 | .owner = THIS_MODULE, |
| 1374 | }, |
Sascha Hauer | a136527 | 2005-05-05 15:14:15 -0700 | [diff] [blame] | 1375 | .probe = dm9000_probe, |
| 1376 | .remove = dm9000_drv_remove, |
| 1377 | .suspend = dm9000_drv_suspend, |
| 1378 | .resume = dm9000_drv_resume, |
| 1379 | }; |
| 1380 | |
| 1381 | static int __init |
| 1382 | dm9000_init(void) |
| 1383 | { |
Ben Dooks | 7da9985 | 2008-02-05 00:02:06 +0000 | [diff] [blame] | 1384 | printk(KERN_INFO "%s Ethernet Driver, V%s\n", CARDNAME, DRV_VERSION); |
Ben Dooks | 2ae2d77 | 2005-07-23 17:29:38 +0100 | [diff] [blame] | 1385 | |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 1386 | return platform_driver_register(&dm9000_driver); /* search board and register */ |
Sascha Hauer | a136527 | 2005-05-05 15:14:15 -0700 | [diff] [blame] | 1387 | } |
| 1388 | |
| 1389 | static void __exit |
| 1390 | dm9000_cleanup(void) |
| 1391 | { |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 1392 | platform_driver_unregister(&dm9000_driver); |
Sascha Hauer | a136527 | 2005-05-05 15:14:15 -0700 | [diff] [blame] | 1393 | } |
| 1394 | |
| 1395 | module_init(dm9000_init); |
| 1396 | module_exit(dm9000_cleanup); |
| 1397 | |
| 1398 | MODULE_AUTHOR("Sascha Hauer, Ben Dooks"); |
| 1399 | MODULE_DESCRIPTION("Davicom DM9000 network driver"); |
| 1400 | MODULE_LICENSE("GPL"); |