blob: 0a4b3a6857e1d250601118e3083f8814896ccdd3 [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
Bartlomiej Zolnierkiewicz8ac2b42a2008-02-01 23:09:30 +0100143static void ide_pci_clear_simplex(unsigned long dma_base, const char *name)
144{
145 u8 dma_stat = inb(dma_base + 2);
146
147 outb(dma_stat & 0x60, dma_base + 2);
148 dma_stat = inb(dma_base + 2);
149 if (dma_stat & 0x80)
150 printk(KERN_INFO "%s: simplex device: DMA forced\n", name);
151}
152
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153/**
154 * ide_get_or_set_dma_base - setup BMIBA
Bartlomiej Zolnierkiewicz039788e2007-10-20 00:32:34 +0200155 * @d: IDE port info
156 * @hwif: IDE interface
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 *
Bartlomiej Zolnierkiewiczc58e79d2007-10-16 22:29:56 +0200158 * Fetch the DMA Bus-Master-I/O-Base-Address (BMIBA) from PCI space.
159 * Where a device has a partner that is already in DMA mode we check
160 * and enforce IDE simplex rules.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 */
162
Bartlomiej Zolnierkiewicz85620432007-10-20 00:32:34 +0200163static 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 -0700164{
165 unsigned long dma_base = 0;
166 struct pci_dev *dev = hwif->pci_dev;
Bartlomiej Zolnierkiewicz8ac2b42a2008-02-01 23:09:30 +0100167 u8 dma_stat = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 if (hwif->mmio)
170 return hwif->dma_base;
171
172 if (hwif->mate && hwif->mate->dma_base) {
173 dma_base = hwif->mate->dma_base - (hwif->channel ? 0 : 8);
174 } else {
Bartlomiej Zolnierkiewicz9ffcf362007-10-19 00:30:07 +0200175 u8 baridx = (d->host_flags & IDE_HFLAG_CS5520) ? 2 : 4;
176
177 dma_base = pci_resource_start(dev, baridx);
178
Bartlomiej Zolnierkiewiczaea5d372008-01-26 20:12:59 +0100179 if (dma_base == 0) {
Bartlomiej Zolnierkiewicz9ffcf362007-10-19 00:30:07 +0200180 printk(KERN_ERR "%s: DMA base is invalid\n", d->name);
Bartlomiej Zolnierkiewiczaea5d372008-01-26 20:12:59 +0100181 return 0;
182 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 }
184
Bartlomiej Zolnierkiewiczaea5d372008-01-26 20:12:59 +0100185 if (hwif->channel)
186 dma_base += 8;
187
Bartlomiej Zolnierkiewicz8ac2b42a2008-02-01 23:09:30 +0100188 if (d->host_flags & IDE_HFLAG_CS5520)
189 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190
Bartlomiej Zolnierkiewicz8ac2b42a2008-02-01 23:09:30 +0100191 if (d->host_flags & IDE_HFLAG_CLEAR_SIMPLEX) {
192 ide_pci_clear_simplex(dma_base, d->name);
193 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 }
Bartlomiej Zolnierkiewicz8ac2b42a2008-02-01 23:09:30 +0100195
196 /*
197 * If the device claims "simplex" DMA, this means that only one of
198 * the two interfaces can be trusted with DMA at any point in time
199 * (so we should enable DMA only on one of the two interfaces).
200 *
201 * FIXME: At this point we haven't probed the drives so we can't make
202 * the appropriate decision. Really we should defer this problem until
203 * we tune the drive then try to grab DMA ownership if we want to be
204 * the DMA end. This has to be become dynamic to handle hot-plug.
205 */
206 dma_stat = hwif->INB(dma_base + 2);
207 if ((dma_stat & 0x80) && hwif->mate && hwif->mate->dma_base) {
208 printk(KERN_INFO "%s: simplex device: DMA disabled\n", d->name);
209 dma_base = 0;
210 }
211out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 return dma_base;
213}
214#endif /* CONFIG_BLK_DEV_IDEDMA_PCI */
215
Bartlomiej Zolnierkiewicz85620432007-10-20 00:32:34 +0200216void ide_setup_pci_noise(struct pci_dev *dev, const struct ide_port_info *d)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217{
Bartlomiej Zolnierkiewiczbde07e52007-10-20 00:32:36 +0200218 printk(KERN_INFO "%s: IDE controller (0x%04x:0x%04x rev 0x%02x) at "
219 " PCI slot %s\n", d->name, dev->vendor, dev->device,
220 dev->revision, pci_name(dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221}
222
223EXPORT_SYMBOL_GPL(ide_setup_pci_noise);
224
225
226/**
227 * ide_pci_enable - do PCI enables
228 * @dev: PCI device
Bartlomiej Zolnierkiewicz039788e2007-10-20 00:32:34 +0200229 * @d: IDE port info
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 *
231 * Enable the IDE PCI device. We attempt to enable the device in full
232 * but if that fails then we only need BAR4 so we will enable that.
233 *
234 * Returns zero on success or an error code
235 */
Bartlomiej Zolnierkiewicz039788e2007-10-20 00:32:34 +0200236
Bartlomiej Zolnierkiewicz85620432007-10-20 00:32:34 +0200237static int ide_pci_enable(struct pci_dev *dev, const struct ide_port_info *d)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238{
239 int ret;
240
241 if (pci_enable_device(dev)) {
242 ret = pci_enable_device_bars(dev, 1 << 4);
243 if (ret < 0) {
244 printk(KERN_WARNING "%s: (ide_setup_pci_device:) "
245 "Could not enable device.\n", d->name);
246 goto out;
247 }
248 printk(KERN_WARNING "%s: BIOS configuration fixed.\n", d->name);
249 }
250
251 /*
Bartlomiej Zolnierkiewicz039788e2007-10-20 00:32:34 +0200252 * assume all devices can do 32-bit DMA for now, we can add
253 * a DMA mask field to the struct ide_port_info if we need it
254 * (or let lower level driver set the DMA mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 */
256 ret = pci_set_dma_mask(dev, DMA_32BIT_MASK);
257 if (ret < 0) {
258 printk(KERN_ERR "%s: can't set dma mask\n", d->name);
259 goto out;
260 }
261
262 /* FIXME: Temporary - until we put in the hotplug interface logic
263 Check that the bits we want are not in use by someone else. */
264 ret = pci_request_region(dev, 4, "ide_tmp");
265 if (ret < 0)
266 goto out;
267
268 pci_release_region(dev, 4);
269out:
270 return ret;
271}
272
273/**
274 * ide_pci_configure - configure an unconfigured device
275 * @dev: PCI device
Bartlomiej Zolnierkiewicz039788e2007-10-20 00:32:34 +0200276 * @d: IDE port info
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 *
278 * Enable and configure the PCI device we have been passed.
279 * Returns zero on success or an error code.
280 */
Bartlomiej Zolnierkiewicz039788e2007-10-20 00:32:34 +0200281
Bartlomiej Zolnierkiewicz85620432007-10-20 00:32:34 +0200282static int ide_pci_configure(struct pci_dev *dev, const struct ide_port_info *d)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283{
284 u16 pcicmd = 0;
285 /*
286 * PnP BIOS was *supposed* to have setup this device, but we
287 * can do it ourselves, so long as the BIOS has assigned an IRQ
288 * (or possibly the device is using a "legacy header" for IRQs).
289 * Maybe the user deliberately *disabled* the device,
290 * but we'll eventually ignore it again if no drives respond.
291 */
292 if (ide_setup_pci_baseregs(dev, d->name) || pci_write_config_word(dev, PCI_COMMAND, pcicmd|PCI_COMMAND_IO))
293 {
294 printk(KERN_INFO "%s: device disabled (BIOS)\n", d->name);
295 return -ENODEV;
296 }
297 if (pci_read_config_word(dev, PCI_COMMAND, &pcicmd)) {
298 printk(KERN_ERR "%s: error accessing PCI regs\n", d->name);
299 return -EIO;
300 }
301 if (!(pcicmd & PCI_COMMAND_IO)) {
302 printk(KERN_ERR "%s: unable to enable IDE controller\n", d->name);
303 return -ENXIO;
304 }
305 return 0;
306}
307
308/**
309 * ide_pci_check_iomem - check a register is I/O
Bartlomiej Zolnierkiewicz039788e2007-10-20 00:32:34 +0200310 * @dev: PCI device
311 * @d: IDE port info
312 * @bar: BAR number
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 *
314 * Checks if a BAR is configured and points to MMIO space. If so
315 * print an error and return an error code. Otherwise return 0
316 */
Bartlomiej Zolnierkiewicz039788e2007-10-20 00:32:34 +0200317
Bartlomiej Zolnierkiewicz85620432007-10-20 00:32:34 +0200318static int ide_pci_check_iomem(struct pci_dev *dev, const struct ide_port_info *d, int bar)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319{
320 ulong flags = pci_resource_flags(dev, bar);
321
322 /* Unconfigured ? */
323 if (!flags || pci_resource_len(dev, bar) == 0)
324 return 0;
325
326 /* I/O space */
327 if(flags & PCI_BASE_ADDRESS_IO_MASK)
328 return 0;
329
330 /* Bad */
331 printk(KERN_ERR "%s: IO baseregs (BIOS) are reported "
332 "as MEM, report to "
333 "<andre@linux-ide.org>.\n", d->name);
334 return -EINVAL;
335}
336
337/**
338 * ide_hwif_configure - configure an IDE interface
339 * @dev: PCI device holding interface
Bartlomiej Zolnierkiewicz039788e2007-10-20 00:32:34 +0200340 * @d: IDE port info
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 * @mate: Paired interface if any
342 *
343 * Perform the initial set up for the hardware interface structure. This
344 * is done per interface port rather than per PCI device. There may be
345 * more than one port per device.
346 *
347 * Returns the new hardware interface structure, or NULL on a failure
348 */
Bartlomiej Zolnierkiewicz039788e2007-10-20 00:32:34 +0200349
Bartlomiej Zolnierkiewicz85620432007-10-20 00:32:34 +0200350static 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 -0700351{
352 unsigned long ctl = 0, base = 0;
353 ide_hwif_t *hwif;
Bartlomiej Zolnierkiewicz7cab14a2007-10-19 00:30:06 +0200354 u8 bootable = (d->host_flags & IDE_HFLAG_BOOTABLE) ? 1 : 0;
Bartlomiej Zolnierkiewicz79127c32008-01-26 20:13:08 +0100355 u8 oldnoprobe = 0;
356 struct hw_regs_s hw;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357
Bartlomiej Zolnierkiewicza5d8c5c2007-07-20 01:11:55 +0200358 if ((d->host_flags & IDE_HFLAG_ISA_PORTS) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 /* Possibly we should fail if these checks report true */
360 ide_pci_check_iomem(dev, d, 2*port);
361 ide_pci_check_iomem(dev, d, 2*port+1);
362
363 ctl = pci_resource_start(dev, 2*port+1);
364 base = pci_resource_start(dev, 2*port);
365 if ((ctl && !base) || (base && !ctl)) {
366 printk(KERN_ERR "%s: inconsistent baseregs (BIOS) "
367 "for port %d, skipping\n", d->name, port);
368 return NULL;
369 }
370 }
371 if (!ctl)
372 {
373 /* Use default values */
374 ctl = port ? 0x374 : 0x3f4;
375 base = port ? 0x170 : 0x1f0;
376 }
Bartlomiej Zolnierkiewicz7cab14a2007-10-19 00:30:06 +0200377 if ((hwif = ide_match_hwif(base, bootable, d->name)) == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 return NULL; /* no room in ide_hwifs[] */
Bartlomiej Zolnierkiewicz9239b332007-10-20 00:32:33 +0200379
Bartlomiej Zolnierkiewicz79127c32008-01-26 20:13:08 +0100380 memset(&hw, 0, sizeof(hw));
381 hw.irq = hwif->irq ? hwif->irq : irq;
382 hw.dev = &dev->dev;
383 hw.chipset = d->chipset ? d->chipset : ide_pci;
384 ide_std_init_ports(&hw, base, ctl | 2);
385
386 if (hwif->io_ports[IDE_DATA_OFFSET] == base &&
387 hwif->io_ports[IDE_CONTROL_OFFSET] == (ctl | 2))
388 oldnoprobe = hwif->noprobe;
389
390 ide_init_port_hw(hwif, &hw);
391
392 hwif->noprobe = oldnoprobe;
393
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 hwif->pci_dev = dev;
Bartlomiej Zolnierkiewicz039788e2007-10-20 00:32:34 +0200395 hwif->cds = d;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 hwif->channel = port;
397
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 if (mate) {
399 hwif->mate = mate;
400 mate->mate = hwif;
401 }
402 return hwif;
403}
404
405/**
406 * ide_hwif_setup_dma - configure DMA interface
407 * @dev: PCI device
Bartlomiej Zolnierkiewicz039788e2007-10-20 00:32:34 +0200408 * @d: IDE port info
409 * @hwif: IDE interface
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 *
411 * Set up the DMA base for the interface. Enable the master bits as
412 * necessary and attempt to bring the device DMA into a ready to use
413 * state
414 */
Bartlomiej Zolnierkiewicz039788e2007-10-20 00:32:34 +0200415
Bartlomiej Zolnierkiewicz85620432007-10-20 00:32:34 +0200416static 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 -0700417{
Bartlomiej Zolnierkiewicz039788e2007-10-20 00:32:34 +0200418#ifdef CONFIG_BLK_DEV_IDEDMA_PCI
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 u16 pcicmd;
Bartlomiej Zolnierkiewicz47b68782007-10-19 00:30:06 +0200420
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 pci_read_config_word(dev, PCI_COMMAND, &pcicmd);
422
Bartlomiej Zolnierkiewicz47b68782007-10-19 00:30:06 +0200423 if ((d->host_flags & IDE_HFLAG_NO_AUTODMA) == 0 ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 ((dev->class >> 8) == PCI_CLASS_STORAGE_IDE &&
425 (dev->class & 0x80))) {
Bartlomiej Zolnierkiewicz9ffcf362007-10-19 00:30:07 +0200426 unsigned long dma_base = ide_get_or_set_dma_base(d, hwif);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 if (dma_base && !(pcicmd & PCI_COMMAND_MASTER)) {
428 /*
429 * Set up BM-DMA capability
430 * (PnP BIOS should have done this)
431 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 pci_set_master(dev);
433 if (pci_read_config_word(dev, PCI_COMMAND, &pcicmd) || !(pcicmd & PCI_COMMAND_MASTER)) {
434 printk(KERN_ERR "%s: %s error updating PCICMD\n",
435 hwif->name, d->name);
436 dma_base = 0;
437 }
438 }
439 if (dma_base) {
440 if (d->init_dma) {
441 d->init_dma(hwif, dma_base);
442 } else {
Sergei Shtylyovecf327962008-02-01 23:09:30 +0100443 ide_setup_dma(hwif, dma_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 }
445 } else {
446 printk(KERN_INFO "%s: %s Bus-Master DMA disabled "
447 "(BIOS)\n", hwif->name, d->name);
448 }
449 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450#endif /* CONFIG_BLK_DEV_IDEDMA_PCI*/
Bartlomiej Zolnierkiewicz039788e2007-10-20 00:32:34 +0200451}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452
453/**
454 * ide_setup_pci_controller - set up IDE PCI
455 * @dev: PCI device
Bartlomiej Zolnierkiewicz039788e2007-10-20 00:32:34 +0200456 * @d: IDE port info
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 * @noisy: verbose flag
458 * @config: returned as 1 if we configured the hardware
459 *
460 * Set up the PCI and controller side of the IDE interface. This brings
461 * up the PCI side of the device, checks that the device is enabled
462 * and enables it if need be
463 */
Bartlomiej Zolnierkiewicz039788e2007-10-20 00:32:34 +0200464
Bartlomiej Zolnierkiewicz85620432007-10-20 00:32:34 +0200465static 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 -0700466{
467 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 u16 pcicmd;
469
470 if (noisy)
471 ide_setup_pci_noise(dev, d);
472
473 ret = ide_pci_enable(dev, d);
474 if (ret < 0)
475 goto out;
476
477 ret = pci_read_config_word(dev, PCI_COMMAND, &pcicmd);
478 if (ret < 0) {
479 printk(KERN_ERR "%s: error accessing PCI regs\n", d->name);
480 goto out;
481 }
482 if (!(pcicmd & PCI_COMMAND_IO)) { /* is device disabled? */
483 ret = ide_pci_configure(dev, d);
484 if (ret < 0)
485 goto out;
486 *config = 1;
487 printk(KERN_INFO "%s: device enabled (Linux)\n", d->name);
488 }
489
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490out:
491 return ret;
492}
493
494/**
495 * ide_pci_setup_ports - configure ports/devices on PCI IDE
496 * @dev: PCI device
Bartlomiej Zolnierkiewicz039788e2007-10-20 00:32:34 +0200497 * @d: IDE port info
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 * @pciirq: IRQ line
Bartlomiej Zolnierkiewicz8447d9d2007-10-20 00:32:31 +0200499 * @idx: ATA index table to update
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 *
501 * Scan the interfaces attached to this device and do any
502 * necessary per port setup. Attach the devices and ask the
503 * generic DMA layer to do its work for us.
504 *
505 * Normally called automaticall from do_ide_pci_setup_device,
506 * but is also used directly as a helper function by some controllers
507 * where the chipset setup is not the default PCI IDE one.
508 */
Bartlomiej Zolnierkiewicz8447d9d2007-10-20 00:32:31 +0200509
Bartlomiej Zolnierkiewicz85620432007-10-20 00:32:34 +0200510void 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 -0700511{
Bartlomiej Zolnierkiewicza5d8c5c2007-07-20 01:11:55 +0200512 int channels = (d->host_flags & IDE_HFLAG_SINGLE) ? 1 : 2, port;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 ide_hwif_t *hwif, *mate = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 u8 tmp;
515
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 /*
517 * Set up the IDE ports
518 */
Bartlomiej Zolnierkiewiczcf6e8542007-10-20 00:32:29 +0200519
Bartlomiej Zolnierkiewicza5d8c5c2007-07-20 01:11:55 +0200520 for (port = 0; port < channels; ++port) {
Bartlomiej Zolnierkiewicz85620432007-10-20 00:32:34 +0200521 const ide_pci_enablebit_t *e = &(d->enablebits[port]);
522
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 if (e->reg && (pci_read_config_byte(dev, e->reg, &tmp) ||
Bartlomiej Zolnierkiewiczcf6e8542007-10-20 00:32:29 +0200524 (tmp & e->mask) != e->val)) {
525 printk(KERN_INFO "%s: IDE port disabled\n", d->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 continue; /* port not enabled */
Bartlomiej Zolnierkiewiczcf6e8542007-10-20 00:32:29 +0200527 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 if ((hwif = ide_hwif_configure(dev, d, mate, port, pciirq)) == NULL)
530 continue;
531
Bartlomiej Zolnierkiewicz8447d9d2007-10-20 00:32:31 +0200532 *(idx + port) = hwif->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 if (d->init_iops)
535 d->init_iops(hwif);
536
Bartlomiej Zolnierkiewicz9ffcf362007-10-19 00:30:07 +0200537 if ((d->host_flags & IDE_HFLAG_NO_DMA) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 ide_hwif_setup_dma(dev, d, hwif);
Bartlomiej Zolnierkiewicz9ffcf362007-10-19 00:30:07 +0200539
Bartlomiej Zolnierkiewicz8acf28c2007-10-20 00:32:30 +0200540 if ((!hwif->irq && (d->host_flags & IDE_HFLAG_LEGACY_IRQS)) ||
541 (d->host_flags & IDE_HFLAG_FORCE_LEGACY_IRQS))
Bartlomiej Zolnierkiewicz3985ee32007-10-19 00:30:11 +0200542 hwif->irq = port ? 15 : 14;
543
Bartlomiej Zolnierkiewicz6a824c92007-07-20 01:11:58 +0200544 hwif->host_flags = d->host_flags;
Bartlomiej Zolnierkiewicz4099d142007-07-20 01:11:59 +0200545 hwif->pio_mask = d->pio_mask;
Bartlomiej Zolnierkiewicz6a824c92007-07-20 01:11:58 +0200546
Bartlomiej Zolnierkiewicz1c513612007-10-19 00:30:10 +0200547 if ((d->host_flags & IDE_HFLAG_SERIALIZE) && hwif->mate)
548 hwif->mate->serialized = hwif->serialized = 1;
549
Bartlomiej Zolnierkiewiczcaea7602007-10-20 00:32:30 +0200550 if (d->host_flags & IDE_HFLAG_IO_32BIT) {
551 hwif->drives[0].io_32bit = 1;
552 hwif->drives[1].io_32bit = 1;
553 }
554
555 if (d->host_flags & IDE_HFLAG_UNMASK_IRQS) {
556 hwif->drives[0].unmask = 1;
557 hwif->drives[1].unmask = 1;
558 }
559
Bartlomiej Zolnierkiewicz5f8b6c32007-10-19 00:30:07 +0200560 if (hwif->dma_base) {
561 hwif->swdma_mask = d->swdma_mask;
562 hwif->mwdma_mask = d->mwdma_mask;
563 hwif->ultra_mask = d->udma_mask;
564 }
565
Bartlomiej Zolnierkiewicz85ad93a2007-10-19 00:30:12 +0200566 hwif->drives[0].autotune = 1;
567 hwif->drives[1].autotune = 1;
568
Bartlomiej Zolnierkiewicz272a3702007-10-20 00:32:30 +0200569 if (d->host_flags & IDE_HFLAG_RQSIZE_256)
570 hwif->rqsize = 256;
571
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 if (d->init_hwif)
573 /* Call chipset-specific routine
574 * for each enabled hwif
575 */
576 d->init_hwif(hwif);
577
578 mate = hwif;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580}
581
582EXPORT_SYMBOL_GPL(ide_pci_setup_ports);
583
584/*
585 * ide_setup_pci_device() looks at the primary/secondary interfaces
586 * on a PCI IDE device and, if they are enabled, prepares the IDE driver
587 * for use with them. This generic code works for most PCI chipsets.
588 *
589 * One thing that is not standardized is the location of the
590 * primary/secondary interface "enable/disable" bits. For chipsets that
Bartlomiej Zolnierkiewicz039788e2007-10-20 00:32:34 +0200591 * we "know" about, this information is in the struct ide_port_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 * for all other chipsets, we just assume both interfaces are enabled.
593 */
Bartlomiej Zolnierkiewicz039788e2007-10-20 00:32:34 +0200594static int do_ide_setup_pci_device(struct pci_dev *dev,
Bartlomiej Zolnierkiewicz85620432007-10-20 00:32:34 +0200595 const struct ide_port_info *d,
Bartlomiej Zolnierkiewicz8447d9d2007-10-20 00:32:31 +0200596 u8 *idx, u8 noisy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 int tried_config = 0;
599 int pciirq, ret;
600
601 ret = ide_setup_pci_controller(dev, d, noisy, &tried_config);
602 if (ret < 0)
603 goto out;
604
605 /*
606 * Can we trust the reported IRQ?
607 */
608 pciirq = dev->irq;
609
610 /* Is it an "IDE storage" device in non-PCI mode? */
611 if ((dev->class >> 8) == PCI_CLASS_STORAGE_IDE && (dev->class & 5) != 5) {
612 if (noisy)
613 printk(KERN_INFO "%s: not 100%% native mode: "
614 "will probe irqs later\n", d->name);
615 /*
616 * This allows offboard ide-pci cards the enable a BIOS,
617 * verify interrupt settings of split-mirror pci-config
618 * space, place chipset into init-mode, and/or preserve
619 * an interrupt if the card is not native ide support.
620 */
621 ret = d->init_chipset ? d->init_chipset(dev, d->name) : 0;
622 if (ret < 0)
623 goto out;
624 pciirq = ret;
625 } else if (tried_config) {
626 if (noisy)
627 printk(KERN_INFO "%s: will probe irqs later\n", d->name);
628 pciirq = 0;
629 } else if (!pciirq) {
630 if (noisy)
631 printk(KERN_WARNING "%s: bad irq (%d): will probe later\n",
632 d->name, pciirq);
633 pciirq = 0;
634 } else {
635 if (d->init_chipset) {
636 ret = d->init_chipset(dev, d->name);
637 if (ret < 0)
638 goto out;
639 }
640 if (noisy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 printk(KERN_INFO "%s: 100%% native mode on irq %d\n",
642 d->name, pciirq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 }
644
645 /* FIXME: silent failure can happen */
646
Bartlomiej Zolnierkiewicz8447d9d2007-10-20 00:32:31 +0200647 ide_pci_setup_ports(dev, d, pciirq, idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648out:
649 return ret;
650}
651
Bartlomiej Zolnierkiewicz85620432007-10-20 00:32:34 +0200652int ide_setup_pci_device(struct pci_dev *dev, const struct ide_port_info *d)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653{
Bartlomiej Zolnierkiewicz8447d9d2007-10-20 00:32:31 +0200654 u8 idx[4] = { 0xff, 0xff, 0xff, 0xff };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 int ret;
656
Bartlomiej Zolnierkiewicz8447d9d2007-10-20 00:32:31 +0200657 ret = do_ide_setup_pci_device(dev, d, &idx[0], 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658
Bartlomiej Zolnierkiewicz8447d9d2007-10-20 00:32:31 +0200659 if (ret >= 0)
660 ide_device_add(idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 return ret;
663}
664
665EXPORT_SYMBOL_GPL(ide_setup_pci_device);
666
667int ide_setup_pci_devices(struct pci_dev *dev1, struct pci_dev *dev2,
Bartlomiej Zolnierkiewicz85620432007-10-20 00:32:34 +0200668 const struct ide_port_info *d)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669{
670 struct pci_dev *pdev[] = { dev1, dev2 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 int ret, i;
Bartlomiej Zolnierkiewicz8447d9d2007-10-20 00:32:31 +0200672 u8 idx[4] = { 0xff, 0xff, 0xff, 0xff };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673
674 for (i = 0; i < 2; i++) {
Bartlomiej Zolnierkiewicz8447d9d2007-10-20 00:32:31 +0200675 ret = do_ide_setup_pci_device(pdev[i], d, &idx[i*2], !i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 /*
677 * FIXME: Mom, mom, they stole me the helper function to undo
678 * do_ide_setup_pci_device() on the first device!
679 */
680 if (ret < 0)
681 goto out;
682 }
683
Bartlomiej Zolnierkiewicz8447d9d2007-10-20 00:32:31 +0200684 ide_device_add(idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685out:
686 return ret;
687}
688
689EXPORT_SYMBOL_GPL(ide_setup_pci_devices);