ata_piix.c 22 KB

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