blob: d2cd5a3d38f82e6a18b12024cf7ad14777eb5612 [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
Linus Torvalds1da177e2005-04-16 15:20:36 -070010#include <linux/module.h>
11#include <linux/types.h>
12#include <linux/kernel.h>
13#include <linux/pci.h>
14#include <linux/init.h>
15#include <linux/timer.h>
16#include <linux/mm.h>
17#include <linux/interrupt.h>
18#include <linux/ide.h>
19#include <linux/dma-mapping.h>
20
21#include <asm/io.h>
22#include <asm/irq.h>
23
24
25/**
26 * ide_match_hwif - match a PCI IDE against an ide_hwif
27 * @io_base: I/O base of device
28 * @bootable: set if its bootable
29 * @name: name of device
30 *
31 * Match a PCI IDE port against an entry in ide_hwifs[],
32 * based on io_base port if possible. Return the matching hwif,
33 * or a new hwif. If we find an error (clashing, out of devices, etc)
34 * return NULL
35 *
36 * FIXME: we need to handle mmio matches here too
37 */
38
39static ide_hwif_t *ide_match_hwif(unsigned long io_base, u8 bootable, const char *name)
40{
41 int h;
42 ide_hwif_t *hwif;
43
44 /*
45 * Look for a hwif with matching io_base specified using
46 * parameters to ide_setup().
47 */
48 for (h = 0; h < MAX_HWIFS; ++h) {
49 hwif = &ide_hwifs[h];
50 if (hwif->io_ports[IDE_DATA_OFFSET] == io_base) {
51 if (hwif->chipset == ide_forced)
52 return hwif; /* a perfect match */
53 }
54 }
55 /*
56 * Look for a hwif with matching io_base default value.
57 * If chipset is "ide_unknown", then claim that hwif slot.
58 * Otherwise, some other chipset has already claimed it.. :(
59 */
60 for (h = 0; h < MAX_HWIFS; ++h) {
61 hwif = &ide_hwifs[h];
62 if (hwif->io_ports[IDE_DATA_OFFSET] == io_base) {
63 if (hwif->chipset == ide_unknown)
64 return hwif; /* match */
65 printk(KERN_ERR "%s: port 0x%04lx already claimed by %s\n",
66 name, io_base, hwif->name);
67 return NULL; /* already claimed */
68 }
69 }
70 /*
71 * Okay, there is no hwif matching our io_base,
72 * so we'll just claim an unassigned slot.
73 * Give preference to claiming other slots before claiming ide0/ide1,
74 * just in case there's another interface yet-to-be-scanned
75 * which uses ports 1f0/170 (the ide0/ide1 defaults).
76 *
77 * Unless there is a bootable card that does not use the standard
78 * ports 1f0/170 (the ide0/ide1 defaults). The (bootable) flag.
79 */
80 if (bootable) {
81 for (h = 0; h < MAX_HWIFS; ++h) {
82 hwif = &ide_hwifs[h];
83 if (hwif->chipset == ide_unknown)
84 return hwif; /* pick an unused entry */
85 }
86 } else {
87 for (h = 2; h < MAX_HWIFS; ++h) {
88 hwif = ide_hwifs + h;
89 if (hwif->chipset == ide_unknown)
90 return hwif; /* pick an unused entry */
91 }
92 }
Matt Mackall83d7dbc2006-10-03 01:14:16 -070093 for (h = 0; h < 2 && h < MAX_HWIFS; ++h) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 hwif = ide_hwifs + h;
95 if (hwif->chipset == ide_unknown)
96 return hwif; /* pick an unused entry */
97 }
98 printk(KERN_ERR "%s: too many IDE interfaces, no room in table\n", name);
99 return NULL;
100}
101
102/**
103 * ide_setup_pci_baseregs - place a PCI IDE controller native
104 * @dev: PCI device of interface to switch native
105 * @name: Name of interface
106 *
107 * We attempt to place the PCI interface into PCI native mode. If
108 * we succeed the BARs are ok and the controller is in PCI mode.
109 * Returns 0 on success or an errno code.
110 *
111 * FIXME: if we program the interface and then fail to set the BARS
112 * we don't switch it back to legacy mode. Do we actually care ??
113 */
114
115static int ide_setup_pci_baseregs (struct pci_dev *dev, const char *name)
116{
117 u8 progif = 0;
118
119 /*
120 * Place both IDE interfaces into PCI "native" mode:
121 */
122 if (pci_read_config_byte(dev, PCI_CLASS_PROG, &progif) ||
123 (progif & 5) != 5) {
124 if ((progif & 0xa) != 0xa) {
125 printk(KERN_INFO "%s: device not capable of full "
126 "native PCI mode\n", name);
127 return -EOPNOTSUPP;
128 }
129 printk("%s: placing both ports into native PCI mode\n", name);
130 (void) pci_write_config_byte(dev, PCI_CLASS_PROG, progif|5);
131 if (pci_read_config_byte(dev, PCI_CLASS_PROG, &progif) ||
132 (progif & 5) != 5) {
133 printk(KERN_ERR "%s: rewrite of PROGIF failed, wanted "
134 "0x%04x, got 0x%04x\n",
135 name, progif|5, progif);
136 return -EOPNOTSUPP;
137 }
138 }
139 return 0;
140}
141
142#ifdef CONFIG_BLK_DEV_IDEDMA_PCI
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143/**
144 * ide_get_or_set_dma_base - setup BMIBA
Bartlomiej Zolnierkiewicz039788e2007-10-20 00:32:34 +0200145 * @d: IDE port info
146 * @hwif: IDE interface
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 *
Bartlomiej Zolnierkiewiczc58e79d2007-10-16 22:29:56 +0200148 * Fetch the DMA Bus-Master-I/O-Base-Address (BMIBA) from PCI space.
149 * Where a device has a partner that is already in DMA mode we check
150 * and enforce IDE simplex rules.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 */
152
Bartlomiej Zolnierkiewicz85620432007-10-20 00:32:34 +0200153static 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 -0700154{
155 unsigned long dma_base = 0;
156 struct pci_dev *dev = hwif->pci_dev;
157
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 if (hwif->mmio)
159 return hwif->dma_base;
160
161 if (hwif->mate && hwif->mate->dma_base) {
162 dma_base = hwif->mate->dma_base - (hwif->channel ? 0 : 8);
163 } else {
Bartlomiej Zolnierkiewicz9ffcf362007-10-19 00:30:07 +0200164 u8 baridx = (d->host_flags & IDE_HFLAG_CS5520) ? 2 : 4;
165
166 dma_base = pci_resource_start(dev, baridx);
167
168 if (dma_base == 0)
169 printk(KERN_ERR "%s: DMA base is invalid\n", d->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 }
171
Bartlomiej Zolnierkiewicz9ffcf362007-10-19 00:30:07 +0200172 if ((d->host_flags & IDE_HFLAG_CS5520) == 0 && dma_base) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 u8 simplex_stat = 0;
174 dma_base += hwif->channel ? 8 : 0;
175
176 switch(dev->device) {
177 case PCI_DEVICE_ID_AL_M5219:
178 case PCI_DEVICE_ID_AL_M5229:
179 case PCI_DEVICE_ID_AMD_VIPER_7409:
180 case PCI_DEVICE_ID_CMD_643:
181 case PCI_DEVICE_ID_SERVERWORKS_CSB5IDE:
Matt Gillette2f09a7f2005-08-18 22:27:07 +0200182 case PCI_DEVICE_ID_REVOLUTION:
Bartlomiej Zolnierkiewicz31e8a462007-10-19 00:30:08 +0200183 simplex_stat = inb(dma_base + 2);
184 outb(simplex_stat & 0x60, dma_base + 2);
185 simplex_stat = inb(dma_base + 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 if (simplex_stat & 0x80) {
187 printk(KERN_INFO "%s: simplex device: "
Bartlomiej Zolnierkiewicz9ffcf362007-10-19 00:30:07 +0200188 "DMA forced\n",
189 d->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 }
191 break;
192 default:
193 /*
194 * If the device claims "simplex" DMA,
195 * this means only one of the two interfaces
196 * can be trusted with DMA at any point in time.
197 * So we should enable DMA only on one of the
198 * two interfaces.
199 */
200 simplex_stat = hwif->INB(dma_base + 2);
201 if (simplex_stat & 0x80) {
202 /* simplex device? */
203/*
204 * At this point we haven't probed the drives so we can't make the
205 * appropriate decision. Really we should defer this problem
206 * until we tune the drive then try to grab DMA ownership if we want
207 * to be the DMA end. This has to be become dynamic to handle hot
208 * plug.
209 */
210 if (hwif->mate && hwif->mate->dma_base) {
211 printk(KERN_INFO "%s: simplex device: "
Bartlomiej Zolnierkiewicz9ffcf362007-10-19 00:30:07 +0200212 "DMA disabled\n",
213 d->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 dma_base = 0;
215 }
216 }
217 }
218 }
219 return dma_base;
220}
221#endif /* CONFIG_BLK_DEV_IDEDMA_PCI */
222
Bartlomiej Zolnierkiewicz85620432007-10-20 00:32:34 +0200223void ide_setup_pci_noise(struct pci_dev *dev, const struct ide_port_info *d)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224{
Bartlomiej Zolnierkiewiczbde07e52007-10-20 00:32:36 +0200225 printk(KERN_INFO "%s: IDE controller (0x%04x:0x%04x rev 0x%02x) at "
226 " PCI slot %s\n", d->name, dev->vendor, dev->device,
227 dev->revision, pci_name(dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228}
229
230EXPORT_SYMBOL_GPL(ide_setup_pci_noise);
231
232
233/**
234 * ide_pci_enable - do PCI enables
235 * @dev: PCI device
Bartlomiej Zolnierkiewicz039788e2007-10-20 00:32:34 +0200236 * @d: IDE port info
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 *
238 * Enable the IDE PCI device. We attempt to enable the device in full
239 * but if that fails then we only need BAR4 so we will enable that.
240 *
241 * Returns zero on success or an error code
242 */
Bartlomiej Zolnierkiewicz039788e2007-10-20 00:32:34 +0200243
Bartlomiej Zolnierkiewicz85620432007-10-20 00:32:34 +0200244static int ide_pci_enable(struct pci_dev *dev, const struct ide_port_info *d)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245{
246 int ret;
247
248 if (pci_enable_device(dev)) {
249 ret = pci_enable_device_bars(dev, 1 << 4);
250 if (ret < 0) {
251 printk(KERN_WARNING "%s: (ide_setup_pci_device:) "
252 "Could not enable device.\n", d->name);
253 goto out;
254 }
255 printk(KERN_WARNING "%s: BIOS configuration fixed.\n", d->name);
256 }
257
258 /*
Bartlomiej Zolnierkiewicz039788e2007-10-20 00:32:34 +0200259 * assume all devices can do 32-bit DMA for now, we can add
260 * a DMA mask field to the struct ide_port_info if we need it
261 * (or let lower level driver set the DMA mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 */
263 ret = pci_set_dma_mask(dev, DMA_32BIT_MASK);
264 if (ret < 0) {
265 printk(KERN_ERR "%s: can't set dma mask\n", d->name);
266 goto out;
267 }
268
269 /* FIXME: Temporary - until we put in the hotplug interface logic
270 Check that the bits we want are not in use by someone else. */
271 ret = pci_request_region(dev, 4, "ide_tmp");
272 if (ret < 0)
273 goto out;
274
275 pci_release_region(dev, 4);
276out:
277 return ret;
278}
279
280/**
281 * ide_pci_configure - configure an unconfigured device
282 * @dev: PCI device
Bartlomiej Zolnierkiewicz039788e2007-10-20 00:32:34 +0200283 * @d: IDE port info
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 *
285 * Enable and configure the PCI device we have been passed.
286 * Returns zero on success or an error code.
287 */
Bartlomiej Zolnierkiewicz039788e2007-10-20 00:32:34 +0200288
Bartlomiej Zolnierkiewicz85620432007-10-20 00:32:34 +0200289static int ide_pci_configure(struct pci_dev *dev, const struct ide_port_info *d)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290{
291 u16 pcicmd = 0;
292 /*
293 * PnP BIOS was *supposed* to have setup this device, but we
294 * can do it ourselves, so long as the BIOS has assigned an IRQ
295 * (or possibly the device is using a "legacy header" for IRQs).
296 * Maybe the user deliberately *disabled* the device,
297 * but we'll eventually ignore it again if no drives respond.
298 */
299 if (ide_setup_pci_baseregs(dev, d->name) || pci_write_config_word(dev, PCI_COMMAND, pcicmd|PCI_COMMAND_IO))
300 {
301 printk(KERN_INFO "%s: device disabled (BIOS)\n", d->name);
302 return -ENODEV;
303 }
304 if (pci_read_config_word(dev, PCI_COMMAND, &pcicmd)) {
305 printk(KERN_ERR "%s: error accessing PCI regs\n", d->name);
306 return -EIO;
307 }
308 if (!(pcicmd & PCI_COMMAND_IO)) {
309 printk(KERN_ERR "%s: unable to enable IDE controller\n", d->name);
310 return -ENXIO;
311 }
312 return 0;
313}
314
315/**
316 * ide_pci_check_iomem - check a register is I/O
Bartlomiej Zolnierkiewicz039788e2007-10-20 00:32:34 +0200317 * @dev: PCI device
318 * @d: IDE port info
319 * @bar: BAR number
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 *
321 * Checks if a BAR is configured and points to MMIO space. If so
322 * print an error and return an error code. Otherwise return 0
323 */
Bartlomiej Zolnierkiewicz039788e2007-10-20 00:32:34 +0200324
Bartlomiej Zolnierkiewicz85620432007-10-20 00:32:34 +0200325static int ide_pci_check_iomem(struct pci_dev *dev, const struct ide_port_info *d, int bar)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326{
327 ulong flags = pci_resource_flags(dev, bar);
328
329 /* Unconfigured ? */
330 if (!flags || pci_resource_len(dev, bar) == 0)
331 return 0;
332
333 /* I/O space */
334 if(flags & PCI_BASE_ADDRESS_IO_MASK)
335 return 0;
336
337 /* Bad */
338 printk(KERN_ERR "%s: IO baseregs (BIOS) are reported "
339 "as MEM, report to "
340 "<andre@linux-ide.org>.\n", d->name);
341 return -EINVAL;
342}
343
344/**
345 * ide_hwif_configure - configure an IDE interface
346 * @dev: PCI device holding interface
Bartlomiej Zolnierkiewicz039788e2007-10-20 00:32:34 +0200347 * @d: IDE port info
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 * @mate: Paired interface if any
349 *
350 * Perform the initial set up for the hardware interface structure. This
351 * is done per interface port rather than per PCI device. There may be
352 * more than one port per device.
353 *
354 * Returns the new hardware interface structure, or NULL on a failure
355 */
Bartlomiej Zolnierkiewicz039788e2007-10-20 00:32:34 +0200356
Bartlomiej Zolnierkiewicz85620432007-10-20 00:32:34 +0200357static 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 -0700358{
359 unsigned long ctl = 0, base = 0;
360 ide_hwif_t *hwif;
Bartlomiej Zolnierkiewicz7cab14a2007-10-19 00:30:06 +0200361 u8 bootable = (d->host_flags & IDE_HFLAG_BOOTABLE) ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362
Bartlomiej Zolnierkiewicza5d8c5c2007-07-20 01:11:55 +0200363 if ((d->host_flags & IDE_HFLAG_ISA_PORTS) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 /* Possibly we should fail if these checks report true */
365 ide_pci_check_iomem(dev, d, 2*port);
366 ide_pci_check_iomem(dev, d, 2*port+1);
367
368 ctl = pci_resource_start(dev, 2*port+1);
369 base = pci_resource_start(dev, 2*port);
370 if ((ctl && !base) || (base && !ctl)) {
371 printk(KERN_ERR "%s: inconsistent baseregs (BIOS) "
372 "for port %d, skipping\n", d->name, port);
373 return NULL;
374 }
375 }
376 if (!ctl)
377 {
378 /* Use default values */
379 ctl = port ? 0x374 : 0x3f4;
380 base = port ? 0x170 : 0x1f0;
381 }
Bartlomiej Zolnierkiewicz7cab14a2007-10-19 00:30:06 +0200382 if ((hwif = ide_match_hwif(base, bootable, d->name)) == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 return NULL; /* no room in ide_hwifs[] */
384 if (hwif->io_ports[IDE_DATA_OFFSET] != base ||
385 hwif->io_ports[IDE_CONTROL_OFFSET] != (ctl | 2)) {
Bartlomiej Zolnierkiewicz9239b332007-10-20 00:32:33 +0200386 hw_regs_t hw;
387
388 memset(&hw, 0, sizeof(hw));
Bartlomiej Zolnierkiewicz847ddd22007-10-20 00:32:32 +0200389#ifndef CONFIG_IDE_ARCH_OBSOLETE_INIT
Bartlomiej Zolnierkiewicz9239b332007-10-20 00:32:33 +0200390 ide_std_init_ports(&hw, base, ctl | 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391#else
Bartlomiej Zolnierkiewicz9239b332007-10-20 00:32:33 +0200392 ide_init_hwif_ports(&hw, base, ctl | 2, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393#endif
Bartlomiej Zolnierkiewicz9239b332007-10-20 00:32:33 +0200394 memcpy(hwif->io_ports, hw.io_ports, sizeof(hwif->io_ports));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 hwif->noprobe = !hwif->io_ports[IDE_DATA_OFFSET];
396 }
Bartlomiej Zolnierkiewicz528a5722007-10-20 00:32:30 +0200397 hwif->chipset = d->chipset ? d->chipset : ide_pci;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 hwif->pci_dev = dev;
Bartlomiej Zolnierkiewicz039788e2007-10-20 00:32:34 +0200399 hwif->cds = d;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 hwif->channel = port;
401
402 if (!hwif->irq)
403 hwif->irq = irq;
404 if (mate) {
405 hwif->mate = mate;
406 mate->mate = hwif;
407 }
408 return hwif;
409}
410
411/**
412 * ide_hwif_setup_dma - configure DMA interface
413 * @dev: PCI device
Bartlomiej Zolnierkiewicz039788e2007-10-20 00:32:34 +0200414 * @d: IDE port info
415 * @hwif: IDE interface
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 *
417 * Set up the DMA base for the interface. Enable the master bits as
418 * necessary and attempt to bring the device DMA into a ready to use
419 * state
420 */
Bartlomiej Zolnierkiewicz039788e2007-10-20 00:32:34 +0200421
Bartlomiej Zolnierkiewicz85620432007-10-20 00:32:34 +0200422static 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 -0700423{
Bartlomiej Zolnierkiewicz039788e2007-10-20 00:32:34 +0200424#ifdef CONFIG_BLK_DEV_IDEDMA_PCI
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 u16 pcicmd;
Bartlomiej Zolnierkiewicz47b68782007-10-19 00:30:06 +0200426
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 pci_read_config_word(dev, PCI_COMMAND, &pcicmd);
428
Bartlomiej Zolnierkiewicz47b68782007-10-19 00:30:06 +0200429 if ((d->host_flags & IDE_HFLAG_NO_AUTODMA) == 0 ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 ((dev->class >> 8) == PCI_CLASS_STORAGE_IDE &&
431 (dev->class & 0x80))) {
Bartlomiej Zolnierkiewicz9ffcf362007-10-19 00:30:07 +0200432 unsigned long dma_base = ide_get_or_set_dma_base(d, hwif);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 if (dma_base && !(pcicmd & PCI_COMMAND_MASTER)) {
434 /*
435 * Set up BM-DMA capability
436 * (PnP BIOS should have done this)
437 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 pci_set_master(dev);
439 if (pci_read_config_word(dev, PCI_COMMAND, &pcicmd) || !(pcicmd & PCI_COMMAND_MASTER)) {
440 printk(KERN_ERR "%s: %s error updating PCICMD\n",
441 hwif->name, d->name);
442 dma_base = 0;
443 }
444 }
445 if (dma_base) {
446 if (d->init_dma) {
447 d->init_dma(hwif, dma_base);
448 } else {
449 ide_setup_dma(hwif, dma_base, 8);
450 }
451 } else {
452 printk(KERN_INFO "%s: %s Bus-Master DMA disabled "
453 "(BIOS)\n", hwif->name, d->name);
454 }
455 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456#endif /* CONFIG_BLK_DEV_IDEDMA_PCI*/
Bartlomiej Zolnierkiewicz039788e2007-10-20 00:32:34 +0200457}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458
459/**
460 * ide_setup_pci_controller - set up IDE PCI
461 * @dev: PCI device
Bartlomiej Zolnierkiewicz039788e2007-10-20 00:32:34 +0200462 * @d: IDE port info
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 * @noisy: verbose flag
464 * @config: returned as 1 if we configured the hardware
465 *
466 * Set up the PCI and controller side of the IDE interface. This brings
467 * up the PCI side of the device, checks that the device is enabled
468 * and enables it if need be
469 */
Bartlomiej Zolnierkiewicz039788e2007-10-20 00:32:34 +0200470
Bartlomiej Zolnierkiewicz85620432007-10-20 00:32:34 +0200471static 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 -0700472{
473 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 u16 pcicmd;
475
476 if (noisy)
477 ide_setup_pci_noise(dev, d);
478
479 ret = ide_pci_enable(dev, d);
480 if (ret < 0)
481 goto out;
482
483 ret = pci_read_config_word(dev, PCI_COMMAND, &pcicmd);
484 if (ret < 0) {
485 printk(KERN_ERR "%s: error accessing PCI regs\n", d->name);
486 goto out;
487 }
488 if (!(pcicmd & PCI_COMMAND_IO)) { /* is device disabled? */
489 ret = ide_pci_configure(dev, d);
490 if (ret < 0)
491 goto out;
492 *config = 1;
493 printk(KERN_INFO "%s: device enabled (Linux)\n", d->name);
494 }
495
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496out:
497 return ret;
498}
499
500/**
501 * ide_pci_setup_ports - configure ports/devices on PCI IDE
502 * @dev: PCI device
Bartlomiej Zolnierkiewicz039788e2007-10-20 00:32:34 +0200503 * @d: IDE port info
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 * @pciirq: IRQ line
Bartlomiej Zolnierkiewicz8447d9d2007-10-20 00:32:31 +0200505 * @idx: ATA index table to update
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 *
507 * Scan the interfaces attached to this device and do any
508 * necessary per port setup. Attach the devices and ask the
509 * generic DMA layer to do its work for us.
510 *
511 * Normally called automaticall from do_ide_pci_setup_device,
512 * but is also used directly as a helper function by some controllers
513 * where the chipset setup is not the default PCI IDE one.
514 */
Bartlomiej Zolnierkiewicz8447d9d2007-10-20 00:32:31 +0200515
Bartlomiej Zolnierkiewicz85620432007-10-20 00:32:34 +0200516void 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 -0700517{
Bartlomiej Zolnierkiewicza5d8c5c2007-07-20 01:11:55 +0200518 int channels = (d->host_flags & IDE_HFLAG_SINGLE) ? 1 : 2, port;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 ide_hwif_t *hwif, *mate = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 u8 tmp;
521
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 /*
523 * Set up the IDE ports
524 */
Bartlomiej Zolnierkiewiczcf6e8542007-10-20 00:32:29 +0200525
Bartlomiej Zolnierkiewicza5d8c5c2007-07-20 01:11:55 +0200526 for (port = 0; port < channels; ++port) {
Bartlomiej Zolnierkiewicz85620432007-10-20 00:32:34 +0200527 const ide_pci_enablebit_t *e = &(d->enablebits[port]);
528
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 if (e->reg && (pci_read_config_byte(dev, e->reg, &tmp) ||
Bartlomiej Zolnierkiewiczcf6e8542007-10-20 00:32:29 +0200530 (tmp & e->mask) != e->val)) {
531 printk(KERN_INFO "%s: IDE port disabled\n", d->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 continue; /* port not enabled */
Bartlomiej Zolnierkiewiczcf6e8542007-10-20 00:32:29 +0200533 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 if ((hwif = ide_hwif_configure(dev, d, mate, port, pciirq)) == NULL)
536 continue;
537
538 /* setup proper ancestral information */
539 hwif->gendev.parent = &dev->dev;
540
Bartlomiej Zolnierkiewicz8447d9d2007-10-20 00:32:31 +0200541 *(idx + port) = hwif->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542
543
544 if (d->init_iops)
545 d->init_iops(hwif);
546
Bartlomiej Zolnierkiewicz9ffcf362007-10-19 00:30:07 +0200547 if ((d->host_flags & IDE_HFLAG_NO_DMA) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 ide_hwif_setup_dma(dev, d, hwif);
Bartlomiej Zolnierkiewicz9ffcf362007-10-19 00:30:07 +0200549
Bartlomiej Zolnierkiewicz8acf28c2007-10-20 00:32:30 +0200550 if ((!hwif->irq && (d->host_flags & IDE_HFLAG_LEGACY_IRQS)) ||
551 (d->host_flags & IDE_HFLAG_FORCE_LEGACY_IRQS))
Bartlomiej Zolnierkiewicz3985ee32007-10-19 00:30:11 +0200552 hwif->irq = port ? 15 : 14;
553
Bartlomiej Zolnierkiewiczfd9bb532007-10-20 00:32:31 +0200554 hwif->fixup = d->fixup;
555
Bartlomiej Zolnierkiewicz6a824c92007-07-20 01:11:58 +0200556 hwif->host_flags = d->host_flags;
Bartlomiej Zolnierkiewicz4099d142007-07-20 01:11:59 +0200557 hwif->pio_mask = d->pio_mask;
Bartlomiej Zolnierkiewicz6a824c92007-07-20 01:11:58 +0200558
Bartlomiej Zolnierkiewicz1c513612007-10-19 00:30:10 +0200559 if ((d->host_flags & IDE_HFLAG_SERIALIZE) && hwif->mate)
560 hwif->mate->serialized = hwif->serialized = 1;
561
Bartlomiej Zolnierkiewiczcaea7602007-10-20 00:32:30 +0200562 if (d->host_flags & IDE_HFLAG_IO_32BIT) {
563 hwif->drives[0].io_32bit = 1;
564 hwif->drives[1].io_32bit = 1;
565 }
566
567 if (d->host_flags & IDE_HFLAG_UNMASK_IRQS) {
568 hwif->drives[0].unmask = 1;
569 hwif->drives[1].unmask = 1;
570 }
571
Bartlomiej Zolnierkiewicz5f8b6c32007-10-19 00:30:07 +0200572 if (hwif->dma_base) {
573 hwif->swdma_mask = d->swdma_mask;
574 hwif->mwdma_mask = d->mwdma_mask;
575 hwif->ultra_mask = d->udma_mask;
576 }
577
Bartlomiej Zolnierkiewicz85ad93a2007-10-19 00:30:12 +0200578 hwif->drives[0].autotune = 1;
579 hwif->drives[1].autotune = 1;
580
Bartlomiej Zolnierkiewicz272a3702007-10-20 00:32:30 +0200581 if (d->host_flags & IDE_HFLAG_RQSIZE_256)
582 hwif->rqsize = 256;
583
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 if (d->init_hwif)
585 /* Call chipset-specific routine
586 * for each enabled hwif
587 */
588 d->init_hwif(hwif);
589
590 mate = hwif;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592}
593
594EXPORT_SYMBOL_GPL(ide_pci_setup_ports);
595
596/*
597 * ide_setup_pci_device() looks at the primary/secondary interfaces
598 * on a PCI IDE device and, if they are enabled, prepares the IDE driver
599 * for use with them. This generic code works for most PCI chipsets.
600 *
601 * One thing that is not standardized is the location of the
602 * primary/secondary interface "enable/disable" bits. For chipsets that
Bartlomiej Zolnierkiewicz039788e2007-10-20 00:32:34 +0200603 * we "know" about, this information is in the struct ide_port_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 * for all other chipsets, we just assume both interfaces are enabled.
605 */
Bartlomiej Zolnierkiewicz039788e2007-10-20 00:32:34 +0200606static int do_ide_setup_pci_device(struct pci_dev *dev,
Bartlomiej Zolnierkiewicz85620432007-10-20 00:32:34 +0200607 const struct ide_port_info *d,
Bartlomiej Zolnierkiewicz8447d9d2007-10-20 00:32:31 +0200608 u8 *idx, u8 noisy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 int tried_config = 0;
611 int pciirq, ret;
612
613 ret = ide_setup_pci_controller(dev, d, noisy, &tried_config);
614 if (ret < 0)
615 goto out;
616
617 /*
618 * Can we trust the reported IRQ?
619 */
620 pciirq = dev->irq;
621
622 /* Is it an "IDE storage" device in non-PCI mode? */
623 if ((dev->class >> 8) == PCI_CLASS_STORAGE_IDE && (dev->class & 5) != 5) {
624 if (noisy)
625 printk(KERN_INFO "%s: not 100%% native mode: "
626 "will probe irqs later\n", d->name);
627 /*
628 * This allows offboard ide-pci cards the enable a BIOS,
629 * verify interrupt settings of split-mirror pci-config
630 * space, place chipset into init-mode, and/or preserve
631 * an interrupt if the card is not native ide support.
632 */
633 ret = d->init_chipset ? d->init_chipset(dev, d->name) : 0;
634 if (ret < 0)
635 goto out;
636 pciirq = ret;
637 } else if (tried_config) {
638 if (noisy)
639 printk(KERN_INFO "%s: will probe irqs later\n", d->name);
640 pciirq = 0;
641 } else if (!pciirq) {
642 if (noisy)
643 printk(KERN_WARNING "%s: bad irq (%d): will probe later\n",
644 d->name, pciirq);
645 pciirq = 0;
646 } else {
647 if (d->init_chipset) {
648 ret = d->init_chipset(dev, d->name);
649 if (ret < 0)
650 goto out;
651 }
652 if (noisy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 printk(KERN_INFO "%s: 100%% native mode on irq %d\n",
654 d->name, pciirq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 }
656
657 /* FIXME: silent failure can happen */
658
Bartlomiej Zolnierkiewicz8447d9d2007-10-20 00:32:31 +0200659 ide_pci_setup_ports(dev, d, pciirq, idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660out:
661 return ret;
662}
663
Bartlomiej Zolnierkiewicz85620432007-10-20 00:32:34 +0200664int ide_setup_pci_device(struct pci_dev *dev, const struct ide_port_info *d)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665{
Bartlomiej Zolnierkiewicz8447d9d2007-10-20 00:32:31 +0200666 u8 idx[4] = { 0xff, 0xff, 0xff, 0xff };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 int ret;
668
Bartlomiej Zolnierkiewicz8447d9d2007-10-20 00:32:31 +0200669 ret = do_ide_setup_pci_device(dev, d, &idx[0], 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670
Bartlomiej Zolnierkiewicz8447d9d2007-10-20 00:32:31 +0200671 if (ret >= 0)
672 ide_device_add(idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 return ret;
675}
676
677EXPORT_SYMBOL_GPL(ide_setup_pci_device);
678
679int ide_setup_pci_devices(struct pci_dev *dev1, struct pci_dev *dev2,
Bartlomiej Zolnierkiewicz85620432007-10-20 00:32:34 +0200680 const struct ide_port_info *d)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681{
682 struct pci_dev *pdev[] = { dev1, dev2 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 int ret, i;
Bartlomiej Zolnierkiewicz8447d9d2007-10-20 00:32:31 +0200684 u8 idx[4] = { 0xff, 0xff, 0xff, 0xff };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685
686 for (i = 0; i < 2; i++) {
Bartlomiej Zolnierkiewicz8447d9d2007-10-20 00:32:31 +0200687 ret = do_ide_setup_pci_device(pdev[i], d, &idx[i*2], !i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 /*
689 * FIXME: Mom, mom, they stole me the helper function to undo
690 * do_ide_setup_pci_device() on the first device!
691 */
692 if (ret < 0)
693 goto out;
694 }
695
Bartlomiej Zolnierkiewicz8447d9d2007-10-20 00:32:31 +0200696 ide_device_add(idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697out:
698 return ret;
699}
700
701EXPORT_SYMBOL_GPL(ide_setup_pci_devices);
702
Bartlomiej Zolnierkiewicz6d208b32007-05-10 00:01:11 +0200703#ifdef CONFIG_IDEPCI_PCIBUS_ORDER
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704/*
705 * Module interfaces
706 */
Bartlomiej Zolnierkiewicz1b7d3472007-12-12 23:31:57 +0100707
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708static int pre_init = 1; /* Before first ordered IDE scan */
709static LIST_HEAD(ide_pci_drivers);
710
711/*
Ralf Baechlec37ea212005-11-18 23:11:24 +0100712 * __ide_pci_register_driver - attach IDE driver
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 * @driver: pci driver
Laurent riffard863b18f2005-10-27 23:12:54 +0200714 * @module: owner module of the driver
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 *
716 * Registers a driver with the IDE layer. The IDE layer arranges that
Bartlomiej Zolnierkiewicz1b7d3472007-12-12 23:31:57 +0100717 * boot time setup is done in the expected device order and then
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 * hands the controllers off to the core PCI code to do the rest of
719 * the work.
720 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 * Returns are the same as for pci_register_driver
722 */
723
Greg Kroah-Hartman725522b2007-01-15 11:50:02 -0800724int __ide_pci_register_driver(struct pci_driver *driver, struct module *module,
725 const char *mod_name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726{
Bartlomiej Zolnierkiewicz1b7d3472007-12-12 23:31:57 +0100727 if (!pre_init)
Greg Kroah-Hartman725522b2007-01-15 11:50:02 -0800728 return __pci_register_driver(driver, module, mod_name);
Laurent riffard863b18f2005-10-27 23:12:54 +0200729 driver->driver.owner = module;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 list_add_tail(&driver->node, &ide_pci_drivers);
731 return 0;
732}
Laurent riffard863b18f2005-10-27 23:12:54 +0200733EXPORT_SYMBOL_GPL(__ide_pci_register_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734
735/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 * ide_scan_pcidev - find an IDE driver for a device
737 * @dev: PCI device to check
738 *
739 * Look for an IDE driver to handle the device we are considering.
740 * This is only used during boot up to get the ordering correct. After
741 * boot up the pci layer takes over the job.
742 */
Bartlomiej Zolnierkiewicz1b7d3472007-12-12 23:31:57 +0100743
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744static int __init ide_scan_pcidev(struct pci_dev *dev)
745{
746 struct list_head *l;
747 struct pci_driver *d;
Bartlomiej Zolnierkiewicz1b7d3472007-12-12 23:31:57 +0100748
Sergei Shtylyov0505b552007-09-11 22:28:34 +0200749 list_for_each(l, &ide_pci_drivers) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 d = list_entry(l, struct pci_driver, node);
Sergei Shtylyov0505b552007-09-11 22:28:34 +0200751 if (d->id_table) {
Bartlomiej Zolnierkiewicz1b7d3472007-12-12 23:31:57 +0100752 const struct pci_device_id *id =
753 pci_match_id(d->id_table, dev);
754
Sergei Shtylyov0505b552007-09-11 22:28:34 +0200755 if (id != NULL && d->probe(dev, id) >= 0) {
756 dev->driver = d;
757 pci_dev_get(dev);
758 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 }
760 }
761 }
762 return 0;
763}
764
765/**
766 * ide_scan_pcibus - perform the initial IDE driver scan
767 * @scan_direction: set for reverse order scanning
768 *
769 * Perform the initial bus rather than driver ordered scan of the
770 * PCI drivers. After this all IDE pci handling becomes standard
771 * module ordering not traditionally ordered.
772 */
773
774void __init ide_scan_pcibus (int scan_direction)
775{
776 struct pci_dev *dev = NULL;
777 struct pci_driver *d;
778 struct list_head *l, *n;
779
780 pre_init = 0;
Sergei Shtylyov0505b552007-09-11 22:28:34 +0200781 if (!scan_direction)
Bartlomiej Zolnierkiewicz1b7d3472007-12-12 23:31:57 +0100782 while ((dev = pci_get_device(PCI_ANY_ID, PCI_ANY_ID, dev)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 ide_scan_pcidev(dev);
Sergei Shtylyov0505b552007-09-11 22:28:34 +0200784 else
Bartlomiej Zolnierkiewicz1b7d3472007-12-12 23:31:57 +0100785 while ((dev = pci_get_device_reverse(PCI_ANY_ID, PCI_ANY_ID,
786 dev)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 ide_scan_pcidev(dev);
Bartlomiej Zolnierkiewicz1b7d3472007-12-12 23:31:57 +0100788
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 /*
790 * Hand the drivers over to the PCI layer now we
791 * are post init.
792 */
793
Andrew Mortond61bcce2007-07-03 22:28:36 +0200794 list_for_each_safe(l, n, &ide_pci_drivers) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 list_del(l);
796 d = list_entry(l, struct pci_driver, node);
Bartlomiej Zolnierkiewicz125a8192007-12-12 23:31:57 +0100797 if (__pci_register_driver(d, d->driver.owner,
798 d->driver.mod_name))
799 printk(KERN_ERR "%s: failed to register %s driver\n",
800 __FUNCTION__, d->driver.mod_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 }
802}
Bartlomiej Zolnierkiewicz6d208b32007-05-10 00:01:11 +0200803#endif