m8260_pci_erratum9.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  1. /*
  2. * arch/ppc/platforms/mpc8260_pci9.c
  3. *
  4. * Workaround for device erratum PCI 9.
  5. * See Motorola's "XPC826xA Family Device Errata Reference."
  6. * The erratum applies to all 8260 family Hip4 processors. It is scheduled
  7. * to be fixed in HiP4 Rev C. Erratum PCI 9 states that a simultaneous PCI
  8. * inbound write transaction and PCI outbound read transaction can result in a
  9. * bus deadlock. The suggested workaround is to use the IDMA controller to
  10. * perform all reads from PCI configuration, memory, and I/O space.
  11. *
  12. * Author: andy_lowe@mvista.com
  13. *
  14. * 2003 (c) MontaVista Software, Inc. This file is licensed under
  15. * the terms of the GNU General Public License version 2. This program
  16. * is licensed "as is" without any warranty of any kind, whether express
  17. * or implied.
  18. */
  19. #include <linux/kernel.h>
  20. #include <linux/config.h>
  21. #include <linux/module.h>
  22. #include <linux/pci.h>
  23. #include <linux/types.h>
  24. #include <linux/string.h>
  25. #include <asm/io.h>
  26. #include <asm/pci-bridge.h>
  27. #include <asm/machdep.h>
  28. #include <asm/byteorder.h>
  29. #include <asm/mpc8260.h>
  30. #include <asm/immap_cpm2.h>
  31. #include <asm/cpm2.h>
  32. #include "m82xx_pci.h"
  33. #ifdef CONFIG_8260_PCI9
  34. /*#include <asm/mpc8260_pci9.h>*/ /* included in asm/io.h */
  35. #define IDMA_XFER_BUF_SIZE 64 /* size of the IDMA transfer buffer */
  36. /* define a structure for the IDMA dpram usage */
  37. typedef struct idma_dpram_s {
  38. idma_t pram; /* IDMA parameter RAM */
  39. u_char xfer_buf[IDMA_XFER_BUF_SIZE]; /* IDMA transfer buffer */
  40. idma_bd_t bd; /* buffer descriptor */
  41. } idma_dpram_t;
  42. /* define offsets relative to start of IDMA dpram */
  43. #define IDMA_XFER_BUF_OFFSET (sizeof(idma_t))
  44. #define IDMA_BD_OFFSET (sizeof(idma_t) + IDMA_XFER_BUF_SIZE)
  45. /* define globals */
  46. static volatile idma_dpram_t *idma_dpram;
  47. /* Exactly one of CONFIG_8260_PCI9_IDMAn must be defined,
  48. * where n is 1, 2, 3, or 4. This selects the IDMA channel used for
  49. * the PCI9 workaround.
  50. */
  51. #ifdef CONFIG_8260_PCI9_IDMA1
  52. #define IDMA_CHAN 0
  53. #define PROFF_IDMA PROFF_IDMA1_BASE
  54. #define IDMA_PAGE CPM_CR_IDMA1_PAGE
  55. #define IDMA_SBLOCK CPM_CR_IDMA1_SBLOCK
  56. #endif
  57. #ifdef CONFIG_8260_PCI9_IDMA2
  58. #define IDMA_CHAN 1
  59. #define PROFF_IDMA PROFF_IDMA2_BASE
  60. #define IDMA_PAGE CPM_CR_IDMA2_PAGE
  61. #define IDMA_SBLOCK CPM_CR_IDMA2_SBLOCK
  62. #endif
  63. #ifdef CONFIG_8260_PCI9_IDMA3
  64. #define IDMA_CHAN 2
  65. #define PROFF_IDMA PROFF_IDMA3_BASE
  66. #define IDMA_PAGE CPM_CR_IDMA3_PAGE
  67. #define IDMA_SBLOCK CPM_CR_IDMA3_SBLOCK
  68. #endif
  69. #ifdef CONFIG_8260_PCI9_IDMA4
  70. #define IDMA_CHAN 3
  71. #define PROFF_IDMA PROFF_IDMA4_BASE
  72. #define IDMA_PAGE CPM_CR_IDMA4_PAGE
  73. #define IDMA_SBLOCK CPM_CR_IDMA4_SBLOCK
  74. #endif
  75. void idma_pci9_init(void)
  76. {
  77. uint dpram_offset;
  78. volatile idma_t *pram;
  79. volatile im_idma_t *idma_reg;
  80. volatile cpm2_map_t *immap = cpm2_immr;
  81. /* allocate IDMA dpram */
  82. dpram_offset = cpm_dpalloc(sizeof(idma_dpram_t), 64);
  83. idma_dpram = cpm_dpram_addr(dpram_offset);
  84. /* initialize the IDMA parameter RAM */
  85. memset((void *)idma_dpram, 0, sizeof(idma_dpram_t));
  86. pram = &idma_dpram->pram;
  87. pram->ibase = dpram_offset + IDMA_BD_OFFSET;
  88. pram->dpr_buf = dpram_offset + IDMA_XFER_BUF_OFFSET;
  89. pram->ss_max = 32;
  90. pram->dts = 32;
  91. /* initialize the IDMA_BASE pointer to the IDMA parameter RAM */
  92. *((ushort *) &immap->im_dprambase[PROFF_IDMA]) = dpram_offset;
  93. /* initialize the IDMA registers */
  94. idma_reg = (volatile im_idma_t *) &immap->im_sdma.sdma_idsr1;
  95. idma_reg[IDMA_CHAN].idmr = 0; /* mask all IDMA interrupts */
  96. idma_reg[IDMA_CHAN].idsr = 0xff; /* clear all event flags */
  97. printk("<4>Using IDMA%d for MPC8260 device erratum PCI 9 workaround\n",
  98. IDMA_CHAN + 1);
  99. return;
  100. }
  101. /* Use the IDMA controller to transfer data from I/O memory to local RAM.
  102. * The src address must be a physical address suitable for use by the DMA
  103. * controller with no translation. The dst address must be a kernel virtual
  104. * address. The dst address is translated to a physical address via
  105. * virt_to_phys().
  106. * The sinc argument specifies whether or not the source address is incremented
  107. * by the DMA controller. The source address is incremented if and only if sinc
  108. * is non-zero. The destination address is always incremented since the
  109. * destination is always host RAM.
  110. */
  111. static void
  112. idma_pci9_read(u8 *dst, u8 *src, int bytes, int unit_size, int sinc)
  113. {
  114. unsigned long flags;
  115. volatile idma_t *pram = &idma_dpram->pram;
  116. volatile idma_bd_t *bd = &idma_dpram->bd;
  117. volatile cpm2_map_t *immap = cpm2_immr;
  118. local_irq_save(flags);
  119. /* initialize IDMA parameter RAM for this transfer */
  120. if (sinc)
  121. pram->dcm = IDMA_DCM_DMA_WRAP_64 | IDMA_DCM_SINC
  122. | IDMA_DCM_DINC | IDMA_DCM_SD_MEM2MEM;
  123. else
  124. pram->dcm = IDMA_DCM_DMA_WRAP_64 | IDMA_DCM_DINC
  125. | IDMA_DCM_SD_MEM2MEM;
  126. pram->ibdptr = pram->ibase;
  127. pram->sts = unit_size;
  128. pram->istate = 0;
  129. /* initialize the buffer descriptor */
  130. bd->dst = virt_to_phys(dst);
  131. bd->src = (uint) src;
  132. bd->len = bytes;
  133. bd->flags = IDMA_BD_V | IDMA_BD_W | IDMA_BD_I | IDMA_BD_L | IDMA_BD_DGBL
  134. | IDMA_BD_DBO_BE | IDMA_BD_SBO_BE | IDMA_BD_SDTB;
  135. /* issue the START_IDMA command to the CP */
  136. while (immap->im_cpm.cp_cpcr & CPM_CR_FLG);
  137. immap->im_cpm.cp_cpcr = mk_cr_cmd(IDMA_PAGE, IDMA_SBLOCK, 0,
  138. CPM_CR_START_IDMA) | CPM_CR_FLG;
  139. while (immap->im_cpm.cp_cpcr & CPM_CR_FLG);
  140. /* wait for transfer to complete */
  141. while(bd->flags & IDMA_BD_V);
  142. local_irq_restore(flags);
  143. return;
  144. }
  145. /* Use the IDMA controller to transfer data from I/O memory to local RAM.
  146. * The dst address must be a physical address suitable for use by the DMA
  147. * controller with no translation. The src address must be a kernel virtual
  148. * address. The src address is translated to a physical address via
  149. * virt_to_phys().
  150. * The dinc argument specifies whether or not the dest address is incremented
  151. * by the DMA controller. The source address is incremented if and only if sinc
  152. * is non-zero. The source address is always incremented since the
  153. * source is always host RAM.
  154. */
  155. static void
  156. idma_pci9_write(u8 *dst, u8 *src, int bytes, int unit_size, int dinc)
  157. {
  158. unsigned long flags;
  159. volatile idma_t *pram = &idma_dpram->pram;
  160. volatile idma_bd_t *bd = &idma_dpram->bd;
  161. volatile cpm2_map_t *immap = cpm2_immr;
  162. local_irq_save(flags);
  163. /* initialize IDMA parameter RAM for this transfer */
  164. if (dinc)
  165. pram->dcm = IDMA_DCM_DMA_WRAP_64 | IDMA_DCM_SINC
  166. | IDMA_DCM_DINC | IDMA_DCM_SD_MEM2MEM;
  167. else
  168. pram->dcm = IDMA_DCM_DMA_WRAP_64 | IDMA_DCM_SINC
  169. | IDMA_DCM_SD_MEM2MEM;
  170. pram->ibdptr = pram->ibase;
  171. pram->sts = unit_size;
  172. pram->istate = 0;
  173. /* initialize the buffer descriptor */
  174. bd->dst = (uint) dst;
  175. bd->src = virt_to_phys(src);
  176. bd->len = bytes;
  177. bd->flags = IDMA_BD_V | IDMA_BD_W | IDMA_BD_I | IDMA_BD_L | IDMA_BD_DGBL
  178. | IDMA_BD_DBO_BE | IDMA_BD_SBO_BE | IDMA_BD_SDTB;
  179. /* issue the START_IDMA command to the CP */
  180. while (immap->im_cpm.cp_cpcr & CPM_CR_FLG);
  181. immap->im_cpm.cp_cpcr = mk_cr_cmd(IDMA_PAGE, IDMA_SBLOCK, 0,
  182. CPM_CR_START_IDMA) | CPM_CR_FLG;
  183. while (immap->im_cpm.cp_cpcr & CPM_CR_FLG);
  184. /* wait for transfer to complete */
  185. while(bd->flags & IDMA_BD_V);
  186. local_irq_restore(flags);
  187. return;
  188. }
  189. /* Same as idma_pci9_read, but 16-bit little-endian byte swapping is performed
  190. * if the unit_size is 2, and 32-bit little-endian byte swapping is performed if
  191. * the unit_size is 4.
  192. */
  193. static void
  194. idma_pci9_read_le(u8 *dst, u8 *src, int bytes, int unit_size, int sinc)
  195. {
  196. int i;
  197. u8 *p;
  198. idma_pci9_read(dst, src, bytes, unit_size, sinc);
  199. switch(unit_size) {
  200. case 2:
  201. for (i = 0, p = dst; i < bytes; i += 2, p += 2)
  202. swab16s((u16 *) p);
  203. break;
  204. case 4:
  205. for (i = 0, p = dst; i < bytes; i += 4, p += 4)
  206. swab32s((u32 *) p);
  207. break;
  208. default:
  209. break;
  210. }
  211. }
  212. EXPORT_SYMBOL(idma_pci9_init);
  213. EXPORT_SYMBOL(idma_pci9_read);
  214. EXPORT_SYMBOL(idma_pci9_read_le);
  215. static inline int is_pci_mem(unsigned long addr)
  216. {
  217. if (addr >= M82xx_PCI_LOWER_MMIO &&
  218. addr <= M82xx_PCI_UPPER_MMIO)
  219. return 1;
  220. if (addr >= M82xx_PCI_LOWER_MEM &&
  221. addr <= M82xx_PCI_UPPER_MEM)
  222. return 1;
  223. return 0;
  224. }
  225. #define is_pci_mem(pa) ( (pa > 0x80000000) && (pa < 0xc0000000))
  226. int readb(volatile unsigned char *addr)
  227. {
  228. u8 val;
  229. unsigned long pa = iopa((unsigned long) addr);
  230. if (!is_pci_mem(pa))
  231. return in_8(addr);
  232. idma_pci9_read((u8 *)&val, (u8 *)pa, sizeof(val), sizeof(val), 0);
  233. return val;
  234. }
  235. int readw(volatile unsigned short *addr)
  236. {
  237. u16 val;
  238. unsigned long pa = iopa((unsigned long) addr);
  239. if (!is_pci_mem(pa))
  240. return in_le16(addr);
  241. idma_pci9_read((u8 *)&val, (u8 *)pa, sizeof(val), sizeof(val), 0);
  242. return swab16(val);
  243. }
  244. unsigned readl(volatile unsigned *addr)
  245. {
  246. u32 val;
  247. unsigned long pa = iopa((unsigned long) addr);
  248. if (!is_pci_mem(pa))
  249. return in_le32(addr);
  250. idma_pci9_read((u8 *)&val, (u8 *)pa, sizeof(val), sizeof(val), 0);
  251. return swab32(val);
  252. }
  253. int inb(unsigned port)
  254. {
  255. u8 val;
  256. u8 *addr = (u8 *)(port + _IO_BASE);
  257. idma_pci9_read((u8 *)&val, (u8 *)addr, sizeof(val), sizeof(val), 0);
  258. return val;
  259. }
  260. int inw(unsigned port)
  261. {
  262. u16 val;
  263. u8 *addr = (u8 *)(port + _IO_BASE);
  264. idma_pci9_read((u8 *)&val, (u8 *)addr, sizeof(val), sizeof(val), 0);
  265. return swab16(val);
  266. }
  267. unsigned inl(unsigned port)
  268. {
  269. u32 val;
  270. u8 *addr = (u8 *)(port + _IO_BASE);
  271. idma_pci9_read((u8 *)&val, (u8 *)addr, sizeof(val), sizeof(val), 0);
  272. return swab32(val);
  273. }
  274. void insb(unsigned port, void *buf, int ns)
  275. {
  276. u8 *addr = (u8 *)(port + _IO_BASE);
  277. idma_pci9_read((u8 *)buf, (u8 *)addr, ns*sizeof(u8), sizeof(u8), 0);
  278. }
  279. void insw(unsigned port, void *buf, int ns)
  280. {
  281. u8 *addr = (u8 *)(port + _IO_BASE);
  282. idma_pci9_read((u8 *)buf, (u8 *)addr, ns*sizeof(u16), sizeof(u16), 0);
  283. }
  284. void insl(unsigned port, void *buf, int nl)
  285. {
  286. u8 *addr = (u8 *)(port + _IO_BASE);
  287. idma_pci9_read((u8 *)buf, (u8 *)addr, nl*sizeof(u32), sizeof(u32), 0);
  288. }
  289. void insw_ns(unsigned port, void *buf, int ns)
  290. {
  291. u8 *addr = (u8 *)(port + _IO_BASE);
  292. idma_pci9_read((u8 *)buf, (u8 *)addr, ns*sizeof(u16), sizeof(u16), 0);
  293. }
  294. void insl_ns(unsigned port, void *buf, int nl)
  295. {
  296. u8 *addr = (u8 *)(port + _IO_BASE);
  297. idma_pci9_read((u8 *)buf, (u8 *)addr, nl*sizeof(u32), sizeof(u32), 0);
  298. }
  299. void *memcpy_fromio(void *dest, unsigned long src, size_t count)
  300. {
  301. unsigned long pa = iopa((unsigned long) src);
  302. if (is_pci_mem(pa))
  303. idma_pci9_read((u8 *)dest, (u8 *)pa, count, 32, 1);
  304. else
  305. memcpy(dest, (void *)src, count);
  306. return dest;
  307. }
  308. EXPORT_SYMBOL(readb);
  309. EXPORT_SYMBOL(readw);
  310. EXPORT_SYMBOL(readl);
  311. EXPORT_SYMBOL(inb);
  312. EXPORT_SYMBOL(inw);
  313. EXPORT_SYMBOL(inl);
  314. EXPORT_SYMBOL(insb);
  315. EXPORT_SYMBOL(insw);
  316. EXPORT_SYMBOL(insl);
  317. EXPORT_SYMBOL(insw_ns);
  318. EXPORT_SYMBOL(insl_ns);
  319. EXPORT_SYMBOL(memcpy_fromio);
  320. #endif /* ifdef CONFIG_8260_PCI9 */
  321. /* Indirect PCI routines adapted from arch/ppc/kernel/indirect_pci.c.
  322. * Copyright (C) 1998 Gabriel Paubert.
  323. */
  324. #ifndef CONFIG_8260_PCI9
  325. #define cfg_read(val, addr, type, op) *val = op((type)(addr))
  326. #else
  327. #define cfg_read(val, addr, type, op) \
  328. idma_pci9_read_le((u8*)(val),(u8*)(addr),sizeof(*(val)),sizeof(*(val)),0)
  329. #endif
  330. #define cfg_write(val, addr, type, op) op((type *)(addr), (val))
  331. static int indirect_write_config(struct pci_bus *pbus, unsigned int devfn, int where,
  332. int size, u32 value)
  333. {
  334. struct pci_controller *hose = pbus->sysdata;
  335. u8 cfg_type = 0;
  336. if (ppc_md.pci_exclude_device)
  337. if (ppc_md.pci_exclude_device(pbus->number, devfn))
  338. return PCIBIOS_DEVICE_NOT_FOUND;
  339. if (hose->set_cfg_type)
  340. if (pbus->number != hose->first_busno)
  341. cfg_type = 1;
  342. out_be32(hose->cfg_addr,
  343. (((where & 0xfc) | cfg_type) << 24) | (devfn << 16)
  344. | ((pbus->number - hose->bus_offset) << 8) | 0x80);
  345. switch (size)
  346. {
  347. case 1:
  348. cfg_write(value, hose->cfg_data + (where & 3), u8, out_8);
  349. break;
  350. case 2:
  351. cfg_write(value, hose->cfg_data + (where & 2), u16, out_le16);
  352. break;
  353. case 4:
  354. cfg_write(value, hose->cfg_data + (where & 0), u32, out_le32);
  355. break;
  356. }
  357. return PCIBIOS_SUCCESSFUL;
  358. }
  359. static int indirect_read_config(struct pci_bus *pbus, unsigned int devfn, int where,
  360. int size, u32 *value)
  361. {
  362. struct pci_controller *hose = pbus->sysdata;
  363. u8 cfg_type = 0;
  364. if (ppc_md.pci_exclude_device)
  365. if (ppc_md.pci_exclude_device(pbus->number, devfn))
  366. return PCIBIOS_DEVICE_NOT_FOUND;
  367. if (hose->set_cfg_type)
  368. if (pbus->number != hose->first_busno)
  369. cfg_type = 1;
  370. out_be32(hose->cfg_addr,
  371. (((where & 0xfc) | cfg_type) << 24) | (devfn << 16)
  372. | ((pbus->number - hose->bus_offset) << 8) | 0x80);
  373. switch (size)
  374. {
  375. case 1:
  376. cfg_read(value, hose->cfg_data + (where & 3), u8 *, in_8);
  377. break;
  378. case 2:
  379. cfg_read(value, hose->cfg_data + (where & 2), u16 *, in_le16);
  380. break;
  381. case 4:
  382. cfg_read(value, hose->cfg_data + (where & 0), u32 *, in_le32);
  383. break;
  384. }
  385. return PCIBIOS_SUCCESSFUL;
  386. }
  387. static struct pci_ops indirect_pci_ops =
  388. {
  389. .read = indirect_read_config,
  390. .write = indirect_write_config,
  391. };
  392. void
  393. setup_m8260_indirect_pci(struct pci_controller* hose, u32 cfg_addr, u32 cfg_data)
  394. {
  395. hose->ops = &indirect_pci_ops;
  396. hose->cfg_addr = (unsigned int *) ioremap(cfg_addr, 4);
  397. hose->cfg_data = (unsigned char *) ioremap(cfg_data, 4);
  398. }