io.c 9.5 KB

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