ata_piix.c 18 KB

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