pci.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  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. * See file CREDITS for list of people who contributed to this
  18. * project.
  19. *
  20. * This program is free software; you can redistribute it and/or
  21. * modify it under the terms of the GNU General Public License as
  22. * published by the Free Software Foundation; either version 2 of
  23. * the License, or (at your option) any later version.
  24. *
  25. * This program is distributed in the hope that it will be useful,
  26. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  27. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  28. * GNU General Public License for more details.
  29. *
  30. * You should have received a copy of the GNU General Public License
  31. * along with this program; if not, write to the Free Software
  32. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  33. * MA 02111-1307 USA
  34. */
  35. #include <common.h>
  36. #include <pci.h>
  37. /*
  38. * Initialize PCI Devices, report devices found.
  39. */
  40. #ifndef CONFIG_PCI_PNP
  41. static struct pci_config_table pci_integrator_config_table[] = {
  42. { PCI_ANY_ID, PCI_ANY_ID, PCI_ANY_ID, PCI_ANY_ID, 0x0f, PCI_ANY_ID,
  43. pci_cfgfunc_config_device, { PCI_ENET0_IOADDR,
  44. PCI_ENET0_MEMADDR,
  45. PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER }},
  46. { }
  47. };
  48. #endif /* CONFIG_PCI_PNP */
  49. /* V3 access routines */
  50. #define _V3Write16(o,v) (*(volatile unsigned short *)(PCI_V3_BASE + (unsigned int)(o)) = (unsigned short)(v))
  51. #define _V3Read16(o) (*(volatile unsigned short *)(PCI_V3_BASE + (unsigned int)(o)))
  52. #define _V3Write32(o,v) (*(volatile unsigned int *)(PCI_V3_BASE + (unsigned int)(o)) = (unsigned int)(v))
  53. #define _V3Read32(o) (*(volatile unsigned int *)(PCI_V3_BASE + (unsigned int)(o)))
  54. /* Compute address necessary to access PCI config space for the given */
  55. /* bus and device. */
  56. #define PCI_CONFIG_ADDRESS( __bus, __devfn, __offset ) ({ \
  57. unsigned int __address, __devicebit; \
  58. unsigned short __mapaddress; \
  59. unsigned int __dev = PCI_DEV (__devfn); /* FIXME to check!! (slot?) */ \
  60. \
  61. if (__bus == 0) { \
  62. /* local bus segment so need a type 0 config cycle */ \
  63. /* build the PCI configuration "address" with one-hot in A31-A11 */ \
  64. __address = PCI_CONFIG_BASE; \
  65. __address |= ((__devfn & 0x07) << 8); \
  66. __address |= __offset & 0xFF; \
  67. __mapaddress = 0x000A; /* 101=>config cycle, 0=>A1=A0=0 */ \
  68. __devicebit = (1 << (__dev + 11)); \
  69. \
  70. if ((__devicebit & 0xFF000000) != 0) { \
  71. /* high order bits are handled by the MAP register */ \
  72. __mapaddress |= (__devicebit >> 16); \
  73. } else { \
  74. /* low order bits handled directly in the address */ \
  75. __address |= __devicebit; \
  76. } \
  77. } else { /* bus !=0 */ \
  78. /* not the local bus segment so need a type 1 config cycle */ \
  79. /* A31-A24 are don't care (so clear to 0) */ \
  80. __mapaddress = 0x000B; /* 101=>config cycle, 1=>A1&A0 from PCI_CFG */ \
  81. __address = PCI_CONFIG_BASE; \
  82. __address |= ((__bus & 0xFF) << 16); /* bits 23..16 = bus number */ \
  83. __address |= ((__dev & 0x1F) << 11); /* bits 15..11 = device number */ \
  84. __address |= ((__devfn & 0x07) << 8); /* bits 10..8 = function number */ \
  85. __address |= __offset & 0xFF; /* bits 7..0 = register number */ \
  86. } \
  87. _V3Write16 (V3_LB_MAP1, __mapaddress); \
  88. __address; \
  89. })
  90. /* _V3OpenConfigWindow - open V3 configuration window */
  91. #define _V3OpenConfigWindow() { \
  92. /* Set up base0 to see all 512Mbytes of memory space (not */ \
  93. /* prefetchable), this frees up base1 for re-use by configuration*/ \
  94. /* memory */ \
  95. \
  96. _V3Write32 (V3_LB_BASE0, ((INTEGRATOR_PCI_BASE & 0xFFF00000) | \
  97. 0x90 | V3_LB_BASE_M_ENABLE)); \
  98. /* Set up base1 to point into configuration space, note that MAP1 */ \
  99. /* register is set up by pciMakeConfigAddress(). */ \
  100. \
  101. _V3Write32 (V3_LB_BASE1, ((CPU_PCI_CNFG_ADRS & 0xFFF00000) | \
  102. 0x40 | V3_LB_BASE_M_ENABLE)); \
  103. }
  104. /* _V3CloseConfigWindow - close V3 configuration window */
  105. #define _V3CloseConfigWindow() { \
  106. /* Reassign base1 for use by prefetchable PCI memory */ \
  107. _V3Write32 (V3_LB_BASE1, (((INTEGRATOR_PCI_BASE + 0x10000000) & 0xFFF00000) \
  108. | 0x84 | V3_LB_BASE_M_ENABLE)); \
  109. _V3Write16 (V3_LB_MAP1, \
  110. (((INTEGRATOR_PCI_BASE + 0x10000000) & 0xFFF00000) >> 16) | 0x0006); \
  111. \
  112. /* And shrink base0 back to a 256M window (NOTE: MAP0 already correct) */ \
  113. \
  114. _V3Write32 (V3_LB_BASE0, ((INTEGRATOR_PCI_BASE & 0xFFF00000) | \
  115. 0x80 | V3_LB_BASE_M_ENABLE)); \
  116. }
  117. static int pci_integrator_read_byte (struct pci_controller *hose, pci_dev_t dev,
  118. int offset, unsigned char *val)
  119. {
  120. _V3OpenConfigWindow ();
  121. *val = *(volatile unsigned char *) PCI_CONFIG_ADDRESS (PCI_BUS (dev),
  122. PCI_FUNC (dev),
  123. offset);
  124. _V3CloseConfigWindow ();
  125. return 0;
  126. }
  127. static int pci_integrator_read__word (struct pci_controller *hose,
  128. pci_dev_t dev, int offset,
  129. unsigned short *val)
  130. {
  131. _V3OpenConfigWindow ();
  132. *val = *(volatile unsigned short *) PCI_CONFIG_ADDRESS (PCI_BUS (dev),
  133. PCI_FUNC (dev),
  134. offset);
  135. _V3CloseConfigWindow ();
  136. return 0;
  137. }
  138. static int pci_integrator_read_dword (struct pci_controller *hose,
  139. pci_dev_t dev, int offset,
  140. unsigned int *val)
  141. {
  142. _V3OpenConfigWindow ();
  143. *val = *(volatile unsigned short *) PCI_CONFIG_ADDRESS (PCI_BUS (dev),
  144. PCI_FUNC (dev),
  145. offset);
  146. *val |= (*(volatile unsigned int *)
  147. PCI_CONFIG_ADDRESS (PCI_BUS (dev), PCI_FUNC (dev),
  148. (offset + 2))) << 16;
  149. _V3CloseConfigWindow ();
  150. return 0;
  151. }
  152. static int pci_integrator_write_byte (struct pci_controller *hose,
  153. pci_dev_t dev, int offset,
  154. unsigned char val)
  155. {
  156. _V3OpenConfigWindow ();
  157. *(volatile unsigned char *) PCI_CONFIG_ADDRESS (PCI_BUS (dev),
  158. PCI_FUNC (dev),
  159. offset) = val;
  160. _V3CloseConfigWindow ();
  161. return 0;
  162. }
  163. static int pci_integrator_write_word (struct pci_controller *hose,
  164. pci_dev_t dev, int offset,
  165. unsigned short val)
  166. {
  167. _V3OpenConfigWindow ();
  168. *(volatile unsigned short *) PCI_CONFIG_ADDRESS (PCI_BUS (dev),
  169. PCI_FUNC (dev),
  170. offset) = val;
  171. _V3CloseConfigWindow ();
  172. return 0;
  173. }
  174. static int pci_integrator_write_dword (struct pci_controller *hose,
  175. pci_dev_t dev, int offset,
  176. unsigned int val)
  177. {
  178. _V3OpenConfigWindow ();
  179. *(volatile unsigned short *) PCI_CONFIG_ADDRESS (PCI_BUS (dev),
  180. PCI_FUNC (dev),
  181. offset) = (val & 0xFFFF);
  182. *(volatile unsigned short *) PCI_CONFIG_ADDRESS (PCI_BUS (dev),
  183. PCI_FUNC (dev),
  184. (offset + 2)) = ((val >> 16) & 0xFFFF);
  185. _V3CloseConfigWindow ();
  186. return 0;
  187. }
  188. /******************************
  189. * PCI initialisation
  190. ******************************/
  191. struct pci_controller integrator_hose = {
  192. #ifndef CONFIG_PCI_PNP
  193. config_table: pci_integrator_config_table,
  194. #endif
  195. };
  196. void pci_init_board (void)
  197. {
  198. volatile int i, j;
  199. struct pci_controller *hose = &integrator_hose;
  200. /* setting this register will take the V3 out of reset */
  201. *(volatile unsigned int *) (INTEGRATOR_SC_PCIENABLE) = 1;
  202. /* wait a few usecs to settle the device and the PCI bus */
  203. for (i = 0; i < 100; i++)
  204. j = i + 1;
  205. /* Now write the Base I/O Address Word to V3_BASE + 0x6C */
  206. *(volatile unsigned short *) (V3_BASE + V3_LB_IO_BASE) =
  207. (unsigned short) (V3_BASE >> 16);
  208. do {
  209. *(volatile unsigned char *) (V3_BASE + V3_MAIL_DATA) = 0xAA;
  210. *(volatile unsigned char *) (V3_BASE + V3_MAIL_DATA + 4) =
  211. 0x55;
  212. } while (*(volatile unsigned char *) (V3_BASE + V3_MAIL_DATA) != 0xAA
  213. || *(volatile unsigned char *) (V3_BASE + V3_MAIL_DATA +
  214. 4) != 0x55);
  215. /* Make sure that V3 register access is not locked, if it is, unlock it */
  216. if ((*(volatile unsigned short *) (V3_BASE + V3_SYSTEM) &
  217. V3_SYSTEM_M_LOCK)
  218. == V3_SYSTEM_M_LOCK)
  219. *(volatile unsigned short *) (V3_BASE + V3_SYSTEM) = 0xA05F;
  220. /* Ensure that the slave accesses from PCI are disabled while we */
  221. /* setup windows */
  222. *(volatile unsigned short *) (V3_BASE + V3_PCI_CMD) &=
  223. ~(V3_COMMAND_M_MEM_EN | V3_COMMAND_M_IO_EN);
  224. /* Clear RST_OUT to 0; keep the PCI bus in reset until we've finished */
  225. *(volatile unsigned short *) (V3_BASE + V3_SYSTEM) &=
  226. ~V3_SYSTEM_M_RST_OUT;
  227. /* Make all accesses from PCI space retry until we're ready for them */
  228. *(volatile unsigned short *) (V3_BASE + V3_PCI_CFG) |=
  229. V3_PCI_CFG_M_RETRY_EN;
  230. /* Set up any V3 PCI Configuration Registers that we absolutely have to */
  231. /* LB_CFG controls Local Bus protocol. */
  232. /* Enable LocalBus byte strobes for READ accesses too. */
  233. /* set bit 7 BE_IMODE and bit 6 BE_OMODE */
  234. *(volatile unsigned short *) (V3_BASE + V3_LB_CFG) |= 0x0C0;
  235. /* PCI_CMD controls overall PCI operation. */
  236. /* Enable PCI bus master. */
  237. *(volatile unsigned short *) (V3_BASE + V3_PCI_CMD) |= 0x04;
  238. /* PCI_MAP0 controls where the PCI to CPU memory window is on Local Bus */
  239. *(volatile unsigned int *) (V3_BASE + V3_PCI_MAP0) =
  240. (INTEGRATOR_BOOT_ROM_BASE) | (V3_PCI_MAP_M_ADR_SIZE_512M |
  241. V3_PCI_MAP_M_REG_EN |
  242. V3_PCI_MAP_M_ENABLE);
  243. /* PCI_BASE0 is the PCI address of the start of the window */
  244. *(volatile unsigned int *) (V3_BASE + V3_PCI_BASE0) =
  245. INTEGRATOR_BOOT_ROM_BASE;
  246. /* PCI_MAP1 is LOCAL address of the start of the window */
  247. *(volatile unsigned int *) (V3_BASE + V3_PCI_MAP1) =
  248. (INTEGRATOR_HDR0_SDRAM_BASE) | (V3_PCI_MAP_M_ADR_SIZE_1024M |
  249. V3_PCI_MAP_M_REG_EN |
  250. V3_PCI_MAP_M_ENABLE);
  251. /* PCI_BASE1 is the PCI address of the start of the window */
  252. *(volatile unsigned int *) (V3_BASE + V3_PCI_BASE1) =
  253. INTEGRATOR_HDR0_SDRAM_BASE;
  254. /* Set up the windows from local bus memory into PCI configuration, */
  255. /* I/O and Memory. */
  256. /* PCI I/O, LB_BASE2 and LB_MAP2 are used exclusively for this. */
  257. *(volatile unsigned short *) (V3_BASE + V3_LB_BASE2) =
  258. ((CPU_PCI_IO_ADRS >> 24) << 8) | V3_LB_BASE_M_ENABLE;
  259. *(volatile unsigned short *) (V3_BASE + V3_LB_MAP2) = 0;
  260. /* PCI Configuration, use LB_BASE1/LB_MAP1. */
  261. /* PCI Memory use LB_BASE0/LB_MAP0 and LB_BASE1/LB_MAP1 */
  262. /* Map first 256Mbytes as non-prefetchable via BASE0/MAP0 */
  263. /* (INTEGRATOR_PCI_BASE == PCI_MEM_BASE) */
  264. *(volatile unsigned int *) (V3_BASE + V3_LB_BASE0) =
  265. INTEGRATOR_PCI_BASE | (0x80 | V3_LB_BASE_M_ENABLE);
  266. *(volatile unsigned short *) (V3_BASE + V3_LB_MAP0) =
  267. ((INTEGRATOR_PCI_BASE >> 20) << 0x4) | 0x0006;
  268. /* Map second 256 Mbytes as prefetchable via BASE1/MAP1 */
  269. *(volatile unsigned int *) (V3_BASE + V3_LB_BASE1) =
  270. INTEGRATOR_PCI_BASE | (0x84 | V3_LB_BASE_M_ENABLE);
  271. *(volatile unsigned short *) (V3_BASE + V3_LB_MAP1) =
  272. (((INTEGRATOR_PCI_BASE + 0x10000000) >> 20) << 4) | 0x0006;
  273. /* Allow accesses to PCI Configuration space */
  274. /* and set up A1, A0 for type 1 config cycles */
  275. *(volatile unsigned short *) (V3_BASE + V3_PCI_CFG) =
  276. ((*(volatile unsigned short *) (V3_BASE + V3_PCI_CFG)) &
  277. ~(V3_PCI_CFG_M_RETRY_EN | V3_PCI_CFG_M_AD_LOW1)) |
  278. V3_PCI_CFG_M_AD_LOW0;
  279. /* now we can allow in PCI MEMORY accesses */
  280. *(volatile unsigned short *) (V3_BASE + V3_PCI_CMD) =
  281. (*(volatile unsigned short *) (V3_BASE + V3_PCI_CMD)) |
  282. V3_COMMAND_M_MEM_EN;
  283. /* Set RST_OUT to take the PCI bus is out of reset, PCI devices can */
  284. /* initialise and lock the V3 system register so that no one else */
  285. /* can play with it */
  286. *(volatile unsigned short *) (V3_BASE + V3_SYSTEM) =
  287. (*(volatile unsigned short *) (V3_BASE + V3_SYSTEM)) |
  288. V3_SYSTEM_M_RST_OUT;
  289. *(volatile unsigned short *) (V3_BASE + V3_SYSTEM) =
  290. (*(volatile unsigned short *) (V3_BASE + V3_SYSTEM)) |
  291. V3_SYSTEM_M_LOCK;
  292. /*
  293. * Register the hose
  294. */
  295. hose->first_busno = 0;
  296. hose->last_busno = 0xff;
  297. /* System memory space */
  298. pci_set_region (hose->regions + 0,
  299. 0x00000000, 0x40000000, 0x01000000,
  300. PCI_REGION_MEM | PCI_REGION_SYS_MEMORY);
  301. /* PCI Memory - config space */
  302. pci_set_region (hose->regions + 1,
  303. 0x00000000, 0x62000000, 0x01000000, PCI_REGION_MEM);
  304. /* PCI V3 regs */
  305. pci_set_region (hose->regions + 2,
  306. 0x00000000, 0x61000000, 0x00080000, PCI_REGION_MEM);
  307. /* PCI I/O space */
  308. pci_set_region (hose->regions + 3,
  309. 0x00000000, 0x60000000, 0x00010000, PCI_REGION_IO);
  310. pci_set_ops (hose,
  311. pci_integrator_read_byte,
  312. pci_integrator_read__word,
  313. pci_integrator_read_dword,
  314. pci_integrator_write_byte,
  315. pci_integrator_write_word, pci_integrator_write_dword);
  316. hose->region_count = 4;
  317. pci_register_hose (hose);
  318. pciauto_config_init (hose);
  319. pciauto_config_device (hose, 0);
  320. hose->last_busno = pci_hose_scan (hose);
  321. }