ops-rc32434.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. /*
  2. * BRIEF MODULE DESCRIPTION
  3. * pci_ops for IDT EB434 board
  4. *
  5. * Copyright 2004 IDT Inc. (rischelp@idt.com)
  6. * Copyright 2006 Felix Fietkau <nbd@openwrt.org>
  7. *
  8. * This program is free software; you can redistribute it and/or modify it
  9. * under the terms of the GNU General Public License as published by the
  10. * Free Software Foundation; either version 2 of the License, or (at your
  11. * option) any later version.
  12. *
  13. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
  14. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  15. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
  16. * NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  17. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  18. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  19. * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  20. * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  21. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  22. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  23. *
  24. * You should have received a copy of the GNU General Public License along
  25. * with this program; if not, write to the Free Software Foundation, Inc.,
  26. * 675 Mass Ave, Cambridge, MA 02139, USA.
  27. */
  28. #include <linux/delay.h>
  29. #include <linux/init.h>
  30. #include <linux/io.h>
  31. #include <linux/pci.h>
  32. #include <linux/types.h>
  33. #include <asm/cpu.h>
  34. #include <asm/mach-rc32434/rc32434.h>
  35. #include <asm/mach-rc32434/pci.h>
  36. #define PCI_ACCESS_READ 0
  37. #define PCI_ACCESS_WRITE 1
  38. #define PCI_CFG_SET(bus, slot, func, off) \
  39. (rc32434_pci->pcicfga = (0x80000000 | \
  40. ((bus) << 16) | ((slot)<<11) | \
  41. ((func)<<8) | (off)))
  42. static inline int config_access(unsigned char access_type,
  43. struct pci_bus *bus, unsigned int devfn,
  44. unsigned char where, u32 *data)
  45. {
  46. unsigned int slot = PCI_SLOT(devfn);
  47. u8 func = PCI_FUNC(devfn);
  48. /* Setup address */
  49. PCI_CFG_SET(bus->number, slot, func, where);
  50. rc32434_sync();
  51. if (access_type == PCI_ACCESS_WRITE)
  52. rc32434_pci->pcicfgd = *data;
  53. else
  54. *data = rc32434_pci->pcicfgd;
  55. rc32434_sync();
  56. return 0;
  57. }
  58. /*
  59. * We can't address 8 and 16 bit words directly. Instead we have to
  60. * read/write a 32bit word and mask/modify the data we actually want.
  61. */
  62. static int read_config_byte(struct pci_bus *bus, unsigned int devfn,
  63. int where, u8 *val)
  64. {
  65. u32 data;
  66. int ret;
  67. ret = config_access(PCI_ACCESS_READ, bus, devfn, where, &data);
  68. *val = (data >> ((where & 3) << 3)) & 0xff;
  69. return ret;
  70. }
  71. static int read_config_word(struct pci_bus *bus, unsigned int devfn,
  72. int where, u16 *val)
  73. {
  74. u32 data;
  75. int ret;
  76. ret = config_access(PCI_ACCESS_READ, bus, devfn, where, &data);
  77. *val = (data >> ((where & 3) << 3)) & 0xffff;
  78. return ret;
  79. }
  80. static int read_config_dword(struct pci_bus *bus, unsigned int devfn,
  81. int where, u32 *val)
  82. {
  83. int ret;
  84. int delay = 1;
  85. /*
  86. * Don't scan too far, else there will be errors with plugged in
  87. * daughterboard (rb564).
  88. */
  89. if (bus->number == 0 && PCI_SLOT(devfn) > 21)
  90. return 0;
  91. retry:
  92. ret = config_access(PCI_ACCESS_READ, bus, devfn, where, val);
  93. /*
  94. * Certain devices react delayed at device scan time, this
  95. * gives them time to settle
  96. */
  97. if (where == PCI_VENDOR_ID) {
  98. if (ret == 0xffffffff || ret == 0x00000000 ||
  99. ret == 0x0000ffff || ret == 0xffff0000) {
  100. if (delay > 4)
  101. return 0;
  102. delay *= 2;
  103. msleep(delay);
  104. goto retry;
  105. }
  106. }
  107. return ret;
  108. }
  109. static int
  110. write_config_byte(struct pci_bus *bus, unsigned int devfn, int where,
  111. u8 val)
  112. {
  113. u32 data = 0;
  114. if (config_access(PCI_ACCESS_READ, bus, devfn, where, &data))
  115. return -1;
  116. data = (data & ~(0xff << ((where & 3) << 3))) |
  117. (val << ((where & 3) << 3));
  118. if (config_access(PCI_ACCESS_WRITE, bus, devfn, where, &data))
  119. return -1;
  120. return PCIBIOS_SUCCESSFUL;
  121. }
  122. static int
  123. write_config_word(struct pci_bus *bus, unsigned int devfn, int where,
  124. u16 val)
  125. {
  126. u32 data = 0;
  127. if (config_access(PCI_ACCESS_READ, bus, devfn, where, &data))
  128. return -1;
  129. data = (data & ~(0xffff << ((where & 3) << 3))) |
  130. (val << ((where & 3) << 3));
  131. if (config_access(PCI_ACCESS_WRITE, bus, devfn, where, &data))
  132. return -1;
  133. return PCIBIOS_SUCCESSFUL;
  134. }
  135. static int
  136. write_config_dword(struct pci_bus *bus, unsigned int devfn, int where,
  137. u32 val)
  138. {
  139. if (config_access(PCI_ACCESS_WRITE, bus, devfn, where, &val))
  140. return -1;
  141. return PCIBIOS_SUCCESSFUL;
  142. }
  143. static int pci_config_read(struct pci_bus *bus, unsigned int devfn,
  144. int where, int size, u32 *val)
  145. {
  146. switch (size) {
  147. case 1:
  148. return read_config_byte(bus, devfn, where, (u8 *) val);
  149. case 2:
  150. return read_config_word(bus, devfn, where, (u16 *) val);
  151. default:
  152. return read_config_dword(bus, devfn, where, val);
  153. }
  154. }
  155. static int pci_config_write(struct pci_bus *bus, unsigned int devfn,
  156. int where, int size, u32 val)
  157. {
  158. switch (size) {
  159. case 1:
  160. return write_config_byte(bus, devfn, where, (u8) val);
  161. case 2:
  162. return write_config_word(bus, devfn, where, (u16) val);
  163. default:
  164. return write_config_dword(bus, devfn, where, val);
  165. }
  166. }
  167. struct pci_ops rc32434_pci_ops = {
  168. .read = pci_config_read,
  169. .write = pci_config_write,
  170. };