pci-adm5120.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. /*
  2. * ADM5120 PCI Host Controller driver
  3. *
  4. * Copyright (C) 2007-2008 Gabor Juhos <juhosg@openwrt.org>
  5. *
  6. * This code was based on the ADM5120 specific port of the Linux 2.6.10 kernel
  7. * done by Jeroen Vreeken
  8. * Copyright (C) 2005 Jeroen Vreeken (pe1rxq@amsat.org)
  9. *
  10. * Jeroen's code was based on the Linux 2.4.xx source codes found in various
  11. * tarballs released by Edimax for it's ADM5120 based devices
  12. * Copyright (C) ADMtek Incorporated
  13. *
  14. * This program is free software; you can redistribute it and/or modify it
  15. * under the terms of the GNU General Public License version 2 as published
  16. * by the Free Software Foundation.
  17. *
  18. */
  19. #include <linux/types.h>
  20. #include <linux/kernel.h>
  21. #include <linux/init.h>
  22. #include <linux/spinlock.h>
  23. #include <linux/io.h>
  24. #include <linux/delay.h>
  25. #include <linux/pci.h>
  26. #include <linux/pci_ids.h>
  27. #include <linux/pci_regs.h>
  28. #include <asm/bootinfo.h>
  29. #include <asm/mach-adm5120/adm5120_defs.h>
  30. #include <asm/mach-adm5120/adm5120_info.h>
  31. #include <asm/mach-adm5120/adm5120_defs.h>
  32. #include <asm/mach-adm5120/adm5120_platform.h>
  33. #undef DEBUG
  34. #ifdef DEBUG
  35. #define DBG(f, a...) printk(KERN_DEBUG f, ## a)
  36. #else
  37. #define DBG(f, a...) do {} while (0)
  38. #endif
  39. #define PCI_ENABLE 0x80000000
  40. /* -------------------------------------------------------------------------*/
  41. static unsigned int adm5120_pci_nr_irqs __initdata;
  42. static struct adm5120_pci_irq *adm5120_pci_irq_map __initdata;
  43. static DEFINE_SPINLOCK(pci_lock);
  44. /* -------------------------------------------------------------------------*/
  45. static inline void write_cfgaddr(u32 addr)
  46. {
  47. __raw_writel((addr | PCI_ENABLE),
  48. (void __iomem *)(KSEG1ADDR(ADM5120_PCICFG_ADDR)));
  49. }
  50. static inline void write_cfgdata(u32 data)
  51. {
  52. __raw_writel(data, (void __iomem *)KSEG1ADDR(ADM5120_PCICFG_DATA));
  53. }
  54. static inline u32 read_cfgdata(void)
  55. {
  56. return __raw_readl((void __iomem *)KSEG1ADDR(ADM5120_PCICFG_DATA));
  57. }
  58. static inline u32 mkaddr(struct pci_bus *bus, unsigned int devfn, int where)
  59. {
  60. return ((bus->number & 0xFF) << 16) | ((devfn & 0xFF) << 8) | \
  61. (where & 0xFC);
  62. }
  63. /* -------------------------------------------------------------------------*/
  64. static int pci_config_read(struct pci_bus *bus, unsigned int devfn, int where,
  65. int size, u32 *val)
  66. {
  67. unsigned long flags;
  68. u32 data;
  69. spin_lock_irqsave(&pci_lock, flags);
  70. write_cfgaddr(mkaddr(bus, devfn, where));
  71. data = read_cfgdata();
  72. DBG("PCI: cfg_read %02u.%02u.%01u/%02X:%01d, cfg:0x%08X",
  73. bus->number, PCI_SLOT(devfn), PCI_FUNC(devfn),
  74. where, size, data);
  75. switch (size) {
  76. case 1:
  77. if (where & 1)
  78. data >>= 8;
  79. if (where & 2)
  80. data >>= 16;
  81. data &= 0xFF;
  82. break;
  83. case 2:
  84. if (where & 2)
  85. data >>= 16;
  86. data &= 0xFFFF;
  87. break;
  88. }
  89. *val = data;
  90. DBG(", 0x%08X returned\n", data);
  91. spin_unlock_irqrestore(&pci_lock, flags);
  92. return PCIBIOS_SUCCESSFUL;
  93. }
  94. static int pci_config_write(struct pci_bus *bus, unsigned int devfn, int where,
  95. int size, u32 val)
  96. {
  97. unsigned long flags;
  98. u32 data;
  99. int s;
  100. spin_lock_irqsave(&pci_lock, flags);
  101. write_cfgaddr(mkaddr(bus, devfn, where));
  102. data = read_cfgdata();
  103. DBG("PCI: cfg_write %02u.%02u.%01u/%02X:%01d, cfg:0x%08X",
  104. bus->number, PCI_SLOT(devfn), PCI_FUNC(devfn),
  105. where, size, data);
  106. switch (size) {
  107. case 1:
  108. s = ((where & 3) << 3);
  109. data &= ~(0xFF << s);
  110. data |= ((val & 0xFF) << s);
  111. break;
  112. case 2:
  113. s = ((where & 2) << 4);
  114. data &= ~(0xFFFF << s);
  115. data |= ((val & 0xFFFF) << s);
  116. break;
  117. case 4:
  118. data = val;
  119. break;
  120. }
  121. write_cfgdata(data);
  122. DBG(", 0x%08X written\n", data);
  123. spin_unlock_irqrestore(&pci_lock, flags);
  124. return PCIBIOS_SUCCESSFUL;
  125. }
  126. struct pci_ops adm5120_pci_ops = {
  127. .read = pci_config_read,
  128. .write = pci_config_write,
  129. };
  130. /* -------------------------------------------------------------------------*/
  131. static void adm5120_pci_fixup(struct pci_dev *dev)
  132. {
  133. if (dev->devfn != 0)
  134. return;
  135. /* setup COMMAND register */
  136. pci_write_config_word(dev, PCI_COMMAND,
  137. (PCI_COMMAND_IO | PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER));
  138. /* setup CACHE_LINE_SIZE register */
  139. pci_write_config_byte(dev, PCI_CACHE_LINE_SIZE, 4);
  140. /* setup BARS */
  141. pci_write_config_dword(dev, PCI_BASE_ADDRESS_0, 0);
  142. pci_write_config_dword(dev, PCI_BASE_ADDRESS_1, 0);
  143. }
  144. DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_ADMTEK, PCI_DEVICE_ID_ADMTEK_ADM5120,
  145. adm5120_pci_fixup);
  146. /* -------------------------------------------------------------------------*/
  147. void __init adm5120_pci_set_irq_map(unsigned int nr_irqs,
  148. struct adm5120_pci_irq *map)
  149. {
  150. adm5120_pci_nr_irqs = nr_irqs;
  151. adm5120_pci_irq_map = map;
  152. }
  153. int __init pcibios_map_irq(const struct pci_dev *dev, u8 slot, u8 pin)
  154. {
  155. int irq = -1;
  156. int i;
  157. if ((!adm5120_pci_nr_irqs) || (!adm5120_pci_irq_map)) {
  158. printk(KERN_ALERT "PCI: pci_irq_map is not initialized\n");
  159. goto out;
  160. }
  161. if (slot < 1 || slot > 4) {
  162. printk(KERN_ALERT "PCI: slot number %u is not supported\n",
  163. slot);
  164. goto out;
  165. }
  166. for (i = 0; i < adm5120_pci_nr_irqs; i++) {
  167. if ((adm5120_pci_irq_map[i].slot == slot)
  168. && (adm5120_pci_irq_map[i].func == PCI_FUNC(dev->devfn))
  169. && (adm5120_pci_irq_map[i].pin == pin)) {
  170. irq = adm5120_pci_irq_map[i].irq;
  171. break;
  172. }
  173. }
  174. if (irq < 0) {
  175. printk(KERN_ALERT "PCI: no irq found for %s pin:%u\n",
  176. pci_name((struct pci_dev *)dev), pin);
  177. } else {
  178. printk(KERN_INFO "PCI: mapping irq for %s pin:%u, irq:%d\n",
  179. pci_name((struct pci_dev *)dev), pin, irq);
  180. }
  181. out:
  182. return irq;
  183. }
  184. int pcibios_plat_dev_init(struct pci_dev *dev)
  185. {
  186. return 0;
  187. }
  188. /* -------------------------------------------------------------------------*/
  189. static struct resource pci_io_resource = {
  190. .name = "ADM5120 PCI I/O",
  191. .start = ADM5120_PCIIO_BASE,
  192. .end = ADM5120_PCICFG_ADDR-1,
  193. .flags = IORESOURCE_IO
  194. };
  195. static struct resource pci_mem_resource = {
  196. .name = "ADM5120 PCI MEM",
  197. .start = ADM5120_PCIMEM_BASE,
  198. .end = ADM5120_PCIIO_BASE-1,
  199. .flags = IORESOURCE_MEM
  200. };
  201. static struct pci_controller adm5120_controller = {
  202. .pci_ops = &adm5120_pci_ops,
  203. .io_resource = &pci_io_resource,
  204. .mem_resource = &pci_mem_resource,
  205. };
  206. static int __init adm5120_pci_setup(void)
  207. {
  208. if (adm5120_package_pqfp()) {
  209. printk(KERN_INFO "PCI: not available on ADM5120P\n");
  210. return -1;
  211. }
  212. /* Avoid ISA compat ranges. */
  213. PCIBIOS_MIN_IO = 0x00000000;
  214. PCIBIOS_MIN_MEM = 0x00000000;
  215. /* Set I/O resource limits. */
  216. ioport_resource.end = 0x1fffffff;
  217. iomem_resource.end = 0xffffffff;
  218. register_pci_controller(&adm5120_controller);
  219. return 0;
  220. }
  221. arch_initcall(adm5120_pci_setup);