io.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. /*
  2. * $Id: io.c,v 1.4 2003/08/03 03:05:10 lethal Exp $
  3. * by Greg Banks <gbanks@pocketpenguins.com>
  4. * (c) 2000 PocketPenguins Inc
  5. *
  6. * Derived from io_hd64461.c, which bore the message:
  7. * Copyright (C) 2000 YAEGASHI Takeshi
  8. *
  9. * Typical I/O routines for HD64465 system.
  10. */
  11. #include <linux/kernel.h>
  12. #include <linux/module.h>
  13. #include <asm/io.h>
  14. #include <asm/hd64465/hd64465.h>
  15. #define HD64465_DEBUG 0
  16. #if HD64465_DEBUG
  17. #define DPRINTK(args...) printk(args)
  18. #define DIPRINTK(n, args...) if (hd64465_io_debug>(n)) printk(args)
  19. #else
  20. #define DPRINTK(args...)
  21. #define DIPRINTK(n, args...)
  22. #endif
  23. /* This is a hack suitable only for debugging IO port problems */
  24. int hd64465_io_debug;
  25. EXPORT_SYMBOL(hd64465_io_debug);
  26. /* Low iomap maps port 0-1K to addresses in 8byte chunks */
  27. #define HD64465_IOMAP_LO_THRESH 0x400
  28. #define HD64465_IOMAP_LO_SHIFT 3
  29. #define HD64465_IOMAP_LO_MASK ((1<<HD64465_IOMAP_LO_SHIFT)-1)
  30. #define HD64465_IOMAP_LO_NMAP (HD64465_IOMAP_LO_THRESH>>HD64465_IOMAP_LO_SHIFT)
  31. static unsigned long hd64465_iomap_lo[HD64465_IOMAP_LO_NMAP];
  32. static unsigned char hd64465_iomap_lo_shift[HD64465_IOMAP_LO_NMAP];
  33. /* High iomap maps port 1K-64K to addresses in 1K chunks */
  34. #define HD64465_IOMAP_HI_THRESH 0x10000
  35. #define HD64465_IOMAP_HI_SHIFT 10
  36. #define HD64465_IOMAP_HI_MASK ((1<<HD64465_IOMAP_HI_SHIFT)-1)
  37. #define HD64465_IOMAP_HI_NMAP (HD64465_IOMAP_HI_THRESH>>HD64465_IOMAP_HI_SHIFT)
  38. static unsigned long hd64465_iomap_hi[HD64465_IOMAP_HI_NMAP];
  39. static unsigned char hd64465_iomap_hi_shift[HD64465_IOMAP_HI_NMAP];
  40. #define PORT2ADDR(x) (sh_mv.mv_isa_port2addr(x))
  41. void hd64465_port_map(unsigned short baseport, unsigned int nports,
  42. unsigned long addr, unsigned char shift)
  43. {
  44. unsigned int port, endport = baseport + nports;
  45. DPRINTK("hd64465_port_map(base=0x%04hx, n=0x%04hx, addr=0x%08lx,endport=0x%04x)\n",
  46. baseport, nports, addr,endport);
  47. for (port = baseport ;
  48. port < endport && port < HD64465_IOMAP_LO_THRESH ;
  49. port += (1<<HD64465_IOMAP_LO_SHIFT)) {
  50. DPRINTK(" maplo[0x%x] = 0x%08lx\n", port, addr);
  51. hd64465_iomap_lo[port>>HD64465_IOMAP_LO_SHIFT] = addr;
  52. hd64465_iomap_lo_shift[port>>HD64465_IOMAP_LO_SHIFT] = shift;
  53. addr += (1<<(HD64465_IOMAP_LO_SHIFT));
  54. }
  55. for (port = max_t(unsigned int, baseport, HD64465_IOMAP_LO_THRESH);
  56. port < endport && port < HD64465_IOMAP_HI_THRESH ;
  57. port += (1<<HD64465_IOMAP_HI_SHIFT)) {
  58. DPRINTK(" maphi[0x%x] = 0x%08lx\n", port, addr);
  59. hd64465_iomap_hi[port>>HD64465_IOMAP_HI_SHIFT] = addr;
  60. hd64465_iomap_hi_shift[port>>HD64465_IOMAP_HI_SHIFT] = shift;
  61. addr += (1<<(HD64465_IOMAP_HI_SHIFT));
  62. }
  63. }
  64. EXPORT_SYMBOL(hd64465_port_map);
  65. void hd64465_port_unmap(unsigned short baseport, unsigned int nports)
  66. {
  67. unsigned int port, endport = baseport + nports;
  68. DPRINTK("hd64465_port_unmap(base=0x%04hx, n=0x%04hx)\n",
  69. baseport, nports);
  70. for (port = baseport ;
  71. port < endport && port < HD64465_IOMAP_LO_THRESH ;
  72. port += (1<<HD64465_IOMAP_LO_SHIFT)) {
  73. hd64465_iomap_lo[port>>HD64465_IOMAP_LO_SHIFT] = 0;
  74. }
  75. for (port = max_t(unsigned int, baseport, HD64465_IOMAP_LO_THRESH);
  76. port < endport && port < HD64465_IOMAP_HI_THRESH ;
  77. port += (1<<HD64465_IOMAP_HI_SHIFT)) {
  78. hd64465_iomap_hi[port>>HD64465_IOMAP_HI_SHIFT] = 0;
  79. }
  80. }
  81. EXPORT_SYMBOL(hd64465_port_unmap);
  82. unsigned long hd64465_isa_port2addr(unsigned long port)
  83. {
  84. unsigned long addr = 0;
  85. unsigned char shift;
  86. /* handle remapping of low IO ports */
  87. if (port < HD64465_IOMAP_LO_THRESH) {
  88. addr = hd64465_iomap_lo[port >> HD64465_IOMAP_LO_SHIFT];
  89. shift = hd64465_iomap_lo_shift[port >> HD64465_IOMAP_LO_SHIFT];
  90. if (addr != 0)
  91. addr += (port & HD64465_IOMAP_LO_MASK) << shift;
  92. else
  93. printk(KERN_NOTICE "io_hd64465: access to un-mapped port %lx\n", port);
  94. } else if (port < HD64465_IOMAP_HI_THRESH) {
  95. addr = hd64465_iomap_hi[port >> HD64465_IOMAP_HI_SHIFT];
  96. shift = hd64465_iomap_hi_shift[port >> HD64465_IOMAP_HI_SHIFT];
  97. if (addr != 0)
  98. addr += (port & HD64465_IOMAP_HI_MASK) << shift;
  99. else
  100. printk(KERN_NOTICE "io_hd64465: access to un-mapped port %lx\n", port);
  101. }
  102. /* HD64465 internal devices (0xb0000000) */
  103. else if (port < 0x20000)
  104. addr = CONFIG_HD64465_IOBASE + port - 0x10000;
  105. /* Whole physical address space (0xa0000000) */
  106. else
  107. addr = P2SEGADDR(port);
  108. DIPRINTK(2, "PORT2ADDR(0x%08lx) = 0x%08lx\n", port, addr);
  109. return addr;
  110. }
  111. static inline void delay(void)
  112. {
  113. ctrl_inw(0xa0000000);
  114. }
  115. unsigned char hd64465_inb(unsigned long port)
  116. {
  117. unsigned long addr = PORT2ADDR(port);
  118. unsigned long b = (addr == 0 ? 0 : *(volatile unsigned char*)addr);
  119. DIPRINTK(0, "inb(%08lx) = %02x\n", addr, (unsigned)b);
  120. return b;
  121. }
  122. unsigned char hd64465_inb_p(unsigned long port)
  123. {
  124. unsigned long v;
  125. unsigned long addr = PORT2ADDR(port);
  126. v = (addr == 0 ? 0 : *(volatile unsigned char*)addr);
  127. delay();
  128. DIPRINTK(0, "inb_p(%08lx) = %02x\n", addr, (unsigned)v);
  129. return v;
  130. }
  131. unsigned short hd64465_inw(unsigned long port)
  132. {
  133. unsigned long addr = PORT2ADDR(port);
  134. unsigned long b = (addr == 0 ? 0 : *(volatile unsigned short*)addr);
  135. DIPRINTK(0, "inw(%08lx) = %04lx\n", addr, b);
  136. return b;
  137. }
  138. unsigned int hd64465_inl(unsigned long port)
  139. {
  140. unsigned long addr = PORT2ADDR(port);
  141. unsigned int b = (addr == 0 ? 0 : *(volatile unsigned long*)addr);
  142. DIPRINTK(0, "inl(%08lx) = %08x\n", addr, b);
  143. return b;
  144. }
  145. void hd64465_outb(unsigned char b, unsigned long port)
  146. {
  147. unsigned long addr = PORT2ADDR(port);
  148. DIPRINTK(0, "outb(%02x, %08lx)\n", (unsigned)b, addr);
  149. if (addr != 0)
  150. *(volatile unsigned char*)addr = b;
  151. }
  152. void hd64465_outb_p(unsigned char b, unsigned long port)
  153. {
  154. unsigned long addr = PORT2ADDR(port);
  155. DIPRINTK(0, "outb_p(%02x, %08lx)\n", (unsigned)b, addr);
  156. if (addr != 0)
  157. *(volatile unsigned char*)addr = b;
  158. delay();
  159. }
  160. void hd64465_outw(unsigned short b, unsigned long port)
  161. {
  162. unsigned long addr = PORT2ADDR(port);
  163. DIPRINTK(0, "outw(%04x, %08lx)\n", (unsigned)b, addr);
  164. if (addr != 0)
  165. *(volatile unsigned short*)addr = b;
  166. }
  167. void hd64465_outl(unsigned int b, unsigned long port)
  168. {
  169. unsigned long addr = PORT2ADDR(port);
  170. DIPRINTK(0, "outl(%08x, %08lx)\n", b, addr);
  171. if (addr != 0)
  172. *(volatile unsigned long*)addr = b;
  173. }