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
6 * Derived from io_hd64461.c, which bore the message:
7 * Copyright (C) 2000 YAEGASHI Takeshi
9 * Typical I/O routines for HD64465 system.
12 #include <linux/config.h>
13 #include <linux/kernel.h>
14 #include <linux/module.h>
16 #include <asm/hd64465/hd64465.h>
19 #define HD64465_DEBUG 0
22 #define DPRINTK(args...) printk(args)
23 #define DIPRINTK(n, args...) if (hd64465_io_debug>(n)) printk(args)
25 #define DPRINTK(args...)
26 #define DIPRINTK(n, args...)
31 /* This is a hack suitable only for debugging IO port problems */
33 EXPORT_SYMBOL(hd64465_io_debug);
35 /* Low iomap maps port 0-1K to addresses in 8byte chunks */
36 #define HD64465_IOMAP_LO_THRESH 0x400
37 #define HD64465_IOMAP_LO_SHIFT 3
38 #define HD64465_IOMAP_LO_MASK ((1<<HD64465_IOMAP_LO_SHIFT)-1)
39 #define HD64465_IOMAP_LO_NMAP (HD64465_IOMAP_LO_THRESH>>HD64465_IOMAP_LO_SHIFT)
40 static unsigned long hd64465_iomap_lo[HD64465_IOMAP_LO_NMAP];
41 static unsigned char hd64465_iomap_lo_shift[HD64465_IOMAP_LO_NMAP];
43 /* High iomap maps port 1K-64K to addresses in 1K chunks */
44 #define HD64465_IOMAP_HI_THRESH 0x10000
45 #define HD64465_IOMAP_HI_SHIFT 10
46 #define HD64465_IOMAP_HI_MASK ((1<<HD64465_IOMAP_HI_SHIFT)-1)
47 #define HD64465_IOMAP_HI_NMAP (HD64465_IOMAP_HI_THRESH>>HD64465_IOMAP_HI_SHIFT)
48 static unsigned long hd64465_iomap_hi[HD64465_IOMAP_HI_NMAP];
49 static unsigned char hd64465_iomap_hi_shift[HD64465_IOMAP_HI_NMAP];
51 #define PORT2ADDR(x) (sh_mv.mv_isa_port2addr(x))
53 void hd64465_port_map(unsigned short baseport, unsigned int nports,
54 unsigned long addr, unsigned char shift)
56 unsigned int port, endport = baseport + nports;
58 DPRINTK("hd64465_port_map(base=0x%04hx, n=0x%04hx, addr=0x%08lx,endport=0x%04x)\n",
59 baseport, nports, addr,endport);
61 for (port = baseport ;
62 port < endport && port < HD64465_IOMAP_LO_THRESH ;
63 port += (1<<HD64465_IOMAP_LO_SHIFT)) {
64 DPRINTK(" maplo[0x%x] = 0x%08lx\n", port, addr);
65 hd64465_iomap_lo[port>>HD64465_IOMAP_LO_SHIFT] = addr;
66 hd64465_iomap_lo_shift[port>>HD64465_IOMAP_LO_SHIFT] = shift;
67 addr += (1<<(HD64465_IOMAP_LO_SHIFT));
70 for (port = max_t(unsigned int, baseport, HD64465_IOMAP_LO_THRESH);
71 port < endport && port < HD64465_IOMAP_HI_THRESH ;
72 port += (1<<HD64465_IOMAP_HI_SHIFT)) {
73 DPRINTK(" maphi[0x%x] = 0x%08lx\n", port, addr);
74 hd64465_iomap_hi[port>>HD64465_IOMAP_HI_SHIFT] = addr;
75 hd64465_iomap_hi_shift[port>>HD64465_IOMAP_HI_SHIFT] = shift;
76 addr += (1<<(HD64465_IOMAP_HI_SHIFT));
79 EXPORT_SYMBOL(hd64465_port_map);
81 void hd64465_port_unmap(unsigned short baseport, unsigned int nports)
83 unsigned int port, endport = baseport + nports;
85 DPRINTK("hd64465_port_unmap(base=0x%04hx, n=0x%04hx)\n",
88 for (port = baseport ;
89 port < endport && port < HD64465_IOMAP_LO_THRESH ;
90 port += (1<<HD64465_IOMAP_LO_SHIFT)) {
91 hd64465_iomap_lo[port>>HD64465_IOMAP_LO_SHIFT] = 0;
94 for (port = max_t(unsigned int, baseport, HD64465_IOMAP_LO_THRESH);
95 port < endport && port < HD64465_IOMAP_HI_THRESH ;
96 port += (1<<HD64465_IOMAP_HI_SHIFT)) {
97 hd64465_iomap_hi[port>>HD64465_IOMAP_HI_SHIFT] = 0;
100 EXPORT_SYMBOL(hd64465_port_unmap);
102 unsigned long hd64465_isa_port2addr(unsigned long port)
104 unsigned long addr = 0;
107 /* handle remapping of low IO ports */
108 if (port < HD64465_IOMAP_LO_THRESH) {
109 addr = hd64465_iomap_lo[port >> HD64465_IOMAP_LO_SHIFT];
110 shift = hd64465_iomap_lo_shift[port >> HD64465_IOMAP_LO_SHIFT];
112 addr += (port & HD64465_IOMAP_LO_MASK) << shift;
114 printk(KERN_NOTICE "io_hd64465: access to un-mapped port %lx\n", port);
115 } else if (port < HD64465_IOMAP_HI_THRESH) {
116 addr = hd64465_iomap_hi[port >> HD64465_IOMAP_HI_SHIFT];
117 shift = hd64465_iomap_hi_shift[port >> HD64465_IOMAP_HI_SHIFT];
119 addr += (port & HD64465_IOMAP_HI_MASK) << shift;
121 printk(KERN_NOTICE "io_hd64465: access to un-mapped port %lx\n", port);
124 /* HD64465 internal devices (0xb0000000) */
125 else if (port < 0x20000)
126 addr = CONFIG_HD64465_IOBASE + port - 0x10000;
128 /* Whole physical address space (0xa0000000) */
130 addr = P2SEGADDR(port);
132 DIPRINTK(2, "PORT2ADDR(0x%08lx) = 0x%08lx\n", port, addr);
137 static inline void delay(void)
139 ctrl_inw(0xa0000000);
142 unsigned char hd64465_inb(unsigned long port)
144 unsigned long addr = PORT2ADDR(port);
145 unsigned long b = (addr == 0 ? 0 : *(volatile unsigned char*)addr);
147 DIPRINTK(0, "inb(%08lx) = %02x\n", addr, (unsigned)b);
151 unsigned char hd64465_inb_p(unsigned long port)
154 unsigned long addr = PORT2ADDR(port);
156 v = (addr == 0 ? 0 : *(volatile unsigned char*)addr);
158 DIPRINTK(0, "inb_p(%08lx) = %02x\n", addr, (unsigned)v);
162 unsigned short hd64465_inw(unsigned long port)
164 unsigned long addr = PORT2ADDR(port);
165 unsigned long b = (addr == 0 ? 0 : *(volatile unsigned short*)addr);
166 DIPRINTK(0, "inw(%08lx) = %04lx\n", addr, b);
170 unsigned int hd64465_inl(unsigned long port)
172 unsigned long addr = PORT2ADDR(port);
173 unsigned int b = (addr == 0 ? 0 : *(volatile unsigned long*)addr);
174 DIPRINTK(0, "inl(%08lx) = %08x\n", addr, b);
178 void hd64465_outb(unsigned char b, unsigned long port)
180 unsigned long addr = PORT2ADDR(port);
182 DIPRINTK(0, "outb(%02x, %08lx)\n", (unsigned)b, addr);
184 *(volatile unsigned char*)addr = b;
187 void hd64465_outb_p(unsigned char b, unsigned long port)
189 unsigned long addr = PORT2ADDR(port);
191 DIPRINTK(0, "outb_p(%02x, %08lx)\n", (unsigned)b, addr);
193 *(volatile unsigned char*)addr = b;
197 void hd64465_outw(unsigned short b, unsigned long port)
199 unsigned long addr = PORT2ADDR(port);
200 DIPRINTK(0, "outw(%04x, %08lx)\n", (unsigned)b, addr);
202 *(volatile unsigned short*)addr = b;
205 void hd64465_outl(unsigned int b, unsigned long port)
207 unsigned long addr = PORT2ADDR(port);
208 DIPRINTK(0, "outl(%08x, %08lx)\n", b, addr);
210 *(volatile unsigned long*)addr = b;