galileo.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588
  1. /*
  2. * Copyright (C) 2000 David J. Mckay (david.mckay@st.com)
  3. *
  4. * May be copied or modified under the terms of the GNU General Public
  5. * License. See linux/COPYING for more information.
  6. *
  7. * This file contains the PCI routines required for the Galileo GT6411
  8. * PCI bridge as used on the Orion and Overdrive boards.
  9. *
  10. */
  11. #include <linux/config.h>
  12. #include <linux/kernel.h>
  13. #include <linux/smp.h>
  14. #include <linux/smp_lock.h>
  15. #include <linux/init.h>
  16. #include <linux/errno.h>
  17. #include <linux/pci.h>
  18. #include <linux/delay.h>
  19. #include <linux/types.h>
  20. #include <linux/ioport.h>
  21. #include <asm/overdrive/overdrive.h>
  22. #include <asm/overdrive/gt64111.h>
  23. /* After boot, we shift the Galileo registers so that they appear
  24. * in BANK6, along with IO space. This means we can have one contingous
  25. * lump of PCI address space without these registers appearing in the
  26. * middle of them
  27. */
  28. #define GT64111_BASE_ADDRESS 0xbb000000
  29. #define GT64111_IO_BASE_ADDRESS 0x1000
  30. /* The GT64111 registers appear at this address to the SH4 after reset */
  31. #define RESET_GT64111_BASE_ADDRESS 0xb4000000
  32. /* Macros used to access the Galileo registers */
  33. #define RESET_GT64111_REG(x) (RESET_GT64111_BASE_ADDRESS+x)
  34. #define GT64111_REG(x) (GT64111_BASE_ADDRESS+x)
  35. #define RESET_GT_WRITE(x,v) writel((v),RESET_GT64111_REG(x))
  36. #define RESET_GT_READ(x) readl(RESET_GT64111_REG(x))
  37. #define GT_WRITE(x,v) writel((v),GT64111_REG(x))
  38. #define GT_WRITE_BYTE(x,v) writeb((v),GT64111_REG(x))
  39. #define GT_WRITE_SHORT(x,v) writew((v),GT64111_REG(x))
  40. #define GT_READ(x) readl(GT64111_REG(x))
  41. #define GT_READ_BYTE(x) readb(GT64111_REG(x))
  42. #define GT_READ_SHORT(x) readw(GT64111_REG(x))
  43. /* Where the various SH banks start at */
  44. #define SH_BANK4_ADR 0xb0000000
  45. #define SH_BANK5_ADR 0xb4000000
  46. #define SH_BANK6_ADR 0xb8000000
  47. /* Masks out everything but lines 28,27,26 */
  48. #define BANK_SELECT_MASK 0x1c000000
  49. #define SH4_TO_BANK(x) ( (x) & BANK_SELECT_MASK)
  50. /*
  51. * Masks used for address conversaion. Bank 6 is used for IO and
  52. * has all the address bits zeroed by the FPGA. Special case this
  53. */
  54. #define MEMORY_BANK_MASK 0x1fffffff
  55. #define IO_BANK_MASK 0x03ffffff
  56. /* Mark bank 6 as the bank used for IO. You can change this in the FPGA code
  57. * if you want
  58. */
  59. #define IO_BANK_ADR PCI_GTIO_BASE
  60. /* Will select the correct mask to apply depending on the SH$ address */
  61. #define SELECT_BANK_MASK(x) \
  62. ( (SH4_TO_BANK(x)==SH4_TO_BANK(IO_BANK_ADR)) ? IO_BANK_MASK : MEMORY_BANK_MASK)
  63. /* Converts between PCI space and P2 region */
  64. #define SH4_TO_PCI(x) ((x)&SELECT_BANK_MASK(x))
  65. /* Various macros for figuring out what to stick in the Galileo registers.
  66. * You *really* don't want to figure this stuff out by hand, you always get
  67. * it wrong
  68. */
  69. #define GT_MEM_LO_ADR(x) ((((unsigned)((x)&SELECT_BANK_MASK(x)))>>21)&0x7ff)
  70. #define GT_MEM_HI_ADR(x) ((((unsigned)((x)&SELECT_BANK_MASK(x)))>>21)&0x7f)
  71. #define GT_MEM_SUB_ADR(x) ((((unsigned)((x)&SELECT_BANK_MASK(x)))>>20)&0xff)
  72. #define PROGRAM_HI_LO(block,a,s) \
  73. GT_WRITE(block##_LO_DEC_ADR,GT_MEM_LO_ADR(a));\
  74. GT_WRITE(block##_HI_DEC_ADR,GT_MEM_HI_ADR(a+s-1))
  75. #define PROGRAM_SUB_HI_LO(block,a,s) \
  76. GT_WRITE(block##_LO_DEC_ADR,GT_MEM_SUB_ADR(a));\
  77. GT_WRITE(block##_HI_DEC_ADR,GT_MEM_SUB_ADR(a+s-1))
  78. /* We need to set the size, and the offset register */
  79. #define GT_BAR_MASK(x) ((x)&~0xfff)
  80. /* Macro to set up the BAR in the Galileo. Essentially used for the DRAM */
  81. #define PROGRAM_GT_BAR(block,a,s) \
  82. GT_WRITE(PCI_##block##_BANK_SIZE,GT_BAR_MASK((s-1)));\
  83. write_config_to_galileo(PCI_CONFIG_##block##_BASE_ADR,\
  84. GT_BAR_MASK(a))
  85. #define DISABLE_GT_BAR(block) \
  86. GT_WRITE(PCI_##block##_BANK_SIZE,0),\
  87. GT_CONFIG_WRITE(PCI_CONFIG_##block##_BASE_ADR,\
  88. 0x80000000)
  89. /* Macros to disable things we are not going to use */
  90. #define DISABLE_DECODE(x) GT_WRITE(x##_LO_DEC_ADR,0x7ff);\
  91. GT_WRITE(x##_HI_DEC_ADR,0x00)
  92. #define DISABLE_SUB_DECODE(x) GT_WRITE(x##_LO_DEC_ADR,0xff);\
  93. GT_WRITE(x##_HI_DEC_ADR,0x00)
  94. static void __init reset_pci(void)
  95. {
  96. /* Set RESET_PCI bit high */
  97. writeb(readb(OVERDRIVE_CTRL) | ENABLE_PCI_BIT, OVERDRIVE_CTRL);
  98. udelay(250);
  99. /* Set RESET_PCI bit low */
  100. writeb(readb(OVERDRIVE_CTRL) & RESET_PCI_MASK, OVERDRIVE_CTRL);
  101. udelay(250);
  102. writeb(readb(OVERDRIVE_CTRL) | ENABLE_PCI_BIT, OVERDRIVE_CTRL);
  103. udelay(250);
  104. }
  105. static int write_config_to_galileo(int where, u32 val);
  106. #define GT_CONFIG_WRITE(where,val) write_config_to_galileo(where,val)
  107. #define ENABLE_PCI_DRAM
  108. #ifdef TEST_DRAM
  109. /* Test function to check out if the PCI DRAM is working OK */
  110. static int /* __init */ test_dram(unsigned *base, unsigned size)
  111. {
  112. unsigned *p = base;
  113. unsigned *end = (unsigned *) (((unsigned) base) + size);
  114. unsigned w;
  115. for (p = base; p < end; p++) {
  116. *p = 0xffffffff;
  117. if (*p != 0xffffffff) {
  118. printk("AAARGH -write failed!!! at %p is %x\n", p,
  119. *p);
  120. return 0;
  121. }
  122. *p = 0x0;
  123. if (*p != 0x0) {
  124. printk("AAARGH -write failed!!!\n");
  125. return 0;
  126. }
  127. }
  128. for (p = base; p < end; p++) {
  129. *p = (unsigned) p;
  130. if (*p != (unsigned) p) {
  131. printk("Failed at 0x%p, actually is 0x%x\n", p,
  132. *p);
  133. return 0;
  134. }
  135. }
  136. for (p = base; p < end; p++) {
  137. w = ((unsigned) p & 0xffff0000);
  138. *p = w | (w >> 16);
  139. }
  140. for (p = base; p < end; p++) {
  141. w = ((unsigned) p & 0xffff0000);
  142. w |= (w >> 16);
  143. if (*p != w) {
  144. printk
  145. ("Failed at 0x%p, should be 0x%x actually is 0x%x\n",
  146. p, w, *p);
  147. return 0;
  148. }
  149. }
  150. return 1;
  151. }
  152. #endif
  153. /* Function to set up and initialise the galileo. This sets up the BARS,
  154. * maps the DRAM into the address space etc,etc
  155. */
  156. int __init galileo_init(void)
  157. {
  158. reset_pci();
  159. /* Now shift the galileo regs into this block */
  160. RESET_GT_WRITE(INTERNAL_SPACE_DEC,
  161. GT_MEM_LO_ADR(GT64111_BASE_ADDRESS));
  162. /* Should have a sanity check here, that you can read back at the new
  163. * address what you just wrote
  164. */
  165. /* Disable decode for all regions */
  166. DISABLE_DECODE(RAS10);
  167. DISABLE_DECODE(RAS32);
  168. DISABLE_DECODE(CS20);
  169. DISABLE_DECODE(CS3);
  170. DISABLE_DECODE(PCI_IO);
  171. DISABLE_DECODE(PCI_MEM0);
  172. DISABLE_DECODE(PCI_MEM1);
  173. /* Disable all BARS */
  174. GT_WRITE(BAR_ENABLE_ADR, 0x1ff);
  175. DISABLE_GT_BAR(RAS10);
  176. DISABLE_GT_BAR(RAS32);
  177. DISABLE_GT_BAR(CS20);
  178. DISABLE_GT_BAR(CS3);
  179. /* Tell the BAR where the IO registers now are */
  180. GT_CONFIG_WRITE(PCI_CONFIG_INT_REG_IO_ADR,GT_BAR_MASK(
  181. (GT64111_IO_BASE_ADDRESS &
  182. IO_BANK_MASK)));
  183. /* set up a 112 Mb decode */
  184. PROGRAM_HI_LO(PCI_MEM0, SH_BANK4_ADR, 112 * 1024 * 1024);
  185. /* Set up a 32 MB io space decode */
  186. PROGRAM_HI_LO(PCI_IO, IO_BANK_ADR, 32 * 1024 * 1024);
  187. #ifdef ENABLE_PCI_DRAM
  188. /* Program up the DRAM configuration - there is DRAM only in bank 0 */
  189. /* Now set up the DRAM decode */
  190. PROGRAM_HI_LO(RAS10, PCI_DRAM_BASE, PCI_DRAM_SIZE);
  191. /* And the sub decode */
  192. PROGRAM_SUB_HI_LO(RAS0, PCI_DRAM_BASE, PCI_DRAM_SIZE);
  193. DISABLE_SUB_DECODE(RAS1);
  194. /* Set refresh rate */
  195. GT_WRITE(DRAM_BANK0_PARMS, 0x3f);
  196. GT_WRITE(DRAM_CFG, 0x100);
  197. /* we have to lob off the top bits rememeber!! */
  198. PROGRAM_GT_BAR(RAS10, SH4_TO_PCI(PCI_DRAM_BASE), PCI_DRAM_SIZE);
  199. #endif
  200. /* We are only interested in decoding RAS10 and the Galileo's internal
  201. * registers (as IO) on the PCI bus
  202. */
  203. #ifdef ENABLE_PCI_DRAM
  204. GT_WRITE(BAR_ENABLE_ADR, (~((1 << 8) | (1 << 3))) & 0x1ff);
  205. #else
  206. GT_WRITE(BAR_ENABLE_ADR, (~(1 << 3)) & 0x1ff);
  207. #endif
  208. /* Change the class code to host bridge, it actually powers up
  209. * as a memory controller
  210. */
  211. GT_CONFIG_WRITE(8, 0x06000011);
  212. /* Allow the galileo to master the PCI bus */
  213. GT_CONFIG_WRITE(PCI_COMMAND,
  214. PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER |
  215. PCI_COMMAND_IO);
  216. #if 0
  217. printk("Testing PCI DRAM - ");
  218. if(test_dram(PCI_DRAM_BASE,PCI_DRAM_SIZE)) {
  219. printk("Passed\n");
  220. }else {
  221. printk("FAILED\n");
  222. }
  223. #endif
  224. return 0;
  225. }
  226. #define SET_CONFIG_BITS(bus,devfn,where)\
  227. ((1<<31) | ((bus) << 16) | ((devfn) << 8) | ((where) & ~3))
  228. #define CONFIG_CMD(dev, where) SET_CONFIG_BITS((dev)->bus->number,(dev)->devfn,where)
  229. /* This write to the galileo config registers, unlike the functions below, can
  230. * be used before the PCI subsystem has started up
  231. */
  232. static int __init write_config_to_galileo(int where, u32 val)
  233. {
  234. GT_WRITE(PCI_CFG_ADR, SET_CONFIG_BITS(0, 0, where));
  235. GT_WRITE(PCI_CFG_DATA, val);
  236. return 0;
  237. }
  238. /* We exclude the galileo and slot 31, the galileo because I don't know how to stop
  239. * the setup code shagging up the setup I have done on it, and 31 because the whole
  240. * thing locks up if you try to access that slot (which doesn't exist of course anyway
  241. */
  242. #define EXCLUDED_DEV(dev) ((dev->bus->number==0) && ((PCI_SLOT(dev->devfn)==0) || (PCI_SLOT(dev->devfn) == 31)))
  243. static int galileo_read_config_byte(struct pci_dev *dev, int where,
  244. u8 * val)
  245. {
  246. /* I suspect this doesn't work because this drives a special cycle ? */
  247. if (EXCLUDED_DEV(dev)) {
  248. *val = 0xff;
  249. return PCIBIOS_SUCCESSFUL;
  250. }
  251. /* Start the config cycle */
  252. GT_WRITE(PCI_CFG_ADR, CONFIG_CMD(dev, where));
  253. /* Read back the result */
  254. *val = GT_READ_BYTE(PCI_CFG_DATA + (where & 3));
  255. return PCIBIOS_SUCCESSFUL;
  256. }
  257. static int galileo_read_config_word(struct pci_dev *dev, int where,
  258. u16 * val)
  259. {
  260. if (EXCLUDED_DEV(dev)) {
  261. *val = 0xffff;
  262. return PCIBIOS_SUCCESSFUL;
  263. }
  264. GT_WRITE(PCI_CFG_ADR, CONFIG_CMD(dev, where));
  265. *val = GT_READ_SHORT(PCI_CFG_DATA + (where & 2));
  266. return PCIBIOS_SUCCESSFUL;
  267. }
  268. static int galileo_read_config_dword(struct pci_dev *dev, int where,
  269. u32 * val)
  270. {
  271. if (EXCLUDED_DEV(dev)) {
  272. *val = 0xffffffff;
  273. return PCIBIOS_SUCCESSFUL;
  274. }
  275. GT_WRITE(PCI_CFG_ADR, CONFIG_CMD(dev, where));
  276. *val = GT_READ(PCI_CFG_DATA);
  277. return PCIBIOS_SUCCESSFUL;
  278. }
  279. static int galileo_write_config_byte(struct pci_dev *dev, int where,
  280. u8 val)
  281. {
  282. GT_WRITE(PCI_CFG_ADR, CONFIG_CMD(dev, where));
  283. GT_WRITE_BYTE(PCI_CFG_DATA + (where & 3), val);
  284. return PCIBIOS_SUCCESSFUL;
  285. }
  286. static int galileo_write_config_word(struct pci_dev *dev, int where,
  287. u16 val)
  288. {
  289. GT_WRITE(PCI_CFG_ADR, CONFIG_CMD(dev, where));
  290. GT_WRITE_SHORT(PCI_CFG_DATA + (where & 2), val);
  291. return PCIBIOS_SUCCESSFUL;
  292. }
  293. static int galileo_write_config_dword(struct pci_dev *dev, int where,
  294. u32 val)
  295. {
  296. GT_WRITE(PCI_CFG_ADR, CONFIG_CMD(dev, where));
  297. GT_WRITE(PCI_CFG_DATA, val);
  298. return PCIBIOS_SUCCESSFUL;
  299. }
  300. static struct pci_ops pci_config_ops = {
  301. galileo_read_config_byte,
  302. galileo_read_config_word,
  303. galileo_read_config_dword,
  304. galileo_write_config_byte,
  305. galileo_write_config_word,
  306. galileo_write_config_dword
  307. };
  308. /* Everything hangs off this */
  309. static struct pci_bus *pci_root_bus;
  310. static u8 __init no_swizzle(struct pci_dev *dev, u8 * pin)
  311. {
  312. return PCI_SLOT(dev->devfn);
  313. }
  314. static int __init map_od_irq(struct pci_dev *dev, u8 slot, u8 pin)
  315. {
  316. /* Slot 1: Galileo
  317. * Slot 2: PCI Slot 1
  318. * Slot 3: PCI Slot 2
  319. * Slot 4: ESS
  320. */
  321. switch (slot) {
  322. case 2:
  323. return OVERDRIVE_PCI_IRQ1;
  324. case 3:
  325. /* Note this assumes you have a hacked card in slot 2 */
  326. return OVERDRIVE_PCI_IRQ2;
  327. case 4:
  328. return OVERDRIVE_ESS_IRQ;
  329. default:
  330. /* printk("PCI: Unexpected IRQ mapping request for slot %d\n", slot); */
  331. return -1;
  332. }
  333. }
  334. void __init
  335. pcibios_fixup_pbus_ranges(struct pci_bus *bus, struct pbus_set_ranges_data *ranges)
  336. {
  337. ranges->io_start -= bus->resource[0]->start;
  338. ranges->io_end -= bus->resource[0]->start;
  339. ranges->mem_start -= bus->resource[1]->start;
  340. ranges->mem_end -= bus->resource[1]->start;
  341. }
  342. static void __init pci_fixup_ide_bases(struct pci_dev *d)
  343. {
  344. int i;
  345. /*
  346. * PCI IDE controllers use non-standard I/O port decoding, respect it.
  347. */
  348. if ((d->class >> 8) != PCI_CLASS_STORAGE_IDE)
  349. return;
  350. printk("PCI: IDE base address fixup for %s\n", pci_name(d));
  351. for(i=0; i<4; i++) {
  352. struct resource *r = &d->resource[i];
  353. if ((r->start & ~0x80) == 0x374) {
  354. r->start |= 2;
  355. r->end = r->start;
  356. }
  357. }
  358. }
  359. DECLARE_PCI_FIXUP_HEADER(PCI_ANY_ID, PCI_ANY_ID, pci_fixup_ide_bases);
  360. void __init pcibios_init(void)
  361. {
  362. static struct resource galio,galmem;
  363. /* Allocate the registers used by the Galileo */
  364. galio.flags = IORESOURCE_IO;
  365. galio.name = "Galileo GT64011";
  366. galmem.flags = IORESOURCE_MEM|IORESOURCE_PREFETCH;
  367. galmem.name = "Galileo GT64011 DRAM";
  368. allocate_resource(&ioport_resource, &galio, 256,
  369. GT64111_IO_BASE_ADDRESS,GT64111_IO_BASE_ADDRESS+256, 256, NULL, NULL);
  370. allocate_resource(&iomem_resource, &galmem,PCI_DRAM_SIZE,
  371. PHYSADDR(PCI_DRAM_BASE), PHYSADDR(PCI_DRAM_BASE)+PCI_DRAM_SIZE,
  372. PCI_DRAM_SIZE, NULL, NULL);
  373. /* ok, do the scan man */
  374. pci_root_bus = pci_scan_bus(0, &pci_config_ops, NULL);
  375. pci_assign_unassigned_resources();
  376. pci_fixup_irqs(no_swizzle, map_od_irq);
  377. #ifdef TEST_DRAM
  378. printk("Testing PCI DRAM - ");
  379. if(test_dram(PCI_DRAM_BASE,PCI_DRAM_SIZE)) {
  380. printk("Passed\n");
  381. }else {
  382. printk("FAILED\n");
  383. }
  384. #endif
  385. }
  386. char * __init pcibios_setup(char *str)
  387. {
  388. return str;
  389. }
  390. int pcibios_enable_device(struct pci_dev *dev)
  391. {
  392. u16 cmd, old_cmd;
  393. int idx;
  394. struct resource *r;
  395. pci_read_config_word(dev, PCI_COMMAND, &cmd);
  396. old_cmd = cmd;
  397. for (idx = 0; idx < 6; idx++) {
  398. r = dev->resource + idx;
  399. if (!r->start && r->end) {
  400. printk(KERN_ERR
  401. "PCI: Device %s not available because"
  402. " of resource collisions\n",
  403. pci_name(dev));
  404. return -EINVAL;
  405. }
  406. if (r->flags & IORESOURCE_IO)
  407. cmd |= PCI_COMMAND_IO;
  408. if (r->flags & IORESOURCE_MEM)
  409. cmd |= PCI_COMMAND_MEMORY;
  410. }
  411. if (cmd != old_cmd) {
  412. printk("PCI: enabling device %s (%04x -> %04x)\n",
  413. pci_name(dev), old_cmd, cmd);
  414. pci_write_config_word(dev, PCI_COMMAND, cmd);
  415. }
  416. return 0;
  417. }
  418. /* We should do some optimisation work here I think. Ok for now though */
  419. void __init pcibios_fixup_bus(struct pci_bus *bus)
  420. {
  421. }
  422. void pcibios_align_resource(void *data, struct resource *res,
  423. unsigned long size)
  424. {
  425. }
  426. void __init pcibios_update_resource(struct pci_dev *dev, struct resource *root,
  427. struct resource *res, int resource)
  428. {
  429. unsigned long where, size;
  430. u32 reg;
  431. printk("PCI: Assigning %3s %08lx to %s\n",
  432. res->flags & IORESOURCE_IO ? "IO" : "MEM",
  433. res->start, dev->name);
  434. where = PCI_BASE_ADDRESS_0 + resource * 4;
  435. size = res->end - res->start;
  436. pci_read_config_dword(dev, where, &reg);
  437. reg = (reg & size) | (((u32) (res->start - root->start)) & ~size);
  438. pci_write_config_dword(dev, where, reg);
  439. }
  440. void __init pcibios_update_irq(struct pci_dev *dev, int irq)
  441. {
  442. printk("PCI: Assigning IRQ %02d to %s\n", irq, dev->name);
  443. pci_write_config_byte(dev, PCI_INTERRUPT_LINE, irq);
  444. }
  445. /*
  446. * If we set up a device for bus mastering, we need to check the latency
  447. * timer as certain crappy BIOSes forget to set it properly.
  448. */
  449. unsigned int pcibios_max_latency = 255;
  450. void pcibios_set_master(struct pci_dev *dev)
  451. {
  452. u8 lat;
  453. pci_read_config_byte(dev, PCI_LATENCY_TIMER, &lat);
  454. if (lat < 16)
  455. lat = (64 <= pcibios_max_latency) ? 64 : pcibios_max_latency;
  456. else if (lat > pcibios_max_latency)
  457. lat = pcibios_max_latency;
  458. else
  459. return;
  460. printk("PCI: Setting latency timer of device %s to %d\n", pci_name(dev), lat);
  461. pci_write_config_byte(dev, PCI_LATENCY_TIMER, lat);
  462. }