pci-sh7780.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. /*
  2. * Low-Level PCI Support for the SH7780
  3. *
  4. * Copyright (C) 2005 - 2010 Paul Mundt
  5. *
  6. * This file is subject to the terms and conditions of the GNU General Public
  7. * License. See the file "COPYING" in the main directory of this archive
  8. * for more details.
  9. */
  10. #include <linux/types.h>
  11. #include <linux/kernel.h>
  12. #include <linux/init.h>
  13. #include <linux/pci.h>
  14. #include <linux/interrupt.h>
  15. #include <linux/timer.h>
  16. #include <linux/irq.h>
  17. #include <linux/errno.h>
  18. #include <linux/delay.h>
  19. #include <linux/log2.h>
  20. #include "pci-sh4.h"
  21. #include <asm/mmu.h>
  22. #include <asm/sizes.h>
  23. static struct resource sh7785_io_resource = {
  24. .name = "SH7785_IO",
  25. .start = 0x1000,
  26. .end = SH7780_PCI_IO_SIZE - 1,
  27. .flags = IORESOURCE_IO
  28. };
  29. static struct resource sh7785_mem_resource = {
  30. .name = "SH7785_mem",
  31. .start = SH7780_PCI_MEMORY_BASE,
  32. .end = SH7780_PCI_MEMORY_BASE + SH7780_PCI_MEM_SIZE - 1,
  33. .flags = IORESOURCE_MEM
  34. };
  35. static struct pci_channel sh7780_pci_controller = {
  36. .pci_ops = &sh4_pci_ops,
  37. .mem_resource = &sh7785_mem_resource,
  38. .mem_offset = 0x00000000,
  39. .io_resource = &sh7785_io_resource,
  40. .io_offset = 0x00000000,
  41. .io_map_base = SH7780_PCI_IO_BASE,
  42. .serr_irq = evt2irq(0xa00),
  43. .err_irq = evt2irq(0xaa0),
  44. };
  45. struct pci_errors {
  46. unsigned int mask;
  47. const char *str;
  48. } pci_arbiter_errors[] = {
  49. { SH4_PCIAINT_MBKN, "master broken" },
  50. { SH4_PCIAINT_TBTO, "target bus time out" },
  51. { SH4_PCIAINT_MBTO, "master bus time out" },
  52. { SH4_PCIAINT_TABT, "target abort" },
  53. { SH4_PCIAINT_MABT, "master abort" },
  54. { SH4_PCIAINT_RDPE, "read data parity error" },
  55. { SH4_PCIAINT_WDPE, "write data parity error" },
  56. }, pci_interrupt_errors[] = {
  57. { SH4_PCIINT_MLCK, "master lock error" },
  58. { SH4_PCIINT_TABT, "target-target abort" },
  59. { SH4_PCIINT_TRET, "target retry time out" },
  60. { SH4_PCIINT_MFDE, "master function disable erorr" },
  61. { SH4_PCIINT_PRTY, "address parity error" },
  62. { SH4_PCIINT_SERR, "SERR" },
  63. { SH4_PCIINT_TWDP, "data parity error for target write" },
  64. { SH4_PCIINT_TRDP, "PERR detected for target read" },
  65. { SH4_PCIINT_MTABT, "target abort for master" },
  66. { SH4_PCIINT_MMABT, "master abort for master" },
  67. { SH4_PCIINT_MWPD, "master write data parity error" },
  68. { SH4_PCIINT_MRPD, "master read data parity error" },
  69. };
  70. static irqreturn_t sh7780_pci_err_irq(int irq, void *dev_id)
  71. {
  72. struct pci_channel *hose = dev_id;
  73. unsigned long addr;
  74. unsigned int status;
  75. unsigned int cmd;
  76. int i;
  77. addr = __raw_readl(hose->reg_base + SH4_PCIALR);
  78. /*
  79. * Handle status errors.
  80. */
  81. status = __raw_readw(hose->reg_base + PCI_STATUS);
  82. if (status & (PCI_STATUS_PARITY |
  83. PCI_STATUS_DETECTED_PARITY |
  84. PCI_STATUS_SIG_TARGET_ABORT |
  85. PCI_STATUS_REC_TARGET_ABORT |
  86. PCI_STATUS_REC_MASTER_ABORT)) {
  87. cmd = pcibios_handle_status_errors(addr, status, hose);
  88. if (likely(cmd))
  89. __raw_writew(cmd, hose->reg_base + PCI_STATUS);
  90. }
  91. /*
  92. * Handle arbiter errors.
  93. */
  94. status = __raw_readl(hose->reg_base + SH4_PCIAINT);
  95. for (i = cmd = 0; i < ARRAY_SIZE(pci_arbiter_errors); i++) {
  96. if (status & pci_arbiter_errors[i].mask) {
  97. printk(KERN_DEBUG "PCI: %s, addr=%08lx\n",
  98. pci_arbiter_errors[i].str, addr);
  99. cmd |= pci_arbiter_errors[i].mask;
  100. }
  101. }
  102. __raw_writel(cmd, hose->reg_base + SH4_PCIAINT);
  103. /*
  104. * Handle the remaining PCI errors.
  105. */
  106. status = __raw_readl(hose->reg_base + SH4_PCIINT);
  107. for (i = cmd = 0; i < ARRAY_SIZE(pci_interrupt_errors); i++) {
  108. if (status & pci_interrupt_errors[i].mask) {
  109. printk(KERN_DEBUG "PCI: %s, addr=%08lx\n",
  110. pci_interrupt_errors[i].str, addr);
  111. cmd |= pci_interrupt_errors[i].mask;
  112. }
  113. }
  114. __raw_writel(cmd, hose->reg_base + SH4_PCIINT);
  115. return IRQ_HANDLED;
  116. }
  117. static irqreturn_t sh7780_pci_serr_irq(int irq, void *dev_id)
  118. {
  119. struct pci_channel *hose = dev_id;
  120. printk(KERN_DEBUG "PCI: system error received: ");
  121. pcibios_report_status(PCI_STATUS_SIG_SYSTEM_ERROR, 1);
  122. printk("\n");
  123. /* Deassert SERR */
  124. __raw_writel(SH4_PCIINTM_SDIM, hose->reg_base + SH4_PCIINTM);
  125. /* Back off the IRQ for awhile */
  126. disable_irq(irq);
  127. hose->serr_timer.expires = jiffies + HZ;
  128. add_timer(&hose->serr_timer);
  129. return IRQ_HANDLED;
  130. }
  131. static int __init sh7780_pci_setup_irqs(struct pci_channel *hose)
  132. {
  133. int ret;
  134. /* Clear out PCI arbiter IRQs */
  135. __raw_writel(0, hose->reg_base + SH4_PCIAINT);
  136. /* Clear all error conditions */
  137. __raw_writew(PCI_STATUS_DETECTED_PARITY | \
  138. PCI_STATUS_SIG_SYSTEM_ERROR | \
  139. PCI_STATUS_REC_MASTER_ABORT | \
  140. PCI_STATUS_REC_TARGET_ABORT | \
  141. PCI_STATUS_SIG_TARGET_ABORT | \
  142. PCI_STATUS_PARITY, hose->reg_base + PCI_STATUS);
  143. ret = request_irq(hose->serr_irq, sh7780_pci_serr_irq, IRQF_DISABLED,
  144. "PCI SERR interrupt", hose);
  145. if (unlikely(ret)) {
  146. printk(KERN_ERR "PCI: Failed hooking SERR IRQ\n");
  147. return ret;
  148. }
  149. /*
  150. * The PCI ERR IRQ needs to be IRQF_SHARED since all of the power
  151. * down IRQ vectors are routed through the ERR IRQ vector. We
  152. * only request_irq() once as there is only a single masking
  153. * source for multiple events.
  154. */
  155. ret = request_irq(hose->err_irq, sh7780_pci_err_irq, IRQF_SHARED,
  156. "PCI ERR interrupt", hose);
  157. if (unlikely(ret)) {
  158. free_irq(hose->serr_irq, hose);
  159. return ret;
  160. }
  161. /* Unmask all of the arbiter IRQs. */
  162. __raw_writel(SH4_PCIAINT_MBKN | SH4_PCIAINT_TBTO | SH4_PCIAINT_MBTO | \
  163. SH4_PCIAINT_TABT | SH4_PCIAINT_MABT | SH4_PCIAINT_RDPE | \
  164. SH4_PCIAINT_WDPE, hose->reg_base + SH4_PCIAINTM);
  165. /* Unmask all of the PCI IRQs */
  166. __raw_writel(SH4_PCIINTM_TTADIM | SH4_PCIINTM_TMTOIM | \
  167. SH4_PCIINTM_MDEIM | SH4_PCIINTM_APEDIM | \
  168. SH4_PCIINTM_SDIM | SH4_PCIINTM_DPEITWM | \
  169. SH4_PCIINTM_PEDITRM | SH4_PCIINTM_TADIMM | \
  170. SH4_PCIINTM_MADIMM | SH4_PCIINTM_MWPDIM | \
  171. SH4_PCIINTM_MRDPEIM, hose->reg_base + SH4_PCIINTM);
  172. return ret;
  173. }
  174. static inline void __init sh7780_pci_teardown_irqs(struct pci_channel *hose)
  175. {
  176. free_irq(hose->err_irq, hose);
  177. free_irq(hose->serr_irq, hose);
  178. }
  179. static void __init sh7780_pci66_init(struct pci_channel *hose)
  180. {
  181. unsigned int tmp;
  182. if (!pci_is_66mhz_capable(hose, 0, 0))
  183. return;
  184. /* Enable register access */
  185. tmp = __raw_readl(hose->reg_base + SH4_PCICR);
  186. tmp |= SH4_PCICR_PREFIX;
  187. __raw_writel(tmp, hose->reg_base + SH4_PCICR);
  188. /* Enable 66MHz operation */
  189. tmp = __raw_readw(hose->reg_base + PCI_STATUS);
  190. tmp |= PCI_STATUS_66MHZ;
  191. __raw_writew(tmp, hose->reg_base + PCI_STATUS);
  192. /* Done */
  193. tmp = __raw_readl(hose->reg_base + SH4_PCICR);
  194. tmp |= SH4_PCICR_PREFIX | SH4_PCICR_CFIN;
  195. __raw_writel(tmp, hose->reg_base + SH4_PCICR);
  196. }
  197. static int __init sh7780_pci_init(void)
  198. {
  199. struct pci_channel *chan = &sh7780_pci_controller;
  200. phys_addr_t memphys;
  201. size_t memsize;
  202. unsigned int id;
  203. const char *type;
  204. int ret;
  205. printk(KERN_NOTICE "PCI: Starting intialization.\n");
  206. chan->reg_base = 0xfe040000;
  207. /* Enable CPU access to the PCIC registers. */
  208. __raw_writel(PCIECR_ENBL, PCIECR);
  209. /* Reset */
  210. __raw_writel(SH4_PCICR_PREFIX | SH4_PCICR_PRST,
  211. chan->reg_base + SH4_PCICR);
  212. /*
  213. * Wait for it to come back up. The spec says to allow for up to
  214. * 1 second after toggling the reset pin, but in practice 100ms
  215. * is more than enough.
  216. */
  217. mdelay(100);
  218. id = __raw_readw(chan->reg_base + PCI_VENDOR_ID);
  219. if (id != PCI_VENDOR_ID_RENESAS) {
  220. printk(KERN_ERR "PCI: Unknown vendor ID 0x%04x.\n", id);
  221. return -ENODEV;
  222. }
  223. id = __raw_readw(chan->reg_base + PCI_DEVICE_ID);
  224. type = (id == PCI_DEVICE_ID_RENESAS_SH7763) ? "SH7763" :
  225. (id == PCI_DEVICE_ID_RENESAS_SH7780) ? "SH7780" :
  226. (id == PCI_DEVICE_ID_RENESAS_SH7781) ? "SH7781" :
  227. (id == PCI_DEVICE_ID_RENESAS_SH7785) ? "SH7785" :
  228. NULL;
  229. if (unlikely(!type)) {
  230. printk(KERN_ERR "PCI: Found an unsupported Renesas host "
  231. "controller, device id 0x%04x.\n", id);
  232. return -EINVAL;
  233. }
  234. printk(KERN_NOTICE "PCI: Found a Renesas %s host "
  235. "controller, revision %d.\n", type,
  236. __raw_readb(chan->reg_base + PCI_REVISION_ID));
  237. /*
  238. * Now throw it in to register initialization mode and
  239. * start the real work.
  240. */
  241. __raw_writel(SH4_PCICR_PREFIX, chan->reg_base + SH4_PCICR);
  242. __raw_writel(0, chan->reg_base + PCI_BASE_ADDRESS_0);
  243. memphys = __pa(memory_start);
  244. memsize = roundup_pow_of_two(memory_end - memory_start);
  245. /*
  246. * If there's more than 512MB of memory, we need to roll over to
  247. * LAR1/LSR1.
  248. */
  249. if (memsize > SZ_512M) {
  250. __raw_writel(memphys + SZ_512M, chan->reg_base + SH4_PCILAR1);
  251. __raw_writel((((memsize - SZ_512M) - SZ_1M) & 0x1ff00000) | 1,
  252. chan->reg_base + SH4_PCILSR1);
  253. memsize = SZ_512M;
  254. } else {
  255. /*
  256. * Otherwise just zero it out and disable it.
  257. */
  258. __raw_writel(0, chan->reg_base + SH4_PCILAR1);
  259. __raw_writel(0, chan->reg_base + SH4_PCILSR1);
  260. }
  261. /*
  262. * LAR0/LSR0 covers up to the first 512MB, which is enough to
  263. * cover all of lowmem on most platforms.
  264. */
  265. __raw_writel(memphys, chan->reg_base + SH4_PCILAR0);
  266. __raw_writel(((memsize - SZ_1M) & 0x1ff00000) | 1,
  267. chan->reg_base + SH4_PCILSR0);
  268. /*
  269. * Hook up the ERR and SERR IRQs.
  270. */
  271. ret = sh7780_pci_setup_irqs(chan);
  272. if (unlikely(ret))
  273. return ret;
  274. /*
  275. * Disable the cache snoop controller for non-coherent DMA.
  276. */
  277. __raw_writel(0, chan->reg_base + SH7780_PCICSCR0);
  278. __raw_writel(0, chan->reg_base + SH7780_PCICSAR0);
  279. __raw_writel(0, chan->reg_base + SH7780_PCICSCR1);
  280. __raw_writel(0, chan->reg_base + SH7780_PCICSAR1);
  281. __raw_writel(0xfd000000, chan->reg_base + SH7780_PCIMBR0);
  282. __raw_writel(0x00fc0000, chan->reg_base + SH7780_PCIMBMR0);
  283. __raw_writel(0, chan->reg_base + SH7780_PCIIOBR);
  284. __raw_writel(0, chan->reg_base + SH7780_PCIIOBMR);
  285. __raw_writew(PCI_COMMAND_SERR | PCI_COMMAND_WAIT | \
  286. PCI_COMMAND_PARITY | PCI_COMMAND_MASTER | \
  287. PCI_COMMAND_MEMORY, chan->reg_base + PCI_COMMAND);
  288. /*
  289. * Initialization mode complete, release the control register and
  290. * enable round robin mode to stop device overruns/starvation.
  291. */
  292. __raw_writel(SH4_PCICR_PREFIX | SH4_PCICR_CFIN | SH4_PCICR_FTO,
  293. chan->reg_base + SH4_PCICR);
  294. ret = register_pci_controller(chan);
  295. if (unlikely(ret))
  296. goto err;
  297. sh7780_pci66_init(chan);
  298. printk(KERN_NOTICE "PCI: Running at %dMHz.\n",
  299. (__raw_readw(chan->reg_base + PCI_STATUS) & PCI_STATUS_66MHZ) ?
  300. 66 : 33);
  301. return 0;
  302. err:
  303. sh7780_pci_teardown_irqs(chan);
  304. return ret;
  305. }
  306. arch_initcall(sh7780_pci_init);