blob: 727544c205b900a754399537bcfefb5e2a54043f [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/drivers/ide/setup-pci.c Version 1.10 2002/08/19
3 *
4 * Copyright (c) 1998-2000 Andre Hedrick <andre@linux-ide.org>
5 *
6 * Copyright (c) 1995-1998 Mark Lord
7 * May be copied or modified under the terms of the GNU General Public License
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 */
9
10/*
11 * This module provides support for automatic detection and
12 * configuration of all PCI IDE interfaces present in a system.
13 */
14
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/module.h>
16#include <linux/types.h>
17#include <linux/kernel.h>
18#include <linux/pci.h>
19#include <linux/init.h>
20#include <linux/timer.h>
21#include <linux/mm.h>
22#include <linux/interrupt.h>
23#include <linux/ide.h>
24#include <linux/dma-mapping.h>
25
26#include <asm/io.h>
27#include <asm/irq.h>
28
29
30/**
31 * ide_match_hwif - match a PCI IDE against an ide_hwif
32 * @io_base: I/O base of device
33 * @bootable: set if its bootable
34 * @name: name of device
35 *
36 * Match a PCI IDE port against an entry in ide_hwifs[],
37 * based on io_base port if possible. Return the matching hwif,
38 * or a new hwif. If we find an error (clashing, out of devices, etc)
39 * return NULL
40 *
41 * FIXME: we need to handle mmio matches here too
42 */
43
44static ide_hwif_t *ide_match_hwif(unsigned long io_base, u8 bootable, const char *name)
45{
46 int h;
47 ide_hwif_t *hwif;
48
49 /*
50 * Look for a hwif with matching io_base specified using
51 * parameters to ide_setup().
52 */
53 for (h = 0; h < MAX_HWIFS; ++h) {
54 hwif = &ide_hwifs[h];
55 if (hwif->io_ports[IDE_DATA_OFFSET] == io_base) {
56 if (hwif->chipset == ide_forced)
57 return hwif; /* a perfect match */
58 }
59 }
60 /*
61 * Look for a hwif with matching io_base default value.
62 * If chipset is "ide_unknown", then claim that hwif slot.
63 * Otherwise, some other chipset has already claimed it.. :(
64 */
65 for (h = 0; h < MAX_HWIFS; ++h) {
66 hwif = &ide_hwifs[h];
67 if (hwif->io_ports[IDE_DATA_OFFSET] == io_base) {
68 if (hwif->chipset == ide_unknown)
69 return hwif; /* match */
70 printk(KERN_ERR "%s: port 0x%04lx already claimed by %s\n",
71 name, io_base, hwif->name);
72 return NULL; /* already claimed */
73 }
74 }
75 /*
76 * Okay, there is no hwif matching our io_base,
77 * so we'll just claim an unassigned slot.
78 * Give preference to claiming other slots before claiming ide0/ide1,
79 * just in case there's another interface yet-to-be-scanned
80 * which uses ports 1f0/170 (the ide0/ide1 defaults).
81 *
82 * Unless there is a bootable card that does not use the standard
83 * ports 1f0/170 (the ide0/ide1 defaults). The (bootable) flag.
84 */
85 if (bootable) {
86 for (h = 0; h < MAX_HWIFS; ++h) {
87 hwif = &ide_hwifs[h];
88 if (hwif->chipset == ide_unknown)
89 return hwif; /* pick an unused entry */
90 }
91 } else {
92 for (h = 2; h < MAX_HWIFS; ++h) {
93 hwif = ide_hwifs + h;
94 if (hwif->chipset == ide_unknown)
95 return hwif; /* pick an unused entry */
96 }
97 }
Matt Mackall83d7dbc2006-10-03 01:14:16 -070098 for (h = 0; h < 2 && h < MAX_HWIFS; ++h) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 hwif = ide_hwifs + h;
100 if (hwif->chipset == ide_unknown)
101 return hwif; /* pick an unused entry */
102 }
103 printk(KERN_ERR "%s: too many IDE interfaces, no room in table\n", name);
104 return NULL;
105}
106
107/**
108 * ide_setup_pci_baseregs - place a PCI IDE controller native
109 * @dev: PCI device of interface to switch native
110 * @name: Name of interface
111 *
112 * We attempt to place the PCI interface into PCI native mode. If
113 * we succeed the BARs are ok and the controller is in PCI mode.
114 * Returns 0 on success or an errno code.
115 *
116 * FIXME: if we program the interface and then fail to set the BARS
117 * we don't switch it back to legacy mode. Do we actually care ??
118 */
119
120static int ide_setup_pci_baseregs (struct pci_dev *dev, const char *name)
121{
122 u8 progif = 0;
123
124 /*
125 * Place both IDE interfaces into PCI "native" mode:
126 */
127 if (pci_read_config_byte(dev, PCI_CLASS_PROG, &progif) ||
128 (progif & 5) != 5) {
129 if ((progif & 0xa) != 0xa) {
130 printk(KERN_INFO "%s: device not capable of full "
131 "native PCI mode\n", name);
132 return -EOPNOTSUPP;
133 }
134 printk("%s: placing both ports into native PCI mode\n", name);
135 (void) pci_write_config_byte(dev, PCI_CLASS_PROG, progif|5);
136 if (pci_read_config_byte(dev, PCI_CLASS_PROG, &progif) ||
137 (progif & 5) != 5) {
138 printk(KERN_ERR "%s: rewrite of PROGIF failed, wanted "
139 "0x%04x, got 0x%04x\n",
140 name, progif|5, progif);
141 return -EOPNOTSUPP;
142 }
143 }
144 return 0;
145}
146
147#ifdef CONFIG_BLK_DEV_IDEDMA_PCI
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148/**
149 * ide_get_or_set_dma_base - setup BMIBA
Bartlomiej Zolnierkiewicz039788e2007-10-20 00:32:34 +0200150 * @d: IDE port info
151 * @hwif: IDE interface
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 *
Bartlomiej Zolnierkiewiczc58e79d2007-10-16 22:29:56 +0200153 * Fetch the DMA Bus-Master-I/O-Base-Address (BMIBA) from PCI space.
154 * Where a device has a partner that is already in DMA mode we check
155 * and enforce IDE simplex rules.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 */
157
Bartlomiej Zolnierkiewicz85620432007-10-20 00:32:34 +0200158static unsigned long ide_get_or_set_dma_base(const struct ide_port_info *d, ide_hwif_t *hwif)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159{
160 unsigned long dma_base = 0;
161 struct pci_dev *dev = hwif->pci_dev;
162
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 if (hwif->mmio)
164 return hwif->dma_base;
165
166 if (hwif->mate && hwif->mate->dma_base) {
167 dma_base = hwif->mate->dma_base - (hwif->channel ? 0 : 8);
168 } else {
Bartlomiej Zolnierkiewicz9ffcf362007-10-19 00:30:07 +0200169 u8 baridx = (d->host_flags & IDE_HFLAG_CS5520) ? 2 : 4;
170
171 dma_base = pci_resource_start(dev, baridx);
172
173 if (dma_base == 0)
174 printk(KERN_ERR "%s: DMA base is invalid\n", d->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 }
176
Bartlomiej Zolnierkiewicz9ffcf362007-10-19 00:30:07 +0200177 if ((d->host_flags & IDE_HFLAG_CS5520) == 0 && dma_base) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 u8 simplex_stat = 0;
179 dma_base += hwif->channel ? 8 : 0;
180
181 switch(dev->device) {
182 case PCI_DEVICE_ID_AL_M5219:
183 case PCI_DEVICE_ID_AL_M5229:
184 case PCI_DEVICE_ID_AMD_VIPER_7409:
185 case PCI_DEVICE_ID_CMD_643:
186 case PCI_DEVICE_ID_SERVERWORKS_CSB5IDE:
Matt Gillette2f09a7f2005-08-18 22:27:07 +0200187 case PCI_DEVICE_ID_REVOLUTION:
Bartlomiej Zolnierkiewicz31e8a462007-10-19 00:30:08 +0200188 simplex_stat = inb(dma_base + 2);
189 outb(simplex_stat & 0x60, dma_base + 2);
190 simplex_stat = inb(dma_base + 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 if (simplex_stat & 0x80) {
192 printk(KERN_INFO "%s: simplex device: "
Bartlomiej Zolnierkiewicz9ffcf362007-10-19 00:30:07 +0200193 "DMA forced\n",
194 d->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 }
196 break;
197 default:
198 /*
199 * If the device claims "simplex" DMA,
200 * this means only one of the two interfaces
201 * can be trusted with DMA at any point in time.
202 * So we should enable DMA only on one of the
203 * two interfaces.
204 */
205 simplex_stat = hwif->INB(dma_base + 2);
206 if (simplex_stat & 0x80) {
207 /* simplex device? */
208/*
209 * At this point we haven't probed the drives so we can't make the
210 * appropriate decision. Really we should defer this problem
211 * until we tune the drive then try to grab DMA ownership if we want
212 * to be the DMA end. This has to be become dynamic to handle hot
213 * plug.
214 */
215 if (hwif->mate && hwif->mate->dma_base) {
216 printk(KERN_INFO "%s: simplex device: "
Bartlomiej Zolnierkiewicz9ffcf362007-10-19 00:30:07 +0200217 "DMA disabled\n",
218 d->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 dma_base = 0;
220 }
221 }
222 }
223 }
224 return dma_base;
225}
226#endif /* CONFIG_BLK_DEV_IDEDMA_PCI */
227
Bartlomiej Zolnierkiewicz85620432007-10-20 00:32:34 +0200228void ide_setup_pci_noise(struct pci_dev *dev, const struct ide_port_info *d)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229{
230 printk(KERN_INFO "%s: IDE controller at PCI slot %s\n",
231 d->name, pci_name(dev));
232}
233
234EXPORT_SYMBOL_GPL(ide_setup_pci_noise);
235
236
237/**
238 * ide_pci_enable - do PCI enables
239 * @dev: PCI device
Bartlomiej Zolnierkiewicz039788e2007-10-20 00:32:34 +0200240 * @d: IDE port info
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 *
242 * Enable the IDE PCI device. We attempt to enable the device in full
243 * but if that fails then we only need BAR4 so we will enable that.
244 *
245 * Returns zero on success or an error code
246 */
Bartlomiej Zolnierkiewicz039788e2007-10-20 00:32:34 +0200247
Bartlomiej Zolnierkiewicz85620432007-10-20 00:32:34 +0200248static int ide_pci_enable(struct pci_dev *dev, const struct ide_port_info *d)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249{
250 int ret;
251
252 if (pci_enable_device(dev)) {
253 ret = pci_enable_device_bars(dev, 1 << 4);
254 if (ret < 0) {
255 printk(KERN_WARNING "%s: (ide_setup_pci_device:) "
256 "Could not enable device.\n", d->name);
257 goto out;
258 }
259 printk(KERN_WARNING "%s: BIOS configuration fixed.\n", d->name);
260 }
261
262 /*
Bartlomiej Zolnierkiewicz039788e2007-10-20 00:32:34 +0200263 * assume all devices can do 32-bit DMA for now, we can add
264 * a DMA mask field to the struct ide_port_info if we need it
265 * (or let lower level driver set the DMA mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 */
267 ret = pci_set_dma_mask(dev, DMA_32BIT_MASK);
268 if (ret < 0) {
269 printk(KERN_ERR "%s: can't set dma mask\n", d->name);
270 goto out;
271 }
272
273 /* FIXME: Temporary - until we put in the hotplug interface logic
274 Check that the bits we want are not in use by someone else. */
275 ret = pci_request_region(dev, 4, "ide_tmp");
276 if (ret < 0)
277 goto out;
278
279 pci_release_region(dev, 4);
280out:
281 return ret;
282}
283
284/**
285 * ide_pci_configure - configure an unconfigured device
286 * @dev: PCI device
Bartlomiej Zolnierkiewicz039788e2007-10-20 00:32:34 +0200287 * @d: IDE port info
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 *
289 * Enable and configure the PCI device we have been passed.
290 * Returns zero on success or an error code.
291 */
Bartlomiej Zolnierkiewicz039788e2007-10-20 00:32:34 +0200292
Bartlomiej Zolnierkiewicz85620432007-10-20 00:32:34 +0200293static int ide_pci_configure(struct pci_dev *dev, const struct ide_port_info *d)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294{
295 u16 pcicmd = 0;
296 /*
297 * PnP BIOS was *supposed* to have setup this device, but we
298 * can do it ourselves, so long as the BIOS has assigned an IRQ
299 * (or possibly the device is using a "legacy header" for IRQs).
300 * Maybe the user deliberately *disabled* the device,
301 * but we'll eventually ignore it again if no drives respond.
302 */
303 if (ide_setup_pci_baseregs(dev, d->name) || pci_write_config_word(dev, PCI_COMMAND, pcicmd|PCI_COMMAND_IO))
304 {
305 printk(KERN_INFO "%s: device disabled (BIOS)\n", d->name);
306 return -ENODEV;
307 }
308 if (pci_read_config_word(dev, PCI_COMMAND, &pcicmd)) {
309 printk(KERN_ERR "%s: error accessing PCI regs\n", d->name);
310 return -EIO;
311 }
312 if (!(pcicmd & PCI_COMMAND_IO)) {
313 printk(KERN_ERR "%s: unable to enable IDE controller\n", d->name);
314 return -ENXIO;
315 }
316 return 0;
317}
318
319/**
320 * ide_pci_check_iomem - check a register is I/O
Bartlomiej Zolnierkiewicz039788e2007-10-20 00:32:34 +0200321 * @dev: PCI device
322 * @d: IDE port info
323 * @bar: BAR number
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 *
325 * Checks if a BAR is configured and points to MMIO space. If so
326 * print an error and return an error code. Otherwise return 0
327 */
Bartlomiej Zolnierkiewicz039788e2007-10-20 00:32:34 +0200328
Bartlomiej Zolnierkiewicz85620432007-10-20 00:32:34 +0200329static int ide_pci_check_iomem(struct pci_dev *dev, const struct ide_port_info *d, int bar)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330{
331 ulong flags = pci_resource_flags(dev, bar);
332
333 /* Unconfigured ? */
334 if (!flags || pci_resource_len(dev, bar) == 0)
335 return 0;
336
337 /* I/O space */
338 if(flags & PCI_BASE_ADDRESS_IO_MASK)
339 return 0;
340
341 /* Bad */
342 printk(KERN_ERR "%s: IO baseregs (BIOS) are reported "
343 "as MEM, report to "
344 "<andre@linux-ide.org>.\n", d->name);
345 return -EINVAL;
346}
347
348/**
349 * ide_hwif_configure - configure an IDE interface
350 * @dev: PCI device holding interface
Bartlomiej Zolnierkiewicz039788e2007-10-20 00:32:34 +0200351 * @d: IDE port info
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 * @mate: Paired interface if any
353 *
354 * Perform the initial set up for the hardware interface structure. This
355 * is done per interface port rather than per PCI device. There may be
356 * more than one port per device.
357 *
358 * Returns the new hardware interface structure, or NULL on a failure
359 */
Bartlomiej Zolnierkiewicz039788e2007-10-20 00:32:34 +0200360
Bartlomiej Zolnierkiewicz85620432007-10-20 00:32:34 +0200361static ide_hwif_t *ide_hwif_configure(struct pci_dev *dev, const struct ide_port_info *d, ide_hwif_t *mate, int port, int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362{
363 unsigned long ctl = 0, base = 0;
364 ide_hwif_t *hwif;
Bartlomiej Zolnierkiewicz7cab14a2007-10-19 00:30:06 +0200365 u8 bootable = (d->host_flags & IDE_HFLAG_BOOTABLE) ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366
Bartlomiej Zolnierkiewicza5d8c5c2007-07-20 01:11:55 +0200367 if ((d->host_flags & IDE_HFLAG_ISA_PORTS) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 /* Possibly we should fail if these checks report true */
369 ide_pci_check_iomem(dev, d, 2*port);
370 ide_pci_check_iomem(dev, d, 2*port+1);
371
372 ctl = pci_resource_start(dev, 2*port+1);
373 base = pci_resource_start(dev, 2*port);
374 if ((ctl && !base) || (base && !ctl)) {
375 printk(KERN_ERR "%s: inconsistent baseregs (BIOS) "
376 "for port %d, skipping\n", d->name, port);
377 return NULL;
378 }
379 }
380 if (!ctl)
381 {
382 /* Use default values */
383 ctl = port ? 0x374 : 0x3f4;
384 base = port ? 0x170 : 0x1f0;
385 }
Bartlomiej Zolnierkiewicz7cab14a2007-10-19 00:30:06 +0200386 if ((hwif = ide_match_hwif(base, bootable, d->name)) == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 return NULL; /* no room in ide_hwifs[] */
388 if (hwif->io_ports[IDE_DATA_OFFSET] != base ||
389 hwif->io_ports[IDE_CONTROL_OFFSET] != (ctl | 2)) {
Bartlomiej Zolnierkiewicz9239b332007-10-20 00:32:33 +0200390 hw_regs_t hw;
391
392 memset(&hw, 0, sizeof(hw));
Bartlomiej Zolnierkiewicz847ddd22007-10-20 00:32:32 +0200393#ifndef CONFIG_IDE_ARCH_OBSOLETE_INIT
Bartlomiej Zolnierkiewicz9239b332007-10-20 00:32:33 +0200394 ide_std_init_ports(&hw, base, ctl | 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395#else
Bartlomiej Zolnierkiewicz9239b332007-10-20 00:32:33 +0200396 ide_init_hwif_ports(&hw, base, ctl | 2, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397#endif
Bartlomiej Zolnierkiewicz9239b332007-10-20 00:32:33 +0200398 memcpy(hwif->io_ports, hw.io_ports, sizeof(hwif->io_ports));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 hwif->noprobe = !hwif->io_ports[IDE_DATA_OFFSET];
400 }
Bartlomiej Zolnierkiewicz528a5722007-10-20 00:32:30 +0200401 hwif->chipset = d->chipset ? d->chipset : ide_pci;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 hwif->pci_dev = dev;
Bartlomiej Zolnierkiewicz039788e2007-10-20 00:32:34 +0200403 hwif->cds = d;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 hwif->channel = port;
405
406 if (!hwif->irq)
407 hwif->irq = irq;
408 if (mate) {
409 hwif->mate = mate;
410 mate->mate = hwif;
411 }
412 return hwif;
413}
414
415/**
416 * ide_hwif_setup_dma - configure DMA interface
417 * @dev: PCI device
Bartlomiej Zolnierkiewicz039788e2007-10-20 00:32:34 +0200418 * @d: IDE port info
419 * @hwif: IDE interface
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 *
421 * Set up the DMA base for the interface. Enable the master bits as
422 * necessary and attempt to bring the device DMA into a ready to use
423 * state
424 */
Bartlomiej Zolnierkiewicz039788e2007-10-20 00:32:34 +0200425
Bartlomiej Zolnierkiewicz85620432007-10-20 00:32:34 +0200426static void ide_hwif_setup_dma(struct pci_dev *dev, const struct ide_port_info *d, ide_hwif_t *hwif)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427{
Bartlomiej Zolnierkiewicz039788e2007-10-20 00:32:34 +0200428#ifdef CONFIG_BLK_DEV_IDEDMA_PCI
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 u16 pcicmd;
Bartlomiej Zolnierkiewicz47b68782007-10-19 00:30:06 +0200430
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 pci_read_config_word(dev, PCI_COMMAND, &pcicmd);
432
Bartlomiej Zolnierkiewicz47b68782007-10-19 00:30:06 +0200433 if ((d->host_flags & IDE_HFLAG_NO_AUTODMA) == 0 ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 ((dev->class >> 8) == PCI_CLASS_STORAGE_IDE &&
435 (dev->class & 0x80))) {
Bartlomiej Zolnierkiewicz9ffcf362007-10-19 00:30:07 +0200436 unsigned long dma_base = ide_get_or_set_dma_base(d, hwif);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 if (dma_base && !(pcicmd & PCI_COMMAND_MASTER)) {
438 /*
439 * Set up BM-DMA capability
440 * (PnP BIOS should have done this)
441 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 pci_set_master(dev);
443 if (pci_read_config_word(dev, PCI_COMMAND, &pcicmd) || !(pcicmd & PCI_COMMAND_MASTER)) {
444 printk(KERN_ERR "%s: %s error updating PCICMD\n",
445 hwif->name, d->name);
446 dma_base = 0;
447 }
448 }
449 if (dma_base) {
450 if (d->init_dma) {
451 d->init_dma(hwif, dma_base);
452 } else {
453 ide_setup_dma(hwif, dma_base, 8);
454 }
455 } else {
456 printk(KERN_INFO "%s: %s Bus-Master DMA disabled "
457 "(BIOS)\n", hwif->name, d->name);
458 }
459 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460#endif /* CONFIG_BLK_DEV_IDEDMA_PCI*/
Bartlomiej Zolnierkiewicz039788e2007-10-20 00:32:34 +0200461}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462
463/**
464 * ide_setup_pci_controller - set up IDE PCI
465 * @dev: PCI device
Bartlomiej Zolnierkiewicz039788e2007-10-20 00:32:34 +0200466 * @d: IDE port info
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 * @noisy: verbose flag
468 * @config: returned as 1 if we configured the hardware
469 *
470 * Set up the PCI and controller side of the IDE interface. This brings
471 * up the PCI side of the device, checks that the device is enabled
472 * and enables it if need be
473 */
Bartlomiej Zolnierkiewicz039788e2007-10-20 00:32:34 +0200474
Bartlomiej Zolnierkiewicz85620432007-10-20 00:32:34 +0200475static int ide_setup_pci_controller(struct pci_dev *dev, const struct ide_port_info *d, int noisy, int *config)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476{
477 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 u16 pcicmd;
479
480 if (noisy)
481 ide_setup_pci_noise(dev, d);
482
483 ret = ide_pci_enable(dev, d);
484 if (ret < 0)
485 goto out;
486
487 ret = pci_read_config_word(dev, PCI_COMMAND, &pcicmd);
488 if (ret < 0) {
489 printk(KERN_ERR "%s: error accessing PCI regs\n", d->name);
490 goto out;
491 }
492 if (!(pcicmd & PCI_COMMAND_IO)) { /* is device disabled? */
493 ret = ide_pci_configure(dev, d);
494 if (ret < 0)
495 goto out;
496 *config = 1;
497 printk(KERN_INFO "%s: device enabled (Linux)\n", d->name);
498 }
499
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 if (noisy)
Bartlomiej Zolnierkiewiczfc212bb2007-10-19 00:30:08 +0200501 printk(KERN_INFO "%s: chipset revision %d\n",
502 d->name, dev->revision);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503out:
504 return ret;
505}
506
507/**
508 * ide_pci_setup_ports - configure ports/devices on PCI IDE
509 * @dev: PCI device
Bartlomiej Zolnierkiewicz039788e2007-10-20 00:32:34 +0200510 * @d: IDE port info
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 * @pciirq: IRQ line
Bartlomiej Zolnierkiewicz8447d9d2007-10-20 00:32:31 +0200512 * @idx: ATA index table to update
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 *
514 * Scan the interfaces attached to this device and do any
515 * necessary per port setup. Attach the devices and ask the
516 * generic DMA layer to do its work for us.
517 *
518 * Normally called automaticall from do_ide_pci_setup_device,
519 * but is also used directly as a helper function by some controllers
520 * where the chipset setup is not the default PCI IDE one.
521 */
Bartlomiej Zolnierkiewicz8447d9d2007-10-20 00:32:31 +0200522
Bartlomiej Zolnierkiewicz85620432007-10-20 00:32:34 +0200523void ide_pci_setup_ports(struct pci_dev *dev, const struct ide_port_info *d, int pciirq, u8 *idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524{
Bartlomiej Zolnierkiewicza5d8c5c2007-07-20 01:11:55 +0200525 int channels = (d->host_flags & IDE_HFLAG_SINGLE) ? 1 : 2, port;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 ide_hwif_t *hwif, *mate = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 u8 tmp;
528
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 /*
530 * Set up the IDE ports
531 */
Bartlomiej Zolnierkiewiczcf6e8542007-10-20 00:32:29 +0200532
Bartlomiej Zolnierkiewicza5d8c5c2007-07-20 01:11:55 +0200533 for (port = 0; port < channels; ++port) {
Bartlomiej Zolnierkiewicz85620432007-10-20 00:32:34 +0200534 const ide_pci_enablebit_t *e = &(d->enablebits[port]);
535
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 if (e->reg && (pci_read_config_byte(dev, e->reg, &tmp) ||
Bartlomiej Zolnierkiewiczcf6e8542007-10-20 00:32:29 +0200537 (tmp & e->mask) != e->val)) {
538 printk(KERN_INFO "%s: IDE port disabled\n", d->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 continue; /* port not enabled */
Bartlomiej Zolnierkiewiczcf6e8542007-10-20 00:32:29 +0200540 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 if ((hwif = ide_hwif_configure(dev, d, mate, port, pciirq)) == NULL)
543 continue;
544
545 /* setup proper ancestral information */
546 hwif->gendev.parent = &dev->dev;
547
Bartlomiej Zolnierkiewicz8447d9d2007-10-20 00:32:31 +0200548 *(idx + port) = hwif->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549
550
551 if (d->init_iops)
552 d->init_iops(hwif);
553
Bartlomiej Zolnierkiewicz9ffcf362007-10-19 00:30:07 +0200554 if ((d->host_flags & IDE_HFLAG_NO_DMA) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 ide_hwif_setup_dma(dev, d, hwif);
Bartlomiej Zolnierkiewicz9ffcf362007-10-19 00:30:07 +0200556
Bartlomiej Zolnierkiewicz8acf28c2007-10-20 00:32:30 +0200557 if ((!hwif->irq && (d->host_flags & IDE_HFLAG_LEGACY_IRQS)) ||
558 (d->host_flags & IDE_HFLAG_FORCE_LEGACY_IRQS))
Bartlomiej Zolnierkiewicz3985ee32007-10-19 00:30:11 +0200559 hwif->irq = port ? 15 : 14;
560
Bartlomiej Zolnierkiewiczfd9bb532007-10-20 00:32:31 +0200561 hwif->fixup = d->fixup;
562
Bartlomiej Zolnierkiewicz6a824c92007-07-20 01:11:58 +0200563 hwif->host_flags = d->host_flags;
Bartlomiej Zolnierkiewicz4099d142007-07-20 01:11:59 +0200564 hwif->pio_mask = d->pio_mask;
Bartlomiej Zolnierkiewicz6a824c92007-07-20 01:11:58 +0200565
Bartlomiej Zolnierkiewicz1c513612007-10-19 00:30:10 +0200566 if ((d->host_flags & IDE_HFLAG_SERIALIZE) && hwif->mate)
567 hwif->mate->serialized = hwif->serialized = 1;
568
Bartlomiej Zolnierkiewiczcaea7602007-10-20 00:32:30 +0200569 if (d->host_flags & IDE_HFLAG_IO_32BIT) {
570 hwif->drives[0].io_32bit = 1;
571 hwif->drives[1].io_32bit = 1;
572 }
573
574 if (d->host_flags & IDE_HFLAG_UNMASK_IRQS) {
575 hwif->drives[0].unmask = 1;
576 hwif->drives[1].unmask = 1;
577 }
578
Bartlomiej Zolnierkiewicz5f8b6c32007-10-19 00:30:07 +0200579 if (hwif->dma_base) {
580 hwif->swdma_mask = d->swdma_mask;
581 hwif->mwdma_mask = d->mwdma_mask;
582 hwif->ultra_mask = d->udma_mask;
583 }
584
Bartlomiej Zolnierkiewicz85ad93a2007-10-19 00:30:12 +0200585 hwif->drives[0].autotune = 1;
586 hwif->drives[1].autotune = 1;
587
Bartlomiej Zolnierkiewicz272a3702007-10-20 00:32:30 +0200588 if (d->host_flags & IDE_HFLAG_RQSIZE_256)
589 hwif->rqsize = 256;
590
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 if (d->init_hwif)
592 /* Call chipset-specific routine
593 * for each enabled hwif
594 */
595 d->init_hwif(hwif);
596
597 mate = hwif;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599}
600
601EXPORT_SYMBOL_GPL(ide_pci_setup_ports);
602
603/*
604 * ide_setup_pci_device() looks at the primary/secondary interfaces
605 * on a PCI IDE device and, if they are enabled, prepares the IDE driver
606 * for use with them. This generic code works for most PCI chipsets.
607 *
608 * One thing that is not standardized is the location of the
609 * primary/secondary interface "enable/disable" bits. For chipsets that
Bartlomiej Zolnierkiewicz039788e2007-10-20 00:32:34 +0200610 * we "know" about, this information is in the struct ide_port_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 * for all other chipsets, we just assume both interfaces are enabled.
612 */
Bartlomiej Zolnierkiewicz039788e2007-10-20 00:32:34 +0200613static int do_ide_setup_pci_device(struct pci_dev *dev,
Bartlomiej Zolnierkiewicz85620432007-10-20 00:32:34 +0200614 const struct ide_port_info *d,
Bartlomiej Zolnierkiewicz8447d9d2007-10-20 00:32:31 +0200615 u8 *idx, u8 noisy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 int tried_config = 0;
618 int pciirq, ret;
619
620 ret = ide_setup_pci_controller(dev, d, noisy, &tried_config);
621 if (ret < 0)
622 goto out;
623
624 /*
625 * Can we trust the reported IRQ?
626 */
627 pciirq = dev->irq;
628
629 /* Is it an "IDE storage" device in non-PCI mode? */
630 if ((dev->class >> 8) == PCI_CLASS_STORAGE_IDE && (dev->class & 5) != 5) {
631 if (noisy)
632 printk(KERN_INFO "%s: not 100%% native mode: "
633 "will probe irqs later\n", d->name);
634 /*
635 * This allows offboard ide-pci cards the enable a BIOS,
636 * verify interrupt settings of split-mirror pci-config
637 * space, place chipset into init-mode, and/or preserve
638 * an interrupt if the card is not native ide support.
639 */
640 ret = d->init_chipset ? d->init_chipset(dev, d->name) : 0;
641 if (ret < 0)
642 goto out;
643 pciirq = ret;
644 } else if (tried_config) {
645 if (noisy)
646 printk(KERN_INFO "%s: will probe irqs later\n", d->name);
647 pciirq = 0;
648 } else if (!pciirq) {
649 if (noisy)
650 printk(KERN_WARNING "%s: bad irq (%d): will probe later\n",
651 d->name, pciirq);
652 pciirq = 0;
653 } else {
654 if (d->init_chipset) {
655 ret = d->init_chipset(dev, d->name);
656 if (ret < 0)
657 goto out;
658 }
659 if (noisy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 printk(KERN_INFO "%s: 100%% native mode on irq %d\n",
661 d->name, pciirq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 }
663
664 /* FIXME: silent failure can happen */
665
Bartlomiej Zolnierkiewicz8447d9d2007-10-20 00:32:31 +0200666 ide_pci_setup_ports(dev, d, pciirq, idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667out:
668 return ret;
669}
670
Bartlomiej Zolnierkiewicz85620432007-10-20 00:32:34 +0200671int ide_setup_pci_device(struct pci_dev *dev, const struct ide_port_info *d)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672{
Bartlomiej Zolnierkiewicz8447d9d2007-10-20 00:32:31 +0200673 u8 idx[4] = { 0xff, 0xff, 0xff, 0xff };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 int ret;
675
Bartlomiej Zolnierkiewicz8447d9d2007-10-20 00:32:31 +0200676 ret = do_ide_setup_pci_device(dev, d, &idx[0], 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677
Bartlomiej Zolnierkiewicz8447d9d2007-10-20 00:32:31 +0200678 if (ret >= 0)
679 ide_device_add(idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 return ret;
682}
683
684EXPORT_SYMBOL_GPL(ide_setup_pci_device);
685
686int ide_setup_pci_devices(struct pci_dev *dev1, struct pci_dev *dev2,
Bartlomiej Zolnierkiewicz85620432007-10-20 00:32:34 +0200687 const struct ide_port_info *d)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688{
689 struct pci_dev *pdev[] = { dev1, dev2 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 int ret, i;
Bartlomiej Zolnierkiewicz8447d9d2007-10-20 00:32:31 +0200691 u8 idx[4] = { 0xff, 0xff, 0xff, 0xff };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692
693 for (i = 0; i < 2; i++) {
Bartlomiej Zolnierkiewicz8447d9d2007-10-20 00:32:31 +0200694 ret = do_ide_setup_pci_device(pdev[i], d, &idx[i*2], !i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 /*
696 * FIXME: Mom, mom, they stole me the helper function to undo
697 * do_ide_setup_pci_device() on the first device!
698 */
699 if (ret < 0)
700 goto out;
701 }
702
Bartlomiej Zolnierkiewicz8447d9d2007-10-20 00:32:31 +0200703 ide_device_add(idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704out:
705 return ret;
706}
707
708EXPORT_SYMBOL_GPL(ide_setup_pci_devices);
709
Bartlomiej Zolnierkiewicz6d208b32007-05-10 00:01:11 +0200710#ifdef CONFIG_IDEPCI_PCIBUS_ORDER
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711/*
712 * Module interfaces
713 */
714
715static int pre_init = 1; /* Before first ordered IDE scan */
716static LIST_HEAD(ide_pci_drivers);
717
718/*
Ralf Baechlec37ea212005-11-18 23:11:24 +0100719 * __ide_pci_register_driver - attach IDE driver
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 * @driver: pci driver
Laurent riffard863b18f2005-10-27 23:12:54 +0200721 * @module: owner module of the driver
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 *
723 * Registers a driver with the IDE layer. The IDE layer arranges that
724 * boot time setup is done in the expected device order and then
725 * hands the controllers off to the core PCI code to do the rest of
726 * the work.
727 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 * Returns are the same as for pci_register_driver
729 */
730
Greg Kroah-Hartman725522b2007-01-15 11:50:02 -0800731int __ide_pci_register_driver(struct pci_driver *driver, struct module *module,
732 const char *mod_name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733{
734 if(!pre_init)
Greg Kroah-Hartman725522b2007-01-15 11:50:02 -0800735 return __pci_register_driver(driver, module, mod_name);
Laurent riffard863b18f2005-10-27 23:12:54 +0200736 driver->driver.owner = module;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 list_add_tail(&driver->node, &ide_pci_drivers);
738 return 0;
739}
740
Laurent riffard863b18f2005-10-27 23:12:54 +0200741EXPORT_SYMBOL_GPL(__ide_pci_register_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742
743/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 * ide_scan_pcidev - find an IDE driver for a device
745 * @dev: PCI device to check
746 *
747 * Look for an IDE driver to handle the device we are considering.
748 * This is only used during boot up to get the ordering correct. After
749 * boot up the pci layer takes over the job.
750 */
751
752static int __init ide_scan_pcidev(struct pci_dev *dev)
753{
754 struct list_head *l;
755 struct pci_driver *d;
756
Sergei Shtylyov0505b552007-09-11 22:28:34 +0200757 list_for_each(l, &ide_pci_drivers) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 d = list_entry(l, struct pci_driver, node);
Sergei Shtylyov0505b552007-09-11 22:28:34 +0200759 if (d->id_table) {
760 const struct pci_device_id *id = pci_match_id(d->id_table,
761 dev);
762 if (id != NULL && d->probe(dev, id) >= 0) {
763 dev->driver = d;
764 pci_dev_get(dev);
765 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 }
767 }
768 }
769 return 0;
770}
771
772/**
773 * ide_scan_pcibus - perform the initial IDE driver scan
774 * @scan_direction: set for reverse order scanning
775 *
776 * Perform the initial bus rather than driver ordered scan of the
777 * PCI drivers. After this all IDE pci handling becomes standard
778 * module ordering not traditionally ordered.
779 */
780
781void __init ide_scan_pcibus (int scan_direction)
782{
783 struct pci_dev *dev = NULL;
784 struct pci_driver *d;
785 struct list_head *l, *n;
786
787 pre_init = 0;
Sergei Shtylyov0505b552007-09-11 22:28:34 +0200788 if (!scan_direction)
789 while ((dev = pci_get_device(PCI_ANY_ID, PCI_ANY_ID, dev)) != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 ide_scan_pcidev(dev);
Sergei Shtylyov0505b552007-09-11 22:28:34 +0200791 else
792 while ((dev = pci_get_device_reverse(PCI_ANY_ID, PCI_ANY_ID, dev))
793 != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 ide_scan_pcidev(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795
796 /*
797 * Hand the drivers over to the PCI layer now we
798 * are post init.
799 */
800
Andrew Mortond61bcce2007-07-03 22:28:36 +0200801 list_for_each_safe(l, n, &ide_pci_drivers) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 list_del(l);
803 d = list_entry(l, struct pci_driver, node);
Sergei Shtylyov0505b552007-09-11 22:28:34 +0200804 if (__pci_register_driver(d, d->driver.owner, d->driver.mod_name))
805 printk(KERN_ERR "%s: failed to register driver for %s\n",
806 __FUNCTION__, d->driver.mod_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 }
808}
Bartlomiej Zolnierkiewicz6d208b32007-05-10 00:01:11 +0200809#endif