io.c 9.7 KB

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