pci-sh7780.c 11 KB

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