pci.c 13 KB

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