io.h 13 KB

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