ops-au1000.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. /*
  2. * BRIEF MODULE DESCRIPTION
  3. * Alchemy/AMD Au1x00 pci support.
  4. *
  5. * Copyright 2001,2002,2003 MontaVista Software Inc.
  6. * Author: MontaVista Software, Inc.
  7. * ppopov@mvista.com or source@mvista.com
  8. *
  9. * Support for all devices (greater than 16) added by David Gathright.
  10. *
  11. * This program is free software; you can redistribute it and/or modify it
  12. * under the terms of the GNU General Public License as published by the
  13. * Free Software Foundation; either version 2 of the License, or (at your
  14. * option) any later version.
  15. *
  16. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
  17. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  18. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
  19. * NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  20. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  21. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  22. * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  23. * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  25. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. *
  27. * You should have received a copy of the GNU General Public License along
  28. * with this program; if not, write to the Free Software Foundation, Inc.,
  29. * 675 Mass Ave, Cambridge, MA 02139, USA.
  30. */
  31. #include <linux/types.h>
  32. #include <linux/pci.h>
  33. #include <linux/kernel.h>
  34. #include <linux/init.h>
  35. #include <linux/vmalloc.h>
  36. #include <asm/mach-au1x00/au1000.h>
  37. #undef DEBUG
  38. #ifdef DEBUG
  39. #define DBG(x...) printk(x)
  40. #else
  41. #define DBG(x...)
  42. #endif
  43. #define PCI_ACCESS_READ 0
  44. #define PCI_ACCESS_WRITE 1
  45. int (*board_pci_idsel)(unsigned int devsel, int assert);
  46. void mod_wired_entry(int entry, unsigned long entrylo0,
  47. unsigned long entrylo1, unsigned long entryhi,
  48. unsigned long pagemask)
  49. {
  50. unsigned long old_pagemask;
  51. unsigned long old_ctx;
  52. /* Save old context and create impossible VPN2 value */
  53. old_ctx = read_c0_entryhi() & 0xff;
  54. old_pagemask = read_c0_pagemask();
  55. write_c0_index(entry);
  56. write_c0_pagemask(pagemask);
  57. write_c0_entryhi(entryhi);
  58. write_c0_entrylo0(entrylo0);
  59. write_c0_entrylo1(entrylo1);
  60. tlb_write_indexed();
  61. write_c0_entryhi(old_ctx);
  62. write_c0_pagemask(old_pagemask);
  63. }
  64. struct vm_struct *pci_cfg_vm;
  65. static int pci_cfg_wired_entry;
  66. static int first_cfg = 1;
  67. unsigned long last_entryLo0, last_entryLo1;
  68. static int config_access(unsigned char access_type, struct pci_bus *bus,
  69. unsigned int dev_fn, unsigned char where,
  70. u32 * data)
  71. {
  72. #if defined( CONFIG_SOC_AU1500 ) || defined( CONFIG_SOC_AU1550 )
  73. unsigned int device = PCI_SLOT(dev_fn);
  74. unsigned int function = PCI_FUNC(dev_fn);
  75. unsigned long offset, status;
  76. unsigned long cfg_base;
  77. unsigned long flags;
  78. int error = PCIBIOS_SUCCESSFUL;
  79. unsigned long entryLo0, entryLo1;
  80. if (device > 19) {
  81. *data = 0xffffffff;
  82. return -1;
  83. }
  84. local_irq_save(flags);
  85. au_writel(((0x2000 << 16) | (au_readl(Au1500_PCI_STATCMD) & 0xffff)),
  86. Au1500_PCI_STATCMD);
  87. au_sync_udelay(1);
  88. /*
  89. * We can't ioremap the entire pci config space because it's
  90. * too large. Nor can we call ioremap dynamically because some
  91. * device drivers use the pci config routines from within
  92. * interrupt handlers and that becomes a problem in get_vm_area().
  93. * We use one wired tlb to handle all config accesses for all
  94. * busses. To improve performance, if the current device
  95. * is the same as the last device accessed, we don't touch the
  96. * tlb.
  97. */
  98. if (first_cfg) {
  99. /* reserve a wired entry for pci config accesses */
  100. first_cfg = 0;
  101. pci_cfg_vm = get_vm_area(0x2000, VM_IOREMAP);
  102. if (!pci_cfg_vm)
  103. panic (KERN_ERR "PCI unable to get vm area\n");
  104. pci_cfg_wired_entry = read_c0_wired();
  105. add_wired_entry(0, 0, (unsigned long)pci_cfg_vm->addr, PM_4K);
  106. last_entryLo0 = last_entryLo1 = 0xffffffff;
  107. }
  108. /* Allow board vendors to implement their own off-chip idsel.
  109. * If it doesn't succeed, may as well bail out at this point.
  110. */
  111. if (board_pci_idsel) {
  112. if (board_pci_idsel(device, 1) == 0) {
  113. *data = 0xffffffff;
  114. local_irq_restore(flags);
  115. return -1;
  116. }
  117. }
  118. /* setup the config window */
  119. if (bus->number == 0) {
  120. cfg_base = ((1<<device)<<11);
  121. } else {
  122. cfg_base = 0x80000000 | (bus->number<<16) | (device<<11);
  123. }
  124. /* setup the lower bits of the 36 bit address */
  125. offset = (function << 8) | (where & ~0x3);
  126. /* pick up any address that falls below the page mask */
  127. offset |= cfg_base & ~PAGE_MASK;
  128. /* page boundary */
  129. cfg_base = cfg_base & PAGE_MASK;
  130. entryLo0 = (6 << 26) | (cfg_base >> 6) | (2 << 3) | 7;
  131. entryLo1 = (6 << 26) | (cfg_base >> 6) | (0x1000 >> 6) | (2 << 3) | 7;
  132. if ((entryLo0 != last_entryLo0) || (entryLo1 != last_entryLo1)) {
  133. mod_wired_entry(pci_cfg_wired_entry, entryLo0, entryLo1,
  134. (unsigned long)pci_cfg_vm->addr, PM_4K);
  135. last_entryLo0 = entryLo0;
  136. last_entryLo1 = entryLo1;
  137. }
  138. if (access_type == PCI_ACCESS_WRITE) {
  139. au_writel(*data, (int)(pci_cfg_vm->addr + offset));
  140. } else {
  141. *data = au_readl((int)(pci_cfg_vm->addr + offset));
  142. }
  143. au_sync_udelay(2);
  144. DBG("cfg_access %d bus->number %d dev %d at %x *data %x conf %x\n",
  145. access_type, bus->number, device, where, *data, offset);
  146. /* check master abort */
  147. status = au_readl(Au1500_PCI_STATCMD);
  148. if (status & (1<<29)) {
  149. *data = 0xffffffff;
  150. error = -1;
  151. DBG("Au1x Master Abort\n");
  152. } else if ((status >> 28) & 0xf) {
  153. DBG("PCI ERR detected: status %x\n", status);
  154. *data = 0xffffffff;
  155. error = -1;
  156. }
  157. /* Take away the idsel.
  158. */
  159. if (board_pci_idsel) {
  160. (void)board_pci_idsel(device, 0);
  161. }
  162. local_irq_restore(flags);
  163. return error;
  164. #endif
  165. }
  166. static int read_config_byte(struct pci_bus *bus, unsigned int devfn,
  167. int where, u8 * val)
  168. {
  169. u32 data;
  170. int ret;
  171. ret = config_access(PCI_ACCESS_READ, bus, devfn, where, &data);
  172. if (where & 1)
  173. data >>= 8;
  174. if (where & 2)
  175. data >>= 16;
  176. *val = data & 0xff;
  177. return ret;
  178. }
  179. static int read_config_word(struct pci_bus *bus, unsigned int devfn,
  180. int where, u16 * val)
  181. {
  182. u32 data;
  183. int ret;
  184. ret = config_access(PCI_ACCESS_READ, bus, devfn, where, &data);
  185. if (where & 2)
  186. data >>= 16;
  187. *val = data & 0xffff;
  188. return ret;
  189. }
  190. static int read_config_dword(struct pci_bus *bus, unsigned int devfn,
  191. int where, u32 * val)
  192. {
  193. int ret;
  194. ret = config_access(PCI_ACCESS_READ, bus, devfn, where, val);
  195. return ret;
  196. }
  197. static int
  198. write_config_byte(struct pci_bus *bus, unsigned int devfn, int where,
  199. u8 val)
  200. {
  201. u32 data = 0;
  202. if (config_access(PCI_ACCESS_READ, bus, devfn, where, &data))
  203. return -1;
  204. data = (data & ~(0xff << ((where & 3) << 3))) |
  205. (val << ((where & 3) << 3));
  206. if (config_access(PCI_ACCESS_WRITE, bus, devfn, where, &data))
  207. return -1;
  208. return PCIBIOS_SUCCESSFUL;
  209. }
  210. static int
  211. write_config_word(struct pci_bus *bus, unsigned int devfn, int where,
  212. u16 val)
  213. {
  214. u32 data = 0;
  215. if (config_access(PCI_ACCESS_READ, bus, devfn, where, &data))
  216. return -1;
  217. data = (data & ~(0xffff << ((where & 3) << 3))) |
  218. (val << ((where & 3) << 3));
  219. if (config_access(PCI_ACCESS_WRITE, bus, devfn, where, &data))
  220. return -1;
  221. return PCIBIOS_SUCCESSFUL;
  222. }
  223. static int
  224. write_config_dword(struct pci_bus *bus, unsigned int devfn, int where,
  225. u32 val)
  226. {
  227. if (config_access(PCI_ACCESS_WRITE, bus, devfn, where, &val))
  228. return -1;
  229. return PCIBIOS_SUCCESSFUL;
  230. }
  231. static int config_read(struct pci_bus *bus, unsigned int devfn,
  232. int where, int size, u32 * val)
  233. {
  234. switch (size) {
  235. case 1: {
  236. u8 _val;
  237. int rc = read_config_byte(bus, devfn, where, &_val);
  238. *val = _val;
  239. return rc;
  240. }
  241. case 2: {
  242. u16 _val;
  243. int rc = read_config_word(bus, devfn, where, &_val);
  244. *val = _val;
  245. return rc;
  246. }
  247. default:
  248. return read_config_dword(bus, devfn, where, val);
  249. }
  250. }
  251. static int config_write(struct pci_bus *bus, unsigned int devfn,
  252. int where, int size, u32 val)
  253. {
  254. switch (size) {
  255. case 1:
  256. return write_config_byte(bus, devfn, where, (u8) val);
  257. case 2:
  258. return write_config_word(bus, devfn, where, (u16) val);
  259. default:
  260. return write_config_dword(bus, devfn, where, val);
  261. }
  262. }
  263. struct pci_ops au1x_pci_ops = {
  264. config_read,
  265. config_write
  266. };