io.c 6.2 KB

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