ops-au1000.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  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/config.h>
  32. #include <linux/types.h>
  33. #include <linux/pci.h>
  34. #include <linux/kernel.h>
  35. #include <linux/init.h>
  36. #include <linux/vmalloc.h>
  37. #include <asm/mach-au1x00/au1000.h>
  38. #undef DEBUG
  39. #ifdef DEBUG
  40. #define DBG(x...) printk(x)
  41. #else
  42. #define DBG(x...)
  43. #endif
  44. #define PCI_ACCESS_READ 0
  45. #define PCI_ACCESS_WRITE 1
  46. int (*board_pci_idsel)(unsigned int devsel, int assert);
  47. void mod_wired_entry(int entry, unsigned long entrylo0,
  48. unsigned long entrylo1, unsigned long entryhi,
  49. unsigned long pagemask)
  50. {
  51. unsigned long old_pagemask;
  52. unsigned long old_ctx;
  53. /* Save old context and create impossible VPN2 value */
  54. old_ctx = read_c0_entryhi() & 0xff;
  55. old_pagemask = read_c0_pagemask();
  56. write_c0_index(entry);
  57. write_c0_pagemask(pagemask);
  58. write_c0_entryhi(entryhi);
  59. write_c0_entrylo0(entrylo0);
  60. write_c0_entrylo1(entrylo1);
  61. tlb_write_indexed();
  62. write_c0_entryhi(old_ctx);
  63. write_c0_pagemask(old_pagemask);
  64. }
  65. struct vm_struct *pci_cfg_vm;
  66. static int pci_cfg_wired_entry;
  67. static int first_cfg = 1;
  68. unsigned long last_entryLo0, last_entryLo1;
  69. static int config_access(unsigned char access_type, struct pci_bus *bus,
  70. unsigned int dev_fn, unsigned char where,
  71. u32 * data)
  72. {
  73. #if defined( CONFIG_SOC_AU1500 ) || defined( CONFIG_SOC_AU1550 )
  74. unsigned int device = PCI_SLOT(dev_fn);
  75. unsigned int function = PCI_FUNC(dev_fn);
  76. unsigned long offset, status;
  77. unsigned long cfg_base;
  78. unsigned long flags;
  79. int error = PCIBIOS_SUCCESSFUL;
  80. unsigned long entryLo0, entryLo1;
  81. if (device > 19) {
  82. *data = 0xffffffff;
  83. return -1;
  84. }
  85. local_irq_save(flags);
  86. au_writel(((0x2000 << 16) | (au_readl(Au1500_PCI_STATCMD) & 0xffff)),
  87. Au1500_PCI_STATCMD);
  88. au_sync_udelay(1);
  89. /*
  90. * We can't ioremap the entire pci config space because it's
  91. * too large. Nor can we call ioremap dynamically because some
  92. * device drivers use the pci config routines from within
  93. * interrupt handlers and that becomes a problem in get_vm_area().
  94. * We use one wired tlb to handle all config accesses for all
  95. * busses. To improve performance, if the current device
  96. * is the same as the last device accessed, we don't touch the
  97. * tlb.
  98. */
  99. if (first_cfg) {
  100. /* reserve a wired entry for pci config accesses */
  101. first_cfg = 0;
  102. pci_cfg_vm = get_vm_area(0x2000, 0);
  103. if (!pci_cfg_vm)
  104. panic (KERN_ERR "PCI unable to get vm area\n");
  105. pci_cfg_wired_entry = read_c0_wired();
  106. add_wired_entry(0, 0, (unsigned long)pci_cfg_vm->addr, PM_4K);
  107. last_entryLo0 = last_entryLo1 = 0xffffffff;
  108. }
  109. /* Allow board vendors to implement their own off-chip idsel.
  110. * If it doesn't succeed, may as well bail out at this point.
  111. */
  112. if (board_pci_idsel) {
  113. if (board_pci_idsel(device, 1) == 0) {
  114. *data = 0xffffffff;
  115. local_irq_restore(flags);
  116. return -1;
  117. }
  118. }
  119. /* setup the config window */
  120. if (bus->number == 0) {
  121. cfg_base = ((1<<device)<<11);
  122. } else {
  123. cfg_base = 0x80000000 | (bus->number<<16) | (device<<11);
  124. }
  125. /* setup the lower bits of the 36 bit address */
  126. offset = (function << 8) | (where & ~0x3);
  127. /* pick up any address that falls below the page mask */
  128. offset |= cfg_base & ~PAGE_MASK;
  129. /* page boundary */
  130. cfg_base = cfg_base & PAGE_MASK;
  131. entryLo0 = (6 << 26) | (cfg_base >> 6) | (2 << 3) | 7;
  132. entryLo1 = (6 << 26) | (cfg_base >> 6) | (0x1000 >> 6) | (2 << 3) | 7;
  133. if ((entryLo0 != last_entryLo0) || (entryLo1 != last_entryLo1)) {
  134. mod_wired_entry(pci_cfg_wired_entry, entryLo0, entryLo1,
  135. (unsigned long)pci_cfg_vm->addr, PM_4K);
  136. last_entryLo0 = entryLo0;
  137. last_entryLo1 = entryLo1;
  138. }
  139. if (access_type == PCI_ACCESS_WRITE) {
  140. au_writel(*data, (int)(pci_cfg_vm->addr + offset));
  141. } else {
  142. *data = au_readl((int)(pci_cfg_vm->addr + offset));
  143. }
  144. au_sync_udelay(2);
  145. DBG("cfg_access %d bus->number %d dev %d at %x *data %x conf %x\n",
  146. access_type, bus->number, device, where, *data, offset);
  147. /* check master abort */
  148. status = au_readl(Au1500_PCI_STATCMD);
  149. if (status & (1<<29)) {
  150. *data = 0xffffffff;
  151. error = -1;
  152. DBG("Au1x Master Abort\n");
  153. } else if ((status >> 28) & 0xf) {
  154. DBG("PCI ERR detected: status %x\n", status);
  155. *data = 0xffffffff;
  156. error = -1;
  157. }
  158. /* Take away the idsel.
  159. */
  160. if (board_pci_idsel) {
  161. (void)board_pci_idsel(device, 0);
  162. }
  163. local_irq_restore(flags);
  164. return error;
  165. #endif
  166. }
  167. static int read_config_byte(struct pci_bus *bus, unsigned int devfn,
  168. int where, u8 * val)
  169. {
  170. u32 data;
  171. int ret;
  172. ret = config_access(PCI_ACCESS_READ, bus, devfn, where, &data);
  173. if (where & 1)
  174. data >>= 8;
  175. if (where & 2)
  176. data >>= 16;
  177. *val = data & 0xff;
  178. return ret;
  179. }
  180. static int read_config_word(struct pci_bus *bus, unsigned int devfn,
  181. int where, u16 * val)
  182. {
  183. u32 data;
  184. int ret;
  185. ret = config_access(PCI_ACCESS_READ, bus, devfn, where, &data);
  186. if (where & 2)
  187. data >>= 16;
  188. *val = data & 0xffff;
  189. return ret;
  190. }
  191. static int read_config_dword(struct pci_bus *bus, unsigned int devfn,
  192. int where, u32 * val)
  193. {
  194. int ret;
  195. ret = config_access(PCI_ACCESS_READ, bus, devfn, where, val);
  196. return ret;
  197. }
  198. static int
  199. write_config_byte(struct pci_bus *bus, unsigned int devfn, int where,
  200. u8 val)
  201. {
  202. u32 data = 0;
  203. if (config_access(PCI_ACCESS_READ, bus, devfn, where, &data))
  204. return -1;
  205. data = (data & ~(0xff << ((where & 3) << 3))) |
  206. (val << ((where & 3) << 3));
  207. if (config_access(PCI_ACCESS_WRITE, bus, devfn, where, &data))
  208. return -1;
  209. return PCIBIOS_SUCCESSFUL;
  210. }
  211. static int
  212. write_config_word(struct pci_bus *bus, unsigned int devfn, int where,
  213. u16 val)
  214. {
  215. u32 data = 0;
  216. if (config_access(PCI_ACCESS_READ, bus, devfn, where, &data))
  217. return -1;
  218. data = (data & ~(0xffff << ((where & 3) << 3))) |
  219. (val << ((where & 3) << 3));
  220. if (config_access(PCI_ACCESS_WRITE, bus, devfn, where, &data))
  221. return -1;
  222. return PCIBIOS_SUCCESSFUL;
  223. }
  224. static int
  225. write_config_dword(struct pci_bus *bus, unsigned int devfn, int where,
  226. u32 val)
  227. {
  228. if (config_access(PCI_ACCESS_WRITE, bus, devfn, where, &val))
  229. return -1;
  230. return PCIBIOS_SUCCESSFUL;
  231. }
  232. static int config_read(struct pci_bus *bus, unsigned int devfn,
  233. int where, int size, u32 * val)
  234. {
  235. switch (size) {
  236. case 1: {
  237. u8 _val;
  238. int rc = read_config_byte(bus, devfn, where, &_val);
  239. *val = _val;
  240. return rc;
  241. }
  242. case 2: {
  243. u16 _val;
  244. int rc = read_config_word(bus, devfn, where, &_val);
  245. *val = _val;
  246. return rc;
  247. }
  248. default:
  249. return read_config_dword(bus, devfn, where, val);
  250. }
  251. }
  252. static int config_write(struct pci_bus *bus, unsigned int devfn,
  253. int where, int size, u32 val)
  254. {
  255. switch (size) {
  256. case 1:
  257. return write_config_byte(bus, devfn, where, (u8) val);
  258. case 2:
  259. return write_config_word(bus, devfn, where, (u16) val);
  260. default:
  261. return write_config_dword(bus, devfn, where, val);
  262. }
  263. }
  264. struct pci_ops au1x_pci_ops = {
  265. config_read,
  266. config_write
  267. };