pci-sh7780.c 11 KB

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