celleb_scc_epci.c 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  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 device_node *node;
  114. struct pci_controller *hose;
  115. /* allignment check */
  116. BUG_ON(where % size);
  117. node = (struct device_node *)bus->sysdata;
  118. hose = pci_find_hose_for_OF_device(node);
  119. if (!celleb_epci_get_epci_cfg(hose))
  120. return PCIBIOS_DEVICE_NOT_FOUND;
  121. if (bus->number == hose->first_busno && devfn == 0) {
  122. /* EPCI controller self */
  123. epci_base = celleb_epci_get_epci_base(hose);
  124. addr = epci_base + where;
  125. switch (size) {
  126. case 1:
  127. *val = in_8(addr);
  128. break;
  129. case 2:
  130. *val = in_be16(addr);
  131. break;
  132. case 4:
  133. *val = in_be32(addr);
  134. break;
  135. default:
  136. return PCIBIOS_DEVICE_NOT_FOUND;
  137. }
  138. } else {
  139. clear_and_disable_master_abort_interrupt(hose);
  140. addr = celleb_epci_make_config_addr(bus, hose, devfn, where);
  141. switch (size) {
  142. case 1:
  143. *val = in_8(addr);
  144. break;
  145. case 2:
  146. *val = in_le16(addr);
  147. break;
  148. case 4:
  149. *val = in_le32(addr);
  150. break;
  151. default:
  152. return PCIBIOS_DEVICE_NOT_FOUND;
  153. }
  154. }
  155. pr_debug("EPCI: "
  156. "addr=0x%p, devfn=0x%x, where=0x%x, size=0x%x, val=0x%x\n",
  157. addr, devfn, where, size, *val);
  158. return celleb_epci_check_abort(hose, NULL);
  159. }
  160. static int celleb_epci_write_config(struct pci_bus *bus,
  161. unsigned int devfn, int where, int size, u32 val)
  162. {
  163. PCI_IO_ADDR epci_base;
  164. PCI_IO_ADDR addr;
  165. struct device_node *node;
  166. struct pci_controller *hose;
  167. /* allignment check */
  168. BUG_ON(where % size);
  169. node = (struct device_node *)bus->sysdata;
  170. hose = pci_find_hose_for_OF_device(node);
  171. if (!celleb_epci_get_epci_cfg(hose))
  172. return PCIBIOS_DEVICE_NOT_FOUND;
  173. if (bus->number == hose->first_busno && devfn == 0) {
  174. /* EPCI controller self */
  175. epci_base = celleb_epci_get_epci_base(hose);
  176. addr = epci_base + where;
  177. switch (size) {
  178. case 1:
  179. out_8(addr, val);
  180. break;
  181. case 2:
  182. out_be16(addr, val);
  183. break;
  184. case 4:
  185. out_be32(addr, val);
  186. break;
  187. default:
  188. return PCIBIOS_DEVICE_NOT_FOUND;
  189. }
  190. } else {
  191. clear_and_disable_master_abort_interrupt(hose);
  192. addr = celleb_epci_make_config_addr(bus, hose, devfn, where);
  193. switch (size) {
  194. case 1:
  195. out_8(addr, val);
  196. break;
  197. case 2:
  198. out_le16(addr, val);
  199. break;
  200. case 4:
  201. out_le32(addr, val);
  202. break;
  203. default:
  204. return PCIBIOS_DEVICE_NOT_FOUND;
  205. }
  206. }
  207. return celleb_epci_check_abort(hose, addr);
  208. }
  209. struct pci_ops celleb_epci_ops = {
  210. .read = celleb_epci_read_config,
  211. .write = celleb_epci_write_config,
  212. };
  213. /* to be moved in FW */
  214. static int __init celleb_epci_init(struct pci_controller *hose)
  215. {
  216. u32 val;
  217. PCI_IO_ADDR reg;
  218. PCI_IO_ADDR epci_base;
  219. int hwres = 0;
  220. epci_base = celleb_epci_get_epci_base(hose);
  221. /* PCI core reset(Internal bus and PCI clock) */
  222. reg = epci_base + SCC_EPCI_CKCTRL;
  223. val = in_be32(reg);
  224. if (val == 0x00030101)
  225. hwres = 1;
  226. else {
  227. val &= ~(SCC_EPCI_CKCTRL_CRST0 | SCC_EPCI_CKCTRL_CRST1);
  228. out_be32(reg, val);
  229. /* set PCI core clock */
  230. val = in_be32(reg);
  231. val |= (SCC_EPCI_CKCTRL_OCLKEN | SCC_EPCI_CKCTRL_LCLKEN);
  232. out_be32(reg, val);
  233. /* release PCI core reset (internal bus) */
  234. val = in_be32(reg);
  235. val |= SCC_EPCI_CKCTRL_CRST0;
  236. out_be32(reg, val);
  237. /* set PCI clock select */
  238. reg = epci_base + SCC_EPCI_CLKRST;
  239. val = in_be32(reg);
  240. val &= ~SCC_EPCI_CLKRST_CKS_MASK;
  241. val |= SCC_EPCI_CLKRST_CKS_2;
  242. out_be32(reg, val);
  243. /* set arbiter */
  244. reg = epci_base + SCC_EPCI_ABTSET;
  245. out_be32(reg, 0x0f1f001f); /* temporary value */
  246. /* buffer on */
  247. reg = epci_base + SCC_EPCI_CLKRST;
  248. val = in_be32(reg);
  249. val |= SCC_EPCI_CLKRST_BC;
  250. out_be32(reg, val);
  251. /* PCI clock enable */
  252. val = in_be32(reg);
  253. val |= SCC_EPCI_CLKRST_PCKEN;
  254. out_be32(reg, val);
  255. /* release PCI core reset (all) */
  256. reg = epci_base + SCC_EPCI_CKCTRL;
  257. val = in_be32(reg);
  258. val |= (SCC_EPCI_CKCTRL_CRST0 | SCC_EPCI_CKCTRL_CRST1);
  259. out_be32(reg, val);
  260. /* set base translation registers. (already set by Beat) */
  261. /* set base address masks. (already set by Beat) */
  262. }
  263. /* release interrupt masks and clear all interrupts */
  264. reg = epci_base + SCC_EPCI_INTSET;
  265. out_be32(reg, 0x013f011f); /* all interrupts enable */
  266. reg = epci_base + SCC_EPCI_VIENAB;
  267. val = SCC_EPCI_VIENAB_PMPEE | SCC_EPCI_VIENAB_PMFEE;
  268. out_be32(reg, val);
  269. reg = epci_base + SCC_EPCI_STATUS;
  270. out_be32(reg, 0xffffffff);
  271. reg = epci_base + SCC_EPCI_VISTAT;
  272. out_be32(reg, 0xffffffff);
  273. /* disable PCI->IB address translation */
  274. reg = epci_base + SCC_EPCI_VCSR;
  275. val = in_be32(reg);
  276. val &= ~(SCC_EPCI_VCSR_DR | SCC_EPCI_VCSR_AT);
  277. out_be32(reg, val);
  278. /* set base addresses. (no need to set?) */
  279. /* memory space, bus master enable */
  280. reg = epci_base + PCI_COMMAND;
  281. val = PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER;
  282. out_be32(reg, val);
  283. /* endian mode setup */
  284. reg = epci_base + SCC_EPCI_ECMODE;
  285. val = 0x00550155;
  286. out_be32(reg, val);
  287. /* set control option */
  288. reg = epci_base + SCC_EPCI_CNTOPT;
  289. val = in_be32(reg);
  290. val |= SCC_EPCI_CNTOPT_O2PMB;
  291. out_be32(reg, val);
  292. /* XXX: temporay: set registers for address conversion setup */
  293. reg = epci_base + SCC_EPCI_CNF10_REG;
  294. out_be32(reg, 0x80000008);
  295. reg = epci_base + SCC_EPCI_CNF14_REG;
  296. out_be32(reg, 0x40000008);
  297. reg = epci_base + SCC_EPCI_BAM0;
  298. out_be32(reg, 0x80000000);
  299. reg = epci_base + SCC_EPCI_BAM1;
  300. out_be32(reg, 0xe0000000);
  301. reg = epci_base + SCC_EPCI_PVBAT;
  302. out_be32(reg, 0x80000000);
  303. if (!hwres) {
  304. /* release external PCI reset */
  305. reg = epci_base + SCC_EPCI_CLKRST;
  306. val = in_be32(reg);
  307. val |= SCC_EPCI_CLKRST_PCIRST;
  308. out_be32(reg, val);
  309. }
  310. return 0;
  311. }
  312. static int __init celleb_setup_epci(struct device_node *node,
  313. struct pci_controller *hose)
  314. {
  315. struct resource r;
  316. pr_debug("PCI: celleb_setup_epci()\n");
  317. /*
  318. * Note:
  319. * Celleb epci uses cfg_addr and cfg_data member of
  320. * pci_controller structure in irregular way.
  321. *
  322. * cfg_addr is used to map for control registers of
  323. * celleb epci.
  324. *
  325. * cfg_data is used for configuration area of devices
  326. * on Celleb epci buses.
  327. */
  328. if (of_address_to_resource(node, 0, &r))
  329. goto error;
  330. hose->cfg_addr = ioremap(r.start, (r.end - r.start + 1));
  331. if (!hose->cfg_addr)
  332. goto error;
  333. pr_debug("EPCI: cfg_addr map 0x%016llx->0x%016lx + 0x%016llx\n",
  334. r.start, (unsigned long)hose->cfg_addr, (r.end - r.start + 1));
  335. if (of_address_to_resource(node, 2, &r))
  336. goto error;
  337. hose->cfg_data = ioremap(r.start, (r.end - r.start + 1));
  338. if (!hose->cfg_data)
  339. goto error;
  340. pr_debug("EPCI: cfg_data map 0x%016llx->0x%016lx + 0x%016llx\n",
  341. r.start, (unsigned long)hose->cfg_data, (r.end - r.start + 1));
  342. hose->ops = &celleb_epci_ops;
  343. celleb_epci_init(hose);
  344. return 0;
  345. error:
  346. if (hose->cfg_addr)
  347. iounmap(hose->cfg_addr);
  348. if (hose->cfg_data)
  349. iounmap(hose->cfg_data);
  350. return 1;
  351. }
  352. struct celleb_phb_spec celleb_epci_spec __initdata = {
  353. .setup = celleb_setup_epci,
  354. .ops = &spiderpci_ops,
  355. .iowa_init = &spiderpci_iowa_init,
  356. .iowa_data = (void *)0,
  357. };