ops-tx4927.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. /*
  2. * Define the pci_ops for the PCIC on Toshiba TX4927, TX4938, etc.
  3. *
  4. * Based on linux/arch/mips/pci/ops-tx4938.c,
  5. * linux/arch/mips/pci/fixup-rbtx4938.c,
  6. * linux/arch/mips/txx9/rbtx4938/setup.c,
  7. * and RBTX49xx patch from CELF patch archive.
  8. *
  9. * 2003-2005 (c) MontaVista Software, Inc.
  10. * Copyright (C) 2004 by Ralf Baechle (ralf@linux-mips.org)
  11. * (C) Copyright TOSHIBA CORPORATION 2000-2001, 2004-2007
  12. *
  13. * This program is free software; you can redistribute it and/or modify it
  14. * under the terms of the GNU General Public License as published by the
  15. * Free Software Foundation; either version 2 of the License, or (at your
  16. * option) any later version.
  17. */
  18. #include <linux/kernel.h>
  19. #include <asm/txx9/tx4927pcic.h>
  20. static struct {
  21. struct pci_controller *channel;
  22. struct tx4927_pcic_reg __iomem *pcicptr;
  23. } pcicptrs[2]; /* TX4938 has 2 pcic */
  24. static void __init set_tx4927_pcicptr(struct pci_controller *channel,
  25. struct tx4927_pcic_reg __iomem *pcicptr)
  26. {
  27. int i;
  28. for (i = 0; i < ARRAY_SIZE(pcicptrs); i++) {
  29. if (pcicptrs[i].channel == channel) {
  30. pcicptrs[i].pcicptr = pcicptr;
  31. return;
  32. }
  33. }
  34. for (i = 0; i < ARRAY_SIZE(pcicptrs); i++) {
  35. if (!pcicptrs[i].channel) {
  36. pcicptrs[i].channel = channel;
  37. pcicptrs[i].pcicptr = pcicptr;
  38. return;
  39. }
  40. }
  41. BUG();
  42. }
  43. struct tx4927_pcic_reg __iomem *get_tx4927_pcicptr(
  44. struct pci_controller *channel)
  45. {
  46. int i;
  47. for (i = 0; i < ARRAY_SIZE(pcicptrs); i++) {
  48. if (pcicptrs[i].channel == channel)
  49. return pcicptrs[i].pcicptr;
  50. }
  51. return NULL;
  52. }
  53. static int mkaddr(struct pci_bus *bus, unsigned int devfn, int where,
  54. struct tx4927_pcic_reg __iomem *pcicptr)
  55. {
  56. if (bus->parent == NULL &&
  57. devfn >= PCI_DEVFN(TX4927_PCIC_MAX_DEVNU, 0))
  58. return -1;
  59. __raw_writel(((bus->number & 0xff) << 0x10)
  60. | ((devfn & 0xff) << 0x08) | (where & 0xfc)
  61. | (bus->parent ? 1 : 0),
  62. &pcicptr->g2pcfgadrs);
  63. /* clear M_ABORT and Disable M_ABORT Int. */
  64. __raw_writel((__raw_readl(&pcicptr->pcistatus) & 0x0000ffff)
  65. | (PCI_STATUS_REC_MASTER_ABORT << 16),
  66. &pcicptr->pcistatus);
  67. return 0;
  68. }
  69. static int check_abort(struct tx4927_pcic_reg __iomem *pcicptr)
  70. {
  71. int code = PCIBIOS_SUCCESSFUL;
  72. /* wait write cycle completion before checking error status */
  73. while (__raw_readl(&pcicptr->pcicstatus) & TX4927_PCIC_PCICSTATUS_IWB)
  74. ;
  75. if (__raw_readl(&pcicptr->pcistatus)
  76. & (PCI_STATUS_REC_MASTER_ABORT << 16)) {
  77. __raw_writel((__raw_readl(&pcicptr->pcistatus) & 0x0000ffff)
  78. | (PCI_STATUS_REC_MASTER_ABORT << 16),
  79. &pcicptr->pcistatus);
  80. code = PCIBIOS_DEVICE_NOT_FOUND;
  81. }
  82. return code;
  83. }
  84. static u8 icd_readb(int offset, struct tx4927_pcic_reg __iomem *pcicptr)
  85. {
  86. #ifdef __BIG_ENDIAN
  87. offset ^= 3;
  88. #endif
  89. return __raw_readb((void __iomem *)&pcicptr->g2pcfgdata + offset);
  90. }
  91. static u16 icd_readw(int offset, struct tx4927_pcic_reg __iomem *pcicptr)
  92. {
  93. #ifdef __BIG_ENDIAN
  94. offset ^= 2;
  95. #endif
  96. return __raw_readw((void __iomem *)&pcicptr->g2pcfgdata + offset);
  97. }
  98. static u32 icd_readl(struct tx4927_pcic_reg __iomem *pcicptr)
  99. {
  100. return __raw_readl(&pcicptr->g2pcfgdata);
  101. }
  102. static void icd_writeb(u8 val, int offset,
  103. struct tx4927_pcic_reg __iomem *pcicptr)
  104. {
  105. #ifdef __BIG_ENDIAN
  106. offset ^= 3;
  107. #endif
  108. __raw_writeb(val, (void __iomem *)&pcicptr->g2pcfgdata + offset);
  109. }
  110. static void icd_writew(u16 val, int offset,
  111. struct tx4927_pcic_reg __iomem *pcicptr)
  112. {
  113. #ifdef __BIG_ENDIAN
  114. offset ^= 2;
  115. #endif
  116. __raw_writew(val, (void __iomem *)&pcicptr->g2pcfgdata + offset);
  117. }
  118. static void icd_writel(u32 val, struct tx4927_pcic_reg __iomem *pcicptr)
  119. {
  120. __raw_writel(val, &pcicptr->g2pcfgdata);
  121. }
  122. static struct tx4927_pcic_reg __iomem *pci_bus_to_pcicptr(struct pci_bus *bus)
  123. {
  124. struct pci_controller *channel = bus->sysdata;
  125. return get_tx4927_pcicptr(channel);
  126. }
  127. static int tx4927_pci_config_read(struct pci_bus *bus, unsigned int devfn,
  128. int where, int size, u32 *val)
  129. {
  130. struct tx4927_pcic_reg __iomem *pcicptr = pci_bus_to_pcicptr(bus);
  131. if (mkaddr(bus, devfn, where, pcicptr)) {
  132. *val = 0xffffffff;
  133. return -1;
  134. }
  135. switch (size) {
  136. case 1:
  137. *val = icd_readb(where & 3, pcicptr);
  138. break;
  139. case 2:
  140. *val = icd_readw(where & 3, pcicptr);
  141. break;
  142. default:
  143. *val = icd_readl(pcicptr);
  144. }
  145. return check_abort(pcicptr);
  146. }
  147. static int tx4927_pci_config_write(struct pci_bus *bus, unsigned int devfn,
  148. int where, int size, u32 val)
  149. {
  150. struct tx4927_pcic_reg __iomem *pcicptr = pci_bus_to_pcicptr(bus);
  151. if (mkaddr(bus, devfn, where, pcicptr))
  152. return -1;
  153. switch (size) {
  154. case 1:
  155. icd_writeb(val, where & 3, pcicptr);
  156. break;
  157. case 2:
  158. icd_writew(val, where & 3, pcicptr);
  159. break;
  160. default:
  161. icd_writel(val, pcicptr);
  162. }
  163. return check_abort(pcicptr);
  164. }
  165. static struct pci_ops tx4927_pci_ops = {
  166. .read = tx4927_pci_config_read,
  167. .write = tx4927_pci_config_write,
  168. };
  169. static struct {
  170. u8 trdyto;
  171. u8 retryto;
  172. u16 gbwc;
  173. } tx4927_pci_opts __devinitdata = {
  174. .trdyto = 0,
  175. .retryto = 0,
  176. .gbwc = 0xfe0, /* 4064 GBUSCLK for CCFG.GTOT=0b11 */
  177. };
  178. void __init tx4927_pcic_setup(struct tx4927_pcic_reg __iomem *pcicptr,
  179. struct pci_controller *channel, int extarb)
  180. {
  181. int i;
  182. unsigned long flags;
  183. set_tx4927_pcicptr(channel, pcicptr);
  184. if (!channel->pci_ops)
  185. printk(KERN_INFO
  186. "PCIC -- DID:%04x VID:%04x RID:%02x Arbiter:%s\n",
  187. __raw_readl(&pcicptr->pciid) >> 16,
  188. __raw_readl(&pcicptr->pciid) & 0xffff,
  189. __raw_readl(&pcicptr->pciccrev) & 0xff,
  190. extarb ? "External" : "Internal");
  191. channel->pci_ops = &tx4927_pci_ops;
  192. local_irq_save(flags);
  193. /* Disable All Initiator Space */
  194. __raw_writel(__raw_readl(&pcicptr->pciccfg)
  195. & ~(TX4927_PCIC_PCICCFG_G2PMEN(0)
  196. | TX4927_PCIC_PCICCFG_G2PMEN(1)
  197. | TX4927_PCIC_PCICCFG_G2PMEN(2)
  198. | TX4927_PCIC_PCICCFG_G2PIOEN),
  199. &pcicptr->pciccfg);
  200. /* GB->PCI mappings */
  201. __raw_writel((channel->io_resource->end - channel->io_resource->start)
  202. >> 4,
  203. &pcicptr->g2piomask);
  204. ____raw_writeq((channel->io_resource->start +
  205. channel->io_map_base - IO_BASE) |
  206. #ifdef __BIG_ENDIAN
  207. TX4927_PCIC_G2PIOGBASE_ECHG
  208. #else
  209. TX4927_PCIC_G2PIOGBASE_BSDIS
  210. #endif
  211. , &pcicptr->g2piogbase);
  212. ____raw_writeq(channel->io_resource->start - channel->io_offset,
  213. &pcicptr->g2piopbase);
  214. for (i = 0; i < 3; i++) {
  215. __raw_writel(0, &pcicptr->g2pmmask[i]);
  216. ____raw_writeq(0, &pcicptr->g2pmgbase[i]);
  217. ____raw_writeq(0, &pcicptr->g2pmpbase[i]);
  218. }
  219. if (channel->mem_resource->end) {
  220. __raw_writel((channel->mem_resource->end
  221. - channel->mem_resource->start) >> 4,
  222. &pcicptr->g2pmmask[0]);
  223. ____raw_writeq(channel->mem_resource->start |
  224. #ifdef __BIG_ENDIAN
  225. TX4927_PCIC_G2PMnGBASE_ECHG
  226. #else
  227. TX4927_PCIC_G2PMnGBASE_BSDIS
  228. #endif
  229. , &pcicptr->g2pmgbase[0]);
  230. ____raw_writeq(channel->mem_resource->start -
  231. channel->mem_offset,
  232. &pcicptr->g2pmpbase[0]);
  233. }
  234. /* PCI->GB mappings (I/O 256B) */
  235. __raw_writel(0, &pcicptr->p2giopbase); /* 256B */
  236. ____raw_writeq(0, &pcicptr->p2giogbase);
  237. /* PCI->GB mappings (MEM 512MB (64MB on R1.x)) */
  238. __raw_writel(0, &pcicptr->p2gm0plbase);
  239. __raw_writel(0, &pcicptr->p2gm0pubase);
  240. ____raw_writeq(TX4927_PCIC_P2GMnGBASE_TMEMEN |
  241. #ifdef __BIG_ENDIAN
  242. TX4927_PCIC_P2GMnGBASE_TECHG
  243. #else
  244. TX4927_PCIC_P2GMnGBASE_TBSDIS
  245. #endif
  246. , &pcicptr->p2gmgbase[0]);
  247. /* PCI->GB mappings (MEM 16MB) */
  248. __raw_writel(0xffffffff, &pcicptr->p2gm1plbase);
  249. __raw_writel(0xffffffff, &pcicptr->p2gm1pubase);
  250. ____raw_writeq(0, &pcicptr->p2gmgbase[1]);
  251. /* PCI->GB mappings (MEM 1MB) */
  252. __raw_writel(0xffffffff, &pcicptr->p2gm2pbase); /* 1MB */
  253. ____raw_writeq(0, &pcicptr->p2gmgbase[2]);
  254. /* Clear all (including IRBER) except for GBWC */
  255. __raw_writel((tx4927_pci_opts.gbwc << 16)
  256. & TX4927_PCIC_PCICCFG_GBWC_MASK,
  257. &pcicptr->pciccfg);
  258. /* Enable Initiator Memory Space */
  259. if (channel->mem_resource->end)
  260. __raw_writel(__raw_readl(&pcicptr->pciccfg)
  261. | TX4927_PCIC_PCICCFG_G2PMEN(0),
  262. &pcicptr->pciccfg);
  263. /* Enable Initiator I/O Space */
  264. if (channel->io_resource->end)
  265. __raw_writel(__raw_readl(&pcicptr->pciccfg)
  266. | TX4927_PCIC_PCICCFG_G2PIOEN,
  267. &pcicptr->pciccfg);
  268. /* Enable Initiator Config */
  269. __raw_writel(__raw_readl(&pcicptr->pciccfg)
  270. | TX4927_PCIC_PCICCFG_ICAEN | TX4927_PCIC_PCICCFG_TCAR,
  271. &pcicptr->pciccfg);
  272. /* Do not use MEMMUL, MEMINF: YMFPCI card causes M_ABORT. */
  273. __raw_writel(0, &pcicptr->pcicfg1);
  274. __raw_writel((__raw_readl(&pcicptr->g2ptocnt) & ~0xffff)
  275. | (tx4927_pci_opts.trdyto & 0xff)
  276. | ((tx4927_pci_opts.retryto & 0xff) << 8),
  277. &pcicptr->g2ptocnt);
  278. /* Clear All Local Bus Status */
  279. __raw_writel(TX4927_PCIC_PCICSTATUS_ALL, &pcicptr->pcicstatus);
  280. /* Enable All Local Bus Interrupts */
  281. __raw_writel(TX4927_PCIC_PCICSTATUS_ALL, &pcicptr->pcicmask);
  282. /* Clear All Initiator Status */
  283. __raw_writel(TX4927_PCIC_G2PSTATUS_ALL, &pcicptr->g2pstatus);
  284. /* Enable All Initiator Interrupts */
  285. __raw_writel(TX4927_PCIC_G2PSTATUS_ALL, &pcicptr->g2pmask);
  286. /* Clear All PCI Status Error */
  287. __raw_writel((__raw_readl(&pcicptr->pcistatus) & 0x0000ffff)
  288. | (TX4927_PCIC_PCISTATUS_ALL << 16),
  289. &pcicptr->pcistatus);
  290. /* Enable All PCI Status Error Interrupts */
  291. __raw_writel(TX4927_PCIC_PCISTATUS_ALL, &pcicptr->pcimask);
  292. if (!extarb) {
  293. /* Reset Bus Arbiter */
  294. __raw_writel(TX4927_PCIC_PBACFG_RPBA, &pcicptr->pbacfg);
  295. __raw_writel(0, &pcicptr->pbabm);
  296. /* Enable Bus Arbiter */
  297. __raw_writel(TX4927_PCIC_PBACFG_PBAEN, &pcicptr->pbacfg);
  298. }
  299. __raw_writel(PCI_COMMAND_MASTER | PCI_COMMAND_MEMORY
  300. | PCI_COMMAND_PARITY | PCI_COMMAND_SERR,
  301. &pcicptr->pcistatus);
  302. local_irq_restore(flags);
  303. printk(KERN_DEBUG
  304. "PCI: COMMAND=%04x,PCIMASK=%04x,"
  305. "TRDYTO=%02x,RETRYTO=%02x,GBWC=%03x\n",
  306. __raw_readl(&pcicptr->pcistatus) & 0xffff,
  307. __raw_readl(&pcicptr->pcimask) & 0xffff,
  308. __raw_readl(&pcicptr->g2ptocnt) & 0xff,
  309. (__raw_readl(&pcicptr->g2ptocnt) & 0xff00) >> 8,
  310. (__raw_readl(&pcicptr->pciccfg) >> 16) & 0xfff);
  311. }
  312. static void tx4927_report_pcic_status1(struct tx4927_pcic_reg __iomem *pcicptr)
  313. {
  314. __u16 pcistatus = (__u16)(__raw_readl(&pcicptr->pcistatus) >> 16);
  315. __u32 g2pstatus = __raw_readl(&pcicptr->g2pstatus);
  316. __u32 pcicstatus = __raw_readl(&pcicptr->pcicstatus);
  317. static struct {
  318. __u32 flag;
  319. const char *str;
  320. } pcistat_tbl[] = {
  321. { PCI_STATUS_DETECTED_PARITY, "DetectedParityError" },
  322. { PCI_STATUS_SIG_SYSTEM_ERROR, "SignaledSystemError" },
  323. { PCI_STATUS_REC_MASTER_ABORT, "ReceivedMasterAbort" },
  324. { PCI_STATUS_REC_TARGET_ABORT, "ReceivedTargetAbort" },
  325. { PCI_STATUS_SIG_TARGET_ABORT, "SignaledTargetAbort" },
  326. { PCI_STATUS_PARITY, "MasterParityError" },
  327. }, g2pstat_tbl[] = {
  328. { TX4927_PCIC_G2PSTATUS_TTOE, "TIOE" },
  329. { TX4927_PCIC_G2PSTATUS_RTOE, "RTOE" },
  330. }, pcicstat_tbl[] = {
  331. { TX4927_PCIC_PCICSTATUS_PME, "PME" },
  332. { TX4927_PCIC_PCICSTATUS_TLB, "TLB" },
  333. { TX4927_PCIC_PCICSTATUS_NIB, "NIB" },
  334. { TX4927_PCIC_PCICSTATUS_ZIB, "ZIB" },
  335. { TX4927_PCIC_PCICSTATUS_PERR, "PERR" },
  336. { TX4927_PCIC_PCICSTATUS_SERR, "SERR" },
  337. { TX4927_PCIC_PCICSTATUS_GBE, "GBE" },
  338. { TX4927_PCIC_PCICSTATUS_IWB, "IWB" },
  339. };
  340. int i, cont;
  341. printk(KERN_ERR "");
  342. if (pcistatus & TX4927_PCIC_PCISTATUS_ALL) {
  343. printk(KERN_CONT "pcistat:%04x(", pcistatus);
  344. for (i = 0, cont = 0; i < ARRAY_SIZE(pcistat_tbl); i++)
  345. if (pcistatus & pcistat_tbl[i].flag)
  346. printk(KERN_CONT "%s%s",
  347. cont++ ? " " : "", pcistat_tbl[i].str);
  348. printk(KERN_CONT ") ");
  349. }
  350. if (g2pstatus & TX4927_PCIC_G2PSTATUS_ALL) {
  351. printk(KERN_CONT "g2pstatus:%08x(", g2pstatus);
  352. for (i = 0, cont = 0; i < ARRAY_SIZE(g2pstat_tbl); i++)
  353. if (g2pstatus & g2pstat_tbl[i].flag)
  354. printk(KERN_CONT "%s%s",
  355. cont++ ? " " : "", g2pstat_tbl[i].str);
  356. printk(KERN_CONT ") ");
  357. }
  358. if (pcicstatus & TX4927_PCIC_PCICSTATUS_ALL) {
  359. printk(KERN_CONT "pcicstatus:%08x(", pcicstatus);
  360. for (i = 0, cont = 0; i < ARRAY_SIZE(pcicstat_tbl); i++)
  361. if (pcicstatus & pcicstat_tbl[i].flag)
  362. printk(KERN_CONT "%s%s",
  363. cont++ ? " " : "", pcicstat_tbl[i].str);
  364. printk(KERN_CONT ")");
  365. }
  366. printk(KERN_CONT "\n");
  367. }
  368. void tx4927_report_pcic_status(void)
  369. {
  370. int i;
  371. for (i = 0; i < ARRAY_SIZE(pcicptrs); i++) {
  372. if (pcicptrs[i].pcicptr)
  373. tx4927_report_pcic_status1(pcicptrs[i].pcicptr);
  374. }
  375. }