io.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  1. /*
  2. * linux/arch/m32r/platforms/mappi3/io.c
  3. *
  4. * Typical I/O routines for Mappi3 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)
  31. static inline void *__port2addr_ata(unsigned long port)
  32. {
  33. static int dummy_reg;
  34. switch (port) {
  35. /* IDE0 CF */
  36. case 0x1f0: return (void *)(0x14002000 | NONCACHE_OFFSET);
  37. case 0x1f1: return (void *)(0x14012800 | NONCACHE_OFFSET);
  38. case 0x1f2: return (void *)(0x14012002 | NONCACHE_OFFSET);
  39. case 0x1f3: return (void *)(0x14012802 | NONCACHE_OFFSET);
  40. case 0x1f4: return (void *)(0x14012004 | NONCACHE_OFFSET);
  41. case 0x1f5: return (void *)(0x14012804 | NONCACHE_OFFSET);
  42. case 0x1f6: return (void *)(0x14012006 | NONCACHE_OFFSET);
  43. case 0x1f7: return (void *)(0x14012806 | NONCACHE_OFFSET);
  44. case 0x3f6: return (void *)(0x1401200e | NONCACHE_OFFSET);
  45. /* IDE1 IDE */
  46. case 0x170: /* Data 16bit */
  47. return (void *)(0x14810000 | NONCACHE_OFFSET);
  48. case 0x171: /* Features / Error */
  49. return (void *)(0x14810002 | NONCACHE_OFFSET);
  50. case 0x172: /* Sector count */
  51. return (void *)(0x14810004 | NONCACHE_OFFSET);
  52. case 0x173: /* Sector number */
  53. return (void *)(0x14810006 | NONCACHE_OFFSET);
  54. case 0x174: /* Cylinder low */
  55. return (void *)(0x14810008 | NONCACHE_OFFSET);
  56. case 0x175: /* Cylinder high */
  57. return (void *)(0x1481000a | NONCACHE_OFFSET);
  58. case 0x176: /* Device head */
  59. return (void *)(0x1481000c | NONCACHE_OFFSET);
  60. case 0x177: /* Command */
  61. return (void *)(0x1481000e | NONCACHE_OFFSET);
  62. case 0x376: /* Device control / Alt status */
  63. return (void *)(0x1480800c | NONCACHE_OFFSET);
  64. default: return (void *)&dummy_reg;
  65. }
  66. }
  67. #endif
  68. #define LAN_IOSTART (0x300 | NONCACHE_OFFSET)
  69. #define LAN_IOEND (0x320 | NONCACHE_OFFSET)
  70. static inline void *_port2addr_ne(unsigned long port)
  71. {
  72. return (void *)(port + 0x10000000);
  73. }
  74. static inline void *_port2addr_usb(unsigned long port)
  75. {
  76. return (void *)(port + NONCACHE_OFFSET + 0x12000000);
  77. }
  78. static inline void delay(void)
  79. {
  80. __asm__ __volatile__ ("push r0; \n\t pop r0;" : : :"memory");
  81. }
  82. /*
  83. * NIC I/O function
  84. */
  85. static inline unsigned char _ne_inb(void *portp)
  86. {
  87. return (unsigned char) *(volatile unsigned char *)portp;
  88. }
  89. static inline unsigned short _ne_inw(void *portp)
  90. {
  91. return (unsigned short)le16_to_cpu(*(volatile unsigned short *)portp);
  92. }
  93. static inline void _ne_insb(void *portp, void * addr, unsigned long count)
  94. {
  95. unsigned char *buf = addr;
  96. while (count--)
  97. *buf++ = *(volatile unsigned char *)portp;
  98. }
  99. static inline void _ne_outb(unsigned char b, void *portp)
  100. {
  101. *(volatile unsigned char *)portp = (unsigned char)b;
  102. }
  103. static inline void _ne_outw(unsigned short w, void *portp)
  104. {
  105. *(volatile unsigned short *)portp = cpu_to_le16(w);
  106. }
  107. unsigned char _inb(unsigned long port)
  108. {
  109. if (port >= LAN_IOSTART && port < LAN_IOEND)
  110. return _ne_inb(PORT2ADDR_NE(port));
  111. #if defined(CONFIG_IDE)
  112. else if ( ((port >= 0x170 && port <=0x177) || port == 0x376) ||
  113. ((port >= 0x1f0 && port <=0x1f7) || port == 0x3f6) ){
  114. return *(volatile unsigned char *)__port2addr_ata(port);
  115. }
  116. #endif
  117. #if defined(CONFIG_PCMCIA) && defined(CONFIG_M32R_CFC)
  118. else if (port >= M32R_PCC_IOSTART0 && port <= M32R_PCC_IOEND0) {
  119. unsigned char b;
  120. pcc_ioread_byte(0, port, &b, sizeof(b), 1, 0);
  121. return b;
  122. } else
  123. #endif
  124. return *(volatile unsigned char *)PORT2ADDR(port);
  125. }
  126. unsigned short _inw(unsigned long port)
  127. {
  128. if (port >= LAN_IOSTART && port < LAN_IOEND)
  129. return _ne_inw(PORT2ADDR_NE(port));
  130. #if defined(CONFIG_IDE)
  131. else if ( ((port >= 0x170 && port <=0x177) || port == 0x376) ||
  132. ((port >= 0x1f0 && port <=0x1f7) || port == 0x3f6) ){
  133. return *(volatile unsigned short *)__port2addr_ata(port);
  134. }
  135. #endif
  136. #if defined(CONFIG_USB)
  137. else if (port >= 0x340 && port < 0x3a0)
  138. return *(volatile unsigned short *)PORT2ADDR_USB(port);
  139. #endif
  140. #if defined(CONFIG_PCMCIA) && defined(CONFIG_M32R_CFC)
  141. else if (port >= M32R_PCC_IOSTART0 && port <= M32R_PCC_IOEND0) {
  142. unsigned short w;
  143. pcc_ioread_word(0, port, &w, sizeof(w), 1, 0);
  144. return w;
  145. } else
  146. #endif
  147. return *(volatile unsigned short *)PORT2ADDR(port);
  148. }
  149. unsigned long _inl(unsigned long port)
  150. {
  151. #if defined(CONFIG_PCMCIA) && defined(CONFIG_M32R_CFC)
  152. if (port >= M32R_PCC_IOSTART0 && port <= M32R_PCC_IOEND0) {
  153. unsigned long l;
  154. pcc_ioread_word(0, port, &l, sizeof(l), 1, 0);
  155. return l;
  156. } else
  157. #endif
  158. return *(volatile unsigned long *)PORT2ADDR(port);
  159. }
  160. unsigned char _inb_p(unsigned long port)
  161. {
  162. unsigned char v = _inb(port);
  163. delay();
  164. return (v);
  165. }
  166. unsigned short _inw_p(unsigned long port)
  167. {
  168. unsigned short v = _inw(port);
  169. delay();
  170. return (v);
  171. }
  172. unsigned long _inl_p(unsigned long port)
  173. {
  174. unsigned long v = _inl(port);
  175. delay();
  176. return (v);
  177. }
  178. void _outb(unsigned char b, unsigned long port)
  179. {
  180. if (port >= LAN_IOSTART && port < LAN_IOEND)
  181. _ne_outb(b, PORT2ADDR_NE(port));
  182. else
  183. #if defined(CONFIG_IDE)
  184. if ( ((port >= 0x170 && port <=0x177) || port == 0x376) ||
  185. ((port >= 0x1f0 && port <=0x1f7) || port == 0x3f6) ){
  186. *(volatile unsigned char *)__port2addr_ata(port) = b;
  187. } else
  188. #endif
  189. #if defined(CONFIG_PCMCIA) && defined(CONFIG_M32R_CFC)
  190. if (port >= M32R_PCC_IOSTART0 && port <= M32R_PCC_IOEND0) {
  191. pcc_iowrite_byte(0, port, &b, sizeof(b), 1, 0);
  192. } else
  193. #endif
  194. *(volatile unsigned char *)PORT2ADDR(port) = b;
  195. }
  196. void _outw(unsigned short w, unsigned long port)
  197. {
  198. if (port >= LAN_IOSTART && port < LAN_IOEND)
  199. _ne_outw(w, PORT2ADDR_NE(port));
  200. else
  201. #if defined(CONFIG_IDE)
  202. if ( ((port >= 0x170 && port <=0x177) || port == 0x376) ||
  203. ((port >= 0x1f0 && port <=0x1f7) || port == 0x3f6) ){
  204. *(volatile unsigned short *)__port2addr_ata(port) = w;
  205. } else
  206. #endif
  207. #if defined(CONFIG_USB)
  208. if (port >= 0x340 && port < 0x3a0)
  209. *(volatile unsigned short *)PORT2ADDR_USB(port) = w;
  210. else
  211. #endif
  212. #if defined(CONFIG_PCMCIA) && defined(CONFIG_M32R_CFC)
  213. if (port >= M32R_PCC_IOSTART0 && port <= M32R_PCC_IOEND0) {
  214. pcc_iowrite_word(0, port, &w, sizeof(w), 1, 0);
  215. } else
  216. #endif
  217. *(volatile unsigned short *)PORT2ADDR(port) = w;
  218. }
  219. void _outl(unsigned long l, unsigned long port)
  220. {
  221. #if defined(CONFIG_PCMCIA) && defined(CONFIG_M32R_CFC)
  222. if (port >= M32R_PCC_IOSTART0 && port <= M32R_PCC_IOEND0) {
  223. pcc_iowrite_word(0, port, &l, sizeof(l), 1, 0);
  224. } else
  225. #endif
  226. *(volatile unsigned long *)PORT2ADDR(port) = l;
  227. }
  228. void _outb_p(unsigned char b, unsigned long port)
  229. {
  230. _outb(b, port);
  231. delay();
  232. }
  233. void _outw_p(unsigned short w, unsigned long port)
  234. {
  235. _outw(w, port);
  236. delay();
  237. }
  238. void _outl_p(unsigned long l, unsigned long port)
  239. {
  240. _outl(l, port);
  241. delay();
  242. }
  243. void _insb(unsigned int port, void * addr, unsigned long count)
  244. {
  245. if (port >= LAN_IOSTART && port < LAN_IOEND)
  246. _ne_insb(PORT2ADDR_NE(port), addr, count);
  247. #if defined(CONFIG_IDE)
  248. else if ( ((port >= 0x170 && port <=0x177) || port == 0x376) ||
  249. ((port >= 0x1f0 && port <=0x1f7) || port == 0x3f6) ){
  250. unsigned char *buf = addr;
  251. unsigned char *portp = __port2addr_ata(port);
  252. while (count--)
  253. *buf++ = *(volatile unsigned char *)portp;
  254. }
  255. #endif
  256. #if defined(CONFIG_PCMCIA) && defined(CONFIG_M32R_CFC)
  257. else if (port >= M32R_PCC_IOSTART0 && port <= M32R_PCC_IOEND0) {
  258. pcc_ioread_byte(0, port, (void *)addr, sizeof(unsigned char),
  259. count, 1);
  260. }
  261. #endif
  262. else {
  263. unsigned char *buf = addr;
  264. unsigned char *portp = PORT2ADDR(port);
  265. while (count--)
  266. *buf++ = *(volatile unsigned char *)portp;
  267. }
  268. }
  269. void _insw(unsigned int port, void * addr, unsigned long count)
  270. {
  271. unsigned short *buf = addr;
  272. unsigned short *portp;
  273. if (port >= LAN_IOSTART && port < LAN_IOEND) {
  274. portp = PORT2ADDR_NE(port);
  275. while (count--)
  276. *buf++ = *(volatile unsigned short *)portp;
  277. #if defined(CONFIG_PCMCIA) && defined(CONFIG_M32R_CFC)
  278. } else if (port >= M32R_PCC_IOSTART0 && port <= M32R_PCC_IOEND0) {
  279. pcc_ioread_word(9, port, (void *)addr, sizeof(unsigned short),
  280. count, 1);
  281. #endif
  282. #if defined(CONFIG_IDE)
  283. } else if ( ((port >= 0x170 && port <=0x177) || port == 0x376) ||
  284. ((port >= 0x1f0 && port <=0x1f7) || port == 0x3f6) ){
  285. portp = __port2addr_ata(port);
  286. while (count--)
  287. *buf++ = *(volatile unsigned short *)portp;
  288. #endif
  289. } else {
  290. portp = PORT2ADDR(port);
  291. while (count--)
  292. *buf++ = *(volatile unsigned short *)portp;
  293. }
  294. }
  295. void _insl(unsigned int port, void * addr, unsigned long count)
  296. {
  297. unsigned long *buf = addr;
  298. unsigned long *portp;
  299. portp = PORT2ADDR(port);
  300. while (count--)
  301. *buf++ = *(volatile unsigned long *)portp;
  302. }
  303. void _outsb(unsigned int port, const void * addr, unsigned long count)
  304. {
  305. const unsigned char *buf = addr;
  306. unsigned char *portp;
  307. if (port >= LAN_IOSTART && port < LAN_IOEND) {
  308. portp = PORT2ADDR_NE(port);
  309. while (count--)
  310. _ne_outb(*buf++, portp);
  311. #if defined(CONFIG_IDE)
  312. } else if ( ((port >= 0x170 && port <=0x177) || port == 0x376) ||
  313. ((port >= 0x1f0 && port <=0x1f7) || port == 0x3f6) ){
  314. portp = __port2addr_ata(port);
  315. while (count--)
  316. *(volatile unsigned char *)portp = *buf++;
  317. #endif
  318. #if defined(CONFIG_PCMCIA) && defined(CONFIG_M32R_CFC)
  319. } else if (port >= M32R_PCC_IOSTART0 && port <= M32R_PCC_IOEND0) {
  320. pcc_iowrite_byte(0, port, (void *)addr, sizeof(unsigned char),
  321. count, 1);
  322. #endif
  323. } else {
  324. portp = PORT2ADDR(port);
  325. while (count--)
  326. *(volatile unsigned char *)portp = *buf++;
  327. }
  328. }
  329. void _outsw(unsigned int port, const void * addr, unsigned long count)
  330. {
  331. const unsigned short *buf = addr;
  332. unsigned short *portp;
  333. if (port >= LAN_IOSTART && port < LAN_IOEND) {
  334. portp = PORT2ADDR_NE(port);
  335. while (count--)
  336. *(volatile unsigned short *)portp = *buf++;
  337. #if defined(CONFIG_IDE)
  338. } else if ( ((port >= 0x170 && port <=0x177) || port == 0x376) ||
  339. ((port >= 0x1f0 && port <=0x1f7) || port == 0x3f6) ){
  340. portp = __port2addr_ata(port);
  341. while (count--)
  342. *(volatile unsigned short *)portp = *buf++;
  343. #endif
  344. #if defined(CONFIG_PCMCIA) && defined(CONFIG_M32R_CFC)
  345. } else if (port >= M32R_PCC_IOSTART0 && port <= M32R_PCC_IOEND0) {
  346. pcc_iowrite_word(9, port, (void *)addr, sizeof(unsigned short),
  347. count, 1);
  348. #endif
  349. } else {
  350. portp = PORT2ADDR(port);
  351. while (count--)
  352. *(volatile unsigned short *)portp = *buf++;
  353. }
  354. }
  355. void _outsl(unsigned int port, const void * addr, unsigned long count)
  356. {
  357. const unsigned long *buf = addr;
  358. unsigned char *portp;
  359. portp = PORT2ADDR(port);
  360. while (count--)
  361. *(volatile unsigned long *)portp = *buf++;
  362. }