1/*-
2 * Copyright 2002 by Peter Grehan. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 *    notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 *    notice, this list of conditions and the following disclaimer in the
11 *    documentation and/or other materials provided with the distribution.
12 * 3. The name of the author may not be used to endorse or promote products
13 *    derived from this software without specific prior written permission.
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,
20 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
22 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
23 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGE.
26 *
27 * $FreeBSD$
28 */
29
30/*
31 * Driver for KeyLargo/Pangea, the MacPPC south bridge ASIC.
32 */
33
34#include <sys/param.h>
35#include <sys/systm.h>
36#include <sys/kernel.h>
37#include <sys/malloc.h>
38#include <sys/module.h>
39#include <sys/bus.h>
40#include <sys/rman.h>
41
42#include <vm/vm.h>
43#include <vm/pmap.h>
44
45#include <machine/bus.h>
46#include <machine/intr_machdep.h>
47#include <machine/pmap.h>
48#include <machine/resource.h>
49#include <machine/vmparam.h>
50
51#include <dev/ofw/ofw_bus.h>
52#include <dev/ofw/ofw_bus_subr.h>
53#include <dev/ofw/openfirm.h>
54
55#include <powerpc/powermac/maciovar.h>
56
57#include <dev/pci/pcivar.h>
58#include <dev/pci/pcireg.h>
59
60/*
61 * Macio softc
62 */
63struct macio_softc {
64	phandle_t    sc_node;
65	vm_offset_t  sc_base;
66	vm_offset_t  sc_size;
67	struct rman  sc_mem_rman;
68
69	/* FCR registers */
70	int          sc_memrid;
71	struct resource	*sc_memr;
72};
73
74static MALLOC_DEFINE(M_MACIO, "macio", "macio device information");
75
76static int  macio_probe(device_t);
77static int  macio_attach(device_t);
78static int  macio_print_child(device_t dev, device_t child);
79static void macio_probe_nomatch(device_t, device_t);
80static struct   resource *macio_alloc_resource(device_t, device_t, int, int *,
81					       u_long, u_long, u_long, u_int);
82static int  macio_activate_resource(device_t, device_t, int, int,
83				    struct resource *);
84static int  macio_deactivate_resource(device_t, device_t, int, int,
85				      struct resource *);
86static int  macio_release_resource(device_t, device_t, int, int,
87				   struct resource *);
88static struct resource_list *macio_get_resource_list (device_t, device_t);
89static ofw_bus_get_devinfo_t macio_get_devinfo;
90
91/*
92 * Bus interface definition
93 */
94static device_method_t macio_methods[] = {
95	/* Device interface */
96	DEVMETHOD(device_probe,         macio_probe),
97	DEVMETHOD(device_attach,        macio_attach),
98	DEVMETHOD(device_detach,        bus_generic_detach),
99	DEVMETHOD(device_shutdown,      bus_generic_shutdown),
100	DEVMETHOD(device_suspend,       bus_generic_suspend),
101	DEVMETHOD(device_resume,        bus_generic_resume),
102
103	/* Bus interface */
104	DEVMETHOD(bus_print_child,      macio_print_child),
105	DEVMETHOD(bus_probe_nomatch,    macio_probe_nomatch),
106	DEVMETHOD(bus_setup_intr,       bus_generic_setup_intr),
107	DEVMETHOD(bus_teardown_intr,    bus_generic_teardown_intr),
108
109        DEVMETHOD(bus_alloc_resource,   macio_alloc_resource),
110        DEVMETHOD(bus_release_resource, macio_release_resource),
111        DEVMETHOD(bus_activate_resource, macio_activate_resource),
112        DEVMETHOD(bus_deactivate_resource, macio_deactivate_resource),
113        DEVMETHOD(bus_get_resource_list, macio_get_resource_list),
114
115	DEVMETHOD(bus_child_pnpinfo_str, ofw_bus_gen_child_pnpinfo_str),
116
117	/* ofw_bus interface */
118	DEVMETHOD(ofw_bus_get_devinfo,	macio_get_devinfo),
119	DEVMETHOD(ofw_bus_get_compat,	ofw_bus_gen_get_compat),
120	DEVMETHOD(ofw_bus_get_model,	ofw_bus_gen_get_model),
121	DEVMETHOD(ofw_bus_get_name,	ofw_bus_gen_get_name),
122	DEVMETHOD(ofw_bus_get_node,	ofw_bus_gen_get_node),
123	DEVMETHOD(ofw_bus_get_type,	ofw_bus_gen_get_type),
124
125	{ 0, 0 }
126};
127
128static driver_t macio_pci_driver = {
129        "macio",
130        macio_methods,
131	sizeof(struct macio_softc)
132};
133
134devclass_t macio_devclass;
135
136DRIVER_MODULE(macio, pci, macio_pci_driver, macio_devclass, 0, 0);
137
138/*
139 * PCI ID search table
140 */
141static struct macio_pci_dev {
142        u_int32_t  mpd_devid;
143	char    *mpd_desc;
144} macio_pci_devlist[] = {
145	{ 0x0017106b, "Paddington I/O Controller" },
146	{ 0x0022106b, "KeyLargo I/O Controller" },
147	{ 0x0025106b, "Pangea I/O Controller" },
148	{ 0x003e106b, "Intrepid I/O Controller" },
149	{ 0x0041106b, "K2 KeyLargo I/O Controller" },
150	{ 0x004f106b, "Shasta I/O Controller" },
151	{ 0, NULL }
152};
153
154/*
155 * Devices to exclude from the probe
156 * XXX some of these may be required in the future...
157 */
158#define	MACIO_QUIRK_IGNORE		0x00000001
159#define	MACIO_QUIRK_CHILD_HAS_INTR	0x00000002
160#define	MACIO_QUIRK_USE_CHILD_REG	0x00000004
161
162struct macio_quirk_entry {
163	const char	*mq_name;
164	int		mq_quirks;
165};
166
167static struct macio_quirk_entry macio_quirks[] = {
168	{ "escc-legacy",		MACIO_QUIRK_IGNORE },
169	{ "timer",			MACIO_QUIRK_IGNORE },
170	{ "escc",			MACIO_QUIRK_CHILD_HAS_INTR },
171        { "i2s", 			MACIO_QUIRK_CHILD_HAS_INTR |
172					MACIO_QUIRK_USE_CHILD_REG },
173	{ NULL,				0 }
174};
175
176static int
177macio_get_quirks(const char *name)
178{
179        struct	macio_quirk_entry *mqe;
180
181        for (mqe = macio_quirks; mqe->mq_name != NULL; mqe++)
182                if (strcmp(name, mqe->mq_name) == 0)
183                        return (mqe->mq_quirks);
184        return (0);
185}
186
187
188/*
189 * Add an interrupt to the dev's resource list if present
190 */
191static void
192macio_add_intr(phandle_t devnode, struct macio_devinfo *dinfo)
193{
194	phandle_t iparent;
195	int	*intr;
196	int	i, nintr;
197	int 	icells;
198
199	if (dinfo->mdi_ninterrupts >= 6) {
200		printf("macio: device has more than 6 interrupts\n");
201		return;
202	}
203
204	if (OF_searchprop(devnode, "#interrupt-cells", &icells, sizeof(icells))
205	    <= 0)
206		icells = 1;
207
208	nintr = OF_getprop_alloc(devnode, "interrupts", sizeof(*intr),
209		(void **)&intr);
210	if (nintr == -1) {
211		nintr = OF_getprop_alloc(devnode, "AAPL,interrupts",
212			sizeof(*intr), (void **)&intr);
213		if (nintr == -1)
214			return;
215	}
216
217	if (intr[0] == -1)
218		return;
219
220	if (OF_getprop(devnode, "interrupt-parent", &iparent, sizeof(iparent))
221	    <= 0)
222		panic("Interrupt but no interrupt parent!\n");
223
224	for (i = 0; i < nintr; i+=icells) {
225		u_int irq = MAP_IRQ(iparent, intr[i]);
226
227		resource_list_add(&dinfo->mdi_resources, SYS_RES_IRQ,
228		    dinfo->mdi_ninterrupts, irq, irq, 1);
229
230		dinfo->mdi_interrupts[dinfo->mdi_ninterrupts] = irq;
231		dinfo->mdi_ninterrupts++;
232	}
233}
234
235
236static void
237macio_add_reg(phandle_t devnode, struct macio_devinfo *dinfo)
238{
239	struct	macio_reg *reg;
240	int	i, nreg;
241
242	nreg = OF_getprop_alloc(devnode, "reg", sizeof(*reg), (void **)&reg);
243	if (nreg == -1)
244		return;
245
246	for (i = 0; i < nreg; i++) {
247		resource_list_add(&dinfo->mdi_resources, SYS_RES_MEMORY, i,
248		    reg[i].mr_base, reg[i].mr_base + reg[i].mr_size,
249		    reg[i].mr_size);
250	}
251}
252
253/*
254 * PCI probe
255 */
256static int
257macio_probe(device_t dev)
258{
259        int i;
260        u_int32_t devid;
261
262        devid = pci_get_devid(dev);
263        for (i = 0; macio_pci_devlist[i].mpd_desc != NULL; i++) {
264                if (devid == macio_pci_devlist[i].mpd_devid) {
265                        device_set_desc(dev, macio_pci_devlist[i].mpd_desc);
266                        return (0);
267                }
268        }
269
270        return (ENXIO);
271}
272
273/*
274 * PCI attach: scan Open Firmware child nodes, and attach these as children
275 * of the macio bus
276 */
277static int
278macio_attach(device_t dev)
279{
280	struct macio_softc *sc;
281        struct macio_devinfo *dinfo;
282        phandle_t  root;
283	phandle_t  child;
284	phandle_t  subchild;
285        device_t cdev;
286        u_int reg[3];
287	int error, quirks;
288
289	sc = device_get_softc(dev);
290	root = sc->sc_node = ofw_bus_get_node(dev);
291
292	/*
293	 * Locate the device node and it's base address
294	 */
295	if (OF_getprop(root, "assigned-addresses",
296		       reg, sizeof(reg)) < (ssize_t)sizeof(reg)) {
297		return (ENXIO);
298	}
299
300	sc->sc_base = reg[2];
301	sc->sc_size = MACIO_REG_SIZE;
302
303	sc->sc_memrid = PCIR_BAR(0);
304	sc->sc_memr = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
305	    &sc->sc_memrid, RF_ACTIVE);
306
307	sc->sc_mem_rman.rm_type = RMAN_ARRAY;
308	sc->sc_mem_rman.rm_descr = "MacIO Device Memory";
309	error = rman_init(&sc->sc_mem_rman);
310	if (error) {
311		device_printf(dev, "rman_init() failed. error = %d\n", error);
312		return (error);
313	}
314	error = rman_manage_region(&sc->sc_mem_rman, 0, sc->sc_size);
315	if (error) {
316		device_printf(dev,
317		    "rman_manage_region() failed. error = %d\n", error);
318		return (error);
319	}
320
321	/*
322	 * Iterate through the sub-devices
323	 */
324	for (child = OF_child(root); child != 0; child = OF_peer(child)) {
325		dinfo = malloc(sizeof(*dinfo), M_MACIO, M_WAITOK | M_ZERO);
326		if (ofw_bus_gen_setup_devinfo(&dinfo->mdi_obdinfo, child) !=
327		    0) {
328			free(dinfo, M_MACIO);
329			continue;
330		}
331		quirks = macio_get_quirks(dinfo->mdi_obdinfo.obd_name);
332		if ((quirks & MACIO_QUIRK_IGNORE) != 0) {
333			ofw_bus_gen_destroy_devinfo(&dinfo->mdi_obdinfo);
334			free(dinfo, M_MACIO);
335			continue;
336		}
337		resource_list_init(&dinfo->mdi_resources);
338		dinfo->mdi_ninterrupts = 0;
339		macio_add_intr(child, dinfo);
340		if ((quirks & MACIO_QUIRK_USE_CHILD_REG) != 0)
341			macio_add_reg(OF_child(child), dinfo);
342		else
343			macio_add_reg(child, dinfo);
344		if ((quirks & MACIO_QUIRK_CHILD_HAS_INTR) != 0)
345			for (subchild = OF_child(child); subchild != 0;
346			    subchild = OF_peer(subchild))
347				macio_add_intr(subchild, dinfo);
348		cdev = device_add_child(dev, NULL, -1);
349		if (cdev == NULL) {
350			device_printf(dev, "<%s>: device_add_child failed\n",
351			    dinfo->mdi_obdinfo.obd_name);
352			resource_list_free(&dinfo->mdi_resources);
353			ofw_bus_gen_destroy_devinfo(&dinfo->mdi_obdinfo);
354			free(dinfo, M_MACIO);
355			continue;
356		}
357		device_set_ivars(cdev, dinfo);
358
359		/* Set FCRs to enable some devices */
360		if (sc->sc_memr == NULL)
361			continue;
362
363		if (strcmp(ofw_bus_get_name(cdev), "bmac") == 0 ||
364		    strcmp(ofw_bus_get_compat(cdev), "bmac+") == 0) {
365			uint32_t fcr;
366
367			fcr = bus_read_4(sc->sc_memr, HEATHROW_FCR);
368
369			fcr |= FCR_ENET_ENABLE & ~FCR_ENET_RESET;
370			bus_write_4(sc->sc_memr, HEATHROW_FCR, fcr);
371			DELAY(50000);
372			fcr |= FCR_ENET_RESET;
373			bus_write_4(sc->sc_memr, HEATHROW_FCR, fcr);
374			DELAY(50000);
375			fcr &= ~FCR_ENET_RESET;
376			bus_write_4(sc->sc_memr, HEATHROW_FCR, fcr);
377			DELAY(50000);
378
379			bus_write_4(sc->sc_memr, HEATHROW_FCR, fcr);
380		}
381	}
382
383	return (bus_generic_attach(dev));
384}
385
386
387static int
388macio_print_child(device_t dev, device_t child)
389{
390        struct macio_devinfo *dinfo;
391        struct resource_list *rl;
392        int retval = 0;
393
394        dinfo = device_get_ivars(child);
395        rl = &dinfo->mdi_resources;
396
397        retval += bus_print_child_header(dev, child);
398
399        retval += resource_list_print_type(rl, "mem", SYS_RES_MEMORY, "%#lx");
400        retval += resource_list_print_type(rl, "irq", SYS_RES_IRQ, "%ld");
401
402        retval += bus_print_child_footer(dev, child);
403
404        return (retval);
405}
406
407
408static void
409macio_probe_nomatch(device_t dev, device_t child)
410{
411        struct macio_devinfo *dinfo;
412        struct resource_list *rl;
413	const char *type;
414
415	if (bootverbose) {
416		dinfo = device_get_ivars(child);
417		rl = &dinfo->mdi_resources;
418
419		if ((type = ofw_bus_get_type(child)) == NULL)
420			type = "(unknown)";
421		device_printf(dev, "<%s, %s>", type, ofw_bus_get_name(child));
422		resource_list_print_type(rl, "mem", SYS_RES_MEMORY, "%#lx");
423		resource_list_print_type(rl, "irq", SYS_RES_IRQ, "%ld");
424		printf(" (no driver attached)\n");
425	}
426}
427
428
429static struct resource *
430macio_alloc_resource(device_t bus, device_t child, int type, int *rid,
431		     u_long start, u_long end, u_long count, u_int flags)
432{
433	struct		macio_softc *sc;
434	int		needactivate;
435	struct		resource *rv;
436	struct		rman *rm;
437	u_long		adjstart, adjend, adjcount;
438	struct		macio_devinfo *dinfo;
439	struct		resource_list_entry *rle;
440
441	sc = device_get_softc(bus);
442	dinfo = device_get_ivars(child);
443
444	needactivate = flags & RF_ACTIVE;
445	flags &= ~RF_ACTIVE;
446
447	switch (type) {
448	case SYS_RES_MEMORY:
449	case SYS_RES_IOPORT:
450		rle = resource_list_find(&dinfo->mdi_resources, SYS_RES_MEMORY,
451		    *rid);
452		if (rle == NULL) {
453			device_printf(bus, "no rle for %s memory %d\n",
454			    device_get_nameunit(child), *rid);
455			return (NULL);
456		}
457
458		if (start < rle->start)
459			adjstart = rle->start;
460		else if (start > rle->end)
461			adjstart = rle->end;
462		else
463			adjstart = start;
464
465		if (end < rle->start)
466			adjend = rle->start;
467		else if (end > rle->end)
468			adjend = rle->end;
469		else
470			adjend = end;
471
472		adjcount = adjend - adjstart;
473
474		rm = &sc->sc_mem_rman;
475		break;
476
477	case SYS_RES_IRQ:
478		/* Check for passthrough from subattachments like macgpio */
479		if (device_get_parent(child) != bus)
480			return BUS_ALLOC_RESOURCE(device_get_parent(bus), child,
481			    type, rid, start, end, count, flags);
482
483		rle = resource_list_find(&dinfo->mdi_resources, SYS_RES_IRQ,
484		    *rid);
485		if (rle == NULL) {
486			if (dinfo->mdi_ninterrupts >= 6) {
487				device_printf(bus,
488				    "%s has more than 6 interrupts\n",
489				    device_get_nameunit(child));
490				return (NULL);
491			}
492			resource_list_add(&dinfo->mdi_resources, SYS_RES_IRQ,
493			    dinfo->mdi_ninterrupts, start, start, 1);
494
495			dinfo->mdi_interrupts[dinfo->mdi_ninterrupts] = start;
496			dinfo->mdi_ninterrupts++;
497		}
498
499		return (resource_list_alloc(&dinfo->mdi_resources, bus, child,
500		    type, rid, start, end, count, flags));
501
502	default:
503		device_printf(bus, "unknown resource request from %s\n",
504			      device_get_nameunit(child));
505		return (NULL);
506	}
507
508	rv = rman_reserve_resource(rm, adjstart, adjend, adjcount, flags,
509	    child);
510	if (rv == NULL) {
511		device_printf(bus,
512		    "failed to reserve resource %#lx - %#lx (%#lx) for %s\n",
513		    adjstart, adjend, adjcount, device_get_nameunit(child));
514		return (NULL);
515	}
516
517	rman_set_rid(rv, *rid);
518
519	if (needactivate) {
520		if (bus_activate_resource(child, type, *rid, rv) != 0) {
521                        device_printf(bus,
522				      "failed to activate resource for %s\n",
523				      device_get_nameunit(child));
524			rman_release_resource(rv);
525			return (NULL);
526                }
527        }
528
529	return (rv);
530}
531
532
533static int
534macio_release_resource(device_t bus, device_t child, int type, int rid,
535		       struct resource *res)
536{
537	if (rman_get_flags(res) & RF_ACTIVE) {
538		int error = bus_deactivate_resource(child, type, rid, res);
539		if (error)
540			return error;
541	}
542
543	return (rman_release_resource(res));
544}
545
546
547static int
548macio_activate_resource(device_t bus, device_t child, int type, int rid,
549			   struct resource *res)
550{
551	struct macio_softc *sc;
552	void    *p;
553
554	sc = device_get_softc(bus);
555
556	if (type == SYS_RES_IRQ)
557                return (bus_activate_resource(bus, type, rid, res));
558
559	if ((type == SYS_RES_MEMORY) || (type == SYS_RES_IOPORT)) {
560		p = pmap_mapdev((vm_offset_t)rman_get_start(res) + sc->sc_base,
561				(vm_size_t)rman_get_size(res));
562		if (p == NULL)
563			return (ENOMEM);
564		rman_set_virtual(res, p);
565		rman_set_bustag(res, &bs_le_tag);
566		rman_set_bushandle(res, (u_long)p);
567	}
568
569	return (rman_activate_resource(res));
570}
571
572
573static int
574macio_deactivate_resource(device_t bus, device_t child, int type, int rid,
575			  struct resource *res)
576{
577        /*
578         * If this is a memory resource, unmap it.
579         */
580        if ((type == SYS_RES_MEMORY) || (type == SYS_RES_IOPORT)) {
581		u_int32_t psize;
582
583		psize = rman_get_size(res);
584		pmap_unmapdev((vm_offset_t)rman_get_virtual(res), psize);
585	}
586
587	return (rman_deactivate_resource(res));
588}
589
590
591static struct resource_list *
592macio_get_resource_list (device_t dev, device_t child)
593{
594	struct macio_devinfo *dinfo;
595
596	dinfo = device_get_ivars(child);
597	return (&dinfo->mdi_resources);
598}
599
600static const struct ofw_bus_devinfo *
601macio_get_devinfo(device_t dev, device_t child)
602{
603	struct macio_devinfo *dinfo;
604
605	dinfo = device_get_ivars(child);
606	return (&dinfo->mdi_obdinfo);
607}
608
609int
610macio_enable_wireless(device_t dev, bool enable)
611{
612	struct macio_softc *sc = device_get_softc(dev);
613	uint32_t x;
614
615	if (enable) {
616		x = bus_read_4(sc->sc_memr, KEYLARGO_FCR2);
617		x |= 0x4;
618		bus_write_4(sc->sc_memr, KEYLARGO_FCR2, x);
619
620		/* Enable card slot. */
621		bus_write_1(sc->sc_memr, KEYLARGO_GPIO_BASE + 0x0f, 5);
622		DELAY(1000);
623		bus_write_1(sc->sc_memr, KEYLARGO_GPIO_BASE + 0x0f, 4);
624		DELAY(1000);
625		x = bus_read_4(sc->sc_memr, KEYLARGO_FCR2);
626		x &= ~0x80000000;
627
628		bus_write_4(sc->sc_memr, KEYLARGO_FCR2, x);
629		/* out8(gpio + 0x10, 4); */
630
631		bus_write_1(sc->sc_memr, KEYLARGO_EXTINT_GPIO_REG_BASE + 0x0b, 0);
632		bus_write_1(sc->sc_memr, KEYLARGO_EXTINT_GPIO_REG_BASE + 0x0a, 0x28);
633		bus_write_1(sc->sc_memr, KEYLARGO_EXTINT_GPIO_REG_BASE + 0x0d, 0x28);
634		bus_write_1(sc->sc_memr, KEYLARGO_GPIO_BASE + 0x0d, 0x28);
635		bus_write_1(sc->sc_memr, KEYLARGO_GPIO_BASE + 0x0e, 0x28);
636		bus_write_4(sc->sc_memr, 0x1c000, 0);
637
638		/* Initialize the card. */
639		bus_write_4(sc->sc_memr, 0x1a3e0, 0x41);
640		x = bus_read_4(sc->sc_memr, KEYLARGO_FCR2);
641		x |= 0x80000000;
642		bus_write_4(sc->sc_memr, KEYLARGO_FCR2, x);
643	} else {
644		x = bus_read_4(sc->sc_memr, KEYLARGO_FCR2);
645		x &= ~0x4;
646		bus_write_4(sc->sc_memr, KEYLARGO_FCR2, x);
647		/* out8(gpio + 0x10, 0); */
648	}
649
650	return (0);
651}
652