galileo.c 15 KB

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