iomap.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  1. /*
  2. * iomap.c - Implement iomap interface for PA-RISC
  3. * Copyright (c) 2004 Matthew Wilcox
  4. */
  5. #include <linux/ioport.h>
  6. #include <linux/pci.h>
  7. #include <linux/export.h>
  8. #include <asm/io.h>
  9. /*
  10. * The iomap space on 32-bit PA-RISC is intended to look like this:
  11. * 00000000-7fffffff virtual mapped IO
  12. * 80000000-8fffffff ISA/EISA port space that can't be virtually mapped
  13. * 90000000-9fffffff Dino port space
  14. * a0000000-afffffff Astro port space
  15. * b0000000-bfffffff PAT port space
  16. * c0000000-cfffffff non-swapped memory IO
  17. * f0000000-ffffffff legacy IO memory pointers
  18. *
  19. * For the moment, here's what it looks like:
  20. * 80000000-8fffffff All ISA/EISA port space
  21. * f0000000-ffffffff legacy IO memory pointers
  22. *
  23. * On 64-bit, everything is extended, so:
  24. * 8000000000000000-8fffffffffffffff All ISA/EISA port space
  25. * f000000000000000-ffffffffffffffff legacy IO memory pointers
  26. */
  27. /*
  28. * Technically, this should be 'if (VMALLOC_START < addr < VMALLOC_END),
  29. * but that's slow and we know it'll be within the first 2GB.
  30. */
  31. #ifdef CONFIG_64BIT
  32. #define INDIRECT_ADDR(addr) (((unsigned long)(addr) & 1UL<<63) != 0)
  33. #define ADDR_TO_REGION(addr) (((unsigned long)addr >> 60) & 7)
  34. #define IOPORT_MAP_BASE (8UL << 60)
  35. #else
  36. #define INDIRECT_ADDR(addr) (((unsigned long)(addr) & 1UL<<31) != 0)
  37. #define ADDR_TO_REGION(addr) (((unsigned long)addr >> 28) & 7)
  38. #define IOPORT_MAP_BASE (8UL << 28)
  39. #endif
  40. struct iomap_ops {
  41. unsigned int (*read8)(void __iomem *);
  42. unsigned int (*read16)(void __iomem *);
  43. unsigned int (*read16be)(void __iomem *);
  44. unsigned int (*read32)(void __iomem *);
  45. unsigned int (*read32be)(void __iomem *);
  46. void (*write8)(u8, void __iomem *);
  47. void (*write16)(u16, void __iomem *);
  48. void (*write16be)(u16, void __iomem *);
  49. void (*write32)(u32, void __iomem *);
  50. void (*write32be)(u32, void __iomem *);
  51. void (*read8r)(void __iomem *, void *, unsigned long);
  52. void (*read16r)(void __iomem *, void *, unsigned long);
  53. void (*read32r)(void __iomem *, void *, unsigned long);
  54. void (*write8r)(void __iomem *, const void *, unsigned long);
  55. void (*write16r)(void __iomem *, const void *, unsigned long);
  56. void (*write32r)(void __iomem *, const void *, unsigned long);
  57. };
  58. /* Generic ioport ops. To be replaced later by specific dino/elroy/wax code */
  59. #define ADDR2PORT(addr) ((unsigned long __force)(addr) & 0xffffff)
  60. static unsigned int ioport_read8(void __iomem *addr)
  61. {
  62. return inb(ADDR2PORT(addr));
  63. }
  64. static unsigned int ioport_read16(void __iomem *addr)
  65. {
  66. return inw(ADDR2PORT(addr));
  67. }
  68. static unsigned int ioport_read32(void __iomem *addr)
  69. {
  70. return inl(ADDR2PORT(addr));
  71. }
  72. static void ioport_write8(u8 datum, void __iomem *addr)
  73. {
  74. outb(datum, ADDR2PORT(addr));
  75. }
  76. static void ioport_write16(u16 datum, void __iomem *addr)
  77. {
  78. outw(datum, ADDR2PORT(addr));
  79. }
  80. static void ioport_write32(u32 datum, void __iomem *addr)
  81. {
  82. outl(datum, ADDR2PORT(addr));
  83. }
  84. static void ioport_read8r(void __iomem *addr, void *dst, unsigned long count)
  85. {
  86. insb(ADDR2PORT(addr), dst, count);
  87. }
  88. static void ioport_read16r(void __iomem *addr, void *dst, unsigned long count)
  89. {
  90. insw(ADDR2PORT(addr), dst, count);
  91. }
  92. static void ioport_read32r(void __iomem *addr, void *dst, unsigned long count)
  93. {
  94. insl(ADDR2PORT(addr), dst, count);
  95. }
  96. static void ioport_write8r(void __iomem *addr, const void *s, unsigned long n)
  97. {
  98. outsb(ADDR2PORT(addr), s, n);
  99. }
  100. static void ioport_write16r(void __iomem *addr, const void *s, unsigned long n)
  101. {
  102. outsw(ADDR2PORT(addr), s, n);
  103. }
  104. static void ioport_write32r(void __iomem *addr, const void *s, unsigned long n)
  105. {
  106. outsl(ADDR2PORT(addr), s, n);
  107. }
  108. static const struct iomap_ops ioport_ops = {
  109. .read8 = ioport_read8,
  110. .read16 = ioport_read16,
  111. .read16be = ioport_read16,
  112. .read32 = ioport_read32,
  113. .read32be = ioport_read32,
  114. .write8 = ioport_write8,
  115. .write16 = ioport_write16,
  116. .write16be = ioport_write16,
  117. .write32 = ioport_write32,
  118. .write32be = ioport_write32,
  119. .read8r = ioport_read8r,
  120. .read16r = ioport_read16r,
  121. .read32r = ioport_read32r,
  122. .write8r = ioport_write8r,
  123. .write16r = ioport_write16r,
  124. .write32r = ioport_write32r,
  125. };
  126. /* Legacy I/O memory ops */
  127. static unsigned int iomem_read8(void __iomem *addr)
  128. {
  129. return readb(addr);
  130. }
  131. static unsigned int iomem_read16(void __iomem *addr)
  132. {
  133. return readw(addr);
  134. }
  135. static unsigned int iomem_read16be(void __iomem *addr)
  136. {
  137. return __raw_readw(addr);
  138. }
  139. static unsigned int iomem_read32(void __iomem *addr)
  140. {
  141. return readl(addr);
  142. }
  143. static unsigned int iomem_read32be(void __iomem *addr)
  144. {
  145. return __raw_readl(addr);
  146. }
  147. static void iomem_write8(u8 datum, void __iomem *addr)
  148. {
  149. writeb(datum, addr);
  150. }
  151. static void iomem_write16(u16 datum, void __iomem *addr)
  152. {
  153. writew(datum, addr);
  154. }
  155. static void iomem_write16be(u16 datum, void __iomem *addr)
  156. {
  157. __raw_writew(datum, addr);
  158. }
  159. static void iomem_write32(u32 datum, void __iomem *addr)
  160. {
  161. writel(datum, addr);
  162. }
  163. static void iomem_write32be(u32 datum, void __iomem *addr)
  164. {
  165. __raw_writel(datum, addr);
  166. }
  167. static void iomem_read8r(void __iomem *addr, void *dst, unsigned long count)
  168. {
  169. while (count--) {
  170. *(u8 *)dst = __raw_readb(addr);
  171. dst++;
  172. }
  173. }
  174. static void iomem_read16r(void __iomem *addr, void *dst, unsigned long count)
  175. {
  176. while (count--) {
  177. *(u16 *)dst = __raw_readw(addr);
  178. dst += 2;
  179. }
  180. }
  181. static void iomem_read32r(void __iomem *addr, void *dst, unsigned long count)
  182. {
  183. while (count--) {
  184. *(u32 *)dst = __raw_readl(addr);
  185. dst += 4;
  186. }
  187. }
  188. static void iomem_write8r(void __iomem *addr, const void *s, unsigned long n)
  189. {
  190. while (n--) {
  191. __raw_writeb(*(u8 *)s, addr);
  192. s++;
  193. }
  194. }
  195. static void iomem_write16r(void __iomem *addr, const void *s, unsigned long n)
  196. {
  197. while (n--) {
  198. __raw_writew(*(u16 *)s, addr);
  199. s += 2;
  200. }
  201. }
  202. static void iomem_write32r(void __iomem *addr, const void *s, unsigned long n)
  203. {
  204. while (n--) {
  205. __raw_writel(*(u32 *)s, addr);
  206. s += 4;
  207. }
  208. }
  209. static const struct iomap_ops iomem_ops = {
  210. .read8 = iomem_read8,
  211. .read16 = iomem_read16,
  212. .read16be = iomem_read16be,
  213. .read32 = iomem_read32,
  214. .read32be = iomem_read32be,
  215. .write8 = iomem_write8,
  216. .write16 = iomem_write16,
  217. .write16be = iomem_write16be,
  218. .write32 = iomem_write32,
  219. .write32be = iomem_write32be,
  220. .read8r = iomem_read8r,
  221. .read16r = iomem_read16r,
  222. .read32r = iomem_read32r,
  223. .write8r = iomem_write8r,
  224. .write16r = iomem_write16r,
  225. .write32r = iomem_write32r,
  226. };
  227. static const struct iomap_ops *iomap_ops[8] = {
  228. [0] = &ioport_ops,
  229. [7] = &iomem_ops
  230. };
  231. unsigned int ioread8(void __iomem *addr)
  232. {
  233. if (unlikely(INDIRECT_ADDR(addr)))
  234. return iomap_ops[ADDR_TO_REGION(addr)]->read8(addr);
  235. return *((u8 *)addr);
  236. }
  237. unsigned int ioread16(void __iomem *addr)
  238. {
  239. if (unlikely(INDIRECT_ADDR(addr)))
  240. return iomap_ops[ADDR_TO_REGION(addr)]->read16(addr);
  241. return le16_to_cpup((u16 *)addr);
  242. }
  243. unsigned int ioread16be(void __iomem *addr)
  244. {
  245. if (unlikely(INDIRECT_ADDR(addr)))
  246. return iomap_ops[ADDR_TO_REGION(addr)]->read16be(addr);
  247. return *((u16 *)addr);
  248. }
  249. unsigned int ioread32(void __iomem *addr)
  250. {
  251. if (unlikely(INDIRECT_ADDR(addr)))
  252. return iomap_ops[ADDR_TO_REGION(addr)]->read32(addr);
  253. return le32_to_cpup((u32 *)addr);
  254. }
  255. unsigned int ioread32be(void __iomem *addr)
  256. {
  257. if (unlikely(INDIRECT_ADDR(addr)))
  258. return iomap_ops[ADDR_TO_REGION(addr)]->read32be(addr);
  259. return *((u32 *)addr);
  260. }
  261. void iowrite8(u8 datum, void __iomem *addr)
  262. {
  263. if (unlikely(INDIRECT_ADDR(addr))) {
  264. iomap_ops[ADDR_TO_REGION(addr)]->write8(datum, addr);
  265. } else {
  266. *((u8 *)addr) = datum;
  267. }
  268. }
  269. void iowrite16(u16 datum, void __iomem *addr)
  270. {
  271. if (unlikely(INDIRECT_ADDR(addr))) {
  272. iomap_ops[ADDR_TO_REGION(addr)]->write16(datum, addr);
  273. } else {
  274. *((u16 *)addr) = cpu_to_le16(datum);
  275. }
  276. }
  277. void iowrite16be(u16 datum, void __iomem *addr)
  278. {
  279. if (unlikely(INDIRECT_ADDR(addr))) {
  280. iomap_ops[ADDR_TO_REGION(addr)]->write16be(datum, addr);
  281. } else {
  282. *((u16 *)addr) = datum;
  283. }
  284. }
  285. void iowrite32(u32 datum, void __iomem *addr)
  286. {
  287. if (unlikely(INDIRECT_ADDR(addr))) {
  288. iomap_ops[ADDR_TO_REGION(addr)]->write32(datum, addr);
  289. } else {
  290. *((u32 *)addr) = cpu_to_le32(datum);
  291. }
  292. }
  293. void iowrite32be(u32 datum, void __iomem *addr)
  294. {
  295. if (unlikely(INDIRECT_ADDR(addr))) {
  296. iomap_ops[ADDR_TO_REGION(addr)]->write32be(datum, addr);
  297. } else {
  298. *((u32 *)addr) = datum;
  299. }
  300. }
  301. /* Repeating interfaces */
  302. void ioread8_rep(void __iomem *addr, void *dst, unsigned long count)
  303. {
  304. if (unlikely(INDIRECT_ADDR(addr))) {
  305. iomap_ops[ADDR_TO_REGION(addr)]->read8r(addr, dst, count);
  306. } else {
  307. while (count--) {
  308. *(u8 *)dst = *(u8 *)addr;
  309. dst++;
  310. }
  311. }
  312. }
  313. void ioread16_rep(void __iomem *addr, void *dst, unsigned long count)
  314. {
  315. if (unlikely(INDIRECT_ADDR(addr))) {
  316. iomap_ops[ADDR_TO_REGION(addr)]->read16r(addr, dst, count);
  317. } else {
  318. while (count--) {
  319. *(u16 *)dst = *(u16 *)addr;
  320. dst += 2;
  321. }
  322. }
  323. }
  324. void ioread32_rep(void __iomem *addr, void *dst, unsigned long count)
  325. {
  326. if (unlikely(INDIRECT_ADDR(addr))) {
  327. iomap_ops[ADDR_TO_REGION(addr)]->read32r(addr, dst, count);
  328. } else {
  329. while (count--) {
  330. *(u32 *)dst = *(u32 *)addr;
  331. dst += 4;
  332. }
  333. }
  334. }
  335. void iowrite8_rep(void __iomem *addr, const void *src, unsigned long count)
  336. {
  337. if (unlikely(INDIRECT_ADDR(addr))) {
  338. iomap_ops[ADDR_TO_REGION(addr)]->write8r(addr, src, count);
  339. } else {
  340. while (count--) {
  341. *(u8 *)addr = *(u8 *)src;
  342. src++;
  343. }
  344. }
  345. }
  346. void iowrite16_rep(void __iomem *addr, const void *src, unsigned long count)
  347. {
  348. if (unlikely(INDIRECT_ADDR(addr))) {
  349. iomap_ops[ADDR_TO_REGION(addr)]->write16r(addr, src, count);
  350. } else {
  351. while (count--) {
  352. *(u16 *)addr = *(u16 *)src;
  353. src += 2;
  354. }
  355. }
  356. }
  357. void iowrite32_rep(void __iomem *addr, const void *src, unsigned long count)
  358. {
  359. if (unlikely(INDIRECT_ADDR(addr))) {
  360. iomap_ops[ADDR_TO_REGION(addr)]->write32r(addr, src, count);
  361. } else {
  362. while (count--) {
  363. *(u32 *)addr = *(u32 *)src;
  364. src += 4;
  365. }
  366. }
  367. }
  368. /* Mapping interfaces */
  369. void __iomem *ioport_map(unsigned long port, unsigned int nr)
  370. {
  371. return (void __iomem *)(IOPORT_MAP_BASE | port);
  372. }
  373. void ioport_unmap(void __iomem *addr)
  374. {
  375. if (!INDIRECT_ADDR(addr)) {
  376. iounmap(addr);
  377. }
  378. }
  379. void pci_iounmap(struct pci_dev *dev, void __iomem * addr)
  380. {
  381. if (!INDIRECT_ADDR(addr)) {
  382. iounmap(addr);
  383. }
  384. }
  385. EXPORT_SYMBOL(ioread8);
  386. EXPORT_SYMBOL(ioread16);
  387. EXPORT_SYMBOL(ioread16be);
  388. EXPORT_SYMBOL(ioread32);
  389. EXPORT_SYMBOL(ioread32be);
  390. EXPORT_SYMBOL(iowrite8);
  391. EXPORT_SYMBOL(iowrite16);
  392. EXPORT_SYMBOL(iowrite16be);
  393. EXPORT_SYMBOL(iowrite32);
  394. EXPORT_SYMBOL(iowrite32be);
  395. EXPORT_SYMBOL(ioread8_rep);
  396. EXPORT_SYMBOL(ioread16_rep);
  397. EXPORT_SYMBOL(ioread32_rep);
  398. EXPORT_SYMBOL(iowrite8_rep);
  399. EXPORT_SYMBOL(iowrite16_rep);
  400. EXPORT_SYMBOL(iowrite32_rep);
  401. EXPORT_SYMBOL(ioport_map);
  402. EXPORT_SYMBOL(ioport_unmap);
  403. EXPORT_SYMBOL(pci_iounmap);