io.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef __ALPHA_IO_H
  3. #define __ALPHA_IO_H
  4. #ifdef __KERNEL__
  5. #include <linux/kernel.h>
  6. #include <linux/mm.h>
  7. #include <asm/compiler.h>
  8. #include <asm/pgtable.h>
  9. #include <asm/machvec.h>
  10. #include <asm/hwrpb.h>
  11. /* The generic header contains only prototypes. Including it ensures that
  12. the implementation we have here matches that interface. */
  13. #include <asm-generic/iomap.h>
  14. /* We don't use IO slowdowns on the Alpha, but.. */
  15. #define __SLOW_DOWN_IO do { } while (0)
  16. #define SLOW_DOWN_IO do { } while (0)
  17. /*
  18. * Virtual -> physical identity mapping starts at this offset
  19. */
  20. #ifdef USE_48_BIT_KSEG
  21. #define IDENT_ADDR 0xffff800000000000UL
  22. #else
  23. #define IDENT_ADDR 0xfffffc0000000000UL
  24. #endif
  25. /*
  26. * We try to avoid hae updates (thus the cache), but when we
  27. * do need to update the hae, we need to do it atomically, so
  28. * that any interrupts wouldn't get confused with the hae
  29. * register not being up-to-date with respect to the hardware
  30. * value.
  31. */
  32. extern inline void __set_hae(unsigned long new_hae)
  33. {
  34. unsigned long flags = swpipl(IPL_MAX);
  35. barrier();
  36. alpha_mv.hae_cache = new_hae;
  37. *alpha_mv.hae_register = new_hae;
  38. mb();
  39. /* Re-read to make sure it was written. */
  40. new_hae = *alpha_mv.hae_register;
  41. setipl(flags);
  42. barrier();
  43. }
  44. extern inline void set_hae(unsigned long new_hae)
  45. {
  46. if (new_hae != alpha_mv.hae_cache)
  47. __set_hae(new_hae);
  48. }
  49. /*
  50. * Change virtual addresses to physical addresses and vv.
  51. */
  52. #ifdef USE_48_BIT_KSEG
  53. static inline unsigned long virt_to_phys(void *address)
  54. {
  55. return (unsigned long)address - IDENT_ADDR;
  56. }
  57. static inline void * phys_to_virt(unsigned long address)
  58. {
  59. return (void *) (address + IDENT_ADDR);
  60. }
  61. #else
  62. static inline unsigned long virt_to_phys(void *address)
  63. {
  64. unsigned long phys = (unsigned long)address;
  65. /* Sign-extend from bit 41. */
  66. phys <<= (64 - 41);
  67. phys = (long)phys >> (64 - 41);
  68. /* Crop to the physical address width of the processor. */
  69. phys &= (1ul << hwrpb->pa_bits) - 1;
  70. return phys;
  71. }
  72. static inline void * phys_to_virt(unsigned long address)
  73. {
  74. return (void *)(IDENT_ADDR + (address & ((1ul << 41) - 1)));
  75. }
  76. #endif
  77. #define page_to_phys(page) page_to_pa(page)
  78. static inline dma_addr_t __deprecated isa_page_to_bus(struct page *page)
  79. {
  80. return page_to_phys(page);
  81. }
  82. /* Maximum PIO space address supported? */
  83. #define IO_SPACE_LIMIT 0xffff
  84. /*
  85. * Change addresses as seen by the kernel (virtual) to addresses as
  86. * seen by a device (bus), and vice versa.
  87. *
  88. * Note that this only works for a limited range of kernel addresses,
  89. * and very well may not span all memory. Consider this interface
  90. * deprecated in favour of the DMA-mapping API.
  91. */
  92. extern unsigned long __direct_map_base;
  93. extern unsigned long __direct_map_size;
  94. static inline unsigned long __deprecated virt_to_bus(void *address)
  95. {
  96. unsigned long phys = virt_to_phys(address);
  97. unsigned long bus = phys + __direct_map_base;
  98. return phys <= __direct_map_size ? bus : 0;
  99. }
  100. #define isa_virt_to_bus virt_to_bus
  101. static inline void * __deprecated bus_to_virt(unsigned long address)
  102. {
  103. void *virt;
  104. /* This check is a sanity check but also ensures that bus address 0
  105. maps to virtual address 0 which is useful to detect null pointers
  106. (the NCR driver is much simpler if NULL pointers are preserved). */
  107. address -= __direct_map_base;
  108. virt = phys_to_virt(address);
  109. return (long)address <= 0 ? NULL : virt;
  110. }
  111. #define isa_bus_to_virt bus_to_virt
  112. /*
  113. * There are different chipsets to interface the Alpha CPUs to the world.
  114. */
  115. #define IO_CONCAT(a,b) _IO_CONCAT(a,b)
  116. #define _IO_CONCAT(a,b) a ## _ ## b
  117. #ifdef CONFIG_ALPHA_GENERIC
  118. /* In a generic kernel, we always go through the machine vector. */
  119. #define REMAP1(TYPE, NAME, QUAL) \
  120. static inline TYPE generic_##NAME(QUAL void __iomem *addr) \
  121. { \
  122. return alpha_mv.mv_##NAME(addr); \
  123. }
  124. #define REMAP2(TYPE, NAME, QUAL) \
  125. static inline void generic_##NAME(TYPE b, QUAL void __iomem *addr) \
  126. { \
  127. alpha_mv.mv_##NAME(b, addr); \
  128. }
  129. REMAP1(unsigned int, ioread8, /**/)
  130. REMAP1(unsigned int, ioread16, /**/)
  131. REMAP1(unsigned int, ioread32, /**/)
  132. REMAP1(u8, readb, const volatile)
  133. REMAP1(u16, readw, const volatile)
  134. REMAP1(u32, readl, const volatile)
  135. REMAP1(u64, readq, const volatile)
  136. REMAP2(u8, iowrite8, /**/)
  137. REMAP2(u16, iowrite16, /**/)
  138. REMAP2(u32, iowrite32, /**/)
  139. REMAP2(u8, writeb, volatile)
  140. REMAP2(u16, writew, volatile)
  141. REMAP2(u32, writel, volatile)
  142. REMAP2(u64, writeq, volatile)
  143. #undef REMAP1
  144. #undef REMAP2
  145. extern inline void __iomem *generic_ioportmap(unsigned long a)
  146. {
  147. return alpha_mv.mv_ioportmap(a);
  148. }
  149. static inline void __iomem *generic_ioremap(unsigned long a, unsigned long s)
  150. {
  151. return alpha_mv.mv_ioremap(a, s);
  152. }
  153. static inline void generic_iounmap(volatile void __iomem *a)
  154. {
  155. return alpha_mv.mv_iounmap(a);
  156. }
  157. static inline int generic_is_ioaddr(unsigned long a)
  158. {
  159. return alpha_mv.mv_is_ioaddr(a);
  160. }
  161. static inline int generic_is_mmio(const volatile void __iomem *a)
  162. {
  163. return alpha_mv.mv_is_mmio(a);
  164. }
  165. #define __IO_PREFIX generic
  166. #define generic_trivial_rw_bw 0
  167. #define generic_trivial_rw_lq 0
  168. #define generic_trivial_io_bw 0
  169. #define generic_trivial_io_lq 0
  170. #define generic_trivial_iounmap 0
  171. #else
  172. #if defined(CONFIG_ALPHA_APECS)
  173. # include <asm/core_apecs.h>
  174. #elif defined(CONFIG_ALPHA_CIA)
  175. # include <asm/core_cia.h>
  176. #elif defined(CONFIG_ALPHA_IRONGATE)
  177. # include <asm/core_irongate.h>
  178. #elif defined(CONFIG_ALPHA_JENSEN)
  179. # include <asm/jensen.h>
  180. #elif defined(CONFIG_ALPHA_LCA)
  181. # include <asm/core_lca.h>
  182. #elif defined(CONFIG_ALPHA_MARVEL)
  183. # include <asm/core_marvel.h>
  184. #elif defined(CONFIG_ALPHA_MCPCIA)
  185. # include <asm/core_mcpcia.h>
  186. #elif defined(CONFIG_ALPHA_POLARIS)
  187. # include <asm/core_polaris.h>
  188. #elif defined(CONFIG_ALPHA_T2)
  189. # include <asm/core_t2.h>
  190. #elif defined(CONFIG_ALPHA_TSUNAMI)
  191. # include <asm/core_tsunami.h>
  192. #elif defined(CONFIG_ALPHA_TITAN)
  193. # include <asm/core_titan.h>
  194. #elif defined(CONFIG_ALPHA_WILDFIRE)
  195. # include <asm/core_wildfire.h>
  196. #else
  197. #error "What system is this?"
  198. #endif
  199. #endif /* GENERIC */
  200. /*
  201. * We always have external versions of these routines.
  202. */
  203. extern u8 inb(unsigned long port);
  204. extern u16 inw(unsigned long port);
  205. extern u32 inl(unsigned long port);
  206. extern void outb(u8 b, unsigned long port);
  207. extern void outw(u16 b, unsigned long port);
  208. extern void outl(u32 b, unsigned long port);
  209. extern u8 readb(const volatile void __iomem *addr);
  210. extern u16 readw(const volatile void __iomem *addr);
  211. extern u32 readl(const volatile void __iomem *addr);
  212. extern u64 readq(const volatile void __iomem *addr);
  213. extern void writeb(u8 b, volatile void __iomem *addr);
  214. extern void writew(u16 b, volatile void __iomem *addr);
  215. extern void writel(u32 b, volatile void __iomem *addr);
  216. extern void writeq(u64 b, volatile void __iomem *addr);
  217. extern u8 __raw_readb(const volatile void __iomem *addr);
  218. extern u16 __raw_readw(const volatile void __iomem *addr);
  219. extern u32 __raw_readl(const volatile void __iomem *addr);
  220. extern u64 __raw_readq(const volatile void __iomem *addr);
  221. extern void __raw_writeb(u8 b, volatile void __iomem *addr);
  222. extern void __raw_writew(u16 b, volatile void __iomem *addr);
  223. extern void __raw_writel(u32 b, volatile void __iomem *addr);
  224. extern void __raw_writeq(u64 b, volatile void __iomem *addr);
  225. /*
  226. * Mapping from port numbers to __iomem space is pretty easy.
  227. */
  228. /* These two have to be extern inline because of the extern prototype from
  229. <asm-generic/iomap.h>. It is not legal to mix "extern" and "static" for
  230. the same declaration. */
  231. extern inline void __iomem *ioport_map(unsigned long port, unsigned int size)
  232. {
  233. return IO_CONCAT(__IO_PREFIX,ioportmap) (port);
  234. }
  235. extern inline void ioport_unmap(void __iomem *addr)
  236. {
  237. }
  238. static inline void __iomem *ioremap(unsigned long port, unsigned long size)
  239. {
  240. return IO_CONCAT(__IO_PREFIX,ioremap) (port, size);
  241. }
  242. static inline void __iomem *__ioremap(unsigned long port, unsigned long size,
  243. unsigned long flags)
  244. {
  245. return ioremap(port, size);
  246. }
  247. static inline void __iomem * ioremap_nocache(unsigned long offset,
  248. unsigned long size)
  249. {
  250. return ioremap(offset, size);
  251. }
  252. #define ioremap_wc ioremap_nocache
  253. #define ioremap_uc ioremap_nocache
  254. static inline void iounmap(volatile void __iomem *addr)
  255. {
  256. IO_CONCAT(__IO_PREFIX,iounmap)(addr);
  257. }
  258. static inline int __is_ioaddr(unsigned long addr)
  259. {
  260. return IO_CONCAT(__IO_PREFIX,is_ioaddr)(addr);
  261. }
  262. #define __is_ioaddr(a) __is_ioaddr((unsigned long)(a))
  263. static inline int __is_mmio(const volatile void __iomem *addr)
  264. {
  265. return IO_CONCAT(__IO_PREFIX,is_mmio)(addr);
  266. }
  267. /*
  268. * If the actual I/O bits are sufficiently trivial, then expand inline.
  269. */
  270. #if IO_CONCAT(__IO_PREFIX,trivial_io_bw)
  271. extern inline unsigned int ioread8(void __iomem *addr)
  272. {
  273. unsigned int ret = IO_CONCAT(__IO_PREFIX,ioread8)(addr);
  274. mb();
  275. return ret;
  276. }
  277. extern inline unsigned int ioread16(void __iomem *addr)
  278. {
  279. unsigned int ret = IO_CONCAT(__IO_PREFIX,ioread16)(addr);
  280. mb();
  281. return ret;
  282. }
  283. extern inline void iowrite8(u8 b, void __iomem *addr)
  284. {
  285. IO_CONCAT(__IO_PREFIX,iowrite8)(b, addr);
  286. mb();
  287. }
  288. extern inline void iowrite16(u16 b, void __iomem *addr)
  289. {
  290. IO_CONCAT(__IO_PREFIX,iowrite16)(b, addr);
  291. mb();
  292. }
  293. extern inline u8 inb(unsigned long port)
  294. {
  295. return ioread8(ioport_map(port, 1));
  296. }
  297. extern inline u16 inw(unsigned long port)
  298. {
  299. return ioread16(ioport_map(port, 2));
  300. }
  301. extern inline void outb(u8 b, unsigned long port)
  302. {
  303. iowrite8(b, ioport_map(port, 1));
  304. }
  305. extern inline void outw(u16 b, unsigned long port)
  306. {
  307. iowrite16(b, ioport_map(port, 2));
  308. }
  309. #endif
  310. #if IO_CONCAT(__IO_PREFIX,trivial_io_lq)
  311. extern inline unsigned int ioread32(void __iomem *addr)
  312. {
  313. unsigned int ret = IO_CONCAT(__IO_PREFIX,ioread32)(addr);
  314. mb();
  315. return ret;
  316. }
  317. extern inline void iowrite32(u32 b, void __iomem *addr)
  318. {
  319. IO_CONCAT(__IO_PREFIX,iowrite32)(b, addr);
  320. mb();
  321. }
  322. extern inline u32 inl(unsigned long port)
  323. {
  324. return ioread32(ioport_map(port, 4));
  325. }
  326. extern inline void outl(u32 b, unsigned long port)
  327. {
  328. iowrite32(b, ioport_map(port, 4));
  329. }
  330. #endif
  331. #if IO_CONCAT(__IO_PREFIX,trivial_rw_bw) == 1
  332. extern inline u8 __raw_readb(const volatile void __iomem *addr)
  333. {
  334. return IO_CONCAT(__IO_PREFIX,readb)(addr);
  335. }
  336. extern inline u16 __raw_readw(const volatile void __iomem *addr)
  337. {
  338. return IO_CONCAT(__IO_PREFIX,readw)(addr);
  339. }
  340. extern inline void __raw_writeb(u8 b, volatile void __iomem *addr)
  341. {
  342. IO_CONCAT(__IO_PREFIX,writeb)(b, addr);
  343. }
  344. extern inline void __raw_writew(u16 b, volatile void __iomem *addr)
  345. {
  346. IO_CONCAT(__IO_PREFIX,writew)(b, addr);
  347. }
  348. extern inline u8 readb(const volatile void __iomem *addr)
  349. {
  350. u8 ret = __raw_readb(addr);
  351. mb();
  352. return ret;
  353. }
  354. extern inline u16 readw(const volatile void __iomem *addr)
  355. {
  356. u16 ret = __raw_readw(addr);
  357. mb();
  358. return ret;
  359. }
  360. extern inline void writeb(u8 b, volatile void __iomem *addr)
  361. {
  362. __raw_writeb(b, addr);
  363. mb();
  364. }
  365. extern inline void writew(u16 b, volatile void __iomem *addr)
  366. {
  367. __raw_writew(b, addr);
  368. mb();
  369. }
  370. #endif
  371. #if IO_CONCAT(__IO_PREFIX,trivial_rw_lq) == 1
  372. extern inline u32 __raw_readl(const volatile void __iomem *addr)
  373. {
  374. return IO_CONCAT(__IO_PREFIX,readl)(addr);
  375. }
  376. extern inline u64 __raw_readq(const volatile void __iomem *addr)
  377. {
  378. return IO_CONCAT(__IO_PREFIX,readq)(addr);
  379. }
  380. extern inline void __raw_writel(u32 b, volatile void __iomem *addr)
  381. {
  382. IO_CONCAT(__IO_PREFIX,writel)(b, addr);
  383. }
  384. extern inline void __raw_writeq(u64 b, volatile void __iomem *addr)
  385. {
  386. IO_CONCAT(__IO_PREFIX,writeq)(b, addr);
  387. }
  388. extern inline u32 readl(const volatile void __iomem *addr)
  389. {
  390. u32 ret = __raw_readl(addr);
  391. mb();
  392. return ret;
  393. }
  394. extern inline u64 readq(const volatile void __iomem *addr)
  395. {
  396. u64 ret = __raw_readq(addr);
  397. mb();
  398. return ret;
  399. }
  400. extern inline void writel(u32 b, volatile void __iomem *addr)
  401. {
  402. __raw_writel(b, addr);
  403. mb();
  404. }
  405. extern inline void writeq(u64 b, volatile void __iomem *addr)
  406. {
  407. __raw_writeq(b, addr);
  408. mb();
  409. }
  410. #endif
  411. #define ioread16be(p) be16_to_cpu(ioread16(p))
  412. #define ioread32be(p) be32_to_cpu(ioread32(p))
  413. #define iowrite16be(v,p) iowrite16(cpu_to_be16(v), (p))
  414. #define iowrite32be(v,p) iowrite32(cpu_to_be32(v), (p))
  415. #define inb_p inb
  416. #define inw_p inw
  417. #define inl_p inl
  418. #define outb_p outb
  419. #define outw_p outw
  420. #define outl_p outl
  421. #define readb_relaxed(addr) __raw_readb(addr)
  422. #define readw_relaxed(addr) __raw_readw(addr)
  423. #define readl_relaxed(addr) __raw_readl(addr)
  424. #define readq_relaxed(addr) __raw_readq(addr)
  425. #define writeb_relaxed(b, addr) __raw_writeb(b, addr)
  426. #define writew_relaxed(b, addr) __raw_writew(b, addr)
  427. #define writel_relaxed(b, addr) __raw_writel(b, addr)
  428. #define writeq_relaxed(b, addr) __raw_writeq(b, addr)
  429. #define mmiowb()
  430. /*
  431. * String version of IO memory access ops:
  432. */
  433. extern void memcpy_fromio(void *, const volatile void __iomem *, long);
  434. extern void memcpy_toio(volatile void __iomem *, const void *, long);
  435. extern void _memset_c_io(volatile void __iomem *, unsigned long, long);
  436. static inline void memset_io(volatile void __iomem *addr, u8 c, long len)
  437. {
  438. _memset_c_io(addr, 0x0101010101010101UL * c, len);
  439. }
  440. #define __HAVE_ARCH_MEMSETW_IO
  441. static inline void memsetw_io(volatile void __iomem *addr, u16 c, long len)
  442. {
  443. _memset_c_io(addr, 0x0001000100010001UL * c, len);
  444. }
  445. /*
  446. * String versions of in/out ops:
  447. */
  448. extern void insb (unsigned long port, void *dst, unsigned long count);
  449. extern void insw (unsigned long port, void *dst, unsigned long count);
  450. extern void insl (unsigned long port, void *dst, unsigned long count);
  451. extern void outsb (unsigned long port, const void *src, unsigned long count);
  452. extern void outsw (unsigned long port, const void *src, unsigned long count);
  453. extern void outsl (unsigned long port, const void *src, unsigned long count);
  454. /*
  455. * The Alpha Jensen hardware for some rather strange reason puts
  456. * the RTC clock at 0x170 instead of 0x70. Probably due to some
  457. * misguided idea about using 0x70 for NMI stuff.
  458. *
  459. * These defines will override the defaults when doing RTC queries
  460. */
  461. #ifdef CONFIG_ALPHA_GENERIC
  462. # define RTC_PORT(x) ((x) + alpha_mv.rtc_port)
  463. #else
  464. # ifdef CONFIG_ALPHA_JENSEN
  465. # define RTC_PORT(x) (0x170+(x))
  466. # else
  467. # define RTC_PORT(x) (0x70 + (x))
  468. # endif
  469. #endif
  470. #define RTC_ALWAYS_BCD 0
  471. /*
  472. * Some mucking forons use if[n]def writeq to check if platform has it.
  473. * It's a bloody bad idea and we probably want ARCH_HAS_WRITEQ for them
  474. * to play with; for now just use cpp anti-recursion logics and make sure
  475. * that damn thing is defined and expands to itself.
  476. */
  477. #define writeq writeq
  478. #define readq readq
  479. /*
  480. * Convert a physical pointer to a virtual kernel pointer for /dev/mem
  481. * access
  482. */
  483. #define xlate_dev_mem_ptr(p) __va(p)
  484. /*
  485. * Convert a virtual cached pointer to an uncached pointer
  486. */
  487. #define xlate_dev_kmem_ptr(p) p
  488. #endif /* __KERNEL__ */
  489. #endif /* __ALPHA_IO_H */