celleb_scc_epci.c 9.8 KB

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