ata_piix.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756
  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 <asm/io.h>
  28. #include <pci.h>
  29. #include <command.h>
  30. #include <config.h>
  31. #include <asm/byteorder.h>
  32. #include <part.h>
  33. #include <ide.h>
  34. #include <ata.h>
  35. extern block_dev_desc_t sata_dev_desc[CONFIG_SYS_SATA_MAX_DEVICE];
  36. extern int sata_curr_device;
  37. #define DEBUG_SATA 0 /*For debug prints set DEBUG_SATA to 1 */
  38. #define SATA_DECL
  39. #define DRV_DECL /*For file specific declarations */
  40. #include "ata_piix.h"
  41. /*Macros realted to PCI*/
  42. #define PCI_SATA_BUS 0x00
  43. #define PCI_SATA_DEV 0x1f
  44. #define PCI_SATA_FUNC 0x02
  45. #define PCI_SATA_BASE1 0x10
  46. #define PCI_SATA_BASE2 0x14
  47. #define PCI_SATA_BASE3 0x18
  48. #define PCI_SATA_BASE4 0x1c
  49. #define PCI_SATA_BASE5 0x20
  50. #define PCI_PMR 0x90
  51. #define PCI_PI 0x09
  52. #define PCI_PCS 0x92
  53. #define PCI_DMA_CTL 0x48
  54. #define PORT_PRESENT (1<<0)
  55. #define PORT_ENABLED (1<<4)
  56. u32 bdf;
  57. u32 iobase1 = 0; /*Primary cmd block */
  58. u32 iobase2 = 0; /*Primary ctl block */
  59. u32 iobase3 = 0; /*Sec cmd block */
  60. u32 iobase4 = 0; /*sec ctl block */
  61. u32 iobase5 = 0; /*BMDMA*/
  62. int
  63. pci_sata_init (void)
  64. {
  65. u32 bus = PCI_SATA_BUS;
  66. u32 dev = PCI_SATA_DEV;
  67. u32 fun = PCI_SATA_FUNC;
  68. u16 cmd = 0;
  69. u8 lat = 0, pcibios_max_latency = 0xff;
  70. u8 pmr; /*Port mapping reg */
  71. u8 pi; /*Prgming Interface reg */
  72. bdf = PCI_BDF (bus, dev, fun);
  73. pci_read_config_dword (bdf, PCI_SATA_BASE1, &iobase1);
  74. pci_read_config_dword (bdf, PCI_SATA_BASE2, &iobase2);
  75. pci_read_config_dword (bdf, PCI_SATA_BASE3, &iobase3);
  76. pci_read_config_dword (bdf, PCI_SATA_BASE4, &iobase4);
  77. pci_read_config_dword (bdf, PCI_SATA_BASE5, &iobase5);
  78. if ((iobase1 == 0xFFFFFFFF) || (iobase2 == 0xFFFFFFFF) ||
  79. (iobase3 == 0xFFFFFFFF) || (iobase4 == 0xFFFFFFFF) ||
  80. (iobase5 == 0xFFFFFFFF)) {
  81. printf ("error no base addr for SATA controller\n");
  82. return 1;
  83. /*ERROR*/}
  84. iobase1 &= 0xFFFFFFFE;
  85. iobase2 &= 0xFFFFFFFE;
  86. iobase3 &= 0xFFFFFFFE;
  87. iobase4 &= 0xFFFFFFFE;
  88. iobase5 &= 0xFFFFFFFE;
  89. /*check for mode */
  90. pci_read_config_byte (bdf, PCI_PMR, &pmr);
  91. if (pmr > 1) {
  92. printf ("combined mode not supported\n");
  93. return 1;
  94. }
  95. pci_read_config_byte (bdf, PCI_PI, &pi);
  96. if ((pi & 0x05) != 0x05) {
  97. printf ("Sata is in Legacy mode\n");
  98. return 1;
  99. } else {
  100. printf ("sata is in Native mode\n");
  101. }
  102. /*MASTER CFG AND IO CFG */
  103. pci_read_config_word (bdf, PCI_COMMAND, &cmd);
  104. cmd |= PCI_COMMAND_MASTER | PCI_COMMAND_IO;
  105. pci_write_config_word (bdf, PCI_COMMAND, cmd);
  106. pci_read_config_byte (dev, PCI_LATENCY_TIMER, &lat);
  107. if (lat < 16)
  108. lat = (64 <= pcibios_max_latency) ? 64 : pcibios_max_latency;
  109. else if (lat > pcibios_max_latency)
  110. lat = pcibios_max_latency;
  111. pci_write_config_byte (dev, PCI_LATENCY_TIMER, lat);
  112. return 0;
  113. }
  114. int
  115. sata_bus_probe (int port_no)
  116. {
  117. int orig_mask, mask;
  118. u16 pcs;
  119. mask = (PORT_PRESENT << port_no);
  120. pci_read_config_word (bdf, PCI_PCS, &pcs);
  121. orig_mask = (int) pcs & 0xff;
  122. if ((orig_mask & mask) != mask)
  123. return 0;
  124. else
  125. return 1;
  126. }
  127. int
  128. init_sata (int dev)
  129. {
  130. static int done = 0;
  131. u8 i, rv = 0;
  132. if (!done)
  133. done = 1;
  134. else
  135. return 0;
  136. rv = pci_sata_init ();
  137. if (rv == 1) {
  138. printf ("pci initialization failed\n");
  139. return 1;
  140. }
  141. port[0].port_no = 0;
  142. port[0].ioaddr.cmd_addr = iobase1;
  143. port[0].ioaddr.altstatus_addr = port[0].ioaddr.ctl_addr =
  144. iobase2 | ATA_PCI_CTL_OFS;
  145. port[0].ioaddr.bmdma_addr = iobase5;
  146. port[1].port_no = 1;
  147. port[1].ioaddr.cmd_addr = iobase3;
  148. port[1].ioaddr.altstatus_addr = port[1].ioaddr.ctl_addr =
  149. iobase4 | ATA_PCI_CTL_OFS;
  150. port[1].ioaddr.bmdma_addr = iobase5 + 0x8;
  151. for (i = 0; i < CONFIG_SYS_SATA_MAXBUS; i++)
  152. sata_port (&port[i].ioaddr);
  153. for (i = 0; i < CONFIG_SYS_SATA_MAXBUS; i++) {
  154. if (!(sata_bus_probe (i))) {
  155. port[i].port_state = 0;
  156. printf ("SATA#%d port is not present \n", i);
  157. } else {
  158. printf ("SATA#%d port is present\n", i);
  159. if (sata_bus_softreset (i)) {
  160. port[i].port_state = 0;
  161. } else {
  162. port[i].port_state = 1;
  163. }
  164. }
  165. }
  166. for (i = 0; i < CONFIG_SYS_SATA_MAXBUS; i++) {
  167. u8 j, devno;
  168. if (port[i].port_state == 0)
  169. continue;
  170. for (j = 0; j < CONFIG_SYS_SATA_DEVS_PER_BUS; j++) {
  171. sata_identify (i, j);
  172. set_Feature_cmd (i, j);
  173. devno = i * CONFIG_SYS_SATA_DEVS_PER_BUS + j;
  174. if ((sata_dev_desc[devno].lba > 0) &&
  175. (sata_dev_desc[devno].blksz > 0)) {
  176. dev_print (&sata_dev_desc[devno]);
  177. /* initialize partition type */
  178. init_part (&sata_dev_desc[devno]);
  179. if (sata_curr_device < 0)
  180. sata_curr_device =
  181. i * CONFIG_SYS_SATA_DEVS_PER_BUS + j;
  182. }
  183. }
  184. }
  185. return 0;
  186. }
  187. static u8 __inline__
  188. sata_inb (unsigned long ioaddr)
  189. {
  190. return inb (ioaddr);
  191. }
  192. static void __inline__
  193. sata_outb (unsigned char val, unsigned long ioaddr)
  194. {
  195. outb (val, ioaddr);
  196. }
  197. static void
  198. output_data (struct sata_ioports *ioaddr, ulong * sect_buf, int words)
  199. {
  200. outsw (ioaddr->data_addr, sect_buf, words << 1);
  201. }
  202. static int
  203. input_data (struct sata_ioports *ioaddr, ulong * sect_buf, int words)
  204. {
  205. insw (ioaddr->data_addr, sect_buf, words << 1);
  206. return 0;
  207. }
  208. static void
  209. sata_cpy (unsigned char *dst, unsigned char *src, unsigned int len)
  210. {
  211. unsigned char *end, *last;
  212. last = dst;
  213. end = src + len - 1;
  214. /* reserve space for '\0' */
  215. if (len < 2)
  216. goto OUT;
  217. /* skip leading white space */
  218. while ((*src) && (src < end) && (*src == ' '))
  219. ++src;
  220. /* copy string, omitting trailing white space */
  221. while ((*src) && (src < end)) {
  222. *dst++ = *src;
  223. if (*src++ != ' ')
  224. last = dst;
  225. }
  226. OUT:
  227. *last = '\0';
  228. }
  229. int
  230. sata_bus_softreset (int num)
  231. {
  232. u8 dev = 0, status = 0, i;
  233. port[num].dev_mask = 0;
  234. for (i = 0; i < CONFIG_SYS_SATA_DEVS_PER_BUS; i++) {
  235. if (!(sata_devchk (&port[num].ioaddr, i))) {
  236. PRINTF ("dev_chk failed for dev#%d\n", i);
  237. } else {
  238. port[num].dev_mask |= (1 << i);
  239. PRINTF ("dev_chk passed for dev#%d\n", i);
  240. }
  241. }
  242. if (!(port[num].dev_mask)) {
  243. printf ("no devices on port%d\n", num);
  244. return 1;
  245. }
  246. dev_select (&port[num].ioaddr, dev);
  247. port[num].ctl_reg = 0x08; /*Default value of control reg */
  248. sata_outb (port[num].ctl_reg, port[num].ioaddr.ctl_addr);
  249. udelay (10);
  250. sata_outb (port[num].ctl_reg | ATA_SRST, port[num].ioaddr.ctl_addr);
  251. udelay (10);
  252. sata_outb (port[num].ctl_reg, port[num].ioaddr.ctl_addr);
  253. /* spec mandates ">= 2ms" before checking status.
  254. * We wait 150ms, because that was the magic delay used for
  255. * ATAPI devices in Hale Landis's ATADRVR, for the period of time
  256. * between when the ATA command register is written, and then
  257. * status is checked. Because waiting for "a while" before
  258. * checking status is fine, post SRST, we perform this magic
  259. * delay here as well.
  260. */
  261. msleep (150);
  262. status = sata_busy_wait (&port[num].ioaddr, ATA_BUSY, 300);
  263. while ((status & ATA_BUSY)) {
  264. msleep (100);
  265. status = sata_busy_wait (&port[num].ioaddr, ATA_BUSY, 3);
  266. }
  267. if (status & ATA_BUSY)
  268. printf ("ata%u is slow to respond,plz be patient\n", port);
  269. while ((status & ATA_BUSY)) {
  270. msleep (100);
  271. status = sata_chk_status (&port[num].ioaddr);
  272. }
  273. if (status & ATA_BUSY) {
  274. printf ("ata%u failed to respond : ", port);
  275. printf ("bus reset failed\n");
  276. return 1;
  277. }
  278. return 0;
  279. }
  280. void
  281. sata_identify (int num, int dev)
  282. {
  283. u8 cmd = 0, status = 0, devno = num * CONFIG_SYS_SATA_DEVS_PER_BUS + dev;
  284. u16 iobuf[ATA_SECT_SIZE];
  285. u64 n_sectors = 0;
  286. u8 mask = 0;
  287. memset (iobuf, 0, sizeof (iobuf));
  288. hd_driveid_t *iop = (hd_driveid_t *) iobuf;
  289. if (dev == 0)
  290. mask = 0x01;
  291. else
  292. mask = 0x02;
  293. if (!(port[num].dev_mask & mask)) {
  294. printf ("dev%d is not present on port#%d\n", dev, num);
  295. return;
  296. }
  297. printf ("port=%d dev=%d\n", num, dev);
  298. dev_select (&port[num].ioaddr, dev);
  299. status = 0;
  300. cmd = ATA_CMD_IDENT; /*Device Identify Command */
  301. sata_outb (cmd, port[num].ioaddr.command_addr);
  302. sata_inb (port[num].ioaddr.altstatus_addr);
  303. udelay (10);
  304. status = sata_busy_wait (&port[num].ioaddr, ATA_BUSY, 1000);
  305. if (status & ATA_ERR) {
  306. printf ("\ndevice not responding\n");
  307. port[num].dev_mask &= ~mask;
  308. return;
  309. }
  310. input_data (&port[num].ioaddr, (ulong *) iobuf, ATA_SECTORWORDS);
  311. PRINTF ("\nata%u: dev %u cfg 49:%04x 82:%04x 83:%04x 84:%04x85:%04x"
  312. "86:%04x" "87:%04x 88:%04x\n", num, dev, iobuf[49],
  313. iobuf[82], iobuf[83], iobuf[84], iobuf[85], iobuf[86],
  314. iobuf[87], iobuf[88]);
  315. /* we require LBA and DMA support (bits 8 & 9 of word 49) */
  316. if (!ata_id_has_dma (iobuf) || !ata_id_has_lba (iobuf)) {
  317. PRINTF ("ata%u: no dma/lba\n", num);
  318. }
  319. ata_dump_id (iobuf);
  320. if (ata_id_has_lba48 (iobuf)) {
  321. n_sectors = ata_id_u64 (iobuf, 100);
  322. } else {
  323. n_sectors = ata_id_u32 (iobuf, 60);
  324. }
  325. PRINTF ("no. of sectors %u\n", ata_id_u64 (iobuf, 100));
  326. PRINTF ("no. of sectors %u\n", ata_id_u32 (iobuf, 60));
  327. if (n_sectors == 0) {
  328. port[num].dev_mask &= ~mask;
  329. return;
  330. }
  331. sata_cpy (sata_dev_desc[devno].revision, iop->fw_rev,
  332. sizeof (sata_dev_desc[devno].revision));
  333. sata_cpy (sata_dev_desc[devno].vendor, iop->model,
  334. sizeof (sata_dev_desc[devno].vendor));
  335. sata_cpy (sata_dev_desc[devno].product, iop->serial_no,
  336. sizeof (sata_dev_desc[devno].product));
  337. strswab (sata_dev_desc[devno].revision);
  338. strswab (sata_dev_desc[devno].vendor);
  339. if ((iop->config & 0x0080) == 0x0080) {
  340. sata_dev_desc[devno].removable = 1;
  341. } else {
  342. sata_dev_desc[devno].removable = 0;
  343. }
  344. sata_dev_desc[devno].lba = iop->lba_capacity;
  345. PRINTF ("lba=0x%x", sata_dev_desc[devno].lba);
  346. #ifdef CONFIG_LBA48
  347. if (iop->command_set_2 & 0x0400) {
  348. sata_dev_desc[devno].lba48 = 1;
  349. lba = (unsigned long long) iop->lba48_capacity[0] |
  350. ((unsigned long long) iop->lba48_capacity[1] << 16) |
  351. ((unsigned long long) iop->lba48_capacity[2] << 32) |
  352. ((unsigned long long) iop->lba48_capacity[3] << 48);
  353. } else {
  354. sata_dev_desc[devno].lba48 = 0;
  355. }
  356. #endif
  357. /* assuming HD */
  358. sata_dev_desc[devno].type = DEV_TYPE_HARDDISK;
  359. sata_dev_desc[devno].blksz = ATA_BLOCKSIZE;
  360. sata_dev_desc[devno].lun = 0; /* just to fill something in... */
  361. }
  362. void
  363. set_Feature_cmd (int num, int dev)
  364. {
  365. u8 mask = 0x00, status = 0;
  366. if (dev == 0)
  367. mask = 0x01;
  368. else
  369. mask = 0x02;
  370. if (!(port[num].dev_mask & mask)) {
  371. PRINTF ("dev%d is not present on port#%d\n", dev, num);
  372. return;
  373. }
  374. dev_select (&port[num].ioaddr, dev);
  375. sata_outb (SETFEATURES_XFER, port[num].ioaddr.feature_addr);
  376. sata_outb (XFER_PIO_4, port[num].ioaddr.nsect_addr);
  377. sata_outb (0, port[num].ioaddr.lbal_addr);
  378. sata_outb (0, port[num].ioaddr.lbam_addr);
  379. sata_outb (0, port[num].ioaddr.lbah_addr);
  380. sata_outb (ATA_DEVICE_OBS, port[num].ioaddr.device_addr);
  381. sata_outb (ATA_CMD_SETF, port[num].ioaddr.command_addr);
  382. udelay (50);
  383. msleep (150);
  384. status = sata_busy_wait (&port[num].ioaddr, ATA_BUSY, 5000);
  385. if ((status & (ATA_STAT_BUSY | ATA_STAT_ERR))) {
  386. printf ("Error : status 0x%02x\n", status);
  387. port[num].dev_mask &= ~mask;
  388. }
  389. }
  390. void
  391. sata_port (struct sata_ioports *ioport)
  392. {
  393. ioport->data_addr = ioport->cmd_addr + ATA_REG_DATA;
  394. ioport->error_addr = ioport->cmd_addr + ATA_REG_ERR;
  395. ioport->feature_addr = ioport->cmd_addr + ATA_REG_FEATURE;
  396. ioport->nsect_addr = ioport->cmd_addr + ATA_REG_NSECT;
  397. ioport->lbal_addr = ioport->cmd_addr + ATA_REG_LBAL;
  398. ioport->lbam_addr = ioport->cmd_addr + ATA_REG_LBAM;
  399. ioport->lbah_addr = ioport->cmd_addr + ATA_REG_LBAH;
  400. ioport->device_addr = ioport->cmd_addr + ATA_REG_DEVICE;
  401. ioport->status_addr = ioport->cmd_addr + ATA_REG_STATUS;
  402. ioport->command_addr = ioport->cmd_addr + ATA_REG_CMD;
  403. }
  404. int
  405. sata_devchk (struct sata_ioports *ioaddr, int dev)
  406. {
  407. u8 nsect, lbal;
  408. dev_select (ioaddr, dev);
  409. sata_outb (0x55, ioaddr->nsect_addr);
  410. sata_outb (0xaa, ioaddr->lbal_addr);
  411. sata_outb (0xaa, ioaddr->nsect_addr);
  412. sata_outb (0x55, ioaddr->lbal_addr);
  413. sata_outb (0x55, ioaddr->nsect_addr);
  414. sata_outb (0xaa, ioaddr->lbal_addr);
  415. nsect = sata_inb (ioaddr->nsect_addr);
  416. lbal = sata_inb (ioaddr->lbal_addr);
  417. if ((nsect == 0x55) && (lbal == 0xaa))
  418. return 1; /* we found a device */
  419. else
  420. return 0; /* nothing found */
  421. }
  422. void
  423. dev_select (struct sata_ioports *ioaddr, int dev)
  424. {
  425. u8 tmp = 0;
  426. if (dev == 0)
  427. tmp = ATA_DEVICE_OBS;
  428. else
  429. tmp = ATA_DEVICE_OBS | ATA_DEV1;
  430. sata_outb (tmp, ioaddr->device_addr);
  431. sata_inb (ioaddr->altstatus_addr);
  432. udelay (5);
  433. }
  434. u8
  435. sata_busy_wait (struct sata_ioports *ioaddr, int bits, unsigned int max)
  436. {
  437. u8 status;
  438. do {
  439. udelay (1000);
  440. status = sata_chk_status (ioaddr);
  441. max--;
  442. } while ((status & bits) && (max > 0));
  443. return status;
  444. }
  445. u8
  446. sata_chk_status (struct sata_ioports * ioaddr)
  447. {
  448. return sata_inb (ioaddr->status_addr);
  449. }
  450. void
  451. msleep (int count)
  452. {
  453. int i;
  454. for (i = 0; i < count; i++)
  455. udelay (1000);
  456. }
  457. ulong
  458. sata_read (int device, ulong blknr,lbaint_t blkcnt, void * buff)
  459. {
  460. ulong n = 0, *buffer = (ulong *)buff;
  461. u8 dev = 0, num = 0, mask = 0, status = 0;
  462. #ifdef CONFIG_LBA48
  463. unsigned char lba48 = 0;
  464. if (blknr & 0x0000fffff0000000) {
  465. if (!sata_dev_desc[devno].lba48) {
  466. printf ("Drive doesn't support 48-bit addressing\n");
  467. return 0;
  468. }
  469. /* more than 28 bits used, use 48bit mode */
  470. lba48 = 1;
  471. }
  472. #endif
  473. /*Port Number */
  474. num = device / CONFIG_SYS_SATA_DEVS_PER_BUS;
  475. /*dev on the port */
  476. if (device >= CONFIG_SYS_SATA_DEVS_PER_BUS)
  477. dev = device - CONFIG_SYS_SATA_DEVS_PER_BUS;
  478. else
  479. dev = device;
  480. if (dev == 0)
  481. mask = 0x01;
  482. else
  483. mask = 0x02;
  484. if (!(port[num].dev_mask & mask)) {
  485. printf ("dev%d is not present on port#%d\n", dev, num);
  486. return 0;
  487. }
  488. /* Select device */
  489. dev_select (&port[num].ioaddr, dev);
  490. status = sata_busy_wait (&port[num].ioaddr, ATA_BUSY, 500);
  491. if (status & ATA_BUSY) {
  492. printf ("ata%u failed to respond\n", port[num].port_no);
  493. return n;
  494. }
  495. while (blkcnt-- > 0) {
  496. status = sata_busy_wait (&port[num].ioaddr, ATA_BUSY, 500);
  497. if (status & ATA_BUSY) {
  498. printf ("ata%u failed to respond\n", 0);
  499. return n;
  500. }
  501. #ifdef CONFIG_LBA48
  502. if (lba48) {
  503. /* write high bits */
  504. sata_outb (0, port[num].ioaddr.nsect_addr);
  505. sata_outb ((blknr >> 24) & 0xFF,
  506. port[num].ioaddr.lbal_addr);
  507. sata_outb ((blknr >> 32) & 0xFF,
  508. port[num].ioaddr.lbam_addr);
  509. sata_outb ((blknr >> 40) & 0xFF,
  510. port[num].ioaddr.lbah_addr);
  511. }
  512. #endif
  513. sata_outb (1, port[num].ioaddr.nsect_addr);
  514. sata_outb (((blknr) >> 0) & 0xFF,
  515. port[num].ioaddr.lbal_addr);
  516. sata_outb ((blknr >> 8) & 0xFF, port[num].ioaddr.lbam_addr);
  517. sata_outb ((blknr >> 16) & 0xFF, port[num].ioaddr.lbah_addr);
  518. #ifdef CONFIG_LBA48
  519. if (lba48) {
  520. sata_outb (ATA_LBA, port[num].ioaddr.device_addr);
  521. sata_outb (ATA_CMD_READ_EXT,
  522. port[num].ioaddr.command_addr);
  523. } else
  524. #endif
  525. {
  526. sata_outb (ATA_LBA | ((blknr >> 24) & 0xF),
  527. port[num].ioaddr.device_addr);
  528. sata_outb (ATA_CMD_READ,
  529. port[num].ioaddr.command_addr);
  530. }
  531. msleep (50);
  532. /*may take up to 4 sec */
  533. status = sata_busy_wait (&port[num].ioaddr, ATA_BUSY, 4000);
  534. if ((status & (ATA_STAT_DRQ | ATA_STAT_BUSY | ATA_STAT_ERR))
  535. != ATA_STAT_DRQ) {
  536. u8 err = 0;
  537. printf ("Error no DRQ dev %d blk %ld: sts 0x%02x\n",
  538. device, (ulong) blknr, status);
  539. err = sata_inb (port[num].ioaddr.error_addr);
  540. printf ("Error reg = 0x%x\n", err);
  541. return (n);
  542. }
  543. input_data (&port[num].ioaddr, buffer, ATA_SECTORWORDS);
  544. sata_inb (port[num].ioaddr.altstatus_addr);
  545. udelay (50);
  546. ++n;
  547. ++blknr;
  548. buffer += ATA_SECTORWORDS;
  549. }
  550. return n;
  551. }
  552. ulong
  553. sata_write (int device, ulong blknr,lbaint_t blkcnt, void * buff)
  554. {
  555. ulong n = 0, *buffer = (ulong *)buff;
  556. unsigned char status = 0, num = 0, dev = 0, mask = 0;
  557. #ifdef CONFIG_LBA48
  558. unsigned char lba48 = 0;
  559. if (blknr & 0x0000fffff0000000) {
  560. if (!sata_dev_desc[devno].lba48) {
  561. printf ("Drive doesn't support 48-bit addressing\n");
  562. return 0;
  563. }
  564. /* more than 28 bits used, use 48bit mode */
  565. lba48 = 1;
  566. }
  567. #endif
  568. /*Port Number */
  569. num = device / CONFIG_SYS_SATA_DEVS_PER_BUS;
  570. /*dev on the Port */
  571. if (device >= CONFIG_SYS_SATA_DEVS_PER_BUS)
  572. dev = device - CONFIG_SYS_SATA_DEVS_PER_BUS;
  573. else
  574. dev = device;
  575. if (dev == 0)
  576. mask = 0x01;
  577. else
  578. mask = 0x02;
  579. /* Select device */
  580. dev_select (&port[num].ioaddr, dev);
  581. status = sata_busy_wait (&port[num].ioaddr, ATA_BUSY, 500);
  582. if (status & ATA_BUSY) {
  583. printf ("ata%u failed to respond\n", port[num].port_no);
  584. return n;
  585. }
  586. while (blkcnt-- > 0) {
  587. status = sata_busy_wait (&port[num].ioaddr, ATA_BUSY, 500);
  588. if (status & ATA_BUSY) {
  589. printf ("ata%u failed to respond\n",
  590. port[num].port_no);
  591. return n;
  592. }
  593. #ifdef CONFIG_LBA48
  594. if (lba48) {
  595. /* write high bits */
  596. sata_outb (0, port[num].ioaddr.nsect_addr);
  597. sata_outb ((blknr >> 24) & 0xFF,
  598. port[num].ioaddr.lbal_addr);
  599. sata_outb ((blknr >> 32) & 0xFF,
  600. port[num].ioaddr.lbam_addr);
  601. sata_outb ((blknr >> 40) & 0xFF,
  602. port[num].ioaddr.lbah_addr);
  603. }
  604. #endif
  605. sata_outb (1, port[num].ioaddr.nsect_addr);
  606. sata_outb ((blknr >> 0) & 0xFF, port[num].ioaddr.lbal_addr);
  607. sata_outb ((blknr >> 8) & 0xFF, port[num].ioaddr.lbam_addr);
  608. sata_outb ((blknr >> 16) & 0xFF, port[num].ioaddr.lbah_addr);
  609. #ifdef CONFIG_LBA48
  610. if (lba48) {
  611. sata_outb (ATA_LBA, port[num].ioaddr.device_addr);
  612. sata_outb (ATA_CMD_WRITE_EXT,
  613. port[num].ioaddr.command_addr);
  614. } else
  615. #endif
  616. {
  617. sata_outb (ATA_LBA | ((blknr >> 24) & 0xF),
  618. port[num].ioaddr.device_addr);
  619. sata_outb (ATA_CMD_WRITE,
  620. port[num].ioaddr.command_addr);
  621. }
  622. msleep (50);
  623. /*may take up to 4 sec */
  624. status = sata_busy_wait (&port[num].ioaddr, ATA_BUSY, 4000);
  625. if ((status & (ATA_STAT_DRQ | ATA_STAT_BUSY | ATA_STAT_ERR))
  626. != ATA_STAT_DRQ) {
  627. printf ("Error no DRQ dev %d blk %ld: sts 0x%02x\n",
  628. device, (ulong) blknr, status);
  629. return (n);
  630. }
  631. output_data (&port[num].ioaddr, buffer, ATA_SECTORWORDS);
  632. sata_inb (port[num].ioaddr.altstatus_addr);
  633. udelay (50);
  634. ++n;
  635. ++blknr;
  636. buffer += ATA_SECTORWORDS;
  637. }
  638. return n;
  639. }
  640. int scan_sata(int dev)
  641. {
  642. return 0;
  643. }