pci-st40.c 12 KB

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