io.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630
  1. /*
  2. * Alpha IO and memory functions.
  3. */
  4. #include <linux/kernel.h>
  5. #include <linux/types.h>
  6. #include <linux/string.h>
  7. #include <linux/module.h>
  8. #include <asm/io.h>
  9. /* Out-of-line versions of the i/o routines that redirect into the
  10. platform-specific version. Note that "platform-specific" may mean
  11. "generic", which bumps through the machine vector. */
  12. unsigned int
  13. ioread8(void __iomem *addr)
  14. {
  15. unsigned int ret = IO_CONCAT(__IO_PREFIX,ioread8)(addr);
  16. mb();
  17. return ret;
  18. }
  19. unsigned int ioread16(void __iomem *addr)
  20. {
  21. unsigned int ret = IO_CONCAT(__IO_PREFIX,ioread16)(addr);
  22. mb();
  23. return ret;
  24. }
  25. unsigned int ioread32(void __iomem *addr)
  26. {
  27. unsigned int ret = IO_CONCAT(__IO_PREFIX,ioread32)(addr);
  28. mb();
  29. return ret;
  30. }
  31. void iowrite8(u8 b, void __iomem *addr)
  32. {
  33. IO_CONCAT(__IO_PREFIX,iowrite8)(b, addr);
  34. mb();
  35. }
  36. void iowrite16(u16 b, void __iomem *addr)
  37. {
  38. IO_CONCAT(__IO_PREFIX,iowrite16)(b, addr);
  39. mb();
  40. }
  41. void iowrite32(u32 b, void __iomem *addr)
  42. {
  43. IO_CONCAT(__IO_PREFIX,iowrite32)(b, addr);
  44. mb();
  45. }
  46. EXPORT_SYMBOL(ioread8);
  47. EXPORT_SYMBOL(ioread16);
  48. EXPORT_SYMBOL(ioread32);
  49. EXPORT_SYMBOL(iowrite8);
  50. EXPORT_SYMBOL(iowrite16);
  51. EXPORT_SYMBOL(iowrite32);
  52. u8 inb(unsigned long port)
  53. {
  54. return ioread8(ioport_map(port, 1));
  55. }
  56. u16 inw(unsigned long port)
  57. {
  58. return ioread16(ioport_map(port, 2));
  59. }
  60. u32 inl(unsigned long port)
  61. {
  62. return ioread32(ioport_map(port, 4));
  63. }
  64. void outb(u8 b, unsigned long port)
  65. {
  66. iowrite8(b, ioport_map(port, 1));
  67. }
  68. void outw(u16 b, unsigned long port)
  69. {
  70. iowrite16(b, ioport_map(port, 2));
  71. }
  72. void outl(u32 b, unsigned long port)
  73. {
  74. iowrite32(b, ioport_map(port, 4));
  75. }
  76. EXPORT_SYMBOL(inb);
  77. EXPORT_SYMBOL(inw);
  78. EXPORT_SYMBOL(inl);
  79. EXPORT_SYMBOL(outb);
  80. EXPORT_SYMBOL(outw);
  81. EXPORT_SYMBOL(outl);
  82. u8 __raw_readb(const volatile void __iomem *addr)
  83. {
  84. return IO_CONCAT(__IO_PREFIX,readb)(addr);
  85. }
  86. u16 __raw_readw(const volatile void __iomem *addr)
  87. {
  88. return IO_CONCAT(__IO_PREFIX,readw)(addr);
  89. }
  90. u32 __raw_readl(const volatile void __iomem *addr)
  91. {
  92. return IO_CONCAT(__IO_PREFIX,readl)(addr);
  93. }
  94. u64 __raw_readq(const volatile void __iomem *addr)
  95. {
  96. return IO_CONCAT(__IO_PREFIX,readq)(addr);
  97. }
  98. void __raw_writeb(u8 b, volatile void __iomem *addr)
  99. {
  100. IO_CONCAT(__IO_PREFIX,writeb)(b, addr);
  101. }
  102. void __raw_writew(u16 b, volatile void __iomem *addr)
  103. {
  104. IO_CONCAT(__IO_PREFIX,writew)(b, addr);
  105. }
  106. void __raw_writel(u32 b, volatile void __iomem *addr)
  107. {
  108. IO_CONCAT(__IO_PREFIX,writel)(b, addr);
  109. }
  110. void __raw_writeq(u64 b, volatile void __iomem *addr)
  111. {
  112. IO_CONCAT(__IO_PREFIX,writeq)(b, addr);
  113. }
  114. EXPORT_SYMBOL(__raw_readb);
  115. EXPORT_SYMBOL(__raw_readw);
  116. EXPORT_SYMBOL(__raw_readl);
  117. EXPORT_SYMBOL(__raw_readq);
  118. EXPORT_SYMBOL(__raw_writeb);
  119. EXPORT_SYMBOL(__raw_writew);
  120. EXPORT_SYMBOL(__raw_writel);
  121. EXPORT_SYMBOL(__raw_writeq);
  122. u8 readb(const volatile void __iomem *addr)
  123. {
  124. u8 ret = __raw_readb(addr);
  125. mb();
  126. return ret;
  127. }
  128. u16 readw(const volatile void __iomem *addr)
  129. {
  130. u16 ret = __raw_readw(addr);
  131. mb();
  132. return ret;
  133. }
  134. u32 readl(const volatile void __iomem *addr)
  135. {
  136. u32 ret = __raw_readl(addr);
  137. mb();
  138. return ret;
  139. }
  140. u64 readq(const volatile void __iomem *addr)
  141. {
  142. u64 ret = __raw_readq(addr);
  143. mb();
  144. return ret;
  145. }
  146. void writeb(u8 b, volatile void __iomem *addr)
  147. {
  148. __raw_writeb(b, addr);
  149. mb();
  150. }
  151. void writew(u16 b, volatile void __iomem *addr)
  152. {
  153. __raw_writew(b, addr);
  154. mb();
  155. }
  156. void writel(u32 b, volatile void __iomem *addr)
  157. {
  158. __raw_writel(b, addr);
  159. mb();
  160. }
  161. void writeq(u64 b, volatile void __iomem *addr)
  162. {
  163. __raw_writeq(b, addr);
  164. mb();
  165. }
  166. EXPORT_SYMBOL(readb);
  167. EXPORT_SYMBOL(readw);
  168. EXPORT_SYMBOL(readl);
  169. EXPORT_SYMBOL(readq);
  170. EXPORT_SYMBOL(writeb);
  171. EXPORT_SYMBOL(writew);
  172. EXPORT_SYMBOL(writel);
  173. EXPORT_SYMBOL(writeq);
  174. /*
  175. * Read COUNT 8-bit bytes from port PORT into memory starting at SRC.
  176. */
  177. void ioread8_rep(void __iomem *port, void *dst, unsigned long count)
  178. {
  179. while ((unsigned long)dst & 0x3) {
  180. if (!count)
  181. return;
  182. count--;
  183. *(unsigned char *)dst = ioread8(port);
  184. dst += 1;
  185. }
  186. while (count >= 4) {
  187. unsigned int w;
  188. count -= 4;
  189. w = ioread8(port);
  190. w |= ioread8(port) << 8;
  191. w |= ioread8(port) << 16;
  192. w |= ioread8(port) << 24;
  193. *(unsigned int *)dst = w;
  194. dst += 4;
  195. }
  196. while (count) {
  197. --count;
  198. *(unsigned char *)dst = ioread8(port);
  199. dst += 1;
  200. }
  201. }
  202. void insb(unsigned long port, void *dst, unsigned long count)
  203. {
  204. ioread8_rep(ioport_map(port, 1), dst, count);
  205. }
  206. EXPORT_SYMBOL(ioread8_rep);
  207. EXPORT_SYMBOL(insb);
  208. /*
  209. * Read COUNT 16-bit words from port PORT into memory starting at
  210. * SRC. SRC must be at least short aligned. This is used by the
  211. * IDE driver to read disk sectors. Performance is important, but
  212. * the interfaces seems to be slow: just using the inlined version
  213. * of the inw() breaks things.
  214. */
  215. void ioread16_rep(void __iomem *port, void *dst, unsigned long count)
  216. {
  217. if (unlikely((unsigned long)dst & 0x3)) {
  218. if (!count)
  219. return;
  220. BUG_ON((unsigned long)dst & 0x1);
  221. count--;
  222. *(unsigned short *)dst = ioread16(port);
  223. dst += 2;
  224. }
  225. while (count >= 2) {
  226. unsigned int w;
  227. count -= 2;
  228. w = ioread16(port);
  229. w |= ioread16(port) << 16;
  230. *(unsigned int *)dst = w;
  231. dst += 4;
  232. }
  233. if (count) {
  234. *(unsigned short*)dst = ioread16(port);
  235. }
  236. }
  237. void insw(unsigned long port, void *dst, unsigned long count)
  238. {
  239. ioread16_rep(ioport_map(port, 2), dst, count);
  240. }
  241. EXPORT_SYMBOL(ioread16_rep);
  242. EXPORT_SYMBOL(insw);
  243. /*
  244. * Read COUNT 32-bit words from port PORT into memory starting at
  245. * SRC. Now works with any alignment in SRC. Performance is important,
  246. * but the interfaces seems to be slow: just using the inlined version
  247. * of the inl() breaks things.
  248. */
  249. void ioread32_rep(void __iomem *port, void *dst, unsigned long count)
  250. {
  251. if (unlikely((unsigned long)dst & 0x3)) {
  252. while (count--) {
  253. struct S { int x __attribute__((packed)); };
  254. ((struct S *)dst)->x = ioread32(port);
  255. dst += 4;
  256. }
  257. } else {
  258. /* Buffer 32-bit aligned. */
  259. while (count--) {
  260. *(unsigned int *)dst = ioread32(port);
  261. dst += 4;
  262. }
  263. }
  264. }
  265. void insl(unsigned long port, void *dst, unsigned long count)
  266. {
  267. ioread32_rep(ioport_map(port, 4), dst, count);
  268. }
  269. EXPORT_SYMBOL(ioread32_rep);
  270. EXPORT_SYMBOL(insl);
  271. /*
  272. * Like insb but in the opposite direction.
  273. * Don't worry as much about doing aligned memory transfers:
  274. * doing byte reads the "slow" way isn't nearly as slow as
  275. * doing byte writes the slow way (no r-m-w cycle).
  276. */
  277. void iowrite8_rep(void __iomem *port, const void *xsrc, unsigned long count)
  278. {
  279. const unsigned char *src = xsrc;
  280. while (count--)
  281. iowrite8(*src++, port);
  282. }
  283. void outsb(unsigned long port, const void *src, unsigned long count)
  284. {
  285. iowrite8_rep(ioport_map(port, 1), src, count);
  286. }
  287. EXPORT_SYMBOL(iowrite8_rep);
  288. EXPORT_SYMBOL(outsb);
  289. /*
  290. * Like insw but in the opposite direction. This is used by the IDE
  291. * driver to write disk sectors. Performance is important, but the
  292. * interfaces seems to be slow: just using the inlined version of the
  293. * outw() breaks things.
  294. */
  295. void iowrite16_rep(void __iomem *port, const void *src, unsigned long count)
  296. {
  297. if (unlikely((unsigned long)src & 0x3)) {
  298. if (!count)
  299. return;
  300. BUG_ON((unsigned long)src & 0x1);
  301. iowrite16(*(unsigned short *)src, port);
  302. src += 2;
  303. --count;
  304. }
  305. while (count >= 2) {
  306. unsigned int w;
  307. count -= 2;
  308. w = *(unsigned int *)src;
  309. src += 4;
  310. iowrite16(w >> 0, port);
  311. iowrite16(w >> 16, port);
  312. }
  313. if (count) {
  314. iowrite16(*(unsigned short *)src, port);
  315. }
  316. }
  317. void outsw(unsigned long port, const void *src, unsigned long count)
  318. {
  319. iowrite16_rep(ioport_map(port, 2), src, count);
  320. }
  321. EXPORT_SYMBOL(iowrite16_rep);
  322. EXPORT_SYMBOL(outsw);
  323. /*
  324. * Like insl but in the opposite direction. This is used by the IDE
  325. * driver to write disk sectors. Works with any alignment in SRC.
  326. * Performance is important, but the interfaces seems to be slow:
  327. * just using the inlined version of the outl() breaks things.
  328. */
  329. void iowrite32_rep(void __iomem *port, const void *src, unsigned long count)
  330. {
  331. if (unlikely((unsigned long)src & 0x3)) {
  332. while (count--) {
  333. struct S { int x __attribute__((packed)); };
  334. iowrite32(((struct S *)src)->x, port);
  335. src += 4;
  336. }
  337. } else {
  338. /* Buffer 32-bit aligned. */
  339. while (count--) {
  340. iowrite32(*(unsigned int *)src, port);
  341. src += 4;
  342. }
  343. }
  344. }
  345. void outsl(unsigned long port, const void *src, unsigned long count)
  346. {
  347. iowrite32_rep(ioport_map(port, 4), src, count);
  348. }
  349. EXPORT_SYMBOL(iowrite32_rep);
  350. EXPORT_SYMBOL(outsl);
  351. /*
  352. * Copy data from IO memory space to "real" memory space.
  353. * This needs to be optimized.
  354. */
  355. void memcpy_fromio(void *to, const volatile void __iomem *from, long count)
  356. {
  357. /* Optimize co-aligned transfers. Everything else gets handled
  358. a byte at a time. */
  359. if (count >= 8 && ((u64)to & 7) == ((u64)from & 7)) {
  360. count -= 8;
  361. do {
  362. *(u64 *)to = __raw_readq(from);
  363. count -= 8;
  364. to += 8;
  365. from += 8;
  366. } while (count >= 0);
  367. count += 8;
  368. }
  369. if (count >= 4 && ((u64)to & 3) == ((u64)from & 3)) {
  370. count -= 4;
  371. do {
  372. *(u32 *)to = __raw_readl(from);
  373. count -= 4;
  374. to += 4;
  375. from += 4;
  376. } while (count >= 0);
  377. count += 4;
  378. }
  379. if (count >= 2 && ((u64)to & 1) == ((u64)from & 1)) {
  380. count -= 2;
  381. do {
  382. *(u16 *)to = __raw_readw(from);
  383. count -= 2;
  384. to += 2;
  385. from += 2;
  386. } while (count >= 0);
  387. count += 2;
  388. }
  389. while (count > 0) {
  390. *(u8 *) to = __raw_readb(from);
  391. count--;
  392. to++;
  393. from++;
  394. }
  395. mb();
  396. }
  397. EXPORT_SYMBOL(memcpy_fromio);
  398. /*
  399. * Copy data from "real" memory space to IO memory space.
  400. * This needs to be optimized.
  401. */
  402. void memcpy_toio(volatile void __iomem *to, const void *from, long count)
  403. {
  404. /* Optimize co-aligned transfers. Everything else gets handled
  405. a byte at a time. */
  406. /* FIXME -- align FROM. */
  407. if (count >= 8 && ((u64)to & 7) == ((u64)from & 7)) {
  408. count -= 8;
  409. do {
  410. __raw_writeq(*(const u64 *)from, to);
  411. count -= 8;
  412. to += 8;
  413. from += 8;
  414. } while (count >= 0);
  415. count += 8;
  416. }
  417. if (count >= 4 && ((u64)to & 3) == ((u64)from & 3)) {
  418. count -= 4;
  419. do {
  420. __raw_writel(*(const u32 *)from, to);
  421. count -= 4;
  422. to += 4;
  423. from += 4;
  424. } while (count >= 0);
  425. count += 4;
  426. }
  427. if (count >= 2 && ((u64)to & 1) == ((u64)from & 1)) {
  428. count -= 2;
  429. do {
  430. __raw_writew(*(const u16 *)from, to);
  431. count -= 2;
  432. to += 2;
  433. from += 2;
  434. } while (count >= 0);
  435. count += 2;
  436. }
  437. while (count > 0) {
  438. __raw_writeb(*(const u8 *) from, to);
  439. count--;
  440. to++;
  441. from++;
  442. }
  443. mb();
  444. }
  445. EXPORT_SYMBOL(memcpy_toio);
  446. /*
  447. * "memset" on IO memory space.
  448. */
  449. void _memset_c_io(volatile void __iomem *to, unsigned long c, long count)
  450. {
  451. /* Handle any initial odd byte */
  452. if (count > 0 && ((u64)to & 1)) {
  453. __raw_writeb(c, to);
  454. to++;
  455. count--;
  456. }
  457. /* Handle any initial odd halfword */
  458. if (count >= 2 && ((u64)to & 2)) {
  459. __raw_writew(c, to);
  460. to += 2;
  461. count -= 2;
  462. }
  463. /* Handle any initial odd word */
  464. if (count >= 4 && ((u64)to & 4)) {
  465. __raw_writel(c, to);
  466. to += 4;
  467. count -= 4;
  468. }
  469. /* Handle all full-sized quadwords: we're aligned
  470. (or have a small count) */
  471. count -= 8;
  472. if (count >= 0) {
  473. do {
  474. __raw_writeq(c, to);
  475. to += 8;
  476. count -= 8;
  477. } while (count >= 0);
  478. }
  479. count += 8;
  480. /* The tail is word-aligned if we still have count >= 4 */
  481. if (count >= 4) {
  482. __raw_writel(c, to);
  483. to += 4;
  484. count -= 4;
  485. }
  486. /* The tail is half-word aligned if we have count >= 2 */
  487. if (count >= 2) {
  488. __raw_writew(c, to);
  489. to += 2;
  490. count -= 2;
  491. }
  492. /* And finally, one last byte.. */
  493. if (count) {
  494. __raw_writeb(c, to);
  495. }
  496. mb();
  497. }
  498. EXPORT_SYMBOL(_memset_c_io);
  499. /* A version of memcpy used by the vga console routines to move data around
  500. arbitrarily between screen and main memory. */
  501. void
  502. scr_memcpyw(u16 *d, const u16 *s, unsigned int count)
  503. {
  504. const u16 __iomem *ios = (const u16 __iomem *) s;
  505. u16 __iomem *iod = (u16 __iomem *) d;
  506. int s_isio = __is_ioaddr(s);
  507. int d_isio = __is_ioaddr(d);
  508. if (s_isio) {
  509. if (d_isio) {
  510. /* FIXME: Should handle unaligned ops and
  511. operation widening. */
  512. count /= 2;
  513. while (count--) {
  514. u16 tmp = __raw_readw(ios++);
  515. __raw_writew(tmp, iod++);
  516. }
  517. }
  518. else
  519. memcpy_fromio(d, ios, count);
  520. } else {
  521. if (d_isio)
  522. memcpy_toio(iod, s, count);
  523. else
  524. memcpy(d, s, count);
  525. }
  526. }
  527. EXPORT_SYMBOL(scr_memcpyw);
  528. void __iomem *ioport_map(unsigned long port, unsigned int size)
  529. {
  530. return IO_CONCAT(__IO_PREFIX,ioportmap) (port);
  531. }
  532. void ioport_unmap(void __iomem *addr)
  533. {
  534. }
  535. EXPORT_SYMBOL(ioport_map);
  536. EXPORT_SYMBOL(ioport_unmap);