pci.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * (C) Copyright 2002
  4. * Sysgo Real-Time Solutions, GmbH <www.elinos.com>
  5. * Marius Groeger <mgroeger@sysgo.de>
  6. *
  7. * (C) Copyright 2002
  8. * David Mueller, ELSOFT AG, <d.mueller@elsoft.ch>
  9. *
  10. * (C) Copyright 2003
  11. * Texas Instruments, <www.ti.com>
  12. * Kshitij Gupta <Kshitij@ti.com>
  13. *
  14. * (C) Copyright 2004
  15. * ARM Ltd.
  16. * Philippe Robin, <philippe.robin@arm.com>
  17. *
  18. * (C) Copyright 2011
  19. * Linaro
  20. * Linus Walleij <linus.walleij@linaro.org>
  21. */
  22. #include <common.h>
  23. #include <pci.h>
  24. #include <asm/io.h>
  25. #include "integrator-sc.h"
  26. #include "pci_v3.h"
  27. #define INTEGRATOR_BOOT_ROM_BASE 0x20000000
  28. #define INTEGRATOR_HDR0_SDRAM_BASE 0x80000000
  29. /*
  30. * These are in the physical addresses on the CPU side, i.e.
  31. * where we read and write stuff - you don't want to try to
  32. * move these around
  33. */
  34. #define PHYS_PCI_MEM_BASE 0x40000000
  35. #define PHYS_PCI_IO_BASE 0x60000000 /* PCI I/O space base */
  36. #define PHYS_PCI_CONFIG_BASE 0x61000000
  37. #define PHYS_PCI_V3_BASE 0x62000000 /* V360EPC registers */
  38. #define SZ_256M 0x10000000
  39. /*
  40. * These are in the PCI BUS address space
  41. * Set to 0x00000000 in the Linux kernel, 0x40000000 in Boot monitor
  42. * we follow the example of the kernel, because that is the address
  43. * range that devices actually use - what would they be doing at
  44. * 0x40000000?
  45. */
  46. #define PCI_BUS_NONMEM_START 0x00000000
  47. #define PCI_BUS_NONMEM_SIZE SZ_256M
  48. #define PCI_BUS_PREMEM_START (PCI_BUS_NONMEM_START + PCI_BUS_NONMEM_SIZE)
  49. #define PCI_BUS_PREMEM_SIZE SZ_256M
  50. #if PCI_BUS_NONMEM_START & 0x000fffff
  51. #error PCI_BUS_NONMEM_START must be megabyte aligned
  52. #endif
  53. #if PCI_BUS_PREMEM_START & 0x000fffff
  54. #error PCI_BUS_PREMEM_START must be megabyte aligned
  55. #endif
  56. /*
  57. * Initialize PCI Devices, report devices found.
  58. */
  59. #ifndef CONFIG_PCI_PNP
  60. #define PCI_ENET0_IOADDR 0x60000000 /* First card in PCI I/O space */
  61. #define PCI_ENET0_MEMADDR 0x40000000 /* First card in PCI memory space */
  62. static struct pci_config_table pci_integrator_config_table[] = {
  63. { PCI_ANY_ID, PCI_ANY_ID, PCI_ANY_ID, PCI_ANY_ID, 0x0f, PCI_ANY_ID,
  64. pci_cfgfunc_config_device, { PCI_ENET0_IOADDR,
  65. PCI_ENET0_MEMADDR,
  66. PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER }},
  67. { }
  68. };
  69. #endif /* CONFIG_PCI_PNP */
  70. /* V3 access routines */
  71. #define v3_writeb(o, v) __raw_writeb(v, PHYS_PCI_V3_BASE + (unsigned int)(o))
  72. #define v3_readb(o) (__raw_readb(PHYS_PCI_V3_BASE + (unsigned int)(o)))
  73. #define v3_writew(o, v) __raw_writew(v, PHYS_PCI_V3_BASE + (unsigned int)(o))
  74. #define v3_readw(o) (__raw_readw(PHYS_PCI_V3_BASE + (unsigned int)(o)))
  75. #define v3_writel(o, v) __raw_writel(v, PHYS_PCI_V3_BASE + (unsigned int)(o))
  76. #define v3_readl(o) (__raw_readl(PHYS_PCI_V3_BASE + (unsigned int)(o)))
  77. static unsigned long v3_open_config_window(pci_dev_t bdf, int offset)
  78. {
  79. unsigned int address, mapaddress;
  80. unsigned int busnr = PCI_BUS(bdf);
  81. unsigned int devfn = PCI_FUNC(bdf);
  82. /*
  83. * Trap out illegal values
  84. */
  85. if (offset > 255)
  86. BUG();
  87. if (busnr > 255)
  88. BUG();
  89. if (devfn > 255)
  90. BUG();
  91. if (busnr == 0) {
  92. /*
  93. * Linux calls the thing U-Boot calls "DEV" "SLOT"
  94. * instead, but it's the same 5 bits
  95. */
  96. int slot = PCI_DEV(bdf);
  97. /*
  98. * local bus segment so need a type 0 config cycle
  99. *
  100. * build the PCI configuration "address" with one-hot in
  101. * A31-A11
  102. *
  103. * mapaddress:
  104. * 3:1 = config cycle (101)
  105. * 0 = PCI A1 & A0 are 0 (0)
  106. */
  107. address = PCI_FUNC(bdf) << 8;
  108. mapaddress = V3_LB_MAP_TYPE_CONFIG;
  109. if (slot > 12)
  110. /*
  111. * high order bits are handled by the MAP register
  112. */
  113. mapaddress |= 1 << (slot - 5);
  114. else
  115. /*
  116. * low order bits handled directly in the address
  117. */
  118. address |= 1 << (slot + 11);
  119. } else {
  120. /*
  121. * not the local bus segment so need a type 1 config cycle
  122. *
  123. * address:
  124. * 23:16 = bus number
  125. * 15:11 = slot number (7:3 of devfn)
  126. * 10:8 = func number (2:0 of devfn)
  127. *
  128. * mapaddress:
  129. * 3:1 = config cycle (101)
  130. * 0 = PCI A1 & A0 from host bus (1)
  131. */
  132. mapaddress = V3_LB_MAP_TYPE_CONFIG | V3_LB_MAP_AD_LOW_EN;
  133. address = (busnr << 16) | (devfn << 8);
  134. }
  135. /*
  136. * Set up base0 to see all 512Mbytes of memory space (not
  137. * prefetchable), this frees up base1 for re-use by
  138. * configuration memory
  139. */
  140. v3_writel(V3_LB_BASE0, v3_addr_to_lb_base(PHYS_PCI_MEM_BASE) |
  141. V3_LB_BASE_ADR_SIZE_512MB | V3_LB_BASE_ENABLE);
  142. /*
  143. * Set up base1/map1 to point into configuration space.
  144. */
  145. v3_writel(V3_LB_BASE1, v3_addr_to_lb_base(PHYS_PCI_CONFIG_BASE) |
  146. V3_LB_BASE_ADR_SIZE_16MB | V3_LB_BASE_ENABLE);
  147. v3_writew(V3_LB_MAP1, mapaddress);
  148. return PHYS_PCI_CONFIG_BASE + address + offset;
  149. }
  150. static void v3_close_config_window(void)
  151. {
  152. /*
  153. * Reassign base1 for use by prefetchable PCI memory
  154. */
  155. v3_writel(V3_LB_BASE1, v3_addr_to_lb_base(PHYS_PCI_MEM_BASE + SZ_256M) |
  156. V3_LB_BASE_ADR_SIZE_256MB | V3_LB_BASE_PREFETCH |
  157. V3_LB_BASE_ENABLE);
  158. v3_writew(V3_LB_MAP1, v3_addr_to_lb_map(PCI_BUS_PREMEM_START) |
  159. V3_LB_MAP_TYPE_MEM_MULTIPLE);
  160. /*
  161. * And shrink base0 back to a 256M window (NOTE: MAP0 already correct)
  162. */
  163. v3_writel(V3_LB_BASE0, v3_addr_to_lb_base(PHYS_PCI_MEM_BASE) |
  164. V3_LB_BASE_ADR_SIZE_256MB | V3_LB_BASE_ENABLE);
  165. }
  166. static int pci_integrator_read_byte(struct pci_controller *hose, pci_dev_t bdf,
  167. int offset, unsigned char *val)
  168. {
  169. unsigned long addr;
  170. addr = v3_open_config_window(bdf, offset);
  171. *val = __raw_readb(addr);
  172. v3_close_config_window();
  173. return 0;
  174. }
  175. static int pci_integrator_read__word(struct pci_controller *hose,
  176. pci_dev_t bdf, int offset,
  177. unsigned short *val)
  178. {
  179. unsigned long addr;
  180. addr = v3_open_config_window(bdf, offset);
  181. *val = __raw_readw(addr);
  182. v3_close_config_window();
  183. return 0;
  184. }
  185. static int pci_integrator_read_dword(struct pci_controller *hose,
  186. pci_dev_t bdf, int offset,
  187. unsigned int *val)
  188. {
  189. unsigned long addr;
  190. addr = v3_open_config_window(bdf, offset);
  191. *val = __raw_readl(addr);
  192. v3_close_config_window();
  193. return 0;
  194. }
  195. static int pci_integrator_write_byte(struct pci_controller *hose,
  196. pci_dev_t bdf, int offset,
  197. unsigned char val)
  198. {
  199. unsigned long addr;
  200. addr = v3_open_config_window(bdf, offset);
  201. __raw_writeb((u8)val, addr);
  202. __raw_readb(addr);
  203. v3_close_config_window();
  204. return 0;
  205. }
  206. static int pci_integrator_write_word(struct pci_controller *hose,
  207. pci_dev_t bdf, int offset,
  208. unsigned short val)
  209. {
  210. unsigned long addr;
  211. addr = v3_open_config_window(bdf, offset);
  212. __raw_writew((u8)val, addr);
  213. __raw_readw(addr);
  214. v3_close_config_window();
  215. return 0;
  216. }
  217. static int pci_integrator_write_dword(struct pci_controller *hose,
  218. pci_dev_t bdf, int offset,
  219. unsigned int val)
  220. {
  221. unsigned long addr;
  222. addr = v3_open_config_window(bdf, offset);
  223. __raw_writel((u8)val, addr);
  224. __raw_readl(addr);
  225. v3_close_config_window();
  226. return 0;
  227. }
  228. struct pci_controller integrator_hose = {
  229. #ifndef CONFIG_PCI_PNP
  230. config_table: pci_integrator_config_table,
  231. #endif
  232. };
  233. void pci_init_board(void)
  234. {
  235. struct pci_controller *hose = &integrator_hose;
  236. u16 val;
  237. /* setting this register will take the V3 out of reset */
  238. __raw_writel(SC_PCI_PCIEN, SC_PCI);
  239. /* Wait for 230 ms (from spec) before accessing any V3 registers */
  240. mdelay(230);
  241. /* Now write the Base I/O Address Word to PHYS_PCI_V3_BASE + 0x6E */
  242. v3_writew(V3_LB_IO_BASE, (PHYS_PCI_V3_BASE >> 16));
  243. /* Wait for the mailbox to settle */
  244. do {
  245. v3_writeb(V3_MAIL_DATA, 0xAA);
  246. v3_writeb(V3_MAIL_DATA + 4, 0x55);
  247. } while (v3_readb(V3_MAIL_DATA) != 0xAA ||
  248. v3_readb(V3_MAIL_DATA + 4) != 0x55);
  249. /* Make sure that V3 register access is not locked, if it is, unlock it */
  250. if (v3_readw(V3_SYSTEM) & V3_SYSTEM_M_LOCK)
  251. v3_writew(V3_SYSTEM, 0xA05F);
  252. /*
  253. * Ensure that the slave accesses from PCI are disabled while we
  254. * setup memory windows
  255. */
  256. val = v3_readw(V3_PCI_CMD);
  257. val &= ~(V3_COMMAND_M_MEM_EN | V3_COMMAND_M_IO_EN);
  258. v3_writew(V3_PCI_CMD, val);
  259. /* Clear RST_OUT to 0; keep the PCI bus in reset until we've finished */
  260. val = v3_readw(V3_SYSTEM);
  261. val &= ~V3_SYSTEM_M_RST_OUT;
  262. v3_writew(V3_SYSTEM, val);
  263. /* Make all accesses from PCI space retry until we're ready for them */
  264. val = v3_readw(V3_PCI_CFG);
  265. val |= V3_PCI_CFG_M_RETRY_EN;
  266. v3_writew(V3_PCI_CFG, val);
  267. /*
  268. * Set up any V3 PCI Configuration Registers that we absolutely have to.
  269. * LB_CFG controls Local Bus protocol.
  270. * Enable LocalBus byte strobes for READ accesses too.
  271. * set bit 7 BE_IMODE and bit 6 BE_OMODE
  272. */
  273. val = v3_readw(V3_LB_CFG);
  274. val |= 0x0C0;
  275. v3_writew(V3_LB_CFG, val);
  276. /* PCI_CMD controls overall PCI operation. Enable PCI bus master. */
  277. val = v3_readw(V3_PCI_CMD);
  278. val |= V3_COMMAND_M_MASTER_EN;
  279. v3_writew(V3_PCI_CMD, val);
  280. /*
  281. * PCI_MAP0 controls where the PCI to CPU memory window is on
  282. * Local Bus
  283. */
  284. v3_writel(V3_PCI_MAP0,
  285. (INTEGRATOR_BOOT_ROM_BASE) | (V3_PCI_MAP_M_ADR_SIZE_512MB |
  286. V3_PCI_MAP_M_REG_EN |
  287. V3_PCI_MAP_M_ENABLE));
  288. /* PCI_BASE0 is the PCI address of the start of the window */
  289. v3_writel(V3_PCI_BASE0, INTEGRATOR_BOOT_ROM_BASE);
  290. /* PCI_MAP1 is LOCAL address of the start of the window */
  291. v3_writel(V3_PCI_MAP1,
  292. (INTEGRATOR_HDR0_SDRAM_BASE) | (V3_PCI_MAP_M_ADR_SIZE_1GB |
  293. V3_PCI_MAP_M_REG_EN |
  294. V3_PCI_MAP_M_ENABLE));
  295. /* PCI_BASE1 is the PCI address of the start of the window */
  296. v3_writel(V3_PCI_BASE1, INTEGRATOR_HDR0_SDRAM_BASE);
  297. /*
  298. * Set up memory the windows from local bus memory into PCI
  299. * configuration, I/O and Memory regions.
  300. * PCI I/O, LB_BASE2 and LB_MAP2 are used exclusively for this.
  301. */
  302. v3_writew(V3_LB_BASE2,
  303. v3_addr_to_lb_map(PHYS_PCI_IO_BASE) | V3_LB_BASE_ENABLE);
  304. v3_writew(V3_LB_MAP2, 0);
  305. /* PCI Configuration, use LB_BASE1/LB_MAP1. */
  306. /*
  307. * PCI Memory use LB_BASE0/LB_MAP0 and LB_BASE1/LB_MAP1
  308. * Map first 256Mbytes as non-prefetchable via BASE0/MAP0
  309. */
  310. v3_writel(V3_LB_BASE0, v3_addr_to_lb_base(PHYS_PCI_MEM_BASE) |
  311. V3_LB_BASE_ADR_SIZE_256MB | V3_LB_BASE_ENABLE);
  312. v3_writew(V3_LB_MAP0,
  313. v3_addr_to_lb_map(PCI_BUS_NONMEM_START) | V3_LB_MAP_TYPE_MEM);
  314. /* Map second 256 Mbytes as prefetchable via BASE1/MAP1 */
  315. v3_writel(V3_LB_BASE1, v3_addr_to_lb_base(PHYS_PCI_MEM_BASE + SZ_256M) |
  316. V3_LB_BASE_ADR_SIZE_256MB | V3_LB_BASE_PREFETCH |
  317. V3_LB_BASE_ENABLE);
  318. v3_writew(V3_LB_MAP1, v3_addr_to_lb_map(PCI_BUS_PREMEM_START) |
  319. V3_LB_MAP_TYPE_MEM_MULTIPLE);
  320. /* Dump PCI to local address space mappings */
  321. debug("LB_BASE0 = %08x\n", v3_readl(V3_LB_BASE0));
  322. debug("LB_MAP0 = %04x\n", v3_readw(V3_LB_MAP0));
  323. debug("LB_BASE1 = %08x\n", v3_readl(V3_LB_BASE1));
  324. debug("LB_MAP1 = %04x\n", v3_readw(V3_LB_MAP1));
  325. debug("LB_BASE2 = %04x\n", v3_readw(V3_LB_BASE2));
  326. debug("LB_MAP2 = %04x\n", v3_readw(V3_LB_MAP2));
  327. debug("LB_IO_BASE = %04x\n", v3_readw(V3_LB_IO_BASE));
  328. /*
  329. * Allow accesses to PCI Configuration space and set up A1, A0 for
  330. * type 1 config cycles
  331. */
  332. val = v3_readw(V3_PCI_CFG);
  333. val &= ~(V3_PCI_CFG_M_RETRY_EN | V3_PCI_CFG_M_AD_LOW1);
  334. val |= V3_PCI_CFG_M_AD_LOW0;
  335. v3_writew(V3_PCI_CFG, val);
  336. /* now we can allow incoming PCI MEMORY accesses */
  337. val = v3_readw(V3_PCI_CMD);
  338. val |= V3_COMMAND_M_MEM_EN;
  339. v3_writew(V3_PCI_CMD, val);
  340. /*
  341. * Set RST_OUT to take the PCI bus is out of reset, PCI devices can
  342. * now initialise.
  343. */
  344. val = v3_readw(V3_SYSTEM);
  345. val |= V3_SYSTEM_M_RST_OUT;
  346. v3_writew(V3_SYSTEM, val);
  347. /* Lock the V3 system register so that no one else can play with it */
  348. val = v3_readw(V3_SYSTEM);
  349. val |= V3_SYSTEM_M_LOCK;
  350. v3_writew(V3_SYSTEM, val);
  351. /*
  352. * Configure and register the PCI hose
  353. */
  354. hose->first_busno = 0;
  355. hose->last_busno = 0xff;
  356. /* System memory space, window 0 256 MB non-prefetchable */
  357. pci_set_region(hose->regions + 0,
  358. PCI_BUS_NONMEM_START, PHYS_PCI_MEM_BASE,
  359. SZ_256M,
  360. PCI_REGION_MEM);
  361. /* System memory space, window 1 256 MB prefetchable */
  362. pci_set_region(hose->regions + 1,
  363. PCI_BUS_PREMEM_START, PHYS_PCI_MEM_BASE + SZ_256M,
  364. SZ_256M,
  365. PCI_REGION_MEM |
  366. PCI_REGION_PREFETCH);
  367. /* PCI I/O space */
  368. pci_set_region(hose->regions + 2,
  369. 0x00000000, PHYS_PCI_IO_BASE, 0x01000000,
  370. PCI_REGION_IO);
  371. /* PCI Memory - config space */
  372. pci_set_region(hose->regions + 3,
  373. 0x00000000, PHYS_PCI_CONFIG_BASE, 0x01000000,
  374. PCI_REGION_MEM);
  375. /* PCI V3 regs */
  376. pci_set_region(hose->regions + 4,
  377. 0x00000000, PHYS_PCI_V3_BASE, 0x01000000,
  378. PCI_REGION_MEM);
  379. hose->region_count = 5;
  380. pci_set_ops(hose,
  381. pci_integrator_read_byte,
  382. pci_integrator_read__word,
  383. pci_integrator_read_dword,
  384. pci_integrator_write_byte,
  385. pci_integrator_write_word,
  386. pci_integrator_write_dword);
  387. pci_register_hose(hose);
  388. pciauto_config_init(hose);
  389. pciauto_config_device(hose, 0);
  390. hose->last_busno = pci_hose_scan(hose);
  391. }