m8260_pci_erratum9.c 13 KB

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