io.h 13 KB

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