Subversion Repositories Kolibri OS

Rev

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

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