]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/mvs/mvs_soc.c
Merge tcpdump 4.2.1.
[FreeBSD/FreeBSD.git] / sys / dev / mvs / mvs_soc.c
1 /*-
2  * Copyright (c) 2010 Alexander Motin <mav@FreeBSD.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer,
10  *    without modification, immediately at the beginning of the file.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  */
26
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29
30 #include <sys/param.h>
31 #include <sys/module.h>
32 #include <sys/systm.h>
33 #include <sys/kernel.h>
34 #include <sys/bus.h>
35 #include <sys/endian.h>
36 #include <sys/malloc.h>
37 #include <sys/lock.h>
38 #include <sys/mutex.h>
39 #include <vm/uma.h>
40 #include <machine/stdarg.h>
41 #include <machine/resource.h>
42 #include <machine/bus.h>
43 #include <sys/rman.h>
44 #include <arm/mv/mvreg.h>
45 #include <arm/mv/mvvar.h>
46 #include <dev/ofw/ofw_bus.h>
47 #include <dev/ofw/ofw_bus_subr.h>
48 #include "mvs.h"
49
50 /* local prototypes */
51 static int mvs_setup_interrupt(device_t dev);
52 static void mvs_intr(void *data);
53 static int mvs_suspend(device_t dev);
54 static int mvs_resume(device_t dev);
55 static int mvs_ctlr_setup(device_t dev);
56
57 static struct {
58         uint32_t        id;
59         uint8_t         rev;
60         const char      *name;
61         int             ports;
62         int             quirks;
63 } mvs_ids[] = {
64         {MV_DEV_88F5182, 0x00,   "Marvell 88F5182",     2, MVS_Q_GENIIE|MVS_Q_SOC},
65         {MV_DEV_88F6281, 0x00,   "Marvell 88F6281",     2, MVS_Q_GENIIE|MVS_Q_SOC},
66         {MV_DEV_MV78100, 0x00,   "Marvell MV78100",     2, MVS_Q_GENIIE|MVS_Q_SOC},
67         {MV_DEV_MV78100_Z0, 0x00,"Marvell MV78100",     2, MVS_Q_GENIIE|MVS_Q_SOC},
68         {0,              0x00,   NULL,                  0, 0}
69 };
70
71 static int
72 mvs_probe(device_t dev)
73 {
74         char buf[64];
75         int i;
76         uint32_t devid, revid;
77
78         if (!ofw_bus_is_compatible(dev, "mrvl,sata"))
79                 return (ENXIO);
80
81         soc_id(&devid, &revid);
82         for (i = 0; mvs_ids[i].id != 0; i++) {
83                 if (mvs_ids[i].id == devid &&
84                     mvs_ids[i].rev <= revid) {
85                         snprintf(buf, sizeof(buf), "%s SATA controller",
86                             mvs_ids[i].name);
87                         device_set_desc_copy(dev, buf);
88                         return (BUS_PROBE_VENDOR);
89                 }
90         }
91         return (ENXIO);
92 }
93
94 static int
95 mvs_attach(device_t dev)
96 {
97         struct mvs_controller *ctlr = device_get_softc(dev);
98         device_t child;
99         int     error, unit, i;
100         uint32_t devid, revid;
101
102         soc_id(&devid, &revid);
103         ctlr->dev = dev;
104         i = 0;
105         while (mvs_ids[i].id != 0 &&
106             (mvs_ids[i].id != devid ||
107              mvs_ids[i].rev > revid))
108                 i++;
109         ctlr->channels = mvs_ids[i].ports;
110         ctlr->quirks = mvs_ids[i].quirks;
111         resource_int_value(device_get_name(dev),
112             device_get_unit(dev), "ccc", &ctlr->ccc);
113         ctlr->cccc = 8;
114         resource_int_value(device_get_name(dev),
115             device_get_unit(dev), "cccc", &ctlr->cccc);
116         if (ctlr->ccc == 0 || ctlr->cccc == 0) {
117                 ctlr->ccc = 0;
118                 ctlr->cccc = 0;
119         }
120         if (ctlr->ccc > 100000)
121                 ctlr->ccc = 100000;
122         device_printf(dev,
123             "Gen-%s, %d %sGbps ports, Port Multiplier %s%s\n",
124             ((ctlr->quirks & MVS_Q_GENI) ? "I" :
125              ((ctlr->quirks & MVS_Q_GENII) ? "II" : "IIe")),
126             ctlr->channels,
127             ((ctlr->quirks & MVS_Q_GENI) ? "1.5" : "3"),
128             ((ctlr->quirks & MVS_Q_GENI) ?
129             "not supported" : "supported"),
130             ((ctlr->quirks & MVS_Q_GENIIE) ?
131             " with FBS" : ""));
132         mtx_init(&ctlr->mtx, "MVS controller lock", NULL, MTX_DEF);
133         /* We should have a memory BAR(0). */
134         ctlr->r_rid = 0;
135         if (!(ctlr->r_mem = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
136             &ctlr->r_rid, RF_ACTIVE)))
137                 return ENXIO;
138         /* Setup our own memory management for channels. */
139         ctlr->sc_iomem.rm_start = rman_get_start(ctlr->r_mem);
140         ctlr->sc_iomem.rm_end = rman_get_end(ctlr->r_mem);
141         ctlr->sc_iomem.rm_type = RMAN_ARRAY;
142         ctlr->sc_iomem.rm_descr = "I/O memory addresses";
143         if ((error = rman_init(&ctlr->sc_iomem)) != 0) {
144                 bus_release_resource(dev, SYS_RES_MEMORY, ctlr->r_rid, ctlr->r_mem);
145                 return (error);
146         }
147         if ((error = rman_manage_region(&ctlr->sc_iomem,
148             rman_get_start(ctlr->r_mem), rman_get_end(ctlr->r_mem))) != 0) {
149                 bus_release_resource(dev, SYS_RES_MEMORY, ctlr->r_rid, ctlr->r_mem);
150                 rman_fini(&ctlr->sc_iomem);
151                 return (error);
152         }
153         mvs_ctlr_setup(dev);
154         /* Setup interrupts. */
155         if (mvs_setup_interrupt(dev)) {
156                 bus_release_resource(dev, SYS_RES_MEMORY, ctlr->r_rid, ctlr->r_mem);
157                 rman_fini(&ctlr->sc_iomem);
158                 return ENXIO;
159         }
160         /* Attach all channels on this controller */
161         for (unit = 0; unit < ctlr->channels; unit++) {
162                 child = device_add_child(dev, "mvsch", -1);
163                 if (child == NULL)
164                         device_printf(dev, "failed to add channel device\n");
165                 else
166                         device_set_ivars(child, (void *)(intptr_t)unit);
167         }
168         bus_generic_attach(dev);
169         return 0;
170 }
171
172 static int
173 mvs_detach(device_t dev)
174 {
175         struct mvs_controller *ctlr = device_get_softc(dev);
176
177         /* Detach & delete all children */
178         device_delete_children(dev);
179
180         /* Free interrupt. */
181         if (ctlr->irq.r_irq) {
182                 bus_teardown_intr(dev, ctlr->irq.r_irq,
183                     ctlr->irq.handle);
184                 bus_release_resource(dev, SYS_RES_IRQ,
185                     ctlr->irq.r_irq_rid, ctlr->irq.r_irq);
186         }
187         /* Free memory. */
188         rman_fini(&ctlr->sc_iomem);
189         if (ctlr->r_mem)
190                 bus_release_resource(dev, SYS_RES_MEMORY, ctlr->r_rid, ctlr->r_mem);
191         mtx_destroy(&ctlr->mtx);
192         return (0);
193 }
194
195 static int
196 mvs_ctlr_setup(device_t dev)
197 {
198         struct mvs_controller *ctlr = device_get_softc(dev);
199         int ccc = ctlr->ccc, cccc = ctlr->cccc, ccim = 0;
200
201         /* Mask chip interrupts */
202         ATA_OUTL(ctlr->r_mem, CHIP_SOC_MIM, 0x00000000);
203         /* Clear HC interrupts */
204         ATA_OUTL(ctlr->r_mem, HC_IC, 0x00000000);
205         /* Clear chip interrupts */
206         ATA_OUTL(ctlr->r_mem, CHIP_SOC_MIC, 0);
207         /* Configure per-HC CCC */
208         if (ccc && bootverbose) {
209                 device_printf(dev,
210                     "CCC with %dus/%dcmd enabled\n",
211                     ctlr->ccc, ctlr->cccc);
212         }
213         ccc *= 150;
214         ATA_OUTL(ctlr->r_mem, HC_ICT, cccc);
215         ATA_OUTL(ctlr->r_mem, HC_ITT, ccc);
216         if (ccc)
217                 ccim |= IC_HC0_COAL_DONE;
218         /* Enable chip interrupts */
219         ctlr->gmim = ((ccc ? IC_HC0_COAL_DONE :
220             (IC_DONE_HC0 & CHIP_SOC_HC0_MASK(ctlr->channels))) |
221             (IC_ERR_HC0 & CHIP_SOC_HC0_MASK(ctlr->channels)));
222         ATA_OUTL(ctlr->r_mem, CHIP_SOC_MIM, ctlr->gmim | ctlr->pmim);
223         return (0);
224 }
225
226 static void
227 mvs_edma(device_t dev, device_t child, int mode)
228 {
229         struct mvs_controller *ctlr = device_get_softc(dev);
230         int unit = ((struct mvs_channel *)device_get_softc(child))->unit;
231         int bit = IC_DONE_IRQ << (unit * 2);
232
233         if (ctlr->ccc == 0)
234                 return;
235         /* CCC is not working for non-EDMA mode. Unmask device interrupts. */
236         mtx_lock(&ctlr->mtx);
237         if (mode == MVS_EDMA_OFF)
238                 ctlr->pmim |= bit;
239         else
240                 ctlr->pmim &= ~bit;
241         ATA_OUTL(ctlr->r_mem, CHIP_SOC_MIM, ctlr->gmim | ctlr->pmim);
242         mtx_unlock(&ctlr->mtx);
243 }
244
245 static int
246 mvs_suspend(device_t dev)
247 {
248         struct mvs_controller *ctlr = device_get_softc(dev);
249
250         bus_generic_suspend(dev);
251         /* Mask chip interrupts */
252         ATA_OUTL(ctlr->r_mem, CHIP_SOC_MIM, 0x00000000);
253         return 0;
254 }
255
256 static int
257 mvs_resume(device_t dev)
258 {
259
260         mvs_ctlr_setup(dev);
261         return (bus_generic_resume(dev));
262 }
263
264 static int
265 mvs_setup_interrupt(device_t dev)
266 {
267         struct mvs_controller *ctlr = device_get_softc(dev);
268
269         /* Allocate all IRQs. */
270         ctlr->irq.r_irq_rid = 0;
271         if (!(ctlr->irq.r_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ,
272             &ctlr->irq.r_irq_rid, RF_SHAREABLE | RF_ACTIVE))) {
273                 device_printf(dev, "unable to map interrupt\n");
274                 return (ENXIO);
275         }
276         if ((bus_setup_intr(dev, ctlr->irq.r_irq, ATA_INTR_FLAGS, NULL,
277             mvs_intr, ctlr, &ctlr->irq.handle))) {
278                 device_printf(dev, "unable to setup interrupt\n");
279                 bus_release_resource(dev, SYS_RES_IRQ,
280                     ctlr->irq.r_irq_rid, ctlr->irq.r_irq);
281                 ctlr->irq.r_irq = 0;
282                 return (ENXIO);
283         }
284         return (0);
285 }
286
287 /*
288  * Common case interrupt handler.
289  */
290 static void
291 mvs_intr(void *data)
292 {
293         struct mvs_controller *ctlr = data;
294         struct mvs_intr_arg arg;
295         void (*function)(void *);
296         int p, chan_num;
297         u_int32_t ic, aic;
298
299         ic = ATA_INL(ctlr->r_mem, CHIP_SOC_MIC);
300         if ((ic & IC_HC0) == 0)
301                 return;
302
303         /* Acknowledge interrupts of this HC. */
304         aic = 0;
305
306         /* Processing interrupts from each initialized channel */
307         for (chan_num = 0; chan_num < ctlr->channels; chan_num++) {
308                 if (ic & (IC_DONE_IRQ << (chan_num * 2)))
309                         aic |= HC_IC_DONE(chan_num) | HC_IC_DEV(chan_num);
310         }
311
312         if (ic & IC_HC0_COAL_DONE)
313                 aic |= HC_IC_COAL;
314         ATA_OUTL(ctlr->r_mem, HC_IC, ~aic);
315
316         /* Call per-port interrupt handler. */
317         for (p = 0; p < ctlr->channels; p++) {
318                 arg.cause = ic & (IC_ERR_IRQ|IC_DONE_IRQ);
319                 if ((arg.cause != 0) &&
320                     (function = ctlr->interrupt[p].function)) {
321                         arg.arg = ctlr->interrupt[p].argument;
322                         function(&arg);
323                 }
324                 ic >>= 2;
325         }
326 }
327
328 static struct resource *
329 mvs_alloc_resource(device_t dev, device_t child, int type, int *rid,
330                        u_long start, u_long end, u_long count, u_int flags)
331 {
332         struct mvs_controller *ctlr = device_get_softc(dev);
333         int unit = ((struct mvs_channel *)device_get_softc(child))->unit;
334         struct resource *res = NULL;
335         int offset = PORT_BASE(unit & 0x03);
336         long st;
337
338         switch (type) {
339         case SYS_RES_MEMORY:
340                 st = rman_get_start(ctlr->r_mem);
341                 res = rman_reserve_resource(&ctlr->sc_iomem, st + offset,
342                     st + offset + PORT_SIZE - 1, PORT_SIZE, RF_ACTIVE, child);
343                 if (res) {
344                         bus_space_handle_t bsh;
345                         bus_space_tag_t bst;
346                         bsh = rman_get_bushandle(ctlr->r_mem);
347                         bst = rman_get_bustag(ctlr->r_mem);
348                         bus_space_subregion(bst, bsh, offset, PORT_SIZE, &bsh);
349                         rman_set_bushandle(res, bsh);
350                         rman_set_bustag(res, bst);
351                 }
352                 break;
353         case SYS_RES_IRQ:
354                 if (*rid == ATA_IRQ_RID)
355                         res = ctlr->irq.r_irq;
356                 break;
357         }
358         return (res);
359 }
360
361 static int
362 mvs_release_resource(device_t dev, device_t child, int type, int rid,
363                          struct resource *r)
364 {
365
366         switch (type) {
367         case SYS_RES_MEMORY:
368                 rman_release_resource(r);
369                 return (0);
370         case SYS_RES_IRQ:
371                 if (rid != ATA_IRQ_RID)
372                         return ENOENT;
373                 return (0);
374         }
375         return (EINVAL);
376 }
377
378 static int
379 mvs_setup_intr(device_t dev, device_t child, struct resource *irq, 
380                    int flags, driver_filter_t *filter, driver_intr_t *function, 
381                    void *argument, void **cookiep)
382 {
383         struct mvs_controller *ctlr = device_get_softc(dev);
384         int unit = (intptr_t)device_get_ivars(child);
385
386         if (filter != NULL) {
387                 printf("mvs.c: we cannot use a filter here\n");
388                 return (EINVAL);
389         }
390         ctlr->interrupt[unit].function = function;
391         ctlr->interrupt[unit].argument = argument;
392         return (0);
393 }
394
395 static int
396 mvs_teardown_intr(device_t dev, device_t child, struct resource *irq,
397                       void *cookie)
398 {
399         struct mvs_controller *ctlr = device_get_softc(dev);
400         int unit = (intptr_t)device_get_ivars(child);
401
402         ctlr->interrupt[unit].function = NULL;
403         ctlr->interrupt[unit].argument = NULL;
404         return (0);
405 }
406
407 static int
408 mvs_print_child(device_t dev, device_t child)
409 {
410         int retval;
411
412         retval = bus_print_child_header(dev, child);
413         retval += printf(" at channel %d",
414             (int)(intptr_t)device_get_ivars(child));
415         retval += bus_print_child_footer(dev, child);
416
417         return (retval);
418 }
419
420 static int
421 mvs_child_location_str(device_t dev, device_t child, char *buf,
422     size_t buflen)
423 {
424
425         snprintf(buf, buflen, "channel=%d",
426             (int)(intptr_t)device_get_ivars(child));
427         return (0);
428 }
429
430 static device_method_t mvs_methods[] = {
431         DEVMETHOD(device_probe,     mvs_probe),
432         DEVMETHOD(device_attach,    mvs_attach),
433         DEVMETHOD(device_detach,    mvs_detach),
434         DEVMETHOD(device_suspend,   mvs_suspend),
435         DEVMETHOD(device_resume,    mvs_resume),
436         DEVMETHOD(bus_print_child,  mvs_print_child),
437         DEVMETHOD(bus_alloc_resource,       mvs_alloc_resource),
438         DEVMETHOD(bus_release_resource,     mvs_release_resource),
439         DEVMETHOD(bus_setup_intr,   mvs_setup_intr),
440         DEVMETHOD(bus_teardown_intr,mvs_teardown_intr),
441         DEVMETHOD(mvs_edma,         mvs_edma),
442         DEVMETHOD(bus_child_location_str, mvs_child_location_str),
443         { 0, 0 }
444 };
445 static driver_t mvs_driver = {
446         "mvs",
447         mvs_methods,
448         sizeof(struct mvs_controller)
449 };
450 DRIVER_MODULE(mvs, simplebus, mvs_driver, mvs_devclass, 0, 0);
451 MODULE_VERSION(mvs, 1);
452 MODULE_DEPEND(mvs, cam, 1, 1, 1);