io.c 11 KB

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