io.c 9.5 KB

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