io.h 17 KB

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