scc_epci.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  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. static inline volatile void __iomem *celleb_epci_get_epci_base(
  40. struct pci_controller *hose)
  41. {
  42. /*
  43. * Note:
  44. * Celleb epci uses cfg_addr as a base address for
  45. * epci control registers.
  46. */
  47. return hose->cfg_addr;
  48. }
  49. static inline volatile void __iomem *celleb_epci_get_epci_cfg(
  50. struct pci_controller *hose)
  51. {
  52. /*
  53. * Note:
  54. * Celleb epci uses cfg_data as a base address for
  55. * configuration area for epci devices.
  56. */
  57. return hose->cfg_data;
  58. }
  59. #if 0 /* test code for epci dummy read */
  60. static void celleb_epci_dummy_read(struct pci_dev *dev)
  61. {
  62. volatile void __iomem *epci_base;
  63. struct device_node *node;
  64. struct pci_controller *hose;
  65. u32 val;
  66. node = (struct device_node *)dev->bus->sysdata;
  67. hose = pci_find_hose_for_OF_device(node);
  68. if (!hose)
  69. return;
  70. epci_base = celleb_epci_get_epci_base(hose);
  71. val = in_be32(epci_base + SCC_EPCI_WATRP);
  72. iosync();
  73. return;
  74. }
  75. #endif
  76. static inline void clear_and_disable_master_abort_interrupt(
  77. struct pci_controller *hose)
  78. {
  79. volatile void __iomem *epci_base, *reg;
  80. epci_base = celleb_epci_get_epci_base(hose);
  81. reg = epci_base + PCI_COMMAND;
  82. out_be32(reg, in_be32(reg) | (PCI_STATUS_REC_MASTER_ABORT << 16));
  83. }
  84. static int celleb_epci_check_abort(struct pci_controller *hose,
  85. volatile void __iomem *addr)
  86. {
  87. volatile void __iomem *reg, *epci_base;
  88. u32 val;
  89. iob();
  90. epci_base = celleb_epci_get_epci_base(hose);
  91. reg = epci_base + PCI_COMMAND;
  92. val = in_be32(reg);
  93. if (val & (PCI_STATUS_REC_MASTER_ABORT << 16)) {
  94. out_be32(reg,
  95. (val & 0xffff) | (PCI_STATUS_REC_MASTER_ABORT << 16));
  96. /* clear PCI Controller error, FRE, PMFE */
  97. reg = epci_base + SCC_EPCI_STATUS;
  98. out_be32(reg, SCC_EPCI_INT_PAI);
  99. reg = epci_base + SCC_EPCI_VCSR;
  100. val = in_be32(reg) & 0xffff;
  101. val |= SCC_EPCI_VCSR_FRE;
  102. out_be32(reg, val);
  103. reg = epci_base + SCC_EPCI_VISTAT;
  104. out_be32(reg, SCC_EPCI_VISTAT_PMFE);
  105. return PCIBIOS_DEVICE_NOT_FOUND;
  106. }
  107. return PCIBIOS_SUCCESSFUL;
  108. }
  109. static volatile void __iomem *celleb_epci_make_config_addr(
  110. struct pci_bus *bus,
  111. struct pci_controller *hose,
  112. unsigned int devfn, int where)
  113. {
  114. volatile void __iomem *addr;
  115. if (bus != hose->bus)
  116. addr = celleb_epci_get_epci_cfg(hose) +
  117. (((bus->number & 0xff) << 16)
  118. | ((devfn & 0xff) << 8)
  119. | (where & 0xff)
  120. | 0x01000000);
  121. else
  122. addr = celleb_epci_get_epci_cfg(hose) +
  123. (((devfn & 0xff) << 8) | (where & 0xff));
  124. pr_debug("EPCI: config_addr = 0x%p\n", addr);
  125. return addr;
  126. }
  127. static int celleb_epci_read_config(struct pci_bus *bus,
  128. unsigned int devfn, int where, int size, u32 * val)
  129. {
  130. volatile void __iomem *epci_base, *addr;
  131. struct device_node *node;
  132. struct pci_controller *hose;
  133. /* allignment check */
  134. BUG_ON(where % size);
  135. node = (struct device_node *)bus->sysdata;
  136. hose = pci_find_hose_for_OF_device(node);
  137. if (!celleb_epci_get_epci_cfg(hose))
  138. return PCIBIOS_DEVICE_NOT_FOUND;
  139. if (bus->number == hose->first_busno && devfn == 0) {
  140. /* EPCI controller self */
  141. epci_base = celleb_epci_get_epci_base(hose);
  142. addr = epci_base + where;
  143. switch (size) {
  144. case 1:
  145. *val = in_8(addr);
  146. break;
  147. case 2:
  148. *val = in_be16(addr);
  149. break;
  150. case 4:
  151. *val = in_be32(addr);
  152. break;
  153. default:
  154. return PCIBIOS_DEVICE_NOT_FOUND;
  155. }
  156. } else {
  157. clear_and_disable_master_abort_interrupt(hose);
  158. addr = celleb_epci_make_config_addr(bus, hose, devfn, where);
  159. switch (size) {
  160. case 1:
  161. *val = in_8(addr);
  162. break;
  163. case 2:
  164. *val = in_le16(addr);
  165. break;
  166. case 4:
  167. *val = in_le32(addr);
  168. break;
  169. default:
  170. return PCIBIOS_DEVICE_NOT_FOUND;
  171. }
  172. }
  173. pr_debug("EPCI: "
  174. "addr=0x%p, devfn=0x%x, where=0x%x, size=0x%x, val=0x%x\n",
  175. addr, devfn, where, size, *val);
  176. return celleb_epci_check_abort(hose, NULL);
  177. }
  178. static int celleb_epci_write_config(struct pci_bus *bus,
  179. unsigned int devfn, int where, int size, u32 val)
  180. {
  181. volatile void __iomem *epci_base, *addr;
  182. struct device_node *node;
  183. struct pci_controller *hose;
  184. /* allignment check */
  185. BUG_ON(where % size);
  186. node = (struct device_node *)bus->sysdata;
  187. hose = pci_find_hose_for_OF_device(node);
  188. if (!celleb_epci_get_epci_cfg(hose))
  189. return PCIBIOS_DEVICE_NOT_FOUND;
  190. if (bus->number == hose->first_busno && devfn == 0) {
  191. /* EPCI controller self */
  192. epci_base = celleb_epci_get_epci_base(hose);
  193. addr = epci_base + where;
  194. switch (size) {
  195. case 1:
  196. out_8(addr, val);
  197. break;
  198. case 2:
  199. out_be16(addr, val);
  200. break;
  201. case 4:
  202. out_be32(addr, val);
  203. break;
  204. default:
  205. return PCIBIOS_DEVICE_NOT_FOUND;
  206. }
  207. } else {
  208. clear_and_disable_master_abort_interrupt(hose);
  209. addr = celleb_epci_make_config_addr(bus, hose, devfn, where);
  210. switch (size) {
  211. case 1:
  212. out_8(addr, val);
  213. break;
  214. case 2:
  215. out_le16(addr, val);
  216. break;
  217. case 4:
  218. out_le32(addr, val);
  219. break;
  220. default:
  221. return PCIBIOS_DEVICE_NOT_FOUND;
  222. }
  223. }
  224. return celleb_epci_check_abort(hose, addr);
  225. }
  226. struct pci_ops celleb_epci_ops = {
  227. celleb_epci_read_config,
  228. celleb_epci_write_config,
  229. };
  230. /* to be moved in FW */
  231. static int __devinit celleb_epci_init(struct pci_controller *hose)
  232. {
  233. u32 val;
  234. volatile void __iomem *reg, *epci_base;
  235. int hwres = 0;
  236. epci_base = celleb_epci_get_epci_base(hose);
  237. /* PCI core reset(Internal bus and PCI clock) */
  238. reg = epci_base + SCC_EPCI_CKCTRL;
  239. val = in_be32(reg);
  240. if (val == 0x00030101)
  241. hwres = 1;
  242. else {
  243. val &= ~(SCC_EPCI_CKCTRL_CRST0 | SCC_EPCI_CKCTRL_CRST1);
  244. out_be32(reg, val);
  245. /* set PCI core clock */
  246. val = in_be32(reg);
  247. val |= (SCC_EPCI_CKCTRL_OCLKEN | SCC_EPCI_CKCTRL_LCLKEN);
  248. out_be32(reg, val);
  249. /* release PCI core reset (internal bus) */
  250. val = in_be32(reg);
  251. val |= SCC_EPCI_CKCTRL_CRST0;
  252. out_be32(reg, val);
  253. /* set PCI clock select */
  254. reg = epci_base + SCC_EPCI_CLKRST;
  255. val = in_be32(reg);
  256. val &= ~SCC_EPCI_CLKRST_CKS_MASK;
  257. val |= SCC_EPCI_CLKRST_CKS_2;
  258. out_be32(reg, val);
  259. /* set arbiter */
  260. reg = epci_base + SCC_EPCI_ABTSET;
  261. out_be32(reg, 0x0f1f001f); /* temporary value */
  262. /* buffer on */
  263. reg = epci_base + SCC_EPCI_CLKRST;
  264. val = in_be32(reg);
  265. val |= SCC_EPCI_CLKRST_BC;
  266. out_be32(reg, val);
  267. /* PCI clock enable */
  268. val = in_be32(reg);
  269. val |= SCC_EPCI_CLKRST_PCKEN;
  270. out_be32(reg, val);
  271. /* release PCI core reset (all) */
  272. reg = epci_base + SCC_EPCI_CKCTRL;
  273. val = in_be32(reg);
  274. val |= (SCC_EPCI_CKCTRL_CRST0 | SCC_EPCI_CKCTRL_CRST1);
  275. out_be32(reg, val);
  276. /* set base translation registers. (already set by Beat) */
  277. /* set base address masks. (already set by Beat) */
  278. }
  279. /* release interrupt masks and clear all interrupts */
  280. reg = epci_base + SCC_EPCI_INTSET;
  281. out_be32(reg, 0x013f011f); /* all interrupts enable */
  282. reg = epci_base + SCC_EPCI_VIENAB;
  283. val = SCC_EPCI_VIENAB_PMPEE | SCC_EPCI_VIENAB_PMFEE;
  284. out_be32(reg, val);
  285. reg = epci_base + SCC_EPCI_STATUS;
  286. out_be32(reg, 0xffffffff);
  287. reg = epci_base + SCC_EPCI_VISTAT;
  288. out_be32(reg, 0xffffffff);
  289. /* disable PCI->IB address translation */
  290. reg = epci_base + SCC_EPCI_VCSR;
  291. val = in_be32(reg);
  292. val &= ~(SCC_EPCI_VCSR_DR | SCC_EPCI_VCSR_AT);
  293. out_be32(reg, val);
  294. /* set base addresses. (no need to set?) */
  295. /* memory space, bus master enable */
  296. reg = epci_base + PCI_COMMAND;
  297. val = PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER;
  298. out_be32(reg, val);
  299. /* endian mode setup */
  300. reg = epci_base + SCC_EPCI_ECMODE;
  301. val = 0x00550155;
  302. out_be32(reg, val);
  303. /* set control option */
  304. reg = epci_base + SCC_EPCI_CNTOPT;
  305. val = in_be32(reg);
  306. val |= SCC_EPCI_CNTOPT_O2PMB;
  307. out_be32(reg, val);
  308. /* XXX: temporay: set registers for address conversion setup */
  309. reg = epci_base + SCC_EPCI_CNF10_REG;
  310. out_be32(reg, 0x80000008);
  311. reg = epci_base + SCC_EPCI_CNF14_REG;
  312. out_be32(reg, 0x40000008);
  313. reg = epci_base + SCC_EPCI_BAM0;
  314. out_be32(reg, 0x80000000);
  315. reg = epci_base + SCC_EPCI_BAM1;
  316. out_be32(reg, 0xe0000000);
  317. reg = epci_base + SCC_EPCI_PVBAT;
  318. out_be32(reg, 0x80000000);
  319. if (!hwres) {
  320. /* release external PCI reset */
  321. reg = epci_base + SCC_EPCI_CLKRST;
  322. val = in_be32(reg);
  323. val |= SCC_EPCI_CLKRST_PCIRST;
  324. out_be32(reg, val);
  325. }
  326. return 0;
  327. }
  328. int __devinit celleb_setup_epci(struct device_node *node,
  329. struct pci_controller *hose)
  330. {
  331. struct resource r;
  332. pr_debug("PCI: celleb_setup_epci()\n");
  333. /*
  334. * Note:
  335. * Celleb epci uses cfg_addr and cfg_data member of
  336. * pci_controller structure in irregular way.
  337. *
  338. * cfg_addr is used to map for control registers of
  339. * celleb epci.
  340. *
  341. * cfg_data is used for configuration area of devices
  342. * on Celleb epci buses.
  343. */
  344. if (of_address_to_resource(node, 0, &r))
  345. goto error;
  346. hose->cfg_addr = ioremap(r.start, (r.end - r.start + 1));
  347. if (!hose->cfg_addr)
  348. goto error;
  349. pr_debug("EPCI: cfg_addr map 0x%016lx->0x%016lx + 0x%016lx\n",
  350. r.start, (unsigned long)hose->cfg_addr,
  351. (r.end - r.start + 1));
  352. if (of_address_to_resource(node, 2, &r))
  353. goto error;
  354. hose->cfg_data = ioremap(r.start, (r.end - r.start + 1));
  355. if (!hose->cfg_data)
  356. goto error;
  357. pr_debug("EPCI: cfg_data map 0x%016lx->0x%016lx + 0x%016lx\n",
  358. r.start, (unsigned long)hose->cfg_data,
  359. (r.end - r.start + 1));
  360. celleb_epci_init(hose);
  361. return 0;
  362. error:
  363. return 1;
  364. }