pci.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. /*
  2. * Low-Level PCI Support for the MPC-1211(CTP/PCI/MPC-SH02)
  3. *
  4. * (c) 2002-2003 Saito.K & Jeanne
  5. *
  6. * Dustin McIntire (dustin@sensoria.com)
  7. * Derived from arch/i386/kernel/pci-*.c which bore the message:
  8. * (c) 1999--2000 Martin Mares <mj@ucw.cz>
  9. *
  10. * May be copied or modified under the terms of the GNU General Public
  11. * License. See linux/COPYING for more information.
  12. *
  13. */
  14. #include <linux/config.h>
  15. #include <linux/types.h>
  16. #include <linux/kernel.h>
  17. #include <linux/init.h>
  18. #include <linux/delay.h>
  19. #include <linux/pci.h>
  20. #include <linux/sched.h>
  21. #include <linux/ioport.h>
  22. #include <linux/errno.h>
  23. #include <linux/irq.h>
  24. #include <linux/interrupt.h>
  25. #include <asm/machvec.h>
  26. #include <asm/io.h>
  27. #include <asm/mpc1211/pci.h>
  28. static struct resource mpcpci_io_resource = {
  29. "MPCPCI IO",
  30. 0x00000000,
  31. 0xffffffff,
  32. IORESOURCE_IO
  33. };
  34. static struct resource mpcpci_mem_resource = {
  35. "MPCPCI mem",
  36. 0x00000000,
  37. 0xffffffff,
  38. IORESOURCE_MEM
  39. };
  40. static struct pci_ops pci_direct_conf1;
  41. struct pci_channel board_pci_channels[] = {
  42. {&pci_direct_conf1, &mpcpci_io_resource, &mpcpci_mem_resource, 0, 256},
  43. {NULL, NULL, NULL, 0, 0},
  44. };
  45. /*
  46. * Direct access to PCI hardware...
  47. */
  48. #define CONFIG_CMD(bus, devfn, where) (0x80000000 | (bus->number << 16) | (devfn << 8) | (where & ~3))
  49. /*
  50. * Functions for accessing PCI configuration space with type 1 accesses
  51. */
  52. static int pci_conf1_read(struct pci_bus *bus, unsigned int devfn, int where, int size, u32 *value)
  53. {
  54. u32 word;
  55. unsigned long flags;
  56. /*
  57. * PCIPDR may only be accessed as 32 bit words,
  58. * so we must do byte alignment by hand
  59. */
  60. local_irq_save(flags);
  61. writel(CONFIG_CMD(bus,devfn,where), PCIPAR);
  62. word = readl(PCIPDR);
  63. local_irq_restore(flags);
  64. switch (size) {
  65. case 1:
  66. switch (where & 0x3) {
  67. case 3:
  68. *value = (u8)(word >> 24);
  69. break;
  70. case 2:
  71. *value = (u8)(word >> 16);
  72. break;
  73. case 1:
  74. *value = (u8)(word >> 8);
  75. break;
  76. default:
  77. *value = (u8)word;
  78. break;
  79. }
  80. break;
  81. case 2:
  82. switch (where & 0x3) {
  83. case 3:
  84. *value = (u16)(word >> 24);
  85. local_irq_save(flags);
  86. writel(CONFIG_CMD(bus,devfn,(where+1)), PCIPAR);
  87. word = readl(PCIPDR);
  88. local_irq_restore(flags);
  89. *value |= ((word & 0xff) << 8);
  90. break;
  91. case 2:
  92. *value = (u16)(word >> 16);
  93. break;
  94. case 1:
  95. *value = (u16)(word >> 8);
  96. break;
  97. default:
  98. *value = (u16)word;
  99. break;
  100. }
  101. break;
  102. case 4:
  103. *value = word;
  104. break;
  105. }
  106. PCIDBG(4,"pci_conf1_read@0x%08x=0x%x\n", CONFIG_CMD(bus,devfn,where),*value);
  107. return PCIBIOS_SUCCESSFUL;
  108. }
  109. /*
  110. * Since MPC-1211 only does 32bit access we'll have to do a read,mask,write operation.
  111. * We'll allow an odd byte offset, though it should be illegal.
  112. */
  113. static int pci_conf1_write(struct pci_bus *bus, unsigned int devfn, int where, int size, u32 value)
  114. {
  115. u32 word,mask = 0;
  116. unsigned long flags;
  117. u32 shift = (where & 3) * 8;
  118. if(size == 1) {
  119. mask = ((1 << 8) - 1) << shift; // create the byte mask
  120. } else if(size == 2){
  121. if(shift == 24)
  122. return PCIBIOS_BAD_REGISTER_NUMBER;
  123. mask = ((1 << 16) - 1) << shift; // create the word mask
  124. }
  125. local_irq_save(flags);
  126. writel(CONFIG_CMD(bus,devfn,where), PCIPAR);
  127. if(size == 4){
  128. writel(value, PCIPDR);
  129. local_irq_restore(flags);
  130. PCIDBG(4,"pci_conf1_write@0x%08x=0x%x\n", CONFIG_CMD(bus,devfn,where),value);
  131. return PCIBIOS_SUCCESSFUL;
  132. }
  133. word = readl(PCIPDR);
  134. word &= ~mask;
  135. word |= ((value << shift) & mask);
  136. writel(word, PCIPDR);
  137. local_irq_restore(flags);
  138. PCIDBG(4,"pci_conf1_write@0x%08x=0x%x\n", CONFIG_CMD(bus,devfn,where),word);
  139. return PCIBIOS_SUCCESSFUL;
  140. }
  141. #undef CONFIG_CMD
  142. static struct pci_ops pci_direct_conf1 = {
  143. .read = pci_conf1_read,
  144. .write = pci_conf1_write,
  145. };
  146. static void __devinit quirk_ali_ide_ports(struct pci_dev *dev)
  147. {
  148. dev->resource[0].start = 0x1f0;
  149. dev->resource[0].end = 0x1f7;
  150. dev->resource[0].flags = IORESOURCE_IO;
  151. dev->resource[1].start = 0x3f6;
  152. dev->resource[1].end = 0x3f6;
  153. dev->resource[1].flags = IORESOURCE_IO;
  154. dev->resource[2].start = 0x170;
  155. dev->resource[2].end = 0x177;
  156. dev->resource[2].flags = IORESOURCE_IO;
  157. dev->resource[3].start = 0x376;
  158. dev->resource[3].end = 0x376;
  159. dev->resource[3].flags = IORESOURCE_IO;
  160. dev->resource[4].start = 0xf000;
  161. dev->resource[4].end = 0xf00f;
  162. dev->resource[4].flags = IORESOURCE_IO;
  163. }
  164. DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_AL, PCI_DEVICE_ID_AL_M5229, quirk_ali_ide_ports);
  165. char * __devinit pcibios_setup(char *str)
  166. {
  167. return str;
  168. }
  169. /*
  170. * Called after each bus is probed, but before its children
  171. * are examined.
  172. */
  173. void __init pcibios_fixup_bus(struct pci_bus *b)
  174. {
  175. pci_read_bridge_bases(b);
  176. }
  177. /*
  178. * IRQ functions
  179. */
  180. static inline u8 bridge_swizzle(u8 pin, u8 slot)
  181. {
  182. return (((pin-1) + slot) % 4) + 1;
  183. }
  184. static inline u8 bridge_swizzle_pci_1(u8 pin, u8 slot)
  185. {
  186. return (((pin-1) - slot) & 3) + 1;
  187. }
  188. static u8 __init mpc1211_swizzle(struct pci_dev *dev, u8 *pinp)
  189. {
  190. unsigned long flags;
  191. u8 pin = *pinp;
  192. u32 word;
  193. for ( ; dev->bus->self; dev = dev->bus->self) {
  194. if (!pin)
  195. continue;
  196. if (dev->bus->number == 1) {
  197. local_irq_save(flags);
  198. writel(0x80000000 | 0x2c, PCIPAR);
  199. word = readl(PCIPDR);
  200. local_irq_restore(flags);
  201. word >>= 16;
  202. if (word == 0x0001)
  203. pin = bridge_swizzle_pci_1(pin, PCI_SLOT(dev->devfn));
  204. else
  205. pin = bridge_swizzle(pin, PCI_SLOT(dev->devfn));
  206. } else
  207. pin = bridge_swizzle(pin, PCI_SLOT(dev->devfn));
  208. }
  209. *pinp = pin;
  210. return PCI_SLOT(dev->devfn);
  211. }
  212. static int __init map_mpc1211_irq(struct pci_dev *dev, u8 slot, u8 pin)
  213. {
  214. int irq = -1;
  215. /* now lookup the actual IRQ on a platform specific basis (pci-'platform'.c) */
  216. if (dev->bus->number == 0) {
  217. switch (slot) {
  218. case 13: irq = 9; break; /* USB */
  219. case 22: irq = 10; break; /* LAN */
  220. default: irq = 0; break;
  221. }
  222. } else {
  223. switch (pin) {
  224. case 0: irq = 0; break;
  225. case 1: irq = 7; break;
  226. case 2: irq = 9; break;
  227. case 3: irq = 10; break;
  228. case 4: irq = 11; break;
  229. }
  230. }
  231. if( irq < 0 ) {
  232. PCIDBG(3, "PCI: Error mapping IRQ on device %s\n", pci_name(dev));
  233. return irq;
  234. }
  235. PCIDBG(2, "Setting IRQ for slot %s to %d\n", pci_name(dev), irq);
  236. return irq;
  237. }
  238. void __init pcibios_fixup_irqs(void)
  239. {
  240. pci_fixup_irqs(mpc1211_swizzle, map_mpc1211_irq);
  241. }
  242. void pcibios_align_resource(void *data, struct resource *res,
  243. unsigned long size, unsigned long align)
  244. {
  245. unsigned long start = res->start;
  246. if (res->flags & IORESOURCE_IO) {
  247. if (start >= 0x10000UL) {
  248. if ((start & 0xffffUL) < 0x4000UL) {
  249. start = (start & 0xffff0000UL) + 0x4000UL;
  250. } else if ((start & 0xffffUL) >= 0xf000UL) {
  251. start = (start & 0xffff0000UL) + 0x10000UL;
  252. }
  253. res->start = start;
  254. } else {
  255. if (start & 0x300) {
  256. start = (start + 0x3ff) & ~0x3ff;
  257. res->start = start;
  258. }
  259. }
  260. }
  261. }