ops-au1000.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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: device %d, status %x\n", device, ((status >> 28) & 0xf));
  154. /* clear errors */
  155. au_writel(status & 0xf000ffff, Au1500_PCI_STATCMD);
  156. *data = 0xffffffff;
  157. error = -1;
  158. }
  159. /* Take away the idsel.
  160. */
  161. if (board_pci_idsel) {
  162. (void)board_pci_idsel(device, 0);
  163. }
  164. local_irq_restore(flags);
  165. return error;
  166. #endif
  167. }
  168. static int read_config_byte(struct pci_bus *bus, unsigned int devfn,
  169. int where, u8 * val)
  170. {
  171. u32 data;
  172. int ret;
  173. ret = config_access(PCI_ACCESS_READ, bus, devfn, where, &data);
  174. if (where & 1)
  175. data >>= 8;
  176. if (where & 2)
  177. data >>= 16;
  178. *val = data & 0xff;
  179. return ret;
  180. }
  181. static int read_config_word(struct pci_bus *bus, unsigned int devfn,
  182. int where, u16 * val)
  183. {
  184. u32 data;
  185. int ret;
  186. ret = config_access(PCI_ACCESS_READ, bus, devfn, where, &data);
  187. if (where & 2)
  188. data >>= 16;
  189. *val = data & 0xffff;
  190. return ret;
  191. }
  192. static int read_config_dword(struct pci_bus *bus, unsigned int devfn,
  193. int where, u32 * val)
  194. {
  195. int ret;
  196. ret = config_access(PCI_ACCESS_READ, bus, devfn, where, val);
  197. return ret;
  198. }
  199. static int
  200. write_config_byte(struct pci_bus *bus, unsigned int devfn, int where,
  201. u8 val)
  202. {
  203. u32 data = 0;
  204. if (config_access(PCI_ACCESS_READ, bus, devfn, where, &data))
  205. return -1;
  206. data = (data & ~(0xff << ((where & 3) << 3))) |
  207. (val << ((where & 3) << 3));
  208. if (config_access(PCI_ACCESS_WRITE, bus, devfn, where, &data))
  209. return -1;
  210. return PCIBIOS_SUCCESSFUL;
  211. }
  212. static int
  213. write_config_word(struct pci_bus *bus, unsigned int devfn, int where,
  214. u16 val)
  215. {
  216. u32 data = 0;
  217. if (config_access(PCI_ACCESS_READ, bus, devfn, where, &data))
  218. return -1;
  219. data = (data & ~(0xffff << ((where & 3) << 3))) |
  220. (val << ((where & 3) << 3));
  221. if (config_access(PCI_ACCESS_WRITE, bus, devfn, where, &data))
  222. return -1;
  223. return PCIBIOS_SUCCESSFUL;
  224. }
  225. static int
  226. write_config_dword(struct pci_bus *bus, unsigned int devfn, int where,
  227. u32 val)
  228. {
  229. if (config_access(PCI_ACCESS_WRITE, bus, devfn, where, &val))
  230. return -1;
  231. return PCIBIOS_SUCCESSFUL;
  232. }
  233. static int config_read(struct pci_bus *bus, unsigned int devfn,
  234. int where, int size, u32 * val)
  235. {
  236. switch (size) {
  237. case 1: {
  238. u8 _val;
  239. int rc = read_config_byte(bus, devfn, where, &_val);
  240. *val = _val;
  241. return rc;
  242. }
  243. case 2: {
  244. u16 _val;
  245. int rc = read_config_word(bus, devfn, where, &_val);
  246. *val = _val;
  247. return rc;
  248. }
  249. default:
  250. return read_config_dword(bus, devfn, where, val);
  251. }
  252. }
  253. static int config_write(struct pci_bus *bus, unsigned int devfn,
  254. int where, int size, u32 val)
  255. {
  256. switch (size) {
  257. case 1:
  258. return write_config_byte(bus, devfn, where, (u8) val);
  259. case 2:
  260. return write_config_word(bus, devfn, where, (u16) val);
  261. default:
  262. return write_config_dword(bus, devfn, where, val);
  263. }
  264. }
  265. struct pci_ops au1x_pci_ops = {
  266. config_read,
  267. config_write
  268. };