ide: manage resources for PCI devices in ide_pci_enable() (take 3)
[pandora-kernel.git] / drivers / ide / setup-pci.c
1 /*
2  *  Copyright (C) 1998-2000  Andre Hedrick <andre@linux-ide.org>
3  *  Copyright (C) 1995-1998  Mark Lord
4  *  Copyright (C)      2007  Bartlomiej Zolnierkiewicz
5  *
6  *  May be copied or modified under the terms of the GNU General Public License
7  */
8
9 #include <linux/module.h>
10 #include <linux/types.h>
11 #include <linux/kernel.h>
12 #include <linux/pci.h>
13 #include <linux/init.h>
14 #include <linux/timer.h>
15 #include <linux/mm.h>
16 #include <linux/interrupt.h>
17 #include <linux/ide.h>
18 #include <linux/dma-mapping.h>
19
20 #include <asm/io.h>
21 #include <asm/irq.h>
22
23 /**
24  *      ide_setup_pci_baseregs  -       place a PCI IDE controller native
25  *      @dev: PCI device of interface to switch native
26  *      @name: Name of interface
27  *
28  *      We attempt to place the PCI interface into PCI native mode. If
29  *      we succeed the BARs are ok and the controller is in PCI mode.
30  *      Returns 0 on success or an errno code.
31  *
32  *      FIXME: if we program the interface and then fail to set the BARS
33  *      we don't switch it back to legacy mode. Do we actually care ??
34  */
35
36 static int ide_setup_pci_baseregs(struct pci_dev *dev, const char *name)
37 {
38         u8 progif = 0;
39
40         /*
41          * Place both IDE interfaces into PCI "native" mode:
42          */
43         if (pci_read_config_byte(dev, PCI_CLASS_PROG, &progif) ||
44                          (progif & 5) != 5) {
45                 if ((progif & 0xa) != 0xa) {
46                         printk(KERN_INFO "%s: device not capable of full "
47                                 "native PCI mode\n", name);
48                         return -EOPNOTSUPP;
49                 }
50                 printk("%s: placing both ports into native PCI mode\n", name);
51                 (void) pci_write_config_byte(dev, PCI_CLASS_PROG, progif|5);
52                 if (pci_read_config_byte(dev, PCI_CLASS_PROG, &progif) ||
53                     (progif & 5) != 5) {
54                         printk(KERN_ERR "%s: rewrite of PROGIF failed, wanted "
55                                 "0x%04x, got 0x%04x\n",
56                                 name, progif|5, progif);
57                         return -EOPNOTSUPP;
58                 }
59         }
60         return 0;
61 }
62
63 #ifdef CONFIG_BLK_DEV_IDEDMA_PCI
64 static void ide_pci_clear_simplex(unsigned long dma_base, const char *name)
65 {
66         u8 dma_stat = inb(dma_base + 2);
67
68         outb(dma_stat & 0x60, dma_base + 2);
69         dma_stat = inb(dma_base + 2);
70         if (dma_stat & 0x80)
71                 printk(KERN_INFO "%s: simplex device: DMA forced\n", name);
72 }
73
74 /**
75  *      ide_get_or_set_dma_base         -       setup BMIBA
76  *      @d: IDE port info
77  *      @hwif: IDE interface
78  *
79  *      Fetch the DMA Bus-Master-I/O-Base-Address (BMIBA) from PCI space.
80  *      Where a device has a partner that is already in DMA mode we check
81  *      and enforce IDE simplex rules.
82  */
83
84 static unsigned long ide_get_or_set_dma_base(const struct ide_port_info *d, ide_hwif_t *hwif)
85 {
86         struct pci_dev *dev = to_pci_dev(hwif->dev);
87         unsigned long dma_base = 0;
88         u8 dma_stat = 0;
89
90         if (hwif->mmio)
91                 return hwif->dma_base;
92
93         if (hwif->mate && hwif->mate->dma_base) {
94                 dma_base = hwif->mate->dma_base - (hwif->channel ? 0 : 8);
95         } else {
96                 u8 baridx = (d->host_flags & IDE_HFLAG_CS5520) ? 2 : 4;
97
98                 dma_base = pci_resource_start(dev, baridx);
99
100                 if (dma_base == 0) {
101                         printk(KERN_ERR "%s: DMA base is invalid\n", d->name);
102                         return 0;
103                 }
104         }
105
106         if (hwif->channel)
107                 dma_base += 8;
108
109         if (d->host_flags & IDE_HFLAG_CS5520)
110                 goto out;
111
112         if (d->host_flags & IDE_HFLAG_CLEAR_SIMPLEX) {
113                 ide_pci_clear_simplex(dma_base, d->name);
114                 goto out;
115         }
116
117         /*
118          * If the device claims "simplex" DMA, this means that only one of
119          * the two interfaces can be trusted with DMA at any point in time
120          * (so we should enable DMA only on one of the two interfaces).
121          *
122          * FIXME: At this point we haven't probed the drives so we can't make
123          * the appropriate decision.  Really we should defer this problem until
124          * we tune the drive then try to grab DMA ownership if we want to be
125          * the DMA end.  This has to be become dynamic to handle hot-plug.
126          */
127         dma_stat = hwif->INB(dma_base + 2);
128         if ((dma_stat & 0x80) && hwif->mate && hwif->mate->dma_base) {
129                 printk(KERN_INFO "%s: simplex device: DMA disabled\n", d->name);
130                 dma_base = 0;
131         }
132 out:
133         return dma_base;
134 }
135 #endif /* CONFIG_BLK_DEV_IDEDMA_PCI */
136
137 void ide_setup_pci_noise(struct pci_dev *dev, const struct ide_port_info *d)
138 {
139         printk(KERN_INFO "%s: IDE controller (0x%04x:0x%04x rev 0x%02x) at "
140                          " PCI slot %s\n", d->name, dev->vendor, dev->device,
141                          dev->revision, pci_name(dev));
142 }
143 EXPORT_SYMBOL_GPL(ide_setup_pci_noise);
144
145
146 /**
147  *      ide_pci_enable  -       do PCI enables
148  *      @dev: PCI device
149  *      @d: IDE port info
150  *
151  *      Enable the IDE PCI device. We attempt to enable the device in full
152  *      but if that fails then we only need IO space. The PCI code should
153  *      have setup the proper resources for us already for controllers in
154  *      legacy mode.
155  *
156  *      Returns zero on success or an error code
157  */
158
159 static int ide_pci_enable(struct pci_dev *dev, const struct ide_port_info *d)
160 {
161         int ret, bars;
162
163         if (pci_enable_device(dev)) {
164                 ret = pci_enable_device_io(dev);
165                 if (ret < 0) {
166                         printk(KERN_WARNING "%s: (ide_setup_pci_device:) "
167                                 "Could not enable device.\n", d->name);
168                         goto out;
169                 }
170                 printk(KERN_WARNING "%s: BIOS configuration fixed.\n", d->name);
171         }
172
173         /*
174          * assume all devices can do 32-bit DMA for now, we can add
175          * a DMA mask field to the struct ide_port_info if we need it
176          * (or let lower level driver set the DMA mask)
177          */
178         ret = pci_set_dma_mask(dev, DMA_32BIT_MASK);
179         if (ret < 0) {
180                 printk(KERN_ERR "%s: can't set dma mask\n", d->name);
181                 goto out;
182         }
183
184         if (d->host_flags & IDE_HFLAG_SINGLE)
185                 bars = (1 << 2) - 1;
186         else
187                 bars = (1 << 4) - 1;
188
189         if ((d->host_flags & IDE_HFLAG_NO_DMA) == 0) {
190                 if (d->host_flags & IDE_HFLAG_CS5520)
191                         bars |= (1 << 2);
192                 else
193                         bars |= (1 << 4);
194         }
195
196         ret = pci_request_selected_regions(dev, bars, d->name);
197         if (ret < 0)
198                 printk(KERN_ERR "%s: can't reserve resources\n", d->name);
199 out:
200         return ret;
201 }
202
203 /**
204  *      ide_pci_configure       -       configure an unconfigured device
205  *      @dev: PCI device
206  *      @d: IDE port info
207  *
208  *      Enable and configure the PCI device we have been passed.
209  *      Returns zero on success or an error code.
210  */
211
212 static int ide_pci_configure(struct pci_dev *dev, const struct ide_port_info *d)
213 {
214         u16 pcicmd = 0;
215         /*
216          * PnP BIOS was *supposed* to have setup this device, but we
217          * can do it ourselves, so long as the BIOS has assigned an IRQ
218          * (or possibly the device is using a "legacy header" for IRQs).
219          * Maybe the user deliberately *disabled* the device,
220          * but we'll eventually ignore it again if no drives respond.
221          */
222         if (ide_setup_pci_baseregs(dev, d->name) ||
223             pci_write_config_word(dev, PCI_COMMAND, pcicmd | PCI_COMMAND_IO)) {
224                 printk(KERN_INFO "%s: device disabled (BIOS)\n", d->name);
225                 return -ENODEV;
226         }
227         if (pci_read_config_word(dev, PCI_COMMAND, &pcicmd)) {
228                 printk(KERN_ERR "%s: error accessing PCI regs\n", d->name);
229                 return -EIO;
230         }
231         if (!(pcicmd & PCI_COMMAND_IO)) {
232                 printk(KERN_ERR "%s: unable to enable IDE controller\n", d->name);
233                 return -ENXIO;
234         }
235         return 0;
236 }
237
238 /**
239  *      ide_pci_check_iomem     -       check a register is I/O
240  *      @dev: PCI device
241  *      @d: IDE port info
242  *      @bar: BAR number
243  *
244  *      Checks if a BAR is configured and points to MMIO space. If so,
245  *      return an error code. Otherwise return 0
246  */
247
248 static int ide_pci_check_iomem(struct pci_dev *dev, const struct ide_port_info *d,
249                                int bar)
250 {
251         ulong flags = pci_resource_flags(dev, bar);
252
253         /* Unconfigured ? */
254         if (!flags || pci_resource_len(dev, bar) == 0)
255                 return 0;
256
257         /* I/O space */
258         if (flags & IORESOURCE_IO)
259                 return 0;
260
261         /* Bad */
262         return -EINVAL;
263 }
264
265 /**
266  *      ide_hwif_configure      -       configure an IDE interface
267  *      @dev: PCI device holding interface
268  *      @d: IDE port info
269  *      @port: port number
270  *      @irq: PCI IRQ
271  *
272  *      Perform the initial set up for the hardware interface structure. This
273  *      is done per interface port rather than per PCI device. There may be
274  *      more than one port per device.
275  *
276  *      Returns the new hardware interface structure, or NULL on a failure
277  */
278
279 static ide_hwif_t *ide_hwif_configure(struct pci_dev *dev,
280                                       const struct ide_port_info *d,
281                                       unsigned int port, int irq)
282 {
283         unsigned long ctl = 0, base = 0;
284         ide_hwif_t *hwif;
285         struct hw_regs_s hw;
286
287         if ((d->host_flags & IDE_HFLAG_ISA_PORTS) == 0) {
288                 if (ide_pci_check_iomem(dev, d, 2 * port) ||
289                     ide_pci_check_iomem(dev, d, 2 * port + 1)) {
290                         printk(KERN_ERR "%s: I/O baseregs (BIOS) are reported "
291                                         "as MEM for port %d!\n", d->name, port);
292                         return NULL;
293                 }
294
295                 ctl  = pci_resource_start(dev, 2*port+1);
296                 base = pci_resource_start(dev, 2*port);
297                 if ((ctl && !base) || (base && !ctl)) {
298                         printk(KERN_ERR "%s: inconsistent baseregs (BIOS) "
299                                 "for port %d, skipping\n", d->name, port);
300                         return NULL;
301                 }
302         }
303         if (!ctl) {
304                 /* Use default values */
305                 ctl = port ? 0x374 : 0x3f4;
306                 base = port ? 0x170 : 0x1f0;
307         }
308
309         hwif = ide_find_port_slot(d);
310         if (hwif == NULL) {
311                 printk(KERN_ERR "%s: too many IDE interfaces, no room in "
312                                 "table\n", d->name);
313                 return NULL;
314         }
315
316         memset(&hw, 0, sizeof(hw));
317         hw.irq = irq;
318         hw.dev = &dev->dev;
319         hw.chipset = d->chipset ? d->chipset : ide_pci;
320         ide_std_init_ports(&hw, base, ctl | 2);
321
322         ide_init_port_hw(hwif, &hw);
323
324         hwif->dev = &dev->dev;
325         hwif->cds = d;
326
327         return hwif;
328 }
329
330 #ifdef CONFIG_BLK_DEV_IDEDMA_PCI
331 /**
332  *      ide_hwif_setup_dma      -       configure DMA interface
333  *      @hwif: IDE interface
334  *      @d: IDE port info
335  *
336  *      Set up the DMA base for the interface. Enable the master bits as
337  *      necessary and attempt to bring the device DMA into a ready to use
338  *      state
339  */
340
341 void ide_hwif_setup_dma(ide_hwif_t *hwif, const struct ide_port_info *d)
342 {
343         struct pci_dev *dev = to_pci_dev(hwif->dev);
344         u16 pcicmd;
345
346         pci_read_config_word(dev, PCI_COMMAND, &pcicmd);
347
348         if ((d->host_flags & IDE_HFLAG_NO_AUTODMA) == 0 ||
349             ((dev->class >> 8) == PCI_CLASS_STORAGE_IDE &&
350              (dev->class & 0x80))) {
351                 unsigned long dma_base = ide_get_or_set_dma_base(d, hwif);
352                 if (dma_base && !(pcicmd & PCI_COMMAND_MASTER)) {
353                         /*
354                          * Set up BM-DMA capability
355                          * (PnP BIOS should have done this)
356                          */
357                         pci_set_master(dev);
358                         if (pci_read_config_word(dev, PCI_COMMAND, &pcicmd) || !(pcicmd & PCI_COMMAND_MASTER)) {
359                                 printk(KERN_ERR "%s: %s error updating PCICMD\n",
360                                         hwif->name, d->name);
361                                 dma_base = 0;
362                         }
363                 }
364                 if (dma_base) {
365                         if (d->init_dma) {
366                                 d->init_dma(hwif, dma_base);
367                         } else {
368                                 ide_setup_dma(hwif, dma_base);
369                         }
370                 } else {
371                         printk(KERN_INFO "%s: %s Bus-Master DMA disabled "
372                                 "(BIOS)\n", hwif->name, d->name);
373                 }
374         }
375 }
376 #endif /* CONFIG_BLK_DEV_IDEDMA_PCI */
377
378 /**
379  *      ide_setup_pci_controller        -       set up IDE PCI
380  *      @dev: PCI device
381  *      @d: IDE port info
382  *      @noisy: verbose flag
383  *      @config: returned as 1 if we configured the hardware
384  *
385  *      Set up the PCI and controller side of the IDE interface. This brings
386  *      up the PCI side of the device, checks that the device is enabled
387  *      and enables it if need be
388  */
389
390 static int ide_setup_pci_controller(struct pci_dev *dev, const struct ide_port_info *d, int noisy, int *config)
391 {
392         int ret;
393         u16 pcicmd;
394
395         if (noisy)
396                 ide_setup_pci_noise(dev, d);
397
398         ret = ide_pci_enable(dev, d);
399         if (ret < 0)
400                 goto out;
401
402         ret = pci_read_config_word(dev, PCI_COMMAND, &pcicmd);
403         if (ret < 0) {
404                 printk(KERN_ERR "%s: error accessing PCI regs\n", d->name);
405                 goto out;
406         }
407         if (!(pcicmd & PCI_COMMAND_IO)) {       /* is device disabled? */
408                 ret = ide_pci_configure(dev, d);
409                 if (ret < 0)
410                         goto out;
411                 *config = 1;
412                 printk(KERN_INFO "%s: device enabled (Linux)\n", d->name);
413         }
414
415 out:
416         return ret;
417 }
418
419 /**
420  *      ide_pci_setup_ports     -       configure ports/devices on PCI IDE
421  *      @dev: PCI device
422  *      @d: IDE port info
423  *      @pciirq: IRQ line
424  *      @idx: ATA index table to update
425  *
426  *      Scan the interfaces attached to this device and do any
427  *      necessary per port setup. Attach the devices and ask the
428  *      generic DMA layer to do its work for us.
429  *
430  *      Normally called automaticall from do_ide_pci_setup_device,
431  *      but is also used directly as a helper function by some controllers
432  *      where the chipset setup is not the default PCI IDE one.
433  */
434
435 void ide_pci_setup_ports(struct pci_dev *dev, const struct ide_port_info *d, int pciirq, u8 *idx)
436 {
437         int channels = (d->host_flags & IDE_HFLAG_SINGLE) ? 1 : 2, port;
438         ide_hwif_t *hwif;
439         u8 tmp;
440
441         /*
442          * Set up the IDE ports
443          */
444
445         for (port = 0; port < channels; ++port) {
446                 const ide_pci_enablebit_t *e = &(d->enablebits[port]);
447
448                 if (e->reg && (pci_read_config_byte(dev, e->reg, &tmp) ||
449                     (tmp & e->mask) != e->val)) {
450                         printk(KERN_INFO "%s: IDE port disabled\n", d->name);
451                         continue;       /* port not enabled */
452                 }
453
454                 hwif = ide_hwif_configure(dev, d, port, pciirq);
455                 if (hwif == NULL)
456                         continue;
457
458                 *(idx + port) = hwif->index;
459         }
460 }
461 EXPORT_SYMBOL_GPL(ide_pci_setup_ports);
462
463 /*
464  * ide_setup_pci_device() looks at the primary/secondary interfaces
465  * on a PCI IDE device and, if they are enabled, prepares the IDE driver
466  * for use with them.  This generic code works for most PCI chipsets.
467  *
468  * One thing that is not standardized is the location of the
469  * primary/secondary interface "enable/disable" bits.  For chipsets that
470  * we "know" about, this information is in the struct ide_port_info;
471  * for all other chipsets, we just assume both interfaces are enabled.
472  */
473 static int do_ide_setup_pci_device(struct pci_dev *dev,
474                                    const struct ide_port_info *d,
475                                    u8 *idx, u8 noisy)
476 {
477         int tried_config = 0;
478         int pciirq, ret;
479
480         ret = ide_setup_pci_controller(dev, d, noisy, &tried_config);
481         if (ret < 0)
482                 goto out;
483
484         /*
485          * Can we trust the reported IRQ?
486          */
487         pciirq = dev->irq;
488
489         /* Is it an "IDE storage" device in non-PCI mode? */
490         if ((dev->class >> 8) == PCI_CLASS_STORAGE_IDE && (dev->class & 5) != 5) {
491                 if (noisy)
492                         printk(KERN_INFO "%s: not 100%% native mode: "
493                                 "will probe irqs later\n", d->name);
494                 /*
495                  * This allows offboard ide-pci cards the enable a BIOS,
496                  * verify interrupt settings of split-mirror pci-config
497                  * space, place chipset into init-mode, and/or preserve
498                  * an interrupt if the card is not native ide support.
499                  */
500                 ret = d->init_chipset ? d->init_chipset(dev, d->name) : 0;
501                 if (ret < 0)
502                         goto out;
503                 pciirq = ret;
504         } else if (tried_config) {
505                 if (noisy)
506                         printk(KERN_INFO "%s: will probe irqs later\n", d->name);
507                 pciirq = 0;
508         } else if (!pciirq) {
509                 if (noisy)
510                         printk(KERN_WARNING "%s: bad irq (%d): will probe later\n",
511                                 d->name, pciirq);
512                 pciirq = 0;
513         } else {
514                 if (d->init_chipset) {
515                         ret = d->init_chipset(dev, d->name);
516                         if (ret < 0)
517                                 goto out;
518                 }
519                 if (noisy)
520                         printk(KERN_INFO "%s: 100%% native mode on irq %d\n",
521                                 d->name, pciirq);
522         }
523
524         /* FIXME: silent failure can happen */
525
526         ide_pci_setup_ports(dev, d, pciirq, idx);
527 out:
528         return ret;
529 }
530
531 int ide_setup_pci_device(struct pci_dev *dev, const struct ide_port_info *d)
532 {
533         u8 idx[4] = { 0xff, 0xff, 0xff, 0xff };
534         int ret;
535
536         ret = do_ide_setup_pci_device(dev, d, &idx[0], 1);
537
538         if (ret >= 0)
539                 ide_device_add(idx, d);
540
541         return ret;
542 }
543 EXPORT_SYMBOL_GPL(ide_setup_pci_device);
544
545 int ide_setup_pci_devices(struct pci_dev *dev1, struct pci_dev *dev2,
546                           const struct ide_port_info *d)
547 {
548         struct pci_dev *pdev[] = { dev1, dev2 };
549         int ret, i;
550         u8 idx[4] = { 0xff, 0xff, 0xff, 0xff };
551
552         for (i = 0; i < 2; i++) {
553                 ret = do_ide_setup_pci_device(pdev[i], d, &idx[i*2], !i);
554                 /*
555                  * FIXME: Mom, mom, they stole me the helper function to undo
556                  * do_ide_setup_pci_device() on the first device!
557                  */
558                 if (ret < 0)
559                         goto out;
560         }
561
562         ide_device_add(idx, d);
563 out:
564         return ret;
565 }
566 EXPORT_SYMBOL_GPL(ide_setup_pci_devices);