ops-au1000.c 8.5 KB

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