common-pci.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  1. /*
  2. * arch/arm/mach-ixp4xx/common-pci.c
  3. *
  4. * IXP4XX PCI routines for all platforms
  5. *
  6. * Maintainer: Deepak Saxena <dsaxena@plexity.net>
  7. *
  8. * Copyright (C) 2002 Intel Corporation.
  9. * Copyright (C) 2003 Greg Ungerer <gerg@snapgear.com>
  10. * Copyright (C) 2003-2004 MontaVista Software, Inc.
  11. *
  12. * This program is free software; you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License version 2 as
  14. * published by the Free Software Foundation.
  15. *
  16. */
  17. #include <linux/sched.h>
  18. #include <linux/kernel.h>
  19. #include <linux/pci.h>
  20. #include <linux/interrupt.h>
  21. #include <linux/mm.h>
  22. #include <linux/init.h>
  23. #include <linux/ioport.h>
  24. #include <linux/slab.h>
  25. #include <linux/delay.h>
  26. #include <linux/device.h>
  27. #include <linux/io.h>
  28. #include <linux/export.h>
  29. #include <asm/dma-mapping.h>
  30. #include <asm/cputype.h>
  31. #include <asm/irq.h>
  32. #include <asm/sizes.h>
  33. #include <asm/mach/pci.h>
  34. #include <mach/hardware.h>
  35. /*
  36. * IXP4xx PCI read function is dependent on whether we are
  37. * running A0 or B0 (AppleGate) silicon.
  38. */
  39. int (*ixp4xx_pci_read)(u32 addr, u32 cmd, u32* data);
  40. /*
  41. * Base address for PCI regsiter region
  42. */
  43. unsigned long ixp4xx_pci_reg_base = 0;
  44. /*
  45. * PCI cfg an I/O routines are done by programming a
  46. * command/byte enable register, and then read/writing
  47. * the data from a data regsiter. We need to ensure
  48. * these transactions are atomic or we will end up
  49. * with corrupt data on the bus or in a driver.
  50. */
  51. static DEFINE_RAW_SPINLOCK(ixp4xx_pci_lock);
  52. /*
  53. * Read from PCI config space
  54. */
  55. static void crp_read(u32 ad_cbe, u32 *data)
  56. {
  57. unsigned long flags;
  58. raw_spin_lock_irqsave(&ixp4xx_pci_lock, flags);
  59. *PCI_CRP_AD_CBE = ad_cbe;
  60. *data = *PCI_CRP_RDATA;
  61. raw_spin_unlock_irqrestore(&ixp4xx_pci_lock, flags);
  62. }
  63. /*
  64. * Write to PCI config space
  65. */
  66. static void crp_write(u32 ad_cbe, u32 data)
  67. {
  68. unsigned long flags;
  69. raw_spin_lock_irqsave(&ixp4xx_pci_lock, flags);
  70. *PCI_CRP_AD_CBE = CRP_AD_CBE_WRITE | ad_cbe;
  71. *PCI_CRP_WDATA = data;
  72. raw_spin_unlock_irqrestore(&ixp4xx_pci_lock, flags);
  73. }
  74. static inline int check_master_abort(void)
  75. {
  76. /* check Master Abort bit after access */
  77. unsigned long isr = *PCI_ISR;
  78. if (isr & PCI_ISR_PFE) {
  79. /* make sure the Master Abort bit is reset */
  80. *PCI_ISR = PCI_ISR_PFE;
  81. pr_debug("%s failed\n", __func__);
  82. return 1;
  83. }
  84. return 0;
  85. }
  86. int ixp4xx_pci_read_errata(u32 addr, u32 cmd, u32* data)
  87. {
  88. unsigned long flags;
  89. int retval = 0;
  90. int i;
  91. raw_spin_lock_irqsave(&ixp4xx_pci_lock, flags);
  92. *PCI_NP_AD = addr;
  93. /*
  94. * PCI workaround - only works if NP PCI space reads have
  95. * no side effects!!! Read 8 times. last one will be good.
  96. */
  97. for (i = 0; i < 8; i++) {
  98. *PCI_NP_CBE = cmd;
  99. *data = *PCI_NP_RDATA;
  100. *data = *PCI_NP_RDATA;
  101. }
  102. if(check_master_abort())
  103. retval = 1;
  104. raw_spin_unlock_irqrestore(&ixp4xx_pci_lock, flags);
  105. return retval;
  106. }
  107. int ixp4xx_pci_read_no_errata(u32 addr, u32 cmd, u32* data)
  108. {
  109. unsigned long flags;
  110. int retval = 0;
  111. raw_spin_lock_irqsave(&ixp4xx_pci_lock, flags);
  112. *PCI_NP_AD = addr;
  113. /* set up and execute the read */
  114. *PCI_NP_CBE = cmd;
  115. /* the result of the read is now in NP_RDATA */
  116. *data = *PCI_NP_RDATA;
  117. if(check_master_abort())
  118. retval = 1;
  119. raw_spin_unlock_irqrestore(&ixp4xx_pci_lock, flags);
  120. return retval;
  121. }
  122. int ixp4xx_pci_write(u32 addr, u32 cmd, u32 data)
  123. {
  124. unsigned long flags;
  125. int retval = 0;
  126. raw_spin_lock_irqsave(&ixp4xx_pci_lock, flags);
  127. *PCI_NP_AD = addr;
  128. /* set up the write */
  129. *PCI_NP_CBE = cmd;
  130. /* execute the write by writing to NP_WDATA */
  131. *PCI_NP_WDATA = data;
  132. if(check_master_abort())
  133. retval = 1;
  134. raw_spin_unlock_irqrestore(&ixp4xx_pci_lock, flags);
  135. return retval;
  136. }
  137. static u32 ixp4xx_config_addr(u8 bus_num, u16 devfn, int where)
  138. {
  139. u32 addr;
  140. if (!bus_num) {
  141. /* type 0 */
  142. addr = BIT(32-PCI_SLOT(devfn)) | ((PCI_FUNC(devfn)) << 8) |
  143. (where & ~3);
  144. } else {
  145. /* type 1 */
  146. addr = (bus_num << 16) | ((PCI_SLOT(devfn)) << 11) |
  147. ((PCI_FUNC(devfn)) << 8) | (where & ~3) | 1;
  148. }
  149. return addr;
  150. }
  151. /*
  152. * Mask table, bits to mask for quantity of size 1, 2 or 4 bytes.
  153. * 0 and 3 are not valid indexes...
  154. */
  155. static u32 bytemask[] = {
  156. /*0*/ 0,
  157. /*1*/ 0xff,
  158. /*2*/ 0xffff,
  159. /*3*/ 0,
  160. /*4*/ 0xffffffff,
  161. };
  162. static u32 local_byte_lane_enable_bits(u32 n, int size)
  163. {
  164. if (size == 1)
  165. return (0xf & ~BIT(n)) << CRP_AD_CBE_BESL;
  166. if (size == 2)
  167. return (0xf & ~(BIT(n) | BIT(n+1))) << CRP_AD_CBE_BESL;
  168. if (size == 4)
  169. return 0;
  170. return 0xffffffff;
  171. }
  172. static int local_read_config(int where, int size, u32 *value)
  173. {
  174. u32 n, data;
  175. pr_debug("local_read_config from %d size %d\n", where, size);
  176. n = where % 4;
  177. crp_read(where & ~3, &data);
  178. *value = (data >> (8*n)) & bytemask[size];
  179. pr_debug("local_read_config read %#x\n", *value);
  180. return PCIBIOS_SUCCESSFUL;
  181. }
  182. static int local_write_config(int where, int size, u32 value)
  183. {
  184. u32 n, byte_enables, data;
  185. pr_debug("local_write_config %#x to %d size %d\n", value, where, size);
  186. n = where % 4;
  187. byte_enables = local_byte_lane_enable_bits(n, size);
  188. if (byte_enables == 0xffffffff)
  189. return PCIBIOS_BAD_REGISTER_NUMBER;
  190. data = value << (8*n);
  191. crp_write((where & ~3) | byte_enables, data);
  192. return PCIBIOS_SUCCESSFUL;
  193. }
  194. static u32 byte_lane_enable_bits(u32 n, int size)
  195. {
  196. if (size == 1)
  197. return (0xf & ~BIT(n)) << 4;
  198. if (size == 2)
  199. return (0xf & ~(BIT(n) | BIT(n+1))) << 4;
  200. if (size == 4)
  201. return 0;
  202. return 0xffffffff;
  203. }
  204. static int ixp4xx_pci_read_config(struct pci_bus *bus, unsigned int devfn, int where, int size, u32 *value)
  205. {
  206. u32 n, byte_enables, addr, data;
  207. u8 bus_num = bus->number;
  208. pr_debug("read_config from %d size %d dev %d:%d:%d\n", where, size,
  209. bus_num, PCI_SLOT(devfn), PCI_FUNC(devfn));
  210. *value = 0xffffffff;
  211. n = where % 4;
  212. byte_enables = byte_lane_enable_bits(n, size);
  213. if (byte_enables == 0xffffffff)
  214. return PCIBIOS_BAD_REGISTER_NUMBER;
  215. addr = ixp4xx_config_addr(bus_num, devfn, where);
  216. if (ixp4xx_pci_read(addr, byte_enables | NP_CMD_CONFIGREAD, &data))
  217. return PCIBIOS_DEVICE_NOT_FOUND;
  218. *value = (data >> (8*n)) & bytemask[size];
  219. pr_debug("read_config_byte read %#x\n", *value);
  220. return PCIBIOS_SUCCESSFUL;
  221. }
  222. static int ixp4xx_pci_write_config(struct pci_bus *bus, unsigned int devfn, int where, int size, u32 value)
  223. {
  224. u32 n, byte_enables, addr, data;
  225. u8 bus_num = bus->number;
  226. pr_debug("write_config_byte %#x to %d size %d dev %d:%d:%d\n", value, where,
  227. size, bus_num, PCI_SLOT(devfn), PCI_FUNC(devfn));
  228. n = where % 4;
  229. byte_enables = byte_lane_enable_bits(n, size);
  230. if (byte_enables == 0xffffffff)
  231. return PCIBIOS_BAD_REGISTER_NUMBER;
  232. addr = ixp4xx_config_addr(bus_num, devfn, where);
  233. data = value << (8*n);
  234. if (ixp4xx_pci_write(addr, byte_enables | NP_CMD_CONFIGWRITE, data))
  235. return PCIBIOS_DEVICE_NOT_FOUND;
  236. return PCIBIOS_SUCCESSFUL;
  237. }
  238. struct pci_ops ixp4xx_ops = {
  239. .read = ixp4xx_pci_read_config,
  240. .write = ixp4xx_pci_write_config,
  241. };
  242. /*
  243. * PCI abort handler
  244. */
  245. static int abort_handler(unsigned long addr, unsigned int fsr, struct pt_regs *regs)
  246. {
  247. u32 isr, status;
  248. isr = *PCI_ISR;
  249. local_read_config(PCI_STATUS, 2, &status);
  250. pr_debug("PCI: abort_handler addr = %#lx, isr = %#x, "
  251. "status = %#x\n", addr, isr, status);
  252. /* make sure the Master Abort bit is reset */
  253. *PCI_ISR = PCI_ISR_PFE;
  254. status |= PCI_STATUS_REC_MASTER_ABORT;
  255. local_write_config(PCI_STATUS, 2, status);
  256. /*
  257. * If it was an imprecise abort, then we need to correct the
  258. * return address to be _after_ the instruction.
  259. */
  260. if (fsr & (1 << 10))
  261. regs->ARM_pc += 4;
  262. return 0;
  263. }
  264. void __init ixp4xx_pci_preinit(void)
  265. {
  266. unsigned long cpuid = read_cpuid_id();
  267. #ifdef CONFIG_IXP4XX_INDIRECT_PCI
  268. pcibios_min_mem = 0x10000000; /* 1 GB of indirect PCI MMIO space */
  269. #else
  270. pcibios_min_mem = 0x48000000; /* 64 MB of PCI MMIO space */
  271. #endif
  272. /*
  273. * Determine which PCI read method to use.
  274. * Rev 0 IXP425 requires workaround.
  275. */
  276. if (!(cpuid & 0xf) && cpu_is_ixp42x()) {
  277. printk("PCI: IXP42x A0 silicon detected - "
  278. "PCI Non-Prefetch Workaround Enabled\n");
  279. ixp4xx_pci_read = ixp4xx_pci_read_errata;
  280. } else
  281. ixp4xx_pci_read = ixp4xx_pci_read_no_errata;
  282. /* hook in our fault handler for PCI errors */
  283. hook_fault_code(16+6, abort_handler, SIGBUS, 0,
  284. "imprecise external abort");
  285. pr_debug("setup PCI-AHB(inbound) and AHB-PCI(outbound) address mappings\n");
  286. /*
  287. * We use identity AHB->PCI address translation
  288. * in the 0x48000000 to 0x4bffffff address space
  289. */
  290. *PCI_PCIMEMBASE = 0x48494A4B;
  291. /*
  292. * We also use identity PCI->AHB address translation
  293. * in 4 16MB BARs that begin at the physical memory start
  294. */
  295. *PCI_AHBMEMBASE = (PHYS_OFFSET & 0xFF000000) +
  296. ((PHYS_OFFSET & 0xFF000000) >> 8) +
  297. ((PHYS_OFFSET & 0xFF000000) >> 16) +
  298. ((PHYS_OFFSET & 0xFF000000) >> 24) +
  299. 0x00010203;
  300. if (*PCI_CSR & PCI_CSR_HOST) {
  301. printk("PCI: IXP4xx is host\n");
  302. pr_debug("setup BARs in controller\n");
  303. /*
  304. * We configure the PCI inbound memory windows to be
  305. * 1:1 mapped to SDRAM
  306. */
  307. local_write_config(PCI_BASE_ADDRESS_0, 4, PHYS_OFFSET);
  308. local_write_config(PCI_BASE_ADDRESS_1, 4, PHYS_OFFSET + SZ_16M);
  309. local_write_config(PCI_BASE_ADDRESS_2, 4, PHYS_OFFSET + SZ_32M);
  310. local_write_config(PCI_BASE_ADDRESS_3, 4,
  311. PHYS_OFFSET + SZ_32M + SZ_16M);
  312. /*
  313. * Enable CSR window at 64 MiB to allow PCI masters
  314. * to continue prefetching past 64 MiB boundary.
  315. */
  316. local_write_config(PCI_BASE_ADDRESS_4, 4, PHYS_OFFSET + SZ_64M);
  317. /*
  318. * Enable the IO window to be way up high, at 0xfffffc00
  319. */
  320. local_write_config(PCI_BASE_ADDRESS_5, 4, 0xfffffc01);
  321. local_write_config(0x40, 4, 0x000080FF); /* No TRDY time limit */
  322. } else {
  323. printk("PCI: IXP4xx is target - No bus scan performed\n");
  324. }
  325. printk("PCI: IXP4xx Using %s access for memory space\n",
  326. #ifndef CONFIG_IXP4XX_INDIRECT_PCI
  327. "direct"
  328. #else
  329. "indirect"
  330. #endif
  331. );
  332. pr_debug("clear error bits in ISR\n");
  333. *PCI_ISR = PCI_ISR_PSE | PCI_ISR_PFE | PCI_ISR_PPE | PCI_ISR_AHBE;
  334. /*
  335. * Set Initialize Complete in PCI Control Register: allow IXP4XX to
  336. * respond to PCI configuration cycles. Specify that the AHB bus is
  337. * operating in big endian mode. Set up byte lane swapping between
  338. * little-endian PCI and the big-endian AHB bus
  339. */
  340. #ifdef __ARMEB__
  341. *PCI_CSR = PCI_CSR_IC | PCI_CSR_ABE | PCI_CSR_PDS | PCI_CSR_ADS;
  342. #else
  343. *PCI_CSR = PCI_CSR_IC | PCI_CSR_ABE;
  344. #endif
  345. pr_debug("DONE\n");
  346. }
  347. int ixp4xx_setup(int nr, struct pci_sys_data *sys)
  348. {
  349. struct resource *res;
  350. if (nr >= 1)
  351. return 0;
  352. res = kzalloc(sizeof(*res) * 2, GFP_KERNEL);
  353. if (res == NULL) {
  354. /*
  355. * If we're out of memory this early, something is wrong,
  356. * so we might as well catch it here.
  357. */
  358. panic("PCI: unable to allocate resources?\n");
  359. }
  360. local_write_config(PCI_COMMAND, 2, PCI_COMMAND_MASTER | PCI_COMMAND_MEMORY);
  361. res[0].name = "PCI I/O Space";
  362. res[0].start = 0x00000000;
  363. res[0].end = 0x0000ffff;
  364. res[0].flags = IORESOURCE_IO;
  365. res[1].name = "PCI Memory Space";
  366. res[1].start = PCIBIOS_MIN_MEM;
  367. res[1].end = PCIBIOS_MAX_MEM;
  368. res[1].flags = IORESOURCE_MEM;
  369. request_resource(&ioport_resource, &res[0]);
  370. request_resource(&iomem_resource, &res[1]);
  371. pci_add_resource_offset(&sys->resources, &res[0], sys->io_offset);
  372. pci_add_resource_offset(&sys->resources, &res[1], sys->mem_offset);
  373. return 1;
  374. }
  375. EXPORT_SYMBOL(ixp4xx_pci_read);
  376. EXPORT_SYMBOL(ixp4xx_pci_write);