io.h 13 KB

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