Linux Audio

Check our new training course

Loading...
v5.9
   1// SPDX-License-Identifier: GPL-2.0-only
   2/* drivers/net/ethernet/8390/ax88796.c
   3 *
   4 * Copyright 2005,2007 Simtec Electronics
   5 *	Ben Dooks <ben@simtec.co.uk>
   6 *
   7 * Asix AX88796 10/100 Ethernet controller support
   8 *	Based on ne.c, by Donald Becker, et-al.
 
 
 
 
   9 */
  10
  11#include <linux/module.h>
  12#include <linux/kernel.h>
  13#include <linux/errno.h>
  14#include <linux/isapnp.h>
  15#include <linux/interrupt.h>
  16#include <linux/io.h>
  17#include <linux/platform_device.h>
  18#include <linux/delay.h>
  19#include <linux/timer.h>
  20#include <linux/netdevice.h>
  21#include <linux/etherdevice.h>
  22#include <linux/ethtool.h>
  23#include <linux/mdio-bitbang.h>
  24#include <linux/phy.h>
  25#include <linux/eeprom_93cx6.h>
  26#include <linux/slab.h>
  27
  28#include <net/ax88796.h>
  29
  30
  31/* Rename the lib8390.c functions to show that they are in this driver */
  32#define __ei_open ax_ei_open
  33#define __ei_close ax_ei_close
  34#define __ei_poll ax_ei_poll
  35#define __ei_start_xmit ax_ei_start_xmit
  36#define __ei_tx_timeout ax_ei_tx_timeout
  37#define __ei_get_stats ax_ei_get_stats
  38#define __ei_set_multicast_list ax_ei_set_multicast_list
  39#define __ei_interrupt ax_ei_interrupt
  40#define ____alloc_ei_netdev ax__alloc_ei_netdev
  41#define __NS8390_init ax_NS8390_init
  42
  43/* force unsigned long back to 'void __iomem *' */
  44#define ax_convert_addr(_a) ((void __force __iomem *)(_a))
  45
  46#define ei_inb(_a) readb(ax_convert_addr(_a))
  47#define ei_outb(_v, _a) writeb(_v, ax_convert_addr(_a))
  48
  49#define ei_inb_p(_a) ei_inb(_a)
  50#define ei_outb_p(_v, _a) ei_outb(_v, _a)
  51
  52/* define EI_SHIFT() to take into account our register offsets */
  53#define EI_SHIFT(x) (ei_local->reg_offset[(x)])
  54
  55/* Ensure we have our RCR base value */
  56#define AX88796_PLATFORM
  57
  58static unsigned char version[] = "ax88796.c: Copyright 2005,2007 Simtec Electronics\n";
  59
  60#include "lib8390.c"
  61
  62#define DRV_NAME "ax88796"
  63#define DRV_VERSION "1.00"
  64
  65/* from ne.c */
  66#define NE_CMD		EI_SHIFT(0x00)
  67#define NE_RESET	EI_SHIFT(0x1f)
  68#define NE_DATAPORT	EI_SHIFT(0x10)
  69
  70#define NE1SM_START_PG	0x20	/* First page of TX buffer */
  71#define NE1SM_STOP_PG	0x40	/* Last page +1 of RX ring */
  72#define NESM_START_PG	0x40	/* First page of TX buffer */
  73#define NESM_STOP_PG	0x80	/* Last page +1 of RX ring */
  74
  75#define AX_GPOC_PPDSET	BIT(6)
  76
 
 
  77/* device private data */
  78
  79struct ax_device {
  80	struct mii_bus *mii_bus;
  81	struct mdiobb_ctrl bb_ctrl;
 
  82	void __iomem *addr_memr;
  83	u8 reg_memr;
  84	int link;
  85	int speed;
  86	int duplex;
  87
  88	void __iomem *map2;
  89	const struct ax_plat_data *plat;
  90
  91	unsigned char running;
  92	unsigned char resume_open;
  93	unsigned int irqflags;
  94
  95	u32 reg_offsets[0x20];
  96};
  97
  98static inline struct ax_device *to_ax_dev(struct net_device *dev)
  99{
 100	struct ei_device *ei_local = netdev_priv(dev);
 101	return (struct ax_device *)(ei_local + 1);
 102}
 103
 104/*
 105 * ax_initial_check
 106 *
 107 * do an initial probe for the card to check whether it exists
 108 * and is functional
 109 */
 110static int ax_initial_check(struct net_device *dev)
 111{
 112	struct ei_device *ei_local = netdev_priv(dev);
 113	void __iomem *ioaddr = ei_local->mem;
 114	int reg0;
 115	int regd;
 116
 117	reg0 = ei_inb(ioaddr);
 118	if (reg0 == 0xFF)
 119		return -ENODEV;
 120
 121	ei_outb(E8390_NODMA + E8390_PAGE1 + E8390_STOP, ioaddr + E8390_CMD);
 122	regd = ei_inb(ioaddr + 0x0d);
 123	ei_outb(0xff, ioaddr + 0x0d);
 124	ei_outb(E8390_NODMA + E8390_PAGE0, ioaddr + E8390_CMD);
 125	ei_inb(ioaddr + EN0_COUNTER0); /* Clear the counter by reading. */
 126	if (ei_inb(ioaddr + EN0_COUNTER0) != 0) {
 127		ei_outb(reg0, ioaddr);
 128		ei_outb(regd, ioaddr + 0x0d);	/* Restore the old values. */
 129		return -ENODEV;
 130	}
 131
 132	return 0;
 133}
 134
 135/*
 136 * Hard reset the card. This used to pause for the same period that a
 137 * 8390 reset command required, but that shouldn't be necessary.
 138 */
 139static void ax_reset_8390(struct net_device *dev)
 140{
 141	struct ei_device *ei_local = netdev_priv(dev);
 142	unsigned long reset_start_time = jiffies;
 143	void __iomem *addr = (void __iomem *)dev->base_addr;
 144
 145	netif_dbg(ei_local, hw, dev, "resetting the 8390 t=%ld...\n", jiffies);
 146
 147	ei_outb(ei_inb(addr + NE_RESET), addr + NE_RESET);
 148
 149	ei_local->txing = 0;
 150	ei_local->dmaing = 0;
 151
 152	/* This check _should_not_ be necessary, omit eventually. */
 153	while ((ei_inb(addr + EN0_ISR) & ENISR_RESET) == 0) {
 154		if (time_after(jiffies, reset_start_time + 2 * HZ / 100)) {
 155			netdev_warn(dev, "%s: did not complete.\n", __func__);
 156			break;
 157		}
 158	}
 159
 160	ei_outb(ENISR_RESET, addr + EN0_ISR);	/* Ack intr. */
 161}
 162
 163/* Wrapper for __ei_interrupt for platforms that have a platform-specific
 164 * way to find out whether the interrupt request might be caused by
 165 * the ax88796 chip.
 166 */
 167static irqreturn_t ax_ei_interrupt_filtered(int irq, void *dev_id)
 168{
 169	struct net_device *dev = dev_id;
 170	struct ax_device *ax = to_ax_dev(dev);
 171	struct platform_device *pdev = to_platform_device(dev->dev.parent);
 172
 173	if (!ax->plat->check_irq(pdev))
 174		return IRQ_NONE;
 175
 176	return ax_ei_interrupt(irq, dev_id);
 177}
 178
 179static void ax_get_8390_hdr(struct net_device *dev, struct e8390_pkt_hdr *hdr,
 180			    int ring_page)
 181{
 182	struct ei_device *ei_local = netdev_priv(dev);
 183	void __iomem *nic_base = ei_local->mem;
 184
 185	/* This *shouldn't* happen. If it does, it's the last thing you'll see */
 186	if (ei_local->dmaing) {
 187		netdev_err(dev, "DMAing conflict in %s "
 188			"[DMAstat:%d][irqlock:%d].\n",
 189			__func__,
 190			ei_local->dmaing, ei_local->irqlock);
 191		return;
 192	}
 193
 194	ei_local->dmaing |= 0x01;
 195	ei_outb(E8390_NODMA + E8390_PAGE0 + E8390_START, nic_base + NE_CMD);
 196	ei_outb(sizeof(struct e8390_pkt_hdr), nic_base + EN0_RCNTLO);
 197	ei_outb(0, nic_base + EN0_RCNTHI);
 198	ei_outb(0, nic_base + EN0_RSARLO);		/* On page boundary */
 199	ei_outb(ring_page, nic_base + EN0_RSARHI);
 200	ei_outb(E8390_RREAD+E8390_START, nic_base + NE_CMD);
 201
 202	if (ei_local->word16)
 203		ioread16_rep(nic_base + NE_DATAPORT, hdr,
 204			     sizeof(struct e8390_pkt_hdr) >> 1);
 205	else
 206		ioread8_rep(nic_base + NE_DATAPORT, hdr,
 207			    sizeof(struct e8390_pkt_hdr));
 208
 209	ei_outb(ENISR_RDC, nic_base + EN0_ISR);	/* Ack intr. */
 210	ei_local->dmaing &= ~0x01;
 211
 212	le16_to_cpus(&hdr->count);
 213}
 214
 215
 216/*
 217 * Block input and output, similar to the Crynwr packet driver. If
 218 * you are porting to a new ethercard, look at the packet driver
 219 * source for hints. The NEx000 doesn't share the on-board packet
 220 * memory -- you have to put the packet out through the "remote DMA"
 221 * dataport using ei_outb.
 222 */
 223static void ax_block_input(struct net_device *dev, int count,
 224			   struct sk_buff *skb, int ring_offset)
 225{
 226	struct ei_device *ei_local = netdev_priv(dev);
 227	void __iomem *nic_base = ei_local->mem;
 228	char *buf = skb->data;
 229
 230	if (ei_local->dmaing) {
 231		netdev_err(dev,
 232			"DMAing conflict in %s "
 233			"[DMAstat:%d][irqlock:%d].\n",
 234			__func__,
 235			ei_local->dmaing, ei_local->irqlock);
 236		return;
 237	}
 238
 239	ei_local->dmaing |= 0x01;
 240
 241	ei_outb(E8390_NODMA+E8390_PAGE0+E8390_START, nic_base + NE_CMD);
 242	ei_outb(count & 0xff, nic_base + EN0_RCNTLO);
 243	ei_outb(count >> 8, nic_base + EN0_RCNTHI);
 244	ei_outb(ring_offset & 0xff, nic_base + EN0_RSARLO);
 245	ei_outb(ring_offset >> 8, nic_base + EN0_RSARHI);
 246	ei_outb(E8390_RREAD+E8390_START, nic_base + NE_CMD);
 247
 248	if (ei_local->word16) {
 249		ioread16_rep(nic_base + NE_DATAPORT, buf, count >> 1);
 250		if (count & 0x01)
 251			buf[count-1] = ei_inb(nic_base + NE_DATAPORT);
 252
 253	} else {
 254		ioread8_rep(nic_base + NE_DATAPORT, buf, count);
 255	}
 256
 257	ei_local->dmaing &= ~1;
 258}
 259
 260static void ax_block_output(struct net_device *dev, int count,
 261			    const unsigned char *buf, const int start_page)
 262{
 263	struct ei_device *ei_local = netdev_priv(dev);
 264	void __iomem *nic_base = ei_local->mem;
 265	unsigned long dma_start;
 266
 267	/*
 268	 * Round the count up for word writes. Do we need to do this?
 269	 * What effect will an odd byte count have on the 8390?  I
 270	 * should check someday.
 271	 */
 272	if (ei_local->word16 && (count & 0x01))
 273		count++;
 274
 275	/* This *shouldn't* happen. If it does, it's the last thing you'll see */
 276	if (ei_local->dmaing) {
 277		netdev_err(dev, "DMAing conflict in %s."
 278			"[DMAstat:%d][irqlock:%d]\n",
 279			__func__,
 280		       ei_local->dmaing, ei_local->irqlock);
 281		return;
 282	}
 283
 284	ei_local->dmaing |= 0x01;
 285	/* We should already be in page 0, but to be safe... */
 286	ei_outb(E8390_PAGE0+E8390_START+E8390_NODMA, nic_base + NE_CMD);
 287
 288	ei_outb(ENISR_RDC, nic_base + EN0_ISR);
 289
 290	/* Now the normal output. */
 291	ei_outb(count & 0xff, nic_base + EN0_RCNTLO);
 292	ei_outb(count >> 8, nic_base + EN0_RCNTHI);
 293	ei_outb(0x00, nic_base + EN0_RSARLO);
 294	ei_outb(start_page, nic_base + EN0_RSARHI);
 295
 296	ei_outb(E8390_RWRITE+E8390_START, nic_base + NE_CMD);
 297	if (ei_local->word16)
 298		iowrite16_rep(nic_base + NE_DATAPORT, buf, count >> 1);
 299	else
 300		iowrite8_rep(nic_base + NE_DATAPORT, buf, count);
 301
 302	dma_start = jiffies;
 303
 304	while ((ei_inb(nic_base + EN0_ISR) & ENISR_RDC) == 0) {
 305		if (time_after(jiffies, dma_start + 2 * HZ / 100)) { /* 20ms */
 306			netdev_warn(dev, "timeout waiting for Tx RDC.\n");
 307			ax_reset_8390(dev);
 308			ax_NS8390_init(dev, 1);
 309			break;
 310		}
 311	}
 312
 313	ei_outb(ENISR_RDC, nic_base + EN0_ISR);	/* Ack intr. */
 314	ei_local->dmaing &= ~0x01;
 315}
 316
 317/* definitions for accessing MII/EEPROM interface */
 318
 319#define AX_MEMR			EI_SHIFT(0x14)
 320#define AX_MEMR_MDC		BIT(0)
 321#define AX_MEMR_MDIR		BIT(1)
 322#define AX_MEMR_MDI		BIT(2)
 323#define AX_MEMR_MDO		BIT(3)
 324#define AX_MEMR_EECS		BIT(4)
 325#define AX_MEMR_EEI		BIT(5)
 326#define AX_MEMR_EEO		BIT(6)
 327#define AX_MEMR_EECLK		BIT(7)
 328
 329static void ax_handle_link_change(struct net_device *dev)
 330{
 331	struct ax_device  *ax = to_ax_dev(dev);
 332	struct phy_device *phy_dev = dev->phydev;
 333	int status_change = 0;
 334
 335	if (phy_dev->link && ((ax->speed != phy_dev->speed) ||
 336			     (ax->duplex != phy_dev->duplex))) {
 337
 338		ax->speed = phy_dev->speed;
 339		ax->duplex = phy_dev->duplex;
 340		status_change = 1;
 341	}
 342
 343	if (phy_dev->link != ax->link) {
 344		if (!phy_dev->link) {
 345			ax->speed = 0;
 346			ax->duplex = -1;
 347		}
 348		ax->link = phy_dev->link;
 349
 350		status_change = 1;
 351	}
 352
 353	if (status_change)
 354		phy_print_status(phy_dev);
 355}
 356
 357static int ax_mii_probe(struct net_device *dev)
 358{
 359	struct ax_device  *ax = to_ax_dev(dev);
 360	struct phy_device *phy_dev = NULL;
 361	int ret;
 362
 363	/* find the first phy */
 364	phy_dev = phy_find_first(ax->mii_bus);
 365	if (!phy_dev) {
 366		netdev_err(dev, "no PHY found\n");
 367		return -ENODEV;
 368	}
 369
 370	ret = phy_connect_direct(dev, phy_dev, ax_handle_link_change,
 371				 PHY_INTERFACE_MODE_MII);
 372	if (ret) {
 373		netdev_err(dev, "Could not attach to PHY\n");
 374		return ret;
 375	}
 376
 377	phy_set_max_speed(phy_dev, SPEED_100);
 
 
 
 
 378
 379	netdev_info(dev, "PHY driver [%s] (mii_bus:phy_addr=%s, irq=%d)\n",
 380		    phy_dev->drv->name, phydev_name(phy_dev), phy_dev->irq);
 381
 382	return 0;
 383}
 384
 385static void ax_phy_switch(struct net_device *dev, int on)
 386{
 387	struct ei_device *ei_local = netdev_priv(dev);
 388	struct ax_device *ax = to_ax_dev(dev);
 389
 390	u8 reg_gpoc =  ax->plat->gpoc_val;
 391
 392	if (!!on)
 393		reg_gpoc &= ~AX_GPOC_PPDSET;
 394	else
 395		reg_gpoc |= AX_GPOC_PPDSET;
 396
 397	ei_outb(reg_gpoc, ei_local->mem + EI_SHIFT(0x17));
 398}
 399
 400static void ax_bb_mdc(struct mdiobb_ctrl *ctrl, int level)
 401{
 402	struct ax_device *ax = container_of(ctrl, struct ax_device, bb_ctrl);
 403
 404	if (level)
 405		ax->reg_memr |= AX_MEMR_MDC;
 406	else
 407		ax->reg_memr &= ~AX_MEMR_MDC;
 408
 409	ei_outb(ax->reg_memr, ax->addr_memr);
 410}
 411
 412static void ax_bb_dir(struct mdiobb_ctrl *ctrl, int output)
 413{
 414	struct ax_device *ax = container_of(ctrl, struct ax_device, bb_ctrl);
 415
 416	if (output)
 417		ax->reg_memr &= ~AX_MEMR_MDIR;
 418	else
 419		ax->reg_memr |= AX_MEMR_MDIR;
 420
 421	ei_outb(ax->reg_memr, ax->addr_memr);
 422}
 423
 424static void ax_bb_set_data(struct mdiobb_ctrl *ctrl, int value)
 425{
 426	struct ax_device *ax = container_of(ctrl, struct ax_device, bb_ctrl);
 427
 428	if (value)
 429		ax->reg_memr |= AX_MEMR_MDO;
 430	else
 431		ax->reg_memr &= ~AX_MEMR_MDO;
 432
 433	ei_outb(ax->reg_memr, ax->addr_memr);
 434}
 435
 436static int ax_bb_get_data(struct mdiobb_ctrl *ctrl)
 437{
 438	struct ax_device *ax = container_of(ctrl, struct ax_device, bb_ctrl);
 439	int reg_memr = ei_inb(ax->addr_memr);
 440
 441	return reg_memr & AX_MEMR_MDI ? 1 : 0;
 442}
 443
 444static const struct mdiobb_ops bb_ops = {
 445	.owner = THIS_MODULE,
 446	.set_mdc = ax_bb_mdc,
 447	.set_mdio_dir = ax_bb_dir,
 448	.set_mdio_data = ax_bb_set_data,
 449	.get_mdio_data = ax_bb_get_data,
 450};
 451
 452static int ax_mii_init(struct net_device *dev)
 453{
 454	struct platform_device *pdev = to_platform_device(dev->dev.parent);
 455	struct ei_device *ei_local = netdev_priv(dev);
 456	struct ax_device *ax = to_ax_dev(dev);
 457	int err;
 458
 459	ax->bb_ctrl.ops = &bb_ops;
 460	ax->addr_memr = ei_local->mem + AX_MEMR;
 461	ax->mii_bus = alloc_mdio_bitbang(&ax->bb_ctrl);
 462	if (!ax->mii_bus) {
 463		err = -ENOMEM;
 464		goto out;
 465	}
 466
 467	ax->mii_bus->name = "ax88796_mii_bus";
 468	ax->mii_bus->parent = dev->dev.parent;
 469	snprintf(ax->mii_bus->id, MII_BUS_ID_SIZE, "%s-%x",
 470		 pdev->name, pdev->id);
 471
 472	err = mdiobus_register(ax->mii_bus);
 473	if (err)
 474		goto out_free_mdio_bitbang;
 475
 476	return 0;
 477
 478 out_free_mdio_bitbang:
 479	free_mdio_bitbang(ax->mii_bus);
 480 out:
 481	return err;
 482}
 483
 484static int ax_open(struct net_device *dev)
 485{
 486	struct ax_device *ax = to_ax_dev(dev);
 487	int ret;
 488
 489	netdev_dbg(dev, "open\n");
 490
 491	ret = ax_mii_init(dev);
 492	if (ret)
 493		goto failed_mii;
 494
 495	if (ax->plat->check_irq)
 496		ret = request_irq(dev->irq, ax_ei_interrupt_filtered,
 497				  ax->irqflags, dev->name, dev);
 498	else
 499		ret = request_irq(dev->irq, ax_ei_interrupt, ax->irqflags,
 500				  dev->name, dev);
 501	if (ret)
 502		goto failed_request_irq;
 503
 504	/* turn the phy on (if turned off) */
 505	ax_phy_switch(dev, 1);
 506
 507	ret = ax_mii_probe(dev);
 508	if (ret)
 509		goto failed_mii_probe;
 510	phy_start(dev->phydev);
 511
 512	ret = ax_ei_open(dev);
 513	if (ret)
 514		goto failed_ax_ei_open;
 515
 516	ax->running = 1;
 517
 518	return 0;
 519
 520 failed_ax_ei_open:
 521	phy_disconnect(dev->phydev);
 522 failed_mii_probe:
 523	ax_phy_switch(dev, 0);
 524	free_irq(dev->irq, dev);
 525 failed_request_irq:
 526	/* unregister mdiobus */
 527	mdiobus_unregister(ax->mii_bus);
 528	free_mdio_bitbang(ax->mii_bus);
 529 failed_mii:
 530	return ret;
 531}
 532
 533static int ax_close(struct net_device *dev)
 534{
 535	struct ax_device *ax = to_ax_dev(dev);
 536
 537	netdev_dbg(dev, "close\n");
 538
 539	ax->running = 0;
 540	wmb();
 541
 542	ax_ei_close(dev);
 543
 544	/* turn the phy off */
 545	ax_phy_switch(dev, 0);
 546	phy_disconnect(dev->phydev);
 547
 548	free_irq(dev->irq, dev);
 549
 550	mdiobus_unregister(ax->mii_bus);
 551	free_mdio_bitbang(ax->mii_bus);
 552	return 0;
 553}
 554
 555static int ax_ioctl(struct net_device *dev, struct ifreq *req, int cmd)
 556{
 557	struct phy_device *phy_dev = dev->phydev;
 
 558
 559	if (!netif_running(dev))
 560		return -EINVAL;
 561
 562	if (!phy_dev)
 563		return -ENODEV;
 564
 565	return phy_mii_ioctl(phy_dev, req, cmd);
 566}
 567
 568/* ethtool ops */
 569
 570static void ax_get_drvinfo(struct net_device *dev,
 571			   struct ethtool_drvinfo *info)
 572{
 573	struct platform_device *pdev = to_platform_device(dev->dev.parent);
 574
 575	strlcpy(info->driver, DRV_NAME, sizeof(info->driver));
 576	strlcpy(info->version, DRV_VERSION, sizeof(info->version));
 577	strlcpy(info->bus_info, pdev->name, sizeof(info->bus_info));
 578}
 579
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 580static u32 ax_get_msglevel(struct net_device *dev)
 581{
 582	struct ei_device *ei_local = netdev_priv(dev);
 583
 584	return ei_local->msg_enable;
 585}
 586
 587static void ax_set_msglevel(struct net_device *dev, u32 v)
 588{
 589	struct ei_device *ei_local = netdev_priv(dev);
 590
 591	ei_local->msg_enable = v;
 592}
 593
 594static const struct ethtool_ops ax_ethtool_ops = {
 595	.get_drvinfo		= ax_get_drvinfo,
 
 
 596	.get_link		= ethtool_op_get_link,
 597	.get_ts_info		= ethtool_op_get_ts_info,
 598	.get_msglevel		= ax_get_msglevel,
 599	.set_msglevel		= ax_set_msglevel,
 600	.get_link_ksettings	= phy_ethtool_get_link_ksettings,
 601	.set_link_ksettings	= phy_ethtool_set_link_ksettings,
 602};
 603
 604#ifdef CONFIG_AX88796_93CX6
 605static void ax_eeprom_register_read(struct eeprom_93cx6 *eeprom)
 606{
 607	struct ei_device *ei_local = eeprom->data;
 608	u8 reg = ei_inb(ei_local->mem + AX_MEMR);
 609
 610	eeprom->reg_data_in = reg & AX_MEMR_EEI;
 611	eeprom->reg_data_out = reg & AX_MEMR_EEO; /* Input pin */
 612	eeprom->reg_data_clock = reg & AX_MEMR_EECLK;
 613	eeprom->reg_chip_select = reg & AX_MEMR_EECS;
 614}
 615
 616static void ax_eeprom_register_write(struct eeprom_93cx6 *eeprom)
 617{
 618	struct ei_device *ei_local = eeprom->data;
 619	u8 reg = ei_inb(ei_local->mem + AX_MEMR);
 620
 621	reg &= ~(AX_MEMR_EEI | AX_MEMR_EECLK | AX_MEMR_EECS);
 622
 623	if (eeprom->reg_data_in)
 624		reg |= AX_MEMR_EEI;
 625	if (eeprom->reg_data_clock)
 626		reg |= AX_MEMR_EECLK;
 627	if (eeprom->reg_chip_select)
 628		reg |= AX_MEMR_EECS;
 629
 630	ei_outb(reg, ei_local->mem + AX_MEMR);
 631	udelay(10);
 632}
 633#endif
 634
 635static const struct net_device_ops ax_netdev_ops = {
 636	.ndo_open		= ax_open,
 637	.ndo_stop		= ax_close,
 638	.ndo_do_ioctl		= ax_ioctl,
 639
 640	.ndo_start_xmit		= ax_ei_start_xmit,
 641	.ndo_tx_timeout		= ax_ei_tx_timeout,
 642	.ndo_get_stats		= ax_ei_get_stats,
 643	.ndo_set_rx_mode	= ax_ei_set_multicast_list,
 644	.ndo_validate_addr	= eth_validate_addr,
 645	.ndo_set_mac_address	= eth_mac_addr,
 
 646#ifdef CONFIG_NET_POLL_CONTROLLER
 647	.ndo_poll_controller	= ax_ei_poll,
 648#endif
 649};
 650
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 651/* setup code */
 652
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 653static void ax_initial_setup(struct net_device *dev, struct ei_device *ei_local)
 654{
 655	void __iomem *ioaddr = ei_local->mem;
 656	struct ax_device *ax = to_ax_dev(dev);
 657
 658	/* Select page 0 */
 659	ei_outb(E8390_NODMA + E8390_PAGE0 + E8390_STOP, ioaddr + E8390_CMD);
 660
 661	/* set to byte access */
 662	ei_outb(ax->plat->dcr_val & ~1, ioaddr + EN0_DCFG);
 663	ei_outb(ax->plat->gpoc_val, ioaddr + EI_SHIFT(0x17));
 664}
 665
 666/*
 667 * ax_init_dev
 668 *
 669 * initialise the specified device, taking care to note the MAC
 670 * address it may already have (if configured), ensure
 671 * the device is ready to be used by lib8390.c and registerd with
 672 * the network layer.
 673 */
 674static int ax_init_dev(struct net_device *dev)
 675{
 676	struct ei_device *ei_local = netdev_priv(dev);
 677	struct ax_device *ax = to_ax_dev(dev);
 678	void __iomem *ioaddr = ei_local->mem;
 679	unsigned int start_page;
 680	unsigned int stop_page;
 681	int ret;
 682	int i;
 683
 684	ret = ax_initial_check(dev);
 685	if (ret)
 686		goto err_out;
 687
 688	/* setup goes here */
 689
 690	ax_initial_setup(dev, ei_local);
 691
 692	/* read the mac from the card prom if we need it */
 693
 694	if (ax->plat->flags & AXFLG_HAS_EEPROM) {
 695		unsigned char SA_prom[32];
 696
 697		ei_outb(6, ioaddr + EN0_RCNTLO);
 698		ei_outb(0, ioaddr + EN0_RCNTHI);
 699		ei_outb(0, ioaddr + EN0_RSARLO);
 700		ei_outb(0, ioaddr + EN0_RSARHI);
 701		ei_outb(E8390_RREAD + E8390_START, ioaddr + NE_CMD);
 702		for (i = 0; i < sizeof(SA_prom); i += 2) {
 703			SA_prom[i] = ei_inb(ioaddr + NE_DATAPORT);
 704			SA_prom[i + 1] = ei_inb(ioaddr + NE_DATAPORT);
 705		}
 706		ei_outb(ENISR_RDC, ioaddr + EN0_ISR);	/* Ack intr. */
 707
 708		if (ax->plat->wordlength == 2)
 709			for (i = 0; i < 16; i++)
 710				SA_prom[i] = SA_prom[i+i];
 711
 712		memcpy(dev->dev_addr, SA_prom, ETH_ALEN);
 713	}
 714
 715#ifdef CONFIG_AX88796_93CX6
 716	if (ax->plat->flags & AXFLG_HAS_93CX6) {
 717		unsigned char mac_addr[ETH_ALEN];
 718		struct eeprom_93cx6 eeprom;
 719
 720		eeprom.data = ei_local;
 721		eeprom.register_read = ax_eeprom_register_read;
 722		eeprom.register_write = ax_eeprom_register_write;
 723		eeprom.width = PCI_EEPROM_WIDTH_93C56;
 724
 725		eeprom_93cx6_multiread(&eeprom, 0,
 726				       (__le16 __force *)mac_addr,
 727				       sizeof(mac_addr) >> 1);
 728
 729		memcpy(dev->dev_addr, mac_addr, ETH_ALEN);
 730	}
 731#endif
 732	if (ax->plat->wordlength == 2) {
 733		/* We must set the 8390 for word mode. */
 734		ei_outb(ax->plat->dcr_val, ei_local->mem + EN0_DCFG);
 735		start_page = NESM_START_PG;
 736		stop_page = NESM_STOP_PG;
 737	} else {
 738		start_page = NE1SM_START_PG;
 739		stop_page = NE1SM_STOP_PG;
 740	}
 741
 742	/* load the mac-address from the device */
 743	if (ax->plat->flags & AXFLG_MAC_FROMDEV) {
 744		ei_outb(E8390_NODMA + E8390_PAGE1 + E8390_STOP,
 745			ei_local->mem + E8390_CMD); /* 0x61 */
 746		for (i = 0; i < ETH_ALEN; i++)
 747			dev->dev_addr[i] =
 748				ei_inb(ioaddr + EN1_PHYS_SHIFT(i));
 749	}
 750
 751	if ((ax->plat->flags & AXFLG_MAC_FROMPLATFORM) &&
 752	    ax->plat->mac_addr)
 753		memcpy(dev->dev_addr, ax->plat->mac_addr, ETH_ALEN);
 754
 755	if (!is_valid_ether_addr(dev->dev_addr)) {
 756		eth_hw_addr_random(dev);
 757		dev_info(&dev->dev, "Using random MAC address: %pM\n",
 758			 dev->dev_addr);
 759	}
 760
 761	ax_reset_8390(dev);
 762
 763	ei_local->name = "AX88796";
 764	ei_local->tx_start_page = start_page;
 765	ei_local->stop_page = stop_page;
 766	ei_local->word16 = (ax->plat->wordlength == 2);
 767	ei_local->rx_start_page = start_page + TX_PAGES;
 768
 769#ifdef PACKETBUF_MEMSIZE
 770	/* Allow the packet buffer size to be overridden by know-it-alls. */
 771	ei_local->stop_page = ei_local->tx_start_page + PACKETBUF_MEMSIZE;
 772#endif
 773
 774	ei_local->reset_8390 = &ax_reset_8390;
 775	if (ax->plat->block_input)
 776		ei_local->block_input = ax->plat->block_input;
 777	else
 778		ei_local->block_input = &ax_block_input;
 779	if (ax->plat->block_output)
 780		ei_local->block_output = ax->plat->block_output;
 781	else
 782		ei_local->block_output = &ax_block_output;
 783	ei_local->get_8390_hdr = &ax_get_8390_hdr;
 784	ei_local->priv = 0;
 
 785
 786	dev->netdev_ops = &ax_netdev_ops;
 787	dev->ethtool_ops = &ax_ethtool_ops;
 788
 
 
 
 
 789	ax_NS8390_init(dev, 0);
 790
 791	ret = register_netdev(dev);
 792	if (ret)
 793		goto err_out;
 794
 795	netdev_info(dev, "%dbit, irq %d, %lx, MAC: %pM\n",
 796		    ei_local->word16 ? 16 : 8, dev->irq, dev->base_addr,
 797		    dev->dev_addr);
 798
 799	return 0;
 800
 
 
 
 801 err_out:
 802	return ret;
 803}
 804
 805static int ax_remove(struct platform_device *pdev)
 806{
 807	struct net_device *dev = platform_get_drvdata(pdev);
 808	struct ei_device *ei_local = netdev_priv(dev);
 809	struct ax_device *ax = to_ax_dev(dev);
 810	struct resource *mem;
 811
 812	unregister_netdev(dev);
 
 813
 814	iounmap(ei_local->mem);
 815	mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
 816	release_mem_region(mem->start, resource_size(mem));
 817
 818	if (ax->map2) {
 819		iounmap(ax->map2);
 820		mem = platform_get_resource(pdev, IORESOURCE_MEM, 1);
 821		release_mem_region(mem->start, resource_size(mem));
 822	}
 823
 824	platform_set_drvdata(pdev, NULL);
 825	free_netdev(dev);
 826
 827	return 0;
 828}
 829
 830/*
 831 * ax_probe
 832 *
 833 * This is the entry point when the platform device system uses to
 834 * notify us of a new device to attach to. Allocate memory, find the
 835 * resources and information passed, and map the necessary registers.
 836 */
 837static int ax_probe(struct platform_device *pdev)
 838{
 839	struct net_device *dev;
 840	struct ei_device *ei_local;
 841	struct ax_device *ax;
 842	struct resource *irq, *mem, *mem2;
 843	unsigned long mem_size, mem2_size = 0;
 844	int ret = 0;
 845
 846	dev = ax__alloc_ei_netdev(sizeof(struct ax_device));
 847	if (dev == NULL)
 848		return -ENOMEM;
 849
 850	/* ok, let's setup our device */
 851	SET_NETDEV_DEV(dev, &pdev->dev);
 852	ei_local = netdev_priv(dev);
 853	ax = to_ax_dev(dev);
 854
 855	ax->plat = dev_get_platdata(&pdev->dev);
 856	platform_set_drvdata(pdev, dev);
 857
 858	ei_local->rxcr_base = ax->plat->rcr_val;
 859
 860	/* find the platform resources */
 861	irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
 862	if (!irq) {
 863		dev_err(&pdev->dev, "no IRQ specified\n");
 864		ret = -ENXIO;
 865		goto exit_mem;
 866	}
 867
 868	dev->irq = irq->start;
 869	ax->irqflags = irq->flags & IRQF_TRIGGER_MASK;
 870
 871	if (irq->flags &  IORESOURCE_IRQ_SHAREABLE)
 872		ax->irqflags |= IRQF_SHARED;
 873
 874	mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
 875	if (!mem) {
 876		dev_err(&pdev->dev, "no MEM specified\n");
 877		ret = -ENXIO;
 878		goto exit_mem;
 879	}
 880
 881	mem_size = resource_size(mem);
 882
 883	/*
 884	 * setup the register offsets from either the platform data or
 885	 * by using the size of the resource provided
 886	 */
 887	if (ax->plat->reg_offsets)
 888		ei_local->reg_offset = ax->plat->reg_offsets;
 889	else {
 890		ei_local->reg_offset = ax->reg_offsets;
 891		for (ret = 0; ret < 0x18; ret++)
 892			ax->reg_offsets[ret] = (mem_size / 0x18) * ret;
 893	}
 894
 895	if (!request_mem_region(mem->start, mem_size, pdev->name)) {
 896		dev_err(&pdev->dev, "cannot reserve registers\n");
 897		ret = -ENXIO;
 898		goto exit_mem;
 899	}
 900
 901	ei_local->mem = ioremap(mem->start, mem_size);
 902	dev->base_addr = (unsigned long)ei_local->mem;
 903
 904	if (ei_local->mem == NULL) {
 905		dev_err(&pdev->dev, "Cannot ioremap area %pR\n", mem);
 906
 907		ret = -ENXIO;
 908		goto exit_req;
 909	}
 910
 911	/* look for reset area */
 912	mem2 = platform_get_resource(pdev, IORESOURCE_MEM, 1);
 913	if (!mem2) {
 914		if (!ax->plat->reg_offsets) {
 915			for (ret = 0; ret < 0x20; ret++)
 916				ax->reg_offsets[ret] = (mem_size / 0x20) * ret;
 917		}
 918	} else {
 919		mem2_size = resource_size(mem2);
 920
 921		if (!request_mem_region(mem2->start, mem2_size, pdev->name)) {
 922			dev_err(&pdev->dev, "cannot reserve registers\n");
 923			ret = -ENXIO;
 924			goto exit_mem1;
 925		}
 926
 927		ax->map2 = ioremap(mem2->start, mem2_size);
 928		if (!ax->map2) {
 929			dev_err(&pdev->dev, "cannot map reset register\n");
 930			ret = -ENXIO;
 931			goto exit_mem2;
 932		}
 933
 934		ei_local->reg_offset[0x1f] = ax->map2 - ei_local->mem;
 935	}
 936
 937	/* got resources, now initialise and register device */
 938	ret = ax_init_dev(dev);
 939	if (!ret)
 940		return 0;
 941
 942	if (!ax->map2)
 943		goto exit_mem1;
 944
 945	iounmap(ax->map2);
 946
 947 exit_mem2:
 948	if (mem2)
 949		release_mem_region(mem2->start, mem2_size);
 950
 951 exit_mem1:
 952	iounmap(ei_local->mem);
 953
 954 exit_req:
 955	release_mem_region(mem->start, mem_size);
 956
 957 exit_mem:
 958	platform_set_drvdata(pdev, NULL);
 959	free_netdev(dev);
 960
 961	return ret;
 962}
 963
 964/* suspend and resume */
 965
 966#ifdef CONFIG_PM
 967static int ax_suspend(struct platform_device *dev, pm_message_t state)
 968{
 969	struct net_device *ndev = platform_get_drvdata(dev);
 970	struct ax_device *ax = to_ax_dev(ndev);
 971
 972	ax->resume_open = ax->running;
 973
 974	netif_device_detach(ndev);
 975	ax_close(ndev);
 976
 977	return 0;
 978}
 979
 980static int ax_resume(struct platform_device *pdev)
 981{
 982	struct net_device *ndev = platform_get_drvdata(pdev);
 983	struct ax_device *ax = to_ax_dev(ndev);
 984
 985	ax_initial_setup(ndev, netdev_priv(ndev));
 986	ax_NS8390_init(ndev, ax->resume_open);
 987	netif_device_attach(ndev);
 988
 989	if (ax->resume_open)
 990		ax_open(ndev);
 991
 992	return 0;
 993}
 994
 995#else
 996#define ax_suspend NULL
 997#define ax_resume NULL
 998#endif
 999
1000static struct platform_driver axdrv = {
1001	.driver	= {
1002		.name		= "ax88796",
1003	},
1004	.probe		= ax_probe,
1005	.remove		= ax_remove,
1006	.suspend	= ax_suspend,
1007	.resume		= ax_resume,
1008};
1009
1010module_platform_driver(axdrv);
1011
1012MODULE_DESCRIPTION("AX88796 10/100 Ethernet platform driver");
1013MODULE_AUTHOR("Ben Dooks, <ben@simtec.co.uk>");
1014MODULE_LICENSE("GPL v2");
1015MODULE_ALIAS("platform:ax88796");
v4.6
 
   1/* drivers/net/ethernet/8390/ax88796.c
   2 *
   3 * Copyright 2005,2007 Simtec Electronics
   4 *	Ben Dooks <ben@simtec.co.uk>
   5 *
   6 * Asix AX88796 10/100 Ethernet controller support
   7 *	Based on ne.c, by Donald Becker, et-al.
   8 *
   9 * This program is free software; you can redistribute it and/or modify
  10 * it under the terms of the GNU General Public License version 2 as
  11 * published by the Free Software Foundation.
  12 */
  13
  14#include <linux/module.h>
  15#include <linux/kernel.h>
  16#include <linux/errno.h>
  17#include <linux/isapnp.h>
  18#include <linux/interrupt.h>
  19#include <linux/io.h>
  20#include <linux/platform_device.h>
  21#include <linux/delay.h>
  22#include <linux/timer.h>
  23#include <linux/netdevice.h>
  24#include <linux/etherdevice.h>
  25#include <linux/ethtool.h>
  26#include <linux/mdio-bitbang.h>
  27#include <linux/phy.h>
  28#include <linux/eeprom_93cx6.h>
  29#include <linux/slab.h>
  30
  31#include <net/ax88796.h>
  32
  33
  34/* Rename the lib8390.c functions to show that they are in this driver */
  35#define __ei_open ax_ei_open
  36#define __ei_close ax_ei_close
  37#define __ei_poll ax_ei_poll
  38#define __ei_start_xmit ax_ei_start_xmit
  39#define __ei_tx_timeout ax_ei_tx_timeout
  40#define __ei_get_stats ax_ei_get_stats
  41#define __ei_set_multicast_list ax_ei_set_multicast_list
  42#define __ei_interrupt ax_ei_interrupt
  43#define ____alloc_ei_netdev ax__alloc_ei_netdev
  44#define __NS8390_init ax_NS8390_init
  45
  46/* force unsigned long back to 'void __iomem *' */
  47#define ax_convert_addr(_a) ((void __force __iomem *)(_a))
  48
  49#define ei_inb(_a) readb(ax_convert_addr(_a))
  50#define ei_outb(_v, _a) writeb(_v, ax_convert_addr(_a))
  51
  52#define ei_inb_p(_a) ei_inb(_a)
  53#define ei_outb_p(_v, _a) ei_outb(_v, _a)
  54
  55/* define EI_SHIFT() to take into account our register offsets */
  56#define EI_SHIFT(x) (ei_local->reg_offset[(x)])
  57
  58/* Ensure we have our RCR base value */
  59#define AX88796_PLATFORM
  60
  61static unsigned char version[] = "ax88796.c: Copyright 2005,2007 Simtec Electronics\n";
  62
  63#include "lib8390.c"
  64
  65#define DRV_NAME "ax88796"
  66#define DRV_VERSION "1.00"
  67
  68/* from ne.c */
  69#define NE_CMD		EI_SHIFT(0x00)
  70#define NE_RESET	EI_SHIFT(0x1f)
  71#define NE_DATAPORT	EI_SHIFT(0x10)
  72
  73#define NE1SM_START_PG	0x20	/* First page of TX buffer */
  74#define NE1SM_STOP_PG	0x40	/* Last page +1 of RX ring */
  75#define NESM_START_PG	0x40	/* First page of TX buffer */
  76#define NESM_STOP_PG	0x80	/* Last page +1 of RX ring */
  77
  78#define AX_GPOC_PPDSET	BIT(6)
  79
  80static u32 ax_msg_enable;
  81
  82/* device private data */
  83
  84struct ax_device {
  85	struct mii_bus *mii_bus;
  86	struct mdiobb_ctrl bb_ctrl;
  87	struct phy_device *phy_dev;
  88	void __iomem *addr_memr;
  89	u8 reg_memr;
  90	int link;
  91	int speed;
  92	int duplex;
  93
  94	void __iomem *map2;
  95	const struct ax_plat_data *plat;
  96
  97	unsigned char running;
  98	unsigned char resume_open;
  99	unsigned int irqflags;
 100
 101	u32 reg_offsets[0x20];
 102};
 103
 104static inline struct ax_device *to_ax_dev(struct net_device *dev)
 105{
 106	struct ei_device *ei_local = netdev_priv(dev);
 107	return (struct ax_device *)(ei_local + 1);
 108}
 109
 110/*
 111 * ax_initial_check
 112 *
 113 * do an initial probe for the card to check whether it exists
 114 * and is functional
 115 */
 116static int ax_initial_check(struct net_device *dev)
 117{
 118	struct ei_device *ei_local = netdev_priv(dev);
 119	void __iomem *ioaddr = ei_local->mem;
 120	int reg0;
 121	int regd;
 122
 123	reg0 = ei_inb(ioaddr);
 124	if (reg0 == 0xFF)
 125		return -ENODEV;
 126
 127	ei_outb(E8390_NODMA + E8390_PAGE1 + E8390_STOP, ioaddr + E8390_CMD);
 128	regd = ei_inb(ioaddr + 0x0d);
 129	ei_outb(0xff, ioaddr + 0x0d);
 130	ei_outb(E8390_NODMA + E8390_PAGE0, ioaddr + E8390_CMD);
 131	ei_inb(ioaddr + EN0_COUNTER0); /* Clear the counter by reading. */
 132	if (ei_inb(ioaddr + EN0_COUNTER0) != 0) {
 133		ei_outb(reg0, ioaddr);
 134		ei_outb(regd, ioaddr + 0x0d);	/* Restore the old values. */
 135		return -ENODEV;
 136	}
 137
 138	return 0;
 139}
 140
 141/*
 142 * Hard reset the card. This used to pause for the same period that a
 143 * 8390 reset command required, but that shouldn't be necessary.
 144 */
 145static void ax_reset_8390(struct net_device *dev)
 146{
 147	struct ei_device *ei_local = netdev_priv(dev);
 148	unsigned long reset_start_time = jiffies;
 149	void __iomem *addr = (void __iomem *)dev->base_addr;
 150
 151	netif_dbg(ei_local, hw, dev, "resetting the 8390 t=%ld...\n", jiffies);
 152
 153	ei_outb(ei_inb(addr + NE_RESET), addr + NE_RESET);
 154
 155	ei_local->txing = 0;
 156	ei_local->dmaing = 0;
 157
 158	/* This check _should_not_ be necessary, omit eventually. */
 159	while ((ei_inb(addr + EN0_ISR) & ENISR_RESET) == 0) {
 160		if (time_after(jiffies, reset_start_time + 2 * HZ / 100)) {
 161			netdev_warn(dev, "%s: did not complete.\n", __func__);
 162			break;
 163		}
 164	}
 165
 166	ei_outb(ENISR_RESET, addr + EN0_ISR);	/* Ack intr. */
 167}
 168
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 169
 170static void ax_get_8390_hdr(struct net_device *dev, struct e8390_pkt_hdr *hdr,
 171			    int ring_page)
 172{
 173	struct ei_device *ei_local = netdev_priv(dev);
 174	void __iomem *nic_base = ei_local->mem;
 175
 176	/* This *shouldn't* happen. If it does, it's the last thing you'll see */
 177	if (ei_local->dmaing) {
 178		netdev_err(dev, "DMAing conflict in %s "
 179			"[DMAstat:%d][irqlock:%d].\n",
 180			__func__,
 181			ei_local->dmaing, ei_local->irqlock);
 182		return;
 183	}
 184
 185	ei_local->dmaing |= 0x01;
 186	ei_outb(E8390_NODMA + E8390_PAGE0 + E8390_START, nic_base + NE_CMD);
 187	ei_outb(sizeof(struct e8390_pkt_hdr), nic_base + EN0_RCNTLO);
 188	ei_outb(0, nic_base + EN0_RCNTHI);
 189	ei_outb(0, nic_base + EN0_RSARLO);		/* On page boundary */
 190	ei_outb(ring_page, nic_base + EN0_RSARHI);
 191	ei_outb(E8390_RREAD+E8390_START, nic_base + NE_CMD);
 192
 193	if (ei_local->word16)
 194		ioread16_rep(nic_base + NE_DATAPORT, hdr,
 195			     sizeof(struct e8390_pkt_hdr) >> 1);
 196	else
 197		ioread8_rep(nic_base + NE_DATAPORT, hdr,
 198			    sizeof(struct e8390_pkt_hdr));
 199
 200	ei_outb(ENISR_RDC, nic_base + EN0_ISR);	/* Ack intr. */
 201	ei_local->dmaing &= ~0x01;
 202
 203	le16_to_cpus(&hdr->count);
 204}
 205
 206
 207/*
 208 * Block input and output, similar to the Crynwr packet driver. If
 209 * you are porting to a new ethercard, look at the packet driver
 210 * source for hints. The NEx000 doesn't share the on-board packet
 211 * memory -- you have to put the packet out through the "remote DMA"
 212 * dataport using ei_outb.
 213 */
 214static void ax_block_input(struct net_device *dev, int count,
 215			   struct sk_buff *skb, int ring_offset)
 216{
 217	struct ei_device *ei_local = netdev_priv(dev);
 218	void __iomem *nic_base = ei_local->mem;
 219	char *buf = skb->data;
 220
 221	if (ei_local->dmaing) {
 222		netdev_err(dev,
 223			"DMAing conflict in %s "
 224			"[DMAstat:%d][irqlock:%d].\n",
 225			__func__,
 226			ei_local->dmaing, ei_local->irqlock);
 227		return;
 228	}
 229
 230	ei_local->dmaing |= 0x01;
 231
 232	ei_outb(E8390_NODMA+E8390_PAGE0+E8390_START, nic_base + NE_CMD);
 233	ei_outb(count & 0xff, nic_base + EN0_RCNTLO);
 234	ei_outb(count >> 8, nic_base + EN0_RCNTHI);
 235	ei_outb(ring_offset & 0xff, nic_base + EN0_RSARLO);
 236	ei_outb(ring_offset >> 8, nic_base + EN0_RSARHI);
 237	ei_outb(E8390_RREAD+E8390_START, nic_base + NE_CMD);
 238
 239	if (ei_local->word16) {
 240		ioread16_rep(nic_base + NE_DATAPORT, buf, count >> 1);
 241		if (count & 0x01)
 242			buf[count-1] = ei_inb(nic_base + NE_DATAPORT);
 243
 244	} else {
 245		ioread8_rep(nic_base + NE_DATAPORT, buf, count);
 246	}
 247
 248	ei_local->dmaing &= ~1;
 249}
 250
 251static void ax_block_output(struct net_device *dev, int count,
 252			    const unsigned char *buf, const int start_page)
 253{
 254	struct ei_device *ei_local = netdev_priv(dev);
 255	void __iomem *nic_base = ei_local->mem;
 256	unsigned long dma_start;
 257
 258	/*
 259	 * Round the count up for word writes. Do we need to do this?
 260	 * What effect will an odd byte count have on the 8390?  I
 261	 * should check someday.
 262	 */
 263	if (ei_local->word16 && (count & 0x01))
 264		count++;
 265
 266	/* This *shouldn't* happen. If it does, it's the last thing you'll see */
 267	if (ei_local->dmaing) {
 268		netdev_err(dev, "DMAing conflict in %s."
 269			"[DMAstat:%d][irqlock:%d]\n",
 270			__func__,
 271		       ei_local->dmaing, ei_local->irqlock);
 272		return;
 273	}
 274
 275	ei_local->dmaing |= 0x01;
 276	/* We should already be in page 0, but to be safe... */
 277	ei_outb(E8390_PAGE0+E8390_START+E8390_NODMA, nic_base + NE_CMD);
 278
 279	ei_outb(ENISR_RDC, nic_base + EN0_ISR);
 280
 281	/* Now the normal output. */
 282	ei_outb(count & 0xff, nic_base + EN0_RCNTLO);
 283	ei_outb(count >> 8, nic_base + EN0_RCNTHI);
 284	ei_outb(0x00, nic_base + EN0_RSARLO);
 285	ei_outb(start_page, nic_base + EN0_RSARHI);
 286
 287	ei_outb(E8390_RWRITE+E8390_START, nic_base + NE_CMD);
 288	if (ei_local->word16)
 289		iowrite16_rep(nic_base + NE_DATAPORT, buf, count >> 1);
 290	else
 291		iowrite8_rep(nic_base + NE_DATAPORT, buf, count);
 292
 293	dma_start = jiffies;
 294
 295	while ((ei_inb(nic_base + EN0_ISR) & ENISR_RDC) == 0) {
 296		if (time_after(jiffies, dma_start + 2 * HZ / 100)) { /* 20ms */
 297			netdev_warn(dev, "timeout waiting for Tx RDC.\n");
 298			ax_reset_8390(dev);
 299			ax_NS8390_init(dev, 1);
 300			break;
 301		}
 302	}
 303
 304	ei_outb(ENISR_RDC, nic_base + EN0_ISR);	/* Ack intr. */
 305	ei_local->dmaing &= ~0x01;
 306}
 307
 308/* definitions for accessing MII/EEPROM interface */
 309
 310#define AX_MEMR			EI_SHIFT(0x14)
 311#define AX_MEMR_MDC		BIT(0)
 312#define AX_MEMR_MDIR		BIT(1)
 313#define AX_MEMR_MDI		BIT(2)
 314#define AX_MEMR_MDO		BIT(3)
 315#define AX_MEMR_EECS		BIT(4)
 316#define AX_MEMR_EEI		BIT(5)
 317#define AX_MEMR_EEO		BIT(6)
 318#define AX_MEMR_EECLK		BIT(7)
 319
 320static void ax_handle_link_change(struct net_device *dev)
 321{
 322	struct ax_device  *ax = to_ax_dev(dev);
 323	struct phy_device *phy_dev = ax->phy_dev;
 324	int status_change = 0;
 325
 326	if (phy_dev->link && ((ax->speed != phy_dev->speed) ||
 327			     (ax->duplex != phy_dev->duplex))) {
 328
 329		ax->speed = phy_dev->speed;
 330		ax->duplex = phy_dev->duplex;
 331		status_change = 1;
 332	}
 333
 334	if (phy_dev->link != ax->link) {
 335		if (!phy_dev->link) {
 336			ax->speed = 0;
 337			ax->duplex = -1;
 338		}
 339		ax->link = phy_dev->link;
 340
 341		status_change = 1;
 342	}
 343
 344	if (status_change)
 345		phy_print_status(phy_dev);
 346}
 347
 348static int ax_mii_probe(struct net_device *dev)
 349{
 350	struct ax_device  *ax = to_ax_dev(dev);
 351	struct phy_device *phy_dev = NULL;
 352	int ret;
 353
 354	/* find the first phy */
 355	phy_dev = phy_find_first(ax->mii_bus);
 356	if (!phy_dev) {
 357		netdev_err(dev, "no PHY found\n");
 358		return -ENODEV;
 359	}
 360
 361	ret = phy_connect_direct(dev, phy_dev, ax_handle_link_change,
 362				 PHY_INTERFACE_MODE_MII);
 363	if (ret) {
 364		netdev_err(dev, "Could not attach to PHY\n");
 365		return ret;
 366	}
 367
 368	/* mask with MAC supported features */
 369	phy_dev->supported &= PHY_BASIC_FEATURES;
 370	phy_dev->advertising = phy_dev->supported;
 371
 372	ax->phy_dev = phy_dev;
 373
 374	netdev_info(dev, "PHY driver [%s] (mii_bus:phy_addr=%s, irq=%d)\n",
 375		    phy_dev->drv->name, phydev_name(phy_dev), phy_dev->irq);
 376
 377	return 0;
 378}
 379
 380static void ax_phy_switch(struct net_device *dev, int on)
 381{
 382	struct ei_device *ei_local = netdev_priv(dev);
 383	struct ax_device *ax = to_ax_dev(dev);
 384
 385	u8 reg_gpoc =  ax->plat->gpoc_val;
 386
 387	if (!!on)
 388		reg_gpoc &= ~AX_GPOC_PPDSET;
 389	else
 390		reg_gpoc |= AX_GPOC_PPDSET;
 391
 392	ei_outb(reg_gpoc, ei_local->mem + EI_SHIFT(0x17));
 393}
 394
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 395static int ax_open(struct net_device *dev)
 396{
 397	struct ax_device *ax = to_ax_dev(dev);
 398	int ret;
 399
 400	netdev_dbg(dev, "open\n");
 401
 402	ret = request_irq(dev->irq, ax_ei_interrupt, ax->irqflags,
 403			  dev->name, dev);
 
 
 
 
 
 
 
 
 404	if (ret)
 405		goto failed_request_irq;
 406
 407	/* turn the phy on (if turned off) */
 408	ax_phy_switch(dev, 1);
 409
 410	ret = ax_mii_probe(dev);
 411	if (ret)
 412		goto failed_mii_probe;
 413	phy_start(ax->phy_dev);
 414
 415	ret = ax_ei_open(dev);
 416	if (ret)
 417		goto failed_ax_ei_open;
 418
 419	ax->running = 1;
 420
 421	return 0;
 422
 423 failed_ax_ei_open:
 424	phy_disconnect(ax->phy_dev);
 425 failed_mii_probe:
 426	ax_phy_switch(dev, 0);
 427	free_irq(dev->irq, dev);
 428 failed_request_irq:
 
 
 
 
 429	return ret;
 430}
 431
 432static int ax_close(struct net_device *dev)
 433{
 434	struct ax_device *ax = to_ax_dev(dev);
 435
 436	netdev_dbg(dev, "close\n");
 437
 438	ax->running = 0;
 439	wmb();
 440
 441	ax_ei_close(dev);
 442
 443	/* turn the phy off */
 444	ax_phy_switch(dev, 0);
 445	phy_disconnect(ax->phy_dev);
 446
 447	free_irq(dev->irq, dev);
 
 
 
 448	return 0;
 449}
 450
 451static int ax_ioctl(struct net_device *dev, struct ifreq *req, int cmd)
 452{
 453	struct ax_device *ax = to_ax_dev(dev);
 454	struct phy_device *phy_dev = ax->phy_dev;
 455
 456	if (!netif_running(dev))
 457		return -EINVAL;
 458
 459	if (!phy_dev)
 460		return -ENODEV;
 461
 462	return phy_mii_ioctl(phy_dev, req, cmd);
 463}
 464
 465/* ethtool ops */
 466
 467static void ax_get_drvinfo(struct net_device *dev,
 468			   struct ethtool_drvinfo *info)
 469{
 470	struct platform_device *pdev = to_platform_device(dev->dev.parent);
 471
 472	strlcpy(info->driver, DRV_NAME, sizeof(info->driver));
 473	strlcpy(info->version, DRV_VERSION, sizeof(info->version));
 474	strlcpy(info->bus_info, pdev->name, sizeof(info->bus_info));
 475}
 476
 477static int ax_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
 478{
 479	struct ax_device *ax = to_ax_dev(dev);
 480	struct phy_device *phy_dev = ax->phy_dev;
 481
 482	if (!phy_dev)
 483		return -ENODEV;
 484
 485	return phy_ethtool_gset(phy_dev, cmd);
 486}
 487
 488static int ax_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
 489{
 490	struct ax_device *ax = to_ax_dev(dev);
 491	struct phy_device *phy_dev = ax->phy_dev;
 492
 493	if (!phy_dev)
 494		return -ENODEV;
 495
 496	return phy_ethtool_sset(phy_dev, cmd);
 497}
 498
 499static u32 ax_get_msglevel(struct net_device *dev)
 500{
 501	struct ei_device *ei_local = netdev_priv(dev);
 502
 503	return ei_local->msg_enable;
 504}
 505
 506static void ax_set_msglevel(struct net_device *dev, u32 v)
 507{
 508	struct ei_device *ei_local = netdev_priv(dev);
 509
 510	ei_local->msg_enable = v;
 511}
 512
 513static const struct ethtool_ops ax_ethtool_ops = {
 514	.get_drvinfo		= ax_get_drvinfo,
 515	.get_settings		= ax_get_settings,
 516	.set_settings		= ax_set_settings,
 517	.get_link		= ethtool_op_get_link,
 518	.get_ts_info		= ethtool_op_get_ts_info,
 519	.get_msglevel		= ax_get_msglevel,
 520	.set_msglevel		= ax_set_msglevel,
 
 
 521};
 522
 523#ifdef CONFIG_AX88796_93CX6
 524static void ax_eeprom_register_read(struct eeprom_93cx6 *eeprom)
 525{
 526	struct ei_device *ei_local = eeprom->data;
 527	u8 reg = ei_inb(ei_local->mem + AX_MEMR);
 528
 529	eeprom->reg_data_in = reg & AX_MEMR_EEI;
 530	eeprom->reg_data_out = reg & AX_MEMR_EEO; /* Input pin */
 531	eeprom->reg_data_clock = reg & AX_MEMR_EECLK;
 532	eeprom->reg_chip_select = reg & AX_MEMR_EECS;
 533}
 534
 535static void ax_eeprom_register_write(struct eeprom_93cx6 *eeprom)
 536{
 537	struct ei_device *ei_local = eeprom->data;
 538	u8 reg = ei_inb(ei_local->mem + AX_MEMR);
 539
 540	reg &= ~(AX_MEMR_EEI | AX_MEMR_EECLK | AX_MEMR_EECS);
 541
 542	if (eeprom->reg_data_in)
 543		reg |= AX_MEMR_EEI;
 544	if (eeprom->reg_data_clock)
 545		reg |= AX_MEMR_EECLK;
 546	if (eeprom->reg_chip_select)
 547		reg |= AX_MEMR_EECS;
 548
 549	ei_outb(reg, ei_local->mem + AX_MEMR);
 550	udelay(10);
 551}
 552#endif
 553
 554static const struct net_device_ops ax_netdev_ops = {
 555	.ndo_open		= ax_open,
 556	.ndo_stop		= ax_close,
 557	.ndo_do_ioctl		= ax_ioctl,
 558
 559	.ndo_start_xmit		= ax_ei_start_xmit,
 560	.ndo_tx_timeout		= ax_ei_tx_timeout,
 561	.ndo_get_stats		= ax_ei_get_stats,
 562	.ndo_set_rx_mode	= ax_ei_set_multicast_list,
 563	.ndo_validate_addr	= eth_validate_addr,
 564	.ndo_set_mac_address	= eth_mac_addr,
 565	.ndo_change_mtu		= eth_change_mtu,
 566#ifdef CONFIG_NET_POLL_CONTROLLER
 567	.ndo_poll_controller	= ax_ei_poll,
 568#endif
 569};
 570
 571static void ax_bb_mdc(struct mdiobb_ctrl *ctrl, int level)
 572{
 573	struct ax_device *ax = container_of(ctrl, struct ax_device, bb_ctrl);
 574
 575	if (level)
 576		ax->reg_memr |= AX_MEMR_MDC;
 577	else
 578		ax->reg_memr &= ~AX_MEMR_MDC;
 579
 580	ei_outb(ax->reg_memr, ax->addr_memr);
 581}
 582
 583static void ax_bb_dir(struct mdiobb_ctrl *ctrl, int output)
 584{
 585	struct ax_device *ax = container_of(ctrl, struct ax_device, bb_ctrl);
 586
 587	if (output)
 588		ax->reg_memr &= ~AX_MEMR_MDIR;
 589	else
 590		ax->reg_memr |= AX_MEMR_MDIR;
 591
 592	ei_outb(ax->reg_memr, ax->addr_memr);
 593}
 594
 595static void ax_bb_set_data(struct mdiobb_ctrl *ctrl, int value)
 596{
 597	struct ax_device *ax = container_of(ctrl, struct ax_device, bb_ctrl);
 598
 599	if (value)
 600		ax->reg_memr |= AX_MEMR_MDO;
 601	else
 602		ax->reg_memr &= ~AX_MEMR_MDO;
 603
 604	ei_outb(ax->reg_memr, ax->addr_memr);
 605}
 606
 607static int ax_bb_get_data(struct mdiobb_ctrl *ctrl)
 608{
 609	struct ax_device *ax = container_of(ctrl, struct ax_device, bb_ctrl);
 610	int reg_memr = ei_inb(ax->addr_memr);
 611
 612	return reg_memr & AX_MEMR_MDI ? 1 : 0;
 613}
 614
 615static struct mdiobb_ops bb_ops = {
 616	.owner = THIS_MODULE,
 617	.set_mdc = ax_bb_mdc,
 618	.set_mdio_dir = ax_bb_dir,
 619	.set_mdio_data = ax_bb_set_data,
 620	.get_mdio_data = ax_bb_get_data,
 621};
 622
 623/* setup code */
 624
 625static int ax_mii_init(struct net_device *dev)
 626{
 627	struct platform_device *pdev = to_platform_device(dev->dev.parent);
 628	struct ei_device *ei_local = netdev_priv(dev);
 629	struct ax_device *ax = to_ax_dev(dev);
 630	int err;
 631
 632	ax->bb_ctrl.ops = &bb_ops;
 633	ax->addr_memr = ei_local->mem + AX_MEMR;
 634	ax->mii_bus = alloc_mdio_bitbang(&ax->bb_ctrl);
 635	if (!ax->mii_bus) {
 636		err = -ENOMEM;
 637		goto out;
 638	}
 639
 640	ax->mii_bus->name = "ax88796_mii_bus";
 641	ax->mii_bus->parent = dev->dev.parent;
 642	snprintf(ax->mii_bus->id, MII_BUS_ID_SIZE, "%s-%x",
 643		pdev->name, pdev->id);
 644
 645	err = mdiobus_register(ax->mii_bus);
 646	if (err)
 647		goto out_free_mdio_bitbang;
 648
 649	return 0;
 650
 651 out_free_mdio_bitbang:
 652	free_mdio_bitbang(ax->mii_bus);
 653 out:
 654	return err;
 655}
 656
 657static void ax_initial_setup(struct net_device *dev, struct ei_device *ei_local)
 658{
 659	void __iomem *ioaddr = ei_local->mem;
 660	struct ax_device *ax = to_ax_dev(dev);
 661
 662	/* Select page 0 */
 663	ei_outb(E8390_NODMA + E8390_PAGE0 + E8390_STOP, ioaddr + E8390_CMD);
 664
 665	/* set to byte access */
 666	ei_outb(ax->plat->dcr_val & ~1, ioaddr + EN0_DCFG);
 667	ei_outb(ax->plat->gpoc_val, ioaddr + EI_SHIFT(0x17));
 668}
 669
 670/*
 671 * ax_init_dev
 672 *
 673 * initialise the specified device, taking care to note the MAC
 674 * address it may already have (if configured), ensure
 675 * the device is ready to be used by lib8390.c and registerd with
 676 * the network layer.
 677 */
 678static int ax_init_dev(struct net_device *dev)
 679{
 680	struct ei_device *ei_local = netdev_priv(dev);
 681	struct ax_device *ax = to_ax_dev(dev);
 682	void __iomem *ioaddr = ei_local->mem;
 683	unsigned int start_page;
 684	unsigned int stop_page;
 685	int ret;
 686	int i;
 687
 688	ret = ax_initial_check(dev);
 689	if (ret)
 690		goto err_out;
 691
 692	/* setup goes here */
 693
 694	ax_initial_setup(dev, ei_local);
 695
 696	/* read the mac from the card prom if we need it */
 697
 698	if (ax->plat->flags & AXFLG_HAS_EEPROM) {
 699		unsigned char SA_prom[32];
 700
 
 
 
 
 
 701		for (i = 0; i < sizeof(SA_prom); i += 2) {
 702			SA_prom[i] = ei_inb(ioaddr + NE_DATAPORT);
 703			SA_prom[i + 1] = ei_inb(ioaddr + NE_DATAPORT);
 704		}
 
 705
 706		if (ax->plat->wordlength == 2)
 707			for (i = 0; i < 16; i++)
 708				SA_prom[i] = SA_prom[i+i];
 709
 710		memcpy(dev->dev_addr, SA_prom, ETH_ALEN);
 711	}
 712
 713#ifdef CONFIG_AX88796_93CX6
 714	if (ax->plat->flags & AXFLG_HAS_93CX6) {
 715		unsigned char mac_addr[ETH_ALEN];
 716		struct eeprom_93cx6 eeprom;
 717
 718		eeprom.data = ei_local;
 719		eeprom.register_read = ax_eeprom_register_read;
 720		eeprom.register_write = ax_eeprom_register_write;
 721		eeprom.width = PCI_EEPROM_WIDTH_93C56;
 722
 723		eeprom_93cx6_multiread(&eeprom, 0,
 724				       (__le16 __force *)mac_addr,
 725				       sizeof(mac_addr) >> 1);
 726
 727		memcpy(dev->dev_addr, mac_addr, ETH_ALEN);
 728	}
 729#endif
 730	if (ax->plat->wordlength == 2) {
 731		/* We must set the 8390 for word mode. */
 732		ei_outb(ax->plat->dcr_val, ei_local->mem + EN0_DCFG);
 733		start_page = NESM_START_PG;
 734		stop_page = NESM_STOP_PG;
 735	} else {
 736		start_page = NE1SM_START_PG;
 737		stop_page = NE1SM_STOP_PG;
 738	}
 739
 740	/* load the mac-address from the device */
 741	if (ax->plat->flags & AXFLG_MAC_FROMDEV) {
 742		ei_outb(E8390_NODMA + E8390_PAGE1 + E8390_STOP,
 743			ei_local->mem + E8390_CMD); /* 0x61 */
 744		for (i = 0; i < ETH_ALEN; i++)
 745			dev->dev_addr[i] =
 746				ei_inb(ioaddr + EN1_PHYS_SHIFT(i));
 747	}
 748
 749	if ((ax->plat->flags & AXFLG_MAC_FROMPLATFORM) &&
 750	    ax->plat->mac_addr)
 751		memcpy(dev->dev_addr, ax->plat->mac_addr, ETH_ALEN);
 752
 
 
 
 
 
 
 753	ax_reset_8390(dev);
 754
 755	ei_local->name = "AX88796";
 756	ei_local->tx_start_page = start_page;
 757	ei_local->stop_page = stop_page;
 758	ei_local->word16 = (ax->plat->wordlength == 2);
 759	ei_local->rx_start_page = start_page + TX_PAGES;
 760
 761#ifdef PACKETBUF_MEMSIZE
 762	/* Allow the packet buffer size to be overridden by know-it-alls. */
 763	ei_local->stop_page = ei_local->tx_start_page + PACKETBUF_MEMSIZE;
 764#endif
 765
 766	ei_local->reset_8390 = &ax_reset_8390;
 767	ei_local->block_input = &ax_block_input;
 768	ei_local->block_output = &ax_block_output;
 
 
 
 
 
 
 769	ei_local->get_8390_hdr = &ax_get_8390_hdr;
 770	ei_local->priv = 0;
 771	ei_local->msg_enable = ax_msg_enable;
 772
 773	dev->netdev_ops = &ax_netdev_ops;
 774	dev->ethtool_ops = &ax_ethtool_ops;
 775
 776	ret = ax_mii_init(dev);
 777	if (ret)
 778		goto out_irq;
 779
 780	ax_NS8390_init(dev, 0);
 781
 782	ret = register_netdev(dev);
 783	if (ret)
 784		goto out_irq;
 785
 786	netdev_info(dev, "%dbit, irq %d, %lx, MAC: %pM\n",
 787		    ei_local->word16 ? 16 : 8, dev->irq, dev->base_addr,
 788		    dev->dev_addr);
 789
 790	return 0;
 791
 792 out_irq:
 793	/* cleanup irq */
 794	free_irq(dev->irq, dev);
 795 err_out:
 796	return ret;
 797}
 798
 799static int ax_remove(struct platform_device *pdev)
 800{
 801	struct net_device *dev = platform_get_drvdata(pdev);
 802	struct ei_device *ei_local = netdev_priv(dev);
 803	struct ax_device *ax = to_ax_dev(dev);
 804	struct resource *mem;
 805
 806	unregister_netdev(dev);
 807	free_irq(dev->irq, dev);
 808
 809	iounmap(ei_local->mem);
 810	mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
 811	release_mem_region(mem->start, resource_size(mem));
 812
 813	if (ax->map2) {
 814		iounmap(ax->map2);
 815		mem = platform_get_resource(pdev, IORESOURCE_MEM, 1);
 816		release_mem_region(mem->start, resource_size(mem));
 817	}
 818
 
 819	free_netdev(dev);
 820
 821	return 0;
 822}
 823
 824/*
 825 * ax_probe
 826 *
 827 * This is the entry point when the platform device system uses to
 828 * notify us of a new device to attach to. Allocate memory, find the
 829 * resources and information passed, and map the necessary registers.
 830 */
 831static int ax_probe(struct platform_device *pdev)
 832{
 833	struct net_device *dev;
 834	struct ei_device *ei_local;
 835	struct ax_device *ax;
 836	struct resource *irq, *mem, *mem2;
 837	unsigned long mem_size, mem2_size = 0;
 838	int ret = 0;
 839
 840	dev = ax__alloc_ei_netdev(sizeof(struct ax_device));
 841	if (dev == NULL)
 842		return -ENOMEM;
 843
 844	/* ok, let's setup our device */
 845	SET_NETDEV_DEV(dev, &pdev->dev);
 846	ei_local = netdev_priv(dev);
 847	ax = to_ax_dev(dev);
 848
 849	ax->plat = dev_get_platdata(&pdev->dev);
 850	platform_set_drvdata(pdev, dev);
 851
 852	ei_local->rxcr_base = ax->plat->rcr_val;
 853
 854	/* find the platform resources */
 855	irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
 856	if (!irq) {
 857		dev_err(&pdev->dev, "no IRQ specified\n");
 858		ret = -ENXIO;
 859		goto exit_mem;
 860	}
 861
 862	dev->irq = irq->start;
 863	ax->irqflags = irq->flags & IRQF_TRIGGER_MASK;
 864
 
 
 
 865	mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
 866	if (!mem) {
 867		dev_err(&pdev->dev, "no MEM specified\n");
 868		ret = -ENXIO;
 869		goto exit_mem;
 870	}
 871
 872	mem_size = resource_size(mem);
 873
 874	/*
 875	 * setup the register offsets from either the platform data or
 876	 * by using the size of the resource provided
 877	 */
 878	if (ax->plat->reg_offsets)
 879		ei_local->reg_offset = ax->plat->reg_offsets;
 880	else {
 881		ei_local->reg_offset = ax->reg_offsets;
 882		for (ret = 0; ret < 0x18; ret++)
 883			ax->reg_offsets[ret] = (mem_size / 0x18) * ret;
 884	}
 885
 886	if (!request_mem_region(mem->start, mem_size, pdev->name)) {
 887		dev_err(&pdev->dev, "cannot reserve registers\n");
 888		ret = -ENXIO;
 889		goto exit_mem;
 890	}
 891
 892	ei_local->mem = ioremap(mem->start, mem_size);
 893	dev->base_addr = (unsigned long)ei_local->mem;
 894
 895	if (ei_local->mem == NULL) {
 896		dev_err(&pdev->dev, "Cannot ioremap area %pR\n", mem);
 897
 898		ret = -ENXIO;
 899		goto exit_req;
 900	}
 901
 902	/* look for reset area */
 903	mem2 = platform_get_resource(pdev, IORESOURCE_MEM, 1);
 904	if (!mem2) {
 905		if (!ax->plat->reg_offsets) {
 906			for (ret = 0; ret < 0x20; ret++)
 907				ax->reg_offsets[ret] = (mem_size / 0x20) * ret;
 908		}
 909	} else {
 910		mem2_size = resource_size(mem2);
 911
 912		if (!request_mem_region(mem2->start, mem2_size, pdev->name)) {
 913			dev_err(&pdev->dev, "cannot reserve registers\n");
 914			ret = -ENXIO;
 915			goto exit_mem1;
 916		}
 917
 918		ax->map2 = ioremap(mem2->start, mem2_size);
 919		if (!ax->map2) {
 920			dev_err(&pdev->dev, "cannot map reset register\n");
 921			ret = -ENXIO;
 922			goto exit_mem2;
 923		}
 924
 925		ei_local->reg_offset[0x1f] = ax->map2 - ei_local->mem;
 926	}
 927
 928	/* got resources, now initialise and register device */
 929	ret = ax_init_dev(dev);
 930	if (!ret)
 931		return 0;
 932
 933	if (!ax->map2)
 934		goto exit_mem1;
 935
 936	iounmap(ax->map2);
 937
 938 exit_mem2:
 939	release_mem_region(mem2->start, mem2_size);
 
 940
 941 exit_mem1:
 942	iounmap(ei_local->mem);
 943
 944 exit_req:
 945	release_mem_region(mem->start, mem_size);
 946
 947 exit_mem:
 
 948	free_netdev(dev);
 949
 950	return ret;
 951}
 952
 953/* suspend and resume */
 954
 955#ifdef CONFIG_PM
 956static int ax_suspend(struct platform_device *dev, pm_message_t state)
 957{
 958	struct net_device *ndev = platform_get_drvdata(dev);
 959	struct ax_device *ax = to_ax_dev(ndev);
 960
 961	ax->resume_open = ax->running;
 962
 963	netif_device_detach(ndev);
 964	ax_close(ndev);
 965
 966	return 0;
 967}
 968
 969static int ax_resume(struct platform_device *pdev)
 970{
 971	struct net_device *ndev = platform_get_drvdata(pdev);
 972	struct ax_device *ax = to_ax_dev(ndev);
 973
 974	ax_initial_setup(ndev, netdev_priv(ndev));
 975	ax_NS8390_init(ndev, ax->resume_open);
 976	netif_device_attach(ndev);
 977
 978	if (ax->resume_open)
 979		ax_open(ndev);
 980
 981	return 0;
 982}
 983
 984#else
 985#define ax_suspend NULL
 986#define ax_resume NULL
 987#endif
 988
 989static struct platform_driver axdrv = {
 990	.driver	= {
 991		.name		= "ax88796",
 992	},
 993	.probe		= ax_probe,
 994	.remove		= ax_remove,
 995	.suspend	= ax_suspend,
 996	.resume		= ax_resume,
 997};
 998
 999module_platform_driver(axdrv);
1000
1001MODULE_DESCRIPTION("AX88796 10/100 Ethernet platform driver");
1002MODULE_AUTHOR("Ben Dooks, <ben@simtec.co.uk>");
1003MODULE_LICENSE("GPL v2");
1004MODULE_ALIAS("platform:ax88796");