Linux-libre 5.3.12-gnu
[librecmc/linux-libre.git] / drivers / net / ethernet / natsemi / jazzsonic.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * jazzsonic.c
4  *
5  * (C) 2005 Finn Thain
6  *
7  * Converted to DMA API, and (from the mac68k project) introduced
8  * dhd's support for 16-bit cards.
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 Mips Jazz
18  * systems (Acer Pica-61, Mips Magnum 4000, Olivetti M700 and
19  * perhaps others, too)
20  */
21
22 #include <linux/kernel.h>
23 #include <linux/module.h>
24 #include <linux/types.h>
25 #include <linux/fcntl.h>
26 #include <linux/gfp.h>
27 #include <linux/interrupt.h>
28 #include <linux/ioport.h>
29 #include <linux/in.h>
30 #include <linux/string.h>
31 #include <linux/delay.h>
32 #include <linux/errno.h>
33 #include <linux/netdevice.h>
34 #include <linux/etherdevice.h>
35 #include <linux/skbuff.h>
36 #include <linux/platform_device.h>
37 #include <linux/dma-mapping.h>
38 #include <linux/slab.h>
39
40 #include <asm/bootinfo.h>
41 #include <asm/pgtable.h>
42 #include <asm/io.h>
43 #include <asm/dma.h>
44 #include <asm/jazz.h>
45 #include <asm/jazzdma.h>
46
47 static char jazz_sonic_string[] = "jazzsonic";
48
49 #define SONIC_MEM_SIZE  0x100
50
51 #include "sonic.h"
52
53 /*
54  * Macros to access SONIC registers
55  */
56 #define SONIC_READ(reg) (*((volatile unsigned int *)dev->base_addr+reg))
57
58 #define SONIC_WRITE(reg,val)                                            \
59 do {                                                                    \
60         *((volatile unsigned int *)dev->base_addr+(reg)) = (val);               \
61 } while (0)
62
63 /*
64  * We cannot use station (ethernet) address prefixes to detect the
65  * sonic controller since these are board manufacturer depended.
66  * So we check for known Silicon Revision IDs instead.
67  */
68 static unsigned short known_revisions[] =
69 {
70         0x04,                   /* Mips Magnum 4000 */
71         0xffff                  /* end of list */
72 };
73
74 static int jazzsonic_open(struct net_device* dev)
75 {
76         int retval;
77
78         retval = request_irq(dev->irq, sonic_interrupt, 0, "sonic", dev);
79         if (retval) {
80                 printk(KERN_ERR "%s: unable to get IRQ %d.\n",
81                                 dev->name, dev->irq);
82                 return retval;
83         }
84
85         retval = sonic_open(dev);
86         if (retval)
87                 free_irq(dev->irq, dev);
88         return retval;
89 }
90
91 static int jazzsonic_close(struct net_device* dev)
92 {
93         int err;
94         err = sonic_close(dev);
95         free_irq(dev->irq, dev);
96         return err;
97 }
98
99 static const struct net_device_ops sonic_netdev_ops = {
100         .ndo_open               = jazzsonic_open,
101         .ndo_stop               = jazzsonic_close,
102         .ndo_start_xmit         = sonic_send_packet,
103         .ndo_get_stats          = sonic_get_stats,
104         .ndo_set_rx_mode        = sonic_multicast_list,
105         .ndo_tx_timeout         = sonic_tx_timeout,
106         .ndo_validate_addr      = eth_validate_addr,
107         .ndo_set_mac_address    = eth_mac_addr,
108 };
109
110 static int sonic_probe1(struct net_device *dev)
111 {
112         unsigned int silicon_revision;
113         unsigned int val;
114         struct sonic_local *lp = netdev_priv(dev);
115         int err = -ENODEV;
116         int i;
117
118         if (!request_mem_region(dev->base_addr, SONIC_MEM_SIZE, jazz_sonic_string))
119                 return -EBUSY;
120
121         /*
122          * get the Silicon Revision ID. If this is one of the known
123          * one assume that we found a SONIC ethernet controller at
124          * the expected location.
125          */
126         silicon_revision = SONIC_READ(SONIC_SR);
127         i = 0;
128         while (known_revisions[i] != 0xffff &&
129                known_revisions[i] != silicon_revision)
130                 i++;
131
132         if (known_revisions[i] == 0xffff) {
133                 pr_info("SONIC ethernet controller not found (0x%4x)\n",
134                         silicon_revision);
135                 goto out;
136         }
137
138         /*
139          * Put the sonic into software reset, then
140          * retrieve and print the ethernet address.
141          */
142         SONIC_WRITE(SONIC_CMD,SONIC_CR_RST);
143         SONIC_WRITE(SONIC_CEP,0);
144         for (i=0; i<3; i++) {
145                 val = SONIC_READ(SONIC_CAP0-i);
146                 dev->dev_addr[i*2] = val;
147                 dev->dev_addr[i*2+1] = val >> 8;
148         }
149
150         err = -ENOMEM;
151
152         /* Initialize the device structure. */
153
154         lp->dma_bitmode = SONIC_BITMODE32;
155
156         /* Allocate the entire chunk of memory for the descriptors.
157            Note that this cannot cross a 64K boundary. */
158         lp->descriptors = dma_alloc_coherent(lp->device,
159                                              SIZEOF_SONIC_DESC *
160                                              SONIC_BUS_SCALE(lp->dma_bitmode),
161                                              &lp->descriptors_laddr,
162                                              GFP_KERNEL);
163         if (lp->descriptors == NULL)
164                 goto out;
165
166         /* Now set up the pointers to point to the appropriate places */
167         lp->cda = lp->descriptors;
168         lp->tda = lp->cda + (SIZEOF_SONIC_CDA
169                              * SONIC_BUS_SCALE(lp->dma_bitmode));
170         lp->rda = lp->tda + (SIZEOF_SONIC_TD * SONIC_NUM_TDS
171                              * SONIC_BUS_SCALE(lp->dma_bitmode));
172         lp->rra = lp->rda + (SIZEOF_SONIC_RD * SONIC_NUM_RDS
173                              * SONIC_BUS_SCALE(lp->dma_bitmode));
174
175         lp->cda_laddr = lp->descriptors_laddr;
176         lp->tda_laddr = lp->cda_laddr + (SIZEOF_SONIC_CDA
177                              * SONIC_BUS_SCALE(lp->dma_bitmode));
178         lp->rda_laddr = lp->tda_laddr + (SIZEOF_SONIC_TD * SONIC_NUM_TDS
179                              * SONIC_BUS_SCALE(lp->dma_bitmode));
180         lp->rra_laddr = lp->rda_laddr + (SIZEOF_SONIC_RD * SONIC_NUM_RDS
181                              * SONIC_BUS_SCALE(lp->dma_bitmode));
182
183         dev->netdev_ops = &sonic_netdev_ops;
184         dev->watchdog_timeo = TX_TIMEOUT;
185
186         /*
187          * clear tally counter
188          */
189         SONIC_WRITE(SONIC_CRCT,0xffff);
190         SONIC_WRITE(SONIC_FAET,0xffff);
191         SONIC_WRITE(SONIC_MPT,0xffff);
192
193         return 0;
194 out:
195         release_mem_region(dev->base_addr, SONIC_MEM_SIZE);
196         return err;
197 }
198
199 /*
200  * Probe for a SONIC ethernet controller on a Mips Jazz board.
201  * Actually probing is superfluous but we're paranoid.
202  */
203 static int jazz_sonic_probe(struct platform_device *pdev)
204 {
205         struct net_device *dev;
206         struct sonic_local *lp;
207         struct resource *res;
208         int err = 0;
209
210         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
211         if (!res)
212                 return -ENODEV;
213
214         dev = alloc_etherdev(sizeof(struct sonic_local));
215         if (!dev)
216                 return -ENOMEM;
217
218         lp = netdev_priv(dev);
219         lp->device = &pdev->dev;
220         SET_NETDEV_DEV(dev, &pdev->dev);
221         platform_set_drvdata(pdev, dev);
222
223         netdev_boot_setup_check(dev);
224
225         dev->base_addr = res->start;
226         dev->irq = platform_get_irq(pdev, 0);
227         err = sonic_probe1(dev);
228         if (err)
229                 goto out;
230
231         pr_info("SONIC ethernet @%08lx, MAC %pM, IRQ %d\n",
232                 dev->base_addr, dev->dev_addr, dev->irq);
233
234         sonic_msg_init(dev);
235
236         err = register_netdev(dev);
237         if (err)
238                 goto out1;
239
240         return 0;
241
242 out1:
243         release_mem_region(dev->base_addr, SONIC_MEM_SIZE);
244 out:
245         free_netdev(dev);
246
247         return err;
248 }
249
250 MODULE_DESCRIPTION("Jazz SONIC ethernet driver");
251 MODULE_ALIAS("platform:jazzsonic");
252
253 #include "sonic.c"
254
255 static int jazz_sonic_device_remove(struct platform_device *pdev)
256 {
257         struct net_device *dev = platform_get_drvdata(pdev);
258         struct sonic_local* lp = netdev_priv(dev);
259
260         unregister_netdev(dev);
261         dma_free_coherent(lp->device, SIZEOF_SONIC_DESC * SONIC_BUS_SCALE(lp->dma_bitmode),
262                           lp->descriptors, lp->descriptors_laddr);
263         release_mem_region(dev->base_addr, SONIC_MEM_SIZE);
264         free_netdev(dev);
265
266         return 0;
267 }
268
269 static struct platform_driver jazz_sonic_driver = {
270         .probe  = jazz_sonic_probe,
271         .remove = jazz_sonic_device_remove,
272         .driver = {
273                 .name   = jazz_sonic_string,
274         },
275 };
276
277 module_platform_driver(jazz_sonic_driver);