common-pci.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539
  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 <asm/dma-mapping.h>
  28. #include <asm/io.h>
  29. #include <asm/irq.h>
  30. #include <asm/sizes.h>
  31. #include <asm/system.h>
  32. #include <asm/mach/pci.h>
  33. #include <asm/hardware.h>
  34. /*
  35. * IXP4xx PCI read function is dependent on whether we are
  36. * running A0 or B0 (AppleGate) silicon.
  37. */
  38. int (*ixp4xx_pci_read)(u32 addr, u32 cmd, u32* data);
  39. /*
  40. * Base address for PCI regsiter region
  41. */
  42. unsigned long ixp4xx_pci_reg_base = 0;
  43. /*
  44. * PCI cfg an I/O routines are done by programming a
  45. * command/byte enable register, and then read/writing
  46. * the data from a data regsiter. We need to ensure
  47. * these transactions are atomic or we will end up
  48. * with corrupt data on the bus or in a driver.
  49. */
  50. static DEFINE_SPINLOCK(ixp4xx_pci_lock);
  51. /*
  52. * Read from PCI config space
  53. */
  54. static void crp_read(u32 ad_cbe, u32 *data)
  55. {
  56. unsigned long flags;
  57. spin_lock_irqsave(&ixp4xx_pci_lock, flags);
  58. *PCI_CRP_AD_CBE = ad_cbe;
  59. *data = *PCI_CRP_RDATA;
  60. spin_unlock_irqrestore(&ixp4xx_pci_lock, flags);
  61. }
  62. /*
  63. * Write to PCI config space
  64. */
  65. static void crp_write(u32 ad_cbe, u32 data)
  66. {
  67. unsigned long flags;
  68. spin_lock_irqsave(&ixp4xx_pci_lock, flags);
  69. *PCI_CRP_AD_CBE = CRP_AD_CBE_WRITE | ad_cbe;
  70. *PCI_CRP_WDATA = data;
  71. spin_unlock_irqrestore(&ixp4xx_pci_lock, flags);
  72. }
  73. static inline int check_master_abort(void)
  74. {
  75. /* check Master Abort bit after access */
  76. unsigned long isr = *PCI_ISR;
  77. if (isr & PCI_ISR_PFE) {
  78. /* make sure the Master Abort bit is reset */
  79. *PCI_ISR = PCI_ISR_PFE;
  80. pr_debug("%s failed\n", __FUNCTION__);
  81. return 1;
  82. }
  83. return 0;
  84. }
  85. int ixp4xx_pci_read_errata(u32 addr, u32 cmd, u32* data)
  86. {
  87. unsigned long flags;
  88. int retval = 0;
  89. int i;
  90. spin_lock_irqsave(&ixp4xx_pci_lock, flags);
  91. *PCI_NP_AD = addr;
  92. /*
  93. * PCI workaround - only works if NP PCI space reads have
  94. * no side effects!!! Read 8 times. last one will be good.
  95. */
  96. for (i = 0; i < 8; i++) {
  97. *PCI_NP_CBE = cmd;
  98. *data = *PCI_NP_RDATA;
  99. *data = *PCI_NP_RDATA;
  100. }
  101. if(check_master_abort())
  102. retval = 1;
  103. spin_unlock_irqrestore(&ixp4xx_pci_lock, flags);
  104. return retval;
  105. }
  106. int ixp4xx_pci_read_no_errata(u32 addr, u32 cmd, u32* data)
  107. {
  108. unsigned long flags;
  109. int retval = 0;
  110. spin_lock_irqsave(&ixp4xx_pci_lock, flags);
  111. *PCI_NP_AD = addr;
  112. /* set up and execute the read */
  113. *PCI_NP_CBE = cmd;
  114. /* the result of the read is now in NP_RDATA */
  115. *data = *PCI_NP_RDATA;
  116. if(check_master_abort())
  117. retval = 1;
  118. spin_unlock_irqrestore(&ixp4xx_pci_lock, flags);
  119. return retval;
  120. }
  121. int ixp4xx_pci_write(u32 addr, u32 cmd, u32 data)
  122. {
  123. unsigned long flags;
  124. int retval = 0;
  125. spin_lock_irqsave(&ixp4xx_pci_lock, flags);
  126. *PCI_NP_AD = addr;
  127. /* set up the write */
  128. *PCI_NP_CBE = cmd;
  129. /* execute the write by writing to NP_WDATA */
  130. *PCI_NP_WDATA = data;
  131. if(check_master_abort())
  132. retval = 1;
  133. spin_unlock_irqrestore(&ixp4xx_pci_lock, flags);
  134. return retval;
  135. }
  136. static u32 ixp4xx_config_addr(u8 bus_num, u16 devfn, int where)
  137. {
  138. u32 addr;
  139. if (!bus_num) {
  140. /* type 0 */
  141. addr = BIT(32-PCI_SLOT(devfn)) | ((PCI_FUNC(devfn)) << 8) |
  142. (where & ~3);
  143. } else {
  144. /* type 1 */
  145. addr = (bus_num << 16) | ((PCI_SLOT(devfn)) << 11) |
  146. ((PCI_FUNC(devfn)) << 8) | (where & ~3) | 1;
  147. }
  148. return addr;
  149. }
  150. /*
  151. * Mask table, bits to mask for quantity of size 1, 2 or 4 bytes.
  152. * 0 and 3 are not valid indexes...
  153. */
  154. static u32 bytemask[] = {
  155. /*0*/ 0,
  156. /*1*/ 0xff,
  157. /*2*/ 0xffff,
  158. /*3*/ 0,
  159. /*4*/ 0xffffffff,
  160. };
  161. static u32 local_byte_lane_enable_bits(u32 n, int size)
  162. {
  163. if (size == 1)
  164. return (0xf & ~BIT(n)) << CRP_AD_CBE_BESL;
  165. if (size == 2)
  166. return (0xf & ~(BIT(n) | BIT(n+1))) << CRP_AD_CBE_BESL;
  167. if (size == 4)
  168. return 0;
  169. return 0xffffffff;
  170. }
  171. static int local_read_config(int where, int size, u32 *value)
  172. {
  173. u32 n, data;
  174. pr_debug("local_read_config from %d size %d\n", where, size);
  175. n = where % 4;
  176. crp_read(where & ~3, &data);
  177. *value = (data >> (8*n)) & bytemask[size];
  178. pr_debug("local_read_config read %#x\n", *value);
  179. return PCIBIOS_SUCCESSFUL;
  180. }
  181. static int local_write_config(int where, int size, u32 value)
  182. {
  183. u32 n, byte_enables, data;
  184. pr_debug("local_write_config %#x to %d size %d\n", value, where, size);
  185. n = where % 4;
  186. byte_enables = local_byte_lane_enable_bits(n, size);
  187. if (byte_enables == 0xffffffff)
  188. return PCIBIOS_BAD_REGISTER_NUMBER;
  189. data = value << (8*n);
  190. crp_write((where & ~3) | byte_enables, data);
  191. return PCIBIOS_SUCCESSFUL;
  192. }
  193. static u32 byte_lane_enable_bits(u32 n, int size)
  194. {
  195. if (size == 1)
  196. return (0xf & ~BIT(n)) << 4;
  197. if (size == 2)
  198. return (0xf & ~(BIT(n) | BIT(n+1))) << 4;
  199. if (size == 4)
  200. return 0;
  201. return 0xffffffff;
  202. }
  203. static int ixp4xx_pci_read_config(struct pci_bus *bus, unsigned int devfn, int where, int size, u32 *value)
  204. {
  205. u32 n, byte_enables, addr, data;
  206. u8 bus_num = bus->number;
  207. pr_debug("read_config from %d size %d dev %d:%d:%d\n", where, size,
  208. bus_num, PCI_SLOT(devfn), PCI_FUNC(devfn));
  209. *value = 0xffffffff;
  210. n = where % 4;
  211. byte_enables = byte_lane_enable_bits(n, size);
  212. if (byte_enables == 0xffffffff)
  213. return PCIBIOS_BAD_REGISTER_NUMBER;
  214. addr = ixp4xx_config_addr(bus_num, devfn, where);
  215. if (ixp4xx_pci_read(addr, byte_enables | NP_CMD_CONFIGREAD, &data))
  216. return PCIBIOS_DEVICE_NOT_FOUND;
  217. *value = (data >> (8*n)) & bytemask[size];
  218. pr_debug("read_config_byte read %#x\n", *value);
  219. return PCIBIOS_SUCCESSFUL;
  220. }
  221. static int ixp4xx_pci_write_config(struct pci_bus *bus, unsigned int devfn, int where, int size, u32 value)
  222. {
  223. u32 n, byte_enables, addr, data;
  224. u8 bus_num = bus->number;
  225. pr_debug("write_config_byte %#x to %d size %d dev %d:%d:%d\n", value, where,
  226. size, bus_num, PCI_SLOT(devfn), PCI_FUNC(devfn));
  227. n = where % 4;
  228. byte_enables = byte_lane_enable_bits(n, size);
  229. if (byte_enables == 0xffffffff)
  230. return PCIBIOS_BAD_REGISTER_NUMBER;
  231. addr = ixp4xx_config_addr(bus_num, devfn, where);
  232. data = value << (8*n);
  233. if (ixp4xx_pci_write(addr, byte_enables | NP_CMD_CONFIGWRITE, data))
  234. return PCIBIOS_DEVICE_NOT_FOUND;
  235. return PCIBIOS_SUCCESSFUL;
  236. }
  237. struct pci_ops ixp4xx_ops = {
  238. .read = ixp4xx_pci_read_config,
  239. .write = ixp4xx_pci_write_config,
  240. };
  241. /*
  242. * PCI abort handler
  243. */
  244. static int abort_handler(unsigned long addr, unsigned int fsr, struct pt_regs *regs)
  245. {
  246. u32 isr, status;
  247. isr = *PCI_ISR;
  248. local_read_config(PCI_STATUS, 2, &status);
  249. pr_debug("PCI: abort_handler addr = %#lx, isr = %#x, "
  250. "status = %#x\n", addr, isr, status);
  251. /* make sure the Master Abort bit is reset */
  252. *PCI_ISR = PCI_ISR_PFE;
  253. status |= PCI_STATUS_REC_MASTER_ABORT;
  254. local_write_config(PCI_STATUS, 2, status);
  255. /*
  256. * If it was an imprecise abort, then we need to correct the
  257. * return address to be _after_ the instruction.
  258. */
  259. if (fsr & (1 << 10))
  260. regs->ARM_pc += 4;
  261. return 0;
  262. }
  263. /*
  264. * Setup DMA mask to 64MB on PCI devices. Ignore all other devices.
  265. */
  266. static int ixp4xx_pci_platform_notify(struct device *dev)
  267. {
  268. if(dev->bus == &pci_bus_type) {
  269. *dev->dma_mask = SZ_64M - 1;
  270. dev->coherent_dma_mask = SZ_64M - 1;
  271. dmabounce_register_dev(dev, 2048, 4096);
  272. }
  273. return 0;
  274. }
  275. static int ixp4xx_pci_platform_notify_remove(struct device *dev)
  276. {
  277. if(dev->bus == &pci_bus_type) {
  278. dmabounce_unregister_dev(dev);
  279. }
  280. return 0;
  281. }
  282. int dma_needs_bounce(struct device *dev, dma_addr_t dma_addr, size_t size)
  283. {
  284. return (dev->bus == &pci_bus_type ) && ((dma_addr + size) >= SZ_64M);
  285. }
  286. /*
  287. * Only first 64MB of memory can be accessed via PCI.
  288. * We use GFP_DMA to allocate safe buffers to do map/unmap.
  289. * This is really ugly and we need a better way of specifying
  290. * DMA-capable regions of memory.
  291. */
  292. void __init ixp4xx_adjust_zones(int node, unsigned long *zone_size,
  293. unsigned long *zhole_size)
  294. {
  295. unsigned int sz = SZ_64M >> PAGE_SHIFT;
  296. /*
  297. * Only adjust if > 64M on current system
  298. */
  299. if (node || (zone_size[0] <= sz))
  300. return;
  301. zone_size[1] = zone_size[0] - sz;
  302. zone_size[0] = sz;
  303. zhole_size[1] = zhole_size[0];
  304. zhole_size[0] = 0;
  305. }
  306. void __init ixp4xx_pci_preinit(void)
  307. {
  308. unsigned long processor_id;
  309. asm("mrc p15, 0, %0, cr0, cr0, 0;" : "=r"(processor_id) :);
  310. /*
  311. * Determine which PCI read method to use.
  312. * Rev 0 IXP425 requires workaround.
  313. */
  314. if (!(processor_id & 0xf) && !cpu_is_ixp46x()) {
  315. printk("PCI: IXP42x A0 silicon detected - "
  316. "PCI Non-Prefetch Workaround Enabled\n");
  317. ixp4xx_pci_read = ixp4xx_pci_read_errata;
  318. } else
  319. ixp4xx_pci_read = ixp4xx_pci_read_no_errata;
  320. /* hook in our fault handler for PCI errors */
  321. hook_fault_code(16+6, abort_handler, SIGBUS, "imprecise external abort");
  322. pr_debug("setup PCI-AHB(inbound) and AHB-PCI(outbound) address mappings\n");
  323. /*
  324. * We use identity AHB->PCI address translation
  325. * in the 0x48000000 to 0x4bffffff address space
  326. */
  327. *PCI_PCIMEMBASE = 0x48494A4B;
  328. /*
  329. * We also use identity PCI->AHB address translation
  330. * in 4 16MB BARs that begin at the physical memory start
  331. */
  332. *PCI_AHBMEMBASE = (PHYS_OFFSET & 0xFF000000) +
  333. ((PHYS_OFFSET & 0xFF000000) >> 8) +
  334. ((PHYS_OFFSET & 0xFF000000) >> 16) +
  335. ((PHYS_OFFSET & 0xFF000000) >> 24) +
  336. 0x00010203;
  337. if (*PCI_CSR & PCI_CSR_HOST) {
  338. printk("PCI: IXP4xx is host\n");
  339. pr_debug("setup BARs in controller\n");
  340. /*
  341. * We configure the PCI inbound memory windows to be
  342. * 1:1 mapped to SDRAM
  343. */
  344. local_write_config(PCI_BASE_ADDRESS_0, 4, PHYS_OFFSET + 0x00000000);
  345. local_write_config(PCI_BASE_ADDRESS_1, 4, PHYS_OFFSET + 0x01000000);
  346. local_write_config(PCI_BASE_ADDRESS_2, 4, PHYS_OFFSET + 0x02000000);
  347. local_write_config(PCI_BASE_ADDRESS_3, 4, PHYS_OFFSET + 0x03000000);
  348. /*
  349. * Enable CSR window at 0xff000000.
  350. */
  351. local_write_config(PCI_BASE_ADDRESS_4, 4, 0xff000008);
  352. /*
  353. * Enable the IO window to be way up high, at 0xfffffc00
  354. */
  355. local_write_config(PCI_BASE_ADDRESS_5, 4, 0xfffffc01);
  356. } else {
  357. printk("PCI: IXP4xx is target - No bus scan performed\n");
  358. }
  359. printk("PCI: IXP4xx Using %s access for memory space\n",
  360. #ifndef CONFIG_IXP4XX_INDIRECT_PCI
  361. "direct"
  362. #else
  363. "indirect"
  364. #endif
  365. );
  366. pr_debug("clear error bits in ISR\n");
  367. *PCI_ISR = PCI_ISR_PSE | PCI_ISR_PFE | PCI_ISR_PPE | PCI_ISR_AHBE;
  368. /*
  369. * Set Initialize Complete in PCI Control Register: allow IXP4XX to
  370. * respond to PCI configuration cycles. Specify that the AHB bus is
  371. * operating in big endian mode. Set up byte lane swapping between
  372. * little-endian PCI and the big-endian AHB bus
  373. */
  374. #ifdef __ARMEB__
  375. *PCI_CSR = PCI_CSR_IC | PCI_CSR_ABE | PCI_CSR_PDS | PCI_CSR_ADS;
  376. #else
  377. *PCI_CSR = PCI_CSR_IC | PCI_CSR_ABE;
  378. #endif
  379. pr_debug("DONE\n");
  380. }
  381. int ixp4xx_setup(int nr, struct pci_sys_data *sys)
  382. {
  383. struct resource *res;
  384. if (nr >= 1)
  385. return 0;
  386. res = kzalloc(sizeof(*res) * 2, GFP_KERNEL);
  387. if (res == NULL) {
  388. /*
  389. * If we're out of memory this early, something is wrong,
  390. * so we might as well catch it here.
  391. */
  392. panic("PCI: unable to allocate resources?\n");
  393. }
  394. local_write_config(PCI_COMMAND, 2, PCI_COMMAND_MASTER | PCI_COMMAND_MEMORY);
  395. res[0].name = "PCI I/O Space";
  396. res[0].start = 0x00000000;
  397. res[0].end = 0x0000ffff;
  398. res[0].flags = IORESOURCE_IO;
  399. res[1].name = "PCI Memory Space";
  400. res[1].start = 0x48000000;
  401. #ifndef CONFIG_IXP4XX_INDIRECT_PCI
  402. res[1].end = 0x4bffffff;
  403. #else
  404. res[1].end = 0x4fffffff;
  405. #endif
  406. res[1].flags = IORESOURCE_MEM;
  407. request_resource(&ioport_resource, &res[0]);
  408. request_resource(&iomem_resource, &res[1]);
  409. sys->resource[0] = &res[0];
  410. sys->resource[1] = &res[1];
  411. sys->resource[2] = NULL;
  412. platform_notify = ixp4xx_pci_platform_notify;
  413. platform_notify_remove = ixp4xx_pci_platform_notify_remove;
  414. return 1;
  415. }
  416. struct pci_bus *ixp4xx_scan_bus(int nr, struct pci_sys_data *sys)
  417. {
  418. return pci_scan_bus(sys->busnr, &ixp4xx_ops, sys);
  419. }
  420. /*
  421. * We override these so we properly do dmabounce otherwise drivers
  422. * are able to set the dma_mask to 0xffffffff and we can no longer
  423. * trap bounces. :(
  424. *
  425. * We just return true on everyhing except for < 64MB in which case
  426. * we will fail miseralby and die since we can't handle that case.
  427. */
  428. int
  429. pci_set_dma_mask(struct pci_dev *dev, u64 mask)
  430. {
  431. if (mask >= SZ_64M - 1 )
  432. return 0;
  433. return -EIO;
  434. }
  435. int
  436. pci_set_consistent_dma_mask(struct pci_dev *dev, u64 mask)
  437. {
  438. if (mask >= SZ_64M - 1 )
  439. return 0;
  440. return -EIO;
  441. }
  442. EXPORT_SYMBOL(pci_set_dma_mask);
  443. EXPORT_SYMBOL(pci_set_consistent_dma_mask);
  444. EXPORT_SYMBOL(ixp4xx_pci_read);
  445. EXPORT_SYMBOL(ixp4xx_pci_write);