io.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600
  1. /*
  2. * Copyright (C) 1994, 1995 Waldorf GmbH
  3. * Copyright (C) 1994 - 2000, 06 Ralf Baechle
  4. * Copyright (C) 1999, 2000 Silicon Graphics, Inc.
  5. * Copyright (C) 2004, 2005 MIPS Technologies, Inc. All rights reserved.
  6. * Author: Maciej W. Rozycki <macro@mips.com>
  7. *
  8. * SPDX-License-Identifier: GPL-2.0
  9. */
  10. #ifndef _ASM_IO_H
  11. #define _ASM_IO_H
  12. #include <linux/bug.h>
  13. #include <linux/compiler.h>
  14. #include <linux/types.h>
  15. #include <asm/addrspace.h>
  16. #include <asm/byteorder.h>
  17. #include <asm/cpu-features.h>
  18. #include <asm/pgtable-bits.h>
  19. #include <asm/processor.h>
  20. #include <asm/string.h>
  21. #include <ioremap.h>
  22. #include <mangle-port.h>
  23. #include <spaces.h>
  24. /*
  25. * Slowdown I/O port space accesses for antique hardware.
  26. */
  27. #undef CONF_SLOWDOWN_IO
  28. /*
  29. * Raw operations are never swapped in software. OTOH values that raw
  30. * operations are working on may or may not have been swapped by the bus
  31. * hardware. An example use would be for flash memory that's used for
  32. * execute in place.
  33. */
  34. # define __raw_ioswabb(a, x) (x)
  35. # define __raw_ioswabw(a, x) (x)
  36. # define __raw_ioswabl(a, x) (x)
  37. # define __raw_ioswabq(a, x) (x)
  38. # define ____raw_ioswabq(a, x) (x)
  39. /* ioswab[bwlq], __mem_ioswab[bwlq] are defined in mangle-port.h */
  40. #define IO_SPACE_LIMIT 0xffff
  41. /*
  42. * On MIPS I/O ports are memory mapped, so we access them using normal
  43. * load/store instructions. mips_io_port_base is the virtual address to
  44. * which all ports are being mapped. For sake of efficiency some code
  45. * assumes that this is an address that can be loaded with a single lui
  46. * instruction, so the lower 16 bits must be zero. Should be true on
  47. * on any sane architecture; generic code does not use this assumption.
  48. */
  49. extern const unsigned long mips_io_port_base;
  50. /*
  51. * Gcc will generate code to load the value of mips_io_port_base after each
  52. * function call which may be fairly wasteful in some cases. So we don't
  53. * play quite by the book. We tell gcc mips_io_port_base is a long variable
  54. * which solves the code generation issue. Now we need to violate the
  55. * aliasing rules a little to make initialization possible and finally we
  56. * will need the barrier() to fight side effects of the aliasing chat.
  57. * This trickery will eventually collapse under gcc's optimizer. Oh well.
  58. */
  59. static inline void set_io_port_base(unsigned long base)
  60. {
  61. * (unsigned long *) &mips_io_port_base = base;
  62. barrier();
  63. }
  64. /*
  65. * Thanks to James van Artsdalen for a better timing-fix than
  66. * the two short jumps: using outb's to a nonexistent port seems
  67. * to guarantee better timings even on fast machines.
  68. *
  69. * On the other hand, I'd like to be sure of a non-existent port:
  70. * I feel a bit unsafe about using 0x80 (should be safe, though)
  71. *
  72. * Linus
  73. *
  74. */
  75. #define __SLOW_DOWN_IO \
  76. __asm__ __volatile__( \
  77. "sb\t$0,0x80(%0)" \
  78. : : "r" (mips_io_port_base));
  79. #ifdef CONF_SLOWDOWN_IO
  80. #ifdef REALLY_SLOW_IO
  81. #define SLOW_DOWN_IO { __SLOW_DOWN_IO; __SLOW_DOWN_IO; __SLOW_DOWN_IO; __SLOW_DOWN_IO; }
  82. #else
  83. #define SLOW_DOWN_IO __SLOW_DOWN_IO
  84. #endif
  85. #else
  86. #define SLOW_DOWN_IO
  87. #endif
  88. /*
  89. * virt_to_phys - map virtual addresses to physical
  90. * @address: address to remap
  91. *
  92. * The returned physical address is the physical (CPU) mapping for
  93. * the memory address given. It is only valid to use this function on
  94. * addresses directly mapped or allocated via kmalloc.
  95. *
  96. * This function does not give bus mappings for DMA transfers. In
  97. * almost all conceivable cases a device driver should not be using
  98. * this function
  99. */
  100. static inline unsigned long virt_to_phys(volatile const void *address)
  101. {
  102. unsigned long addr = (unsigned long)address;
  103. /* this corresponds to kernel implementation of __pa() */
  104. #ifdef CONFIG_64BIT
  105. if (addr < CKSEG0)
  106. return XPHYSADDR(addr);
  107. return CPHYSADDR(addr);
  108. #else
  109. return addr - PAGE_OFFSET + PHYS_OFFSET;
  110. #endif
  111. }
  112. /*
  113. * phys_to_virt - map physical address to virtual
  114. * @address: address to remap
  115. *
  116. * The returned virtual address is a current CPU mapping for
  117. * the memory address given. It is only valid to use this function on
  118. * addresses that have a kernel mapping
  119. *
  120. * This function does not handle bus mappings for DMA transfers. In
  121. * almost all conceivable cases a device driver should not be using
  122. * this function
  123. */
  124. static inline void *phys_to_virt(unsigned long address)
  125. {
  126. return (void *)(address + PAGE_OFFSET - PHYS_OFFSET);
  127. }
  128. /*
  129. * ISA I/O bus memory addresses are 1:1 with the physical address.
  130. */
  131. static inline unsigned long isa_virt_to_bus(volatile void *address)
  132. {
  133. return (unsigned long)address - PAGE_OFFSET;
  134. }
  135. static inline void *isa_bus_to_virt(unsigned long address)
  136. {
  137. return (void *)(address + PAGE_OFFSET);
  138. }
  139. #define isa_page_to_bus page_to_phys
  140. /*
  141. * However PCI ones are not necessarily 1:1 and therefore these interfaces
  142. * are forbidden in portable PCI drivers.
  143. *
  144. * Allow them for x86 for legacy drivers, though.
  145. */
  146. #define virt_to_bus virt_to_phys
  147. #define bus_to_virt phys_to_virt
  148. static inline void __iomem *__ioremap_mode(phys_addr_t offset, unsigned long size,
  149. unsigned long flags)
  150. {
  151. void __iomem *addr;
  152. phys_addr_t phys_addr;
  153. addr = plat_ioremap(offset, size, flags);
  154. if (addr)
  155. return addr;
  156. phys_addr = fixup_bigphys_addr(offset, size);
  157. return (void __iomem *)(unsigned long)CKSEG1ADDR(phys_addr);
  158. }
  159. /*
  160. * ioremap - map bus memory into CPU space
  161. * @offset: bus address of the memory
  162. * @size: size of the resource to map
  163. *
  164. * ioremap performs a platform specific sequence of operations to
  165. * make bus memory CPU accessible via the readb/readw/readl/writeb/
  166. * writew/writel functions and the other mmio helpers. The returned
  167. * address is not guaranteed to be usable directly as a virtual
  168. * address.
  169. */
  170. #define ioremap(offset, size) \
  171. __ioremap_mode((offset), (size), _CACHE_UNCACHED)
  172. /*
  173. * ioremap_nocache - map bus memory into CPU space
  174. * @offset: bus address of the memory
  175. * @size: size of the resource to map
  176. *
  177. * ioremap_nocache performs a platform specific sequence of operations to
  178. * make bus memory CPU accessible via the readb/readw/readl/writeb/
  179. * writew/writel functions and the other mmio helpers. The returned
  180. * address is not guaranteed to be usable directly as a virtual
  181. * address.
  182. *
  183. * This version of ioremap ensures that the memory is marked uncachable
  184. * on the CPU as well as honouring existing caching rules from things like
  185. * the PCI bus. Note that there are other caches and buffers on many
  186. * busses. In particular driver authors should read up on PCI writes
  187. *
  188. * It's useful if some control registers are in such an area and
  189. * write combining or read caching is not desirable:
  190. */
  191. #define ioremap_nocache(offset, size) \
  192. __ioremap_mode((offset), (size), _CACHE_UNCACHED)
  193. #define ioremap_uc ioremap_nocache
  194. /*
  195. * ioremap_cachable - map bus memory into CPU space
  196. * @offset: bus address of the memory
  197. * @size: size of the resource to map
  198. *
  199. * ioremap_nocache performs a platform specific sequence of operations to
  200. * make bus memory CPU accessible via the readb/readw/readl/writeb/
  201. * writew/writel functions and the other mmio helpers. The returned
  202. * address is not guaranteed to be usable directly as a virtual
  203. * address.
  204. *
  205. * This version of ioremap ensures that the memory is marked cachable by
  206. * the CPU. Also enables full write-combining. Useful for some
  207. * memory-like regions on I/O busses.
  208. */
  209. #define ioremap_cachable(offset, size) \
  210. __ioremap_mode((offset), (size), _page_cachable_default)
  211. /*
  212. * These two are MIPS specific ioremap variant. ioremap_cacheable_cow
  213. * requests a cachable mapping, ioremap_uncached_accelerated requests a
  214. * mapping using the uncached accelerated mode which isn't supported on
  215. * all processors.
  216. */
  217. #define ioremap_cacheable_cow(offset, size) \
  218. __ioremap_mode((offset), (size), _CACHE_CACHABLE_COW)
  219. #define ioremap_uncached_accelerated(offset, size) \
  220. __ioremap_mode((offset), (size), _CACHE_UNCACHED_ACCELERATED)
  221. static inline void iounmap(const volatile void __iomem *addr)
  222. {
  223. plat_iounmap(addr);
  224. }
  225. #ifdef CONFIG_CPU_CAVIUM_OCTEON
  226. #define war_octeon_io_reorder_wmb() wmb()
  227. #else
  228. #define war_octeon_io_reorder_wmb() do { } while (0)
  229. #endif
  230. #define __BUILD_MEMORY_SINGLE(pfx, bwlq, type, irq) \
  231. \
  232. static inline void pfx##write##bwlq(type val, \
  233. volatile void __iomem *mem) \
  234. { \
  235. volatile type *__mem; \
  236. type __val; \
  237. \
  238. war_octeon_io_reorder_wmb(); \
  239. \
  240. __mem = (void *)__swizzle_addr_##bwlq((unsigned long)(mem)); \
  241. \
  242. __val = pfx##ioswab##bwlq(__mem, val); \
  243. \
  244. if (sizeof(type) != sizeof(u64) || sizeof(u64) == sizeof(long)) \
  245. *__mem = __val; \
  246. else if (cpu_has_64bits) { \
  247. type __tmp; \
  248. \
  249. __asm__ __volatile__( \
  250. ".set arch=r4000" "\t\t# __writeq""\n\t" \
  251. "dsll32 %L0, %L0, 0" "\n\t" \
  252. "dsrl32 %L0, %L0, 0" "\n\t" \
  253. "dsll32 %M0, %M0, 0" "\n\t" \
  254. "or %L0, %L0, %M0" "\n\t" \
  255. "sd %L0, %2" "\n\t" \
  256. ".set mips0" "\n" \
  257. : "=r" (__tmp) \
  258. : "0" (__val), "m" (*__mem)); \
  259. } else \
  260. BUG(); \
  261. } \
  262. \
  263. static inline type pfx##read##bwlq(const volatile void __iomem *mem) \
  264. { \
  265. volatile type *__mem; \
  266. type __val; \
  267. \
  268. __mem = (void *)__swizzle_addr_##bwlq((unsigned long)(mem)); \
  269. \
  270. if (sizeof(type) != sizeof(u64) || sizeof(u64) == sizeof(long)) \
  271. __val = *__mem; \
  272. else if (cpu_has_64bits) { \
  273. __asm__ __volatile__( \
  274. ".set arch=r4000" "\t\t# __readq" "\n\t" \
  275. "ld %L0, %1" "\n\t" \
  276. "dsra32 %M0, %L0, 0" "\n\t" \
  277. "sll %L0, %L0, 0" "\n\t" \
  278. ".set mips0" "\n" \
  279. : "=r" (__val) \
  280. : "m" (*__mem)); \
  281. } else { \
  282. __val = 0; \
  283. BUG(); \
  284. } \
  285. \
  286. return pfx##ioswab##bwlq(__mem, __val); \
  287. }
  288. #define __BUILD_IOPORT_SINGLE(pfx, bwlq, type, p, slow) \
  289. \
  290. static inline void pfx##out##bwlq##p(type val, unsigned long port) \
  291. { \
  292. volatile type *__addr; \
  293. type __val; \
  294. \
  295. war_octeon_io_reorder_wmb(); \
  296. \
  297. __addr = (void *)__swizzle_addr_##bwlq(mips_io_port_base + port); \
  298. \
  299. __val = pfx##ioswab##bwlq(__addr, val); \
  300. \
  301. /* Really, we want this to be atomic */ \
  302. BUILD_BUG_ON(sizeof(type) > sizeof(unsigned long)); \
  303. \
  304. *__addr = __val; \
  305. slow; \
  306. } \
  307. \
  308. static inline type pfx##in##bwlq##p(unsigned long port) \
  309. { \
  310. volatile type *__addr; \
  311. type __val; \
  312. \
  313. __addr = (void *)__swizzle_addr_##bwlq(mips_io_port_base + port); \
  314. \
  315. BUILD_BUG_ON(sizeof(type) > sizeof(unsigned long)); \
  316. \
  317. __val = *__addr; \
  318. slow; \
  319. \
  320. return pfx##ioswab##bwlq(__addr, __val); \
  321. }
  322. #define __BUILD_MEMORY_PFX(bus, bwlq, type) \
  323. \
  324. __BUILD_MEMORY_SINGLE(bus, bwlq, type, 1)
  325. #define BUILDIO_MEM(bwlq, type) \
  326. \
  327. __BUILD_MEMORY_PFX(__raw_, bwlq, type) \
  328. __BUILD_MEMORY_PFX(, bwlq, type) \
  329. __BUILD_MEMORY_PFX(__mem_, bwlq, type) \
  330. BUILDIO_MEM(b, u8)
  331. BUILDIO_MEM(w, u16)
  332. BUILDIO_MEM(l, u32)
  333. BUILDIO_MEM(q, u64)
  334. #define __BUILD_IOPORT_PFX(bus, bwlq, type) \
  335. __BUILD_IOPORT_SINGLE(bus, bwlq, type, ,) \
  336. __BUILD_IOPORT_SINGLE(bus, bwlq, type, _p, SLOW_DOWN_IO)
  337. #define BUILDIO_IOPORT(bwlq, type) \
  338. __BUILD_IOPORT_PFX(, bwlq, type) \
  339. __BUILD_IOPORT_PFX(__mem_, bwlq, type)
  340. BUILDIO_IOPORT(b, u8)
  341. BUILDIO_IOPORT(w, u16)
  342. BUILDIO_IOPORT(l, u32)
  343. #ifdef CONFIG_64BIT
  344. BUILDIO_IOPORT(q, u64)
  345. #endif
  346. #define __BUILDIO(bwlq, type) \
  347. \
  348. __BUILD_MEMORY_SINGLE(____raw_, bwlq, type, 0)
  349. __BUILDIO(q, u64)
  350. #define readb_relaxed readb
  351. #define readw_relaxed readw
  352. #define readl_relaxed readl
  353. #define readq_relaxed readq
  354. #define writeb_relaxed writeb
  355. #define writew_relaxed writew
  356. #define writel_relaxed writel
  357. #define writeq_relaxed writeq
  358. #define readb_be(addr) \
  359. __raw_readb((__force unsigned *)(addr))
  360. #define readw_be(addr) \
  361. be16_to_cpu(__raw_readw((__force unsigned *)(addr)))
  362. #define readl_be(addr) \
  363. be32_to_cpu(__raw_readl((__force unsigned *)(addr)))
  364. #define readq_be(addr) \
  365. be64_to_cpu(__raw_readq((__force unsigned *)(addr)))
  366. #define writeb_be(val, addr) \
  367. __raw_writeb((val), (__force unsigned *)(addr))
  368. #define writew_be(val, addr) \
  369. __raw_writew(cpu_to_be16((val)), (__force unsigned *)(addr))
  370. #define writel_be(val, addr) \
  371. __raw_writel(cpu_to_be32((val)), (__force unsigned *)(addr))
  372. #define writeq_be(val, addr) \
  373. __raw_writeq(cpu_to_be64((val)), (__force unsigned *)(addr))
  374. /*
  375. * Some code tests for these symbols
  376. */
  377. #define readq readq
  378. #define writeq writeq
  379. #define __BUILD_MEMORY_STRING(bwlq, type) \
  380. \
  381. static inline void writes##bwlq(volatile void __iomem *mem, \
  382. const void *addr, unsigned int count) \
  383. { \
  384. const volatile type *__addr = addr; \
  385. \
  386. while (count--) { \
  387. __mem_write##bwlq(*__addr, mem); \
  388. __addr++; \
  389. } \
  390. } \
  391. \
  392. static inline void reads##bwlq(volatile void __iomem *mem, void *addr, \
  393. unsigned int count) \
  394. { \
  395. volatile type *__addr = addr; \
  396. \
  397. while (count--) { \
  398. *__addr = __mem_read##bwlq(mem); \
  399. __addr++; \
  400. } \
  401. }
  402. #define __BUILD_IOPORT_STRING(bwlq, type) \
  403. \
  404. static inline void outs##bwlq(unsigned long port, const void *addr, \
  405. unsigned int count) \
  406. { \
  407. const volatile type *__addr = addr; \
  408. \
  409. while (count--) { \
  410. __mem_out##bwlq(*__addr, port); \
  411. __addr++; \
  412. } \
  413. } \
  414. \
  415. static inline void ins##bwlq(unsigned long port, void *addr, \
  416. unsigned int count) \
  417. { \
  418. volatile type *__addr = addr; \
  419. \
  420. while (count--) { \
  421. *__addr = __mem_in##bwlq(port); \
  422. __addr++; \
  423. } \
  424. }
  425. #define BUILDSTRING(bwlq, type) \
  426. \
  427. __BUILD_MEMORY_STRING(bwlq, type) \
  428. __BUILD_IOPORT_STRING(bwlq, type)
  429. BUILDSTRING(b, u8)
  430. BUILDSTRING(w, u16)
  431. BUILDSTRING(l, u32)
  432. #ifdef CONFIG_64BIT
  433. BUILDSTRING(q, u64)
  434. #endif
  435. #ifdef CONFIG_CPU_CAVIUM_OCTEON
  436. #define mmiowb() wmb()
  437. #else
  438. /* Depends on MIPS II instruction set */
  439. #define mmiowb() asm volatile ("sync" ::: "memory")
  440. #endif
  441. static inline void memset_io(volatile void __iomem *addr, unsigned char val, int count)
  442. {
  443. memset((void __force *)addr, val, count);
  444. }
  445. static inline void memcpy_fromio(void *dst, const volatile void __iomem *src, int count)
  446. {
  447. memcpy(dst, (void __force *)src, count);
  448. }
  449. static inline void memcpy_toio(volatile void __iomem *dst, const void *src, int count)
  450. {
  451. memcpy((void __force *)dst, src, count);
  452. }
  453. /*
  454. * Read a 32-bit register that requires a 64-bit read cycle on the bus.
  455. * Avoid interrupt mucking, just adjust the address for 4-byte access.
  456. * Assume the addresses are 8-byte aligned.
  457. */
  458. #ifdef __MIPSEB__
  459. #define __CSR_32_ADJUST 4
  460. #else
  461. #define __CSR_32_ADJUST 0
  462. #endif
  463. #define csr_out32(v, a) (*(volatile u32 *)((unsigned long)(a) + __CSR_32_ADJUST) = (v))
  464. #define csr_in32(a) (*(volatile u32 *)((unsigned long)(a) + __CSR_32_ADJUST))
  465. /*
  466. * U-Boot specific
  467. */
  468. #define sync() mmiowb()
  469. #define MAP_NOCACHE (1)
  470. #define MAP_WRCOMBINE (0)
  471. #define MAP_WRBACK (0)
  472. #define MAP_WRTHROUGH (0)
  473. static inline void *
  474. map_physmem(phys_addr_t paddr, unsigned long len, unsigned long flags)
  475. {
  476. if (flags == MAP_NOCACHE)
  477. return ioremap(paddr, len);
  478. return (void *)paddr;
  479. }
  480. /*
  481. * Take down a mapping set up by map_physmem().
  482. */
  483. static inline void unmap_physmem(void *vaddr, unsigned long flags)
  484. {
  485. }
  486. #define __BUILD_CLRBITS(bwlq, sfx, end, type) \
  487. \
  488. static inline void clrbits_##sfx(volatile void __iomem *mem, type clr) \
  489. { \
  490. type __val = __raw_read##bwlq(mem); \
  491. __val = end##_to_cpu(__val); \
  492. __val &= ~clr; \
  493. __val = cpu_to_##end(__val); \
  494. __raw_write##bwlq(__val, mem); \
  495. }
  496. #define __BUILD_SETBITS(bwlq, sfx, end, type) \
  497. \
  498. static inline void setbits_##sfx(volatile void __iomem *mem, type set) \
  499. { \
  500. type __val = __raw_read##bwlq(mem); \
  501. __val = end##_to_cpu(__val); \
  502. __val |= set; \
  503. __val = cpu_to_##end(__val); \
  504. __raw_write##bwlq(__val, mem); \
  505. }
  506. #define __BUILD_CLRSETBITS(bwlq, sfx, end, type) \
  507. \
  508. static inline void clrsetbits_##sfx(volatile void __iomem *mem, \
  509. type clr, type set) \
  510. { \
  511. type __val = __raw_read##bwlq(mem); \
  512. __val = end##_to_cpu(__val); \
  513. __val &= ~clr; \
  514. __val |= set; \
  515. __val = cpu_to_##end(__val); \
  516. __raw_write##bwlq(__val, mem); \
  517. }
  518. #define BUILD_CLRSETBITS(bwlq, sfx, end, type) \
  519. \
  520. __BUILD_CLRBITS(bwlq, sfx, end, type) \
  521. __BUILD_SETBITS(bwlq, sfx, end, type) \
  522. __BUILD_CLRSETBITS(bwlq, sfx, end, type)
  523. #define __to_cpu(v) (v)
  524. #define cpu_to__(v) (v)
  525. BUILD_CLRSETBITS(b, 8, _, u8)
  526. BUILD_CLRSETBITS(w, le16, le16, u16)
  527. BUILD_CLRSETBITS(w, be16, be16, u16)
  528. BUILD_CLRSETBITS(w, 16, _, u16)
  529. BUILD_CLRSETBITS(l, le32, le32, u32)
  530. BUILD_CLRSETBITS(l, be32, be32, u32)
  531. BUILD_CLRSETBITS(l, 32, _, u32)
  532. BUILD_CLRSETBITS(q, le64, le64, u64)
  533. BUILD_CLRSETBITS(q, be64, be64, u64)
  534. BUILD_CLRSETBITS(q, 64, _, u64)
  535. #endif /* _ASM_IO_H */