pci_ixp.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. /*
  2. * IXP PCI Init
  3. *
  4. * (C) Copyright 2011
  5. * Michael Schwingen, michael@schwingen.org
  6. * (C) Copyright 2004 eslab.whut.edu.cn
  7. * Yue Hu(huyue_whut@yahoo.com.cn), Ligong Xue(lgxue@hotmail.com)
  8. *
  9. * See file CREDITS for list of people who contributed to this
  10. * project.
  11. *
  12. * This program is free software; you can redistribute it and/or
  13. * modify it under the terms of the GNU General Public License as
  14. * published by the Free Software Foundation; either version 2 of
  15. * the License, or (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU General Public License
  23. * along with this program; if not, write to the Free Software
  24. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  25. * MA 02111-1307 USA
  26. */
  27. #include <common.h>
  28. #include <asm/processor.h>
  29. #include <asm/io.h>
  30. #include <pci.h>
  31. #include <asm/arch/ixp425.h>
  32. #include <asm/arch/ixp425pci.h>
  33. DECLARE_GLOBAL_DATA_PTR;
  34. static void non_prefetch_read(unsigned int addr, unsigned int cmd,
  35. unsigned int *data);
  36. static void non_prefetch_write(unsigned int addr, unsigned int cmd,
  37. unsigned int data);
  38. /*define the sub vendor and subsystem to be used */
  39. #define IXP425_PCI_SUB_VENDOR_SYSTEM 0x00000000
  40. #define PCI_MEMORY_BUS 0x00000000
  41. #define PCI_MEMORY_PHY 0x00000000
  42. #define PCI_MEMORY_SIZE 0x04000000
  43. #define PCI_MEM_BUS 0x48000000
  44. #define PCI_MEM_PHY 0x00000000
  45. #define PCI_MEM_SIZE 0x04000000
  46. #define PCI_IO_BUS 0x00000000
  47. #define PCI_IO_PHY 0x00000000
  48. #define PCI_IO_SIZE 0x00010000
  49. /* build address value for config sycle */
  50. static unsigned int pci_config_addr(pci_dev_t bdf, unsigned int reg)
  51. {
  52. unsigned int bus = PCI_BUS(bdf);
  53. unsigned int dev = PCI_DEV(bdf);
  54. unsigned int func = PCI_FUNC(bdf);
  55. unsigned int addr;
  56. if (bus) { /* secondary bus, use type 1 config cycle */
  57. addr = bdf | (reg & ~3) | 1;
  58. } else {
  59. /*
  60. primary bus, type 0 config cycle. address bits 31:28
  61. specify the device 10:8 specify the function
  62. */
  63. addr = BIT((31 - dev)) | (func << 8) | (reg & ~3);
  64. }
  65. return addr;
  66. }
  67. static int pci_config_status(void)
  68. {
  69. unsigned int regval;
  70. regval = readl(PCI_CSR_BASE + PCI_ISR_OFFSET);
  71. if ((regval & PCI_ISR_PFE) == 0)
  72. return OK;
  73. /* no device present, make sure that the master abort bit is reset */
  74. writel(PCI_ISR_PFE, PCI_CSR_BASE + PCI_ISR_OFFSET);
  75. return ERROR;
  76. }
  77. static int pci_ixp_hose_read_config_dword(struct pci_controller *hose,
  78. pci_dev_t bdf, int where, unsigned int *val)
  79. {
  80. unsigned int retval;
  81. unsigned int addr;
  82. int stat;
  83. debug("pci_ixp_hose_read_config_dword: bdf %x, reg %x", bdf, where);
  84. /*Set the address to be read */
  85. addr = pci_config_addr(bdf, where);
  86. non_prefetch_read(addr, NP_CMD_CONFIGREAD, &retval);
  87. *val = retval;
  88. stat = pci_config_status();
  89. if (stat < 0)
  90. *val = -1;
  91. debug("-> val %x, status %x\n", *val, stat);
  92. return stat;
  93. }
  94. static int pci_ixp_hose_read_config_word(struct pci_controller *hose,
  95. pci_dev_t bdf, int where, unsigned short *val)
  96. {
  97. unsigned int n;
  98. unsigned int retval;
  99. unsigned int addr;
  100. unsigned int byteEnables;
  101. int stat;
  102. debug("pci_ixp_hose_read_config_word: bdf %x, reg %x", bdf, where);
  103. n = where % 4;
  104. /*byte enables are 4 bits active low, the position of each
  105. bit maps to the byte that it enables */
  106. byteEnables =
  107. (~(BIT(n) | BIT((n + 1)))) &
  108. IXP425_PCI_BOTTOM_NIBBLE_OF_LONG_MASK;
  109. byteEnables = byteEnables << PCI_NP_CBE_BESL;
  110. /*Set the address to be read */
  111. addr = pci_config_addr(bdf, where);
  112. non_prefetch_read(addr, byteEnables | NP_CMD_CONFIGREAD, &retval);
  113. /*Pick out the word we are interested in */
  114. *val = retval >> (8 * n);
  115. stat = pci_config_status();
  116. if (stat < 0)
  117. *val = -1;
  118. debug("-> val %x, status %x\n", *val, stat);
  119. return stat;
  120. }
  121. static int pci_ixp_hose_read_config_byte(struct pci_controller *hose,
  122. pci_dev_t bdf, int where, unsigned char *val)
  123. {
  124. unsigned int retval;
  125. unsigned int n;
  126. unsigned int byteEnables;
  127. unsigned int addr;
  128. int stat;
  129. debug("pci_ixp_hose_read_config_byte: bdf %x, reg %x", bdf, where);
  130. n = where % 4;
  131. /*byte enables are 4 bits, active low, the position of each
  132. bit maps to the byte that it enables */
  133. byteEnables = (~BIT(n)) & IXP425_PCI_BOTTOM_NIBBLE_OF_LONG_MASK;
  134. byteEnables = byteEnables << PCI_NP_CBE_BESL;
  135. /*Set the address to be read */
  136. addr = pci_config_addr(bdf, where);
  137. non_prefetch_read(addr, byteEnables | NP_CMD_CONFIGREAD, &retval);
  138. /*Pick out the byte we are interested in */
  139. *val = retval >> (8 * n);
  140. stat = pci_config_status();
  141. if (stat < 0)
  142. *val = -1;
  143. debug("-> val %x, status %x\n", *val, stat);
  144. return stat;
  145. }
  146. static int pci_ixp_hose_write_config_byte(struct pci_controller *hose,
  147. pci_dev_t bdf, int where, unsigned char val)
  148. {
  149. unsigned int addr;
  150. unsigned int byteEnables;
  151. unsigned int n;
  152. unsigned int ldata;
  153. int stat;
  154. debug("pci_ixp_hose_write_config_byte: bdf %x, reg %x, val %x",
  155. bdf, where, val);
  156. n = where % 4;
  157. /*byte enables are 4 bits active low, the position of each
  158. bit maps to the byte that it enables */
  159. byteEnables = (~BIT(n)) & IXP425_PCI_BOTTOM_NIBBLE_OF_LONG_MASK;
  160. byteEnables = byteEnables << PCI_NP_CBE_BESL;
  161. ldata = val << (8 * n);
  162. /*Set the address to be written */
  163. addr = pci_config_addr(bdf, where);
  164. non_prefetch_write(addr, byteEnables | NP_CMD_CONFIGWRITE, ldata);
  165. stat = pci_config_status();
  166. debug("-> status %x\n", stat);
  167. return stat;
  168. }
  169. static int pci_ixp_hose_write_config_word(struct pci_controller *hose,
  170. pci_dev_t bdf, int where, unsigned short val)
  171. {
  172. unsigned int addr;
  173. unsigned int byteEnables;
  174. unsigned int n;
  175. unsigned int ldata;
  176. int stat;
  177. debug("pci_ixp_hose_write_config_word: bdf %x, reg %x, val %x",
  178. bdf, where, val);
  179. n = where % 4;
  180. /*byte enables are 4 bits active low, the position of each
  181. bit maps to the byte that it enables */
  182. byteEnables =
  183. (~(BIT(n) | BIT((n + 1)))) &
  184. IXP425_PCI_BOTTOM_NIBBLE_OF_LONG_MASK;
  185. byteEnables = byteEnables << PCI_NP_CBE_BESL;
  186. ldata = val << (8 * n);
  187. /*Set the address to be written */
  188. addr = pci_config_addr(bdf, where);
  189. non_prefetch_write(addr, byteEnables | NP_CMD_CONFIGWRITE, ldata);
  190. stat = pci_config_status();
  191. debug("-> status %x\n", stat);
  192. return stat;
  193. }
  194. static int pci_ixp_hose_write_config_dword(struct pci_controller *hose,
  195. pci_dev_t bdf, int where, unsigned int val)
  196. {
  197. unsigned int addr;
  198. int stat;
  199. debug("pci_ixp_hose_write_config_dword: bdf %x, reg %x, val %x",
  200. bdf, where, val);
  201. /*Set the address to be written */
  202. addr = pci_config_addr(bdf, where);
  203. non_prefetch_write(addr, NP_CMD_CONFIGWRITE, val);
  204. stat = pci_config_status();
  205. debug("-> status %x\n", stat);
  206. return stat;
  207. }
  208. static void non_prefetch_read(unsigned int addr,
  209. unsigned int cmd, unsigned int *data)
  210. {
  211. writel(addr, PCI_CSR_BASE + PCI_NP_AD_OFFSET);
  212. /*set up and execute the read */
  213. writel(cmd, PCI_CSR_BASE + PCI_NP_CBE_OFFSET);
  214. /*The result of the read is now in np_rdata */
  215. *data = readl(PCI_CSR_BASE + PCI_NP_RDATA_OFFSET);
  216. return;
  217. }
  218. static void non_prefetch_write(unsigned int addr,
  219. unsigned int cmd, unsigned int data)
  220. {
  221. writel(addr, PCI_CSR_BASE + PCI_NP_AD_OFFSET);
  222. /*set up the write */
  223. writel(cmd, PCI_CSR_BASE + PCI_NP_CBE_OFFSET);
  224. /*Execute the write by writing to NP_WDATA */
  225. writel(data, PCI_CSR_BASE + PCI_NP_WDATA_OFFSET);
  226. return;
  227. }
  228. static void crp_write(unsigned int offset, unsigned int data)
  229. {
  230. /*
  231. * The CRP address register bit 16 indicates that we want to do a
  232. * write
  233. */
  234. writel(PCI_CRP_WRITE | offset, PCI_CSR_BASE + PCI_CRP_AD_CBE_OFFSET);
  235. writel(data, PCI_CSR_BASE + PCI_CRP_WDATA_OFFSET);
  236. }
  237. void pci_ixp_init(struct pci_controller *hose)
  238. {
  239. unsigned int csr;
  240. /*
  241. * Specify that the AHB bus is operating in big endian mode. Set up
  242. * byte lane swapping between little-endian PCI and the big-endian
  243. * AHB bus
  244. */
  245. #ifdef __ARMEB__
  246. csr = PCI_CSR_ABE | PCI_CSR_PDS | PCI_CSR_ADS;
  247. #else
  248. csr = PCI_CSR_ABE;
  249. #endif
  250. writel(csr, PCI_CSR_BASE + PCI_CSR_OFFSET);
  251. writel(0, PCI_CSR_BASE + PCI_INTEN_OFFSET);
  252. /*
  253. * We configure the PCI inbound memory windows to be
  254. * 1:1 mapped to SDRAM
  255. */
  256. crp_write(PCI_CFG_BASE_ADDRESS_0, 0x00000000);
  257. crp_write(PCI_CFG_BASE_ADDRESS_1, 0x01000000);
  258. crp_write(PCI_CFG_BASE_ADDRESS_2, 0x02000000);
  259. crp_write(PCI_CFG_BASE_ADDRESS_3, 0x03000000);
  260. /*
  261. * Enable CSR window at 64 MiB to allow PCI masters
  262. * to continue prefetching past 64 MiB boundary.
  263. */
  264. crp_write(PCI_CFG_BASE_ADDRESS_4, 0x04000000);
  265. /*
  266. * Enable the IO window to be way up high, at 0xfffffc00
  267. */
  268. crp_write(PCI_CFG_BASE_ADDRESS_5, 0xfffffc01);
  269. /*Setup PCI-AHB and AHB-PCI address mappings */
  270. writel(0x00010203, PCI_CSR_BASE + PCI_AHBMEMBASE_OFFSET);
  271. writel(0x00000000, PCI_CSR_BASE + PCI_AHBIOBASE_OFFSET);
  272. writel(0x48494a4b, PCI_CSR_BASE + PCI_PCIMEMBASE_OFFSET);
  273. crp_write(PCI_CFG_SUB_VENDOR_ID, IXP425_PCI_SUB_VENDOR_SYSTEM);
  274. crp_write(PCI_CFG_COMMAND, PCI_CFG_CMD_MAE | PCI_CFG_CMD_BME);
  275. udelay(1000);
  276. /* clear error bits in status register */
  277. writel(PCI_ISR_PSE | PCI_ISR_PFE | PCI_ISR_PPE | PCI_ISR_AHBE,
  278. PCI_CSR_BASE + PCI_ISR_OFFSET);
  279. /*
  280. * Set Initialize Complete in PCI Control Register: allow IXP4XX to
  281. * respond to PCI configuration cycles.
  282. */
  283. csr |= PCI_CSR_IC;
  284. writel(csr, PCI_CSR_BASE + PCI_CSR_OFFSET);
  285. hose->first_busno = 0;
  286. hose->last_busno = 0;
  287. /* System memory space */
  288. pci_set_region(hose->regions + 0,
  289. PCI_MEMORY_BUS,
  290. PCI_MEMORY_PHY, PCI_MEMORY_SIZE, PCI_REGION_SYS_MEMORY);
  291. /* PCI memory space */
  292. pci_set_region(hose->regions + 1,
  293. PCI_MEM_BUS,
  294. PCI_MEM_PHY, PCI_MEM_SIZE, PCI_REGION_MEM);
  295. /* PCI I/O space */
  296. pci_set_region(hose->regions + 2,
  297. PCI_IO_BUS, PCI_IO_PHY, PCI_IO_SIZE, PCI_REGION_IO);
  298. hose->region_count = 3;
  299. pci_set_ops(hose,
  300. pci_ixp_hose_read_config_byte,
  301. pci_ixp_hose_read_config_word,
  302. pci_ixp_hose_read_config_dword,
  303. pci_ixp_hose_write_config_byte,
  304. pci_ixp_hose_write_config_word,
  305. pci_ixp_hose_write_config_dword);
  306. pci_register_hose(hose);
  307. hose->last_busno = pci_hose_scan(hose);
  308. }