ops-au1000.c 8.2 KB

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