io.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * arch/parisc/lib/io.c
  4. *
  5. * Copyright (c) Matthew Wilcox 2001 for Hewlett-Packard
  6. * Copyright (c) Randolph Chung 2001 <tausq@debian.org>
  7. *
  8. * IO accessing functions which shouldn't be inlined because they're too big
  9. */
  10. #include <linux/kernel.h>
  11. #include <linux/module.h>
  12. #include <asm/io.h>
  13. /*
  14. * Read COUNT 8-bit bytes from port PORT into memory starting at
  15. * SRC.
  16. */
  17. void insb (unsigned long port, void *dst, unsigned long count)
  18. {
  19. unsigned char *p;
  20. p = (unsigned char *)dst;
  21. while (((unsigned long)p) & 0x3) {
  22. if (!count)
  23. return;
  24. count--;
  25. *p = inb(port);
  26. p++;
  27. }
  28. while (count >= 4) {
  29. unsigned int w;
  30. count -= 4;
  31. w = inb(port) << 24;
  32. w |= inb(port) << 16;
  33. w |= inb(port) << 8;
  34. w |= inb(port);
  35. *(unsigned int *) p = w;
  36. p += 4;
  37. }
  38. while (count) {
  39. --count;
  40. *p = inb(port);
  41. p++;
  42. }
  43. }
  44. /*
  45. * Read COUNT 16-bit words from port PORT into memory starting at
  46. * SRC. SRC must be at least short aligned. This is used by the
  47. * IDE driver to read disk sectors. Performance is important, but
  48. * the interfaces seems to be slow: just using the inlined version
  49. * of the inw() breaks things.
  50. */
  51. void insw (unsigned long port, void *dst, unsigned long count)
  52. {
  53. unsigned int l = 0, l2;
  54. unsigned char *p;
  55. p = (unsigned char *)dst;
  56. if (!count)
  57. return;
  58. switch (((unsigned long)p) & 0x3)
  59. {
  60. case 0x00: /* Buffer 32-bit aligned */
  61. while (count>=2) {
  62. count -= 2;
  63. l = cpu_to_le16(inw(port)) << 16;
  64. l |= cpu_to_le16(inw(port));
  65. *(unsigned int *)p = l;
  66. p += 4;
  67. }
  68. if (count) {
  69. *(unsigned short *)p = cpu_to_le16(inw(port));
  70. }
  71. break;
  72. case 0x02: /* Buffer 16-bit aligned */
  73. *(unsigned short *)p = cpu_to_le16(inw(port));
  74. p += 2;
  75. count--;
  76. while (count>=2) {
  77. count -= 2;
  78. l = cpu_to_le16(inw(port)) << 16;
  79. l |= cpu_to_le16(inw(port));
  80. *(unsigned int *)p = l;
  81. p += 4;
  82. }
  83. if (count) {
  84. *(unsigned short *)p = cpu_to_le16(inw(port));
  85. }
  86. break;
  87. case 0x01: /* Buffer 8-bit aligned */
  88. case 0x03:
  89. /* I don't bother with 32bit transfers
  90. * in this case, 16bit will have to do -- DE */
  91. --count;
  92. l = cpu_to_le16(inw(port));
  93. *p = l >> 8;
  94. p++;
  95. while (count--)
  96. {
  97. l2 = cpu_to_le16(inw(port));
  98. *(unsigned short *)p = (l & 0xff) << 8 | (l2 >> 8);
  99. p += 2;
  100. l = l2;
  101. }
  102. *p = l & 0xff;
  103. break;
  104. }
  105. }
  106. /*
  107. * Read COUNT 32-bit words from port PORT into memory starting at
  108. * SRC. Now works with any alignment in SRC. Performance is important,
  109. * but the interfaces seems to be slow: just using the inlined version
  110. * of the inl() breaks things.
  111. */
  112. void insl (unsigned long port, void *dst, unsigned long count)
  113. {
  114. unsigned int l = 0, l2;
  115. unsigned char *p;
  116. p = (unsigned char *)dst;
  117. if (!count)
  118. return;
  119. switch (((unsigned long) dst) & 0x3)
  120. {
  121. case 0x00: /* Buffer 32-bit aligned */
  122. while (count--)
  123. {
  124. *(unsigned int *)p = cpu_to_le32(inl(port));
  125. p += 4;
  126. }
  127. break;
  128. case 0x02: /* Buffer 16-bit aligned */
  129. --count;
  130. l = cpu_to_le32(inl(port));
  131. *(unsigned short *)p = l >> 16;
  132. p += 2;
  133. while (count--)
  134. {
  135. l2 = cpu_to_le32(inl(port));
  136. *(unsigned int *)p = (l & 0xffff) << 16 | (l2 >> 16);
  137. p += 4;
  138. l = l2;
  139. }
  140. *(unsigned short *)p = l & 0xffff;
  141. break;
  142. case 0x01: /* Buffer 8-bit aligned */
  143. --count;
  144. l = cpu_to_le32(inl(port));
  145. *(unsigned char *)p = l >> 24;
  146. p++;
  147. *(unsigned short *)p = (l >> 8) & 0xffff;
  148. p += 2;
  149. while (count--)
  150. {
  151. l2 = cpu_to_le32(inl(port));
  152. *(unsigned int *)p = (l & 0xff) << 24 | (l2 >> 8);
  153. p += 4;
  154. l = l2;
  155. }
  156. *p = l & 0xff;
  157. break;
  158. case 0x03: /* Buffer 8-bit aligned */
  159. --count;
  160. l = cpu_to_le32(inl(port));
  161. *p = l >> 24;
  162. p++;
  163. while (count--)
  164. {
  165. l2 = cpu_to_le32(inl(port));
  166. *(unsigned int *)p = (l & 0xffffff) << 8 | l2 >> 24;
  167. p += 4;
  168. l = l2;
  169. }
  170. *(unsigned short *)p = (l >> 8) & 0xffff;
  171. p += 2;
  172. *p = l & 0xff;
  173. break;
  174. }
  175. }
  176. /*
  177. * Like insb but in the opposite direction.
  178. * Don't worry as much about doing aligned memory transfers:
  179. * doing byte reads the "slow" way isn't nearly as slow as
  180. * doing byte writes the slow way (no r-m-w cycle).
  181. */
  182. void outsb(unsigned long port, const void * src, unsigned long count)
  183. {
  184. const unsigned char *p;
  185. p = (const unsigned char *)src;
  186. while (count) {
  187. count--;
  188. outb(*p, port);
  189. p++;
  190. }
  191. }
  192. /*
  193. * Like insw but in the opposite direction. This is used by the IDE
  194. * driver to write disk sectors. Performance is important, but the
  195. * interfaces seems to be slow: just using the inlined version of the
  196. * outw() breaks things.
  197. */
  198. void outsw (unsigned long port, const void *src, unsigned long count)
  199. {
  200. unsigned int l = 0, l2;
  201. const unsigned char *p;
  202. p = (const unsigned char *)src;
  203. if (!count)
  204. return;
  205. switch (((unsigned long)p) & 0x3)
  206. {
  207. case 0x00: /* Buffer 32-bit aligned */
  208. while (count>=2) {
  209. count -= 2;
  210. l = *(unsigned int *)p;
  211. p += 4;
  212. outw(le16_to_cpu(l >> 16), port);
  213. outw(le16_to_cpu(l & 0xffff), port);
  214. }
  215. if (count) {
  216. outw(le16_to_cpu(*(unsigned short*)p), port);
  217. }
  218. break;
  219. case 0x02: /* Buffer 16-bit aligned */
  220. outw(le16_to_cpu(*(unsigned short*)p), port);
  221. p += 2;
  222. count--;
  223. while (count>=2) {
  224. count -= 2;
  225. l = *(unsigned int *)p;
  226. p += 4;
  227. outw(le16_to_cpu(l >> 16), port);
  228. outw(le16_to_cpu(l & 0xffff), port);
  229. }
  230. if (count) {
  231. outw(le16_to_cpu(*(unsigned short *)p), port);
  232. }
  233. break;
  234. case 0x01: /* Buffer 8-bit aligned */
  235. /* I don't bother with 32bit transfers
  236. * in this case, 16bit will have to do -- DE */
  237. l = *p << 8;
  238. p++;
  239. count--;
  240. while (count)
  241. {
  242. count--;
  243. l2 = *(unsigned short *)p;
  244. p += 2;
  245. outw(le16_to_cpu(l | l2 >> 8), port);
  246. l = l2 << 8;
  247. }
  248. l2 = *(unsigned char *)p;
  249. outw (le16_to_cpu(l | l2>>8), port);
  250. break;
  251. }
  252. }
  253. /*
  254. * Like insl but in the opposite direction. This is used by the IDE
  255. * driver to write disk sectors. Works with any alignment in SRC.
  256. * Performance is important, but the interfaces seems to be slow:
  257. * just using the inlined version of the outl() breaks things.
  258. */
  259. void outsl (unsigned long port, const void *src, unsigned long count)
  260. {
  261. unsigned int l = 0, l2;
  262. const unsigned char *p;
  263. p = (const unsigned char *)src;
  264. if (!count)
  265. return;
  266. switch (((unsigned long)p) & 0x3)
  267. {
  268. case 0x00: /* Buffer 32-bit aligned */
  269. while (count--)
  270. {
  271. outl(le32_to_cpu(*(unsigned int *)p), port);
  272. p += 4;
  273. }
  274. break;
  275. case 0x02: /* Buffer 16-bit aligned */
  276. --count;
  277. l = *(unsigned short *)p;
  278. p += 2;
  279. while (count--)
  280. {
  281. l2 = *(unsigned int *)p;
  282. p += 4;
  283. outl (le32_to_cpu(l << 16 | l2 >> 16), port);
  284. l = l2;
  285. }
  286. l2 = *(unsigned short *)p;
  287. outl (le32_to_cpu(l << 16 | l2), port);
  288. break;
  289. case 0x01: /* Buffer 8-bit aligned */
  290. --count;
  291. l = *p << 24;
  292. p++;
  293. l |= *(unsigned short *)p << 8;
  294. p += 2;
  295. while (count--)
  296. {
  297. l2 = *(unsigned int *)p;
  298. p += 4;
  299. outl (le32_to_cpu(l | l2 >> 24), port);
  300. l = l2 << 8;
  301. }
  302. l2 = *p;
  303. outl (le32_to_cpu(l | l2), port);
  304. break;
  305. case 0x03: /* Buffer 8-bit aligned */
  306. --count;
  307. l = *p << 24;
  308. p++;
  309. while (count--)
  310. {
  311. l2 = *(unsigned int *)p;
  312. p += 4;
  313. outl (le32_to_cpu(l | l2 >> 8), port);
  314. l = l2 << 24;
  315. }
  316. l2 = *(unsigned short *)p << 16;
  317. p += 2;
  318. l2 |= *p;
  319. outl (le32_to_cpu(l | l2), port);
  320. break;
  321. }
  322. }
  323. EXPORT_SYMBOL(insb);
  324. EXPORT_SYMBOL(insw);
  325. EXPORT_SYMBOL(insl);
  326. EXPORT_SYMBOL(outsb);
  327. EXPORT_SYMBOL(outsw);
  328. EXPORT_SYMBOL(outsl);