pci-adm8668.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. /*
  2. * Copyright (C) 2010 Scott Nicholas <neutronscott@scottn.us>
  3. * Copyright (C) 2012 Florian Fainelli <florian@openwrt.org>
  4. *
  5. * This file is subject to the terms and conditions of the GNU General Public
  6. * License. See the file "COPYING" in the main directory of this archive
  7. * for more details.
  8. *
  9. * Note that this controller is identical to the ADM5120 one
  10. */
  11. #include <linux/kernel.h>
  12. #include <linux/init.h>
  13. #include <linux/pci.h>
  14. #include <linux/types.h>
  15. #include <linux/spinlock.h>
  16. #include <asm/byteorder.h>
  17. #include <asm/pci.h>
  18. #include <adm8668.h>
  19. static DEFINE_SPINLOCK(pci_lock);
  20. #define PCI_ENABLE 0x80000000
  21. #define ADMPCI_IO_BASE 0x12600000
  22. #define ADMPCI_IO_SIZE 0x1fffff
  23. #define ADMPCI_MEM_BASE 0x16000000
  24. #define ADMPCI_MEM_SIZE 0x7ffffff
  25. static inline void write_cfgaddr(u32 addr)
  26. {
  27. __raw_writel((addr | PCI_ENABLE),
  28. (void __iomem *)KSEG1ADDR(ADM8668_PCICFG_BASE));
  29. }
  30. static inline void write_cfgdata(u32 data)
  31. {
  32. __raw_writel(data, (void __iomem *)KSEG1ADDR(ADM8668_PCIDAT_BASE));
  33. }
  34. static inline u32 read_cfgdata(void)
  35. {
  36. return __raw_readl((void __iomem *)KSEG1ADDR(ADM8668_PCIDAT_BASE));
  37. }
  38. static inline u32 mkaddr(struct pci_bus *bus, unsigned int devfn, int where)
  39. {
  40. return ((bus->number & 0xff) << 16) | ((devfn & 0xff) << 8) |
  41. (where & 0xfc);
  42. }
  43. static int pci_read_config(struct pci_bus *bus, unsigned int devfn,
  44. int where, int size, u32 *val)
  45. {
  46. unsigned long flags;
  47. u32 data;
  48. spin_lock_irqsave(&pci_lock, flags);
  49. write_cfgaddr(mkaddr(bus, devfn, where));
  50. data = read_cfgdata();
  51. switch (size) {
  52. case 1:
  53. if (where & 1)
  54. data >>= 8;
  55. if (where & 2)
  56. data >>= 16;
  57. data &= 0xff;
  58. break;
  59. case 2:
  60. if (where & 2)
  61. data >>= 16;
  62. data &= 0xffff;
  63. break;
  64. }
  65. *val = data;
  66. spin_unlock_irqrestore(&pci_lock, flags);
  67. return PCIBIOS_SUCCESSFUL;
  68. }
  69. static int pci_write_config(struct pci_bus *bus, unsigned int devfn,
  70. int where, int size, u32 val)
  71. {
  72. unsigned long flags;
  73. u32 data;
  74. int s;
  75. spin_lock_irqsave(&pci_lock, flags);
  76. write_cfgaddr(mkaddr(bus, devfn, where));
  77. data = read_cfgdata();
  78. switch (size) {
  79. case 1:
  80. s = ((where & 3) << 3);
  81. data &= ~(0xff << s);
  82. data |= ((val & 0xff) << s);
  83. break;
  84. case 2:
  85. s = ((where & 2) << 4);
  86. data &= ~(0xffff << s);
  87. data |= ((val & 0xffff) << s);
  88. break;
  89. case 4:
  90. data = val;
  91. break;
  92. }
  93. write_cfgdata(data);
  94. spin_unlock_irqrestore(&pci_lock, flags);
  95. return PCIBIOS_SUCCESSFUL;
  96. }
  97. struct pci_ops adm8668_pci_ops = {
  98. .read = pci_read_config,
  99. .write = pci_write_config
  100. };
  101. struct resource pciioport_resource = {
  102. .name = "adm8668_pci",
  103. .start = ADMPCI_IO_BASE,
  104. .end = ADMPCI_IO_BASE + ADMPCI_IO_SIZE,
  105. .flags = IORESOURCE_IO
  106. };
  107. struct resource pciiomem_resource = {
  108. .name = "adm8668_pci",
  109. .start = ADMPCI_MEM_BASE,
  110. .end = ADMPCI_MEM_BASE + ADMPCI_MEM_SIZE,
  111. .flags = IORESOURCE_MEM
  112. };
  113. struct pci_controller adm8668_pci_controller = {
  114. .pci_ops = &adm8668_pci_ops,
  115. .io_resource = &pciioport_resource,
  116. .mem_resource = &pciiomem_resource,
  117. };
  118. int pcibios_map_irq(const struct pci_dev *dev, u8 slot, u8 pin)
  119. {
  120. switch (slot) {
  121. case 1:
  122. return 14;
  123. case 2:
  124. return 13;
  125. case 3:
  126. return 12;
  127. default:
  128. return dev->irq;
  129. }
  130. }
  131. int pcibios_plat_dev_init(struct pci_dev *dev)
  132. {
  133. return 0;
  134. }
  135. static void adm8668_pci_fixup(struct pci_dev *dev)
  136. {
  137. if (dev->devfn != 0)
  138. return;
  139. pr_info("PCI: fixing up ADM8668 controller\n");
  140. /* setup COMMAND register */
  141. pci_write_config_word(dev, PCI_COMMAND,
  142. (PCI_COMMAND_IO | PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER));
  143. /* setup CACHE_LINE_SIZE register */
  144. pci_write_config_byte(dev, PCI_CACHE_LINE_SIZE, 4);
  145. /* setup BARS */
  146. pci_write_config_dword(dev, PCI_BASE_ADDRESS_0, 0);
  147. pci_write_config_dword(dev, PCI_BASE_ADDRESS_1, 0);
  148. }
  149. DECLARE_PCI_FIXUP_HEADER(0x1317, 0x8688, adm8668_pci_fixup);
  150. static int __init adm8668_pci_init(void)
  151. {
  152. void __iomem *io_map_base;
  153. ioport_resource.start = ADMPCI_IO_BASE;
  154. ioport_resource.end = ADMPCI_IO_BASE + ADMPCI_IO_SIZE;
  155. io_map_base = ioremap(ADMPCI_IO_BASE, ADMPCI_IO_SIZE);
  156. if (!io_map_base)
  157. printk("io_map_base didn't work.\n");
  158. adm8668_pci_controller.io_map_base = (unsigned long)io_map_base;
  159. register_pci_controller(&adm8668_pci_controller);
  160. return 0;
  161. }
  162. arch_initcall(adm8668_pci_init);