io.h 16 KB

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