pci.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. /*
  2. * arch/arm/mach-ks8695/pci.c
  3. *
  4. * Copyright (C) 2003, Micrel Semiconductors
  5. * Copyright (C) 2006, Greg Ungerer <gerg@snapgear.com>
  6. * Copyright (C) 2006, Ben Dooks
  7. * Copyright (C) 2007, Andrew Victor
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  22. */
  23. #include <linux/kernel.h>
  24. #include <linux/pci.h>
  25. #include <linux/mm.h>
  26. #include <linux/init.h>
  27. #include <linux/irq.h>
  28. #include <linux/delay.h>
  29. #include <linux/io.h>
  30. #include <asm/signal.h>
  31. #include <asm/mach/pci.h>
  32. #include <mach/hardware.h>
  33. #include <mach/devices.h>
  34. #include <mach/regs-pci.h>
  35. static int pci_dbg;
  36. static int pci_cfg_dbg;
  37. static void ks8695_pci_setupconfig(unsigned int bus_nr, unsigned int devfn, unsigned int where)
  38. {
  39. unsigned long pbca;
  40. pbca = PBCA_ENABLE | (where & ~3);
  41. pbca |= PCI_SLOT(devfn) << 11 ;
  42. pbca |= PCI_FUNC(devfn) << 8;
  43. pbca |= bus_nr << 16;
  44. if (bus_nr == 0) {
  45. /* use Type-0 transaction */
  46. __raw_writel(pbca, KS8695_PCI_VA + KS8695_PBCA);
  47. } else {
  48. /* use Type-1 transaction */
  49. __raw_writel(pbca | PBCA_TYPE1, KS8695_PCI_VA + KS8695_PBCA);
  50. }
  51. }
  52. /*
  53. * The KS8695 datasheet prohibits anything other than 32bit accesses
  54. * to the IO registers, so all our configuration must be done with
  55. * 32bit operations, and the correct bit masking and shifting.
  56. */
  57. static int ks8695_pci_readconfig(struct pci_bus *bus,
  58. unsigned int devfn, int where, int size, u32 *value)
  59. {
  60. ks8695_pci_setupconfig(bus->number, devfn, where);
  61. *value = __raw_readl(KS8695_PCI_VA + KS8695_PBCD);
  62. switch (size) {
  63. case 4:
  64. break;
  65. case 2:
  66. *value = *value >> ((where & 2) * 8);
  67. *value &= 0xffff;
  68. break;
  69. case 1:
  70. *value = *value >> ((where & 3) * 8);
  71. *value &= 0xff;
  72. break;
  73. }
  74. if (pci_cfg_dbg) {
  75. printk("read: %d,%08x,%02x,%d: %08x (%08x)\n",
  76. bus->number, devfn, where, size, *value,
  77. __raw_readl(KS8695_PCI_VA + KS8695_PBCD));
  78. }
  79. return PCIBIOS_SUCCESSFUL;
  80. }
  81. static int ks8695_pci_writeconfig(struct pci_bus *bus,
  82. unsigned int devfn, int where, int size, u32 value)
  83. {
  84. unsigned long tmp;
  85. if (pci_cfg_dbg) {
  86. printk("write: %d,%08x,%02x,%d: %08x\n",
  87. bus->number, devfn, where, size, value);
  88. }
  89. ks8695_pci_setupconfig(bus->number, devfn, where);
  90. switch (size) {
  91. case 4:
  92. __raw_writel(value, KS8695_PCI_VA + KS8695_PBCD);
  93. break;
  94. case 2:
  95. tmp = __raw_readl(KS8695_PCI_VA + KS8695_PBCD);
  96. tmp &= ~(0xffff << ((where & 2) * 8));
  97. tmp |= value << ((where & 2) * 8);
  98. __raw_writel(tmp, KS8695_PCI_VA + KS8695_PBCD);
  99. break;
  100. case 1:
  101. tmp = __raw_readl(KS8695_PCI_VA + KS8695_PBCD);
  102. tmp &= ~(0xff << ((where & 3) * 8));
  103. tmp |= value << ((where & 3) * 8);
  104. __raw_writel(tmp, KS8695_PCI_VA + KS8695_PBCD);
  105. break;
  106. }
  107. return PCIBIOS_SUCCESSFUL;
  108. }
  109. static void ks8695_local_writeconfig(int where, u32 value)
  110. {
  111. ks8695_pci_setupconfig(0, 0, where);
  112. __raw_writel(value, KS8695_PCI_VA + KS8695_PBCD);
  113. }
  114. static struct pci_ops ks8695_pci_ops = {
  115. .read = ks8695_pci_readconfig,
  116. .write = ks8695_pci_writeconfig,
  117. };
  118. static struct pci_bus* __init ks8695_pci_scan_bus(int nr, struct pci_sys_data *sys)
  119. {
  120. return pci_scan_bus(sys->busnr, &ks8695_pci_ops, sys);
  121. }
  122. static struct resource pci_mem = {
  123. .name = "PCI Memory space",
  124. .start = KS8695_PCIMEM_PA,
  125. .end = KS8695_PCIMEM_PA + (KS8695_PCIMEM_SIZE - 1),
  126. .flags = IORESOURCE_MEM,
  127. };
  128. static struct resource pci_io = {
  129. .name = "PCI IO space",
  130. .start = KS8695_PCIIO_PA,
  131. .end = KS8695_PCIIO_PA + (KS8695_PCIIO_SIZE - 1),
  132. .flags = IORESOURCE_IO,
  133. };
  134. static int __init ks8695_pci_setup(int nr, struct pci_sys_data *sys)
  135. {
  136. if (nr > 0)
  137. return 0;
  138. request_resource(&iomem_resource, &pci_mem);
  139. request_resource(&ioport_resource, &pci_io);
  140. sys->resource[0] = &pci_io;
  141. sys->resource[1] = &pci_mem;
  142. sys->resource[2] = NULL;
  143. /* Assign and enable processor bridge */
  144. ks8695_local_writeconfig(PCI_BASE_ADDRESS_0, KS8695_PCIMEM_PA);
  145. /* Enable bus-master & Memory Space access */
  146. ks8695_local_writeconfig(PCI_COMMAND, PCI_COMMAND_MASTER | PCI_COMMAND_MEMORY);
  147. /* Set cache-line size & latency. */
  148. ks8695_local_writeconfig(PCI_CACHE_LINE_SIZE, (32 << 8) | (L1_CACHE_BYTES / sizeof(u32)));
  149. /* Reserve PCI memory space for PCI-AHB resources */
  150. if (!request_mem_region(KS8695_PCIMEM_PA, SZ_64M, "PCI-AHB Bridge")) {
  151. printk(KERN_ERR "Cannot allocate PCI-AHB Bridge memory.\n");
  152. return -EBUSY;
  153. }
  154. return 1;
  155. }
  156. static inline unsigned int size_mask(unsigned long size)
  157. {
  158. return (~size) + 1;
  159. }
  160. static int ks8695_pci_fault(unsigned long addr, unsigned int fsr, struct pt_regs *regs)
  161. {
  162. unsigned long pc = instruction_pointer(regs);
  163. unsigned long instr = *(unsigned long *)pc;
  164. unsigned long cmdstat;
  165. cmdstat = __raw_readl(KS8695_PCI_VA + KS8695_CRCFCS);
  166. printk(KERN_ERR "PCI abort: address = 0x%08lx fsr = 0x%03x PC = 0x%08lx LR = 0x%08lx [%s%s%s%s%s]\n",
  167. addr, fsr, regs->ARM_pc, regs->ARM_lr,
  168. cmdstat & (PCI_STATUS_SIG_TARGET_ABORT << 16) ? "GenTarget" : " ",
  169. cmdstat & (PCI_STATUS_REC_TARGET_ABORT << 16) ? "RecvTarget" : " ",
  170. cmdstat & (PCI_STATUS_REC_MASTER_ABORT << 16) ? "MasterAbort" : " ",
  171. cmdstat & (PCI_STATUS_SIG_SYSTEM_ERROR << 16) ? "SysError" : " ",
  172. cmdstat & (PCI_STATUS_DETECTED_PARITY << 16) ? "Parity" : " "
  173. );
  174. __raw_writel(cmdstat, KS8695_PCI_VA + KS8695_CRCFCS);
  175. /*
  176. * If the instruction being executed was a read,
  177. * make it look like it read all-ones.
  178. */
  179. if ((instr & 0x0c100000) == 0x04100000) {
  180. int reg = (instr >> 12) & 15;
  181. unsigned long val;
  182. if (instr & 0x00400000)
  183. val = 255;
  184. else
  185. val = -1;
  186. regs->uregs[reg] = val;
  187. regs->ARM_pc += 4;
  188. return 0;
  189. }
  190. if ((instr & 0x0e100090) == 0x00100090) {
  191. int reg = (instr >> 12) & 15;
  192. regs->uregs[reg] = -1;
  193. regs->ARM_pc += 4;
  194. return 0;
  195. }
  196. return 1;
  197. }
  198. static void __init ks8695_pci_preinit(void)
  199. {
  200. /* make software reset to avoid freeze if PCI bus was messed up */
  201. __raw_writel(0x80000000, KS8695_PCI_VA + KS8695_PBCS);
  202. /* stage 1 initialization, subid, subdevice = 0x0001 */
  203. __raw_writel(0x00010001, KS8695_PCI_VA + KS8695_CRCSID);
  204. /* stage 2 initialization */
  205. /* prefetch limits with 16 words, retry enable */
  206. __raw_writel(0x40000000, KS8695_PCI_VA + KS8695_PBCS);
  207. /* configure memory mapping */
  208. __raw_writel(KS8695_PCIMEM_PA, KS8695_PCI_VA + KS8695_PMBA);
  209. __raw_writel(size_mask(KS8695_PCIMEM_SIZE), KS8695_PCI_VA + KS8695_PMBAM);
  210. __raw_writel(KS8695_PCIMEM_PA, KS8695_PCI_VA + KS8695_PMBAT);
  211. __raw_writel(0, KS8695_PCI_VA + KS8695_PMBAC);
  212. /* configure IO mapping */
  213. __raw_writel(KS8695_PCIIO_PA, KS8695_PCI_VA + KS8695_PIOBA);
  214. __raw_writel(size_mask(KS8695_PCIIO_SIZE), KS8695_PCI_VA + KS8695_PIOBAM);
  215. __raw_writel(KS8695_PCIIO_PA, KS8695_PCI_VA + KS8695_PIOBAT);
  216. __raw_writel(0, KS8695_PCI_VA + KS8695_PIOBAC);
  217. /* hook in fault handlers */
  218. hook_fault_code(8, ks8695_pci_fault, SIGBUS, "external abort on non-linefetch");
  219. hook_fault_code(10, ks8695_pci_fault, SIGBUS, "external abort on non-linefetch");
  220. }
  221. static void ks8695_show_pciregs(void)
  222. {
  223. if (!pci_dbg)
  224. return;
  225. printk(KERN_INFO "PCI: CRCFID = %08x\n", __raw_readl(KS8695_PCI_VA + KS8695_CRCFID));
  226. printk(KERN_INFO "PCI: CRCFCS = %08x\n", __raw_readl(KS8695_PCI_VA + KS8695_CRCFCS));
  227. printk(KERN_INFO "PCI: CRCFRV = %08x\n", __raw_readl(KS8695_PCI_VA + KS8695_CRCFRV));
  228. printk(KERN_INFO "PCI: CRCFLT = %08x\n", __raw_readl(KS8695_PCI_VA + KS8695_CRCFLT));
  229. printk(KERN_INFO "PCI: CRCBMA = %08x\n", __raw_readl(KS8695_PCI_VA + KS8695_CRCBMA));
  230. printk(KERN_INFO "PCI: CRCSID = %08x\n", __raw_readl(KS8695_PCI_VA + KS8695_CRCSID));
  231. printk(KERN_INFO "PCI: CRCFIT = %08x\n", __raw_readl(KS8695_PCI_VA + KS8695_CRCFIT));
  232. printk(KERN_INFO "PCI: PBM = %08x\n", __raw_readl(KS8695_PCI_VA + KS8695_PBM));
  233. printk(KERN_INFO "PCI: PBCS = %08x\n", __raw_readl(KS8695_PCI_VA + KS8695_PBCS));
  234. printk(KERN_INFO "PCI: PMBA = %08x\n", __raw_readl(KS8695_PCI_VA + KS8695_PMBA));
  235. printk(KERN_INFO "PCI: PMBAC = %08x\n", __raw_readl(KS8695_PCI_VA + KS8695_PMBAC));
  236. printk(KERN_INFO "PCI: PMBAM = %08x\n", __raw_readl(KS8695_PCI_VA + KS8695_PMBAM));
  237. printk(KERN_INFO "PCI: PMBAT = %08x\n", __raw_readl(KS8695_PCI_VA + KS8695_PMBAT));
  238. printk(KERN_INFO "PCI: PIOBA = %08x\n", __raw_readl(KS8695_PCI_VA + KS8695_PIOBA));
  239. printk(KERN_INFO "PCI: PIOBAC = %08x\n", __raw_readl(KS8695_PCI_VA + KS8695_PIOBAC));
  240. printk(KERN_INFO "PCI: PIOBAM = %08x\n", __raw_readl(KS8695_PCI_VA + KS8695_PIOBAM));
  241. printk(KERN_INFO "PCI: PIOBAT = %08x\n", __raw_readl(KS8695_PCI_VA + KS8695_PIOBAT));
  242. }
  243. static struct hw_pci ks8695_pci __initdata = {
  244. .nr_controllers = 1,
  245. .preinit = ks8695_pci_preinit,
  246. .setup = ks8695_pci_setup,
  247. .scan = ks8695_pci_scan_bus,
  248. .postinit = NULL,
  249. .swizzle = pci_std_swizzle,
  250. .map_irq = NULL,
  251. };
  252. void __init ks8695_init_pci(struct ks8695_pci_cfg *cfg)
  253. {
  254. if (__raw_readl(KS8695_PCI_VA + KS8695_CRCFRV) & CFRV_GUEST) {
  255. printk("PCI: KS8695 in guest mode, not initialising\n");
  256. return;
  257. }
  258. printk(KERN_INFO "PCI: Initialising\n");
  259. ks8695_show_pciregs();
  260. /* set Mode */
  261. __raw_writel(cfg->mode << 29, KS8695_PCI_VA + KS8695_PBM);
  262. ks8695_pci.map_irq = cfg->map_irq; /* board-specific map_irq method */
  263. pci_common_init(&ks8695_pci);
  264. }