pci-sh7780.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  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. #if defined(CONFIG_CPU_BIG_ENDIAN)
  24. # define PCICR_ENDIANNESS SH4_PCICR_BSWP
  25. #else
  26. # define PCICR_ENDIANNESS 0
  27. #endif
  28. static struct resource sh7785_pci_resources[] = {
  29. {
  30. .name = "PCI IO",
  31. .start = 0x1000,
  32. .end = SZ_4M - 1,
  33. .flags = IORESOURCE_IO,
  34. }, {
  35. .name = "PCI MEM 0",
  36. .start = 0xfd000000,
  37. .end = 0xfd000000 + SZ_16M - 1,
  38. .flags = IORESOURCE_MEM,
  39. }, {
  40. .name = "PCI MEM 1",
  41. .start = 0x10000000,
  42. .end = 0x10000000 + SZ_64M - 1,
  43. .flags = IORESOURCE_MEM,
  44. }, {
  45. /*
  46. * 32-bit only resources must be last.
  47. */
  48. .name = "PCI MEM 2",
  49. .start = 0xc0000000,
  50. .end = 0xc0000000 + SZ_512M - 1,
  51. .flags = IORESOURCE_MEM | IORESOURCE_MEM_32BIT,
  52. },
  53. };
  54. static struct pci_channel sh7780_pci_controller = {
  55. .pci_ops = &sh4_pci_ops,
  56. .resources = sh7785_pci_resources,
  57. .nr_resources = ARRAY_SIZE(sh7785_pci_resources),
  58. .io_offset = 0,
  59. .mem_offset = 0,
  60. .io_map_base = 0xfe200000,
  61. .serr_irq = evt2irq(0xa00),
  62. .err_irq = evt2irq(0xaa0),
  63. };
  64. struct pci_errors {
  65. unsigned int mask;
  66. const char *str;
  67. } pci_arbiter_errors[] = {
  68. { SH4_PCIAINT_MBKN, "master broken" },
  69. { SH4_PCIAINT_TBTO, "target bus time out" },
  70. { SH4_PCIAINT_MBTO, "master bus time out" },
  71. { SH4_PCIAINT_TABT, "target abort" },
  72. { SH4_PCIAINT_MABT, "master abort" },
  73. { SH4_PCIAINT_RDPE, "read data parity error" },
  74. { SH4_PCIAINT_WDPE, "write data parity error" },
  75. }, pci_interrupt_errors[] = {
  76. { SH4_PCIINT_MLCK, "master lock error" },
  77. { SH4_PCIINT_TABT, "target-target abort" },
  78. { SH4_PCIINT_TRET, "target retry time out" },
  79. { SH4_PCIINT_MFDE, "master function disable error" },
  80. { SH4_PCIINT_PRTY, "address parity error" },
  81. { SH4_PCIINT_SERR, "SERR" },
  82. { SH4_PCIINT_TWDP, "data parity error for target write" },
  83. { SH4_PCIINT_TRDP, "PERR detected for target read" },
  84. { SH4_PCIINT_MTABT, "target abort for master" },
  85. { SH4_PCIINT_MMABT, "master abort for master" },
  86. { SH4_PCIINT_MWPD, "master write data parity error" },
  87. { SH4_PCIINT_MRPD, "master read data parity error" },
  88. };
  89. static irqreturn_t sh7780_pci_err_irq(int irq, void *dev_id)
  90. {
  91. struct pci_channel *hose = dev_id;
  92. unsigned long addr;
  93. unsigned int status;
  94. unsigned int cmd;
  95. int i;
  96. addr = __raw_readl(hose->reg_base + SH4_PCIALR);
  97. /*
  98. * Handle status errors.
  99. */
  100. status = __raw_readw(hose->reg_base + PCI_STATUS);
  101. if (status & (PCI_STATUS_PARITY |
  102. PCI_STATUS_DETECTED_PARITY |
  103. PCI_STATUS_SIG_TARGET_ABORT |
  104. PCI_STATUS_REC_TARGET_ABORT |
  105. PCI_STATUS_REC_MASTER_ABORT)) {
  106. cmd = pcibios_handle_status_errors(addr, status, hose);
  107. if (likely(cmd))
  108. __raw_writew(cmd, hose->reg_base + PCI_STATUS);
  109. }
  110. /*
  111. * Handle arbiter errors.
  112. */
  113. status = __raw_readl(hose->reg_base + SH4_PCIAINT);
  114. for (i = cmd = 0; i < ARRAY_SIZE(pci_arbiter_errors); i++) {
  115. if (status & pci_arbiter_errors[i].mask) {
  116. printk(KERN_DEBUG "PCI: %s, addr=%08lx\n",
  117. pci_arbiter_errors[i].str, addr);
  118. cmd |= pci_arbiter_errors[i].mask;
  119. }
  120. }
  121. __raw_writel(cmd, hose->reg_base + SH4_PCIAINT);
  122. /*
  123. * Handle the remaining PCI errors.
  124. */
  125. status = __raw_readl(hose->reg_base + SH4_PCIINT);
  126. for (i = cmd = 0; i < ARRAY_SIZE(pci_interrupt_errors); i++) {
  127. if (status & pci_interrupt_errors[i].mask) {
  128. printk(KERN_DEBUG "PCI: %s, addr=%08lx\n",
  129. pci_interrupt_errors[i].str, addr);
  130. cmd |= pci_interrupt_errors[i].mask;
  131. }
  132. }
  133. __raw_writel(cmd, hose->reg_base + SH4_PCIINT);
  134. return IRQ_HANDLED;
  135. }
  136. static irqreturn_t sh7780_pci_serr_irq(int irq, void *dev_id)
  137. {
  138. struct pci_channel *hose = dev_id;
  139. printk(KERN_DEBUG "PCI: system error received: ");
  140. pcibios_report_status(PCI_STATUS_SIG_SYSTEM_ERROR, 1);
  141. printk("\n");
  142. /* Deassert SERR */
  143. __raw_writel(SH4_PCIINTM_SDIM, hose->reg_base + SH4_PCIINTM);
  144. /* Back off the IRQ for awhile */
  145. disable_irq_nosync(irq);
  146. hose->serr_timer.expires = jiffies + HZ;
  147. add_timer(&hose->serr_timer);
  148. return IRQ_HANDLED;
  149. }
  150. static int __init sh7780_pci_setup_irqs(struct pci_channel *hose)
  151. {
  152. int ret;
  153. /* Clear out PCI arbiter IRQs */
  154. __raw_writel(0, hose->reg_base + SH4_PCIAINT);
  155. /* Clear all error conditions */
  156. __raw_writew(PCI_STATUS_DETECTED_PARITY | \
  157. PCI_STATUS_SIG_SYSTEM_ERROR | \
  158. PCI_STATUS_REC_MASTER_ABORT | \
  159. PCI_STATUS_REC_TARGET_ABORT | \
  160. PCI_STATUS_SIG_TARGET_ABORT | \
  161. PCI_STATUS_PARITY, hose->reg_base + PCI_STATUS);
  162. ret = request_irq(hose->serr_irq, sh7780_pci_serr_irq, 0,
  163. "PCI SERR interrupt", hose);
  164. if (unlikely(ret)) {
  165. printk(KERN_ERR "PCI: Failed hooking SERR IRQ\n");
  166. return ret;
  167. }
  168. /*
  169. * The PCI ERR IRQ needs to be IRQF_SHARED since all of the power
  170. * down IRQ vectors are routed through the ERR IRQ vector. We
  171. * only request_irq() once as there is only a single masking
  172. * source for multiple events.
  173. */
  174. ret = request_irq(hose->err_irq, sh7780_pci_err_irq, IRQF_SHARED,
  175. "PCI ERR interrupt", hose);
  176. if (unlikely(ret)) {
  177. free_irq(hose->serr_irq, hose);
  178. return ret;
  179. }
  180. /* Unmask all of the arbiter IRQs. */
  181. __raw_writel(SH4_PCIAINT_MBKN | SH4_PCIAINT_TBTO | SH4_PCIAINT_MBTO | \
  182. SH4_PCIAINT_TABT | SH4_PCIAINT_MABT | SH4_PCIAINT_RDPE | \
  183. SH4_PCIAINT_WDPE, hose->reg_base + SH4_PCIAINTM);
  184. /* Unmask all of the PCI IRQs */
  185. __raw_writel(SH4_PCIINTM_TTADIM | SH4_PCIINTM_TMTOIM | \
  186. SH4_PCIINTM_MDEIM | SH4_PCIINTM_APEDIM | \
  187. SH4_PCIINTM_SDIM | SH4_PCIINTM_DPEITWM | \
  188. SH4_PCIINTM_PEDITRM | SH4_PCIINTM_TADIMM | \
  189. SH4_PCIINTM_MADIMM | SH4_PCIINTM_MWPDIM | \
  190. SH4_PCIINTM_MRDPEIM, hose->reg_base + SH4_PCIINTM);
  191. return ret;
  192. }
  193. static inline void __init sh7780_pci_teardown_irqs(struct pci_channel *hose)
  194. {
  195. free_irq(hose->err_irq, hose);
  196. free_irq(hose->serr_irq, hose);
  197. }
  198. static void __init sh7780_pci66_init(struct pci_channel *hose)
  199. {
  200. unsigned int tmp;
  201. if (!pci_is_66mhz_capable(hose, 0, 0))
  202. return;
  203. /* Enable register access */
  204. tmp = __raw_readl(hose->reg_base + SH4_PCICR);
  205. tmp |= SH4_PCICR_PREFIX;
  206. __raw_writel(tmp, hose->reg_base + SH4_PCICR);
  207. /* Enable 66MHz operation */
  208. tmp = __raw_readw(hose->reg_base + PCI_STATUS);
  209. tmp |= PCI_STATUS_66MHZ;
  210. __raw_writew(tmp, hose->reg_base + PCI_STATUS);
  211. /* Done */
  212. tmp = __raw_readl(hose->reg_base + SH4_PCICR);
  213. tmp |= SH4_PCICR_PREFIX | SH4_PCICR_CFIN;
  214. __raw_writel(tmp, hose->reg_base + SH4_PCICR);
  215. }
  216. static int __init sh7780_pci_init(void)
  217. {
  218. struct pci_channel *chan = &sh7780_pci_controller;
  219. phys_addr_t memphys;
  220. size_t memsize;
  221. unsigned int id;
  222. const char *type;
  223. int ret, i;
  224. printk(KERN_NOTICE "PCI: Starting initialization.\n");
  225. chan->reg_base = 0xfe040000;
  226. /* Enable CPU access to the PCIC registers. */
  227. __raw_writel(PCIECR_ENBL, PCIECR);
  228. /* Reset */
  229. __raw_writel(SH4_PCICR_PREFIX | SH4_PCICR_PRST | PCICR_ENDIANNESS,
  230. chan->reg_base + SH4_PCICR);
  231. /*
  232. * Wait for it to come back up. The spec says to allow for up to
  233. * 1 second after toggling the reset pin, but in practice 100ms
  234. * is more than enough.
  235. */
  236. mdelay(100);
  237. id = __raw_readw(chan->reg_base + PCI_VENDOR_ID);
  238. if (id != PCI_VENDOR_ID_RENESAS) {
  239. printk(KERN_ERR "PCI: Unknown vendor ID 0x%04x.\n", id);
  240. return -ENODEV;
  241. }
  242. id = __raw_readw(chan->reg_base + PCI_DEVICE_ID);
  243. type = (id == PCI_DEVICE_ID_RENESAS_SH7763) ? "SH7763" :
  244. (id == PCI_DEVICE_ID_RENESAS_SH7780) ? "SH7780" :
  245. (id == PCI_DEVICE_ID_RENESAS_SH7781) ? "SH7781" :
  246. (id == PCI_DEVICE_ID_RENESAS_SH7785) ? "SH7785" :
  247. NULL;
  248. if (unlikely(!type)) {
  249. printk(KERN_ERR "PCI: Found an unsupported Renesas host "
  250. "controller, device id 0x%04x.\n", id);
  251. return -EINVAL;
  252. }
  253. printk(KERN_NOTICE "PCI: Found a Renesas %s host "
  254. "controller, revision %d.\n", type,
  255. __raw_readb(chan->reg_base + PCI_REVISION_ID));
  256. /*
  257. * Now throw it in to register initialization mode and
  258. * start the real work.
  259. */
  260. __raw_writel(SH4_PCICR_PREFIX | PCICR_ENDIANNESS,
  261. chan->reg_base + SH4_PCICR);
  262. memphys = __pa(memory_start);
  263. memsize = roundup_pow_of_two(memory_end - memory_start);
  264. /*
  265. * If there's more than 512MB of memory, we need to roll over to
  266. * LAR1/LSR1.
  267. */
  268. if (memsize > SZ_512M) {
  269. __raw_writel(memphys + SZ_512M, chan->reg_base + SH4_PCILAR1);
  270. __raw_writel((((memsize - SZ_512M) - SZ_1M) & 0x1ff00000) | 1,
  271. chan->reg_base + SH4_PCILSR1);
  272. memsize = SZ_512M;
  273. } else {
  274. /*
  275. * Otherwise just zero it out and disable it.
  276. */
  277. __raw_writel(0, chan->reg_base + SH4_PCILAR1);
  278. __raw_writel(0, chan->reg_base + SH4_PCILSR1);
  279. }
  280. /*
  281. * LAR0/LSR0 covers up to the first 512MB, which is enough to
  282. * cover all of lowmem on most platforms.
  283. */
  284. __raw_writel(memphys, chan->reg_base + SH4_PCILAR0);
  285. __raw_writel(((memsize - SZ_1M) & 0x1ff00000) | 1,
  286. chan->reg_base + SH4_PCILSR0);
  287. /*
  288. * Hook up the ERR and SERR IRQs.
  289. */
  290. ret = sh7780_pci_setup_irqs(chan);
  291. if (unlikely(ret))
  292. return ret;
  293. /*
  294. * Disable the cache snoop controller for non-coherent DMA.
  295. */
  296. __raw_writel(0, chan->reg_base + SH7780_PCICSCR0);
  297. __raw_writel(0, chan->reg_base + SH7780_PCICSAR0);
  298. __raw_writel(0, chan->reg_base + SH7780_PCICSCR1);
  299. __raw_writel(0, chan->reg_base + SH7780_PCICSAR1);
  300. /*
  301. * Setup the memory BARs
  302. */
  303. for (i = 1; i < chan->nr_resources; i++) {
  304. struct resource *res = chan->resources + i;
  305. resource_size_t size;
  306. if (unlikely(res->flags & IORESOURCE_IO))
  307. continue;
  308. /*
  309. * Make sure we're in the right physical addressing mode
  310. * for dealing with the resource.
  311. */
  312. if ((res->flags & IORESOURCE_MEM_32BIT) && __in_29bit_mode()) {
  313. chan->nr_resources--;
  314. continue;
  315. }
  316. size = resource_size(res);
  317. /*
  318. * The MBMR mask is calculated in units of 256kB, which
  319. * keeps things pretty simple.
  320. */
  321. __raw_writel(((roundup_pow_of_two(size) / SZ_256K) - 1) << 18,
  322. chan->reg_base + SH7780_PCIMBMR(i - 1));
  323. __raw_writel(res->start, chan->reg_base + SH7780_PCIMBR(i - 1));
  324. }
  325. /*
  326. * And I/O.
  327. */
  328. __raw_writel(0, chan->reg_base + PCI_BASE_ADDRESS_0);
  329. __raw_writel(0, chan->reg_base + SH7780_PCIIOBR);
  330. __raw_writel(0, chan->reg_base + SH7780_PCIIOBMR);
  331. __raw_writew(PCI_COMMAND_SERR | PCI_COMMAND_WAIT | \
  332. PCI_COMMAND_PARITY | PCI_COMMAND_MASTER | \
  333. PCI_COMMAND_MEMORY, chan->reg_base + PCI_COMMAND);
  334. /*
  335. * Initialization mode complete, release the control register and
  336. * enable round robin mode to stop device overruns/starvation.
  337. */
  338. __raw_writel(SH4_PCICR_PREFIX | SH4_PCICR_CFIN | SH4_PCICR_FTO |
  339. PCICR_ENDIANNESS,
  340. chan->reg_base + SH4_PCICR);
  341. ret = register_pci_controller(chan);
  342. if (unlikely(ret))
  343. goto err;
  344. sh7780_pci66_init(chan);
  345. printk(KERN_NOTICE "PCI: Running at %dMHz.\n",
  346. (__raw_readw(chan->reg_base + PCI_STATUS) & PCI_STATUS_66MHZ) ?
  347. 66 : 33);
  348. return 0;
  349. err:
  350. sh7780_pci_teardown_irqs(chan);
  351. return ret;
  352. }
  353. arch_initcall(sh7780_pci_init);