io.h 13 KB

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