pci-st40.c 12 KB

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