io.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509
  1. /*
  2. * Copyright 2010 Tilera Corporation. All Rights Reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License
  6. * as published by the Free Software Foundation, version 2.
  7. *
  8. * This program is distributed in the hope that it will be useful, but
  9. * WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
  11. * NON INFRINGEMENT. See the GNU General Public License for
  12. * more details.
  13. */
  14. #ifndef _ASM_TILE_IO_H
  15. #define _ASM_TILE_IO_H
  16. #include <linux/kernel.h>
  17. #include <linux/bug.h>
  18. #include <asm/page.h>
  19. /* Maximum PCI I/O space address supported. */
  20. #define IO_SPACE_LIMIT 0xffffffff
  21. /*
  22. * Convert a physical pointer to a virtual kernel pointer for /dev/mem
  23. * access.
  24. */
  25. #define xlate_dev_mem_ptr(p) __va(p)
  26. /*
  27. * Convert a virtual cached pointer to an uncached pointer.
  28. */
  29. #define xlate_dev_kmem_ptr(p) p
  30. /*
  31. * Change "struct page" to physical address.
  32. */
  33. #define page_to_phys(page) ((dma_addr_t)page_to_pfn(page) << PAGE_SHIFT)
  34. /*
  35. * Some places try to pass in an loff_t for PHYSADDR (?!), so we cast it to
  36. * long before casting it to a pointer to avoid compiler warnings.
  37. */
  38. #if CHIP_HAS_MMIO()
  39. extern void __iomem *ioremap(resource_size_t offset, unsigned long size);
  40. extern void __iomem *ioremap_prot(resource_size_t offset, unsigned long size,
  41. pgprot_t pgprot);
  42. extern void iounmap(volatile void __iomem *addr);
  43. #else
  44. #define ioremap(physaddr, size) ((void __iomem *)(unsigned long)(physaddr))
  45. #define iounmap(addr) ((void)0)
  46. #endif
  47. #define ioremap_nocache(physaddr, size) ioremap(physaddr, size)
  48. #define ioremap_wc(physaddr, size) ioremap(physaddr, size)
  49. #define ioremap_wt(physaddr, size) ioremap(physaddr, size)
  50. #define ioremap_uc(physaddr, size) ioremap(physaddr, size)
  51. #define ioremap_fullcache(physaddr, size) ioremap(physaddr, size)
  52. #define mmiowb()
  53. /* Conversion between virtual and physical mappings. */
  54. #define mm_ptov(addr) ((void *)phys_to_virt(addr))
  55. #define mm_vtop(addr) ((unsigned long)virt_to_phys(addr))
  56. #if CHIP_HAS_MMIO()
  57. /*
  58. * We use inline assembly to guarantee that the compiler does not
  59. * split an access into multiple byte-sized accesses as it might
  60. * sometimes do if a register data structure is marked "packed".
  61. * Obviously on tile we can't tolerate such an access being
  62. * actually unaligned, but we want to avoid the case where the
  63. * compiler conservatively would generate multiple accesses even
  64. * for an aligned read or write.
  65. */
  66. static inline u8 __raw_readb(const volatile void __iomem *addr)
  67. {
  68. return *(const volatile u8 __force *)addr;
  69. }
  70. static inline u16 __raw_readw(const volatile void __iomem *addr)
  71. {
  72. u16 ret;
  73. asm volatile("ld2u %0, %1" : "=r" (ret) : "r" (addr));
  74. barrier();
  75. return le16_to_cpu(ret);
  76. }
  77. static inline u32 __raw_readl(const volatile void __iomem *addr)
  78. {
  79. u32 ret;
  80. /* Sign-extend to conform to u32 ABI sign-extension convention. */
  81. asm volatile("ld4s %0, %1" : "=r" (ret) : "r" (addr));
  82. barrier();
  83. return le32_to_cpu(ret);
  84. }
  85. static inline u64 __raw_readq(const volatile void __iomem *addr)
  86. {
  87. u64 ret;
  88. asm volatile("ld %0, %1" : "=r" (ret) : "r" (addr));
  89. barrier();
  90. return le64_to_cpu(ret);
  91. }
  92. static inline void __raw_writeb(u8 val, volatile void __iomem *addr)
  93. {
  94. *(volatile u8 __force *)addr = val;
  95. }
  96. static inline void __raw_writew(u16 val, volatile void __iomem *addr)
  97. {
  98. asm volatile("st2 %0, %1" :: "r" (addr), "r" (cpu_to_le16(val)));
  99. }
  100. static inline void __raw_writel(u32 val, volatile void __iomem *addr)
  101. {
  102. asm volatile("st4 %0, %1" :: "r" (addr), "r" (cpu_to_le32(val)));
  103. }
  104. static inline void __raw_writeq(u64 val, volatile void __iomem *addr)
  105. {
  106. asm volatile("st %0, %1" :: "r" (addr), "r" (cpu_to_le64(val)));
  107. }
  108. /*
  109. * The on-chip I/O hardware on tilegx is configured with VA=PA for the
  110. * kernel's PA range. The low-level APIs and field names use "va" and
  111. * "void *" nomenclature, to be consistent with the general notion
  112. * that the addresses in question are virtualizable, but in the kernel
  113. * context we are actually manipulating PA values. (In other contexts,
  114. * e.g. access from user space, we do in fact use real virtual addresses
  115. * in the va fields.) To allow readers of the code to understand what's
  116. * happening, we direct their attention to this comment by using the
  117. * following two functions that just duplicate __va() and __pa().
  118. */
  119. typedef unsigned long tile_io_addr_t;
  120. static inline tile_io_addr_t va_to_tile_io_addr(void *va)
  121. {
  122. BUILD_BUG_ON(sizeof(phys_addr_t) != sizeof(tile_io_addr_t));
  123. return __pa(va);
  124. }
  125. static inline void *tile_io_addr_to_va(tile_io_addr_t tile_io_addr)
  126. {
  127. return __va(tile_io_addr);
  128. }
  129. #else /* CHIP_HAS_MMIO() */
  130. #ifdef CONFIG_PCI
  131. extern u8 _tile_readb(unsigned long addr);
  132. extern u16 _tile_readw(unsigned long addr);
  133. extern u32 _tile_readl(unsigned long addr);
  134. extern u64 _tile_readq(unsigned long addr);
  135. extern void _tile_writeb(u8 val, unsigned long addr);
  136. extern void _tile_writew(u16 val, unsigned long addr);
  137. extern void _tile_writel(u32 val, unsigned long addr);
  138. extern void _tile_writeq(u64 val, unsigned long addr);
  139. #define __raw_readb(addr) _tile_readb((unsigned long)addr)
  140. #define __raw_readw(addr) _tile_readw((unsigned long)addr)
  141. #define __raw_readl(addr) _tile_readl((unsigned long)addr)
  142. #define __raw_readq(addr) _tile_readq((unsigned long)addr)
  143. #define __raw_writeb(val, addr) _tile_writeb(val, (unsigned long)addr)
  144. #define __raw_writew(val, addr) _tile_writew(val, (unsigned long)addr)
  145. #define __raw_writel(val, addr) _tile_writel(val, (unsigned long)addr)
  146. #define __raw_writeq(val, addr) _tile_writeq(val, (unsigned long)addr)
  147. #else /* CONFIG_PCI */
  148. /*
  149. * The tilepro architecture does not support IOMEM unless PCI is enabled.
  150. * Unfortunately we can't yet simply not declare these methods,
  151. * since some generic code that compiles into the kernel, but
  152. * we never run, uses them unconditionally.
  153. */
  154. static inline int iomem_panic(void)
  155. {
  156. panic("readb/writeb and friends do not exist on tile without PCI");
  157. return 0;
  158. }
  159. static inline u8 readb(unsigned long addr)
  160. {
  161. return iomem_panic();
  162. }
  163. static inline u16 _readw(unsigned long addr)
  164. {
  165. return iomem_panic();
  166. }
  167. static inline u32 readl(unsigned long addr)
  168. {
  169. return iomem_panic();
  170. }
  171. static inline u64 readq(unsigned long addr)
  172. {
  173. return iomem_panic();
  174. }
  175. static inline void writeb(u8 val, unsigned long addr)
  176. {
  177. iomem_panic();
  178. }
  179. static inline void writew(u16 val, unsigned long addr)
  180. {
  181. iomem_panic();
  182. }
  183. static inline void writel(u32 val, unsigned long addr)
  184. {
  185. iomem_panic();
  186. }
  187. static inline void writeq(u64 val, unsigned long addr)
  188. {
  189. iomem_panic();
  190. }
  191. #endif /* CONFIG_PCI */
  192. #endif /* CHIP_HAS_MMIO() */
  193. #define readb __raw_readb
  194. #define readw __raw_readw
  195. #define readl __raw_readl
  196. #define readq __raw_readq
  197. #define writeb __raw_writeb
  198. #define writew __raw_writew
  199. #define writel __raw_writel
  200. #define writeq __raw_writeq
  201. #define readb_relaxed readb
  202. #define readw_relaxed readw
  203. #define readl_relaxed readl
  204. #define readq_relaxed readq
  205. #define writeb_relaxed writeb
  206. #define writew_relaxed writew
  207. #define writel_relaxed writel
  208. #define writeq_relaxed writeq
  209. #define ioread8 readb
  210. #define ioread16 readw
  211. #define ioread32 readl
  212. #define ioread64 readq
  213. #define iowrite8 writeb
  214. #define iowrite16 writew
  215. #define iowrite32 writel
  216. #define iowrite64 writeq
  217. #if CHIP_HAS_MMIO() || defined(CONFIG_PCI)
  218. static inline void memset_io(volatile void *dst, int val, size_t len)
  219. {
  220. size_t x;
  221. BUG_ON((unsigned long)dst & 0x3);
  222. val = (val & 0xff) * 0x01010101;
  223. for (x = 0; x < len; x += 4)
  224. writel(val, dst + x);
  225. }
  226. static inline void memcpy_fromio(void *dst, const volatile void __iomem *src,
  227. size_t len)
  228. {
  229. size_t x;
  230. BUG_ON((unsigned long)src & 0x3);
  231. for (x = 0; x < len; x += 4)
  232. *(u32 *)(dst + x) = readl(src + x);
  233. }
  234. static inline void memcpy_toio(volatile void __iomem *dst, const void *src,
  235. size_t len)
  236. {
  237. size_t x;
  238. BUG_ON((unsigned long)dst & 0x3);
  239. for (x = 0; x < len; x += 4)
  240. writel(*(u32 *)(src + x), dst + x);
  241. }
  242. #endif
  243. #if CHIP_HAS_MMIO() && defined(CONFIG_TILE_PCI_IO)
  244. static inline u8 inb(unsigned long addr)
  245. {
  246. return readb((volatile void __iomem *) addr);
  247. }
  248. static inline u16 inw(unsigned long addr)
  249. {
  250. return readw((volatile void __iomem *) addr);
  251. }
  252. static inline u32 inl(unsigned long addr)
  253. {
  254. return readl((volatile void __iomem *) addr);
  255. }
  256. static inline void outb(u8 b, unsigned long addr)
  257. {
  258. writeb(b, (volatile void __iomem *) addr);
  259. }
  260. static inline void outw(u16 b, unsigned long addr)
  261. {
  262. writew(b, (volatile void __iomem *) addr);
  263. }
  264. static inline void outl(u32 b, unsigned long addr)
  265. {
  266. writel(b, (volatile void __iomem *) addr);
  267. }
  268. static inline void insb(unsigned long addr, void *buffer, int count)
  269. {
  270. if (count) {
  271. u8 *buf = buffer;
  272. do {
  273. u8 x = inb(addr);
  274. *buf++ = x;
  275. } while (--count);
  276. }
  277. }
  278. static inline void insw(unsigned long addr, void *buffer, int count)
  279. {
  280. if (count) {
  281. u16 *buf = buffer;
  282. do {
  283. u16 x = inw(addr);
  284. *buf++ = x;
  285. } while (--count);
  286. }
  287. }
  288. static inline void insl(unsigned long addr, void *buffer, int count)
  289. {
  290. if (count) {
  291. u32 *buf = buffer;
  292. do {
  293. u32 x = inl(addr);
  294. *buf++ = x;
  295. } while (--count);
  296. }
  297. }
  298. static inline void outsb(unsigned long addr, const void *buffer, int count)
  299. {
  300. if (count) {
  301. const u8 *buf = buffer;
  302. do {
  303. outb(*buf++, addr);
  304. } while (--count);
  305. }
  306. }
  307. static inline void outsw(unsigned long addr, const void *buffer, int count)
  308. {
  309. if (count) {
  310. const u16 *buf = buffer;
  311. do {
  312. outw(*buf++, addr);
  313. } while (--count);
  314. }
  315. }
  316. static inline void outsl(unsigned long addr, const void *buffer, int count)
  317. {
  318. if (count) {
  319. const u32 *buf = buffer;
  320. do {
  321. outl(*buf++, addr);
  322. } while (--count);
  323. }
  324. }
  325. extern void __iomem *ioport_map(unsigned long port, unsigned int len);
  326. extern void ioport_unmap(void __iomem *addr);
  327. #else
  328. /*
  329. * The TilePro architecture does not support IOPORT, even with PCI.
  330. * Unfortunately we can't yet simply not declare these methods,
  331. * since some generic code that compiles into the kernel, but
  332. * we never run, uses them unconditionally.
  333. */
  334. static inline long ioport_panic(void)
  335. {
  336. #ifdef __tilegx__
  337. panic("PCI IO space support is disabled. Configure the kernel with CONFIG_TILE_PCI_IO to enable it");
  338. #else
  339. panic("inb/outb and friends do not exist on tile");
  340. #endif
  341. return 0;
  342. }
  343. static inline void __iomem *ioport_map(unsigned long port, unsigned int len)
  344. {
  345. pr_info("ioport_map: mapping IO resources is unsupported on tile\n");
  346. return NULL;
  347. }
  348. static inline void ioport_unmap(void __iomem *addr)
  349. {
  350. ioport_panic();
  351. }
  352. static inline u8 inb(unsigned long addr)
  353. {
  354. return ioport_panic();
  355. }
  356. static inline u16 inw(unsigned long addr)
  357. {
  358. return ioport_panic();
  359. }
  360. static inline u32 inl(unsigned long addr)
  361. {
  362. return ioport_panic();
  363. }
  364. static inline void outb(u8 b, unsigned long addr)
  365. {
  366. ioport_panic();
  367. }
  368. static inline void outw(u16 b, unsigned long addr)
  369. {
  370. ioport_panic();
  371. }
  372. static inline void outl(u32 b, unsigned long addr)
  373. {
  374. ioport_panic();
  375. }
  376. static inline void insb(unsigned long addr, void *buffer, int count)
  377. {
  378. ioport_panic();
  379. }
  380. static inline void insw(unsigned long addr, void *buffer, int count)
  381. {
  382. ioport_panic();
  383. }
  384. static inline void insl(unsigned long addr, void *buffer, int count)
  385. {
  386. ioport_panic();
  387. }
  388. static inline void outsb(unsigned long addr, const void *buffer, int count)
  389. {
  390. ioport_panic();
  391. }
  392. static inline void outsw(unsigned long addr, const void *buffer, int count)
  393. {
  394. ioport_panic();
  395. }
  396. static inline void outsl(unsigned long addr, const void *buffer, int count)
  397. {
  398. ioport_panic();
  399. }
  400. #endif /* CHIP_HAS_MMIO() && defined(CONFIG_TILE_PCI_IO) */
  401. #define inb_p(addr) inb(addr)
  402. #define inw_p(addr) inw(addr)
  403. #define inl_p(addr) inl(addr)
  404. #define outb_p(x, addr) outb((x), (addr))
  405. #define outw_p(x, addr) outw((x), (addr))
  406. #define outl_p(x, addr) outl((x), (addr))
  407. #define ioread16be(addr) be16_to_cpu(ioread16(addr))
  408. #define ioread32be(addr) be32_to_cpu(ioread32(addr))
  409. #define iowrite16be(v, addr) iowrite16(be16_to_cpu(v), (addr))
  410. #define iowrite32be(v, addr) iowrite32(be32_to_cpu(v), (addr))
  411. #define ioread8_rep(p, dst, count) \
  412. insb((unsigned long) (p), (dst), (count))
  413. #define ioread16_rep(p, dst, count) \
  414. insw((unsigned long) (p), (dst), (count))
  415. #define ioread32_rep(p, dst, count) \
  416. insl((unsigned long) (p), (dst), (count))
  417. #define iowrite8_rep(p, src, count) \
  418. outsb((unsigned long) (p), (src), (count))
  419. #define iowrite16_rep(p, src, count) \
  420. outsw((unsigned long) (p), (src), (count))
  421. #define iowrite32_rep(p, src, count) \
  422. outsl((unsigned long) (p), (src), (count))
  423. #define virt_to_bus virt_to_phys
  424. #define bus_to_virt phys_to_virt
  425. #endif /* _ASM_TILE_IO_H */