io.h 16 KB

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