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