io.h 13 KB

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