ata_piix.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. /*
  2. * Copyright (C) Procsys. All rights reserved.
  3. * Author: Mushtaq Khan <mushtaq_k@procsys.com>
  4. * <mushtaqk_921@yahoo.co.in>
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License as
  8. * published by the Free Software Foundation; either version 2 of
  9. * the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  19. * MA 02111-1307 USA
  20. *
  21. * with the reference to ata_piix driver in kernel 2.4.32
  22. */
  23. /*
  24. * This file contains SATA controller and SATA drive initialization functions
  25. */
  26. #include <common.h>
  27. #include <pci.h>
  28. #include <command.h>
  29. #include <config.h>
  30. #include <asm/byteorder.h>
  31. #include <ide.h>
  32. #include <ata.h>
  33. #ifdef CFG_ATA_PIIX /*ata_piix driver */
  34. #define DEBUG_SATA 0 /*For debug prints set DEBUG_SATA to 1 */
  35. #define DRV_DECL /*For file specific declarations */
  36. #include <sata.h>
  37. #undef DRV_DECL
  38. /*Macros realted to PCI*/
  39. #define PCI_SATA_BUS 0x00
  40. #define PCI_SATA_DEV 0x1f
  41. #define PCI_SATA_FUNC 0x02
  42. #define PCI_SATA_BASE1 0x10
  43. #define PCI_SATA_BASE2 0x14
  44. #define PCI_SATA_BASE3 0x18
  45. #define PCI_SATA_BASE4 0x1c
  46. #define PCI_SATA_BASE5 0x20
  47. #define PCI_PMR 0x90
  48. #define PCI_PI 0x09
  49. #define PCI_PCS 0x92
  50. #define PCI_DMA_CTL 0x48
  51. #define PORT_PRESENT (1<<0)
  52. #define PORT_ENABLED (1<<4)
  53. u32 bdf;
  54. u32 iobase1 = 0; /*Primary cmd block */
  55. u32 iobase2 = 0; /*Primary ctl block */
  56. u32 iobase3 = 0; /*Sec cmd block */
  57. u32 iobase4 = 0; /*sec ctl block */
  58. u32 iobase5 = 0; /*BMDMA*/
  59. int
  60. pci_sata_init (void)
  61. {
  62. u32 bus = PCI_SATA_BUS;
  63. u32 dev = PCI_SATA_DEV;
  64. u32 fun = PCI_SATA_FUNC;
  65. u16 cmd = 0;
  66. u8 lat = 0, pcibios_max_latency = 0xff;
  67. u8 pmr; /*Port mapping reg */
  68. u8 pi; /*Prgming Interface reg */
  69. bdf = PCI_BDF (bus, dev, fun);
  70. pci_read_config_dword (bdf, PCI_SATA_BASE1, &iobase1);
  71. pci_read_config_dword (bdf, PCI_SATA_BASE2, &iobase2);
  72. pci_read_config_dword (bdf, PCI_SATA_BASE3, &iobase3);
  73. pci_read_config_dword (bdf, PCI_SATA_BASE4, &iobase4);
  74. pci_read_config_dword (bdf, PCI_SATA_BASE5, &iobase5);
  75. if ((iobase1 == 0xFFFFFFFF) || (iobase2 == 0xFFFFFFFF) ||
  76. (iobase3 == 0xFFFFFFFF) || (iobase4 == 0xFFFFFFFF) ||
  77. (iobase5 == 0xFFFFFFFF)) {
  78. printf ("error no base addr for SATA controller\n");
  79. return 1;
  80. /*ERROR*/}
  81. iobase1 &= 0xFFFFFFFE;
  82. iobase2 &= 0xFFFFFFFE;
  83. iobase3 &= 0xFFFFFFFE;
  84. iobase4 &= 0xFFFFFFFE;
  85. iobase5 &= 0xFFFFFFFE;
  86. /*check for mode */
  87. pci_read_config_byte (bdf, PCI_PMR, &pmr);
  88. if (pmr > 1) {
  89. printf ("combined mode not supported\n");
  90. return 1;
  91. }
  92. pci_read_config_byte (bdf, PCI_PI, &pi);
  93. if ((pi & 0x05) != 0x05) {
  94. printf ("Sata is in Legacy mode\n");
  95. return 1;
  96. } else {
  97. printf ("sata is in Native mode\n");
  98. }
  99. /*MASTER CFG AND IO CFG */
  100. pci_read_config_word (bdf, PCI_COMMAND, &cmd);
  101. cmd |= PCI_COMMAND_MASTER | PCI_COMMAND_IO;
  102. pci_write_config_word (bdf, PCI_COMMAND, cmd);
  103. pci_read_config_byte (dev, PCI_LATENCY_TIMER, &lat);
  104. if (lat < 16)
  105. lat = (64 <= pcibios_max_latency) ? 64 : pcibios_max_latency;
  106. else if (lat > pcibios_max_latency)
  107. lat = pcibios_max_latency;
  108. pci_write_config_byte (dev, PCI_LATENCY_TIMER, lat);
  109. return 0;
  110. }
  111. int
  112. sata_bus_probe (int port_no)
  113. {
  114. int orig_mask, mask;
  115. u16 pcs;
  116. mask = (PORT_PRESENT << port_no);
  117. pci_read_config_word (bdf, PCI_PCS, &pcs);
  118. orig_mask = (int) pcs & 0xff;
  119. if ((orig_mask & mask) != mask)
  120. return 0;
  121. else
  122. return 1;
  123. }
  124. int
  125. init_sata (void)
  126. {
  127. u8 i, rv = 0;
  128. for (i = 0; i < CFG_SATA_MAXDEVICES; i++) {
  129. sata_dev_desc[i].type = DEV_TYPE_UNKNOWN;
  130. sata_dev_desc[i].if_type = IF_TYPE_IDE;
  131. sata_dev_desc[i].dev = i;
  132. sata_dev_desc[i].part_type = PART_TYPE_UNKNOWN;
  133. sata_dev_desc[i].blksz = 0;
  134. sata_dev_desc[i].lba = 0;
  135. sata_dev_desc[i].block_read = sata_read;
  136. }
  137. rv = pci_sata_init ();
  138. if (rv == 1) {
  139. printf ("pci initialization failed\n");
  140. return 1;
  141. }
  142. port[0].port_no = 0;
  143. port[0].ioaddr.cmd_addr = iobase1;
  144. port[0].ioaddr.altstatus_addr = port[0].ioaddr.ctl_addr =
  145. iobase2 | ATA_PCI_CTL_OFS;
  146. port[0].ioaddr.bmdma_addr = iobase5;
  147. port[1].port_no = 1;
  148. port[1].ioaddr.cmd_addr = iobase3;
  149. port[1].ioaddr.altstatus_addr = port[1].ioaddr.ctl_addr =
  150. iobase4 | ATA_PCI_CTL_OFS;
  151. port[1].ioaddr.bmdma_addr = iobase5 + 0x8;
  152. for (i = 0; i < CFG_SATA_MAXBUS; i++)
  153. sata_port (&port[i].ioaddr);
  154. for (i = 0; i < CFG_SATA_MAXBUS; i++) {
  155. if (!(sata_bus_probe (i))) {
  156. port[i].port_state = 0;
  157. printf ("SATA#%d port is not present \n", i);
  158. } else {
  159. printf ("SATA#%d port is present\n", i);
  160. if (sata_bus_softreset (i)) {
  161. port[i].port_state = 0;
  162. } else {
  163. port[i].port_state = 1;
  164. }
  165. }
  166. }
  167. for (i = 0; i < CFG_SATA_MAXBUS; i++) {
  168. u8 j, devno;
  169. if (port[i].port_state == 0)
  170. continue;
  171. for (j = 0; j < CFG_SATA_DEVS_PER_BUS; j++) {
  172. sata_identify (i, j);
  173. set_Feature_cmd (i, j);
  174. devno = i * CFG_SATA_DEVS_PER_BUS + j;
  175. if ((sata_dev_desc[devno].lba > 0) &&
  176. (sata_dev_desc[devno].blksz > 0)) {
  177. dev_print (&sata_dev_desc[devno]);
  178. /* initialize partition type */
  179. init_part (&sata_dev_desc[devno]);
  180. if (curr_dev < 0)
  181. curr_dev =
  182. i * CFG_SATA_DEVS_PER_BUS + j;
  183. }
  184. }
  185. }
  186. return 0;
  187. }
  188. #endif