io.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590
  1. /*
  2. * linux/include/asm-arm/arch-ixp4xx/io.h
  3. *
  4. * Author: Deepak Saxena <dsaxena@plexity.net>
  5. *
  6. * Copyright (C) 2002-2005 MontaVista Software, Inc.
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. */
  12. #ifndef __ASM_ARM_ARCH_IO_H
  13. #define __ASM_ARM_ARCH_IO_H
  14. #include <asm/hardware.h>
  15. #define IO_SPACE_LIMIT 0xffff0000
  16. #define BIT(x) ((1)<<(x))
  17. extern int (*ixp4xx_pci_read)(u32 addr, u32 cmd, u32* data);
  18. extern int ixp4xx_pci_write(u32 addr, u32 cmd, u32 data);
  19. /*
  20. * IXP4xx provides two methods of accessing PCI memory space:
  21. *
  22. * 1) A direct mapped window from 0x48000000 to 0x4bffffff (64MB).
  23. * To access PCI via this space, we simply ioremap() the BAR
  24. * into the kernel and we can use the standard read[bwl]/write[bwl]
  25. * macros. This is the preffered method due to speed but it
  26. * limits the system to just 64MB of PCI memory. This can be
  27. * problamatic if using video cards and other memory-heavy
  28. * targets.
  29. *
  30. * 2) If > 64MB of memory space is required, the IXP4xx can be configured
  31. * to use indirect registers to access PCI (as we do below for I/O
  32. * transactions). This allows for up to 128MB (0x48000000 to 0x4fffffff)
  33. * of memory on the bus. The disadvantage of this is that every
  34. * PCI access requires three local register accesses plus a spinlock,
  35. * but in some cases the performance hit is acceptable. In addition,
  36. * you cannot mmap() PCI devices in this case.
  37. *
  38. */
  39. #ifndef CONFIG_IXP4XX_INDIRECT_PCI
  40. #define __mem_pci(a) (a)
  41. #else
  42. #include <linux/mm.h>
  43. /*
  44. * In the case of using indirect PCI, we simply return the actual PCI
  45. * address and our read/write implementation use that to drive the
  46. * access registers. If something outside of PCI is ioremap'd, we
  47. * fallback to the default.
  48. */
  49. static inline void __iomem *
  50. __ixp4xx_ioremap(unsigned long addr, size_t size, unsigned long flags)
  51. {
  52. if((addr < 0x48000000) || (addr > 0x4fffffff))
  53. return __ioremap(addr, size, flags);
  54. return (void *)addr;
  55. }
  56. static inline void
  57. __ixp4xx_iounmap(void __iomem *addr)
  58. {
  59. if ((u32)addr >= VMALLOC_START)
  60. __iounmap(addr);
  61. }
  62. #define __arch_ioremap(a, s, f) __ixp4xx_ioremap(a, s, f)
  63. #define __arch_iounmap(a) __ixp4xx_iounmap(a)
  64. #define writeb(v, p) __ixp4xx_writeb(v, p)
  65. #define writew(v, p) __ixp4xx_writew(v, p)
  66. #define writel(v, p) __ixp4xx_writel(v, p)
  67. #define writesb(p, v, l) __ixp4xx_writesb(p, v, l)
  68. #define writesw(p, v, l) __ixp4xx_writesw(p, v, l)
  69. #define writesl(p, v, l) __ixp4xx_writesl(p, v, l)
  70. #define readb(p) __ixp4xx_readb(p)
  71. #define readw(p) __ixp4xx_readw(p)
  72. #define readl(p) __ixp4xx_readl(p)
  73. #define readsb(p, v, l) __ixp4xx_readsb(p, v, l)
  74. #define readsw(p, v, l) __ixp4xx_readsw(p, v, l)
  75. #define readsl(p, v, l) __ixp4xx_readsl(p, v, l)
  76. static inline void
  77. __ixp4xx_writeb(u8 value, volatile void __iomem *p)
  78. {
  79. u32 addr = (u32)p;
  80. u32 n, byte_enables, data;
  81. if (addr >= VMALLOC_START) {
  82. __raw_writeb(value, addr);
  83. return;
  84. }
  85. n = addr % 4;
  86. byte_enables = (0xf & ~BIT(n)) << IXP4XX_PCI_NP_CBE_BESL;
  87. data = value << (8*n);
  88. ixp4xx_pci_write(addr, byte_enables | NP_CMD_MEMWRITE, data);
  89. }
  90. static inline void
  91. __ixp4xx_writesb(volatile void __iomem *bus_addr, const u8 *vaddr, int count)
  92. {
  93. while (count--)
  94. writeb(*vaddr++, bus_addr);
  95. }
  96. static inline void
  97. __ixp4xx_writew(u16 value, volatile void __iomem *p)
  98. {
  99. u32 addr = (u32)p;
  100. u32 n, byte_enables, data;
  101. if (addr >= VMALLOC_START) {
  102. __raw_writew(value, addr);
  103. return;
  104. }
  105. n = addr % 4;
  106. byte_enables = (0xf & ~(BIT(n) | BIT(n+1))) << IXP4XX_PCI_NP_CBE_BESL;
  107. data = value << (8*n);
  108. ixp4xx_pci_write(addr, byte_enables | NP_CMD_MEMWRITE, data);
  109. }
  110. static inline void
  111. __ixp4xx_writesw(volatile void __iomem *bus_addr, const u16 *vaddr, int count)
  112. {
  113. while (count--)
  114. writew(*vaddr++, bus_addr);
  115. }
  116. static inline void
  117. __ixp4xx_writel(u32 value, volatile void __iomem *p)
  118. {
  119. u32 addr = (u32)p;
  120. if (addr >= VMALLOC_START) {
  121. __raw_writel(value, addr);
  122. return;
  123. }
  124. ixp4xx_pci_write(addr, NP_CMD_MEMWRITE, value);
  125. }
  126. static inline void
  127. __ixp4xx_writesl(volatile void __iomem *bus_addr, const u32 *vaddr, int count)
  128. {
  129. while (count--)
  130. writel(*vaddr++, bus_addr);
  131. }
  132. static inline unsigned char
  133. __ixp4xx_readb(const volatile void __iomem *p)
  134. {
  135. u32 addr = (u32)p;
  136. u32 n, byte_enables, data;
  137. if (addr >= VMALLOC_START)
  138. return __raw_readb(addr);
  139. n = addr % 4;
  140. byte_enables = (0xf & ~BIT(n)) << IXP4XX_PCI_NP_CBE_BESL;
  141. if (ixp4xx_pci_read(addr, byte_enables | NP_CMD_MEMREAD, &data))
  142. return 0xff;
  143. return data >> (8*n);
  144. }
  145. static inline void
  146. __ixp4xx_readsb(const volatile void __iomem *bus_addr, u8 *vaddr, u32 count)
  147. {
  148. while (count--)
  149. *vaddr++ = readb(bus_addr);
  150. }
  151. static inline unsigned short
  152. __ixp4xx_readw(const volatile void __iomem *p)
  153. {
  154. u32 addr = (u32)p;
  155. u32 n, byte_enables, data;
  156. if (addr >= VMALLOC_START)
  157. return __raw_readw(addr);
  158. n = addr % 4;
  159. byte_enables = (0xf & ~(BIT(n) | BIT(n+1))) << IXP4XX_PCI_NP_CBE_BESL;
  160. if (ixp4xx_pci_read(addr, byte_enables | NP_CMD_MEMREAD, &data))
  161. return 0xffff;
  162. return data>>(8*n);
  163. }
  164. static inline void
  165. __ixp4xx_readsw(const volatile void __iomem *bus_addr, u16 *vaddr, u32 count)
  166. {
  167. while (count--)
  168. *vaddr++ = readw(bus_addr);
  169. }
  170. static inline unsigned long
  171. __ixp4xx_readl(const volatile void __iomem *p)
  172. {
  173. u32 addr = (u32)p;
  174. u32 data;
  175. if (addr >= VMALLOC_START)
  176. return __raw_readl(addr);
  177. if (ixp4xx_pci_read(addr, NP_CMD_MEMREAD, &data))
  178. return 0xffffffff;
  179. return data;
  180. }
  181. static inline void
  182. __ixp4xx_readsl(const volatile void __iomem *bus_addr, u32 *vaddr, u32 count)
  183. {
  184. while (count--)
  185. *vaddr++ = readl(bus_addr);
  186. }
  187. /*
  188. * We can use the built-in functions b/c they end up calling writeb/readb
  189. */
  190. #define memset_io(c,v,l) _memset_io((c),(v),(l))
  191. #define memcpy_fromio(a,c,l) _memcpy_fromio((a),(c),(l))
  192. #define memcpy_toio(c,a,l) _memcpy_toio((c),(a),(l))
  193. #define eth_io_copy_and_sum(s,c,l,b) \
  194. eth_copy_and_sum((s),__mem_pci(c),(l),(b))
  195. static inline int
  196. check_signature(const unsigned char __iomem *bus_addr, const unsigned char *signature,
  197. int length)
  198. {
  199. int retval = 0;
  200. do {
  201. if (readb(bus_addr) != *signature)
  202. goto out;
  203. bus_addr++;
  204. signature++;
  205. length--;
  206. } while (length);
  207. retval = 1;
  208. out:
  209. return retval;
  210. }
  211. #endif
  212. #ifndef CONFIG_PCI
  213. #define __io(v) v
  214. #else
  215. /*
  216. * IXP4xx does not have a transparent cpu -> PCI I/O translation
  217. * window. Instead, it has a set of registers that must be tweaked
  218. * with the proper byte lanes, command types, and address for the
  219. * transaction. This means that we need to override the default
  220. * I/O functions.
  221. */
  222. #define outb(p, v) __ixp4xx_outb(p, v)
  223. #define outw(p, v) __ixp4xx_outw(p, v)
  224. #define outl(p, v) __ixp4xx_outl(p, v)
  225. #define outsb(p, v, l) __ixp4xx_outsb(p, v, l)
  226. #define outsw(p, v, l) __ixp4xx_outsw(p, v, l)
  227. #define outsl(p, v, l) __ixp4xx_outsl(p, v, l)
  228. #define inb(p) __ixp4xx_inb(p)
  229. #define inw(p) __ixp4xx_inw(p)
  230. #define inl(p) __ixp4xx_inl(p)
  231. #define insb(p, v, l) __ixp4xx_insb(p, v, l)
  232. #define insw(p, v, l) __ixp4xx_insw(p, v, l)
  233. #define insl(p, v, l) __ixp4xx_insl(p, v, l)
  234. static inline void
  235. __ixp4xx_outb(u8 value, u32 addr)
  236. {
  237. u32 n, byte_enables, data;
  238. n = addr % 4;
  239. byte_enables = (0xf & ~BIT(n)) << IXP4XX_PCI_NP_CBE_BESL;
  240. data = value << (8*n);
  241. ixp4xx_pci_write(addr, byte_enables | NP_CMD_IOWRITE, data);
  242. }
  243. static inline void
  244. __ixp4xx_outsb(u32 io_addr, const u8 *vaddr, u32 count)
  245. {
  246. while (count--)
  247. outb(*vaddr++, io_addr);
  248. }
  249. static inline void
  250. __ixp4xx_outw(u16 value, u32 addr)
  251. {
  252. u32 n, byte_enables, data;
  253. n = addr % 4;
  254. byte_enables = (0xf & ~(BIT(n) | BIT(n+1))) << IXP4XX_PCI_NP_CBE_BESL;
  255. data = value << (8*n);
  256. ixp4xx_pci_write(addr, byte_enables | NP_CMD_IOWRITE, data);
  257. }
  258. static inline void
  259. __ixp4xx_outsw(u32 io_addr, const u16 *vaddr, u32 count)
  260. {
  261. while (count--)
  262. outw(cpu_to_le16(*vaddr++), io_addr);
  263. }
  264. static inline void
  265. __ixp4xx_outl(u32 value, u32 addr)
  266. {
  267. ixp4xx_pci_write(addr, NP_CMD_IOWRITE, value);
  268. }
  269. static inline void
  270. __ixp4xx_outsl(u32 io_addr, const u32 *vaddr, u32 count)
  271. {
  272. while (count--)
  273. outl(*vaddr++, io_addr);
  274. }
  275. static inline u8
  276. __ixp4xx_inb(u32 addr)
  277. {
  278. u32 n, byte_enables, data;
  279. n = addr % 4;
  280. byte_enables = (0xf & ~BIT(n)) << IXP4XX_PCI_NP_CBE_BESL;
  281. if (ixp4xx_pci_read(addr, byte_enables | NP_CMD_IOREAD, &data))
  282. return 0xff;
  283. return data >> (8*n);
  284. }
  285. static inline void
  286. __ixp4xx_insb(u32 io_addr, u8 *vaddr, u32 count)
  287. {
  288. while (count--)
  289. *vaddr++ = inb(io_addr);
  290. }
  291. static inline u16
  292. __ixp4xx_inw(u32 addr)
  293. {
  294. u32 n, byte_enables, data;
  295. n = addr % 4;
  296. byte_enables = (0xf & ~(BIT(n) | BIT(n+1))) << IXP4XX_PCI_NP_CBE_BESL;
  297. if (ixp4xx_pci_read(addr, byte_enables | NP_CMD_IOREAD, &data))
  298. return 0xffff;
  299. return data>>(8*n);
  300. }
  301. static inline void
  302. __ixp4xx_insw(u32 io_addr, u16 *vaddr, u32 count)
  303. {
  304. while (count--)
  305. *vaddr++ = le16_to_cpu(inw(io_addr));
  306. }
  307. static inline u32
  308. __ixp4xx_inl(u32 addr)
  309. {
  310. u32 data;
  311. if (ixp4xx_pci_read(addr, NP_CMD_IOREAD, &data))
  312. return 0xffffffff;
  313. return data;
  314. }
  315. static inline void
  316. __ixp4xx_insl(u32 io_addr, u32 *vaddr, u32 count)
  317. {
  318. while (count--)
  319. *vaddr++ = inl(io_addr);
  320. }
  321. #define PIO_OFFSET 0x10000UL
  322. #define PIO_MASK 0x0ffffUL
  323. #define __is_io_address(p) (((unsigned long)p >= PIO_OFFSET) && \
  324. ((unsigned long)p <= (PIO_MASK + PIO_OFFSET)))
  325. static inline unsigned int
  326. __ixp4xx_ioread8(const void __iomem *addr)
  327. {
  328. unsigned long port = (unsigned long __force)addr;
  329. if (__is_io_address(port))
  330. return (unsigned int)__ixp4xx_inb(port & PIO_MASK);
  331. else
  332. #ifndef CONFIG_IXP4XX_INDIRECT_PCI
  333. return (unsigned int)__raw_readb(port);
  334. #else
  335. return (unsigned int)__ixp4xx_readb(addr);
  336. #endif
  337. }
  338. static inline void
  339. __ixp4xx_ioread8_rep(const void __iomem *addr, void *vaddr, u32 count)
  340. {
  341. unsigned long port = (unsigned long __force)addr;
  342. if (__is_io_address(port))
  343. __ixp4xx_insb(port & PIO_MASK, vaddr, count);
  344. else
  345. #ifndef CONFIG_IXP4XX_INDIRECT_PCI
  346. __raw_readsb(addr, vaddr, count);
  347. #else
  348. __ixp4xx_readsb(addr, vaddr, count);
  349. #endif
  350. }
  351. static inline unsigned int
  352. __ixp4xx_ioread16(const void __iomem *addr)
  353. {
  354. unsigned long port = (unsigned long __force)addr;
  355. if (__is_io_address(port))
  356. return (unsigned int)__ixp4xx_inw(port & PIO_MASK);
  357. else
  358. #ifndef CONFIG_IXP4XX_INDIRECT_PCI
  359. return le16_to_cpu(__raw_readw((u32)port));
  360. #else
  361. return (unsigned int)__ixp4xx_readw(addr);
  362. #endif
  363. }
  364. static inline void
  365. __ixp4xx_ioread16_rep(const void __iomem *addr, void *vaddr, u32 count)
  366. {
  367. unsigned long port = (unsigned long __force)addr;
  368. if (__is_io_address(port))
  369. __ixp4xx_insw(port & PIO_MASK, vaddr, count);
  370. else
  371. #ifndef CONFIG_IXP4XX_INDIRECT_PCI
  372. __raw_readsw(addr, vaddr, count);
  373. #else
  374. __ixp4xx_readsw(addr, vaddr, count);
  375. #endif
  376. }
  377. static inline unsigned int
  378. __ixp4xx_ioread32(const void __iomem *addr)
  379. {
  380. unsigned long port = (unsigned long __force)addr;
  381. if (__is_io_address(port))
  382. return (unsigned int)__ixp4xx_inl(port & PIO_MASK);
  383. else {
  384. #ifndef CONFIG_IXP4XX_INDIRECT_PCI
  385. return le32_to_cpu(__raw_readl((u32)port));
  386. #else
  387. return (unsigned int)__ixp4xx_readl(addr);
  388. #endif
  389. }
  390. }
  391. static inline void
  392. __ixp4xx_ioread32_rep(const void __iomem *addr, void *vaddr, u32 count)
  393. {
  394. unsigned long port = (unsigned long __force)addr;
  395. if (__is_io_address(port))
  396. __ixp4xx_insl(port & PIO_MASK, vaddr, count);
  397. else
  398. #ifndef CONFIG_IXP4XX_INDIRECT_PCI
  399. __raw_readsl(addr, vaddr, count);
  400. #else
  401. __ixp4xx_readsl(addr, vaddr, count);
  402. #endif
  403. }
  404. static inline void
  405. __ixp4xx_iowrite8(u8 value, void __iomem *addr)
  406. {
  407. unsigned long port = (unsigned long __force)addr;
  408. if (__is_io_address(port))
  409. __ixp4xx_outb(value, port & PIO_MASK);
  410. else
  411. #ifndef CONFIG_IXP4XX_INDIRECT_PCI
  412. __raw_writeb(value, port);
  413. #else
  414. __ixp4xx_writeb(value, addr);
  415. #endif
  416. }
  417. static inline void
  418. __ixp4xx_iowrite8_rep(void __iomem *addr, const void *vaddr, u32 count)
  419. {
  420. unsigned long port = (unsigned long __force)addr;
  421. if (__is_io_address(port))
  422. __ixp4xx_outsb(port & PIO_MASK, vaddr, count);
  423. else
  424. #ifndef CONFIG_IXP4XX_INDIRECT_PCI
  425. __raw_writesb(addr, vaddr, count);
  426. #else
  427. __ixp4xx_writesb(addr, vaddr, count);
  428. #endif
  429. }
  430. static inline void
  431. __ixp4xx_iowrite16(u16 value, void __iomem *addr)
  432. {
  433. unsigned long port = (unsigned long __force)addr;
  434. if (__is_io_address(port))
  435. __ixp4xx_outw(value, port & PIO_MASK);
  436. else
  437. #ifndef CONFIG_IXP4XX_INDIRECT_PCI
  438. __raw_writew(cpu_to_le16(value), addr);
  439. #else
  440. __ixp4xx_writew(value, addr);
  441. #endif
  442. }
  443. static inline void
  444. __ixp4xx_iowrite16_rep(void __iomem *addr, const void *vaddr, u32 count)
  445. {
  446. unsigned long port = (unsigned long __force)addr;
  447. if (__is_io_address(port))
  448. __ixp4xx_outsw(port & PIO_MASK, vaddr, count);
  449. else
  450. #ifndef CONFIG_IXP4XX_INDIRECT_PCI
  451. __raw_writesw(addr, vaddr, count);
  452. #else
  453. __ixp4xx_writesw(addr, vaddr, count);
  454. #endif
  455. }
  456. static inline void
  457. __ixp4xx_iowrite32(u32 value, void __iomem *addr)
  458. {
  459. unsigned long port = (unsigned long __force)addr;
  460. if (__is_io_address(port))
  461. __ixp4xx_outl(value, port & PIO_MASK);
  462. else
  463. #ifndef CONFIG_IXP4XX_INDIRECT_PCI
  464. __raw_writel(cpu_to_le32(value), port);
  465. #else
  466. __ixp4xx_writel(value, addr);
  467. #endif
  468. }
  469. static inline void
  470. __ixp4xx_iowrite32_rep(void __iomem *addr, const void *vaddr, u32 count)
  471. {
  472. unsigned long port = (unsigned long __force)addr;
  473. if (__is_io_address(port))
  474. __ixp4xx_outsl(port & PIO_MASK, vaddr, count);
  475. else
  476. #ifndef CONFIG_IXP4XX_INDIRECT_PCI
  477. __raw_writesl(addr, vaddr, count);
  478. #else
  479. __ixp4xx_writesl(addr, vaddr, count);
  480. #endif
  481. }
  482. #define ioread8(p) __ixp4xx_ioread8(p)
  483. #define ioread16(p) __ixp4xx_ioread16(p)
  484. #define ioread32(p) __ixp4xx_ioread32(p)
  485. #define ioread8_rep(p, v, c) __ixp4xx_ioread8_rep(p, v, c)
  486. #define ioread16_rep(p, v, c) __ixp4xx_ioread16_rep(p, v, c)
  487. #define ioread32_rep(p, v, c) __ixp4xx_ioread32_rep(p, v, c)
  488. #define iowrite8(v,p) __ixp4xx_iowrite8(v,p)
  489. #define iowrite16(v,p) __ixp4xx_iowrite16(v,p)
  490. #define iowrite32(v,p) __ixp4xx_iowrite32(v,p)
  491. #define iowrite8_rep(p, v, c) __ixp4xx_iowrite8_rep(p, v, c)
  492. #define iowrite16_rep(p, v, c) __ixp4xx_iowrite16_rep(p, v, c)
  493. #define iowrite32_rep(p, v, c) __ixp4xx_iowrite32_rep(p, v, c)
  494. #define ioport_map(port, nr) ((void __iomem*)(port + PIO_OFFSET))
  495. #define ioport_unmap(addr)
  496. #endif // !CONFIG_PCI
  497. #endif // __ASM_ARM_ARCH_IO_H