io.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. /*
  2. * linux/arch/sh/boards/superh/microdev/io.c
  3. *
  4. * Copyright (C) 2003 Sean McGoogan (Sean.McGoogan@superh.com)
  5. * Copyright (C) 2003, 2004 SuperH, Inc.
  6. * Copyright (C) 2004 Paul Mundt
  7. *
  8. * SuperH SH4-202 MicroDev board support.
  9. *
  10. * May be copied or modified under the terms of the GNU General Public
  11. * License. See linux/COPYING for more information.
  12. */
  13. #include <linux/init.h>
  14. #include <linux/pci.h>
  15. #include <linux/wait.h>
  16. #include <asm/io.h>
  17. #include <mach/microdev.h>
  18. /*
  19. * we need to have a 'safe' address to re-direct all I/O requests
  20. * that we do not explicitly wish to handle. This safe address
  21. * must have the following properies:
  22. *
  23. * * writes are ignored (no exception)
  24. * * reads are benign (no side-effects)
  25. * * accesses of width 1, 2 and 4-bytes are all valid.
  26. *
  27. * The Processor Version Register (PVR) has these properties.
  28. */
  29. #define PVR 0xff000030 /* Processor Version Register */
  30. #define IO_IDE2_BASE 0x170ul /* I/O base for SMSC FDC37C93xAPM IDE #2 */
  31. #define IO_IDE1_BASE 0x1f0ul /* I/O base for SMSC FDC37C93xAPM IDE #1 */
  32. #define IO_ISP1161_BASE 0x290ul /* I/O port for Philips ISP1161x USB chip */
  33. #define IO_SERIAL2_BASE 0x2f8ul /* I/O base for SMSC FDC37C93xAPM Serial #2 */
  34. #define IO_LAN91C111_BASE 0x300ul /* I/O base for SMSC LAN91C111 Ethernet chip */
  35. #define IO_IDE2_MISC 0x376ul /* I/O misc for SMSC FDC37C93xAPM IDE #2 */
  36. #define IO_SUPERIO_BASE 0x3f0ul /* I/O base for SMSC FDC37C93xAPM SuperIO chip */
  37. #define IO_IDE1_MISC 0x3f6ul /* I/O misc for SMSC FDC37C93xAPM IDE #1 */
  38. #define IO_SERIAL1_BASE 0x3f8ul /* I/O base for SMSC FDC37C93xAPM Serial #1 */
  39. #define IO_ISP1161_EXTENT 0x04ul /* I/O extent for Philips ISP1161x USB chip */
  40. #define IO_LAN91C111_EXTENT 0x10ul /* I/O extent for SMSC LAN91C111 Ethernet chip */
  41. #define IO_SUPERIO_EXTENT 0x02ul /* I/O extent for SMSC FDC37C93xAPM SuperIO chip */
  42. #define IO_IDE_EXTENT 0x08ul /* I/O extent for IDE Task Register set */
  43. #define IO_SERIAL_EXTENT 0x10ul
  44. #define IO_LAN91C111_PHYS 0xa7500000ul /* Physical address of SMSC LAN91C111 Ethernet chip */
  45. #define IO_ISP1161_PHYS 0xa7700000ul /* Physical address of Philips ISP1161x USB chip */
  46. #define IO_SUPERIO_PHYS 0xa7800000ul /* Physical address of SMSC FDC37C93xAPM SuperIO chip */
  47. /*
  48. * map I/O ports to memory-mapped addresses
  49. */
  50. static unsigned long microdev_isa_port2addr(unsigned long offset)
  51. {
  52. unsigned long result;
  53. if ((offset >= IO_LAN91C111_BASE) &&
  54. (offset < IO_LAN91C111_BASE + IO_LAN91C111_EXTENT)) {
  55. /*
  56. * SMSC LAN91C111 Ethernet chip
  57. */
  58. result = IO_LAN91C111_PHYS + offset - IO_LAN91C111_BASE;
  59. } else if ((offset >= IO_SUPERIO_BASE) &&
  60. (offset < IO_SUPERIO_BASE + IO_SUPERIO_EXTENT)) {
  61. /*
  62. * SMSC FDC37C93xAPM SuperIO chip
  63. *
  64. * Configuration Registers
  65. */
  66. result = IO_SUPERIO_PHYS + (offset << 1);
  67. #if 0
  68. } else if (offset == KBD_DATA_REG || offset == KBD_CNTL_REG ||
  69. offset == KBD_STATUS_REG) {
  70. /*
  71. * SMSC FDC37C93xAPM SuperIO chip
  72. *
  73. * PS/2 Keyboard + Mouse (ports 0x60 and 0x64).
  74. */
  75. result = IO_SUPERIO_PHYS + (offset << 1);
  76. #endif
  77. } else if (((offset >= IO_IDE1_BASE) &&
  78. (offset < IO_IDE1_BASE + IO_IDE_EXTENT)) ||
  79. (offset == IO_IDE1_MISC)) {
  80. /*
  81. * SMSC FDC37C93xAPM SuperIO chip
  82. *
  83. * IDE #1
  84. */
  85. result = IO_SUPERIO_PHYS + (offset << 1);
  86. } else if (((offset >= IO_IDE2_BASE) &&
  87. (offset < IO_IDE2_BASE + IO_IDE_EXTENT)) ||
  88. (offset == IO_IDE2_MISC)) {
  89. /*
  90. * SMSC FDC37C93xAPM SuperIO chip
  91. *
  92. * IDE #2
  93. */
  94. result = IO_SUPERIO_PHYS + (offset << 1);
  95. } else if ((offset >= IO_SERIAL1_BASE) &&
  96. (offset < IO_SERIAL1_BASE + IO_SERIAL_EXTENT)) {
  97. /*
  98. * SMSC FDC37C93xAPM SuperIO chip
  99. *
  100. * Serial #1
  101. */
  102. result = IO_SUPERIO_PHYS + (offset << 1);
  103. } else if ((offset >= IO_SERIAL2_BASE) &&
  104. (offset < IO_SERIAL2_BASE + IO_SERIAL_EXTENT)) {
  105. /*
  106. * SMSC FDC37C93xAPM SuperIO chip
  107. *
  108. * Serial #2
  109. */
  110. result = IO_SUPERIO_PHYS + (offset << 1);
  111. } else if ((offset >= IO_ISP1161_BASE) &&
  112. (offset < IO_ISP1161_BASE + IO_ISP1161_EXTENT)) {
  113. /*
  114. * Philips USB ISP1161x chip
  115. */
  116. result = IO_ISP1161_PHYS + offset - IO_ISP1161_BASE;
  117. } else {
  118. /*
  119. * safe default.
  120. */
  121. printk("Warning: unexpected port in %s( offset = 0x%lx )\n",
  122. __func__, offset);
  123. result = PVR;
  124. }
  125. return result;
  126. }
  127. #define PORT2ADDR(x) (microdev_isa_port2addr(x))
  128. static inline void delay(void)
  129. {
  130. #if defined(CONFIG_PCI)
  131. /* System board present, just make a dummy SRAM access. (CS0 will be
  132. mapped to PCI memory, probably good to avoid it.) */
  133. ctrl_inw(0xa6800000);
  134. #else
  135. /* CS0 will be mapped to flash, ROM etc so safe to access it. */
  136. ctrl_inw(0xa0000000);
  137. #endif
  138. }
  139. unsigned char microdev_inb(unsigned long port)
  140. {
  141. #ifdef CONFIG_PCI
  142. if (port >= PCIBIOS_MIN_IO)
  143. return microdev_pci_inb(port);
  144. #endif
  145. return *(volatile unsigned char*)PORT2ADDR(port);
  146. }
  147. unsigned short microdev_inw(unsigned long port)
  148. {
  149. #ifdef CONFIG_PCI
  150. if (port >= PCIBIOS_MIN_IO)
  151. return microdev_pci_inw(port);
  152. #endif
  153. return *(volatile unsigned short*)PORT2ADDR(port);
  154. }
  155. unsigned int microdev_inl(unsigned long port)
  156. {
  157. #ifdef CONFIG_PCI
  158. if (port >= PCIBIOS_MIN_IO)
  159. return microdev_pci_inl(port);
  160. #endif
  161. return *(volatile unsigned int*)PORT2ADDR(port);
  162. }
  163. void microdev_outw(unsigned short b, unsigned long port)
  164. {
  165. #ifdef CONFIG_PCI
  166. if (port >= PCIBIOS_MIN_IO) {
  167. microdev_pci_outw(b, port);
  168. return;
  169. }
  170. #endif
  171. *(volatile unsigned short*)PORT2ADDR(port) = b;
  172. }
  173. void microdev_outb(unsigned char b, unsigned long port)
  174. {
  175. #ifdef CONFIG_PCI
  176. if (port >= PCIBIOS_MIN_IO) {
  177. microdev_pci_outb(b, port);
  178. return;
  179. }
  180. #endif
  181. /*
  182. * There is a board feature with the current SH4-202 MicroDev in
  183. * that the 2 byte enables (nBE0 and nBE1) are tied together (and
  184. * to the Chip Select Line (Ethernet_CS)). Due to this connectivity,
  185. * it is not possible to safely perform 8-bit writes to the
  186. * Ethernet registers, as 16-bits will be consumed from the Data
  187. * lines (corrupting the other byte). Hence, this function is
  188. * written to implement 16-bit read/modify/write for all byte-wide
  189. * accesses.
  190. *
  191. * Note: there is no problem with byte READS (even or odd).
  192. *
  193. * Sean McGoogan - 16th June 2003.
  194. */
  195. if ((port >= IO_LAN91C111_BASE) &&
  196. (port < IO_LAN91C111_BASE + IO_LAN91C111_EXTENT)) {
  197. /*
  198. * Then are trying to perform a byte-write to the
  199. * LAN91C111. This needs special care.
  200. */
  201. if (port % 2 == 1) { /* is the port odd ? */
  202. /* unset bit-0, i.e. make even */
  203. const unsigned long evenPort = port-1;
  204. unsigned short word;
  205. /*
  206. * do a 16-bit read/write to write to 'port',
  207. * preserving even byte.
  208. *
  209. * Even addresses are bits 0-7
  210. * Odd addresses are bits 8-15
  211. */
  212. word = microdev_inw(evenPort);
  213. word = (word & 0xffu) | (b << 8);
  214. microdev_outw(word, evenPort);
  215. } else {
  216. /* else, we are trying to do an even byte write */
  217. unsigned short word;
  218. /*
  219. * do a 16-bit read/write to write to 'port',
  220. * preserving odd byte.
  221. *
  222. * Even addresses are bits 0-7
  223. * Odd addresses are bits 8-15
  224. */
  225. word = microdev_inw(port);
  226. word = (word & 0xff00u) | (b);
  227. microdev_outw(word, port);
  228. }
  229. } else {
  230. *(volatile unsigned char*)PORT2ADDR(port) = b;
  231. }
  232. }
  233. void microdev_outl(unsigned int b, unsigned long port)
  234. {
  235. #ifdef CONFIG_PCI
  236. if (port >= PCIBIOS_MIN_IO) {
  237. microdev_pci_outl(b, port);
  238. return;
  239. }
  240. #endif
  241. *(volatile unsigned int*)PORT2ADDR(port) = b;
  242. }
  243. unsigned char microdev_inb_p(unsigned long port)
  244. {
  245. unsigned char v = microdev_inb(port);
  246. delay();
  247. return v;
  248. }
  249. unsigned short microdev_inw_p(unsigned long port)
  250. {
  251. unsigned short v = microdev_inw(port);
  252. delay();
  253. return v;
  254. }
  255. unsigned int microdev_inl_p(unsigned long port)
  256. {
  257. unsigned int v = microdev_inl(port);
  258. delay();
  259. return v;
  260. }
  261. void microdev_outb_p(unsigned char b, unsigned long port)
  262. {
  263. microdev_outb(b, port);
  264. delay();
  265. }
  266. void microdev_outw_p(unsigned short b, unsigned long port)
  267. {
  268. microdev_outw(b, port);
  269. delay();
  270. }
  271. void microdev_outl_p(unsigned int b, unsigned long port)
  272. {
  273. microdev_outl(b, port);
  274. delay();
  275. }
  276. void microdev_insb(unsigned long port, void *buffer, unsigned long count)
  277. {
  278. volatile unsigned char *port_addr;
  279. unsigned char *buf = buffer;
  280. port_addr = (volatile unsigned char *)PORT2ADDR(port);
  281. while (count--)
  282. *buf++ = *port_addr;
  283. }
  284. void microdev_insw(unsigned long port, void *buffer, unsigned long count)
  285. {
  286. volatile unsigned short *port_addr;
  287. unsigned short *buf = buffer;
  288. port_addr = (volatile unsigned short *)PORT2ADDR(port);
  289. while (count--)
  290. *buf++ = *port_addr;
  291. }
  292. void microdev_insl(unsigned long port, void *buffer, unsigned long count)
  293. {
  294. volatile unsigned long *port_addr;
  295. unsigned int *buf = buffer;
  296. port_addr = (volatile unsigned long *)PORT2ADDR(port);
  297. while (count--)
  298. *buf++ = *port_addr;
  299. }
  300. void microdev_outsb(unsigned long port, const void *buffer, unsigned long count)
  301. {
  302. volatile unsigned char *port_addr;
  303. const unsigned char *buf = buffer;
  304. port_addr = (volatile unsigned char *)PORT2ADDR(port);
  305. while (count--)
  306. *port_addr = *buf++;
  307. }
  308. void microdev_outsw(unsigned long port, const void *buffer, unsigned long count)
  309. {
  310. volatile unsigned short *port_addr;
  311. const unsigned short *buf = buffer;
  312. port_addr = (volatile unsigned short *)PORT2ADDR(port);
  313. while (count--)
  314. *port_addr = *buf++;
  315. }
  316. void microdev_outsl(unsigned long port, const void *buffer, unsigned long count)
  317. {
  318. volatile unsigned long *port_addr;
  319. const unsigned int *buf = buffer;
  320. port_addr = (volatile unsigned long *)PORT2ADDR(port);
  321. while (count--)
  322. *port_addr = *buf++;
  323. }