pci.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. /*
  2. * linux/arch/mips/txx9/pci.c
  3. *
  4. * Based on linux/arch/mips/txx9/rbtx4927/setup.c,
  5. * linux/arch/mips/txx9/rbtx4938/setup.c,
  6. * and RBTX49xx patch from CELF patch archive.
  7. *
  8. * Copyright 2001-2005 MontaVista Software Inc.
  9. * Copyright (C) 1996, 97, 2001, 04 Ralf Baechle (ralf@linux-mips.org)
  10. * (C) Copyright TOSHIBA CORPORATION 2000-2001, 2004-2007
  11. *
  12. * This file is subject to the terms and conditions of the GNU General Public
  13. * License. See the file "COPYING" in the main directory of this archive
  14. * for more details.
  15. */
  16. #include <linux/delay.h>
  17. #include <linux/jiffies.h>
  18. #include <linux/io.h>
  19. #include <asm/txx9/pci.h>
  20. #ifdef CONFIG_TOSHIBA_FPCIB0
  21. #include <linux/interrupt.h>
  22. #include <asm/i8259.h>
  23. #include <asm/txx9/smsc_fdc37m81x.h>
  24. #endif
  25. static int __init
  26. early_read_config_word(struct pci_controller *hose,
  27. int top_bus, int bus, int devfn, int offset, u16 *value)
  28. {
  29. struct pci_dev fake_dev;
  30. struct pci_bus fake_bus;
  31. fake_dev.bus = &fake_bus;
  32. fake_dev.sysdata = hose;
  33. fake_dev.devfn = devfn;
  34. fake_bus.number = bus;
  35. fake_bus.sysdata = hose;
  36. fake_bus.ops = hose->pci_ops;
  37. if (bus != top_bus)
  38. /* Fake a parent bus structure. */
  39. fake_bus.parent = &fake_bus;
  40. else
  41. fake_bus.parent = NULL;
  42. return pci_read_config_word(&fake_dev, offset, value);
  43. }
  44. int __init txx9_pci66_check(struct pci_controller *hose, int top_bus,
  45. int current_bus)
  46. {
  47. u32 pci_devfn;
  48. unsigned short vid;
  49. int cap66 = -1;
  50. u16 stat;
  51. /* It seems SLC90E66 needs some time after PCI reset... */
  52. mdelay(80);
  53. printk(KERN_INFO "PCI: Checking 66MHz capabilities...\n");
  54. for (pci_devfn = 0; pci_devfn < 0xff; pci_devfn++) {
  55. if (PCI_FUNC(pci_devfn))
  56. continue;
  57. if (early_read_config_word(hose, top_bus, current_bus,
  58. pci_devfn, PCI_VENDOR_ID, &vid) !=
  59. PCIBIOS_SUCCESSFUL)
  60. continue;
  61. if (vid == 0xffff)
  62. continue;
  63. /* check 66MHz capability */
  64. if (cap66 < 0)
  65. cap66 = 1;
  66. if (cap66) {
  67. early_read_config_word(hose, top_bus, current_bus,
  68. pci_devfn, PCI_STATUS, &stat);
  69. if (!(stat & PCI_STATUS_66MHZ)) {
  70. printk(KERN_DEBUG
  71. "PCI: %02x:%02x not 66MHz capable.\n",
  72. current_bus, pci_devfn);
  73. cap66 = 0;
  74. break;
  75. }
  76. }
  77. }
  78. return cap66 > 0;
  79. }
  80. static struct resource primary_pci_mem_res[2] = {
  81. { .name = "PCI MEM" },
  82. { .name = "PCI MMIO" },
  83. };
  84. static struct resource primary_pci_io_res = { .name = "PCI IO" };
  85. struct pci_controller txx9_primary_pcic = {
  86. .mem_resource = &primary_pci_mem_res[0],
  87. .io_resource = &primary_pci_io_res,
  88. };
  89. #ifdef CONFIG_64BIT
  90. int txx9_pci_mem_high __initdata = 1;
  91. #else
  92. int txx9_pci_mem_high __initdata;
  93. #endif
  94. /*
  95. * allocate pci_controller and resources.
  96. * mem_base, io_base: physical addresss. 0 for auto assignment.
  97. * mem_size and io_size means max size on auto assignment.
  98. * pcic must be &txx9_primary_pcic or NULL.
  99. */
  100. struct pci_controller *__init
  101. txx9_alloc_pci_controller(struct pci_controller *pcic,
  102. unsigned long mem_base, unsigned long mem_size,
  103. unsigned long io_base, unsigned long io_size)
  104. {
  105. struct pcic {
  106. struct pci_controller c;
  107. struct resource r_mem[2];
  108. struct resource r_io;
  109. } *new = NULL;
  110. int min_size = 0x10000;
  111. if (!pcic) {
  112. new = kzalloc(sizeof(*new), GFP_KERNEL);
  113. if (!new)
  114. return NULL;
  115. new->r_mem[0].name = "PCI mem";
  116. new->r_mem[1].name = "PCI mmio";
  117. new->r_io.name = "PCI io";
  118. new->c.mem_resource = new->r_mem;
  119. new->c.io_resource = &new->r_io;
  120. pcic = &new->c;
  121. } else
  122. BUG_ON(pcic != &txx9_primary_pcic);
  123. pcic->io_resource->flags = IORESOURCE_IO;
  124. /*
  125. * for auto assignment, first search a (big) region for PCI
  126. * MEM, then search a region for PCI IO.
  127. */
  128. if (mem_base) {
  129. pcic->mem_resource[0].start = mem_base;
  130. pcic->mem_resource[0].end = mem_base + mem_size - 1;
  131. if (request_resource(&iomem_resource, &pcic->mem_resource[0]))
  132. goto free_and_exit;
  133. } else {
  134. unsigned long min = 0, max = 0x20000000; /* low 512MB */
  135. if (!mem_size) {
  136. /* default size for auto assignment */
  137. if (txx9_pci_mem_high)
  138. mem_size = 0x20000000; /* mem:512M(max) */
  139. else
  140. mem_size = 0x08000000; /* mem:128M(max) */
  141. }
  142. if (txx9_pci_mem_high) {
  143. min = 0x20000000;
  144. max = 0xe0000000;
  145. }
  146. /* search free region for PCI MEM */
  147. for (; mem_size >= min_size; mem_size /= 2) {
  148. if (allocate_resource(&iomem_resource,
  149. &pcic->mem_resource[0],
  150. mem_size, min, max,
  151. mem_size, NULL, NULL) == 0)
  152. break;
  153. }
  154. if (mem_size < min_size)
  155. goto free_and_exit;
  156. }
  157. pcic->mem_resource[1].flags = IORESOURCE_MEM | IORESOURCE_BUSY;
  158. if (io_base) {
  159. pcic->mem_resource[1].start = io_base;
  160. pcic->mem_resource[1].end = io_base + io_size - 1;
  161. if (request_resource(&iomem_resource, &pcic->mem_resource[1]))
  162. goto release_and_exit;
  163. } else {
  164. if (!io_size)
  165. /* default size for auto assignment */
  166. io_size = 0x01000000; /* io:16M(max) */
  167. /* search free region for PCI IO in low 512MB */
  168. for (; io_size >= min_size; io_size /= 2) {
  169. if (allocate_resource(&iomem_resource,
  170. &pcic->mem_resource[1],
  171. io_size, 0, 0x20000000,
  172. io_size, NULL, NULL) == 0)
  173. break;
  174. }
  175. if (io_size < min_size)
  176. goto release_and_exit;
  177. io_base = pcic->mem_resource[1].start;
  178. }
  179. pcic->mem_resource[0].flags = IORESOURCE_MEM;
  180. if (pcic == &txx9_primary_pcic &&
  181. mips_io_port_base == (unsigned long)-1) {
  182. /* map ioport 0 to PCI I/O space address 0 */
  183. set_io_port_base(IO_BASE + pcic->mem_resource[1].start);
  184. pcic->io_resource->start = 0;
  185. pcic->io_offset = 0; /* busaddr == ioaddr */
  186. pcic->io_map_base = IO_BASE + pcic->mem_resource[1].start;
  187. } else {
  188. /* physaddr to ioaddr */
  189. pcic->io_resource->start =
  190. io_base - (mips_io_port_base - IO_BASE);
  191. pcic->io_offset = io_base - (mips_io_port_base - IO_BASE);
  192. pcic->io_map_base = mips_io_port_base;
  193. }
  194. pcic->io_resource->end = pcic->io_resource->start + io_size - 1;
  195. pcic->mem_offset = 0; /* busaddr == physaddr */
  196. printk(KERN_INFO "PCI: IO 0x%08llx-0x%08llx MEM 0x%08llx-0x%08llx\n",
  197. (unsigned long long)pcic->mem_resource[1].start,
  198. (unsigned long long)pcic->mem_resource[1].end,
  199. (unsigned long long)pcic->mem_resource[0].start,
  200. (unsigned long long)pcic->mem_resource[0].end);
  201. /* register_pci_controller() will request MEM resource */
  202. release_resource(&pcic->mem_resource[0]);
  203. return pcic;
  204. release_and_exit:
  205. release_resource(&pcic->mem_resource[0]);
  206. free_and_exit:
  207. kfree(new);
  208. printk(KERN_ERR "PCI: Failed to allocate resources.\n");
  209. return NULL;
  210. }
  211. static int __init
  212. txx9_arch_pci_init(void)
  213. {
  214. PCIBIOS_MIN_IO = 0x8000; /* reseve legacy I/O space */
  215. return 0;
  216. }
  217. arch_initcall(txx9_arch_pci_init);
  218. /* IRQ/IDSEL mapping */
  219. int txx9_pci_option =
  220. #ifdef CONFIG_PICMG_PCI_BACKPLANE_DEFAULT
  221. TXX9_PCI_OPT_PICMG |
  222. #endif
  223. TXX9_PCI_OPT_CLK_AUTO;
  224. enum txx9_pci_err_action txx9_pci_err_action = TXX9_PCI_ERR_REPORT;
  225. #ifdef CONFIG_TOSHIBA_FPCIB0
  226. static irqreturn_t i8259_interrupt(int irq, void *dev_id)
  227. {
  228. int isairq;
  229. isairq = i8259_irq();
  230. if (unlikely(isairq <= I8259A_IRQ_BASE))
  231. return IRQ_NONE;
  232. generic_handle_irq(isairq);
  233. return IRQ_HANDLED;
  234. }
  235. static int __init
  236. txx9_i8259_irq_setup(int irq)
  237. {
  238. int err;
  239. init_i8259_irqs();
  240. err = request_irq(irq, &i8259_interrupt, IRQF_DISABLED|IRQF_SHARED,
  241. "cascade(i8259)", (void *)(long)irq);
  242. if (!err)
  243. printk(KERN_INFO "PCI-ISA bridge PIC (irq %d)\n", irq);
  244. return err;
  245. }
  246. static void __init quirk_slc90e66_bridge(struct pci_dev *dev)
  247. {
  248. int irq; /* PCI/ISA Bridge interrupt */
  249. u8 reg_64;
  250. u32 reg_b0;
  251. u8 reg_e1;
  252. irq = pcibios_map_irq(dev, PCI_SLOT(dev->devfn), 1); /* INTA */
  253. if (!irq)
  254. return;
  255. txx9_i8259_irq_setup(irq);
  256. pci_read_config_byte(dev, 0x64, &reg_64);
  257. pci_read_config_dword(dev, 0xb0, &reg_b0);
  258. pci_read_config_byte(dev, 0xe1, &reg_e1);
  259. /* serial irq control */
  260. reg_64 = 0xd0;
  261. /* serial irq pin */
  262. reg_b0 |= 0x00010000;
  263. /* ide irq on isa14 */
  264. reg_e1 &= 0xf0;
  265. reg_e1 |= 0x0d;
  266. pci_write_config_byte(dev, 0x64, reg_64);
  267. pci_write_config_dword(dev, 0xb0, reg_b0);
  268. pci_write_config_byte(dev, 0xe1, reg_e1);
  269. smsc_fdc37m81x_init(0x3f0);
  270. smsc_fdc37m81x_config_beg();
  271. smsc_fdc37m81x_config_set(SMSC_FDC37M81X_DNUM,
  272. SMSC_FDC37M81X_KBD);
  273. smsc_fdc37m81x_config_set(SMSC_FDC37M81X_INT, 1);
  274. smsc_fdc37m81x_config_set(SMSC_FDC37M81X_INT2, 12);
  275. smsc_fdc37m81x_config_set(SMSC_FDC37M81X_ACTIVE,
  276. 1);
  277. smsc_fdc37m81x_config_end();
  278. }
  279. static void quirk_slc90e66_ide(struct pci_dev *dev)
  280. {
  281. unsigned char dat;
  282. int regs[2] = {0x41, 0x43};
  283. int i;
  284. /* SMSC SLC90E66 IDE uses irq 14, 15 (default) */
  285. pci_write_config_byte(dev, PCI_INTERRUPT_LINE, 14);
  286. pci_read_config_byte(dev, PCI_INTERRUPT_LINE, &dat);
  287. printk(KERN_INFO "PCI: %s: IRQ %02x", pci_name(dev), dat);
  288. /* enable SMSC SLC90E66 IDE */
  289. for (i = 0; i < ARRAY_SIZE(regs); i++) {
  290. pci_read_config_byte(dev, regs[i], &dat);
  291. pci_write_config_byte(dev, regs[i], dat | 0x80);
  292. pci_read_config_byte(dev, regs[i], &dat);
  293. printk(KERN_CONT " IDETIM%d %02x", i, dat);
  294. }
  295. pci_read_config_byte(dev, 0x5c, &dat);
  296. /*
  297. * !!! DO NOT REMOVE THIS COMMENT IT IS REQUIRED BY SMSC !!!
  298. *
  299. * This line of code is intended to provide the user with a work
  300. * around solution to the anomalies cited in SMSC's anomaly sheet
  301. * entitled, "SLC90E66 Functional Rev.J_0.1 Anomalies"".
  302. *
  303. * !!! DO NOT REMOVE THIS COMMENT IT IS REQUIRED BY SMSC !!!
  304. */
  305. dat |= 0x01;
  306. pci_write_config_byte(dev, regs[i], dat);
  307. pci_read_config_byte(dev, 0x5c, &dat);
  308. printk(KERN_CONT " REG5C %02x", dat);
  309. printk(KERN_CONT "\n");
  310. }
  311. #endif /* CONFIG_TOSHIBA_FPCIB0 */
  312. static void final_fixup(struct pci_dev *dev)
  313. {
  314. unsigned char bist;
  315. /* Do build-in self test */
  316. if (pci_read_config_byte(dev, PCI_BIST, &bist) == PCIBIOS_SUCCESSFUL &&
  317. (bist & PCI_BIST_CAPABLE)) {
  318. unsigned long timeout;
  319. pci_set_power_state(dev, PCI_D0);
  320. printk(KERN_INFO "PCI: %s BIST...", pci_name(dev));
  321. pci_write_config_byte(dev, PCI_BIST, PCI_BIST_START);
  322. timeout = jiffies + HZ * 2; /* timeout after 2 sec */
  323. do {
  324. pci_read_config_byte(dev, PCI_BIST, &bist);
  325. if (time_after(jiffies, timeout))
  326. break;
  327. } while (bist & PCI_BIST_START);
  328. if (bist & (PCI_BIST_CODE_MASK | PCI_BIST_START))
  329. printk(KERN_CONT "failed. (0x%x)\n", bist);
  330. else
  331. printk(KERN_CONT "OK.\n");
  332. }
  333. }
  334. #ifdef CONFIG_TOSHIBA_FPCIB0
  335. #define PCI_DEVICE_ID_EFAR_SLC90E66_0 0x9460
  336. DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_EFAR, PCI_DEVICE_ID_EFAR_SLC90E66_0,
  337. quirk_slc90e66_bridge);
  338. DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_EFAR, PCI_DEVICE_ID_EFAR_SLC90E66_1,
  339. quirk_slc90e66_ide);
  340. DECLARE_PCI_FIXUP_RESUME(PCI_VENDOR_ID_EFAR, PCI_DEVICE_ID_EFAR_SLC90E66_1,
  341. quirk_slc90e66_ide);
  342. #endif
  343. DECLARE_PCI_FIXUP_FINAL(PCI_ANY_ID, PCI_ANY_ID, final_fixup);
  344. DECLARE_PCI_FIXUP_RESUME(PCI_ANY_ID, PCI_ANY_ID, final_fixup);