pci-sh7751.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  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/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-sh7751.h"
  29. static unsigned int pci_probe = PCI_PROBE_CONF1;
  30. extern int pci_fixup_pcic(void);
  31. void pcibios_fixup_irqs(void) __attribute__ ((weak));
  32. /*
  33. * Direct access to PCI hardware...
  34. */
  35. #define CONFIG_CMD(bus, devfn, where) (0x80000000 | (bus->number << 16) | (devfn << 8) | (where & ~3))
  36. /*
  37. * Functions for accessing PCI configuration space with type 1 accesses
  38. */
  39. static int sh7751_pci_read(struct pci_bus *bus, unsigned int devfn,
  40. int where, int size, u32 *val)
  41. {
  42. unsigned long flags;
  43. u32 data;
  44. /*
  45. * PCIPDR may only be accessed as 32 bit words,
  46. * so we must do byte alignment by hand
  47. */
  48. local_irq_save(flags);
  49. outl(CONFIG_CMD(bus,devfn,where), PCI_REG(SH7751_PCIPAR));
  50. data = inl(PCI_REG(SH7751_PCIPDR));
  51. local_irq_restore(flags);
  52. switch (size) {
  53. case 1:
  54. *val = (data >> ((where & 3) << 3)) & 0xff;
  55. break;
  56. case 2:
  57. *val = (data >> ((where & 2) << 3)) & 0xffff;
  58. break;
  59. case 4:
  60. *val = data;
  61. break;
  62. default:
  63. return PCIBIOS_FUNC_NOT_SUPPORTED;
  64. }
  65. return PCIBIOS_SUCCESSFUL;
  66. }
  67. /*
  68. * Since SH7751 only does 32bit access we'll have to do a read,
  69. * mask,write operation.
  70. * We'll allow an odd byte offset, though it should be illegal.
  71. */
  72. static int sh7751_pci_write(struct pci_bus *bus, unsigned int devfn,
  73. int where, int size, u32 val)
  74. {
  75. unsigned long flags;
  76. int shift;
  77. u32 data;
  78. local_irq_save(flags);
  79. outl(CONFIG_CMD(bus,devfn,where), PCI_REG(SH7751_PCIPAR));
  80. data = inl(PCI_REG(SH7751_PCIPDR));
  81. local_irq_restore(flags);
  82. switch (size) {
  83. case 1:
  84. shift = (where & 3) << 3;
  85. data &= ~(0xff << shift);
  86. data |= ((val & 0xff) << shift);
  87. break;
  88. case 2:
  89. shift = (where & 2) << 3;
  90. data &= ~(0xffff << shift);
  91. data |= ((val & 0xffff) << shift);
  92. break;
  93. case 4:
  94. data = val;
  95. break;
  96. default:
  97. return PCIBIOS_FUNC_NOT_SUPPORTED;
  98. }
  99. outl(data, PCI_REG(SH7751_PCIPDR));
  100. return PCIBIOS_SUCCESSFUL;
  101. }
  102. #undef CONFIG_CMD
  103. struct pci_ops sh7751_pci_ops = {
  104. .read = sh7751_pci_read,
  105. .write = sh7751_pci_write,
  106. };
  107. static int __init pci_check_direct(void)
  108. {
  109. unsigned int tmp, id;
  110. /* check for SH7751/SH7751R hardware */
  111. id = inl(SH7751_PCIREG_BASE+SH7751_PCICONF0);
  112. if (id != ((SH7751_DEVICE_ID << 16) | SH7751_VENDOR_ID) &&
  113. id != ((SH7751R_DEVICE_ID << 16) | SH7751_VENDOR_ID)) {
  114. pr_debug("PCI: This is not an SH7751(R) (%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(SH7751_PCIPAR));
  122. outl (0x80000000, PCI_REG(SH7751_PCIPAR));
  123. if (inl (PCI_REG(SH7751_PCIPAR)) == 0x80000000) {
  124. outl (tmp, PCI_REG(SH7751_PCIPAR));
  125. printk(KERN_INFO "PCI: Using configuration type 1\n");
  126. request_region(PCI_REG(SH7751_PCIPAR), 8, "PCI conf1");
  127. return 0;
  128. }
  129. outl (tmp, PCI_REG(SH7751_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. * exitst 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 sh7751_pci_init(void)
  178. {
  179. int ret;
  180. pr_debug("PCI: Starting intialization.\n");
  181. if ((ret = pci_check_direct()) != 0)
  182. return ret;
  183. return pcibios_init_platform();
  184. }
  185. subsys_initcall(sh7751_pci_init);
  186. static int __init __area_sdram_check(unsigned int area)
  187. {
  188. u32 word;
  189. word = inl(SH7751_BCR1);
  190. /* check BCR for SDRAM in area */
  191. if(((word >> area) & 1) == 0) {
  192. printk("PCI: Area %d is not configured for SDRAM. BCR1=0x%x\n",
  193. area, word);
  194. return 0;
  195. }
  196. outl(word, PCI_REG(SH7751_PCIBCR1));
  197. word = (u16)inw(SH7751_BCR2);
  198. /* check BCR2 for 32bit SDRAM interface*/
  199. if(((word >> (area << 1)) & 0x3) != 0x3) {
  200. printk("PCI: Area %d is not 32 bit SDRAM. BCR2=0x%x\n",
  201. area, word);
  202. return 0;
  203. }
  204. outl(word, PCI_REG(SH7751_PCIBCR2));
  205. return 1;
  206. }
  207. int __init sh7751_pcic_init(struct sh7751_pci_address_map *map)
  208. {
  209. u32 reg;
  210. u32 word;
  211. /* Set the BCR's to enable PCI access */
  212. reg = inl(SH7751_BCR1);
  213. reg |= 0x80000;
  214. outl(reg, SH7751_BCR1);
  215. /* Turn the clocks back on (not done in reset)*/
  216. outl(0, PCI_REG(SH7751_PCICLKR));
  217. /* Clear Powerdown IRQ's (not done in reset) */
  218. word = SH7751_PCIPINT_D3 | SH7751_PCIPINT_D0;
  219. outl(word, PCI_REG(SH7751_PCIPINT));
  220. /*
  221. * This code is unused for some boards as it is done in the
  222. * bootloader and doing it here means the MAC addresses loaded
  223. * by the bootloader get lost.
  224. */
  225. if (!(map->flags & SH7751_PCIC_NO_RESET)) {
  226. /* toggle PCI reset pin */
  227. word = SH7751_PCICR_PREFIX | SH7751_PCICR_PRST;
  228. outl(word,PCI_REG(SH7751_PCICR));
  229. /* Wait for a long time... not 1 sec. but long enough */
  230. mdelay(100);
  231. word = SH7751_PCICR_PREFIX;
  232. outl(word,PCI_REG(SH7751_PCICR));
  233. }
  234. /* set the command/status bits to:
  235. * Wait Cycle Control + Parity Enable + Bus Master +
  236. * Mem space enable
  237. */
  238. word = SH7751_PCICONF1_WCC | SH7751_PCICONF1_PER |
  239. SH7751_PCICONF1_BUM | SH7751_PCICONF1_MES;
  240. outl(word, PCI_REG(SH7751_PCICONF1));
  241. /* define this host as the host bridge */
  242. word = SH7751_PCI_HOST_BRIDGE << 24;
  243. outl(word, PCI_REG(SH7751_PCICONF2));
  244. /* Set IO and Mem windows to local address
  245. * Make PCI and local address the same for easy 1 to 1 mapping
  246. * Window0 = map->window0.size @ non-cached area base = SDRAM
  247. * Window1 = map->window1.size @ cached area base = SDRAM
  248. */
  249. word = map->window0.size - 1;
  250. outl(word, PCI_REG(SH7751_PCILSR0));
  251. word = map->window1.size - 1;
  252. outl(word, PCI_REG(SH7751_PCILSR1));
  253. /* Set the values on window 0 PCI config registers */
  254. word = P2SEGADDR(map->window0.base);
  255. outl(word, PCI_REG(SH7751_PCILAR0));
  256. outl(word, PCI_REG(SH7751_PCICONF5));
  257. /* Set the values on window 1 PCI config registers */
  258. word = PHYSADDR(map->window1.base);
  259. outl(word, PCI_REG(SH7751_PCILAR1));
  260. outl(word, PCI_REG(SH7751_PCICONF6));
  261. /* Set the local 16MB PCI memory space window to
  262. * the lowest PCI mapped address
  263. */
  264. word = PCIBIOS_MIN_MEM & SH7751_PCIMBR_MASK;
  265. PCIDBG(2,"PCI: Setting upper bits of Memory window to 0x%x\n", word);
  266. outl(word , PCI_REG(SH7751_PCIMBR));
  267. /* Map IO space into PCI IO window
  268. * The IO window is 64K-PCIBIOS_MIN_IO in size
  269. * IO addresses will be translated to the
  270. * PCI IO window base address
  271. */
  272. PCIDBG(3,"PCI: Mapping IO address 0x%x - 0x%x to base 0x%x\n", PCIBIOS_MIN_IO,
  273. (64*1024), SH7751_PCI_IO_BASE+PCIBIOS_MIN_IO);
  274. /*
  275. * XXX: For now, leave this board-specific. In the event we have other
  276. * boards that need to do similar work, this can be wrapped.
  277. */
  278. #ifdef CONFIG_SH_BIGSUR
  279. bigsur_port_map(PCIBIOS_MIN_IO, (64*1024), SH7751_PCI_IO_BASE+PCIBIOS_MIN_IO,0);
  280. #endif
  281. /* Make sure the MSB's of IO window are set to access PCI space correctly */
  282. word = PCIBIOS_MIN_IO & SH7751_PCIIOBR_MASK;
  283. PCIDBG(2,"PCI: Setting upper bits of IO window to 0x%x\n", word);
  284. outl(word, PCI_REG(SH7751_PCIIOBR));
  285. /* Set PCI WCRx, BCRx's, copy from BSC locations */
  286. /* check BCR for SDRAM in specified area */
  287. switch (map->window0.base) {
  288. case SH7751_CS0_BASE_ADDR: word = __area_sdram_check(0); break;
  289. case SH7751_CS1_BASE_ADDR: word = __area_sdram_check(1); break;
  290. case SH7751_CS2_BASE_ADDR: word = __area_sdram_check(2); break;
  291. case SH7751_CS3_BASE_ADDR: word = __area_sdram_check(3); break;
  292. case SH7751_CS4_BASE_ADDR: word = __area_sdram_check(4); break;
  293. case SH7751_CS5_BASE_ADDR: word = __area_sdram_check(5); break;
  294. case SH7751_CS6_BASE_ADDR: word = __area_sdram_check(6); break;
  295. }
  296. if (!word)
  297. return 0;
  298. /* configure the wait control registers */
  299. word = inl(SH7751_WCR1);
  300. outl(word, PCI_REG(SH7751_PCIWCR1));
  301. word = inl(SH7751_WCR2);
  302. outl(word, PCI_REG(SH7751_PCIWCR2));
  303. word = inl(SH7751_WCR3);
  304. outl(word, PCI_REG(SH7751_PCIWCR3));
  305. word = inl(SH7751_MCR);
  306. outl(word, PCI_REG(SH7751_PCIMCR));
  307. /* NOTE: I'm ignoring the PCI error IRQs for now..
  308. * TODO: add support for the internal error interrupts and
  309. * DMA interrupts...
  310. */
  311. #ifdef CONFIG_SH_RTS7751R2D
  312. pci_fixup_pcic();
  313. #endif
  314. /* SH7751 init done, set central function init complete */
  315. /* use round robin mode to stop a device starving/overruning */
  316. word = SH7751_PCICR_PREFIX | SH7751_PCICR_CFIN | SH7751_PCICR_ARBM;
  317. outl(word,PCI_REG(SH7751_PCICR));
  318. return 1;
  319. }
  320. char * __init pcibios_setup(char *str)
  321. {
  322. if (!strcmp(str, "off")) {
  323. pci_probe = 0;
  324. return NULL;
  325. }
  326. return str;
  327. }
  328. /*
  329. * IRQ functions
  330. */
  331. static u8 __init sh7751_no_swizzle(struct pci_dev *dev, u8 *pin)
  332. {
  333. /* no swizzling */
  334. return PCI_SLOT(dev->devfn);
  335. }
  336. static int sh7751_pci_lookup_irq(struct pci_dev *dev, u8 slot, u8 pin)
  337. {
  338. int irq = -1;
  339. /* now lookup the actual IRQ on a platform specific basis (pci-'platform'.c) */
  340. irq = pcibios_map_platform_irq(slot,pin);
  341. if( irq < 0 ) {
  342. pr_debug("PCI: Error mapping IRQ on device %s\n", pci_name(dev));
  343. return irq;
  344. }
  345. pr_debug("Setting IRQ for slot %s to %d\n", pci_name(dev), irq);
  346. return irq;
  347. }
  348. void __init pcibios_fixup_irqs(void)
  349. {
  350. pci_fixup_irqs(sh7751_no_swizzle, sh7751_pci_lookup_irq);
  351. }