xref: /openbmc/linux/drivers/net/ethernet/amd/hplance.c (revision 03ab8e6297acd1bc0eedaa050e2a1635c576fd11)
109c434b8SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2b955f6caSJeff Kirsher /* hplance.c  : the  Linux/hp300/lance ethernet driver
3b955f6caSJeff Kirsher  *
4b955f6caSJeff Kirsher  * Copyright (C) 05/1998 Peter Maydell <pmaydell@chiark.greenend.org.uk>
5b955f6caSJeff Kirsher  * Based on the Sun Lance driver and the NetBSD HP Lance driver
6b955f6caSJeff Kirsher  * Uses the generic 7990.c LANCE code.
7b955f6caSJeff Kirsher  */
8b955f6caSJeff Kirsher 
9b955f6caSJeff Kirsher #include <linux/module.h>
10b955f6caSJeff Kirsher #include <linux/kernel.h>
11b955f6caSJeff Kirsher #include <linux/types.h>
12b955f6caSJeff Kirsher #include <linux/interrupt.h>
13b955f6caSJeff Kirsher #include <linux/ioport.h>
14b955f6caSJeff Kirsher #include <linux/string.h>
15b955f6caSJeff Kirsher #include <linux/delay.h>
16b955f6caSJeff Kirsher #include <linux/init.h>
17b955f6caSJeff Kirsher #include <linux/errno.h>
1865fddcfcSMike Rapoport #include <linux/pgtable.h>
19b955f6caSJeff Kirsher /* Used for the temporal inet entries and routing */
20b955f6caSJeff Kirsher #include <linux/socket.h>
21b955f6caSJeff Kirsher #include <linux/route.h>
22b955f6caSJeff Kirsher #include <linux/dio.h>
23b955f6caSJeff Kirsher #include <linux/netdevice.h>
24b955f6caSJeff Kirsher #include <linux/etherdevice.h>
25b955f6caSJeff Kirsher #include <linux/skbuff.h>
26b955f6caSJeff Kirsher 
27b955f6caSJeff Kirsher #include <asm/io.h>
28b955f6caSJeff Kirsher 
29b955f6caSJeff Kirsher #include "hplance.h"
30b955f6caSJeff Kirsher 
3178c8dbb6SAmos Kong /* We have 16392 bytes of RAM for the init block and buffers. This places
32b955f6caSJeff Kirsher  * an upper limit on the number of buffers we can use. NetBSD uses 8 Rx
3378c8dbb6SAmos Kong  * buffers and 2 Tx buffers, it takes (8 + 2) * 1544 bytes.
34b955f6caSJeff Kirsher  */
35b955f6caSJeff Kirsher #define LANCE_LOG_TX_BUFFERS 1
36b955f6caSJeff Kirsher #define LANCE_LOG_RX_BUFFERS 3
37b955f6caSJeff Kirsher 
38b955f6caSJeff Kirsher #include "7990.h"                                 /* use generic LANCE code */
39b955f6caSJeff Kirsher 
40b955f6caSJeff Kirsher /* Our private data structure */
41b955f6caSJeff Kirsher struct hplance_private {
42b955f6caSJeff Kirsher 	struct lance_private lance;
43b955f6caSJeff Kirsher };
44b955f6caSJeff Kirsher 
45b955f6caSJeff Kirsher /* function prototypes... This is easy because all the grot is in the
46b955f6caSJeff Kirsher  * generic LANCE support. All we have to support is probing for boards,
47b955f6caSJeff Kirsher  * plus board-specific init, open and close actions.
48b955f6caSJeff Kirsher  * Oh, and we need to tell the generic code how to read and write LANCE registers...
49b955f6caSJeff Kirsher  */
501dd06ae8SGreg Kroah-Hartman static int hplance_init_one(struct dio_dev *d, const struct dio_device_id *ent);
511dd06ae8SGreg Kroah-Hartman static void hplance_init(struct net_device *dev, struct dio_dev *d);
520cb0568dSBill Pemberton static void hplance_remove_one(struct dio_dev *d);
53b955f6caSJeff Kirsher static void hplance_writerap(void *priv, unsigned short value);
54b955f6caSJeff Kirsher static void hplance_writerdp(void *priv, unsigned short value);
55b955f6caSJeff Kirsher static unsigned short hplance_readrdp(void *priv);
56b955f6caSJeff Kirsher static int hplance_open(struct net_device *dev);
57b955f6caSJeff Kirsher static int hplance_close(struct net_device *dev);
58b955f6caSJeff Kirsher 
59b955f6caSJeff Kirsher static struct dio_device_id hplance_dio_tbl[] = {
60b955f6caSJeff Kirsher 	{ DIO_ID_LAN },
61b955f6caSJeff Kirsher 	{ 0 }
62b955f6caSJeff Kirsher };
63b955f6caSJeff Kirsher 
64b955f6caSJeff Kirsher static struct dio_driver hplance_driver = {
65b955f6caSJeff Kirsher 	.name      = "hplance",
66b955f6caSJeff Kirsher 	.id_table  = hplance_dio_tbl,
67b955f6caSJeff Kirsher 	.probe     = hplance_init_one,
680cb0568dSBill Pemberton 	.remove    = hplance_remove_one,
69b955f6caSJeff Kirsher };
70b955f6caSJeff Kirsher 
71b955f6caSJeff Kirsher static const struct net_device_ops hplance_netdev_ops = {
72b955f6caSJeff Kirsher 	.ndo_open		= hplance_open,
73b955f6caSJeff Kirsher 	.ndo_stop		= hplance_close,
74b955f6caSJeff Kirsher 	.ndo_start_xmit		= lance_start_xmit,
75afc4b13dSJiri Pirko 	.ndo_set_rx_mode	= lance_set_multicast,
76b955f6caSJeff Kirsher 	.ndo_validate_addr	= eth_validate_addr,
77b955f6caSJeff Kirsher 	.ndo_set_mac_address	= eth_mac_addr,
78b955f6caSJeff Kirsher #ifdef CONFIG_NET_POLL_CONTROLLER
79b955f6caSJeff Kirsher 	.ndo_poll_controller	= lance_poll,
80b955f6caSJeff Kirsher #endif
81b955f6caSJeff Kirsher };
82b955f6caSJeff Kirsher 
83b955f6caSJeff Kirsher /* Find all the HP Lance boards and initialise them... */
hplance_init_one(struct dio_dev * d,const struct dio_device_id * ent)841dd06ae8SGreg Kroah-Hartman static int hplance_init_one(struct dio_dev *d, const struct dio_device_id *ent)
85b955f6caSJeff Kirsher {
86b955f6caSJeff Kirsher 	struct net_device *dev;
87b955f6caSJeff Kirsher 	int err = -ENOMEM;
88b955f6caSJeff Kirsher 
89b955f6caSJeff Kirsher 	dev = alloc_etherdev(sizeof(struct hplance_private));
90b955f6caSJeff Kirsher 	if (!dev)
91b955f6caSJeff Kirsher 		goto out;
92b955f6caSJeff Kirsher 
93b955f6caSJeff Kirsher 	err = -EBUSY;
94b955f6caSJeff Kirsher 	if (!request_mem_region(dio_resource_start(d),
95b955f6caSJeff Kirsher 				dio_resource_len(d), d->name))
96b955f6caSJeff Kirsher 		goto out_free_netdev;
97b955f6caSJeff Kirsher 
98b955f6caSJeff Kirsher 	hplance_init(dev, d);
99b955f6caSJeff Kirsher 	err = register_netdev(dev);
100b955f6caSJeff Kirsher 	if (err)
101b955f6caSJeff Kirsher 		goto out_release_mem_region;
102b955f6caSJeff Kirsher 
103b955f6caSJeff Kirsher 	dio_set_drvdata(d, dev);
104b955f6caSJeff Kirsher 
105f27fd499SDanny Kukawka 	printk(KERN_INFO "%s: %s; select code %d, addr %pM, irq %d\n",
106f27fd499SDanny Kukawka 	       dev->name, d->name, d->scode, dev->dev_addr, d->ipl);
107b955f6caSJeff Kirsher 
108b955f6caSJeff Kirsher 	return 0;
109b955f6caSJeff Kirsher 
110b955f6caSJeff Kirsher  out_release_mem_region:
111b955f6caSJeff Kirsher 	release_mem_region(dio_resource_start(d), dio_resource_len(d));
112b955f6caSJeff Kirsher  out_free_netdev:
113b955f6caSJeff Kirsher 	free_netdev(dev);
114b955f6caSJeff Kirsher  out:
115b955f6caSJeff Kirsher 	return err;
116b955f6caSJeff Kirsher }
117b955f6caSJeff Kirsher 
hplance_remove_one(struct dio_dev * d)1180cb0568dSBill Pemberton static void hplance_remove_one(struct dio_dev *d)
119b955f6caSJeff Kirsher {
120b955f6caSJeff Kirsher 	struct net_device *dev = dio_get_drvdata(d);
121b955f6caSJeff Kirsher 
122b955f6caSJeff Kirsher 	unregister_netdev(dev);
123b955f6caSJeff Kirsher 	release_mem_region(dio_resource_start(d), dio_resource_len(d));
124b955f6caSJeff Kirsher 	free_netdev(dev);
125b955f6caSJeff Kirsher }
126b955f6caSJeff Kirsher 
127b955f6caSJeff Kirsher /* Initialise a single lance board at the given DIO device */
hplance_init(struct net_device * dev,struct dio_dev * d)1280cb0568dSBill Pemberton static void hplance_init(struct net_device *dev, struct dio_dev *d)
129b955f6caSJeff Kirsher {
130b955f6caSJeff Kirsher 	unsigned long va = (d->resource.start + DIO_VIRADDRBASE);
131b955f6caSJeff Kirsher 	struct hplance_private *lp;
132*21942eefSJakub Kicinski 	u8 addr[ETH_ALEN];
133b955f6caSJeff Kirsher 	int i;
134b955f6caSJeff Kirsher 
135b955f6caSJeff Kirsher 	/* reset the board */
136b955f6caSJeff Kirsher 	out_8(va + DIO_IDOFF, 0xff);
137b955f6caSJeff Kirsher 	udelay(100);                              /* ariba! ariba! udelay! udelay! */
138b955f6caSJeff Kirsher 
139b955f6caSJeff Kirsher 	/* Fill the dev fields */
140b955f6caSJeff Kirsher 	dev->base_addr = va;
141b955f6caSJeff Kirsher 	dev->netdev_ops = &hplance_netdev_ops;
142b955f6caSJeff Kirsher 	dev->dma = 0;
143b955f6caSJeff Kirsher 
144b955f6caSJeff Kirsher 	for (i = 0; i < 6; i++) {
145b955f6caSJeff Kirsher 		/* The NVRAM holds our ethernet address, one nibble per byte,
146b955f6caSJeff Kirsher 		 * at bytes NVRAMOFF+1,3,5,7,9...
147b955f6caSJeff Kirsher 		 */
148*21942eefSJakub Kicinski 		addr[i] = ((in_8(va + HPLANCE_NVRAMOFF + i*4 + 1) & 0xF) << 4)
149b955f6caSJeff Kirsher 			| (in_8(va + HPLANCE_NVRAMOFF + i*4 + 3) & 0xF);
150b955f6caSJeff Kirsher 	}
151*21942eefSJakub Kicinski 	eth_hw_addr_set(dev, addr);
152b955f6caSJeff Kirsher 
153b955f6caSJeff Kirsher 	lp = netdev_priv(dev);
1544ffa8fcdSGeert Uytterhoeven 	lp->lance.name = d->name;
155b955f6caSJeff Kirsher 	lp->lance.base = va;
156b955f6caSJeff Kirsher 	lp->lance.init_block = (struct lance_init_block *)(va + HPLANCE_MEMOFF); /* CPU addr */
157b955f6caSJeff Kirsher 	lp->lance.lance_init_block = NULL;              /* LANCE addr of same RAM */
158b955f6caSJeff Kirsher 	lp->lance.busmaster_regval = LE_C3_BSWP;        /* we're bigendian */
159b955f6caSJeff Kirsher 	lp->lance.irq = d->ipl;
160b955f6caSJeff Kirsher 	lp->lance.writerap = hplance_writerap;
161b955f6caSJeff Kirsher 	lp->lance.writerdp = hplance_writerdp;
162b955f6caSJeff Kirsher 	lp->lance.readrdp = hplance_readrdp;
163b955f6caSJeff Kirsher 	lp->lance.lance_log_rx_bufs = LANCE_LOG_RX_BUFFERS;
164b955f6caSJeff Kirsher 	lp->lance.lance_log_tx_bufs = LANCE_LOG_TX_BUFFERS;
165b955f6caSJeff Kirsher 	lp->lance.rx_ring_mod_mask = RX_RING_MOD_MASK;
166b955f6caSJeff Kirsher 	lp->lance.tx_ring_mod_mask = TX_RING_MOD_MASK;
167b955f6caSJeff Kirsher }
168b955f6caSJeff Kirsher 
169b955f6caSJeff Kirsher /* This is disgusting. We have to check the DIO status register for ack every
170b955f6caSJeff Kirsher  * time we read or write the LANCE registers.
171b955f6caSJeff Kirsher  */
hplance_writerap(void * priv,unsigned short value)172b955f6caSJeff Kirsher static void hplance_writerap(void *priv, unsigned short value)
173b955f6caSJeff Kirsher {
174b955f6caSJeff Kirsher 	struct lance_private *lp = (struct lance_private *)priv;
1753f6ebcffSYixing Liu 
176b955f6caSJeff Kirsher 	do {
177b955f6caSJeff Kirsher 		out_be16(lp->base + HPLANCE_REGOFF + LANCE_RAP, value);
178b955f6caSJeff Kirsher 	} while ((in_8(lp->base + HPLANCE_STATUS) & LE_ACK) == 0);
179b955f6caSJeff Kirsher }
180b955f6caSJeff Kirsher 
hplance_writerdp(void * priv,unsigned short value)181b955f6caSJeff Kirsher static void hplance_writerdp(void *priv, unsigned short value)
182b955f6caSJeff Kirsher {
183b955f6caSJeff Kirsher 	struct lance_private *lp = (struct lance_private *)priv;
1843f6ebcffSYixing Liu 
185b955f6caSJeff Kirsher 	do {
186b955f6caSJeff Kirsher 		out_be16(lp->base + HPLANCE_REGOFF + LANCE_RDP, value);
187b955f6caSJeff Kirsher 	} while ((in_8(lp->base + HPLANCE_STATUS) & LE_ACK) == 0);
188b955f6caSJeff Kirsher }
189b955f6caSJeff Kirsher 
hplance_readrdp(void * priv)190b955f6caSJeff Kirsher static unsigned short hplance_readrdp(void *priv)
191b955f6caSJeff Kirsher {
192b955f6caSJeff Kirsher 	struct lance_private *lp = (struct lance_private *)priv;
193b955f6caSJeff Kirsher 	__u16 value;
1943f6ebcffSYixing Liu 
195b955f6caSJeff Kirsher 	do {
196b955f6caSJeff Kirsher 		value = in_be16(lp->base + HPLANCE_REGOFF + LANCE_RDP);
197b955f6caSJeff Kirsher 	} while ((in_8(lp->base + HPLANCE_STATUS) & LE_ACK) == 0);
198b955f6caSJeff Kirsher 	return value;
199b955f6caSJeff Kirsher }
200b955f6caSJeff Kirsher 
hplance_open(struct net_device * dev)201b955f6caSJeff Kirsher static int hplance_open(struct net_device *dev)
202b955f6caSJeff Kirsher {
203b955f6caSJeff Kirsher 	int status;
204b955f6caSJeff Kirsher 	struct lance_private *lp = netdev_priv(dev);
205b955f6caSJeff Kirsher 
206b955f6caSJeff Kirsher 	status = lance_open(dev);                 /* call generic lance open code */
207b955f6caSJeff Kirsher 	if (status)
208b955f6caSJeff Kirsher 		return status;
209b955f6caSJeff Kirsher 	/* enable interrupts at board level. */
210b955f6caSJeff Kirsher 	out_8(lp->base + HPLANCE_STATUS, LE_IE);
211b955f6caSJeff Kirsher 
212b955f6caSJeff Kirsher 	return 0;
213b955f6caSJeff Kirsher }
214b955f6caSJeff Kirsher 
hplance_close(struct net_device * dev)215b955f6caSJeff Kirsher static int hplance_close(struct net_device *dev)
216b955f6caSJeff Kirsher {
217b955f6caSJeff Kirsher 	struct lance_private *lp = netdev_priv(dev);
218b955f6caSJeff Kirsher 
219b955f6caSJeff Kirsher 	out_8(lp->base + HPLANCE_STATUS, 0);	/* disable interrupts at boardlevel */
220b955f6caSJeff Kirsher 	lance_close(dev);
221b955f6caSJeff Kirsher 	return 0;
222b955f6caSJeff Kirsher }
223b955f6caSJeff Kirsher 
hplance_init_module(void)224b955f6caSJeff Kirsher static int __init hplance_init_module(void)
225b955f6caSJeff Kirsher {
226b955f6caSJeff Kirsher 	return dio_register_driver(&hplance_driver);
227b955f6caSJeff Kirsher }
228b955f6caSJeff Kirsher 
hplance_cleanup_module(void)229b955f6caSJeff Kirsher static void __exit hplance_cleanup_module(void)
230b955f6caSJeff Kirsher {
231b955f6caSJeff Kirsher 	dio_unregister_driver(&hplance_driver);
232b955f6caSJeff Kirsher }
233b955f6caSJeff Kirsher 
234b955f6caSJeff Kirsher module_init(hplance_init_module);
235b955f6caSJeff Kirsher module_exit(hplance_cleanup_module);
236b955f6caSJeff Kirsher 
237b955f6caSJeff Kirsher MODULE_LICENSE("GPL");
238