scc_epci.c 11 KB

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