io.h 12 KB

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