io.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  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 <[email protected]>
  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. /* Copies a block of memory to a device in an efficient manner.
  14. * Assumes the device can cope with 32-bit transfers. If it can't,
  15. * don't use this function.
  16. */
  17. void memcpy_toio(volatile void __iomem *dst, const void *src, int count)
  18. {
  19. if (((unsigned long)dst & 3) != ((unsigned long)src & 3))
  20. goto bytecopy;
  21. while ((unsigned long)dst & 3) {
  22. writeb(*(char *)src, dst++);
  23. src++;
  24. count--;
  25. }
  26. while (count > 3) {
  27. __raw_writel(*(u32 *)src, dst);
  28. src += 4;
  29. dst += 4;
  30. count -= 4;
  31. }
  32. bytecopy:
  33. while (count--) {
  34. writeb(*(char *)src, dst++);
  35. src++;
  36. }
  37. }
  38. /*
  39. ** Copies a block of memory from a device in an efficient manner.
  40. ** Assumes the device can cope with 32-bit transfers. If it can't,
  41. ** don't use this function.
  42. **
  43. ** CR16 counts on C3000 reading 256 bytes from Symbios 896 RAM:
  44. ** 27341/64 = 427 cyc per int
  45. ** 61311/128 = 478 cyc per short
  46. ** 122637/256 = 479 cyc per byte
  47. ** Ergo bus latencies dominant (not transfer size).
  48. ** Minimize total number of transfers at cost of CPU cycles.
  49. ** TODO: only look at src alignment and adjust the stores to dest.
  50. */
  51. void memcpy_fromio(void *dst, const volatile void __iomem *src, int count)
  52. {
  53. /* first compare alignment of src/dst */
  54. if ( (((unsigned long)dst ^ (unsigned long)src) & 1) || (count < 2) )
  55. goto bytecopy;
  56. if ( (((unsigned long)dst ^ (unsigned long)src) & 2) || (count < 4) )
  57. goto shortcopy;
  58. /* Then check for misaligned start address */
  59. if ((unsigned long)src & 1) {
  60. *(u8 *)dst = readb(src);
  61. src++;
  62. dst++;
  63. count--;
  64. if (count < 2) goto bytecopy;
  65. }
  66. if ((unsigned long)src & 2) {
  67. *(u16 *)dst = __raw_readw(src);
  68. src += 2;
  69. dst += 2;
  70. count -= 2;
  71. }
  72. while (count > 3) {
  73. *(u32 *)dst = __raw_readl(src);
  74. dst += 4;
  75. src += 4;
  76. count -= 4;
  77. }
  78. shortcopy:
  79. while (count > 1) {
  80. *(u16 *)dst = __raw_readw(src);
  81. src += 2;
  82. dst += 2;
  83. count -= 2;
  84. }
  85. bytecopy:
  86. while (count--) {
  87. *(char *)dst = readb(src);
  88. src++;
  89. dst++;
  90. }
  91. }
  92. /* Sets a block of memory on a device to a given value.
  93. * Assumes the device can cope with 32-bit transfers. If it can't,
  94. * don't use this function.
  95. */
  96. void memset_io(volatile void __iomem *addr, unsigned char val, int count)
  97. {
  98. u32 val32 = (val << 24) | (val << 16) | (val << 8) | val;
  99. while ((unsigned long)addr & 3) {
  100. writeb(val, addr++);
  101. count--;
  102. }
  103. while (count > 3) {
  104. __raw_writel(val32, addr);
  105. addr += 4;
  106. count -= 4;
  107. }
  108. while (count--) {
  109. writeb(val, addr++);
  110. }
  111. }
  112. /*
  113. * Read COUNT 8-bit bytes from port PORT into memory starting at
  114. * SRC.
  115. */
  116. void insb (unsigned long port, void *dst, unsigned long count)
  117. {
  118. unsigned char *p;
  119. p = (unsigned char *)dst;
  120. while (((unsigned long)p) & 0x3) {
  121. if (!count)
  122. return;
  123. count--;
  124. *p = inb(port);
  125. p++;
  126. }
  127. while (count >= 4) {
  128. unsigned int w;
  129. count -= 4;
  130. w = inb(port) << 24;
  131. w |= inb(port) << 16;
  132. w |= inb(port) << 8;
  133. w |= inb(port);
  134. *(unsigned int *) p = w;
  135. p += 4;
  136. }
  137. while (count) {
  138. --count;
  139. *p = inb(port);
  140. p++;
  141. }
  142. }
  143. /*
  144. * Read COUNT 16-bit words from port PORT into memory starting at
  145. * SRC. SRC must be at least short aligned. This is used by the
  146. * IDE driver to read disk sectors. Performance is important, but
  147. * the interfaces seems to be slow: just using the inlined version
  148. * of the inw() breaks things.
  149. */
  150. void insw (unsigned long port, void *dst, unsigned long count)
  151. {
  152. unsigned int l = 0, l2;
  153. unsigned char *p;
  154. p = (unsigned char *)dst;
  155. if (!count)
  156. return;
  157. switch (((unsigned long)p) & 0x3)
  158. {
  159. case 0x00: /* Buffer 32-bit aligned */
  160. while (count>=2) {
  161. count -= 2;
  162. l = cpu_to_le16(inw(port)) << 16;
  163. l |= cpu_to_le16(inw(port));
  164. *(unsigned int *)p = l;
  165. p += 4;
  166. }
  167. if (count) {
  168. *(unsigned short *)p = cpu_to_le16(inw(port));
  169. }
  170. break;
  171. case 0x02: /* Buffer 16-bit aligned */
  172. *(unsigned short *)p = cpu_to_le16(inw(port));
  173. p += 2;
  174. count--;
  175. while (count>=2) {
  176. count -= 2;
  177. l = cpu_to_le16(inw(port)) << 16;
  178. l |= cpu_to_le16(inw(port));
  179. *(unsigned int *)p = l;
  180. p += 4;
  181. }
  182. if (count) {
  183. *(unsigned short *)p = cpu_to_le16(inw(port));
  184. }
  185. break;
  186. case 0x01: /* Buffer 8-bit aligned */
  187. case 0x03:
  188. /* I don't bother with 32bit transfers
  189. * in this case, 16bit will have to do -- DE */
  190. --count;
  191. l = cpu_to_le16(inw(port));
  192. *p = l >> 8;
  193. p++;
  194. while (count--)
  195. {
  196. l2 = cpu_to_le16(inw(port));
  197. *(unsigned short *)p = (l & 0xff) << 8 | (l2 >> 8);
  198. p += 2;
  199. l = l2;
  200. }
  201. *p = l & 0xff;
  202. break;
  203. }
  204. }
  205. /*
  206. * Read COUNT 32-bit words from port PORT into memory starting at
  207. * SRC. Now works with any alignment in SRC. Performance is important,
  208. * but the interfaces seems to be slow: just using the inlined version
  209. * of the inl() breaks things.
  210. */
  211. void insl (unsigned long port, void *dst, unsigned long count)
  212. {
  213. unsigned int l = 0, l2;
  214. unsigned char *p;
  215. p = (unsigned char *)dst;
  216. if (!count)
  217. return;
  218. switch (((unsigned long) dst) & 0x3)
  219. {
  220. case 0x00: /* Buffer 32-bit aligned */
  221. while (count--)
  222. {
  223. *(unsigned int *)p = cpu_to_le32(inl(port));
  224. p += 4;
  225. }
  226. break;
  227. case 0x02: /* Buffer 16-bit aligned */
  228. --count;
  229. l = cpu_to_le32(inl(port));
  230. *(unsigned short *)p = l >> 16;
  231. p += 2;
  232. while (count--)
  233. {
  234. l2 = cpu_to_le32(inl(port));
  235. *(unsigned int *)p = (l & 0xffff) << 16 | (l2 >> 16);
  236. p += 4;
  237. l = l2;
  238. }
  239. *(unsigned short *)p = l & 0xffff;
  240. break;
  241. case 0x01: /* Buffer 8-bit aligned */
  242. --count;
  243. l = cpu_to_le32(inl(port));
  244. *(unsigned char *)p = l >> 24;
  245. p++;
  246. *(unsigned short *)p = (l >> 8) & 0xffff;
  247. p += 2;
  248. while (count--)
  249. {
  250. l2 = cpu_to_le32(inl(port));
  251. *(unsigned int *)p = (l & 0xff) << 24 | (l2 >> 8);
  252. p += 4;
  253. l = l2;
  254. }
  255. *p = l & 0xff;
  256. break;
  257. case 0x03: /* Buffer 8-bit aligned */
  258. --count;
  259. l = cpu_to_le32(inl(port));
  260. *p = l >> 24;
  261. p++;
  262. while (count--)
  263. {
  264. l2 = cpu_to_le32(inl(port));
  265. *(unsigned int *)p = (l & 0xffffff) << 8 | l2 >> 24;
  266. p += 4;
  267. l = l2;
  268. }
  269. *(unsigned short *)p = (l >> 8) & 0xffff;
  270. p += 2;
  271. *p = l & 0xff;
  272. break;
  273. }
  274. }
  275. /*
  276. * Like insb but in the opposite direction.
  277. * Don't worry as much about doing aligned memory transfers:
  278. * doing byte reads the "slow" way isn't nearly as slow as
  279. * doing byte writes the slow way (no r-m-w cycle).
  280. */
  281. void outsb(unsigned long port, const void * src, unsigned long count)
  282. {
  283. const unsigned char *p;
  284. p = (const unsigned char *)src;
  285. while (count) {
  286. count--;
  287. outb(*p, port);
  288. p++;
  289. }
  290. }
  291. /*
  292. * Like insw but in the opposite direction. This is used by the IDE
  293. * driver to write disk sectors. Performance is important, but the
  294. * interfaces seems to be slow: just using the inlined version of the
  295. * outw() breaks things.
  296. */
  297. void outsw (unsigned long port, const void *src, unsigned long count)
  298. {
  299. unsigned int l = 0, l2;
  300. const unsigned char *p;
  301. p = (const unsigned char *)src;
  302. if (!count)
  303. return;
  304. switch (((unsigned long)p) & 0x3)
  305. {
  306. case 0x00: /* Buffer 32-bit aligned */
  307. while (count>=2) {
  308. count -= 2;
  309. l = *(unsigned int *)p;
  310. p += 4;
  311. outw(le16_to_cpu(l >> 16), port);
  312. outw(le16_to_cpu(l & 0xffff), port);
  313. }
  314. if (count) {
  315. outw(le16_to_cpu(*(unsigned short*)p), port);
  316. }
  317. break;
  318. case 0x02: /* Buffer 16-bit aligned */
  319. outw(le16_to_cpu(*(unsigned short*)p), port);
  320. p += 2;
  321. count--;
  322. while (count>=2) {
  323. count -= 2;
  324. l = *(unsigned int *)p;
  325. p += 4;
  326. outw(le16_to_cpu(l >> 16), port);
  327. outw(le16_to_cpu(l & 0xffff), port);
  328. }
  329. if (count) {
  330. outw(le16_to_cpu(*(unsigned short *)p), port);
  331. }
  332. break;
  333. case 0x01: /* Buffer 8-bit aligned */
  334. /* I don't bother with 32bit transfers
  335. * in this case, 16bit will have to do -- DE */
  336. l = *p << 8;
  337. p++;
  338. count--;
  339. while (count)
  340. {
  341. count--;
  342. l2 = *(unsigned short *)p;
  343. p += 2;
  344. outw(le16_to_cpu(l | l2 >> 8), port);
  345. l = l2 << 8;
  346. }
  347. l2 = *(unsigned char *)p;
  348. outw (le16_to_cpu(l | l2>>8), port);
  349. break;
  350. }
  351. }
  352. /*
  353. * Like insl but in the opposite direction. This is used by the IDE
  354. * driver to write disk sectors. Works with any alignment in SRC.
  355. * Performance is important, but the interfaces seems to be slow:
  356. * just using the inlined version of the outl() breaks things.
  357. */
  358. void outsl (unsigned long port, const void *src, unsigned long count)
  359. {
  360. unsigned int l = 0, l2;
  361. const unsigned char *p;
  362. p = (const unsigned char *)src;
  363. if (!count)
  364. return;
  365. switch (((unsigned long)p) & 0x3)
  366. {
  367. case 0x00: /* Buffer 32-bit aligned */
  368. while (count--)
  369. {
  370. outl(le32_to_cpu(*(unsigned int *)p), port);
  371. p += 4;
  372. }
  373. break;
  374. case 0x02: /* Buffer 16-bit aligned */
  375. --count;
  376. l = *(unsigned short *)p;
  377. p += 2;
  378. while (count--)
  379. {
  380. l2 = *(unsigned int *)p;
  381. p += 4;
  382. outl (le32_to_cpu(l << 16 | l2 >> 16), port);
  383. l = l2;
  384. }
  385. l2 = *(unsigned short *)p;
  386. outl (le32_to_cpu(l << 16 | l2), port);
  387. break;
  388. case 0x01: /* Buffer 8-bit aligned */
  389. --count;
  390. l = *p << 24;
  391. p++;
  392. l |= *(unsigned short *)p << 8;
  393. p += 2;
  394. while (count--)
  395. {
  396. l2 = *(unsigned int *)p;
  397. p += 4;
  398. outl (le32_to_cpu(l | l2 >> 24), port);
  399. l = l2 << 8;
  400. }
  401. l2 = *p;
  402. outl (le32_to_cpu(l | l2), port);
  403. break;
  404. case 0x03: /* Buffer 8-bit aligned */
  405. --count;
  406. l = *p << 24;
  407. p++;
  408. while (count--)
  409. {
  410. l2 = *(unsigned int *)p;
  411. p += 4;
  412. outl (le32_to_cpu(l | l2 >> 8), port);
  413. l = l2 << 24;
  414. }
  415. l2 = *(unsigned short *)p << 16;
  416. p += 2;
  417. l2 |= *p;
  418. outl (le32_to_cpu(l | l2), port);
  419. break;
  420. }
  421. }
  422. EXPORT_SYMBOL(insb);
  423. EXPORT_SYMBOL(insw);
  424. EXPORT_SYMBOL(insl);
  425. EXPORT_SYMBOL(outsb);
  426. EXPORT_SYMBOL(outsw);
  427. EXPORT_SYMBOL(outsl);