io.h 16 KB

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