Subversion Repositories Kolibri OS

Rev

Rev 1963 | Rev 2160 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1.  
  2. #include <linux/kernel.h>
  3. #include <linux/mutex.h>
  4. #include <linux/mod_devicetable.h>
  5. #include <pci.h>
  6. #include <errno-base.h>
  7. #include <syscall.h>
  8.  
  9. static LIST_HEAD(devices);
  10.  
  11. static pci_dev_t* pci_scan_device(u32_t bus, int devfn);
  12.  
  13.  
  14. /* PCI control bits.  Shares IORESOURCE_BITS with above PCI ROM.  */
  15. #define IORESOURCE_PCI_FIXED            (1<<4)  /* Do not move resource */
  16.  
  17. #define LEGACY_IO_RESOURCE      (IORESOURCE_IO | IORESOURCE_PCI_FIXED)
  18.  
  19. /*
  20.  * Translate the low bits of the PCI base
  21.  * to the resource type
  22.  */
  23. static inline unsigned int pci_calc_resource_flags(unsigned int flags)
  24. {
  25.     if (flags & PCI_BASE_ADDRESS_SPACE_IO)
  26.         return IORESOURCE_IO;
  27.  
  28.     if (flags & PCI_BASE_ADDRESS_MEM_PREFETCH)
  29.         return IORESOURCE_MEM | IORESOURCE_PREFETCH;
  30.  
  31.     return IORESOURCE_MEM;
  32. }
  33.  
  34.  
  35. static u32_t pci_size(u32_t base, u32_t maxbase, u32_t mask)
  36. {
  37.     u32_t size = mask & maxbase;      /* Find the significant bits */
  38.  
  39.     if (!size)
  40.         return 0;
  41.  
  42.     /* Get the lowest of them to find the decode size, and
  43.        from that the extent.  */
  44.     size = (size & ~(size-1)) - 1;
  45.  
  46.     /* base == maxbase can be valid only if the BAR has
  47.        already been programmed with all 1s.  */
  48.     if (base == maxbase && ((base | size) & mask) != mask)
  49.         return 0;
  50.  
  51.     return size;
  52. }
  53.  
  54. static u64_t pci_size64(u64_t base, u64_t maxbase, u64_t mask)
  55. {
  56.     u64_t size = mask & maxbase;      /* Find the significant bits */
  57.  
  58.     if (!size)
  59.         return 0;
  60.  
  61.     /* Get the lowest of them to find the decode size, and
  62.        from that the extent.  */
  63.     size = (size & ~(size-1)) - 1;
  64.  
  65.     /* base == maxbase can be valid only if the BAR has
  66.        already been programmed with all 1s.  */
  67.     if (base == maxbase && ((base | size) & mask) != mask)
  68.         return 0;
  69.  
  70.     return size;
  71. }
  72.  
  73. static inline int is_64bit_memory(u32_t mask)
  74. {
  75.     if ((mask & (PCI_BASE_ADDRESS_SPACE|PCI_BASE_ADDRESS_MEM_TYPE_MASK)) ==
  76.         (PCI_BASE_ADDRESS_SPACE_MEMORY|PCI_BASE_ADDRESS_MEM_TYPE_64))
  77.         return 1;
  78.     return 0;
  79. }
  80.  
  81. static void pci_read_bases(struct pci_dev *dev, unsigned int howmany, int rom)
  82. {
  83.     u32_t  pos, reg, next;
  84.     u32_t  l, sz;
  85.     struct resource *res;
  86.  
  87.     for(pos=0; pos < howmany; pos = next)
  88.     {
  89.         u64_t  l64;
  90.         u64_t  sz64;
  91.         u32_t  raw_sz;
  92.  
  93.         next = pos + 1;
  94.  
  95.         res  = &dev->resource[pos];
  96.  
  97.         reg = PCI_BASE_ADDRESS_0 + (pos << 2);
  98.         l = PciRead32(dev->bus, dev->devfn, reg);
  99.         PciWrite32(dev->bus, dev->devfn, reg, ~0);
  100.         sz = PciRead32(dev->bus, dev->devfn, reg);
  101.         PciWrite32(dev->bus, dev->devfn, reg, l);
  102.  
  103.         if (!sz || sz == 0xffffffff)
  104.             continue;
  105.  
  106.         if (l == 0xffffffff)
  107.             l = 0;
  108.  
  109.         raw_sz = sz;
  110.         if ((l & PCI_BASE_ADDRESS_SPACE) ==
  111.                         PCI_BASE_ADDRESS_SPACE_MEMORY)
  112.         {
  113.             sz = pci_size(l, sz, (u32_t)PCI_BASE_ADDRESS_MEM_MASK);
  114.             /*
  115.              * For 64bit prefetchable memory sz could be 0, if the
  116.              * real size is bigger than 4G, so we need to check
  117.              * szhi for that.
  118.              */
  119.             if (!is_64bit_memory(l) && !sz)
  120.                     continue;
  121.             res->start = l & PCI_BASE_ADDRESS_MEM_MASK;
  122.             res->flags |= l & ~PCI_BASE_ADDRESS_MEM_MASK;
  123.         }
  124.         else {
  125.             sz = pci_size(l, sz, PCI_BASE_ADDRESS_IO_MASK & 0xffff);
  126.             if (!sz)
  127.                 continue;
  128.             res->start = l & PCI_BASE_ADDRESS_IO_MASK;
  129.             res->flags |= l & ~PCI_BASE_ADDRESS_IO_MASK;
  130.         }
  131.         res->end = res->start + (unsigned long) sz;
  132.         res->flags |= pci_calc_resource_flags(l);
  133.         if (is_64bit_memory(l))
  134.         {
  135.             u32_t szhi, lhi;
  136.  
  137.             lhi = PciRead32(dev->bus, dev->devfn, reg+4);
  138.             PciWrite32(dev->bus, dev->devfn, reg+4, ~0);
  139.             szhi = PciRead32(dev->bus, dev->devfn, reg+4);
  140.             PciWrite32(dev->bus, dev->devfn, reg+4, lhi);
  141.             sz64 = ((u64_t)szhi << 32) | raw_sz;
  142.             l64 = ((u64_t)lhi << 32) | l;
  143.             sz64 = pci_size64(l64, sz64, PCI_BASE_ADDRESS_MEM_MASK);
  144.             next++;
  145.  
  146. #if BITS_PER_LONG == 64
  147.             if (!sz64) {
  148.                 res->start = 0;
  149.                 res->end = 0;
  150.                 res->flags = 0;
  151.                 continue;
  152.             }
  153.             res->start = l64 & PCI_BASE_ADDRESS_MEM_MASK;
  154.             res->end = res->start + sz64;
  155. #else
  156.             if (sz64 > 0x100000000ULL) {
  157.                 printk(KERN_ERR "PCI: Unable to handle 64-bit "
  158.                                 "BAR for device %s\n", pci_name(dev));
  159.                 res->start = 0;
  160.                 res->flags = 0;
  161.             }
  162.             else if (lhi)
  163.             {
  164.                 /* 64-bit wide address, treat as disabled */
  165.                 PciWrite32(dev->bus, dev->devfn, reg,
  166.                         l & ~(u32_t)PCI_BASE_ADDRESS_MEM_MASK);
  167.                 PciWrite32(dev->bus, dev->devfn, reg+4, 0);
  168.                 res->start = 0;
  169.                 res->end = sz;
  170.             }
  171. #endif
  172.         }
  173.     }
  174.  
  175.     if ( rom )
  176.     {
  177.         dev->rom_base_reg = rom;
  178.         res = &dev->resource[PCI_ROM_RESOURCE];
  179.  
  180.         l = PciRead32(dev->bus, dev->devfn, rom);
  181.         PciWrite32(dev->bus, dev->devfn, rom, ~PCI_ROM_ADDRESS_ENABLE);
  182.         sz = PciRead32(dev->bus, dev->devfn, rom);
  183.         PciWrite32(dev->bus, dev->devfn, rom, l);
  184.  
  185.         if (l == 0xffffffff)
  186.             l = 0;
  187.  
  188.         if (sz && sz != 0xffffffff)
  189.         {
  190.             sz = pci_size(l, sz, (u32_t)PCI_ROM_ADDRESS_MASK);
  191.  
  192.             if (sz)
  193.             {
  194.                 res->flags = (l & IORESOURCE_ROM_ENABLE) |
  195.                                   IORESOURCE_MEM | IORESOURCE_PREFETCH |
  196.                                   IORESOURCE_READONLY | IORESOURCE_CACHEABLE;
  197.                 res->start = l & PCI_ROM_ADDRESS_MASK;
  198.                 res->end = res->start + (unsigned long) sz;
  199.             }
  200.         }
  201.     }
  202. }
  203.  
  204. static void pci_read_irq(struct pci_dev *dev)
  205. {
  206.     u8_t irq;
  207.  
  208.     irq = PciRead8(dev->bus, dev->devfn, PCI_INTERRUPT_PIN);
  209.     dev->pin = irq;
  210.     if (irq)
  211.         irq = PciRead8(dev->bus, dev->devfn, PCI_INTERRUPT_LINE);
  212.     dev->irq = irq;
  213. };
  214.  
  215.  
  216. static int pci_setup_device(struct pci_dev *dev)
  217. {
  218.     u32_t  class;
  219.  
  220.     class = PciRead32(dev->bus, dev->devfn, PCI_CLASS_REVISION);
  221.     dev->revision = class & 0xff;
  222.     class >>= 8;                                /* upper 3 bytes */
  223.     dev->class = class;
  224.  
  225.     /* "Unknown power state" */
  226. //    dev->current_state = PCI_UNKNOWN;
  227.  
  228.     /* Early fixups, before probing the BARs */
  229.  //   pci_fixup_device(pci_fixup_early, dev);
  230.     class = dev->class >> 8;
  231.  
  232.     switch (dev->hdr_type)
  233.     {
  234.         case PCI_HEADER_TYPE_NORMAL:                /* standard header */
  235.             if (class == PCI_CLASS_BRIDGE_PCI)
  236.                 goto bad;
  237.             pci_read_irq(dev);
  238.             pci_read_bases(dev, 6, PCI_ROM_ADDRESS);
  239.             dev->subsystem_vendor = PciRead16(dev->bus, dev->devfn,PCI_SUBSYSTEM_VENDOR_ID);
  240.             dev->subsystem_device = PciRead16(dev->bus, dev->devfn, PCI_SUBSYSTEM_ID);
  241.  
  242.             /*
  243.              *      Do the ugly legacy mode stuff here rather than broken chip
  244.              *      quirk code. Legacy mode ATA controllers have fixed
  245.              *      addresses. These are not always echoed in BAR0-3, and
  246.              *      BAR0-3 in a few cases contain junk!
  247.              */
  248.             if (class == PCI_CLASS_STORAGE_IDE)
  249.             {
  250.                 u8_t progif;
  251.  
  252.                 progif = PciRead8(dev->bus, dev->devfn,PCI_CLASS_PROG);
  253.                 if ((progif & 1) == 0)
  254.                 {
  255.                     dev->resource[0].start = 0x1F0;
  256.                     dev->resource[0].end = 0x1F7;
  257.                     dev->resource[0].flags = LEGACY_IO_RESOURCE;
  258.                     dev->resource[1].start = 0x3F6;
  259.                     dev->resource[1].end = 0x3F6;
  260.                     dev->resource[1].flags = LEGACY_IO_RESOURCE;
  261.                 }
  262.                 if ((progif & 4) == 0)
  263.                 {
  264.                     dev->resource[2].start = 0x170;
  265.                     dev->resource[2].end = 0x177;
  266.                     dev->resource[2].flags = LEGACY_IO_RESOURCE;
  267.                     dev->resource[3].start = 0x376;
  268.                     dev->resource[3].end = 0x376;
  269.                     dev->resource[3].flags = LEGACY_IO_RESOURCE;
  270.                 };
  271.             }
  272.             break;
  273.  
  274.         case PCI_HEADER_TYPE_BRIDGE:                /* bridge header */
  275.                 if (class != PCI_CLASS_BRIDGE_PCI)
  276.                         goto bad;
  277.                 /* The PCI-to-PCI bridge spec requires that subtractive
  278.                    decoding (i.e. transparent) bridge must have programming
  279.                    interface code of 0x01. */
  280.                 pci_read_irq(dev);
  281.                 dev->transparent = ((dev->class & 0xff) == 1);
  282.                 pci_read_bases(dev, 2, PCI_ROM_ADDRESS1);
  283.                 break;
  284.  
  285.         case PCI_HEADER_TYPE_CARDBUS:               /* CardBus bridge header */
  286.                 if (class != PCI_CLASS_BRIDGE_CARDBUS)
  287.                         goto bad;
  288.                 pci_read_irq(dev);
  289.                 pci_read_bases(dev, 1, 0);
  290.                 dev->subsystem_vendor = PciRead16(dev->bus,
  291.                                                   dev->devfn,
  292.                                                   PCI_CB_SUBSYSTEM_VENDOR_ID);
  293.  
  294.                 dev->subsystem_device = PciRead16(dev->bus,
  295.                                                   dev->devfn,
  296.                                                   PCI_CB_SUBSYSTEM_ID);
  297.                 break;
  298.  
  299.         default:                                    /* unknown header */
  300.                 printk(KERN_ERR "PCI: device %s has unknown header type %02x, ignoring.\n",
  301.                         pci_name(dev), dev->hdr_type);
  302.                 return -1;
  303.  
  304.         bad:
  305.                 printk(KERN_ERR "PCI: %s: class %x doesn't match header type %02x. Ignoring class.\n",
  306.                        pci_name(dev), class, dev->hdr_type);
  307.                 dev->class = PCI_CLASS_NOT_DEFINED;
  308.     }
  309.  
  310.     /* We found a fine healthy device, go go go... */
  311.  
  312.     return 0;
  313. };
  314.  
  315. static pci_dev_t* pci_scan_device(u32_t bus, int devfn)
  316. {
  317.     pci_dev_t  *dev;
  318.  
  319.     u32_t   id;
  320.     u8_t    hdr;
  321.  
  322.     int     timeout = 10;
  323.  
  324.     id = PciRead32(bus,devfn, PCI_VENDOR_ID);
  325.  
  326.     /* some broken boards return 0 or ~0 if a slot is empty: */
  327.     if (id == 0xffffffff || id == 0x00000000 ||
  328.         id == 0x0000ffff || id == 0xffff0000)
  329.         return NULL;
  330.  
  331.     while (id == 0xffff0001)
  332.     {
  333.  
  334.         delay(timeout/10);
  335.         timeout *= 2;
  336.  
  337.         id = PciRead32(bus, devfn, PCI_VENDOR_ID);
  338.  
  339.         /* Card hasn't responded in 60 seconds?  Must be stuck. */
  340.         if (timeout > 60 * 100)
  341.         {
  342.             printk(KERN_WARNING "Device %04x:%02x:%02x.%d not "
  343.                    "responding\n", bus,PCI_SLOT(devfn),PCI_FUNC(devfn));
  344.             return NULL;
  345.         }
  346.     };
  347.  
  348.     hdr = PciRead8(bus, devfn, PCI_HEADER_TYPE);
  349.  
  350.     dev = (pci_dev_t*)kzalloc(sizeof(pci_dev_t), 0);
  351.  
  352.     INIT_LIST_HEAD(&dev->link);
  353.  
  354.     if(unlikely(dev == NULL))
  355.         return NULL;
  356.  
  357.     dev->pci_dev.bus      = bus;
  358.     dev->pci_dev.devfn    = devfn;
  359.     dev->pci_dev.hdr_type = hdr & 0x7f;
  360.     dev->pci_dev.multifunction    = !!(hdr & 0x80);
  361.     dev->pci_dev.vendor   = id & 0xffff;
  362.     dev->pci_dev.device   = (id >> 16) & 0xffff;
  363.  
  364.     pci_setup_device(&dev->pci_dev);
  365.  
  366.     return dev;
  367.  
  368. };
  369.  
  370. int pci_scan_slot(u32_t bus, int devfn)
  371. {
  372.     int  func, nr = 0;
  373.  
  374.     for (func = 0; func < 8; func++, devfn++)
  375.     {
  376.         pci_dev_t  *dev;
  377.  
  378.         dev = pci_scan_device(bus, devfn);
  379.         if( dev )
  380.         {
  381.             list_add(&dev->link, &devices);
  382.  
  383.             nr++;
  384.  
  385.             /*
  386.              * If this is a single function device,
  387.              * don't scan past the first function.
  388.              */
  389.             if (!dev->pci_dev.multifunction)
  390.             {
  391.                 if (func > 0) {
  392.                     dev->pci_dev.multifunction = 1;
  393.                 }
  394.                 else {
  395.                     break;
  396.                 }
  397.              }
  398.         }
  399.         else {
  400.             if (func == 0)
  401.                 break;
  402.         }
  403.     };
  404.  
  405.     return nr;
  406. };
  407.  
  408.  
  409. void pci_scan_bus(u32_t bus)
  410. {
  411.     u32_t devfn;
  412.     pci_dev_t *dev;
  413.  
  414.  
  415.     for (devfn = 0; devfn < 0x100; devfn += 8)
  416.         pci_scan_slot(bus, devfn);
  417.  
  418. }
  419.  
  420. int enum_pci_devices()
  421. {
  422.     pci_dev_t  *dev;
  423.     u32_t       last_bus;
  424.     u32_t       bus = 0 , devfn = 0;
  425.  
  426.   //  list_initialize(&devices);
  427.  
  428.     last_bus = PciApi(1);
  429.  
  430.  
  431.     if( unlikely(last_bus == -1))
  432.         return -1;
  433.  
  434.     for(;bus <= last_bus; bus++)
  435.         pci_scan_bus(bus);
  436.  
  437. //    for(dev = (dev_t*)devices.next;
  438. //        &dev->link != &devices;
  439. //        dev = (dev_t*)dev->link.next)
  440. //    {
  441. //        dbgprintf("PCI device %x:%x bus:%x devfn:%x\n",
  442. //                dev->pci_dev.vendor,
  443. //                dev->pci_dev.device,
  444. //                dev->pci_dev.bus,
  445. //                dev->pci_dev.devfn);
  446. //
  447. //    }
  448.     return 0;
  449. }
  450.  
  451. #define PCI_FIND_CAP_TTL    48
  452.  
  453. static int __pci_find_next_cap_ttl(unsigned int bus, unsigned int devfn,
  454.                    u8 pos, int cap, int *ttl)
  455. {
  456.     u8 id;
  457.  
  458.     while ((*ttl)--) {
  459.         pos = PciRead8(bus, devfn, pos);
  460.         if (pos < 0x40)
  461.             break;
  462.         pos &= ~3;
  463.         id = PciRead8(bus, devfn, pos + PCI_CAP_LIST_ID);
  464.         if (id == 0xff)
  465.             break;
  466.         if (id == cap)
  467.             return pos;
  468.         pos += PCI_CAP_LIST_NEXT;
  469.     }
  470.     return 0;
  471. }
  472.  
  473. static int __pci_find_next_cap(unsigned int bus, unsigned int devfn,
  474.                    u8 pos, int cap)
  475. {
  476.     int ttl = PCI_FIND_CAP_TTL;
  477.  
  478.     return __pci_find_next_cap_ttl(bus, devfn, pos, cap, &ttl);
  479. }
  480.  
  481. static int __pci_bus_find_cap_start(unsigned int bus,
  482.                     unsigned int devfn, u8 hdr_type)
  483. {
  484.     u16 status;
  485.  
  486.     status = PciRead16(bus, devfn, PCI_STATUS);
  487.     if (!(status & PCI_STATUS_CAP_LIST))
  488.         return 0;
  489.  
  490.     switch (hdr_type) {
  491.     case PCI_HEADER_TYPE_NORMAL:
  492.     case PCI_HEADER_TYPE_BRIDGE:
  493.         return PCI_CAPABILITY_LIST;
  494.     case PCI_HEADER_TYPE_CARDBUS:
  495.         return PCI_CB_CAPABILITY_LIST;
  496.     default:
  497.         return 0;
  498.     }
  499.  
  500.     return 0;
  501. }
  502.  
  503.  
  504. int pci_find_capability(struct pci_dev *dev, int cap)
  505. {
  506.     int pos;
  507.  
  508.     pos = __pci_bus_find_cap_start(dev->bus, dev->devfn, dev->hdr_type);
  509.     if (pos)
  510.         pos = __pci_find_next_cap(dev->bus, dev->devfn, pos, cap);
  511.  
  512.     return pos;
  513. }
  514.  
  515.  
  516. #if 0
  517. /**
  518.  * pci_set_power_state - Set the power state of a PCI device
  519.  * @dev: PCI device to be suspended
  520.  * @state: PCI power state (D0, D1, D2, D3hot, D3cold) we're entering
  521.  *
  522.  * Transition a device to a new power state, using the Power Management
  523.  * Capabilities in the device's config space.
  524.  *
  525.  * RETURN VALUE:
  526.  * -EINVAL if trying to enter a lower state than we're already in.
  527.  * 0 if we're already in the requested state.
  528.  * -EIO if device does not support PCI PM.
  529.  * 0 if we can successfully change the power state.
  530.  */
  531. int
  532. pci_set_power_state(struct pci_dev *dev, pci_power_t state)
  533. {
  534.         int pm, need_restore = 0;
  535.         u16 pmcsr, pmc;
  536.  
  537.         /* bound the state we're entering */
  538.         if (state > PCI_D3hot)
  539.                 state = PCI_D3hot;
  540.  
  541.         /*
  542.          * If the device or the parent bridge can't support PCI PM, ignore
  543.          * the request if we're doing anything besides putting it into D0
  544.          * (which would only happen on boot).
  545.          */
  546.         if ((state == PCI_D1 || state == PCI_D2) && pci_no_d1d2(dev))
  547.                 return 0;
  548.  
  549.         /* find PCI PM capability in list */
  550.         pm = pci_find_capability(dev, PCI_CAP_ID_PM);
  551.  
  552.         /* abort if the device doesn't support PM capabilities */
  553.         if (!pm)
  554.                 return -EIO;
  555.  
  556.         /* Validate current state:
  557.          * Can enter D0 from any state, but if we can only go deeper
  558.          * to sleep if we're already in a low power state
  559.          */
  560.         if (state != PCI_D0 && dev->current_state > state) {
  561.                 printk(KERN_ERR "%s(): %s: state=%d, current state=%d\n",
  562.                         __FUNCTION__, pci_name(dev), state, dev->current_state);
  563.                 return -EINVAL;
  564.         } else if (dev->current_state == state)
  565.                 return 0;        /* we're already there */
  566.  
  567.  
  568.         pci_read_config_word(dev,pm + PCI_PM_PMC,&pmc);
  569.         if ((pmc & PCI_PM_CAP_VER_MASK) > 3) {
  570.                 printk(KERN_DEBUG
  571.                        "PCI: %s has unsupported PM cap regs version (%u)\n",
  572.                        pci_name(dev), pmc & PCI_PM_CAP_VER_MASK);
  573.                 return -EIO;
  574.         }
  575.  
  576.         /* check if this device supports the desired state */
  577.         if (state == PCI_D1 && !(pmc & PCI_PM_CAP_D1))
  578.                 return -EIO;
  579.         else if (state == PCI_D2 && !(pmc & PCI_PM_CAP_D2))
  580.                 return -EIO;
  581.  
  582.         pci_read_config_word(dev, pm + PCI_PM_CTRL, &pmcsr);
  583.  
  584.         /* If we're (effectively) in D3, force entire word to 0.
  585.          * This doesn't affect PME_Status, disables PME_En, and
  586.          * sets PowerState to 0.
  587.          */
  588.         switch (dev->current_state) {
  589.         case PCI_D0:
  590.         case PCI_D1:
  591.         case PCI_D2:
  592.                 pmcsr &= ~PCI_PM_CTRL_STATE_MASK;
  593.                 pmcsr |= state;
  594.                 break;
  595.         case PCI_UNKNOWN: /* Boot-up */
  596.                 if ((pmcsr & PCI_PM_CTRL_STATE_MASK) == PCI_D3hot
  597.                  && !(pmcsr & PCI_PM_CTRL_NO_SOFT_RESET))
  598.                         need_restore = 1;
  599.                 /* Fall-through: force to D0 */
  600.         default:
  601.                 pmcsr = 0;
  602.                 break;
  603.         }
  604.  
  605.         /* enter specified state */
  606.         pci_write_config_word(dev, pm + PCI_PM_CTRL, pmcsr);
  607.  
  608.         /* Mandatory power management transition delays */
  609.         /* see PCI PM 1.1 5.6.1 table 18 */
  610.         if (state == PCI_D3hot || dev->current_state == PCI_D3hot)
  611.                 msleep(pci_pm_d3_delay);
  612.         else if (state == PCI_D2 || dev->current_state == PCI_D2)
  613.                 udelay(200);
  614.  
  615.         /*
  616.          * Give firmware a chance to be called, such as ACPI _PRx, _PSx
  617.          * Firmware method after native method ?
  618.          */
  619.         if (platform_pci_set_power_state)
  620.                 platform_pci_set_power_state(dev, state);
  621.  
  622.         dev->current_state = state;
  623.  
  624.         /* According to section 5.4.1 of the "PCI BUS POWER MANAGEMENT
  625.          * INTERFACE SPECIFICATION, REV. 1.2", a device transitioning
  626.          * from D3hot to D0 _may_ perform an internal reset, thereby
  627.          * going to "D0 Uninitialized" rather than "D0 Initialized".
  628.          * For example, at least some versions of the 3c905B and the
  629.          * 3c556B exhibit this behaviour.
  630.          *
  631.          * At least some laptop BIOSen (e.g. the Thinkpad T21) leave
  632.          * devices in a D3hot state at boot.  Consequently, we need to
  633.          * restore at least the BARs so that the device will be
  634.          * accessible to its driver.
  635.          */
  636.         if (need_restore)
  637.                 pci_restore_bars(dev);
  638.  
  639.         return 0;
  640. }
  641. #endif
  642.  
  643. int pcibios_enable_resources(struct pci_dev *dev, int mask)
  644. {
  645.     u16_t cmd, old_cmd;
  646.     int  idx;
  647.     struct resource *r;
  648.  
  649.     cmd = PciRead16(dev->bus, dev->devfn, PCI_COMMAND);
  650.     old_cmd = cmd;
  651.     for (idx = 0; idx < PCI_NUM_RESOURCES; idx++)
  652.     {
  653.         /* Only set up the requested stuff */
  654.         if (!(mask & (1 << idx)))
  655.                 continue;
  656.  
  657.         r = &dev->resource[idx];
  658.         if (!(r->flags & (IORESOURCE_IO | IORESOURCE_MEM)))
  659.                 continue;
  660.         if ((idx == PCI_ROM_RESOURCE) &&
  661.                         (!(r->flags & IORESOURCE_ROM_ENABLE)))
  662.                 continue;
  663.         if (!r->start && r->end) {
  664.                 printk(KERN_ERR "PCI: Device %s not available "
  665.                         "because of resource %d collisions\n",
  666.                         pci_name(dev), idx);
  667.                 return -EINVAL;
  668.         }
  669.         if (r->flags & IORESOURCE_IO)
  670.                 cmd |= PCI_COMMAND_IO;
  671.         if (r->flags & IORESOURCE_MEM)
  672.                 cmd |= PCI_COMMAND_MEMORY;
  673.     }
  674.     if (cmd != old_cmd) {
  675.         printk("PCI: Enabling device %s (%04x -> %04x)\n",
  676.                 pci_name(dev), old_cmd, cmd);
  677.         PciWrite16(dev->bus, dev->devfn, PCI_COMMAND, cmd);
  678.     }
  679.     return 0;
  680. }
  681.  
  682.  
  683. int pcibios_enable_device(struct pci_dev *dev, int mask)
  684. {
  685.         int err;
  686.  
  687.         if ((err = pcibios_enable_resources(dev, mask)) < 0)
  688.                 return err;
  689.  
  690. //        if (!dev->msi_enabled)
  691. //                return pcibios_enable_irq(dev);
  692.         return 0;
  693. }
  694.  
  695.  
  696. static int do_pci_enable_device(struct pci_dev *dev, int bars)
  697. {
  698.         int err;
  699.  
  700. //        err = pci_set_power_state(dev, PCI_D0);
  701. //        if (err < 0 && err != -EIO)
  702. //                return err;
  703.         err = pcibios_enable_device(dev, bars);
  704. //        if (err < 0)
  705. //                return err;
  706. //        pci_fixup_device(pci_fixup_enable, dev);
  707.  
  708.         return 0;
  709. }
  710.  
  711.  
  712. static int __pci_enable_device_flags(struct pci_dev *dev,
  713.                                      resource_size_t flags)
  714. {
  715.         int err;
  716.         int i, bars = 0;
  717.  
  718. //        if (atomic_add_return(1, &dev->enable_cnt) > 1)
  719. //                return 0;               /* already enabled */
  720.  
  721.         for (i = 0; i < DEVICE_COUNT_RESOURCE; i++)
  722.                 if (dev->resource[i].flags & flags)
  723.                         bars |= (1 << i);
  724.  
  725.         err = do_pci_enable_device(dev, bars);
  726. //        if (err < 0)
  727. //                atomic_dec(&dev->enable_cnt);
  728.         return err;
  729. }
  730.  
  731.  
  732. /**
  733.  * pci_enable_device - Initialize device before it's used by a driver.
  734.  * @dev: PCI device to be initialized
  735.  *
  736.  *  Initialize device before it's used by a driver. Ask low-level code
  737.  *  to enable I/O and memory. Wake up the device if it was suspended.
  738.  *  Beware, this function can fail.
  739.  *
  740.  *  Note we don't actually enable the device many times if we call
  741.  *  this function repeatedly (we just increment the count).
  742.  */
  743. int pci_enable_device(struct pci_dev *dev)
  744. {
  745.         return __pci_enable_device_flags(dev, IORESOURCE_MEM | IORESOURCE_IO);
  746. }
  747.  
  748.  
  749.  
  750. struct pci_device_id* find_pci_device(pci_dev_t* pdev, struct pci_device_id *idlist)
  751. {
  752.     pci_dev_t *dev;
  753.     struct pci_device_id *ent;
  754.  
  755.     for(dev = (pci_dev_t*)devices.next;
  756.         &dev->link != &devices;
  757.         dev = (pci_dev_t*)dev->link.next)
  758.     {
  759.         if( dev->pci_dev.vendor != idlist->vendor )
  760.             continue;
  761.  
  762.         for(ent = idlist; ent->vendor != 0; ent++)
  763.         {
  764.             if(unlikely(ent->device == dev->pci_dev.device))
  765.             {
  766.                 pdev->pci_dev = dev->pci_dev;
  767.                 return  ent;
  768.             }
  769.         };
  770.     }
  771.  
  772.     return NULL;
  773. };
  774.  
  775.  
  776.  
  777. /**
  778.  * pci_map_rom - map a PCI ROM to kernel space
  779.  * @pdev: pointer to pci device struct
  780.  * @size: pointer to receive size of pci window over ROM
  781.  * @return: kernel virtual pointer to image of ROM
  782.  *
  783.  * Map a PCI ROM into kernel space. If ROM is boot video ROM,
  784.  * the shadow BIOS copy will be returned instead of the
  785.  * actual ROM.
  786.  */
  787.  
  788. #define legacyBIOSLocation 0xC0000
  789. #define OS_BASE   0x80000000
  790.  
  791. void *pci_map_rom(struct pci_dev *pdev, size_t *size)
  792. {
  793.     struct resource *res = &pdev->resource[PCI_ROM_RESOURCE];
  794.     u32_t start;
  795.     void  *rom;
  796.  
  797. #if 0
  798.     /*
  799.      * IORESOURCE_ROM_SHADOW set on x86, x86_64 and IA64 supports legacy
  800.      * memory map if the VGA enable bit of the Bridge Control register is
  801.      * set for embedded VGA.
  802.      */
  803.     if (res->flags & IORESOURCE_ROM_SHADOW) {
  804.         /* primary video rom always starts here */
  805.         start = (u32_t)0xC0000;
  806.         *size = 0x20000; /* cover C000:0 through E000:0 */
  807.     } else {
  808.         if (res->flags & (IORESOURCE_ROM_COPY | IORESOURCE_ROM_BIOS_COPY)) {
  809.             *size = pci_resource_len(pdev, PCI_ROM_RESOURCE);
  810.              return (void *)(unsigned long)
  811.                      pci_resource_start(pdev, PCI_ROM_RESOURCE);
  812.         } else {
  813.                 /* assign the ROM an address if it doesn't have one */
  814.             //if (res->parent == NULL &&
  815.             //     pci_assign_resource(pdev,PCI_ROM_RESOURCE))
  816.             //         return NULL;
  817.              start = pci_resource_start(pdev, PCI_ROM_RESOURCE);
  818.              *size = pci_resource_len(pdev, PCI_ROM_RESOURCE);
  819.              if (*size == 0)
  820.                      return NULL;
  821.  
  822.              /* Enable ROM space decodes */
  823.              if (pci_enable_rom(pdev))
  824.                      return NULL;
  825.         }
  826.     }
  827.  
  828.     rom = ioremap(start, *size);
  829.     if (!rom) {
  830.             /* restore enable if ioremap fails */
  831.             if (!(res->flags & (IORESOURCE_ROM_ENABLE |
  832.                                 IORESOURCE_ROM_SHADOW |
  833.                                 IORESOURCE_ROM_COPY)))
  834.                     pci_disable_rom(pdev);
  835.             return NULL;
  836.     }
  837.  
  838.     /*
  839.      * Try to find the true size of the ROM since sometimes the PCI window
  840.      * size is much larger than the actual size of the ROM.
  841.      * True size is important if the ROM is going to be copied.
  842.      */
  843.     *size = pci_get_rom_size(rom, *size);
  844.  
  845. #endif
  846.  
  847.     unsigned char tmp[32];
  848.     rom = NULL;
  849.  
  850.     dbgprintf("Getting BIOS copy from legacy VBIOS location\n");
  851.     memcpy(tmp,(char*)(OS_BASE+legacyBIOSLocation), 32);
  852.     *size = tmp[2] * 512;
  853.     if (*size > 0x10000 )
  854.     {
  855.         *size = 0;
  856.         dbgprintf("Invalid BIOS length field\n");
  857.     }
  858.     else
  859.         rom = (void*)( OS_BASE+legacyBIOSLocation);
  860.  
  861.     return rom;
  862. }
  863.  
  864.  
  865. int
  866. pci_set_dma_mask(struct pci_dev *dev, u64 mask)
  867. {
  868. //        if (!pci_dma_supported(dev, mask))
  869. //                return -EIO;
  870.  
  871.         dev->dma_mask = mask;
  872.  
  873.         return 0;
  874. }
  875.  
  876.  
  877.