pci-sh7751.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  1. /*
  2. * Low-Level PCI Support for the SH7751
  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/types.h>
  17. #include <linux/kernel.h>
  18. #include <linux/init.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/delay.h>
  25. #include <asm/machvec.h>
  26. #include <asm/io.h>
  27. #include "pci-sh7751.h"
  28. static unsigned int pci_probe = PCI_PROBE_CONF1;
  29. extern int pci_fixup_pcic(void);
  30. void pcibios_fixup_irqs(void) __attribute__ ((weak));
  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 sh7751_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(SH7751_PCIPAR));
  49. data = inl(PCI_REG(SH7751_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 SH7751 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 sh7751_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(SH7751_PCIPAR));
  79. data = inl(PCI_REG(SH7751_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(SH7751_PCIPDR));
  99. return PCIBIOS_SUCCESSFUL;
  100. }
  101. #undef CONFIG_CMD
  102. struct pci_ops sh7751_pci_ops = {
  103. .read = sh7751_pci_read,
  104. .write = sh7751_pci_write,
  105. };
  106. static int __init pci_check_direct(void)
  107. {
  108. unsigned int tmp, id;
  109. /* check for SH7751/SH7751R hardware */
  110. id = inl(SH7751_PCIREG_BASE+SH7751_PCICONF0);
  111. if (id != ((SH7751_DEVICE_ID << 16) | SH7751_VENDOR_ID) &&
  112. id != ((SH7751R_DEVICE_ID << 16) | SH7751_VENDOR_ID)) {
  113. pr_debug("PCI: This is not an SH7751(R) (%x)\n", id);
  114. return -ENODEV;
  115. }
  116. /*
  117. * Check if configuration works.
  118. */
  119. if (pci_probe & PCI_PROBE_CONF1) {
  120. tmp = inl (PCI_REG(SH7751_PCIPAR));
  121. outl (0x80000000, PCI_REG(SH7751_PCIPAR));
  122. if (inl (PCI_REG(SH7751_PCIPAR)) == 0x80000000) {
  123. outl (tmp, PCI_REG(SH7751_PCIPAR));
  124. printk(KERN_INFO "PCI: Using configuration type 1\n");
  125. request_region(PCI_REG(SH7751_PCIPAR), 8, "PCI conf1");
  126. return 0;
  127. }
  128. outl (tmp, PCI_REG(SH7751_PCIPAR));
  129. }
  130. pr_debug("PCI: pci_check_direct failed\n");
  131. return -EINVAL;
  132. }
  133. /***************************************************************************************/
  134. /*
  135. * Handle bus scanning and fixups ....
  136. */
  137. static void __init pci_fixup_ide_bases(struct pci_dev *d)
  138. {
  139. int i;
  140. /*
  141. * PCI IDE controllers use non-standard I/O port decoding, respect it.
  142. */
  143. if ((d->class >> 8) != PCI_CLASS_STORAGE_IDE)
  144. return;
  145. pr_debug("PCI: IDE base address fixup for %s\n", pci_name(d));
  146. for(i=0; i<4; i++) {
  147. struct resource *r = &d->resource[i];
  148. if ((r->start & ~0x80) == 0x374) {
  149. r->start |= 2;
  150. r->end = r->start;
  151. }
  152. }
  153. }
  154. DECLARE_PCI_FIXUP_HEADER(PCI_ANY_ID, PCI_ANY_ID, pci_fixup_ide_bases);
  155. /*
  156. * Called after each bus is probed, but before its children
  157. * are examined.
  158. */
  159. void __init pcibios_fixup_bus(struct pci_bus *b)
  160. {
  161. pci_read_bridge_bases(b);
  162. }
  163. /*
  164. * Initialization. Try all known PCI access methods. Note that we support
  165. * using both PCI BIOS and direct access: in such cases, we use I/O ports
  166. * to access config space.
  167. *
  168. * Note that the platform specific initialization (BSC registers, and memory
  169. * space mapping) will be called via the machine vectors (sh_mv.mv_pci_init()) if it
  170. * exitst and via the platform defined function pcibios_init_platform().
  171. * See pci_bigsur.c for implementation;
  172. *
  173. * The BIOS version of the pci functions is not yet implemented but it is left
  174. * in for completeness. Currently an error will be genereated at compile time.
  175. */
  176. static int __init sh7751_pci_init(void)
  177. {
  178. int ret;
  179. pr_debug("PCI: Starting intialization.\n");
  180. if ((ret = pci_check_direct()) != 0)
  181. return ret;
  182. return pcibios_init_platform();
  183. }
  184. subsys_initcall(sh7751_pci_init);
  185. static int __init __area_sdram_check(unsigned int area)
  186. {
  187. u32 word;
  188. word = inl(SH7751_BCR1);
  189. /* check BCR for SDRAM in area */
  190. if(((word >> area) & 1) == 0) {
  191. printk("PCI: Area %d is not configured for SDRAM. BCR1=0x%x\n",
  192. area, word);
  193. return 0;
  194. }
  195. outl(word, PCI_REG(SH7751_PCIBCR1));
  196. word = (u16)inw(SH7751_BCR2);
  197. /* check BCR2 for 32bit SDRAM interface*/
  198. if(((word >> (area << 1)) & 0x3) != 0x3) {
  199. printk("PCI: Area %d is not 32 bit SDRAM. BCR2=0x%x\n",
  200. area, word);
  201. return 0;
  202. }
  203. outl(word, PCI_REG(SH7751_PCIBCR2));
  204. return 1;
  205. }
  206. int __init sh7751_pcic_init(struct sh7751_pci_address_map *map)
  207. {
  208. u32 reg;
  209. u32 word;
  210. /* Set the BCR's to enable PCI access */
  211. reg = inl(SH7751_BCR1);
  212. reg |= 0x80000;
  213. outl(reg, SH7751_BCR1);
  214. /* Turn the clocks back on (not done in reset)*/
  215. outl(0, PCI_REG(SH7751_PCICLKR));
  216. /* Clear Powerdown IRQ's (not done in reset) */
  217. word = SH7751_PCIPINT_D3 | SH7751_PCIPINT_D0;
  218. outl(word, PCI_REG(SH7751_PCIPINT));
  219. /*
  220. * This code is unused for some boards as it is done in the
  221. * bootloader and doing it here means the MAC addresses loaded
  222. * by the bootloader get lost.
  223. */
  224. if (!(map->flags & SH7751_PCIC_NO_RESET)) {
  225. /* toggle PCI reset pin */
  226. word = SH7751_PCICR_PREFIX | SH7751_PCICR_PRST;
  227. outl(word,PCI_REG(SH7751_PCICR));
  228. /* Wait for a long time... not 1 sec. but long enough */
  229. mdelay(100);
  230. word = SH7751_PCICR_PREFIX;
  231. outl(word,PCI_REG(SH7751_PCICR));
  232. }
  233. /* set the command/status bits to:
  234. * Wait Cycle Control + Parity Enable + Bus Master +
  235. * Mem space enable
  236. */
  237. word = SH7751_PCICONF1_WCC | SH7751_PCICONF1_PER |
  238. SH7751_PCICONF1_BUM | SH7751_PCICONF1_MES;
  239. outl(word, PCI_REG(SH7751_PCICONF1));
  240. /* define this host as the host bridge */
  241. word = SH7751_PCI_HOST_BRIDGE << 24;
  242. outl(word, PCI_REG(SH7751_PCICONF2));
  243. /* Set IO and Mem windows to local address
  244. * Make PCI and local address the same for easy 1 to 1 mapping
  245. * Window0 = map->window0.size @ non-cached area base = SDRAM
  246. * Window1 = map->window1.size @ cached area base = SDRAM
  247. */
  248. word = map->window0.size - 1;
  249. outl(word, PCI_REG(SH7751_PCILSR0));
  250. word = map->window1.size - 1;
  251. outl(word, PCI_REG(SH7751_PCILSR1));
  252. /* Set the values on window 0 PCI config registers */
  253. word = P2SEGADDR(map->window0.base);
  254. outl(word, PCI_REG(SH7751_PCILAR0));
  255. outl(word, PCI_REG(SH7751_PCICONF5));
  256. /* Set the values on window 1 PCI config registers */
  257. word = PHYSADDR(map->window1.base);
  258. outl(word, PCI_REG(SH7751_PCILAR1));
  259. outl(word, PCI_REG(SH7751_PCICONF6));
  260. /* Set the local 16MB PCI memory space window to
  261. * the lowest PCI mapped address
  262. */
  263. word = PCIBIOS_MIN_MEM & SH7751_PCIMBR_MASK;
  264. PCIDBG(2,"PCI: Setting upper bits of Memory window to 0x%x\n", word);
  265. outl(word , PCI_REG(SH7751_PCIMBR));
  266. /* Map IO space into PCI IO window
  267. * The IO window is 64K-PCIBIOS_MIN_IO in size
  268. * IO addresses will be translated to the
  269. * PCI IO window base address
  270. */
  271. PCIDBG(3,"PCI: Mapping IO address 0x%x - 0x%x to base 0x%x\n", PCIBIOS_MIN_IO,
  272. (64*1024), SH7751_PCI_IO_BASE+PCIBIOS_MIN_IO);
  273. /*
  274. * XXX: For now, leave this board-specific. In the event we have other
  275. * boards that need to do similar work, this can be wrapped.
  276. */
  277. #ifdef CONFIG_SH_BIGSUR
  278. bigsur_port_map(PCIBIOS_MIN_IO, (64*1024), SH7751_PCI_IO_BASE+PCIBIOS_MIN_IO,0);
  279. #endif
  280. /* Make sure the MSB's of IO window are set to access PCI space correctly */
  281. word = PCIBIOS_MIN_IO & SH7751_PCIIOBR_MASK;
  282. PCIDBG(2,"PCI: Setting upper bits of IO window to 0x%x\n", word);
  283. outl(word, PCI_REG(SH7751_PCIIOBR));
  284. /* Set PCI WCRx, BCRx's, copy from BSC locations */
  285. /* check BCR for SDRAM in specified area */
  286. switch (map->window0.base) {
  287. case SH7751_CS0_BASE_ADDR: word = __area_sdram_check(0); break;
  288. case SH7751_CS1_BASE_ADDR: word = __area_sdram_check(1); break;
  289. case SH7751_CS2_BASE_ADDR: word = __area_sdram_check(2); break;
  290. case SH7751_CS3_BASE_ADDR: word = __area_sdram_check(3); break;
  291. case SH7751_CS4_BASE_ADDR: word = __area_sdram_check(4); break;
  292. case SH7751_CS5_BASE_ADDR: word = __area_sdram_check(5); break;
  293. case SH7751_CS6_BASE_ADDR: word = __area_sdram_check(6); break;
  294. }
  295. if (!word)
  296. return 0;
  297. /* configure the wait control registers */
  298. word = inl(SH7751_WCR1);
  299. outl(word, PCI_REG(SH7751_PCIWCR1));
  300. word = inl(SH7751_WCR2);
  301. outl(word, PCI_REG(SH7751_PCIWCR2));
  302. word = inl(SH7751_WCR3);
  303. outl(word, PCI_REG(SH7751_PCIWCR3));
  304. word = inl(SH7751_MCR);
  305. outl(word, PCI_REG(SH7751_PCIMCR));
  306. /* NOTE: I'm ignoring the PCI error IRQs for now..
  307. * TODO: add support for the internal error interrupts and
  308. * DMA interrupts...
  309. */
  310. #ifdef CONFIG_SH_RTS7751R2D
  311. pci_fixup_pcic();
  312. #endif
  313. /* SH7751 init done, set central function init complete */
  314. /* use round robin mode to stop a device starving/overruning */
  315. word = SH7751_PCICR_PREFIX | SH7751_PCICR_CFIN | SH7751_PCICR_ARBM;
  316. outl(word,PCI_REG(SH7751_PCICR));
  317. return 1;
  318. }
  319. char * __init pcibios_setup(char *str)
  320. {
  321. if (!strcmp(str, "off")) {
  322. pci_probe = 0;
  323. return NULL;
  324. }
  325. return str;
  326. }
  327. /*
  328. * IRQ functions
  329. */
  330. static u8 __init sh7751_no_swizzle(struct pci_dev *dev, u8 *pin)
  331. {
  332. /* no swizzling */
  333. return PCI_SLOT(dev->devfn);
  334. }
  335. static int sh7751_pci_lookup_irq(struct pci_dev *dev, u8 slot, u8 pin)
  336. {
  337. int irq = -1;
  338. /* now lookup the actual IRQ on a platform specific basis (pci-'platform'.c) */
  339. irq = pcibios_map_platform_irq(slot,pin);
  340. if( irq < 0 ) {
  341. pr_debug("PCI: Error mapping IRQ on device %s\n", pci_name(dev));
  342. return irq;
  343. }
  344. pr_debug("Setting IRQ for slot %s to %d\n", pci_name(dev), irq);
  345. return irq;
  346. }
  347. void __init pcibios_fixup_irqs(void)
  348. {
  349. pci_fixup_irqs(sh7751_no_swizzle, sh7751_pci_lookup_irq);
  350. }