pci-st40.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  1. /*
  2. * Copyright (C) 2001 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. * Support functions for the ST40 PCI hardware.
  8. */
  9. #include <linux/kernel.h>
  10. #include <linux/smp.h>
  11. #include <linux/smp_lock.h>
  12. #include <linux/init.h>
  13. #include <linux/errno.h>
  14. #include <linux/pci.h>
  15. #include <linux/delay.h>
  16. #include <linux/types.h>
  17. #include <asm/pci.h>
  18. #include <linux/irq.h>
  19. #include <linux/interrupt.h> /* irqreturn_t */
  20. #include "pci-st40.h"
  21. /* This is in P2 of course */
  22. #define ST40PCI_BASE_ADDRESS (0xb0000000)
  23. #define ST40PCI_MEM_ADDRESS (ST40PCI_BASE_ADDRESS+0x0)
  24. #define ST40PCI_IO_ADDRESS (ST40PCI_BASE_ADDRESS+0x06000000)
  25. #define ST40PCI_REG_ADDRESS (ST40PCI_BASE_ADDRESS+0x07000000)
  26. #define ST40PCI_REG(x) (ST40PCI_REG_ADDRESS+(ST40PCI_##x))
  27. #define ST40PCI_REG_INDEXED(reg, index) \
  28. (ST40PCI_REG(reg##0) + \
  29. ((ST40PCI_REG(reg##1) - ST40PCI_REG(reg##0))*index))
  30. #define ST40PCI_WRITE(reg,val) writel((val),ST40PCI_REG(reg))
  31. #define ST40PCI_WRITE_SHORT(reg,val) writew((val),ST40PCI_REG(reg))
  32. #define ST40PCI_WRITE_BYTE(reg,val) writeb((val),ST40PCI_REG(reg))
  33. #define ST40PCI_WRITE_INDEXED(reg, index, val) \
  34. writel((val), ST40PCI_REG_INDEXED(reg, index));
  35. #define ST40PCI_READ(reg) readl(ST40PCI_REG(reg))
  36. #define ST40PCI_READ_SHORT(reg) readw(ST40PCI_REG(reg))
  37. #define ST40PCI_READ_BYTE(reg) readb(ST40PCI_REG(reg))
  38. #define ST40PCI_SERR_IRQ 64
  39. #define ST40PCI_ERR_IRQ 65
  40. /* Macros to extract PLL params */
  41. #define PLL_MDIV(reg) ( ((unsigned)reg) & 0xff )
  42. #define PLL_NDIV(reg) ( (((unsigned)reg)>>8) & 0xff )
  43. #define PLL_PDIV(reg) ( (((unsigned)reg)>>16) & 0x3 )
  44. #define PLL_SETUP(reg) ( (((unsigned)reg)>>19) & 0x1ff )
  45. /* Build up the appropriate settings */
  46. #define PLL_SET(mdiv,ndiv,pdiv,setup) \
  47. ( ((mdiv)&0xff) | (((ndiv)&0xff)<<8) | (((pdiv)&3)<<16)| (((setup)&0x1ff)<<19))
  48. #define PLLPCICR (0xbb040000+0x10)
  49. #define PLLPCICR_POWERON (1<<28)
  50. #define PLLPCICR_OUT_EN (1<<29)
  51. #define PLLPCICR_LOCKSELECT (1<<30)
  52. #define PLLPCICR_LOCK (1<<31)
  53. #define PLL_25MHZ 0x793c8512
  54. #define PLL_33MHZ PLL_SET(18,88,3,295)
  55. static void pci_set_rbar_region(unsigned int region, unsigned long localAddr,
  56. unsigned long pciOffset, unsigned long regionSize);
  57. /*
  58. * The pcibios_map_platform_irq function is defined in the appropriate
  59. * board specific code and referenced here
  60. */
  61. extern int __init pcibios_map_platform_irq(struct pci_dev *dev, u8 slot, u8 pin);
  62. static __init void SetPCIPLL(void)
  63. {
  64. {
  65. /* Lets play with the PLL values */
  66. unsigned long pll1cr1;
  67. unsigned long mdiv, ndiv, pdiv;
  68. unsigned long muxcr;
  69. unsigned int muxcr_ratios[4] = { 8, 16, 21, 1 };
  70. unsigned int freq;
  71. #define CLKGENA 0xbb040000
  72. #define CLKGENA_PLL2_MUXCR CLKGENA + 0x48
  73. pll1cr1 = ctrl_inl(PLLPCICR);
  74. printk("PLL1CR1 %08lx\n", pll1cr1);
  75. mdiv = PLL_MDIV(pll1cr1);
  76. ndiv = PLL_NDIV(pll1cr1);
  77. pdiv = PLL_PDIV(pll1cr1);
  78. printk("mdiv %02lx ndiv %02lx pdiv %02lx\n", mdiv, ndiv, pdiv);
  79. freq = ((2*27*ndiv)/mdiv) / (1 << pdiv);
  80. printk("PLL freq %dMHz\n", freq);
  81. muxcr = ctrl_inl(CLKGENA_PLL2_MUXCR);
  82. printk("PCI freq %dMhz\n", freq / muxcr_ratios[muxcr & 3]);
  83. }
  84. }
  85. struct pci_err {
  86. unsigned mask;
  87. const char *error_string;
  88. };
  89. static struct pci_err int_error[]={
  90. { INT_MNLTDIM,"MNLTDIM: Master non-lock transfer"},
  91. { INT_TTADI, "TTADI: Illegal byte enable in I/O transfer"},
  92. { INT_TMTO, "TMTO: Target memory read/write timeout"},
  93. { INT_MDEI, "MDEI: Master function disable error"},
  94. { INT_APEDI, "APEDI: Address parity error"},
  95. { INT_SDI, "SDI: SERR detected"},
  96. { INT_DPEITW, "DPEITW: Data parity error target write"},
  97. { INT_PEDITR, "PEDITR: PERR detected"},
  98. { INT_TADIM, "TADIM: Target abort detected"},
  99. { INT_MADIM, "MADIM: Master abort detected"},
  100. { INT_MWPDI, "MWPDI: PERR from target at data write"},
  101. { INT_MRDPEI, "MRDPEI: Master read data parity error"}
  102. };
  103. #define NUM_PCI_INT_ERRS (sizeof(int_error)/sizeof(struct pci_err))
  104. static struct pci_err aint_error[]={
  105. { AINT_MBI, "MBI: Master broken"},
  106. { AINT_TBTOI, "TBTOI: Target bus timeout"},
  107. { AINT_MBTOI, "MBTOI: Master bus timeout"},
  108. { AINT_TAI, "TAI: Target abort"},
  109. { AINT_MAI, "MAI: Master abort"},
  110. { AINT_RDPEI, "RDPEI: Read data parity"},
  111. { AINT_WDPE, "WDPE: Write data parity"}
  112. };
  113. #define NUM_PCI_AINT_ERRS (sizeof(aint_error)/sizeof(struct pci_err))
  114. static void print_pci_errors(unsigned reg,struct pci_err *error,int num_errors)
  115. {
  116. int i;
  117. for(i=0;i<num_errors;i++) {
  118. if(reg & error[i].mask) {
  119. printk("%s\n",error[i].error_string);
  120. }
  121. }
  122. }
  123. static char * pci_commands[16]={
  124. "Int Ack",
  125. "Special Cycle",
  126. "I/O Read",
  127. "I/O Write",
  128. "Reserved",
  129. "Reserved",
  130. "Memory Read",
  131. "Memory Write",
  132. "Reserved",
  133. "Reserved",
  134. "Configuration Read",
  135. "Configuration Write",
  136. "Memory Read Multiple",
  137. "Dual Address Cycle",
  138. "Memory Read Line",
  139. "Memory Write-and-Invalidate"
  140. };
  141. static irqreturn_t st40_pci_irq(int irq, void *dev_instance, struct pt_regs *regs)
  142. {
  143. unsigned pci_int, pci_air, pci_cir, pci_aint;
  144. static int count=0;
  145. pci_int = ST40PCI_READ(INT);pci_aint = ST40PCI_READ(AINT);
  146. pci_cir = ST40PCI_READ(CIR);pci_air = ST40PCI_READ(AIR);
  147. /* Reset state to stop multiple interrupts */
  148. ST40PCI_WRITE(INT, ~0); ST40PCI_WRITE(AINT, ~0);
  149. if(++count>1) return IRQ_HANDLED;
  150. printk("** PCI ERROR **\n");
  151. if(pci_int) {
  152. printk("** INT register status\n");
  153. print_pci_errors(pci_int,int_error,NUM_PCI_INT_ERRS);
  154. }
  155. if(pci_aint) {
  156. printk("** AINT register status\n");
  157. print_pci_errors(pci_aint,aint_error,NUM_PCI_AINT_ERRS);
  158. }
  159. printk("** Address and command info\n");
  160. printk("** Command %s : Address 0x%x\n",
  161. pci_commands[pci_cir&0xf],pci_air);
  162. if(pci_cir&CIR_PIOTEM) {
  163. printk("CIR_PIOTEM:PIO transfer error for master\n");
  164. }
  165. if(pci_cir&CIR_RWTET) {
  166. printk("CIR_RWTET:Read/Write transfer error for target\n");
  167. }
  168. return IRQ_HANDLED;
  169. }
  170. /* Rounds a number UP to the nearest power of two. Used for
  171. * sizing the PCI window.
  172. */
  173. static u32 r2p2(u32 num)
  174. {
  175. int i = 31;
  176. u32 tmp = num;
  177. if (num == 0)
  178. return 0;
  179. do {
  180. if (tmp & (1 << 31))
  181. break;
  182. i--;
  183. tmp <<= 1;
  184. } while (i >= 0);
  185. tmp = 1 << i;
  186. /* If the original number isn't a power of 2, round it up */
  187. if (tmp != num)
  188. tmp <<= 1;
  189. return tmp;
  190. }
  191. static void __init pci_fixup_ide_bases(struct pci_dev *d)
  192. {
  193. int i;
  194. /*
  195. * PCI IDE controllers use non-standard I/O port decoding, respect it.
  196. */
  197. if ((d->class >> 8) != PCI_CLASS_STORAGE_IDE)
  198. return;
  199. printk("PCI: IDE base address fixup for %s\n", pci_name(d));
  200. for(i=0; i<4; i++) {
  201. struct resource *r = &d->resource[i];
  202. if ((r->start & ~0x80) == 0x374) {
  203. r->start |= 2;
  204. r->end = r->start;
  205. }
  206. }
  207. }
  208. DECLARE_PCI_FIXUP_HEADER(PCI_ANY_ID, PCI_ANY_ID, pci_fixup_ide_bases);
  209. int __init st40pci_init(unsigned memStart, unsigned memSize)
  210. {
  211. u32 lsr0;
  212. SetPCIPLL();
  213. /* Initialises the ST40 pci subsystem, performing a reset, then programming
  214. * up the address space decoders appropriately
  215. */
  216. /* Should reset core here as well methink */
  217. ST40PCI_WRITE(CR, CR_LOCK_MASK | CR_SOFT_RESET);
  218. /* Loop while core resets */
  219. while (ST40PCI_READ(CR) & CR_SOFT_RESET);
  220. /* Switch off interrupts */
  221. ST40PCI_WRITE(INTM, 0);
  222. ST40PCI_WRITE(AINT, 0);
  223. /* Now, lets reset all the cards on the bus with extreme prejudice */
  224. ST40PCI_WRITE(CR, CR_LOCK_MASK | CR_RSTCTL);
  225. udelay(250);
  226. /* Set bus active, take it out of reset */
  227. ST40PCI_WRITE(CR, CR_LOCK_MASK | CR_BMAM | CR_CFINT | CR_PFCS | CR_PFE);
  228. /* The PCI spec says that no access must be made to the bus until 1 second
  229. * after reset. This seem ludicrously long, but some delay is needed here
  230. */
  231. mdelay(1000);
  232. /* Switch off interrupts */
  233. ST40PCI_WRITE(INTM, 0);
  234. ST40PCI_WRITE(AINT, 0);
  235. /* Allow it to be a master */
  236. ST40PCI_WRITE_SHORT(CSR_CMD,
  237. PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER |
  238. PCI_COMMAND_IO);
  239. /* Accesse to the 0xb0000000 -> 0xb6000000 area will go through to 0x10000000 -> 0x16000000
  240. * on the PCI bus. This allows a nice 1-1 bus to phys mapping.
  241. */
  242. ST40PCI_WRITE(MBR, 0x10000000);
  243. /* Always set the max size 128M (actually, it is only 96MB wide) */
  244. ST40PCI_WRITE(MBMR, 0x07ff0000);
  245. /* I/O addresses are mapped at 0xb6000000 -> 0xb7000000. These are changed to 0, to
  246. * allow cards that have legacy io such as vga to function correctly. This gives a
  247. * maximum of 64K of io/space as only the bottom 16 bits of the address are copied
  248. * over to the bus when the transaction is made. 64K of io space is more than enough
  249. */
  250. ST40PCI_WRITE(IOBR, 0x0);
  251. /* Set up the 64K window */
  252. ST40PCI_WRITE(IOBMR, 0x0);
  253. /* Now we set up the mbars so the PCI bus can see the local memory */
  254. /* Expose a 256M window starting at PCI address 0... */
  255. ST40PCI_WRITE(CSR_MBAR0, 0);
  256. ST40PCI_WRITE(LSR0, 0x0fff0001);
  257. /* ... and set up the initial incomming window to expose all of RAM */
  258. pci_set_rbar_region(7, memStart, memStart, memSize);
  259. /* Maximise timeout values */
  260. ST40PCI_WRITE_BYTE(CSR_TRDY, 0xff);
  261. ST40PCI_WRITE_BYTE(CSR_RETRY, 0xff);
  262. ST40PCI_WRITE_BYTE(CSR_MIT, 0xff);
  263. ST40PCI_WRITE_BYTE(PERF,PERF_MASTER_WRITE_POSTING);
  264. return 1;
  265. }
  266. char * __init pcibios_setup(char *str)
  267. {
  268. return str;
  269. }
  270. #define SET_CONFIG_BITS(bus,devfn,where)\
  271. (((bus) << 16) | ((devfn) << 8) | ((where) & ~3) | (bus!=0))
  272. #define CONFIG_CMD(bus, devfn, where) SET_CONFIG_BITS(bus->number,devfn,where)
  273. static int CheckForMasterAbort(void)
  274. {
  275. if (ST40PCI_READ(INT) & INT_MADIM) {
  276. /* Should we clear config space version as well ??? */
  277. ST40PCI_WRITE(INT, INT_MADIM);
  278. ST40PCI_WRITE_SHORT(CSR_STATUS, 0);
  279. return 1;
  280. }
  281. return 0;
  282. }
  283. /* Write to config register */
  284. static int st40pci_read(struct pci_bus *bus, unsigned int devfn, int where, int size, u32 * val)
  285. {
  286. ST40PCI_WRITE(PAR, CONFIG_CMD(bus, devfn, where));
  287. switch (size) {
  288. case 1:
  289. *val = (u8)ST40PCI_READ_BYTE(PDR + (where & 3));
  290. break;
  291. case 2:
  292. *val = (u16)ST40PCI_READ_SHORT(PDR + (where & 2));
  293. break;
  294. case 4:
  295. *val = ST40PCI_READ(PDR);
  296. break;
  297. }
  298. if (CheckForMasterAbort()){
  299. switch (size) {
  300. case 1:
  301. *val = (u8)0xff;
  302. break;
  303. case 2:
  304. *val = (u16)0xffff;
  305. break;
  306. case 4:
  307. *val = 0xffffffff;
  308. break;
  309. }
  310. }
  311. return PCIBIOS_SUCCESSFUL;
  312. }
  313. static int st40pci_write(struct pci_bus *bus, unsigned int devfn, int where, int size, u32 val)
  314. {
  315. ST40PCI_WRITE(PAR, CONFIG_CMD(bus, devfn, where));
  316. switch (size) {
  317. case 1:
  318. ST40PCI_WRITE_BYTE(PDR + (where & 3), (u8)val);
  319. break;
  320. case 2:
  321. ST40PCI_WRITE_SHORT(PDR + (where & 2), (u16)val);
  322. break;
  323. case 4:
  324. ST40PCI_WRITE(PDR, val);
  325. break;
  326. }
  327. CheckForMasterAbort();
  328. return PCIBIOS_SUCCESSFUL;
  329. }
  330. struct pci_ops st40pci_config_ops = {
  331. .read = st40pci_read,
  332. .write = st40pci_write,
  333. };
  334. /* Everything hangs off this */
  335. static struct pci_bus *pci_root_bus;
  336. static u8 __init no_swizzle(struct pci_dev *dev, u8 * pin)
  337. {
  338. return PCI_SLOT(dev->devfn);
  339. }
  340. static int __init pcibios_init(void)
  341. {
  342. extern unsigned long memory_start, memory_end;
  343. printk(KERN_ALERT "pci-st40.c: pcibios_init\n");
  344. if (sh_mv.mv_init_pci != NULL) {
  345. sh_mv.mv_init_pci();
  346. }
  347. /* The pci subsytem needs to know where memory is and how much
  348. * of it there is. I've simply made these globals. A better mechanism
  349. * is probably needed.
  350. */
  351. st40pci_init(PHYSADDR(memory_start),
  352. PHYSADDR(memory_end) - PHYSADDR(memory_start));
  353. if (request_irq(ST40PCI_ERR_IRQ, st40_pci_irq,
  354. IRQF_DISABLED, "st40pci", NULL)) {
  355. printk(KERN_ERR "st40pci: Cannot hook interrupt\n");
  356. return -EIO;
  357. }
  358. /* Enable the PCI interrupts on the device */
  359. ST40PCI_WRITE(INTM, ~0);
  360. ST40PCI_WRITE(AINT, ~0);
  361. /* Map the io address apprioately */
  362. #ifdef CONFIG_HD64465
  363. hd64465_port_map(PCIBIOS_MIN_IO, (64 * 1024) - PCIBIOS_MIN_IO + 1,
  364. ST40_IO_ADDR + PCIBIOS_MIN_IO, 0);
  365. #endif
  366. /* ok, do the scan man */
  367. pci_root_bus = pci_scan_bus(0, &st40pci_config_ops, NULL);
  368. pci_assign_unassigned_resources();
  369. pci_fixup_irqs(no_swizzle, pcibios_map_platform_irq);
  370. return 0;
  371. }
  372. subsys_initcall(pcibios_init);
  373. void __init pcibios_fixup_bus(struct pci_bus *bus)
  374. {
  375. }
  376. /*
  377. * Publish a region of local address space over the PCI bus
  378. * to other devices.
  379. */
  380. static void pci_set_rbar_region(unsigned int region, unsigned long localAddr,
  381. unsigned long pciOffset, unsigned long regionSize)
  382. {
  383. unsigned long mask;
  384. if (region > 7)
  385. return;
  386. if (regionSize > (512 * 1024 * 1024))
  387. return;
  388. mask = r2p2(regionSize) - 0x10000;
  389. /* Diable the region (in case currently in use, should never happen) */
  390. ST40PCI_WRITE_INDEXED(RSR, region, 0);
  391. /* Start of local address space to publish */
  392. ST40PCI_WRITE_INDEXED(RLAR, region, PHYSADDR(localAddr) );
  393. /* Start of region in PCI address space as an offset from MBAR0 */
  394. ST40PCI_WRITE_INDEXED(RBAR, region, pciOffset);
  395. /* Size of region */
  396. ST40PCI_WRITE_INDEXED(RSR, region, mask | 1);
  397. }