pci.c 6.7 KB

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