celleb_scc_epci.c 9.8 KB

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