io.h 13 KB

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