scc_epci.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  1. /*
  2. * Support for SCC external PCI
  3. *
  4. * (C) Copyright 2004-2007 TOSHIBA CORPORATION
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  19. */
  20. #undef DEBUG
  21. #include <linux/kernel.h>
  22. #include <linux/threads.h>
  23. #include <linux/pci.h>
  24. #include <linux/init.h>
  25. #include <linux/pci_regs.h>
  26. #include <linux/bootmem.h>
  27. #include <asm/io.h>
  28. #include <asm/irq.h>
  29. #include <asm/prom.h>
  30. #include <asm/machdep.h>
  31. #include <asm/pci-bridge.h>
  32. #include <asm/ppc-pci.h>
  33. #include "scc.h"
  34. #include "pci.h"
  35. #include "interrupt.h"
  36. #define MAX_PCI_DEVICES 32
  37. #define MAX_PCI_FUNCTIONS 8
  38. #define iob() __asm__ __volatile__("eieio; sync":::"memory")
  39. #if 0 /* test code for epci dummy read */
  40. static void celleb_epci_dummy_read(struct pci_dev *dev)
  41. {
  42. void __iomem *epci_base;
  43. struct device_node *node;
  44. struct pci_controller *hose;
  45. u32 val;
  46. node = (struct device_node *)dev->bus->sysdata;
  47. hose = pci_find_hose_for_OF_device(node);
  48. if (!hose)
  49. return;
  50. epci_base = hose->cfg_addr;
  51. val = in_be32(epci_base + SCC_EPCI_WATRP);
  52. iosync();
  53. return;
  54. }
  55. #endif
  56. static inline void clear_and_disable_master_abort_interrupt(
  57. struct pci_controller *hose)
  58. {
  59. void __iomem *addr;
  60. addr = hose->cfg_addr + PCI_COMMAND;
  61. out_be32(addr, in_be32(addr) | (PCI_STATUS_REC_MASTER_ABORT << 16));
  62. }
  63. static int celleb_epci_check_abort(struct pci_controller *hose,
  64. void __iomem *addr)
  65. {
  66. void __iomem *reg, *epci_base;
  67. u32 val;
  68. iob();
  69. epci_base = hose->cfg_addr;
  70. reg = epci_base + PCI_COMMAND;
  71. val = in_be32(reg);
  72. if (val & (PCI_STATUS_REC_MASTER_ABORT << 16)) {
  73. out_be32(reg,
  74. (val & 0xffff) | (PCI_STATUS_REC_MASTER_ABORT << 16));
  75. /* clear PCI Controller error, FRE, PMFE */
  76. reg = epci_base + SCC_EPCI_STATUS;
  77. out_be32(reg, SCC_EPCI_INT_PAI);
  78. reg = epci_base + SCC_EPCI_VCSR;
  79. val = in_be32(reg) & 0xffff;
  80. val |= SCC_EPCI_VCSR_FRE;
  81. out_be32(reg, val);
  82. reg = epci_base + SCC_EPCI_VISTAT;
  83. out_be32(reg, SCC_EPCI_VISTAT_PMFE);
  84. return PCIBIOS_DEVICE_NOT_FOUND;
  85. }
  86. return PCIBIOS_SUCCESSFUL;
  87. }
  88. static void __iomem *celleb_epci_make_config_addr(struct pci_controller *hose,
  89. unsigned int devfn, int where)
  90. {
  91. void __iomem *addr;
  92. struct pci_bus *bus = hose->bus;
  93. if (bus->self)
  94. addr = hose->cfg_data +
  95. (((bus->number & 0xff) << 16)
  96. | ((devfn & 0xff) << 8)
  97. | (where & 0xff)
  98. | 0x01000000);
  99. else
  100. addr = hose->cfg_data +
  101. (((devfn & 0xff) << 8) | (where & 0xff));
  102. pr_debug("EPCI: config_addr = 0x%p\n", addr);
  103. return addr;
  104. }
  105. static int celleb_epci_read_config(struct pci_bus *bus,
  106. unsigned int devfn, int where, int size, u32 * val)
  107. {
  108. void __iomem *addr;
  109. struct device_node *node;
  110. struct pci_controller *hose;
  111. /* allignment check */
  112. BUG_ON(where % size);
  113. node = (struct device_node *)bus->sysdata;
  114. hose = pci_find_hose_for_OF_device(node);
  115. if (!hose->cfg_data)
  116. return PCIBIOS_DEVICE_NOT_FOUND;
  117. if (bus->number == hose->first_busno && devfn == 0) {
  118. /* EPCI controller self */
  119. addr = hose->cfg_addr + where;
  120. switch (size) {
  121. case 1:
  122. *val = in_8(addr);
  123. break;
  124. case 2:
  125. *val = in_be16(addr);
  126. break;
  127. case 4:
  128. *val = in_be32(addr);
  129. break;
  130. default:
  131. return PCIBIOS_DEVICE_NOT_FOUND;
  132. }
  133. } else {
  134. clear_and_disable_master_abort_interrupt(hose);
  135. addr = celleb_epci_make_config_addr(hose, devfn, where);
  136. switch (size) {
  137. case 1:
  138. *val = in_8(addr);
  139. break;
  140. case 2:
  141. *val = in_le16(addr);
  142. break;
  143. case 4:
  144. *val = in_le32(addr);
  145. break;
  146. default:
  147. return PCIBIOS_DEVICE_NOT_FOUND;
  148. }
  149. }
  150. pr_debug("EPCI: "
  151. "addr=0x%lx, devfn=0x%x, where=0x%x, size=0x%x, val=0x%x\n",
  152. addr, devfn, where, size, *val);
  153. return celleb_epci_check_abort(hose, NULL);
  154. }
  155. static int celleb_epci_write_config(struct pci_bus *bus,
  156. unsigned int devfn, int where, int size, u32 val)
  157. {
  158. void __iomem *addr;
  159. struct device_node *node;
  160. struct pci_controller *hose;
  161. /* allignment check */
  162. BUG_ON(where % size);
  163. node = (struct device_node *)bus->sysdata;
  164. hose = pci_find_hose_for_OF_device(node);
  165. if (!hose->cfg_data)
  166. return PCIBIOS_DEVICE_NOT_FOUND;
  167. if (bus->number == hose->first_busno && devfn == 0) {
  168. /* EPCI controller self */
  169. addr = hose->cfg_addr + where;
  170. switch (size) {
  171. case 1:
  172. out_8(addr, val);
  173. break;
  174. case 2:
  175. out_be16(addr, val);
  176. break;
  177. case 4:
  178. out_be32(addr, val);
  179. break;
  180. default:
  181. return PCIBIOS_DEVICE_NOT_FOUND;
  182. }
  183. } else {
  184. clear_and_disable_master_abort_interrupt(hose);
  185. addr = celleb_epci_make_config_addr(hose, devfn, where);
  186. switch (size) {
  187. case 1:
  188. out_8(addr, val);
  189. break;
  190. case 2:
  191. out_le16(addr, val);
  192. break;
  193. case 4:
  194. out_le32(addr, val);
  195. break;
  196. default:
  197. return PCIBIOS_DEVICE_NOT_FOUND;
  198. }
  199. }
  200. return celleb_epci_check_abort(hose, addr);
  201. }
  202. struct pci_ops celleb_epci_ops = {
  203. celleb_epci_read_config,
  204. celleb_epci_write_config,
  205. };
  206. /* to be moved in FW */
  207. static int __devinit celleb_epci_init(struct pci_controller *hose)
  208. {
  209. u32 val;
  210. void __iomem *reg, *epci_base;
  211. int hwres = 0;
  212. epci_base = hose->cfg_addr;
  213. /* PCI core reset(Internal bus and PCI clock) */
  214. reg = epci_base + SCC_EPCI_CKCTRL;
  215. val = in_be32(reg);
  216. if (val == 0x00030101)
  217. hwres = 1;
  218. else {
  219. val &= ~(SCC_EPCI_CKCTRL_CRST0 | SCC_EPCI_CKCTRL_CRST1);
  220. out_be32(reg, val);
  221. /* set PCI core clock */
  222. val = in_be32(reg);
  223. val |= (SCC_EPCI_CKCTRL_OCLKEN | SCC_EPCI_CKCTRL_LCLKEN);
  224. out_be32(reg, val);
  225. /* release PCI core reset (internal bus) */
  226. val = in_be32(reg);
  227. val |= SCC_EPCI_CKCTRL_CRST0;
  228. out_be32(reg, val);
  229. /* set PCI clock select */
  230. reg = epci_base + SCC_EPCI_CLKRST;
  231. val = in_be32(reg);
  232. val &= ~SCC_EPCI_CLKRST_CKS_MASK;
  233. val |= SCC_EPCI_CLKRST_CKS_2;
  234. out_be32(reg, val);
  235. /* set arbiter */
  236. reg = epci_base + SCC_EPCI_ABTSET;
  237. out_be32(reg, 0x0f1f001f); /* temporary value */
  238. /* buffer on */
  239. reg = epci_base + SCC_EPCI_CLKRST;
  240. val = in_be32(reg);
  241. val |= SCC_EPCI_CLKRST_BC;
  242. out_be32(reg, val);
  243. /* PCI clock enable */
  244. val = in_be32(reg);
  245. val |= SCC_EPCI_CLKRST_PCKEN;
  246. out_be32(reg, val);
  247. /* release PCI core reset (all) */
  248. reg = epci_base + SCC_EPCI_CKCTRL;
  249. val = in_be32(reg);
  250. val |= (SCC_EPCI_CKCTRL_CRST0 | SCC_EPCI_CKCTRL_CRST1);
  251. out_be32(reg, val);
  252. /* set base translation registers. (already set by Beat) */
  253. /* set base address masks. (already set by Beat) */
  254. }
  255. /* release interrupt masks and clear all interrupts */
  256. reg = epci_base + SCC_EPCI_INTSET;
  257. out_be32(reg, 0x013f011f); /* all interrupts enable */
  258. reg = epci_base + SCC_EPCI_VIENAB;
  259. val = SCC_EPCI_VIENAB_PMPEE | SCC_EPCI_VIENAB_PMFEE;
  260. out_be32(reg, val);
  261. reg = epci_base + SCC_EPCI_STATUS;
  262. out_be32(reg, 0xffffffff);
  263. reg = epci_base + SCC_EPCI_VISTAT;
  264. out_be32(reg, 0xffffffff);
  265. /* disable PCI->IB address translation */
  266. reg = epci_base + SCC_EPCI_VCSR;
  267. val = in_be32(reg);
  268. val &= ~(SCC_EPCI_VCSR_DR | SCC_EPCI_VCSR_AT);
  269. out_be32(reg, val);
  270. /* set base addresses. (no need to set?) */
  271. /* memory space, bus master enable */
  272. reg = epci_base + PCI_COMMAND;
  273. val = PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER;
  274. out_be32(reg, val);
  275. /* endian mode setup */
  276. reg = epci_base + SCC_EPCI_ECMODE;
  277. val = 0x00550155;
  278. out_be32(reg, val);
  279. /* set control option */
  280. reg = epci_base + SCC_EPCI_CNTOPT;
  281. val = in_be32(reg);
  282. val |= SCC_EPCI_CNTOPT_O2PMB;
  283. out_be32(reg, val);
  284. /* XXX: temporay: set registers for address conversion setup */
  285. reg = epci_base + SCC_EPCI_CNF10_REG;
  286. out_be32(reg, 0x80000008);
  287. reg = epci_base + SCC_EPCI_CNF14_REG;
  288. out_be32(reg, 0x40000008);
  289. reg = epci_base + SCC_EPCI_BAM0;
  290. out_be32(reg, 0x80000000);
  291. reg = epci_base + SCC_EPCI_BAM1;
  292. out_be32(reg, 0xe0000000);
  293. reg = epci_base + SCC_EPCI_PVBAT;
  294. out_be32(reg, 0x80000000);
  295. if (!hwres) {
  296. /* release external PCI reset */
  297. reg = epci_base + SCC_EPCI_CLKRST;
  298. val = in_be32(reg);
  299. val |= SCC_EPCI_CLKRST_PCIRST;
  300. out_be32(reg, val);
  301. }
  302. return 0;
  303. }
  304. int __devinit celleb_setup_epci(struct device_node *node,
  305. struct pci_controller *hose)
  306. {
  307. struct resource r;
  308. pr_debug("PCI: celleb_setup_epci()\n");
  309. if (of_address_to_resource(node, 0, &r))
  310. goto error;
  311. hose->cfg_addr = ioremap(r.start, (r.end - r.start + 1));
  312. if (!hose->cfg_addr)
  313. goto error;
  314. pr_debug("EPCI: cfg_addr map 0x%016lx->0x%016lx + 0x%016lx\n",
  315. r.start, (unsigned long)hose->cfg_addr,
  316. (r.end - r.start + 1));
  317. if (of_address_to_resource(node, 2, &r))
  318. goto error;
  319. hose->cfg_data = ioremap(r.start, (r.end - r.start + 1));
  320. if (!hose->cfg_data)
  321. goto error;
  322. pr_debug("EPCI: cfg_data map 0x%016lx->0x%016lx + 0x%016lx\n",
  323. r.start, (unsigned long)hose->cfg_data,
  324. (r.end - r.start + 1));
  325. celleb_epci_init(hose);
  326. return 0;
  327. error:
  328. return 1;
  329. }