pci.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. /*
  2. *
  3. * (C) Copyright 2000
  4. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  5. *
  6. * (C) Copyright 2001
  7. * James Dougherty (jfd@cs.stanford.edu)
  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. /*
  28. * PCI Configuration space access support for MPC824x/MPC107 PCI Bridge
  29. */
  30. #include <common.h>
  31. #include <mpc824x.h>
  32. #include <pci.h>
  33. #include "mousse.h"
  34. /*
  35. * Promise ATA/66 support.
  36. */
  37. #define XFER_PIO_4 0x0C /* 0000|1100 */
  38. #define XFER_PIO_3 0x0B /* 0000|1011 */
  39. #define XFER_PIO_2 0x0A /* 0000|1010 */
  40. #define XFER_PIO_1 0x09 /* 0000|1001 */
  41. #define XFER_PIO_0 0x08 /* 0000|1000 */
  42. #define XFER_PIO_SLOW 0x00 /* 0000|0000 */
  43. /* Promise Regs */
  44. #define REG_A 0x01
  45. #define REG_B 0x02
  46. #define REG_C 0x04
  47. #define REG_D 0x08
  48. void
  49. pdc202xx_decode_registers (unsigned char registers, unsigned char value)
  50. {
  51. unsigned char bit = 0, bit1 = 0, bit2 = 0;
  52. switch(registers) {
  53. case REG_A:
  54. bit2 = 0;
  55. printf(" A Register ");
  56. if (value & 0x80) printf("SYNC_IN ");
  57. if (value & 0x40) printf("ERRDY_EN ");
  58. if (value & 0x20) printf("IORDY_EN ");
  59. if (value & 0x10) printf("PREFETCH_EN ");
  60. if (value & 0x08) { printf("PA3 ");bit2 |= 0x08; }
  61. if (value & 0x04) { printf("PA2 ");bit2 |= 0x04; }
  62. if (value & 0x02) { printf("PA1 ");bit2 |= 0x02; }
  63. if (value & 0x01) { printf("PA0 ");bit2 |= 0x01; }
  64. printf("PIO(A) = %d ", bit2);
  65. break;
  66. case REG_B:
  67. bit1 = 0;bit2 = 0;
  68. printf(" B Register ");
  69. if (value & 0x80) { printf("MB2 ");bit1 |= 0x80; }
  70. if (value & 0x40) { printf("MB1 ");bit1 |= 0x40; }
  71. if (value & 0x20) { printf("MB0 ");bit1 |= 0x20; }
  72. printf("DMA(B) = %d ", bit1 >> 5);
  73. if (value & 0x10) printf("PIO_FORCED/PB4 ");
  74. if (value & 0x08) { printf("PB3 ");bit2 |= 0x08; }
  75. if (value & 0x04) { printf("PB2 ");bit2 |= 0x04; }
  76. if (value & 0x02) { printf("PB1 ");bit2 |= 0x02; }
  77. if (value & 0x01) { printf("PB0 ");bit2 |= 0x01; }
  78. printf("PIO(B) = %d ", bit2);
  79. break;
  80. case REG_C:
  81. bit2 = 0;
  82. printf(" C Register ");
  83. if (value & 0x80) printf("DMARQp ");
  84. if (value & 0x40) printf("IORDYp ");
  85. if (value & 0x20) printf("DMAR_EN ");
  86. if (value & 0x10) printf("DMAW_EN ");
  87. if (value & 0x08) { printf("MC3 ");bit2 |= 0x08; }
  88. if (value & 0x04) { printf("MC2 ");bit2 |= 0x04; }
  89. if (value & 0x02) { printf("MC1 ");bit2 |= 0x02; }
  90. if (value & 0x01) { printf("MC0 ");bit2 |= 0x01; }
  91. printf("DMA(C) = %d ", bit2);
  92. break;
  93. case REG_D:
  94. printf(" D Register ");
  95. break;
  96. default:
  97. return;
  98. }
  99. printf("\n %s ", (registers & REG_D) ? "DP" :
  100. (registers & REG_C) ? "CP" :
  101. (registers & REG_B) ? "BP" :
  102. (registers & REG_A) ? "AP" : "ERROR");
  103. for (bit=128;bit>0;bit/=2)
  104. printf("%s", (value & bit) ? "1" : "0");
  105. printf("\n");
  106. }
  107. /*
  108. * Promise ATA/66 Support: configure Promise ATA66 card in specified mode.
  109. */
  110. int
  111. pdc202xx_tune_chipset (pci_dev_t dev, int drive, unsigned char speed)
  112. {
  113. unsigned short drive_conf;
  114. int err = 0;
  115. unsigned char drive_pci, AP, BP, CP, DP;
  116. unsigned char TA = 0, TB = 0;
  117. switch (drive) {
  118. case 0: drive_pci = 0x60; break;
  119. case 1: drive_pci = 0x64; break;
  120. case 2: drive_pci = 0x68; break;
  121. case 3: drive_pci = 0x6c; break;
  122. default: return -1;
  123. }
  124. pci_read_config_word(dev, drive_pci, &drive_conf);
  125. pci_read_config_byte(dev, (drive_pci), &AP);
  126. pci_read_config_byte(dev, (drive_pci)|0x01, &BP);
  127. pci_read_config_byte(dev, (drive_pci)|0x02, &CP);
  128. pci_read_config_byte(dev, (drive_pci)|0x03, &DP);
  129. if ((AP & 0x0F) || (BP & 0x07)) {
  130. /* clear PIO modes of lower 8421 bits of A Register */
  131. pci_write_config_byte(dev, (drive_pci), AP & ~0x0F);
  132. pci_read_config_byte(dev, (drive_pci), &AP);
  133. /* clear PIO modes of lower 421 bits of B Register */
  134. pci_write_config_byte(dev, (drive_pci)|0x01, BP & ~0x07);
  135. pci_read_config_byte(dev, (drive_pci)|0x01, &BP);
  136. pci_read_config_byte(dev, (drive_pci), &AP);
  137. pci_read_config_byte(dev, (drive_pci)|0x01, &BP);
  138. }
  139. pci_read_config_byte(dev, (drive_pci), &AP);
  140. pci_read_config_byte(dev, (drive_pci)|0x01, &BP);
  141. pci_read_config_byte(dev, (drive_pci)|0x02, &CP);
  142. switch(speed) {
  143. case XFER_PIO_4: TA = 0x01; TB = 0x04; break;
  144. case XFER_PIO_3: TA = 0x02; TB = 0x06; break;
  145. case XFER_PIO_2: TA = 0x03; TB = 0x08; break;
  146. case XFER_PIO_1: TA = 0x05; TB = 0x0C; break;
  147. case XFER_PIO_0:
  148. default: TA = 0x09; TB = 0x13; break;
  149. }
  150. pci_write_config_byte(dev, (drive_pci), AP|TA);
  151. pci_write_config_byte(dev, (drive_pci)|0x01, BP|TB);
  152. pci_read_config_byte(dev, (drive_pci), &AP);
  153. pci_read_config_byte(dev, (drive_pci)|0x01, &BP);
  154. pci_read_config_byte(dev, (drive_pci)|0x02, &CP);
  155. pci_read_config_byte(dev, (drive_pci)|0x03, &DP);
  156. #ifdef PDC202XX_DEBUG
  157. pdc202xx_decode_registers(REG_A, AP);
  158. pdc202xx_decode_registers(REG_B, BP);
  159. pdc202xx_decode_registers(REG_C, CP);
  160. pdc202xx_decode_registers(REG_D, DP);
  161. #endif
  162. return err;
  163. }
  164. /*
  165. * Show/Init PCI devices on the specified bus number.
  166. */
  167. void pci_mousse_fixup_irq(struct pci_controller *hose, pci_dev_t dev)
  168. {
  169. unsigned int line;
  170. switch(PCI_DEV(dev)) {
  171. case 0x0d:
  172. line = 0x00000101;
  173. break;
  174. case 0x0e:
  175. default:
  176. line = 0x00000303;
  177. break;
  178. }
  179. pci_write_config_dword(dev, PCI_INTERRUPT_LINE, line);
  180. }
  181. void pci_mousse_setup_pdc202xx(struct pci_controller *hose, pci_dev_t dev,
  182. struct pci_config_table *_)
  183. {
  184. unsigned short vendorId;
  185. unsigned int mbar0, cmd;
  186. int bar, a;
  187. pci_read_config_word(dev, PCI_VENDOR_ID, &vendorId);
  188. if(vendorId == PCI_VENDOR_ID_PROMISE || vendorId == PCI_VENDOR_ID_CMD){
  189. /* PDC 202xx card is handled differently, it is a bootable
  190. * device and needs all 5 MBAR's configured
  191. */
  192. for(bar = 0; bar < 5; bar++){
  193. pci_read_config_dword(dev, PCI_BASE_ADDRESS_0+bar*4, &mbar0);
  194. pci_write_config_dword(dev, PCI_BASE_ADDRESS_0+bar*4, ~0);
  195. pci_read_config_dword(dev, PCI_BASE_ADDRESS_0+bar*4, &mbar0);
  196. #ifdef DEBUG
  197. printf(" ATA_bar[%d] = %dbytes\n", bar,
  198. ~(mbar0 & PCI_BASE_ADDRESS_MEM_MASK) + 1);
  199. #endif
  200. }
  201. /* Program all BAR's */
  202. pci_write_config_dword(dev, PCI_BASE_ADDRESS_0, PROMISE_MBAR0);
  203. pci_write_config_dword(dev, PCI_BASE_ADDRESS_1, PROMISE_MBAR1);
  204. pci_write_config_dword(dev, PCI_BASE_ADDRESS_2, PROMISE_MBAR2);
  205. pci_write_config_dword(dev, PCI_BASE_ADDRESS_3, PROMISE_MBAR3);
  206. pci_write_config_dword(dev, PCI_BASE_ADDRESS_4, PROMISE_MBAR4);
  207. pci_write_config_dword(dev, PCI_BASE_ADDRESS_5, PROMISE_MBAR5);
  208. for(bar = 0; bar < 5; bar++){
  209. pci_read_config_dword(dev, PCI_BASE_ADDRESS_0+bar*4, &mbar0);
  210. #ifdef DEBUG
  211. printf(" ATA_bar[%d]@0x%x\n", bar, mbar0);
  212. #endif
  213. }
  214. /* Enable ROM Expansion base */
  215. pci_write_config_dword(dev, PCI_ROM_ADDRESS, PROMISE_MBAR5|1);
  216. /* Io enable, Memory enable, master enable */
  217. pci_read_config_dword(dev, PCI_COMMAND, &cmd);
  218. cmd &= ~0xffff0000;
  219. cmd |= PCI_COMMAND_MASTER | PCI_COMMAND_MEMORY | PCI_COMMAND_IO;
  220. pci_write_config_dword(dev, PCI_COMMAND, cmd);
  221. /* Breath some life into the controller */
  222. for( a = 0; a < 4; a++)
  223. pdc202xx_tune_chipset(dev, a, XFER_PIO_0);
  224. }
  225. }
  226. static struct pci_config_table pci_sandpoint_config_table[] = {
  227. { PCI_ANY_ID, PCI_ANY_ID, PCI_ANY_ID, 0x00, 0x0e, 0x00,
  228. pci_mousse_setup_pdc202xx },
  229. #ifndef CONFIG_PCI_PNP
  230. { PCI_ANY_ID, PCI_ANY_ID, PCI_ANY_ID, 0x00, 0x0d, 0x00,
  231. pci_cfgfunc_config_device, {PCI_ENET_IOADDR,
  232. PCI_ENET_MEMADDR,
  233. PCI_COMMAND_MEMORY |
  234. PCI_COMMAND_MASTER}},
  235. { PCI_ANY_ID, PCI_ANY_ID, PCI_ANY_ID, PCI_ANY_ID, PCI_ANY_ID, PCI_ANY_ID,
  236. pci_cfgfunc_config_device, {PCI_SLOT_IOADDR,
  237. PCI_SLOT_MEMADDR,
  238. PCI_COMMAND_MEMORY |
  239. PCI_COMMAND_MASTER}},
  240. #endif
  241. { }
  242. };
  243. struct pci_controller hose = {
  244. config_table: pci_sandpoint_config_table,
  245. fixup_irq: pci_mousse_fixup_irq,
  246. };
  247. void pci_init_board(void)
  248. {
  249. pci_mpc824x_init(&hose);
  250. }