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");
1016