GNU Linux-libre 4.14.332-gnu1
[releases.git] / drivers / net / ethernet / natsemi / xtsonic.c
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
39 #include <asm/io.h>
40 #include <asm/pgtable.h>
41 #include <asm/dma.h>
42
43 static char xtsonic_string[] = "xtsonic";
44
45 extern unsigned xtboard_nvram_valid(void);
46 extern 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 /* Use 0 for production, 1 for verification, and >2 for debug */
78 #ifdef SONIC_DEBUG
79 static unsigned int sonic_debug = SONIC_DEBUG;
80 #else
81 static unsigned int sonic_debug = 1;
82 #endif
83
84 /*
85  * We cannot use station (ethernet) address prefixes to detect the
86  * sonic controller since these are board manufacturer depended.
87  * So we check for known Silicon Revision IDs instead.
88  */
89 static unsigned short known_revisions[] =
90 {
91         0x101,                  /* SONIC 83934 */
92         0xffff                  /* end of list */
93 };
94
95 static int xtsonic_open(struct net_device *dev)
96 {
97         int retval;
98
99         retval = request_irq(dev->irq, sonic_interrupt, 0, "sonic", dev);
100         if (retval) {
101                 printk(KERN_ERR "%s: unable to get IRQ %d.\n",
102                        dev->name, dev->irq);
103                 return -EAGAIN;
104         }
105
106         retval = sonic_open(dev);
107         if (retval)
108                 free_irq(dev->irq, dev);
109         return retval;
110 }
111
112 static int xtsonic_close(struct net_device *dev)
113 {
114         int err;
115         err = sonic_close(dev);
116         free_irq(dev->irq, dev);
117         return err;
118 }
119
120 static const struct net_device_ops xtsonic_netdev_ops = {
121         .ndo_open               = xtsonic_open,
122         .ndo_stop               = xtsonic_close,
123         .ndo_start_xmit         = sonic_send_packet,
124         .ndo_get_stats          = sonic_get_stats,
125         .ndo_set_rx_mode        = sonic_multicast_list,
126         .ndo_tx_timeout         = sonic_tx_timeout,
127         .ndo_validate_addr      = eth_validate_addr,
128         .ndo_set_mac_address    = eth_mac_addr,
129 };
130
131 static int sonic_probe1(struct net_device *dev)
132 {
133         static unsigned version_printed = 0;
134         unsigned int silicon_revision;
135         struct sonic_local *lp = netdev_priv(dev);
136         unsigned int base_addr = dev->base_addr;
137         int i;
138         int err = 0;
139
140         if (!request_mem_region(base_addr, 0x100, xtsonic_string))
141                 return -EBUSY;
142
143         /*
144          * get the Silicon Revision ID. If this is one of the known
145          * one assume that we found a SONIC ethernet controller at
146          * the expected location.
147          */
148         silicon_revision = SONIC_READ(SONIC_SR);
149         if (sonic_debug > 1)
150                 printk("SONIC Silicon Revision = 0x%04x\n",silicon_revision);
151
152         i = 0;
153         while ((known_revisions[i] != 0xffff) &&
154                         (known_revisions[i] != silicon_revision))
155                 i++;
156
157         if (known_revisions[i] == 0xffff) {
158                 printk("SONIC ethernet controller not found (0x%4x)\n",
159                                 silicon_revision);
160                 return -ENODEV;
161         }
162
163         if (sonic_debug  &&  version_printed++ == 0)
164                 printk(version);
165
166         /*
167          * Put the sonic into software reset, then retrieve ethernet address.
168          * Note: we are assuming that the boot-loader has initialized the cam.
169          */
170         SONIC_WRITE(SONIC_CMD,SONIC_CR_RST);
171         SONIC_WRITE(SONIC_DCR,
172                     SONIC_DCR_WC0|SONIC_DCR_DW|SONIC_DCR_LBR|SONIC_DCR_SBUS);
173         SONIC_WRITE(SONIC_CEP,0);
174         SONIC_WRITE(SONIC_IMR,0);
175
176         SONIC_WRITE(SONIC_CMD,SONIC_CR_RST);
177         SONIC_WRITE(SONIC_CEP,0);
178
179         for (i=0; i<3; i++) {
180                 unsigned int val = SONIC_READ(SONIC_CAP0-i);
181                 dev->dev_addr[i*2] = val;
182                 dev->dev_addr[i*2+1] = val >> 8;
183         }
184
185         /* Initialize the device structure. */
186
187         lp->dma_bitmode = SONIC_BITMODE32;
188
189         /*
190          *  Allocate local private descriptor areas in uncached space.
191          *  The entire structure must be located within the same 64kb segment.
192          *  A simple way to ensure this is to allocate twice the
193          *  size of the structure -- given that the structure is
194          *  much less than 64 kB, at least one of the halves of
195          *  the allocated area will be contained entirely in 64 kB.
196          *  We also allocate extra space for a pointer to allow freeing
197          *  this structure later on (in xtsonic_cleanup_module()).
198          */
199         lp->descriptors = dma_alloc_coherent(lp->device,
200                                              SIZEOF_SONIC_DESC *
201                                              SONIC_BUS_SCALE(lp->dma_bitmode),
202                                              &lp->descriptors_laddr,
203                                              GFP_KERNEL);
204         if (lp->descriptors == NULL) {
205                 err = -ENOMEM;
206                 goto out;
207         }
208
209         lp->cda = lp->descriptors;
210         lp->tda = lp->cda + (SIZEOF_SONIC_CDA
211                              * SONIC_BUS_SCALE(lp->dma_bitmode));
212         lp->rda = lp->tda + (SIZEOF_SONIC_TD * SONIC_NUM_TDS
213                              * SONIC_BUS_SCALE(lp->dma_bitmode));
214         lp->rra = lp->rda + (SIZEOF_SONIC_RD * SONIC_NUM_RDS
215                              * SONIC_BUS_SCALE(lp->dma_bitmode));
216
217         /* get the virtual dma address */
218
219         lp->cda_laddr = lp->descriptors_laddr;
220         lp->tda_laddr = lp->cda_laddr + (SIZEOF_SONIC_CDA
221                                          * SONIC_BUS_SCALE(lp->dma_bitmode));
222         lp->rda_laddr = lp->tda_laddr + (SIZEOF_SONIC_TD * SONIC_NUM_TDS
223                                          * SONIC_BUS_SCALE(lp->dma_bitmode));
224         lp->rra_laddr = lp->rda_laddr + (SIZEOF_SONIC_RD * SONIC_NUM_RDS
225                                          * SONIC_BUS_SCALE(lp->dma_bitmode));
226
227         dev->netdev_ops         = &xtsonic_netdev_ops;
228         dev->watchdog_timeo     = TX_TIMEOUT;
229
230         /*
231          * clear tally counter
232          */
233         SONIC_WRITE(SONIC_CRCT,0xffff);
234         SONIC_WRITE(SONIC_FAET,0xffff);
235         SONIC_WRITE(SONIC_MPT,0xffff);
236
237         return 0;
238 out:
239         release_region(dev->base_addr, SONIC_MEM_SIZE);
240         return err;
241 }
242
243
244 /*
245  * Probe for a SONIC ethernet controller on an XT2000 board.
246  * Actually probing is superfluous but we're paranoid.
247  */
248
249 int xtsonic_probe(struct platform_device *pdev)
250 {
251         struct net_device *dev;
252         struct sonic_local *lp;
253         struct resource *resmem, *resirq;
254         int err = 0;
255
256         if ((resmem = platform_get_resource(pdev, IORESOURCE_MEM, 0)) == NULL)
257                 return -ENODEV;
258
259         if ((resirq = platform_get_resource(pdev, IORESOURCE_IRQ, 0)) == NULL)
260                 return -ENODEV;
261
262         if ((dev = alloc_etherdev(sizeof(struct sonic_local))) == NULL)
263                 return -ENOMEM;
264
265         lp = netdev_priv(dev);
266         lp->device = &pdev->dev;
267         platform_set_drvdata(pdev, dev);
268         SET_NETDEV_DEV(dev, &pdev->dev);
269         netdev_boot_setup_check(dev);
270
271         dev->base_addr = resmem->start;
272         dev->irq = resirq->start;
273
274         if ((err = sonic_probe1(dev)))
275                 goto out;
276         if ((err = register_netdev(dev)))
277                 goto out1;
278
279         printk("%s: SONIC ethernet @%08lx, MAC %pM, IRQ %d\n", dev->name,
280                dev->base_addr, dev->dev_addr, dev->irq);
281
282         return 0;
283
284 out1:
285         release_region(dev->base_addr, SONIC_MEM_SIZE);
286 out:
287         free_netdev(dev);
288
289         return err;
290 }
291
292 MODULE_DESCRIPTION("Xtensa XT2000 SONIC ethernet driver");
293 module_param(sonic_debug, int, 0);
294 MODULE_PARM_DESC(sonic_debug, "xtsonic debug level (1-4)");
295
296 #include "sonic.c"
297
298 static int xtsonic_device_remove(struct platform_device *pdev)
299 {
300         struct net_device *dev = platform_get_drvdata(pdev);
301         struct sonic_local *lp = netdev_priv(dev);
302
303         unregister_netdev(dev);
304         dma_free_coherent(lp->device,
305                           SIZEOF_SONIC_DESC * SONIC_BUS_SCALE(lp->dma_bitmode),
306                           lp->descriptors, lp->descriptors_laddr);
307         release_region (dev->base_addr, SONIC_MEM_SIZE);
308         free_netdev(dev);
309
310         return 0;
311 }
312
313 static struct platform_driver xtsonic_driver = {
314         .probe = xtsonic_probe,
315         .remove = xtsonic_device_remove,
316         .driver = {
317                 .name = xtsonic_string,
318         },
319 };
320
321 module_platform_driver(xtsonic_driver);