pci-st40.c 13 KB

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