io.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  1. /*
  2. * arch/arm/mach-ixp4xx/include/mach/io.h
  3. *
  4. * Author: Deepak Saxena <dsaxena@plexity.net>
  5. *
  6. * Copyright (C) 2002-2005 MontaVista Software, Inc.
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. */
  12. #ifndef __ASM_ARM_ARCH_IO_H
  13. #define __ASM_ARM_ARCH_IO_H
  14. #include <linux/bitops.h>
  15. #include <mach/hardware.h>
  16. extern int (*ixp4xx_pci_read)(u32 addr, u32 cmd, u32* data);
  17. extern int ixp4xx_pci_write(u32 addr, u32 cmd, u32 data);
  18. /*
  19. * IXP4xx provides two methods of accessing PCI memory space:
  20. *
  21. * 1) A direct mapped window from 0x48000000 to 0x4BFFFFFF (64MB).
  22. * To access PCI via this space, we simply ioremap() the BAR
  23. * into the kernel and we can use the standard read[bwl]/write[bwl]
  24. * macros. This is the preffered method due to speed but it
  25. * limits the system to just 64MB of PCI memory. This can be
  26. * problematic if using video cards and other memory-heavy targets.
  27. *
  28. * 2) If > 64MB of memory space is required, the IXP4xx can use indirect
  29. * registers to access the whole 4 GB of PCI memory space (as we do below
  30. * for I/O transactions). This allows currently for up to 1 GB (0x10000000
  31. * to 0x4FFFFFFF) of memory on the bus. The disadvantage of this is that
  32. * every PCI access requires three local register accesses plus a spinlock,
  33. * but in some cases the performance hit is acceptable. In addition, you
  34. * cannot mmap() PCI devices in this case.
  35. */
  36. #ifdef CONFIG_IXP4XX_INDIRECT_PCI
  37. /*
  38. * In the case of using indirect PCI, we simply return the actual PCI
  39. * address and our read/write implementation use that to drive the
  40. * access registers. If something outside of PCI is ioremap'd, we
  41. * fallback to the default.
  42. */
  43. static inline int is_pci_memory(u32 addr)
  44. {
  45. return (addr >= PCIBIOS_MIN_MEM) && (addr <= 0x4FFFFFFF);
  46. }
  47. #define writeb(v, p) __indirect_writeb(v, p)
  48. #define writew(v, p) __indirect_writew(v, p)
  49. #define writel(v, p) __indirect_writel(v, p)
  50. #define writesb(p, v, l) __indirect_writesb(p, v, l)
  51. #define writesw(p, v, l) __indirect_writesw(p, v, l)
  52. #define writesl(p, v, l) __indirect_writesl(p, v, l)
  53. #define readb(p) __indirect_readb(p)
  54. #define readw(p) __indirect_readw(p)
  55. #define readl(p) __indirect_readl(p)
  56. #define readsb(p, v, l) __indirect_readsb(p, v, l)
  57. #define readsw(p, v, l) __indirect_readsw(p, v, l)
  58. #define readsl(p, v, l) __indirect_readsl(p, v, l)
  59. static inline void __indirect_writeb(u8 value, volatile void __iomem *p)
  60. {
  61. u32 addr = (u32)p;
  62. u32 n, byte_enables, data;
  63. if (!is_pci_memory(addr)) {
  64. __raw_writeb(value, addr);
  65. return;
  66. }
  67. n = addr % 4;
  68. byte_enables = (0xf & ~BIT(n)) << IXP4XX_PCI_NP_CBE_BESL;
  69. data = value << (8*n);
  70. ixp4xx_pci_write(addr, byte_enables | NP_CMD_MEMWRITE, data);
  71. }
  72. static inline void __indirect_writesb(volatile void __iomem *bus_addr,
  73. const u8 *vaddr, int count)
  74. {
  75. while (count--)
  76. writeb(*vaddr++, bus_addr);
  77. }
  78. static inline void __indirect_writew(u16 value, volatile void __iomem *p)
  79. {
  80. u32 addr = (u32)p;
  81. u32 n, byte_enables, data;
  82. if (!is_pci_memory(addr)) {
  83. __raw_writew(value, addr);
  84. return;
  85. }
  86. n = addr % 4;
  87. byte_enables = (0xf & ~(BIT(n) | BIT(n+1))) << IXP4XX_PCI_NP_CBE_BESL;
  88. data = value << (8*n);
  89. ixp4xx_pci_write(addr, byte_enables | NP_CMD_MEMWRITE, data);
  90. }
  91. static inline void __indirect_writesw(volatile void __iomem *bus_addr,
  92. const u16 *vaddr, int count)
  93. {
  94. while (count--)
  95. writew(*vaddr++, bus_addr);
  96. }
  97. static inline void __indirect_writel(u32 value, volatile void __iomem *p)
  98. {
  99. u32 addr = (__force u32)p;
  100. if (!is_pci_memory(addr)) {
  101. __raw_writel(value, p);
  102. return;
  103. }
  104. ixp4xx_pci_write(addr, NP_CMD_MEMWRITE, value);
  105. }
  106. static inline void __indirect_writesl(volatile void __iomem *bus_addr,
  107. const u32 *vaddr, int count)
  108. {
  109. while (count--)
  110. writel(*vaddr++, bus_addr);
  111. }
  112. static inline unsigned char __indirect_readb(const volatile void __iomem *p)
  113. {
  114. u32 addr = (u32)p;
  115. u32 n, byte_enables, data;
  116. if (!is_pci_memory(addr))
  117. return __raw_readb(addr);
  118. n = addr % 4;
  119. byte_enables = (0xf & ~BIT(n)) << IXP4XX_PCI_NP_CBE_BESL;
  120. if (ixp4xx_pci_read(addr, byte_enables | NP_CMD_MEMREAD, &data))
  121. return 0xff;
  122. return data >> (8*n);
  123. }
  124. static inline void __indirect_readsb(const volatile void __iomem *bus_addr,
  125. u8 *vaddr, u32 count)
  126. {
  127. while (count--)
  128. *vaddr++ = readb(bus_addr);
  129. }
  130. static inline unsigned short __indirect_readw(const volatile void __iomem *p)
  131. {
  132. u32 addr = (u32)p;
  133. u32 n, byte_enables, data;
  134. if (!is_pci_memory(addr))
  135. return __raw_readw(addr);
  136. n = addr % 4;
  137. byte_enables = (0xf & ~(BIT(n) | BIT(n+1))) << IXP4XX_PCI_NP_CBE_BESL;
  138. if (ixp4xx_pci_read(addr, byte_enables | NP_CMD_MEMREAD, &data))
  139. return 0xffff;
  140. return data>>(8*n);
  141. }
  142. static inline void __indirect_readsw(const volatile void __iomem *bus_addr,
  143. u16 *vaddr, u32 count)
  144. {
  145. while (count--)
  146. *vaddr++ = readw(bus_addr);
  147. }
  148. static inline unsigned long __indirect_readl(const volatile void __iomem *p)
  149. {
  150. u32 addr = (__force u32)p;
  151. u32 data;
  152. if (!is_pci_memory(addr))
  153. return __raw_readl(p);
  154. if (ixp4xx_pci_read(addr, NP_CMD_MEMREAD, &data))
  155. return 0xffffffff;
  156. return data;
  157. }
  158. static inline void __indirect_readsl(const volatile void __iomem *bus_addr,
  159. u32 *vaddr, u32 count)
  160. {
  161. while (count--)
  162. *vaddr++ = readl(bus_addr);
  163. }
  164. /*
  165. * We can use the built-in functions b/c they end up calling writeb/readb
  166. */
  167. #define memset_io(c,v,l) _memset_io((c),(v),(l))
  168. #define memcpy_fromio(a,c,l) _memcpy_fromio((a),(c),(l))
  169. #define memcpy_toio(c,a,l) _memcpy_toio((c),(a),(l))
  170. #endif /* CONFIG_IXP4XX_INDIRECT_PCI */
  171. #ifndef CONFIG_PCI
  172. #define __io(v) __typesafe_io(v)
  173. #else
  174. /*
  175. * IXP4xx does not have a transparent cpu -> PCI I/O translation
  176. * window. Instead, it has a set of registers that must be tweaked
  177. * with the proper byte lanes, command types, and address for the
  178. * transaction. This means that we need to override the default
  179. * I/O functions.
  180. */
  181. static inline void outb(u8 value, u32 addr)
  182. {
  183. u32 n, byte_enables, data;
  184. n = addr % 4;
  185. byte_enables = (0xf & ~BIT(n)) << IXP4XX_PCI_NP_CBE_BESL;
  186. data = value << (8*n);
  187. ixp4xx_pci_write(addr, byte_enables | NP_CMD_IOWRITE, data);
  188. }
  189. static inline void outsb(u32 io_addr, const u8 *vaddr, u32 count)
  190. {
  191. while (count--)
  192. outb(*vaddr++, io_addr);
  193. }
  194. static inline void outw(u16 value, u32 addr)
  195. {
  196. u32 n, byte_enables, data;
  197. n = addr % 4;
  198. byte_enables = (0xf & ~(BIT(n) | BIT(n+1))) << IXP4XX_PCI_NP_CBE_BESL;
  199. data = value << (8*n);
  200. ixp4xx_pci_write(addr, byte_enables | NP_CMD_IOWRITE, data);
  201. }
  202. static inline void outsw(u32 io_addr, const u16 *vaddr, u32 count)
  203. {
  204. while (count--)
  205. outw(cpu_to_le16(*vaddr++), io_addr);
  206. }
  207. static inline void outl(u32 value, u32 addr)
  208. {
  209. ixp4xx_pci_write(addr, NP_CMD_IOWRITE, value);
  210. }
  211. static inline void outsl(u32 io_addr, const u32 *vaddr, u32 count)
  212. {
  213. while (count--)
  214. outl(cpu_to_le32(*vaddr++), io_addr);
  215. }
  216. static inline u8 inb(u32 addr)
  217. {
  218. u32 n, byte_enables, data;
  219. n = addr % 4;
  220. byte_enables = (0xf & ~BIT(n)) << IXP4XX_PCI_NP_CBE_BESL;
  221. if (ixp4xx_pci_read(addr, byte_enables | NP_CMD_IOREAD, &data))
  222. return 0xff;
  223. return data >> (8*n);
  224. }
  225. static inline void insb(u32 io_addr, u8 *vaddr, u32 count)
  226. {
  227. while (count--)
  228. *vaddr++ = inb(io_addr);
  229. }
  230. static inline u16 inw(u32 addr)
  231. {
  232. u32 n, byte_enables, data;
  233. n = addr % 4;
  234. byte_enables = (0xf & ~(BIT(n) | BIT(n+1))) << IXP4XX_PCI_NP_CBE_BESL;
  235. if (ixp4xx_pci_read(addr, byte_enables | NP_CMD_IOREAD, &data))
  236. return 0xffff;
  237. return data>>(8*n);
  238. }
  239. static inline void insw(u32 io_addr, u16 *vaddr, u32 count)
  240. {
  241. while (count--)
  242. *vaddr++ = le16_to_cpu(inw(io_addr));
  243. }
  244. static inline u32 inl(u32 addr)
  245. {
  246. u32 data;
  247. if (ixp4xx_pci_read(addr, NP_CMD_IOREAD, &data))
  248. return 0xffffffff;
  249. return data;
  250. }
  251. static inline void insl(u32 io_addr, u32 *vaddr, u32 count)
  252. {
  253. while (count--)
  254. *vaddr++ = le32_to_cpu(inl(io_addr));
  255. }
  256. #define PIO_OFFSET 0x10000UL
  257. #define PIO_MASK 0x0ffffUL
  258. #define __is_io_address(p) (((unsigned long)p >= PIO_OFFSET) && \
  259. ((unsigned long)p <= (PIO_MASK + PIO_OFFSET)))
  260. #define ioread8(p) ioread8(p)
  261. static inline unsigned int ioread8(const void __iomem *addr)
  262. {
  263. unsigned long port = (unsigned long __force)addr;
  264. if (__is_io_address(port))
  265. return (unsigned int)inb(port & PIO_MASK);
  266. else
  267. #ifndef CONFIG_IXP4XX_INDIRECT_PCI
  268. return (unsigned int)__raw_readb(addr);
  269. #else
  270. return (unsigned int)__indirect_readb(addr);
  271. #endif
  272. }
  273. #define ioread8_rep(p, v, c) ioread8_rep(p, v, c)
  274. static inline void ioread8_rep(const void __iomem *addr, void *vaddr, u32 count)
  275. {
  276. unsigned long port = (unsigned long __force)addr;
  277. if (__is_io_address(port))
  278. insb(port & PIO_MASK, vaddr, count);
  279. else
  280. #ifndef CONFIG_IXP4XX_INDIRECT_PCI
  281. __raw_readsb(addr, vaddr, count);
  282. #else
  283. __indirect_readsb(addr, vaddr, count);
  284. #endif
  285. }
  286. #define ioread16(p) ioread16(p)
  287. static inline unsigned int ioread16(const void __iomem *addr)
  288. {
  289. unsigned long port = (unsigned long __force)addr;
  290. if (__is_io_address(port))
  291. return (unsigned int)inw(port & PIO_MASK);
  292. else
  293. #ifndef CONFIG_IXP4XX_INDIRECT_PCI
  294. return le16_to_cpu((__force __le16)__raw_readw(addr));
  295. #else
  296. return (unsigned int)__indirect_readw(addr);
  297. #endif
  298. }
  299. #define ioread16_rep(p, v, c) ioread16_rep(p, v, c)
  300. static inline void ioread16_rep(const void __iomem *addr, void *vaddr,
  301. u32 count)
  302. {
  303. unsigned long port = (unsigned long __force)addr;
  304. if (__is_io_address(port))
  305. insw(port & PIO_MASK, vaddr, count);
  306. else
  307. #ifndef CONFIG_IXP4XX_INDIRECT_PCI
  308. __raw_readsw(addr, vaddr, count);
  309. #else
  310. __indirect_readsw(addr, vaddr, count);
  311. #endif
  312. }
  313. #define ioread32(p) ioread32(p)
  314. static inline unsigned int ioread32(const void __iomem *addr)
  315. {
  316. unsigned long port = (unsigned long __force)addr;
  317. if (__is_io_address(port))
  318. return (unsigned int)inl(port & PIO_MASK);
  319. else {
  320. #ifndef CONFIG_IXP4XX_INDIRECT_PCI
  321. return le32_to_cpu((__force __le32)__raw_readl(addr));
  322. #else
  323. return (unsigned int)__indirect_readl(addr);
  324. #endif
  325. }
  326. }
  327. #define ioread32_rep(p, v, c) ioread32_rep(p, v, c)
  328. static inline void ioread32_rep(const void __iomem *addr, void *vaddr,
  329. u32 count)
  330. {
  331. unsigned long port = (unsigned long __force)addr;
  332. if (__is_io_address(port))
  333. insl(port & PIO_MASK, vaddr, count);
  334. else
  335. #ifndef CONFIG_IXP4XX_INDIRECT_PCI
  336. __raw_readsl(addr, vaddr, count);
  337. #else
  338. __indirect_readsl(addr, vaddr, count);
  339. #endif
  340. }
  341. #define iowrite8(v, p) iowrite8(v, p)
  342. static inline void iowrite8(u8 value, void __iomem *addr)
  343. {
  344. unsigned long port = (unsigned long __force)addr;
  345. if (__is_io_address(port))
  346. outb(value, port & PIO_MASK);
  347. else
  348. #ifndef CONFIG_IXP4XX_INDIRECT_PCI
  349. __raw_writeb(value, addr);
  350. #else
  351. __indirect_writeb(value, addr);
  352. #endif
  353. }
  354. #define iowrite8_rep(p, v, c) iowrite8_rep(p, v, c)
  355. static inline void iowrite8_rep(void __iomem *addr, const void *vaddr,
  356. u32 count)
  357. {
  358. unsigned long port = (unsigned long __force)addr;
  359. if (__is_io_address(port))
  360. outsb(port & PIO_MASK, vaddr, count);
  361. else
  362. #ifndef CONFIG_IXP4XX_INDIRECT_PCI
  363. __raw_writesb(addr, vaddr, count);
  364. #else
  365. __indirect_writesb(addr, vaddr, count);
  366. #endif
  367. }
  368. #define iowrite16(v, p) iowrite16(v, p)
  369. static inline void iowrite16(u16 value, void __iomem *addr)
  370. {
  371. unsigned long port = (unsigned long __force)addr;
  372. if (__is_io_address(port))
  373. outw(value, port & PIO_MASK);
  374. else
  375. #ifndef CONFIG_IXP4XX_INDIRECT_PCI
  376. __raw_writew(cpu_to_le16(value), addr);
  377. #else
  378. __indirect_writew(value, addr);
  379. #endif
  380. }
  381. #define iowrite16_rep(p, v, c) iowrite16_rep(p, v, c)
  382. static inline void iowrite16_rep(void __iomem *addr, const void *vaddr,
  383. u32 count)
  384. {
  385. unsigned long port = (unsigned long __force)addr;
  386. if (__is_io_address(port))
  387. outsw(port & PIO_MASK, vaddr, count);
  388. else
  389. #ifndef CONFIG_IXP4XX_INDIRECT_PCI
  390. __raw_writesw(addr, vaddr, count);
  391. #else
  392. __indirect_writesw(addr, vaddr, count);
  393. #endif
  394. }
  395. #define iowrite32(v, p) iowrite32(v, p)
  396. static inline void iowrite32(u32 value, void __iomem *addr)
  397. {
  398. unsigned long port = (unsigned long __force)addr;
  399. if (__is_io_address(port))
  400. outl(value, port & PIO_MASK);
  401. else
  402. #ifndef CONFIG_IXP4XX_INDIRECT_PCI
  403. __raw_writel((u32 __force)cpu_to_le32(value), addr);
  404. #else
  405. __indirect_writel(value, addr);
  406. #endif
  407. }
  408. #define iowrite32_rep(p, v, c) iowrite32_rep(p, v, c)
  409. static inline void iowrite32_rep(void __iomem *addr, const void *vaddr,
  410. u32 count)
  411. {
  412. unsigned long port = (unsigned long __force)addr;
  413. if (__is_io_address(port))
  414. outsl(port & PIO_MASK, vaddr, count);
  415. else
  416. #ifndef CONFIG_IXP4XX_INDIRECT_PCI
  417. __raw_writesl(addr, vaddr, count);
  418. #else
  419. __indirect_writesl(addr, vaddr, count);
  420. #endif
  421. }
  422. #define ioport_map(port, nr) ((void __iomem*)(port + PIO_OFFSET))
  423. #define ioport_unmap(addr)
  424. #endif /* CONFIG_PCI */
  425. #endif /* __ASM_ARM_ARCH_IO_H */