ata_piix.c 18 KB

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