ata_piix.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724
  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].lun = 0; /* just to fill something in... */
  345. }
  346. void set_Feature_cmd(int num, int dev)
  347. {
  348. u8 mask = 0x00, status = 0;
  349. if (dev == 0)
  350. mask = 0x01;
  351. else
  352. mask = 0x02;
  353. if (!(port[num].dev_mask & mask)) {
  354. debug("dev%d is not present on port#%d\n", dev, num);
  355. return;
  356. }
  357. dev_select(&port[num].ioaddr, dev);
  358. sata_outb(SETFEATURES_XFER, port[num].ioaddr.feature_addr);
  359. sata_outb(XFER_PIO_4, port[num].ioaddr.nsect_addr);
  360. sata_outb(0, port[num].ioaddr.lbal_addr);
  361. sata_outb(0, port[num].ioaddr.lbam_addr);
  362. sata_outb(0, port[num].ioaddr.lbah_addr);
  363. sata_outb(ATA_DEVICE_OBS, port[num].ioaddr.device_addr);
  364. sata_outb(ATA_CMD_SETF, port[num].ioaddr.command_addr);
  365. udelay(50);
  366. mdelay(150);
  367. status = sata_busy_wait(&port[num].ioaddr, ATA_BUSY, 5000);
  368. if ((status & (ATA_STAT_BUSY | ATA_STAT_ERR))) {
  369. printf("Error : status 0x%02x\n", status);
  370. port[num].dev_mask &= ~mask;
  371. }
  372. }
  373. void sata_port(struct sata_ioports *ioport)
  374. {
  375. ioport->data_addr = ioport->cmd_addr + ATA_REG_DATA;
  376. ioport->error_addr = ioport->cmd_addr + ATA_REG_ERR;
  377. ioport->feature_addr = ioport->cmd_addr + ATA_REG_FEATURE;
  378. ioport->nsect_addr = ioport->cmd_addr + ATA_REG_NSECT;
  379. ioport->lbal_addr = ioport->cmd_addr + ATA_REG_LBAL;
  380. ioport->lbam_addr = ioport->cmd_addr + ATA_REG_LBAM;
  381. ioport->lbah_addr = ioport->cmd_addr + ATA_REG_LBAH;
  382. ioport->device_addr = ioport->cmd_addr + ATA_REG_DEVICE;
  383. ioport->status_addr = ioport->cmd_addr + ATA_REG_STATUS;
  384. ioport->command_addr = ioport->cmd_addr + ATA_REG_CMD;
  385. }
  386. int sata_devchk(struct sata_ioports *ioaddr, int dev)
  387. {
  388. u8 nsect, lbal;
  389. dev_select(ioaddr, dev);
  390. sata_outb(0x55, ioaddr->nsect_addr);
  391. sata_outb(0xaa, ioaddr->lbal_addr);
  392. sata_outb(0xaa, ioaddr->nsect_addr);
  393. sata_outb(0x55, ioaddr->lbal_addr);
  394. sata_outb(0x55, ioaddr->nsect_addr);
  395. sata_outb(0xaa, ioaddr->lbal_addr);
  396. nsect = sata_inb(ioaddr->nsect_addr);
  397. lbal = sata_inb(ioaddr->lbal_addr);
  398. if ((nsect == 0x55) && (lbal == 0xaa))
  399. return 1; /* we found a device */
  400. else
  401. return 0; /* nothing found */
  402. }
  403. void dev_select(struct sata_ioports *ioaddr, int dev)
  404. {
  405. u8 tmp = 0;
  406. if (dev == 0)
  407. tmp = ATA_DEVICE_OBS;
  408. else
  409. tmp = ATA_DEVICE_OBS | ATA_DEV1;
  410. sata_outb(tmp, ioaddr->device_addr);
  411. sata_inb(ioaddr->altstatus_addr);
  412. udelay(5);
  413. }
  414. u8 sata_busy_wait(struct sata_ioports *ioaddr, int bits, unsigned int max)
  415. {
  416. u8 status;
  417. do {
  418. udelay(1000);
  419. status = sata_chk_status(ioaddr);
  420. max--;
  421. } while ((status & bits) && (max > 0));
  422. return status;
  423. }
  424. u8 sata_chk_status(struct sata_ioports *ioaddr)
  425. {
  426. return sata_inb(ioaddr->status_addr);
  427. }
  428. ulong sata_read(int device, ulong blknr, lbaint_t blkcnt, void *buff)
  429. {
  430. ulong n = 0, *buffer = (ulong *)buff;
  431. u8 dev = 0, num = 0, mask = 0, status = 0;
  432. #ifdef CONFIG_LBA48
  433. unsigned char lba48 = 0;
  434. if (blknr & 0x0000fffff0000000) {
  435. if (!sata_dev_desc[devno].lba48) {
  436. printf("Drive doesn't support 48-bit addressing\n");
  437. return 0;
  438. }
  439. /* more than 28 bits used, use 48bit mode */
  440. lba48 = 1;
  441. }
  442. #endif
  443. /* Port Number */
  444. num = device / CONFIG_SYS_SATA_DEVS_PER_BUS;
  445. /* dev on the port */
  446. if (device >= CONFIG_SYS_SATA_DEVS_PER_BUS)
  447. dev = device - CONFIG_SYS_SATA_DEVS_PER_BUS;
  448. else
  449. dev = device;
  450. if (dev == 0)
  451. mask = 0x01;
  452. else
  453. mask = 0x02;
  454. if (!(port[num].dev_mask & mask)) {
  455. printf("dev%d is not present on port#%d\n", dev, num);
  456. return 0;
  457. }
  458. /* Select device */
  459. dev_select(&port[num].ioaddr, dev);
  460. status = sata_busy_wait(&port[num].ioaddr, ATA_BUSY, 500);
  461. if (status & ATA_BUSY) {
  462. printf("ata%u failed to respond\n", port[num].port_no);
  463. return n;
  464. }
  465. while (blkcnt-- > 0) {
  466. status = sata_busy_wait(&port[num].ioaddr, ATA_BUSY, 500);
  467. if (status & ATA_BUSY) {
  468. printf("ata%u failed to respond\n", 0);
  469. return n;
  470. }
  471. #ifdef CONFIG_LBA48
  472. if (lba48) {
  473. /* write high bits */
  474. sata_outb(0, port[num].ioaddr.nsect_addr);
  475. sata_outb((blknr >> 24) & 0xFF,
  476. port[num].ioaddr.lbal_addr);
  477. sata_outb((blknr >> 32) & 0xFF,
  478. port[num].ioaddr.lbam_addr);
  479. sata_outb((blknr >> 40) & 0xFF,
  480. port[num].ioaddr.lbah_addr);
  481. }
  482. #endif
  483. sata_outb(1, port[num].ioaddr.nsect_addr);
  484. sata_outb(((blknr) >> 0) & 0xFF,
  485. port[num].ioaddr.lbal_addr);
  486. sata_outb((blknr >> 8) & 0xFF, port[num].ioaddr.lbam_addr);
  487. sata_outb((blknr >> 16) & 0xFF, port[num].ioaddr.lbah_addr);
  488. #ifdef CONFIG_LBA48
  489. if (lba48) {
  490. sata_outb(ATA_LBA, port[num].ioaddr.device_addr);
  491. sata_outb(ATA_CMD_READ_EXT,
  492. port[num].ioaddr.command_addr);
  493. } else
  494. #endif
  495. {
  496. sata_outb(ATA_LBA | ((blknr >> 24) & 0xF),
  497. port[num].ioaddr.device_addr);
  498. sata_outb(ATA_CMD_READ,
  499. port[num].ioaddr.command_addr);
  500. }
  501. mdelay(50);
  502. /* may take up to 4 sec */
  503. status = sata_busy_wait(&port[num].ioaddr, ATA_BUSY, 4000);
  504. if ((status & (ATA_STAT_DRQ | ATA_STAT_BUSY | ATA_STAT_ERR))
  505. != ATA_STAT_DRQ) {
  506. u8 err = 0;
  507. printf("Error no DRQ dev %d blk %ld: sts 0x%02x\n",
  508. device, (ulong) blknr, status);
  509. err = sata_inb(port[num].ioaddr.error_addr);
  510. printf("Error reg = 0x%x\n", err);
  511. return n;
  512. }
  513. input_data(&port[num].ioaddr, buffer, ATA_SECTORWORDS);
  514. sata_inb(port[num].ioaddr.altstatus_addr);
  515. udelay(50);
  516. ++n;
  517. ++blknr;
  518. buffer += ATA_SECTORWORDS;
  519. }
  520. return n;
  521. }
  522. ulong sata_write(int device, ulong blknr, lbaint_t blkcnt, const void *buff)
  523. {
  524. ulong n = 0, *buffer = (ulong *)buff;
  525. unsigned char status = 0, num = 0, dev = 0, mask = 0;
  526. #ifdef CONFIG_LBA48
  527. unsigned char lba48 = 0;
  528. if (blknr & 0x0000fffff0000000) {
  529. if (!sata_dev_desc[devno].lba48) {
  530. printf("Drive doesn't support 48-bit addressing\n");
  531. return 0;
  532. }
  533. /* more than 28 bits used, use 48bit mode */
  534. lba48 = 1;
  535. }
  536. #endif
  537. /* Port Number */
  538. num = device / CONFIG_SYS_SATA_DEVS_PER_BUS;
  539. /* dev on the Port */
  540. if (device >= CONFIG_SYS_SATA_DEVS_PER_BUS)
  541. dev = device - CONFIG_SYS_SATA_DEVS_PER_BUS;
  542. else
  543. dev = device;
  544. if (dev == 0)
  545. mask = 0x01;
  546. else
  547. mask = 0x02;
  548. /* Select device */
  549. dev_select(&port[num].ioaddr, dev);
  550. status = sata_busy_wait(&port[num].ioaddr, ATA_BUSY, 500);
  551. if (status & ATA_BUSY) {
  552. printf("ata%u failed to respond\n", port[num].port_no);
  553. return n;
  554. }
  555. while (blkcnt-- > 0) {
  556. status = sata_busy_wait(&port[num].ioaddr, ATA_BUSY, 500);
  557. if (status & ATA_BUSY) {
  558. printf("ata%u failed to respond\n",
  559. port[num].port_no);
  560. return n;
  561. }
  562. #ifdef CONFIG_LBA48
  563. if (lba48) {
  564. /* write high bits */
  565. sata_outb(0, port[num].ioaddr.nsect_addr);
  566. sata_outb((blknr >> 24) & 0xFF,
  567. port[num].ioaddr.lbal_addr);
  568. sata_outb((blknr >> 32) & 0xFF,
  569. port[num].ioaddr.lbam_addr);
  570. sata_outb((blknr >> 40) & 0xFF,
  571. port[num].ioaddr.lbah_addr);
  572. }
  573. #endif
  574. sata_outb(1, port[num].ioaddr.nsect_addr);
  575. sata_outb((blknr >> 0) & 0xFF, port[num].ioaddr.lbal_addr);
  576. sata_outb((blknr >> 8) & 0xFF, port[num].ioaddr.lbam_addr);
  577. sata_outb((blknr >> 16) & 0xFF, port[num].ioaddr.lbah_addr);
  578. #ifdef CONFIG_LBA48
  579. if (lba48) {
  580. sata_outb(ATA_LBA, port[num].ioaddr.device_addr);
  581. sata_outb(ATA_CMD_WRITE_EXT,
  582. port[num].ioaddr.command_addr);
  583. } else
  584. #endif
  585. {
  586. sata_outb(ATA_LBA | ((blknr >> 24) & 0xF),
  587. port[num].ioaddr.device_addr);
  588. sata_outb(ATA_CMD_WRITE,
  589. port[num].ioaddr.command_addr);
  590. }
  591. mdelay(50);
  592. /* may take up to 4 sec */
  593. status = sata_busy_wait(&port[num].ioaddr, ATA_BUSY, 4000);
  594. if ((status & (ATA_STAT_DRQ | ATA_STAT_BUSY | ATA_STAT_ERR))
  595. != ATA_STAT_DRQ) {
  596. printf("Error no DRQ dev %d blk %ld: sts 0x%02x\n",
  597. device, (ulong) blknr, status);
  598. return n;
  599. }
  600. output_data(&port[num].ioaddr, buffer, ATA_SECTORWORDS);
  601. sata_inb(port[num].ioaddr.altstatus_addr);
  602. udelay(50);
  603. ++n;
  604. ++blknr;
  605. buffer += ATA_SECTORWORDS;
  606. }
  607. return n;
  608. }
  609. int scan_sata(int dev)
  610. {
  611. return 0;
  612. }