Linux Audio

Check our new training course

Loading...
v6.8
  1// SPDX-License-Identifier: GPL-2.0
  2/*
  3 * xtsonic.c
  4 *
  5 * (C) 2001 - 2007 Tensilica Inc.
  6 *	Kevin Chea <kchea@yahoo.com>
  7 *	Marc Gauthier <marc@linux-xtensa.org>
  8 *	Chris Zankel <chris@zankel.net>
  9 *
 10 * (C) 1996,1998 by Thomas Bogendoerfer (tsbogend@alpha.franken.de)
 11 *
 12 * This driver is based on work from Andreas Busse, but most of
 13 * the code is rewritten.
 14 *
 15 * (C) 1995 by Andreas Busse (andy@waldorf-gmbh.de)
 16 *
 17 * A driver for the onboard Sonic ethernet controller on the XT2000.
 18 */
 19
 20#include <linux/kernel.h>
 21#include <linux/module.h>
 22#include <linux/types.h>
 23#include <linux/fcntl.h>
 24#include <linux/gfp.h>
 25#include <linux/interrupt.h>
 26#include <linux/init.h>
 27#include <linux/ioport.h>
 28#include <linux/in.h>
 29#include <linux/string.h>
 30#include <linux/delay.h>
 31#include <linux/errno.h>
 32#include <linux/netdevice.h>
 33#include <linux/etherdevice.h>
 34#include <linux/skbuff.h>
 35#include <linux/platform_device.h>
 36#include <linux/dma-mapping.h>
 37#include <linux/slab.h>
 38#include <linux/pgtable.h>
 39
 40#include <asm/io.h>
 
 41#include <asm/dma.h>
 42
 43static char xtsonic_string[] = "xtsonic";
 44
 45extern unsigned xtboard_nvram_valid(void);
 46extern void xtboard_get_ether_addr(unsigned char *buf);
 47
 48#include "sonic.h"
 49
 50/*
 51 * According to the documentation for the Sonic ethernet controller,
 52 * EOBC should be 760 words (1520 bytes) for 32-bit applications, and,
 53 * as such, 2 words less than the buffer size. The value for RBSIZE
 54 * defined in sonic.h, however is only 1520.
 55 *
 56 * (Note that in 16-bit configurations, EOBC is 759 words (1518 bytes) and
 57 * RBSIZE 1520 bytes)
 58 */
 59#undef SONIC_RBSIZE
 60#define SONIC_RBSIZE	1524
 61
 62/*
 63 * The chip provides 256 byte register space.
 64 */
 65#define SONIC_MEM_SIZE	0x100
 66
 67/*
 68 * Macros to access SONIC registers
 69 */
 70#define SONIC_READ(reg) \
 71	(0xffff & *((volatile unsigned int *)dev->base_addr+reg))
 72
 73#define SONIC_WRITE(reg,val) \
 74	*((volatile unsigned int *)dev->base_addr+reg) = val
 75
 
 
 
 
 
 
 
 
 76/*
 77 * We cannot use station (ethernet) address prefixes to detect the
 78 * sonic controller since these are board manufacturer depended.
 79 * So we check for known Silicon Revision IDs instead.
 80 */
 81static unsigned short known_revisions[] =
 82{
 83	0x101,			/* SONIC 83934 */
 84	0xffff			/* end of list */
 85};
 86
 87static int xtsonic_open(struct net_device *dev)
 88{
 89	int retval;
 90
 91	retval = request_irq(dev->irq, sonic_interrupt, 0, "sonic", dev);
 92	if (retval) {
 93		printk(KERN_ERR "%s: unable to get IRQ %d.\n",
 94		       dev->name, dev->irq);
 95		return -EAGAIN;
 96	}
 97
 98	retval = sonic_open(dev);
 99	if (retval)
100		free_irq(dev->irq, dev);
101	return retval;
102}
103
104static int xtsonic_close(struct net_device *dev)
105{
106	int err;
107	err = sonic_close(dev);
108	free_irq(dev->irq, dev);
109	return err;
110}
111
112static const struct net_device_ops xtsonic_netdev_ops = {
113	.ndo_open		= xtsonic_open,
114	.ndo_stop		= xtsonic_close,
115	.ndo_start_xmit		= sonic_send_packet,
116	.ndo_get_stats		= sonic_get_stats,
117	.ndo_set_rx_mode	= sonic_multicast_list,
118	.ndo_tx_timeout		= sonic_tx_timeout,
119	.ndo_validate_addr	= eth_validate_addr,
120	.ndo_set_mac_address	= eth_mac_addr,
121};
122
123static int sonic_probe1(struct net_device *dev)
124{
 
125	unsigned int silicon_revision;
126	struct sonic_local *lp = netdev_priv(dev);
127	unsigned int base_addr = dev->base_addr;
128	int i;
129	int err = 0;
130	unsigned char addr[ETH_ALEN];
131
132	if (!request_mem_region(base_addr, 0x100, xtsonic_string))
133		return -EBUSY;
134
135	/*
136	 * get the Silicon Revision ID. If this is one of the known
137	 * one assume that we found a SONIC ethernet controller at
138	 * the expected location.
139	 */
140	silicon_revision = SONIC_READ(SONIC_SR);
 
 
 
141	i = 0;
142	while ((known_revisions[i] != 0xffff) &&
143			(known_revisions[i] != silicon_revision))
144		i++;
145
146	if (known_revisions[i] == 0xffff) {
147		pr_info("SONIC ethernet controller not found (0x%4x)\n",
148			silicon_revision);
149		return -ENODEV;
150	}
151
 
 
 
152	/*
153	 * Put the sonic into software reset, then retrieve ethernet address.
154	 * Note: we are assuming that the boot-loader has initialized the cam.
155	 */
156	SONIC_WRITE(SONIC_CMD,SONIC_CR_RST);
157	SONIC_WRITE(SONIC_DCR,
158		    SONIC_DCR_WC0|SONIC_DCR_DW|SONIC_DCR_LBR|SONIC_DCR_SBUS);
159	SONIC_WRITE(SONIC_CEP,0);
160	SONIC_WRITE(SONIC_IMR,0);
161
162	SONIC_WRITE(SONIC_CMD,SONIC_CR_RST);
163	SONIC_WRITE(SONIC_CEP,0);
164
165	for (i=0; i<3; i++) {
166		unsigned int val = SONIC_READ(SONIC_CAP0-i);
167		addr[i*2] = val;
168		addr[i*2+1] = val >> 8;
169	}
170	eth_hw_addr_set(dev, addr);
 
171
172	lp->dma_bitmode = SONIC_BITMODE32;
173
174	err = sonic_alloc_descriptors(dev);
175	if (err)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
176		goto out;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
177
178	dev->netdev_ops		= &xtsonic_netdev_ops;
179	dev->watchdog_timeo	= TX_TIMEOUT;
180
181	/*
182	 * clear tally counter
183	 */
184	SONIC_WRITE(SONIC_CRCT,0xffff);
185	SONIC_WRITE(SONIC_FAET,0xffff);
186	SONIC_WRITE(SONIC_MPT,0xffff);
187
188	return 0;
189out:
190	release_region(dev->base_addr, SONIC_MEM_SIZE);
191	return err;
192}
193
194
195/*
196 * Probe for a SONIC ethernet controller on an XT2000 board.
197 * Actually probing is superfluous but we're paranoid.
198 */
199
200int xtsonic_probe(struct platform_device *pdev)
201{
202	struct net_device *dev;
203	struct sonic_local *lp;
204	struct resource *resmem, *resirq;
205	int err = 0;
206
207	if ((resmem = platform_get_resource(pdev, IORESOURCE_MEM, 0)) == NULL)
208		return -ENODEV;
209
210	if ((resirq = platform_get_resource(pdev, IORESOURCE_IRQ, 0)) == NULL)
211		return -ENODEV;
212
213	if ((dev = alloc_etherdev(sizeof(struct sonic_local))) == NULL)
214		return -ENOMEM;
215
216	lp = netdev_priv(dev);
217	lp->device = &pdev->dev;
218	platform_set_drvdata(pdev, dev);
219	SET_NETDEV_DEV(dev, &pdev->dev);
 
220
221	dev->base_addr = resmem->start;
222	dev->irq = resirq->start;
223
224	if ((err = sonic_probe1(dev)))
225		goto out;
226
227	pr_info("SONIC ethernet @%08lx, MAC %pM, IRQ %d\n",
228		dev->base_addr, dev->dev_addr, dev->irq);
229
230	sonic_msg_init(dev);
231
232	if ((err = register_netdev(dev)))
233		goto undo_probe1;
 
 
 
234
235	return 0;
236
237undo_probe1:
238	dma_free_coherent(lp->device,
239			  SIZEOF_SONIC_DESC * SONIC_BUS_SCALE(lp->dma_bitmode),
240			  lp->descriptors, lp->descriptors_laddr);
241	release_region(dev->base_addr, SONIC_MEM_SIZE);
242out:
243	free_netdev(dev);
244
245	return err;
246}
247
248MODULE_DESCRIPTION("Xtensa XT2000 SONIC ethernet driver");
 
 
249
250#include "sonic.c"
251
252static void xtsonic_device_remove(struct platform_device *pdev)
253{
254	struct net_device *dev = platform_get_drvdata(pdev);
255	struct sonic_local *lp = netdev_priv(dev);
256
257	unregister_netdev(dev);
258	dma_free_coherent(lp->device,
259			  SIZEOF_SONIC_DESC * SONIC_BUS_SCALE(lp->dma_bitmode),
260			  lp->descriptors, lp->descriptors_laddr);
261	release_region (dev->base_addr, SONIC_MEM_SIZE);
262	free_netdev(dev);
 
 
263}
264
265static struct platform_driver xtsonic_driver = {
266	.probe = xtsonic_probe,
267	.remove_new = xtsonic_device_remove,
268	.driver = {
269		.name = xtsonic_string,
270	},
271};
272
273module_platform_driver(xtsonic_driver);
v4.10.11
 
  1/*
  2 * xtsonic.c
  3 *
  4 * (C) 2001 - 2007 Tensilica Inc.
  5 *	Kevin Chea <kchea@yahoo.com>
  6 *	Marc Gauthier <marc@linux-xtensa.org>
  7 *	Chris Zankel <chris@zankel.net>
  8 *
  9 * (C) 1996,1998 by Thomas Bogendoerfer (tsbogend@alpha.franken.de)
 10 *
 11 * This driver is based on work from Andreas Busse, but most of
 12 * the code is rewritten.
 13 *
 14 * (C) 1995 by Andreas Busse (andy@waldorf-gmbh.de)
 15 *
 16 * A driver for the onboard Sonic ethernet controller on the XT2000.
 17 */
 18
 19#include <linux/kernel.h>
 20#include <linux/module.h>
 21#include <linux/types.h>
 22#include <linux/fcntl.h>
 23#include <linux/gfp.h>
 24#include <linux/interrupt.h>
 25#include <linux/init.h>
 26#include <linux/ioport.h>
 27#include <linux/in.h>
 28#include <linux/string.h>
 29#include <linux/delay.h>
 30#include <linux/errno.h>
 31#include <linux/netdevice.h>
 32#include <linux/etherdevice.h>
 33#include <linux/skbuff.h>
 34#include <linux/platform_device.h>
 35#include <linux/dma-mapping.h>
 36#include <linux/slab.h>
 
 37
 38#include <asm/io.h>
 39#include <asm/pgtable.h>
 40#include <asm/dma.h>
 41
 42static char xtsonic_string[] = "xtsonic";
 43
 44extern unsigned xtboard_nvram_valid(void);
 45extern void xtboard_get_ether_addr(unsigned char *buf);
 46
 47#include "sonic.h"
 48
 49/*
 50 * According to the documentation for the Sonic ethernet controller,
 51 * EOBC should be 760 words (1520 bytes) for 32-bit applications, and,
 52 * as such, 2 words less than the buffer size. The value for RBSIZE
 53 * defined in sonic.h, however is only 1520.
 54 *
 55 * (Note that in 16-bit configurations, EOBC is 759 words (1518 bytes) and
 56 * RBSIZE 1520 bytes)
 57 */
 58#undef SONIC_RBSIZE
 59#define SONIC_RBSIZE	1524
 60
 61/*
 62 * The chip provides 256 byte register space.
 63 */
 64#define SONIC_MEM_SIZE	0x100
 65
 66/*
 67 * Macros to access SONIC registers
 68 */
 69#define SONIC_READ(reg) \
 70	(0xffff & *((volatile unsigned int *)dev->base_addr+reg))
 71
 72#define SONIC_WRITE(reg,val) \
 73	*((volatile unsigned int *)dev->base_addr+reg) = val
 74
 75
 76/* Use 0 for production, 1 for verification, and >2 for debug */
 77#ifdef SONIC_DEBUG
 78static unsigned int sonic_debug = SONIC_DEBUG;
 79#else
 80static unsigned int sonic_debug = 1;
 81#endif
 82
 83/*
 84 * We cannot use station (ethernet) address prefixes to detect the
 85 * sonic controller since these are board manufacturer depended.
 86 * So we check for known Silicon Revision IDs instead.
 87 */
 88static unsigned short known_revisions[] =
 89{
 90	0x101,			/* SONIC 83934 */
 91	0xffff			/* end of list */
 92};
 93
 94static int xtsonic_open(struct net_device *dev)
 95{
 96	int retval;
 97
 98	retval = request_irq(dev->irq, sonic_interrupt, 0, "sonic", dev);
 99	if (retval) {
100		printk(KERN_ERR "%s: unable to get IRQ %d.\n",
101		       dev->name, dev->irq);
102		return -EAGAIN;
103	}
104
105	retval = sonic_open(dev);
106	if (retval)
107		free_irq(dev->irq, dev);
108	return retval;
109}
110
111static int xtsonic_close(struct net_device *dev)
112{
113	int err;
114	err = sonic_close(dev);
115	free_irq(dev->irq, dev);
116	return err;
117}
118
119static const struct net_device_ops xtsonic_netdev_ops = {
120	.ndo_open		= xtsonic_open,
121	.ndo_stop		= xtsonic_close,
122	.ndo_start_xmit		= sonic_send_packet,
123	.ndo_get_stats		= sonic_get_stats,
124	.ndo_set_rx_mode	= sonic_multicast_list,
125	.ndo_tx_timeout		= sonic_tx_timeout,
126	.ndo_validate_addr	= eth_validate_addr,
127	.ndo_set_mac_address	= eth_mac_addr,
128};
129
130static int __init sonic_probe1(struct net_device *dev)
131{
132	static unsigned version_printed = 0;
133	unsigned int silicon_revision;
134	struct sonic_local *lp = netdev_priv(dev);
135	unsigned int base_addr = dev->base_addr;
136	int i;
137	int err = 0;
 
138
139	if (!request_mem_region(base_addr, 0x100, xtsonic_string))
140		return -EBUSY;
141
142	/*
143	 * get the Silicon Revision ID. If this is one of the known
144	 * one assume that we found a SONIC ethernet controller at
145	 * the expected location.
146	 */
147	silicon_revision = SONIC_READ(SONIC_SR);
148	if (sonic_debug > 1)
149		printk("SONIC Silicon Revision = 0x%04x\n",silicon_revision);
150
151	i = 0;
152	while ((known_revisions[i] != 0xffff) &&
153			(known_revisions[i] != silicon_revision))
154		i++;
155
156	if (known_revisions[i] == 0xffff) {
157		printk("SONIC ethernet controller not found (0x%4x)\n",
158				silicon_revision);
159		return -ENODEV;
160	}
161
162	if (sonic_debug  &&  version_printed++ == 0)
163		printk(version);
164
165	/*
166	 * Put the sonic into software reset, then retrieve ethernet address.
167	 * Note: we are assuming that the boot-loader has initialized the cam.
168	 */
169	SONIC_WRITE(SONIC_CMD,SONIC_CR_RST);
170	SONIC_WRITE(SONIC_DCR,
171		    SONIC_DCR_WC0|SONIC_DCR_DW|SONIC_DCR_LBR|SONIC_DCR_SBUS);
172	SONIC_WRITE(SONIC_CEP,0);
173	SONIC_WRITE(SONIC_IMR,0);
174
175	SONIC_WRITE(SONIC_CMD,SONIC_CR_RST);
176	SONIC_WRITE(SONIC_CEP,0);
177
178	for (i=0; i<3; i++) {
179		unsigned int val = SONIC_READ(SONIC_CAP0-i);
180		dev->dev_addr[i*2] = val;
181		dev->dev_addr[i*2+1] = val >> 8;
182	}
183
184	/* Initialize the device structure. */
185
186	lp->dma_bitmode = SONIC_BITMODE32;
187
188	/*
189	 *  Allocate local private descriptor areas in uncached space.
190	 *  The entire structure must be located within the same 64kb segment.
191	 *  A simple way to ensure this is to allocate twice the
192	 *  size of the structure -- given that the structure is
193	 *  much less than 64 kB, at least one of the halves of
194	 *  the allocated area will be contained entirely in 64 kB.
195	 *  We also allocate extra space for a pointer to allow freeing
196	 *  this structure later on (in xtsonic_cleanup_module()).
197	 */
198	lp->descriptors = dma_alloc_coherent(lp->device,
199					     SIZEOF_SONIC_DESC *
200					     SONIC_BUS_SCALE(lp->dma_bitmode),
201					     &lp->descriptors_laddr,
202					     GFP_KERNEL);
203	if (lp->descriptors == NULL) {
204		err = -ENOMEM;
205		goto out;
206	}
207
208	lp->cda = lp->descriptors;
209	lp->tda = lp->cda + (SIZEOF_SONIC_CDA
210			     * SONIC_BUS_SCALE(lp->dma_bitmode));
211	lp->rda = lp->tda + (SIZEOF_SONIC_TD * SONIC_NUM_TDS
212			     * SONIC_BUS_SCALE(lp->dma_bitmode));
213	lp->rra = lp->rda + (SIZEOF_SONIC_RD * SONIC_NUM_RDS
214			     * SONIC_BUS_SCALE(lp->dma_bitmode));
215
216	/* get the virtual dma address */
217
218	lp->cda_laddr = lp->descriptors_laddr;
219	lp->tda_laddr = lp->cda_laddr + (SIZEOF_SONIC_CDA
220				         * SONIC_BUS_SCALE(lp->dma_bitmode));
221	lp->rda_laddr = lp->tda_laddr + (SIZEOF_SONIC_TD * SONIC_NUM_TDS
222					 * SONIC_BUS_SCALE(lp->dma_bitmode));
223	lp->rra_laddr = lp->rda_laddr + (SIZEOF_SONIC_RD * SONIC_NUM_RDS
224					 * SONIC_BUS_SCALE(lp->dma_bitmode));
225
226	dev->netdev_ops		= &xtsonic_netdev_ops;
227	dev->watchdog_timeo	= TX_TIMEOUT;
228
229	/*
230	 * clear tally counter
231	 */
232	SONIC_WRITE(SONIC_CRCT,0xffff);
233	SONIC_WRITE(SONIC_FAET,0xffff);
234	SONIC_WRITE(SONIC_MPT,0xffff);
235
236	return 0;
237out:
238	release_region(dev->base_addr, SONIC_MEM_SIZE);
239	return err;
240}
241
242
243/*
244 * Probe for a SONIC ethernet controller on an XT2000 board.
245 * Actually probing is superfluous but we're paranoid.
246 */
247
248int xtsonic_probe(struct platform_device *pdev)
249{
250	struct net_device *dev;
251	struct sonic_local *lp;
252	struct resource *resmem, *resirq;
253	int err = 0;
254
255	if ((resmem = platform_get_resource(pdev, IORESOURCE_MEM, 0)) == NULL)
256		return -ENODEV;
257
258	if ((resirq = platform_get_resource(pdev, IORESOURCE_IRQ, 0)) == NULL)
259		return -ENODEV;
260
261	if ((dev = alloc_etherdev(sizeof(struct sonic_local))) == NULL)
262		return -ENOMEM;
263
264	lp = netdev_priv(dev);
265	lp->device = &pdev->dev;
266	platform_set_drvdata(pdev, dev);
267	SET_NETDEV_DEV(dev, &pdev->dev);
268	netdev_boot_setup_check(dev);
269
270	dev->base_addr = resmem->start;
271	dev->irq = resirq->start;
272
273	if ((err = sonic_probe1(dev)))
274		goto out;
 
 
 
 
 
 
275	if ((err = register_netdev(dev)))
276		goto out1;
277
278	printk("%s: SONIC ethernet @%08lx, MAC %pM, IRQ %d\n", dev->name,
279	       dev->base_addr, dev->dev_addr, dev->irq);
280
281	return 0;
282
283out1:
 
 
 
284	release_region(dev->base_addr, SONIC_MEM_SIZE);
285out:
286	free_netdev(dev);
287
288	return err;
289}
290
291MODULE_DESCRIPTION("Xtensa XT2000 SONIC ethernet driver");
292module_param(sonic_debug, int, 0);
293MODULE_PARM_DESC(sonic_debug, "xtsonic debug level (1-4)");
294
295#include "sonic.c"
296
297static int xtsonic_device_remove(struct platform_device *pdev)
298{
299	struct net_device *dev = platform_get_drvdata(pdev);
300	struct sonic_local *lp = netdev_priv(dev);
301
302	unregister_netdev(dev);
303	dma_free_coherent(lp->device,
304			  SIZEOF_SONIC_DESC * SONIC_BUS_SCALE(lp->dma_bitmode),
305			  lp->descriptors, lp->descriptors_laddr);
306	release_region (dev->base_addr, SONIC_MEM_SIZE);
307	free_netdev(dev);
308
309	return 0;
310}
311
312static struct platform_driver xtsonic_driver = {
313	.probe = xtsonic_probe,
314	.remove = xtsonic_device_remove,
315	.driver = {
316		.name = xtsonic_string,
317	},
318};
319
320module_platform_driver(xtsonic_driver);