ohci-pci.c 9.77 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/*
 * OHCI HCD (Host Controller Driver) for USB.
 *
 * (C) Copyright 1999 Roman Weissgaerber <weissg@vienna.at>
 * (C) Copyright 2000-2002 David Brownell <dbrownell@users.sourceforge.net>
 * 
 * [ Initialisation is based on Linus'  ]
 * [ uhci code and gregs ohci fragments ]
 * [ (C) Copyright 1999 Linus Torvalds  ]
 * [ (C) Copyright 1999 Gregory P. Smith]
 * 
 * PCI Bus Glue
 *
 * This file is licenced under the GPL.
 */
 
#ifdef CONFIG_PMAC_PBOOK
#include <asm/machdep.h>
#include <asm/pmac_feature.h>
#include <asm/pci-bridge.h>
21
#include <asm/prom.h>
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
#ifndef CONFIG_PM
#	define CONFIG_PM
#endif
#endif

#ifndef CONFIG_PCI
#error "This file is PCI bus glue.  CONFIG_PCI must be defined."
#endif

/*-------------------------------------------------------------------------*/

static int __devinit
ohci_pci_start (struct usb_hcd *hcd)
{
	struct ohci_hcd	*ohci = hcd_to_ohci (hcd);
	int		ret;

	if (hcd->pdev) {
		ohci->hcca = pci_alloc_consistent (hcd->pdev,
				sizeof *ohci->hcca, &ohci->hcca_dma);
		if (!ohci->hcca)
			return -ENOMEM;

		/* AMD 756, for most chips (early revs), corrupts register
		 * values on read ... so enable the vendor workaround.
		 */
48
		if (hcd->pdev->vendor == PCI_VENDOR_ID_AMD
49 50
				&& hcd->pdev->device == 0x740c) {
			ohci->flags = OHCI_QUIRK_AMD756;
51
			ohci_info (ohci, "AMD756 erratum 4 workaround\n");
52 53
		}

54 55 56 57
		/* FIXME for some of the early AMD 760 southbridges, OHCI
		 * won't work at all.  blacklist them.
		 */

58 59 60 61
		/* Apple's OHCI driver has a lot of bizarre workarounds
		 * for this chip.  Evidently control and bulk lists
		 * can get confused.  (B&W G3 models, and ...)
		 */
62
		else if (hcd->pdev->vendor == PCI_VENDOR_ID_OPTI
63
				&& hcd->pdev->device == 0xc861) {
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
			ohci_info (ohci,
				"WARNING: OPTi workarounds unavailable\n");
		}

		/* Check for NSC87560. We have to look at the bridge (fn1) to
		 * identify the USB (fn2). This quirk might apply to more or
		 * even all NSC stuff.
		 */
		else if (hcd->pdev->vendor == PCI_VENDOR_ID_NS) {
			struct pci_dev	*b, *hc;

			hc = hcd->pdev;
			b  = pci_find_slot (hc->bus->number,
					PCI_DEVFN (PCI_SLOT (hc->devfn), 1));
			if (b && b->device == PCI_DEVICE_ID_NS_87560_LIO
					&& b->vendor == PCI_VENDOR_ID_NS) {
				ohci->flags |= OHCI_QUIRK_SUPERIO;
				ohci_info (ohci, "Using NSC SuperIO setup\n");
			}
83
		}
84
	
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
	}

        memset (ohci->hcca, 0, sizeof (struct ohci_hcca));
	if ((ret = ohci_mem_init (ohci)) < 0) {
		ohci_stop (hcd);
		return ret;
	}
	ohci->regs = hcd->regs;

	if (hc_reset (ohci) < 0) {
		ohci_stop (hcd);
		return -ENODEV;
	}

	if (hc_start (ohci) < 0) {
100
		ohci_err (ohci, "can't start\n");
101 102 103
		ohci_stop (hcd);
		return -EBUSY;
	}
104
	create_debug_files (ohci);
105 106 107 108 109 110 111 112 113 114 115 116 117 118

#ifdef	DEBUG
	ohci_dump (ohci, 1);
#endif
	return 0;
}

#ifdef	CONFIG_PM

static int ohci_pci_suspend (struct usb_hcd *hcd, u32 state)
{
	struct ohci_hcd		*ohci = hcd_to_ohci (hcd);
	unsigned long		flags;
	u16			cmd;
119
	u32			tmp;
120 121

	if ((ohci->hc_control & OHCI_CTRL_HCFS) != OHCI_USB_OPER) {
122
		ohci_dbg (ohci, "can't suspend (state is %s)\n",
123 124 125 126 127
			hcfs2string (ohci->hc_control & OHCI_CTRL_HCFS));
		return -EIO;
	}

	/* act as if usb suspend can always be used */
128
	ohci_dbg (ohci, "suspend to %d\n", state);
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
	ohci->sleeping = 1;

	/* First stop processing */
  	spin_lock_irqsave (&ohci->lock, flags);
	ohci->hc_control &=
		~(OHCI_CTRL_PLE|OHCI_CTRL_CLE|OHCI_CTRL_BLE|OHCI_CTRL_IE);
	writel (ohci->hc_control, &ohci->regs->control);
	writel (OHCI_INTR_SF, &ohci->regs->intrstatus);
	(void) readl (&ohci->regs->intrstatus);
  	spin_unlock_irqrestore (&ohci->lock, flags);

	/* Wait a frame or two */
	mdelay (1);
	if (!readl (&ohci->regs->intrstatus) & OHCI_INTR_SF)
		mdelay (1);
		
#ifdef CONFIG_PMAC_PBOOK
	if (_machine == _MACH_Pmac)
		disable_irq (hcd->pdev->irq);
 	/* else, 2.4 assumes shared irqs -- don't disable */
#endif

	/* Enable remote wakeup */
	writel (readl (&ohci->regs->intrenable) | OHCI_INTR_RD,
		&ohci->regs->intrenable);

	/* Suspend chip and let things settle down a bit */
 	ohci->hc_control = OHCI_USB_SUSPEND;
 	writel (ohci->hc_control, &ohci->regs->control);
	(void) readl (&ohci->regs->control);
	mdelay (500); /* No schedule here ! */

161 162
	tmp = readl (&ohci->regs->control) | OHCI_CTRL_HCFS;
	switch (tmp) {
163 164 165
		case OHCI_USB_RESET:
		case OHCI_USB_RESUME:
		case OHCI_USB_OPER:
166
			ohci_err (ohci, "can't suspend; hcfs %d\n", tmp);
167 168
			break;
		case OHCI_USB_SUSPEND:
169
			ohci_dbg (ohci, "suspended\n");
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
			break;
	}

	/* In some rare situations, Apple's OHCI have happily trashed
	 * memory during sleep. We disable its bus master bit during
	 * suspend
	 */
	pci_read_config_word (hcd->pdev, PCI_COMMAND, &cmd);
	cmd &= ~PCI_COMMAND_MASTER;
	pci_write_config_word (hcd->pdev, PCI_COMMAND, cmd);
#ifdef CONFIG_PMAC_PBOOK
	{
	   	struct device_node	*of_node;
 
		/* Disable USB PAD & cell clock */
		of_node = pci_device_to_OF_node (hcd->pdev);
		if (of_node)
			pmac_call_feature(PMAC_FTR_USB_ENABLE, of_node, 0, 0);
	}
#endif
	return 0;
}


static int ohci_pci_resume (struct usb_hcd *hcd)
{
	struct ohci_hcd		*ohci = hcd_to_ohci (hcd);
	int			temp;
	int			retval = 0;
	unsigned long		flags;

#ifdef CONFIG_PMAC_PBOOK
	{
		struct device_node *of_node;

		/* Re-enable USB PAD & cell clock */
		of_node = pci_device_to_OF_node (hcd->pdev);
		if (of_node)
			pmac_call_feature (PMAC_FTR_USB_ENABLE, of_node, 0, 1);
	}
#endif
	/* did we suspend, or were we powered off? */
	ohci->hc_control = readl (&ohci->regs->control);
	temp = ohci->hc_control & OHCI_CTRL_HCFS;

#ifdef DEBUG
	/* the registers may look crazy here */
217
	ohci_dump_status (ohci, 0, 0);
218 219 220 221 222 223 224 225
#endif

	/* Re-enable bus mastering */
	pci_set_master (ohci->hcd.pdev);
	
	switch (temp) {

	case OHCI_USB_RESET:	// lost power
226
		ohci_info (ohci, "USB restart\n");
227 228 229 230 231
		retval = hc_restart (ohci);
		break;

	case OHCI_USB_SUSPEND:	// host wakeup
	case OHCI_USB_RESUME:	// remote wakeup
232
		ohci_info (ohci, "USB continue from %s wakeup\n",
233 234 235 236 237 238 239 240 241 242 243 244
			 (temp == OHCI_USB_SUSPEND)
				? "host" : "remote");
		ohci->hc_control = OHCI_USB_RESUME;
		writel (ohci->hc_control, &ohci->regs->control);
		(void) readl (&ohci->regs->control);
		mdelay (20); /* no schedule here ! */
		/* Some controllers (lucent) need a longer delay here */
		mdelay (15);

		temp = readl (&ohci->regs->control);
		temp = ohci->hc_control & OHCI_CTRL_HCFS;
		if (temp != OHCI_USB_RESUME) {
245
			ohci_err (ohci, "controller won't resume\n");
246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283
			ohci->disabled = 1;
			retval = -EIO;
			break;
		}

		/* Some chips likes being resumed first */
		writel (OHCI_USB_OPER, &ohci->regs->control);
		(void) readl (&ohci->regs->control);
		mdelay (3);

		/* Then re-enable operations */
		spin_lock_irqsave (&ohci->lock, flags);
		ohci->disabled = 0;
		ohci->sleeping = 0;
		ohci->hc_control = OHCI_CONTROL_INIT | OHCI_USB_OPER;
		if (!ohci->ed_rm_list) {
			if (ohci->ed_controltail)
				ohci->hc_control |= OHCI_CTRL_CLE;
			if (ohci->ed_bulktail)
				ohci->hc_control |= OHCI_CTRL_BLE;
		}
		hcd->state = USB_STATE_READY;
		writel (ohci->hc_control, &ohci->regs->control);

		/* trigger a start-frame interrupt (why?) */
		writel (OHCI_INTR_SF, &ohci->regs->intrstatus);
		writel (OHCI_INTR_SF, &ohci->regs->intrenable);

		/* Check for a pending done list */
		writel (OHCI_INTR_WDH, &ohci->regs->intrdisable);	
		(void) readl (&ohci->regs->intrdisable);
		spin_unlock_irqrestore (&ohci->lock, flags);

#ifdef CONFIG_PMAC_PBOOK
		if (_machine == _MACH_Pmac)
			enable_irq (hcd->pdev->irq);
#endif
		if (ohci->hcca->done_head)
David S. Miller's avatar
David S. Miller committed
284
			dl_done_list (ohci, dl_reverse_done_list (ohci), NULL);
285 286 287 288 289 290
		writel (OHCI_INTR_WDH, &ohci->regs->intrenable); 

		/* assume there are TDs on the bulk and control lists */
		writel (OHCI_BLF | OHCI_CLF, &ohci->regs->cmdstatus);

// ohci_dump_status (ohci);
291 292
ohci_dbg (ohci, "sleeping = %d, disabled = %d\n",
		ohci->sleeping, ohci->disabled);
293 294 295
		break;

	default:
296
		ohci_warn (ohci, "odd PCI resume\n");
297 298 299 300 301 302 303 304 305 306
	}
	return retval;
}

#endif	/* CONFIG_PM */


/*-------------------------------------------------------------------------*/

static const struct hc_driver ohci_pci_hc_driver = {
307
	.description =		hcd_name,
308 309 310 311

	/*
	 * generic hardware linkage
	 */
312 313
	.irq =			ohci_irq,
	.flags =		HCD_MEMORY | HCD_USB11,
314 315 316 317

	/*
	 * basic lifecycle operations
	 */
318
	.start =		ohci_pci_start,
319
#ifdef	CONFIG_PM
320 321
	.suspend =		ohci_pci_suspend,
	.resume =		ohci_pci_resume,
322
#endif
323
	.stop =			ohci_stop,
324 325 326 327

	/*
	 * memory lifecycle (except per-request)
	 */
328 329
	.hcd_alloc =		ohci_hcd_alloc,
	.hcd_free =		ohci_hcd_free,
330 331 332 333

	/*
	 * managing i/o requests and associated device resources
	 */
334 335
	.urb_enqueue =		ohci_urb_enqueue,
	.urb_dequeue =		ohci_urb_dequeue,
336
	.endpoint_disable =	ohci_endpoint_disable,
337 338 339 340

	/*
	 * scheduling support
	 */
341
	.get_frame_number =	ohci_get_frame,
342 343 344 345

	/*
	 * root hub support
	 */
346 347
	.hub_status_data =	ohci_hub_status_data,
	.hub_control =		ohci_hub_control,
348 349 350 351 352
};

/*-------------------------------------------------------------------------*/


353
static const struct pci_device_id pci_ids [] = { {
354
	/* handle any USB OHCI controller */
355
	PCI_DEVICE_CLASS((PCI_CLASS_SERIAL_USB << 8) | 0x10, ~0),
356
	.driver_data =	(unsigned long) &ohci_pci_hc_driver,
357 358 359 360 361 362
	}, { /* end: all zeroes */ }
};
MODULE_DEVICE_TABLE (pci, pci_ids);

/* pci driver glue; this is a "new style" PCI driver module */
static struct pci_driver ohci_pci_driver = {
363 364
	.name =		(char *) hcd_name,
	.id_table =	pci_ids,
365

366 367
	.probe =	usb_hcd_pci_probe,
	.remove =	usb_hcd_pci_remove,
368 369

#ifdef	CONFIG_PM
370 371
	.suspend =	usb_hcd_pci_suspend,
	.resume =	usb_hcd_pci_resume,
372 373 374 375 376 377
#endif
};

 
static int __init ohci_hcd_pci_init (void) 
{
378
	printk (KERN_DEBUG "%s: " DRIVER_INFO " (PCI)\n", hcd_name);
379 380 381
	if (usb_disabled())
		return -ENODEV;

382
	printk (KERN_DEBUG "%s: block sizes: ed %Zd td %Zd\n", hcd_name,
383 384 385 386 387 388 389 390 391 392 393 394
		sizeof (struct ed), sizeof (struct td));
	return pci_module_init (&ohci_pci_driver);
}
module_init (ohci_hcd_pci_init);

/*-------------------------------------------------------------------------*/

static void __exit ohci_hcd_pci_cleanup (void) 
{	
	pci_unregister_driver (&ohci_pci_driver);
}
module_exit (ohci_hcd_pci_cleanup);