macio_asic.c

来自「linux 内核源代码」· C语言 代码 · 共 734 行 · 第 1/2 页

C
734
字号
/* * Bus & driver management routines for devices within * a MacIO ASIC. Interface to new driver model mostly * stolen from the PCI version. *  *  Copyright (C) 2005 Ben. Herrenschmidt (benh@kernel.crashing.org) * *  This program is free software; you can redistribute it and/or *  modify it under the terms of the GNU General Public License *  as published by the Free Software Foundation; either version *  2 of the License, or (at your option) any later version. * * TODO: *  *  - Don't probe below media bay by default, but instead provide *    some hooks for media bay to dynamically add/remove it's own *    sub-devices. */ #include <linux/string.h>#include <linux/kernel.h>#include <linux/pci.h>#include <linux/pci_ids.h>#include <linux/init.h>#include <linux/module.h>#include <linux/slab.h>#include <asm/machdep.h>#include <asm/macio.h>#include <asm/pmac_feature.h>#include <asm/prom.h>#include <asm/pci-bridge.h>#undef DEBUG#define MAX_NODE_NAME_SIZE (BUS_ID_SIZE - 12)static struct macio_chip      *macio_on_hold;static int macio_bus_match(struct device *dev, struct device_driver *drv) {	struct macio_dev * macio_dev = to_macio_device(dev);	struct macio_driver * macio_drv = to_macio_driver(drv);	const struct of_device_id * matches = macio_drv->match_table;	if (!matches) 		return 0;	return of_match_device(matches, &macio_dev->ofdev) != NULL;}struct macio_dev *macio_dev_get(struct macio_dev *dev){	struct device *tmp;	if (!dev)		return NULL;	tmp = get_device(&dev->ofdev.dev);	if (tmp)		return to_macio_device(tmp);	else		return NULL;}void macio_dev_put(struct macio_dev *dev){	if (dev)		put_device(&dev->ofdev.dev);}static int macio_device_probe(struct device *dev){	int error = -ENODEV;	struct macio_driver *drv;	struct macio_dev *macio_dev;	const struct of_device_id *match;	drv = to_macio_driver(dev->driver);	macio_dev = to_macio_device(dev);	if (!drv->probe)		return error;	macio_dev_get(macio_dev);	match = of_match_device(drv->match_table, &macio_dev->ofdev);	if (match)		error = drv->probe(macio_dev, match);	if (error)		macio_dev_put(macio_dev);	return error;}static int macio_device_remove(struct device *dev){	struct macio_dev * macio_dev = to_macio_device(dev);	struct macio_driver * drv = to_macio_driver(dev->driver);	if (dev->driver && drv->remove)		drv->remove(macio_dev);	macio_dev_put(macio_dev);	return 0;}static void macio_device_shutdown(struct device *dev){	struct macio_dev * macio_dev = to_macio_device(dev);	struct macio_driver * drv = to_macio_driver(dev->driver);	if (dev->driver && drv->shutdown)		drv->shutdown(macio_dev);}static int macio_device_suspend(struct device *dev, pm_message_t state){	struct macio_dev * macio_dev = to_macio_device(dev);	struct macio_driver * drv = to_macio_driver(dev->driver);	if (dev->driver && drv->suspend)		return drv->suspend(macio_dev, state);	return 0;}static int macio_device_resume(struct device * dev){	struct macio_dev * macio_dev = to_macio_device(dev);	struct macio_driver * drv = to_macio_driver(dev->driver);	if (dev->driver && drv->resume)		return drv->resume(macio_dev);	return 0;}extern struct device_attribute macio_dev_attrs[];struct bus_type macio_bus_type = {       .name	= "macio",       .match	= macio_bus_match,       .uevent = of_device_uevent,       .probe	= macio_device_probe,       .remove	= macio_device_remove,       .shutdown = macio_device_shutdown,       .suspend	= macio_device_suspend,       .resume	= macio_device_resume,       .dev_attrs = macio_dev_attrs,};static int __init macio_bus_driver_init(void){	return bus_register(&macio_bus_type);}postcore_initcall(macio_bus_driver_init);/** * macio_release_dev - free a macio device structure when all users of it are * finished. * @dev: device that's been disconnected * * Will be called only by the device core when all users of this macio device * are done. This currently means never as we don't hot remove any macio * device yet, though that will happen with mediabay based devices in a later * implementation. */static void macio_release_dev(struct device *dev){	struct macio_dev *mdev;        mdev = to_macio_device(dev);	kfree(mdev);}/** * macio_resource_quirks - tweak or skip some resources for a device * @np: pointer to the device node * @res: resulting resource * @index: index of resource in node * * If this routine returns non-null, then the resource is completely * skipped. */static int macio_resource_quirks(struct device_node *np, struct resource *res,				 int index){	/* Only quirks for memory resources for now */	if ((res->flags & IORESOURCE_MEM) == 0)		return 0;	/* Grand Central has too large resource 0 on some machines */	if (index == 0 && !strcmp(np->name, "gc"))		res->end = res->start + 0x1ffff;	/* Airport has bogus resource 2 */	if (index >= 2 && !strcmp(np->name, "radio"))		return 1;#ifndef CONFIG_PPC64	/* DBDMAs may have bogus sizes */	if ((res->start & 0x0001f000) == 0x00008000)		res->end = res->start + 0xff;#endif /* CONFIG_PPC64 */	/* ESCC parent eats child resources. We could have added a	 * level of hierarchy, but I don't really feel the need	 * for it	 */	if (!strcmp(np->name, "escc"))		return 1;	/* ESCC has bogus resources >= 3 */	if (index >= 3 && !(strcmp(np->name, "ch-a") &&			    strcmp(np->name, "ch-b")))		return 1;	/* Media bay has too many resources, keep only first one */	if (index > 0 && !strcmp(np->name, "media-bay"))		return 1;	/* Some older IDE resources have bogus sizes */	if (!(strcmp(np->name, "IDE") && strcmp(np->name, "ATA") &&	      strcmp(np->type, "ide") && strcmp(np->type, "ata"))) {		if (index == 0 && (res->end - res->start) > 0xfff)			res->end = res->start + 0xfff;		if (index == 1 && (res->end - res->start) > 0xff)			res->end = res->start + 0xff;	}	return 0;}static void macio_create_fixup_irq(struct macio_dev *dev, int index,				   unsigned int line){	unsigned int irq;	irq = irq_create_mapping(NULL, line);	if (irq != NO_IRQ) {		dev->interrupt[index].start = irq;		dev->interrupt[index].flags = IORESOURCE_IRQ;		dev->interrupt[index].name = dev->ofdev.dev.bus_id;	}	if (dev->n_interrupts <= index)		dev->n_interrupts = index + 1;}static void macio_add_missing_resources(struct macio_dev *dev){	struct device_node *np = dev->ofdev.node;	unsigned int irq_base;	/* Gatwick has some missing interrupts on child nodes */	if (dev->bus->chip->type != macio_gatwick)		return;	/* irq_base is always 64 on gatwick. I have no cleaner way to get	 * that value from here at this point	 */	irq_base = 64;	/* Fix SCC */	if (strcmp(np->name, "ch-a") == 0) {		macio_create_fixup_irq(dev, 0, 15 + irq_base);		macio_create_fixup_irq(dev, 1,  4 + irq_base);		macio_create_fixup_irq(dev, 2,  5 + irq_base);		printk(KERN_INFO "macio: fixed SCC irqs on gatwick\n");	}	/* Fix media-bay */	if (strcmp(np->name, "media-bay") == 0) {		macio_create_fixup_irq(dev, 0, 29 + irq_base);		printk(KERN_INFO "macio: fixed media-bay irq on gatwick\n");	}	/* Fix left media bay childs */	if (dev->media_bay != NULL && strcmp(np->name, "floppy") == 0) {		macio_create_fixup_irq(dev, 0, 19 + irq_base);		macio_create_fixup_irq(dev, 1,  1 + irq_base);		printk(KERN_INFO "macio: fixed left floppy irqs\n");	}	if (dev->media_bay != NULL && strcasecmp(np->name, "ata4") == 0) {		macio_create_fixup_irq(dev, 0, 14 + irq_base);		macio_create_fixup_irq(dev, 0,  3 + irq_base);		printk(KERN_INFO "macio: fixed left ide irqs\n");	}}static void macio_setup_interrupts(struct macio_dev *dev){	struct device_node *np = dev->ofdev.node;	unsigned int irq;	int i = 0, j = 0;	for (;;) {		struct resource *res = &dev->interrupt[j];		if (j >= MACIO_DEV_COUNT_IRQS)			break;		irq = irq_of_parse_and_map(np, i++);		if (irq == NO_IRQ)			break;		res->start = irq;		res->flags = IORESOURCE_IRQ;		res->name = dev->ofdev.dev.bus_id;		if (macio_resource_quirks(np, res, i - 1)) {			memset(res, 0, sizeof(struct resource));			continue;		} else			j++;	}	dev->n_interrupts = j;}static void macio_setup_resources(struct macio_dev *dev,				  struct resource *parent_res){	struct device_node *np = dev->ofdev.node;	struct resource r;	int index;	for (index = 0; of_address_to_resource(np, index, &r) == 0; index++) {		struct resource *res = &dev->resource[index];		if (index >= MACIO_DEV_COUNT_RESOURCES)			break;		*res = r;		res->name = dev->ofdev.dev.bus_id;		if (macio_resource_quirks(np, res, index)) {			memset(res, 0, sizeof(struct resource));			continue;		}		/* Currently, we consider failure as harmless, this may		 * change in the future, once I've found all the device		 * tree bugs in older machines & worked around them		 */		if (insert_resource(parent_res, res)) {			printk(KERN_WARNING "Can't request resource "			       "%d for MacIO device %s\n",			       index, dev->ofdev.dev.bus_id);		}	}	dev->n_resources = index;}/** * macio_add_one_device - Add one device from OF node to the device tree * @chip: pointer to the macio_chip holding the device * @np: pointer to the device node in the OF tree * @in_bay: set to 1 if device is part of a media-bay * * When media-bay is changed to hotswap drivers, this function will * be exposed to the bay driver some way... */static struct macio_dev * macio_add_one_device(struct macio_chip *chip,					       struct device *parent,					       struct device_node *np,					       struct macio_dev *in_bay,					       struct resource *parent_res){	struct macio_dev *dev;	const u32 *reg;		if (np == NULL)		return NULL;

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?