m8260_pci_erratum9.c 12 KB

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