pci-sh7780.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. /*
  2. * Low-Level PCI Support for the SH7780
  3. *
  4. * Dustin McIntire (dustin@sensoria.com)
  5. * Derived from arch/i386/kernel/pci-*.c which bore the message:
  6. * (c) 1999--2000 Martin Mares <mj@ucw.cz>
  7. *
  8. * Ported to the new API by Paul Mundt <lethal@linux-sh.org>
  9. * With cleanup by Paul van Gool <pvangool@mimotech.com>
  10. *
  11. * May be copied or modified under the terms of the GNU General Public
  12. * License. See linux/COPYING for more information.
  13. *
  14. */
  15. #undef DEBUG
  16. #include <linux/config.h>
  17. #include <linux/types.h>
  18. #include <linux/kernel.h>
  19. #include <linux/init.h>
  20. #include <linux/pci.h>
  21. #include <linux/sched.h>
  22. #include <linux/ioport.h>
  23. #include <linux/errno.h>
  24. #include <linux/irq.h>
  25. #include <linux/delay.h>
  26. #include <asm/machvec.h>
  27. #include <asm/io.h>
  28. #include "pci-sh7780.h"
  29. static unsigned int pci_probe = PCI_PROBE_CONF1;
  30. extern int pci_fixup_pcic(void);
  31. /*
  32. * Direct access to PCI hardware...
  33. */
  34. #define CONFIG_CMD(bus, devfn, where) (0x80000000 | (bus->number << 16) | (devfn << 8) | (where & ~3))
  35. /*
  36. * Functions for accessing PCI configuration space with type 1 accesses
  37. */
  38. static int sh7780_pci_read(struct pci_bus *bus, unsigned int devfn,
  39. int where, int size, u32 *val)
  40. {
  41. unsigned long flags;
  42. u32 data;
  43. /*
  44. * PCIPDR may only be accessed as 32 bit words,
  45. * so we must do byte alignment by hand
  46. */
  47. local_irq_save(flags);
  48. outl(CONFIG_CMD(bus, devfn, where), PCI_REG(SH7780_PCIPAR));
  49. data = inl(PCI_REG(SH7780_PCIPDR));
  50. local_irq_restore(flags);
  51. switch (size) {
  52. case 1:
  53. *val = (data >> ((where & 3) << 3)) & 0xff;
  54. break;
  55. case 2:
  56. *val = (data >> ((where & 2) << 3)) & 0xffff;
  57. break;
  58. case 4:
  59. *val = data;
  60. break;
  61. default:
  62. return PCIBIOS_FUNC_NOT_SUPPORTED;
  63. }
  64. return PCIBIOS_SUCCESSFUL;
  65. }
  66. /*
  67. * Since SH7780 only does 32bit access we'll have to do a read,
  68. * mask,write operation.
  69. * We'll allow an odd byte offset, though it should be illegal.
  70. */
  71. static int sh7780_pci_write(struct pci_bus *bus, unsigned int devfn,
  72. int where, int size, u32 val)
  73. {
  74. unsigned long flags;
  75. int shift;
  76. u32 data;
  77. local_irq_save(flags);
  78. outl(CONFIG_CMD(bus, devfn, where), PCI_REG(SH7780_PCIPAR));
  79. data = inl(PCI_REG(SH7780_PCIPDR));
  80. local_irq_restore(flags);
  81. switch (size) {
  82. case 1:
  83. shift = (where & 3) << 3;
  84. data &= ~(0xff << shift);
  85. data |= ((val & 0xff) << shift);
  86. break;
  87. case 2:
  88. shift = (where & 2) << 3;
  89. data &= ~(0xffff << shift);
  90. data |= ((val & 0xffff) << shift);
  91. break;
  92. case 4:
  93. data = val;
  94. break;
  95. default:
  96. return PCIBIOS_FUNC_NOT_SUPPORTED;
  97. }
  98. outl(data, PCI_REG(SH7780_PCIPDR));
  99. return PCIBIOS_SUCCESSFUL;
  100. }
  101. #undef CONFIG_CMD
  102. struct pci_ops sh7780_pci_ops = {
  103. .read = sh7780_pci_read,
  104. .write = sh7780_pci_write,
  105. };
  106. static int __init pci_check_direct(void)
  107. {
  108. unsigned int tmp, id;
  109. outl(0x00000001, SH7780_PCI_VCR2); /* Enable PCIC */
  110. /* check for SH7780/SH7780R hardware */
  111. id = inl(PCI_REG(SH7780_PCIVID));
  112. if ((id != ((SH7780_DEVICE_ID << 16) | SH7780_VENDOR_ID)) &&
  113. (id != ((SH7781_DEVICE_ID << 16) | SH7780_VENDOR_ID))) {
  114. printk(KERN_ERR "PCI: This is not an SH7780 (%x)\n", id);
  115. return -ENODEV;
  116. }
  117. /*
  118. * Check if configuration works.
  119. */
  120. if (pci_probe & PCI_PROBE_CONF1) {
  121. tmp = inl(PCI_REG(SH7780_PCIPAR));
  122. outl(0x80000000, PCI_REG(SH7780_PCIPAR));
  123. if (inl(PCI_REG(SH7780_PCIPAR)) == 0x80000000) {
  124. outl(tmp, PCI_REG(SH7780_PCIPAR));
  125. printk(KERN_INFO "PCI: Using configuration type 1\n");
  126. request_region(PCI_REG(SH7780_PCIPAR), 8, "PCI conf1");
  127. return 0;
  128. }
  129. outl(tmp, PCI_REG(SH7780_PCIPAR));
  130. }
  131. pr_debug("PCI: pci_check_direct failed\n");
  132. return -EINVAL;
  133. }
  134. /***************************************************************************************/
  135. /*
  136. * Handle bus scanning and fixups ....
  137. */
  138. static void __init pci_fixup_ide_bases(struct pci_dev *d)
  139. {
  140. int i;
  141. /*
  142. * PCI IDE controllers use non-standard I/O port decoding, respect it.
  143. */
  144. if ((d->class >> 8) != PCI_CLASS_STORAGE_IDE)
  145. return;
  146. pr_debug("PCI: IDE base address fixup for %s\n", pci_name(d));
  147. for(i=0; i<4; i++) {
  148. struct resource *r = &d->resource[i];
  149. if ((r->start & ~0x80) == 0x374) {
  150. r->start |= 2;
  151. r->end = r->start;
  152. }
  153. }
  154. }
  155. DECLARE_PCI_FIXUP_HEADER(PCI_ANY_ID, PCI_ANY_ID, pci_fixup_ide_bases);
  156. /*
  157. * Called after each bus is probed, but before its children
  158. * are examined.
  159. */
  160. void __init pcibios_fixup_bus(struct pci_bus *b)
  161. {
  162. pci_read_bridge_bases(b);
  163. }
  164. /*
  165. * Initialization. Try all known PCI access methods. Note that we support
  166. * using both PCI BIOS and direct access: in such cases, we use I/O ports
  167. * to access config space.
  168. *
  169. * Note that the platform specific initialization (BSC registers, and memory
  170. * space mapping) will be called via the machine vectors (sh_mv.mv_pci_init()) if it
  171. * exists and via the platform defined function pcibios_init_platform().
  172. * See pci_bigsur.c for implementation;
  173. *
  174. * The BIOS version of the pci functions is not yet implemented but it is left
  175. * in for completeness. Currently an error will be genereated at compile time.
  176. */
  177. static int __init sh7780_pci_init(void)
  178. {
  179. int ret;
  180. pr_debug("PCI: Starting intialization.\n");
  181. /* Setup the INTC */
  182. ctrl_outl(0x00200000, INTC_ICR0); /* INTC SH-4 Mode */
  183. ctrl_outl(0x00078000, INTC_INT2MSKCR); /* enable PCIINTA - PCIINTD */
  184. ctrl_outl(0x40000000, INTC_INTMSK1); /* disable IRL4-7 Interrupt */
  185. ctrl_outl(0x0000fffe, INTC_INTMSK2); /* disable IRL4-7 Interrupt */
  186. ctrl_outl(0x80000000, INTC_INTMSKCLR1); /* enable IRL0-3 Interrupt */
  187. ctrl_outl(0xfffe0000, INTC_INTMSKCLR2); /* enable IRL0-3 Interrupt */
  188. if ((ret = pci_check_direct()) != 0)
  189. return ret;
  190. return pcibios_init_platform();
  191. }
  192. core_initcall(sh7780_pci_init);
  193. int __init sh7780_pcic_init(struct sh7780_pci_address_map *map)
  194. {
  195. u32 word;
  196. /*
  197. * This code is unused for some boards as it is done in the
  198. * bootloader and doing it here means the MAC addresses loaded
  199. * by the bootloader get lost.
  200. */
  201. if (!(map->flags & SH7780_PCIC_NO_RESET)) {
  202. /* toggle PCI reset pin */
  203. word = SH7780_PCICR_PREFIX | SH7780_PCICR_PRST;
  204. outl(word,PCI_REG(SH7780_PCICR));
  205. /* Wait for a long time... not 1 sec. but long enough */
  206. mdelay(100);
  207. word = SH7780_PCICR_PREFIX;
  208. outl(word,PCI_REG(SH7780_PCICR));
  209. }
  210. /* set the command/status bits to:
  211. * Wait Cycle Control + Parity Enable + Bus Master +
  212. * Mem space enable
  213. */
  214. outl(0x00000046, PCI_REG(SH7780_PCICMD));
  215. /* define this host as the host bridge */
  216. word = SH7780_PCI_HOST_BRIDGE << 24;
  217. outl(word, PCI_REG(SH7780_PCIRID));
  218. /* Set IO and Mem windows to local address
  219. * Make PCI and local address the same for easy 1 to 1 mapping
  220. * Window0 = map->window0.size @ non-cached area base = SDRAM
  221. * Window1 = map->window1.size @ cached area base = SDRAM
  222. */
  223. word = ((map->window0.size - 1) & 0x1ff00001) | 0x01;
  224. outl(0x07f00001, PCI_REG(SH7780_PCILSR0));
  225. word = ((map->window1.size - 1) & 0x1ff00001) | 0x01;
  226. outl(0x00000001, PCI_REG(SH7780_PCILSR1));
  227. /* Set the values on window 0 PCI config registers */
  228. word = P2SEGADDR(map->window0.base);
  229. outl(0xa8000000, PCI_REG(SH7780_PCILAR0));
  230. outl(0x08000000, PCI_REG(SH7780_PCIMBAR0));
  231. /* Set the values on window 1 PCI config registers */
  232. word = P2SEGADDR(map->window1.base);
  233. outl(0x00000000, PCI_REG(SH7780_PCILAR1));
  234. outl(0x00000000, PCI_REG(SH7780_PCIMBAR1));
  235. /* Map IO space into PCI IO window
  236. * The IO window is 64K-PCIBIOS_MIN_IO in size
  237. * IO addresses will be translated to the
  238. * PCI IO window base address
  239. */
  240. PCIDBG(3,"PCI: Mapping IO address 0x%x - 0x%x to base 0x%x\n", PCIBIOS_MIN_IO,
  241. (64*1024), SH7780_PCI_IO_BASE+PCIBIOS_MIN_IO);
  242. /* NOTE: I'm ignoring the PCI error IRQs for now..
  243. * TODO: add support for the internal error interrupts and
  244. * DMA interrupts...
  245. */
  246. #ifdef CONFIG_SH_R7780RP
  247. pci_fixup_pcic();
  248. #endif
  249. /* SH7780 init done, set central function init complete */
  250. /* use round robin mode to stop a device starving/overruning */
  251. word = SH7780_PCICR_PREFIX | SH7780_PCICR_CFIN | /* SH7780_PCICR_ARBM |*/ SH7780_PCICR_FTO;
  252. outl(word, PCI_REG(SH7780_PCICR));
  253. return 1;
  254. }
  255. char * __init pcibios_setup(char *str)
  256. {
  257. if (!strcmp(str, "off")) {
  258. pci_probe = 0;
  259. return NULL;
  260. }
  261. return str;
  262. }
  263. /*
  264. * IRQ functions
  265. */
  266. static u8 __init sh7780_no_swizzle(struct pci_dev *dev, u8 *pin)
  267. {
  268. /* no swizzling */
  269. return PCI_SLOT(dev->devfn);
  270. }
  271. static int sh7780_pci_lookup_irq(struct pci_dev *dev, u8 slot, u8 pin)
  272. {
  273. int irq = -1;
  274. /* now lookup the actual IRQ on a platform specific basis (pci-'platform'.c) */
  275. irq = pcibios_map_platform_irq(slot,pin);
  276. if( irq < 0 ) {
  277. pr_debug("PCI: Error mapping IRQ on device %s\n", pci_name(dev));
  278. return irq;
  279. }
  280. pr_debug("Setting IRQ for slot %s to %d\n", pci_name(dev), irq);
  281. return irq;
  282. }
  283. void __init pcibios_fixup_irqs(void)
  284. {
  285. pci_fixup_irqs(sh7780_no_swizzle, sh7780_pci_lookup_irq);
  286. }