io.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. /*
  2. * linux/arch/m32r/platforms/mappi2/io.c
  3. *
  4. * Typical I/O routines for Mappi2 board.
  5. *
  6. * Copyright (c) 2001-2005 Hiroyuki Kondo, Hirokazu Takata,
  7. * Hitoshi Yamamoto, Mamoru Sakugawa
  8. */
  9. #include <asm/m32r.h>
  10. #include <asm/page.h>
  11. #include <asm/io.h>
  12. #include <asm/byteorder.h>
  13. #if defined(CONFIG_PCMCIA) && defined(CONFIG_M32R_CFC)
  14. #include <linux/types.h>
  15. #define M32R_PCC_IOMAP_SIZE 0x1000
  16. #define M32R_PCC_IOSTART0 0x1000
  17. #define M32R_PCC_IOEND0 (M32R_PCC_IOSTART0 + M32R_PCC_IOMAP_SIZE - 1)
  18. extern void pcc_ioread_byte(int, unsigned long, void *, size_t, size_t, int);
  19. extern void pcc_ioread_word(int, unsigned long, void *, size_t, size_t, int);
  20. extern void pcc_iowrite_byte(int, unsigned long, void *, size_t, size_t, int);
  21. extern void pcc_iowrite_word(int, unsigned long, void *, size_t, size_t, int);
  22. #endif /* CONFIG_PCMCIA && CONFIG_M32R_CFC */
  23. #define PORT2ADDR(port) _port2addr(port)
  24. #define PORT2ADDR_NE(port) _port2addr_ne(port)
  25. #define PORT2ADDR_USB(port) _port2addr_usb(port)
  26. static inline void *_port2addr(unsigned long port)
  27. {
  28. return (void *)(port | NONCACHE_OFFSET);
  29. }
  30. #if defined(CONFIG_IDE) && !defined(CONFIG_M32R_CFC)
  31. static inline void *__port2addr_ata(unsigned long port)
  32. {
  33. static int dummy_reg;
  34. switch (port) {
  35. case 0x1f0: return (void *)(0x0c002000 | NONCACHE_OFFSET);
  36. case 0x1f1: return (void *)(0x0c012800 | NONCACHE_OFFSET);
  37. case 0x1f2: return (void *)(0x0c012002 | NONCACHE_OFFSET);
  38. case 0x1f3: return (void *)(0x0c012802 | NONCACHE_OFFSET);
  39. case 0x1f4: return (void *)(0x0c012004 | NONCACHE_OFFSET);
  40. case 0x1f5: return (void *)(0x0c012804 | NONCACHE_OFFSET);
  41. case 0x1f6: return (void *)(0x0c012006 | NONCACHE_OFFSET);
  42. case 0x1f7: return (void *)(0x0c012806 | NONCACHE_OFFSET);
  43. case 0x3f6: return (void *)(0x0c01200e | NONCACHE_OFFSET);
  44. default: return (void *)&dummy_reg;
  45. }
  46. }
  47. #endif
  48. #define LAN_IOSTART (0x300 | NONCACHE_OFFSET)
  49. #define LAN_IOEND (0x320 | NONCACHE_OFFSET)
  50. #ifdef CONFIG_CHIP_OPSP
  51. static inline void *_port2addr_ne(unsigned long port)
  52. {
  53. return (void *)(port + 0x10000000);
  54. }
  55. #else
  56. static inline void *_port2addr_ne(unsigned long port)
  57. {
  58. return (void *)(port + 0x04000000);
  59. }
  60. #endif
  61. static inline void *_port2addr_usb(unsigned long port)
  62. {
  63. return (void *)(port + NONCACHE_OFFSET + 0x14000000);
  64. }
  65. static inline void delay(void)
  66. {
  67. __asm__ __volatile__ ("push r0; \n\t pop r0;" : : :"memory");
  68. }
  69. /*
  70. * NIC I/O function
  71. */
  72. static inline unsigned char _ne_inb(void *portp)
  73. {
  74. return (unsigned char) *(volatile unsigned char *)portp;
  75. }
  76. static inline unsigned short _ne_inw(void *portp)
  77. {
  78. return (unsigned short)le16_to_cpu(*(volatile unsigned short *)portp);
  79. }
  80. static inline void _ne_insb(void *portp, void * addr, unsigned long count)
  81. {
  82. unsigned char *buf = addr;
  83. while (count--)
  84. *buf++ = *(volatile unsigned char *)portp;
  85. }
  86. static inline void _ne_outb(unsigned char b, void *portp)
  87. {
  88. *(volatile unsigned char *)portp = (unsigned char)b;
  89. }
  90. static inline void _ne_outw(unsigned short w, void *portp)
  91. {
  92. *(volatile unsigned short *)portp = cpu_to_le16(w);
  93. }
  94. unsigned char _inb(unsigned long port)
  95. {
  96. if (port >= LAN_IOSTART && port < LAN_IOEND)
  97. return _ne_inb(PORT2ADDR_NE(port));
  98. #if defined(CONFIG_IDE) && !defined(CONFIG_M32R_CFC)
  99. else if ((port >= 0x1f0 && port <=0x1f7) || port == 0x3f6) {
  100. return *(volatile unsigned char *)__port2addr_ata(port);
  101. }
  102. #endif
  103. #if defined(CONFIG_PCMCIA) && defined(CONFIG_M32R_CFC)
  104. else if (port >= M32R_PCC_IOSTART0 && port <= M32R_PCC_IOEND0) {
  105. unsigned char b;
  106. pcc_ioread_byte(0, port, &b, sizeof(b), 1, 0);
  107. return b;
  108. } else
  109. #endif
  110. return *(volatile unsigned char *)PORT2ADDR(port);
  111. }
  112. unsigned short _inw(unsigned long port)
  113. {
  114. if (port >= LAN_IOSTART && port < LAN_IOEND)
  115. return _ne_inw(PORT2ADDR_NE(port));
  116. #if defined(CONFIG_IDE) && !defined(CONFIG_M32R_CFC)
  117. else if ((port >= 0x1f0 && port <=0x1f7) || port == 0x3f6) {
  118. return *(volatile unsigned short *)__port2addr_ata(port);
  119. }
  120. #endif
  121. #if defined(CONFIG_USB)
  122. else if (port >= 0x340 && port < 0x3a0)
  123. return *(volatile unsigned short *)PORT2ADDR_USB(port);
  124. #endif
  125. #if defined(CONFIG_PCMCIA) && defined(CONFIG_M32R_CFC)
  126. else if (port >= M32R_PCC_IOSTART0 && port <= M32R_PCC_IOEND0) {
  127. unsigned short w;
  128. pcc_ioread_word(0, port, &w, sizeof(w), 1, 0);
  129. return w;
  130. } else
  131. #endif
  132. return *(volatile unsigned short *)PORT2ADDR(port);
  133. }
  134. unsigned long _inl(unsigned long port)
  135. {
  136. #if defined(CONFIG_PCMCIA) && defined(CONFIG_M32R_CFC)
  137. if (port >= M32R_PCC_IOSTART0 && port <= M32R_PCC_IOEND0) {
  138. unsigned long l;
  139. pcc_ioread_word(0, port, &l, sizeof(l), 1, 0);
  140. return l;
  141. } else
  142. #endif
  143. return *(volatile unsigned long *)PORT2ADDR(port);
  144. }
  145. unsigned char _inb_p(unsigned long port)
  146. {
  147. unsigned char v = _inb(port);
  148. delay();
  149. return (v);
  150. }
  151. unsigned short _inw_p(unsigned long port)
  152. {
  153. unsigned short v = _inw(port);
  154. delay();
  155. return (v);
  156. }
  157. unsigned long _inl_p(unsigned long port)
  158. {
  159. unsigned long v = _inl(port);
  160. delay();
  161. return (v);
  162. }
  163. void _outb(unsigned char b, unsigned long port)
  164. {
  165. if (port >= LAN_IOSTART && port < LAN_IOEND)
  166. _ne_outb(b, PORT2ADDR_NE(port));
  167. else
  168. #if defined(CONFIG_IDE) && !defined(CONFIG_M32R_CFC)
  169. if ((port >= 0x1f0 && port <=0x1f7) || port == 0x3f6) {
  170. *(volatile unsigned char *)__port2addr_ata(port) = b;
  171. } else
  172. #endif
  173. #if defined(CONFIG_PCMCIA) && defined(CONFIG_M32R_CFC)
  174. if (port >= M32R_PCC_IOSTART0 && port <= M32R_PCC_IOEND0) {
  175. pcc_iowrite_byte(0, port, &b, sizeof(b), 1, 0);
  176. } else
  177. #endif
  178. *(volatile unsigned char *)PORT2ADDR(port) = b;
  179. }
  180. void _outw(unsigned short w, unsigned long port)
  181. {
  182. if (port >= LAN_IOSTART && port < LAN_IOEND)
  183. _ne_outw(w, PORT2ADDR_NE(port));
  184. else
  185. #if defined(CONFIG_IDE) && !defined(CONFIG_M32R_CFC)
  186. if ((port >= 0x1f0 && port <=0x1f7) || port == 0x3f6) {
  187. *(volatile unsigned short *)__port2addr_ata(port) = w;
  188. } else
  189. #endif
  190. #if defined(CONFIG_USB)
  191. if (port >= 0x340 && port < 0x3a0)
  192. *(volatile unsigned short *)PORT2ADDR_USB(port) = w;
  193. else
  194. #endif
  195. #if defined(CONFIG_PCMCIA) && defined(CONFIG_M32R_CFC)
  196. if (port >= M32R_PCC_IOSTART0 && port <= M32R_PCC_IOEND0) {
  197. pcc_iowrite_word(0, port, &w, sizeof(w), 1, 0);
  198. } else
  199. #endif
  200. *(volatile unsigned short *)PORT2ADDR(port) = w;
  201. }
  202. void _outl(unsigned long l, unsigned long port)
  203. {
  204. #if defined(CONFIG_PCMCIA) && defined(CONFIG_M32R_CFC)
  205. if (port >= M32R_PCC_IOSTART0 && port <= M32R_PCC_IOEND0) {
  206. pcc_iowrite_word(0, port, &l, sizeof(l), 1, 0);
  207. } else
  208. #endif
  209. *(volatile unsigned long *)PORT2ADDR(port) = l;
  210. }
  211. void _outb_p(unsigned char b, unsigned long port)
  212. {
  213. _outb(b, port);
  214. delay();
  215. }
  216. void _outw_p(unsigned short w, unsigned long port)
  217. {
  218. _outw(w, port);
  219. delay();
  220. }
  221. void _outl_p(unsigned long l, unsigned long port)
  222. {
  223. _outl(l, port);
  224. delay();
  225. }
  226. void _insb(unsigned int port, void * addr, unsigned long count)
  227. {
  228. if (port >= LAN_IOSTART && port < LAN_IOEND)
  229. _ne_insb(PORT2ADDR_NE(port), addr, count);
  230. #if defined(CONFIG_IDE) && !defined(CONFIG_M32R_CFC)
  231. else if ((port >= 0x1f0 && port <=0x1f7) || port == 0x3f6) {
  232. unsigned char *buf = addr;
  233. unsigned char *portp = __port2addr_ata(port);
  234. while (count--)
  235. *buf++ = *(volatile unsigned char *)portp;
  236. }
  237. #endif
  238. #if defined(CONFIG_PCMCIA) && defined(CONFIG_M32R_CFC)
  239. else if (port >= M32R_PCC_IOSTART0 && port <= M32R_PCC_IOEND0) {
  240. pcc_ioread_byte(0, port, (void *)addr, sizeof(unsigned char),
  241. count, 1);
  242. }
  243. #endif
  244. else {
  245. unsigned char *buf = addr;
  246. unsigned char *portp = PORT2ADDR(port);
  247. while (count--)
  248. *buf++ = *(volatile unsigned char *)portp;
  249. }
  250. }
  251. void _insw(unsigned int port, void * addr, unsigned long count)
  252. {
  253. unsigned short *buf = addr;
  254. unsigned short *portp;
  255. if (port >= LAN_IOSTART && port < LAN_IOEND) {
  256. portp = PORT2ADDR_NE(port);
  257. while (count--)
  258. *buf++ = *(volatile unsigned short *)portp;
  259. #if defined(CONFIG_PCMCIA) && defined(CONFIG_M32R_CFC)
  260. } else if (port >= M32R_PCC_IOSTART0 && port <= M32R_PCC_IOEND0) {
  261. pcc_ioread_word(9, port, (void *)addr, sizeof(unsigned short),
  262. count, 1);
  263. #endif
  264. #if defined(CONFIG_IDE) && !defined(CONFIG_M32R_CFC)
  265. } else if ((port >= 0x1f0 && port <=0x1f7) || port == 0x3f6) {
  266. portp = __port2addr_ata(port);
  267. while (count--)
  268. *buf++ = *(volatile unsigned short *)portp;
  269. #endif
  270. } else {
  271. portp = PORT2ADDR(port);
  272. while (count--)
  273. *buf++ = *(volatile unsigned short *)portp;
  274. }
  275. }
  276. void _insl(unsigned int port, void * addr, unsigned long count)
  277. {
  278. unsigned long *buf = addr;
  279. unsigned long *portp;
  280. portp = PORT2ADDR(port);
  281. while (count--)
  282. *buf++ = *(volatile unsigned long *)portp;
  283. }
  284. void _outsb(unsigned int port, const void * addr, unsigned long count)
  285. {
  286. const unsigned char *buf = addr;
  287. unsigned char *portp;
  288. if (port >= LAN_IOSTART && port < LAN_IOEND) {
  289. portp = PORT2ADDR_NE(port);
  290. while (count--)
  291. _ne_outb(*buf++, portp);
  292. #if defined(CONFIG_IDE) && !defined(CONFIG_M32R_CFC)
  293. } else if ((port >= 0x1f0 && port <=0x1f7) || port == 0x3f6) {
  294. portp = __port2addr_ata(port);
  295. while (count--)
  296. *(volatile unsigned char *)portp = *buf++;
  297. #endif
  298. #if defined(CONFIG_PCMCIA) && defined(CONFIG_M32R_CFC)
  299. } else if (port >= M32R_PCC_IOSTART0 && port <= M32R_PCC_IOEND0) {
  300. pcc_iowrite_byte(0, port, (void *)addr, sizeof(unsigned char),
  301. count, 1);
  302. #endif
  303. } else {
  304. portp = PORT2ADDR(port);
  305. while (count--)
  306. *(volatile unsigned char *)portp = *buf++;
  307. }
  308. }
  309. void _outsw(unsigned int port, const void * addr, unsigned long count)
  310. {
  311. const unsigned short *buf = addr;
  312. unsigned short *portp;
  313. if (port >= LAN_IOSTART && port < LAN_IOEND) {
  314. portp = PORT2ADDR_NE(port);
  315. while (count--)
  316. *(volatile unsigned short *)portp = *buf++;
  317. #if defined(CONFIG_IDE) && !defined(CONFIG_M32R_CFC)
  318. } else if ((port >= 0x1f0 && port <=0x1f7) || port == 0x3f6) {
  319. portp = __port2addr_ata(port);
  320. while (count--)
  321. *(volatile unsigned short *)portp = *buf++;
  322. #endif
  323. #if defined(CONFIG_PCMCIA) && defined(CONFIG_M32R_CFC)
  324. } else if (port >= M32R_PCC_IOSTART0 && port <= M32R_PCC_IOEND0) {
  325. pcc_iowrite_word(9, port, (void *)addr, sizeof(unsigned short),
  326. count, 1);
  327. #endif
  328. } else {
  329. portp = PORT2ADDR(port);
  330. while (count--)
  331. *(volatile unsigned short *)portp = *buf++;
  332. }
  333. }
  334. void _outsl(unsigned int port, const void * addr, unsigned long count)
  335. {
  336. const unsigned long *buf = addr;
  337. unsigned char *portp;
  338. portp = PORT2ADDR(port);
  339. while (count--)
  340. *(volatile unsigned long *)portp = *buf++;
  341. }