sata_sil3114.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839
  1. /*
  2. * Copyright (C) Excito Elektronik i Skåne AB, All rights reserved.
  3. * Author: Tor Krill <tor@excito.com>
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License as
  7. * published by the Free Software Foundation; either version 2 of
  8. * the License, or (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  18. * MA 02111-1307 USA
  19. *
  20. * This is a driver for Silicon Image sil3114 sata chip modelled on
  21. * the ata_piix driver
  22. */
  23. #include <common.h>
  24. #include <pci.h>
  25. #include <command.h>
  26. #include <config.h>
  27. #include <asm/byteorder.h>
  28. #include <asm/io.h>
  29. #include <ide.h>
  30. #include <libata.h>
  31. #include "sata_sil3114.h"
  32. /* Convert sectorsize to wordsize */
  33. #define ATA_SECTOR_WORDS (ATA_SECT_SIZE/2)
  34. /* Forwards */
  35. u8 sil3114_spin_up (int num);
  36. u8 sil3114_spin_down (int num);
  37. static int sata_bus_softreset (int num);
  38. static void sata_identify (int num, int dev);
  39. static u8 check_power_mode (int num);
  40. static void sata_port (struct sata_ioports *ioport);
  41. static void set_Feature_cmd (int num, int dev);
  42. static u8 sata_busy_wait (struct sata_ioports *ioaddr, int bits,
  43. unsigned int max, u8 usealtstatus);
  44. static u8 sata_chk_status (struct sata_ioports *ioaddr, u8 usealtstatus);
  45. static void msleep (int count);
  46. static u32 iobase[6] = { 0, 0, 0, 0, 0, 0}; /* PCI BAR registers for device */
  47. extern block_dev_desc_t sata_dev_desc[CONFIG_SYS_SATA_MAX_DEVICE];
  48. static struct sata_port port[CONFIG_SYS_SATA_MAX_DEVICE];
  49. static void output_data (struct sata_ioports *ioaddr, u16 * sect_buf, int words)
  50. {
  51. while (words--) {
  52. __raw_writew (*sect_buf++, (void *)ioaddr->data_addr);
  53. }
  54. }
  55. static int input_data (struct sata_ioports *ioaddr, u16 * sect_buf, int words)
  56. {
  57. while (words--) {
  58. *sect_buf++ = __raw_readw ((void *)ioaddr->data_addr);
  59. }
  60. return 0;
  61. }
  62. static int sata_bus_softreset (int num)
  63. {
  64. u8 status = 0;
  65. port[num].dev_mask = 1;
  66. port[num].ctl_reg = 0x08; /*Default value of control reg */
  67. writeb (port[num].ctl_reg, port[num].ioaddr.ctl_addr);
  68. udelay (10);
  69. writeb (port[num].ctl_reg | ATA_SRST, port[num].ioaddr.ctl_addr);
  70. udelay (10);
  71. writeb (port[num].ctl_reg, port[num].ioaddr.ctl_addr);
  72. /* spec mandates ">= 2ms" before checking status.
  73. * We wait 150ms, because that was the magic delay used for
  74. * ATAPI devices in Hale Landis's ATADRVR, for the period of time
  75. * between when the ATA command register is written, and then
  76. * status is checked. Because waiting for "a while" before
  77. * checking status is fine, post SRST, we perform this magic
  78. * delay here as well.
  79. */
  80. msleep (150);
  81. status = sata_busy_wait (&port[num].ioaddr, ATA_BUSY, 300, 0);
  82. while ((status & ATA_BUSY)) {
  83. msleep (100);
  84. status = sata_busy_wait (&port[num].ioaddr, ATA_BUSY, 3, 0);
  85. }
  86. if (status & ATA_BUSY) {
  87. printf ("ata%u is slow to respond,plz be patient\n", num);
  88. }
  89. while ((status & ATA_BUSY)) {
  90. msleep (100);
  91. status = sata_chk_status (&port[num].ioaddr, 0);
  92. }
  93. if (status & ATA_BUSY) {
  94. printf ("ata%u failed to respond : ", num);
  95. printf ("bus reset failed\n");
  96. port[num].dev_mask = 0;
  97. return 1;
  98. }
  99. return 0;
  100. }
  101. static void sata_identify (int num, int dev)
  102. {
  103. u8 cmd = 0, status = 0, devno = num;
  104. u16 iobuf[ATA_SECTOR_WORDS];
  105. u64 n_sectors = 0;
  106. memset (iobuf, 0, sizeof (iobuf));
  107. if (!(port[num].dev_mask & 0x01)) {
  108. printf ("dev%d is not present on port#%d\n", dev, num);
  109. return;
  110. }
  111. debug ("port=%d dev=%d\n", num, dev);
  112. status = 0;
  113. cmd = ATA_CMD_ID_ATA; /*Device Identify Command */
  114. writeb (cmd, port[num].ioaddr.command_addr);
  115. readb (port[num].ioaddr.altstatus_addr);
  116. udelay (10);
  117. status = sata_busy_wait (&port[num].ioaddr, ATA_BUSY, 1000, 0);
  118. if (status & ATA_ERR) {
  119. printf ("\ndevice not responding\n");
  120. port[num].dev_mask &= ~0x01;
  121. return;
  122. }
  123. input_data (&port[num].ioaddr, iobuf, ATA_SECTOR_WORDS);
  124. ata_swap_buf_le16 (iobuf, ATA_SECTOR_WORDS);
  125. debug ("Specific config: %x\n", iobuf[2]);
  126. /* we require LBA and DMA support (bits 8 & 9 of word 49) */
  127. if (!ata_id_has_dma (iobuf) || !ata_id_has_lba (iobuf)) {
  128. debug ("ata%u: no dma/lba\n", num);
  129. }
  130. #ifdef DEBUG
  131. ata_dump_id (iobuf);
  132. #endif
  133. n_sectors = ata_id_n_sectors (iobuf);
  134. if (n_sectors == 0) {
  135. port[num].dev_mask &= ~0x01;
  136. return;
  137. }
  138. ata_id_c_string (iobuf, (unsigned char *)sata_dev_desc[devno].revision,
  139. ATA_ID_FW_REV, sizeof (sata_dev_desc[devno].revision));
  140. ata_id_c_string (iobuf, (unsigned char *)sata_dev_desc[devno].vendor,
  141. ATA_ID_PROD, sizeof (sata_dev_desc[devno].vendor));
  142. ata_id_c_string (iobuf, (unsigned char *)sata_dev_desc[devno].product,
  143. ATA_ID_SERNO, sizeof (sata_dev_desc[devno].product));
  144. /* TODO - atm we asume harddisk ie not removable */
  145. sata_dev_desc[devno].removable = 0;
  146. sata_dev_desc[devno].lba = (u32) n_sectors;
  147. debug ("lba=0x%x\n", sata_dev_desc[devno].lba);
  148. #ifdef CONFIG_LBA48
  149. if (iobuf[83] & (1 << 10)) {
  150. sata_dev_desc[devno].lba48 = 1;
  151. } else {
  152. sata_dev_desc[devno].lba48 = 0;
  153. }
  154. #endif
  155. /* assuming HD */
  156. sata_dev_desc[devno].type = DEV_TYPE_HARDDISK;
  157. sata_dev_desc[devno].blksz = ATA_SECT_SIZE;
  158. sata_dev_desc[devno].lun = 0; /* just to fill something in... */
  159. }
  160. static void set_Feature_cmd (int num, int dev)
  161. {
  162. u8 status = 0;
  163. if (!(port[num].dev_mask & 0x01)) {
  164. debug ("dev%d is not present on port#%d\n", dev, num);
  165. return;
  166. }
  167. writeb (SETFEATURES_XFER, port[num].ioaddr.feature_addr);
  168. writeb (XFER_PIO_4, port[num].ioaddr.nsect_addr);
  169. writeb (0, port[num].ioaddr.lbal_addr);
  170. writeb (0, port[num].ioaddr.lbam_addr);
  171. writeb (0, port[num].ioaddr.lbah_addr);
  172. writeb (ATA_DEVICE_OBS, port[num].ioaddr.device_addr);
  173. writeb (ATA_CMD_SET_FEATURES, port[num].ioaddr.command_addr);
  174. udelay (50);
  175. msleep (150);
  176. status = sata_busy_wait (&port[num].ioaddr, ATA_BUSY, 5000, 0);
  177. if ((status & (ATA_BUSY | ATA_ERR))) {
  178. printf ("Error : status 0x%02x\n", status);
  179. port[num].dev_mask &= ~0x01;
  180. }
  181. }
  182. u8 sil3114_spin_down (int num)
  183. {
  184. u8 status = 0;
  185. debug ("Spin down disk\n");
  186. if (!(port[num].dev_mask & 0x01)) {
  187. debug ("Device ata%d is not present\n", num);
  188. return 1;
  189. }
  190. if ((status = check_power_mode (num)) == 0x00) {
  191. debug ("Already in standby\n");
  192. return 0;
  193. }
  194. if (status == 0x01) {
  195. printf ("Failed to check power mode on ata%d\n", num);
  196. return 1;
  197. }
  198. if (!((status = sata_chk_status (&port[num].ioaddr, 0)) & ATA_DRDY)) {
  199. printf ("Device ata%d not ready\n", num);
  200. return 1;
  201. }
  202. writeb (0x00, port[num].ioaddr.feature_addr);
  203. writeb (0x00, port[num].ioaddr.nsect_addr);
  204. writeb (0x00, port[num].ioaddr.lbal_addr);
  205. writeb (0x00, port[num].ioaddr.lbam_addr);
  206. writeb (0x00, port[num].ioaddr.lbah_addr);
  207. writeb (ATA_DEVICE_OBS, port[num].ioaddr.device_addr);
  208. writeb (ATA_CMD_STANDBY, port[num].ioaddr.command_addr);
  209. status = sata_busy_wait (&port[num].ioaddr, ATA_BUSY, 30000, 0);
  210. if ((status & (ATA_BUSY | ATA_ERR))) {
  211. printf ("Error waiting for disk spin down: status 0x%02x\n",
  212. status);
  213. port[num].dev_mask &= ~0x01;
  214. return 1;
  215. }
  216. return 0;
  217. }
  218. u8 sil3114_spin_up (int num)
  219. {
  220. u8 status = 0;
  221. debug ("Spin up disk\n");
  222. if (!(port[num].dev_mask & 0x01)) {
  223. debug ("Device ata%d is not present\n", num);
  224. return 1;
  225. }
  226. if ((status = check_power_mode (num)) != 0x00) {
  227. if (status == 0x01) {
  228. printf ("Failed to check power mode on ata%d\n", num);
  229. return 1;
  230. } else {
  231. /* should be up and running already */
  232. return 0;
  233. }
  234. }
  235. if (!((status = sata_chk_status (&port[num].ioaddr, 0)) & ATA_DRDY)) {
  236. printf ("Device ata%d not ready\n", num);
  237. return 1;
  238. }
  239. debug ("Stautus of device check: %d\n", status);
  240. writeb (0x00, port[num].ioaddr.feature_addr);
  241. writeb (0x00, port[num].ioaddr.nsect_addr);
  242. writeb (0x00, port[num].ioaddr.lbal_addr);
  243. writeb (0x00, port[num].ioaddr.lbam_addr);
  244. writeb (0x00, port[num].ioaddr.lbah_addr);
  245. writeb (ATA_DEVICE_OBS, port[num].ioaddr.device_addr);
  246. writeb (ATA_CMD_IDLE, port[num].ioaddr.command_addr);
  247. status = sata_busy_wait (&port[num].ioaddr, ATA_BUSY, 30000, 0);
  248. if ((status & (ATA_BUSY | ATA_ERR))) {
  249. printf ("Error waiting for disk spin up: status 0x%02x\n",
  250. status);
  251. port[num].dev_mask &= ~0x01;
  252. return 1;
  253. }
  254. /* Wait for disk to enter Active state */
  255. do {
  256. msleep (10);
  257. status = check_power_mode (num);
  258. } while ((status == 0x00) || (status == 0x80));
  259. if (status == 0x01) {
  260. printf ("Falied waiting for disk to spin up\n");
  261. return 1;
  262. }
  263. return 0;
  264. }
  265. /* Return value is not the usual here
  266. * 0x00 - Device stand by
  267. * 0x01 - Operation failed
  268. * 0x80 - Device idle
  269. * 0xff - Device active
  270. */
  271. static u8 check_power_mode (int num)
  272. {
  273. u8 status = 0;
  274. u8 res = 0;
  275. if (!(port[num].dev_mask & 0x01)) {
  276. debug ("Device ata%d is not present\n", num);
  277. return 1;
  278. }
  279. if (!(sata_chk_status (&port[num].ioaddr, 0) & ATA_DRDY)) {
  280. printf ("Device ata%d not ready\n", num);
  281. return 1;
  282. }
  283. writeb (0, port[num].ioaddr.feature_addr);
  284. writeb (0, port[num].ioaddr.nsect_addr);
  285. writeb (0, port[num].ioaddr.lbal_addr);
  286. writeb (0, port[num].ioaddr.lbam_addr);
  287. writeb (0, port[num].ioaddr.lbah_addr);
  288. writeb (ATA_DEVICE_OBS, port[num].ioaddr.device_addr);
  289. writeb (ATA_CMD_CHK_POWER, port[num].ioaddr.command_addr);
  290. status = sata_busy_wait (&port[num].ioaddr, ATA_BUSY, 5000, 0);
  291. if ((status & (ATA_BUSY | ATA_ERR))) {
  292. printf
  293. ("Error waiting for check power mode complete : status 0x%02x\n",
  294. status);
  295. port[num].dev_mask &= ~0x01;
  296. return 1;
  297. }
  298. res = readb (port[num].ioaddr.nsect_addr);
  299. debug ("Check powermode: %d\n", res);
  300. return res;
  301. }
  302. static void sata_port (struct sata_ioports *ioport)
  303. {
  304. ioport->data_addr = ioport->cmd_addr + ATA_REG_DATA;
  305. ioport->error_addr = ioport->cmd_addr + ATA_REG_ERR;
  306. ioport->feature_addr = ioport->cmd_addr + ATA_REG_FEATURE;
  307. ioport->nsect_addr = ioport->cmd_addr + ATA_REG_NSECT;
  308. ioport->lbal_addr = ioport->cmd_addr + ATA_REG_LBAL;
  309. ioport->lbam_addr = ioport->cmd_addr + ATA_REG_LBAM;
  310. ioport->lbah_addr = ioport->cmd_addr + ATA_REG_LBAH;
  311. ioport->device_addr = ioport->cmd_addr + ATA_REG_DEVICE;
  312. ioport->status_addr = ioport->cmd_addr + ATA_REG_STATUS;
  313. ioport->command_addr = ioport->cmd_addr + ATA_REG_CMD;
  314. }
  315. static u8 wait_for_irq (int num, unsigned int max)
  316. {
  317. u32 port = iobase[5];
  318. switch (num) {
  319. case 0:
  320. port += VND_TF_CNST_CH0;
  321. break;
  322. case 1:
  323. port += VND_TF_CNST_CH1;
  324. break;
  325. case 2:
  326. port += VND_TF_CNST_CH2;
  327. break;
  328. case 3:
  329. port += VND_TF_CNST_CH3;
  330. break;
  331. default:
  332. return 1;
  333. }
  334. do {
  335. if (readl (port) & VND_TF_CNST_INTST) {
  336. break;
  337. }
  338. udelay (1000);
  339. max--;
  340. } while ((max > 0));
  341. return (max == 0);
  342. }
  343. static u8 sata_busy_wait (struct sata_ioports *ioaddr, int bits,
  344. unsigned int max, u8 usealtstatus)
  345. {
  346. u8 status;
  347. do {
  348. if (!((status = sata_chk_status (ioaddr, usealtstatus)) & bits)) {
  349. break;
  350. }
  351. udelay (1000);
  352. max--;
  353. } while ((status & bits) && (max > 0));
  354. return status;
  355. }
  356. static u8 sata_chk_status (struct sata_ioports *ioaddr, u8 usealtstatus)
  357. {
  358. if (!usealtstatus) {
  359. return readb (ioaddr->status_addr);
  360. } else {
  361. return readb (ioaddr->altstatus_addr);
  362. }
  363. }
  364. static void msleep (int count)
  365. {
  366. int i;
  367. for (i = 0; i < count; i++)
  368. udelay (1000);
  369. }
  370. /* Read up to 255 sectors
  371. *
  372. * Returns sectors read
  373. */
  374. static u8 do_one_read (int device, ulong block, u8 blkcnt, u16 * buff,
  375. uchar lba48)
  376. {
  377. u8 sr = 0;
  378. u8 status;
  379. u64 blknr = (u64) block;
  380. if (!(sata_chk_status (&port[device].ioaddr, 0) & ATA_DRDY)) {
  381. printf ("Device ata%d not ready\n", device);
  382. return 0;
  383. }
  384. /* Set up transfer */
  385. #ifdef CONFIG_LBA48
  386. if (lba48) {
  387. /* write high bits */
  388. writeb (0, port[device].ioaddr.nsect_addr);
  389. writeb ((blknr >> 24) & 0xFF, port[device].ioaddr.lbal_addr);
  390. writeb ((blknr >> 32) & 0xFF, port[device].ioaddr.lbam_addr);
  391. writeb ((blknr >> 40) & 0xFF, port[device].ioaddr.lbah_addr);
  392. }
  393. #endif
  394. writeb (blkcnt, port[device].ioaddr.nsect_addr);
  395. writeb (((blknr) >> 0) & 0xFF, port[device].ioaddr.lbal_addr);
  396. writeb ((blknr >> 8) & 0xFF, port[device].ioaddr.lbam_addr);
  397. writeb ((blknr >> 16) & 0xFF, port[device].ioaddr.lbah_addr);
  398. #ifdef CONFIG_LBA48
  399. if (lba48) {
  400. writeb (ATA_LBA, port[device].ioaddr.device_addr);
  401. writeb (ATA_CMD_PIO_READ_EXT, port[device].ioaddr.command_addr);
  402. } else
  403. #endif
  404. {
  405. writeb (ATA_LBA | ((blknr >> 24) & 0xF),
  406. port[device].ioaddr.device_addr);
  407. writeb (ATA_CMD_PIO_READ, port[device].ioaddr.command_addr);
  408. }
  409. status = sata_busy_wait (&port[device].ioaddr, ATA_BUSY, 10000, 1);
  410. if (status & ATA_BUSY) {
  411. u8 err = 0;
  412. printf ("Device %d not responding status %d\n", device, status);
  413. err = readb (port[device].ioaddr.error_addr);
  414. printf ("Error reg = 0x%x\n", err);
  415. return (sr);
  416. }
  417. while (blkcnt--) {
  418. if (wait_for_irq (device, 500)) {
  419. printf ("ata%u irq failed\n", device);
  420. return sr;
  421. }
  422. status = sata_chk_status (&port[device].ioaddr, 0);
  423. if (status & ATA_ERR) {
  424. printf ("ata%u error %d\n", device,
  425. readb (port[device].ioaddr.error_addr));
  426. return sr;
  427. }
  428. /* Read one sector */
  429. input_data (&port[device].ioaddr, buff, ATA_SECTOR_WORDS);
  430. buff += ATA_SECTOR_WORDS;
  431. sr++;
  432. }
  433. return sr;
  434. }
  435. ulong sata_read (int device, ulong block, lbaint_t blkcnt, void *buff)
  436. {
  437. ulong n = 0, sread;
  438. u16 *buffer = (u16 *) buff;
  439. u8 status = 0;
  440. u64 blknr = (u64) block;
  441. unsigned char lba48 = 0;
  442. #ifdef CONFIG_LBA48
  443. if (blknr > 0xfffffff) {
  444. if (!sata_dev_desc[device].lba48) {
  445. printf ("Drive doesn't support 48-bit addressing\n");
  446. return 0;
  447. }
  448. /* more than 28 bits used, use 48bit mode */
  449. lba48 = 1;
  450. }
  451. #endif
  452. while (blkcnt > 0) {
  453. if (blkcnt > 255) {
  454. sread = 255;
  455. } else {
  456. sread = blkcnt;
  457. }
  458. status = do_one_read (device, blknr, sread, buffer, lba48);
  459. if (status != sread) {
  460. printf ("Read failed\n");
  461. return n;
  462. }
  463. blkcnt -= sread;
  464. blknr += sread;
  465. n += sread;
  466. buffer += sread * ATA_SECTOR_WORDS;
  467. }
  468. return n;
  469. }
  470. ulong sata_write (int device, ulong block, lbaint_t blkcnt, const void *buff)
  471. {
  472. ulong n = 0;
  473. u16 *buffer = (u16 *) buff;
  474. unsigned char status = 0, num = 0;
  475. u64 blknr = (u64) block;
  476. #ifdef CONFIG_LBA48
  477. unsigned char lba48 = 0;
  478. if (blknr > 0xfffffff) {
  479. if (!sata_dev_desc[device].lba48) {
  480. printf ("Drive doesn't support 48-bit addressing\n");
  481. return 0;
  482. }
  483. /* more than 28 bits used, use 48bit mode */
  484. lba48 = 1;
  485. }
  486. #endif
  487. /*Port Number */
  488. num = device;
  489. while (blkcnt-- > 0) {
  490. status = sata_busy_wait (&port[num].ioaddr, ATA_BUSY, 500, 0);
  491. if (status & ATA_BUSY) {
  492. printf ("ata%u failed to respond\n", port[num].port_no);
  493. return n;
  494. }
  495. #ifdef CONFIG_LBA48
  496. if (lba48) {
  497. /* write high bits */
  498. writeb (0, port[num].ioaddr.nsect_addr);
  499. writeb ((blknr >> 24) & 0xFF,
  500. port[num].ioaddr.lbal_addr);
  501. writeb ((blknr >> 32) & 0xFF,
  502. port[num].ioaddr.lbam_addr);
  503. writeb ((blknr >> 40) & 0xFF,
  504. port[num].ioaddr.lbah_addr);
  505. }
  506. #endif
  507. writeb (1, port[num].ioaddr.nsect_addr);
  508. writeb ((blknr >> 0) & 0xFF, port[num].ioaddr.lbal_addr);
  509. writeb ((blknr >> 8) & 0xFF, port[num].ioaddr.lbam_addr);
  510. writeb ((blknr >> 16) & 0xFF, port[num].ioaddr.lbah_addr);
  511. #ifdef CONFIG_LBA48
  512. if (lba48) {
  513. writeb (ATA_LBA, port[num].ioaddr.device_addr);
  514. writeb (ATA_CMD_PIO_WRITE_EXT, port[num].ioaddr.command_addr);
  515. } else
  516. #endif
  517. {
  518. writeb (ATA_LBA | ((blknr >> 24) & 0xF),
  519. port[num].ioaddr.device_addr);
  520. writeb (ATA_CMD_PIO_WRITE, port[num].ioaddr.command_addr);
  521. }
  522. msleep (50);
  523. /*may take up to 4 sec */
  524. status = sata_busy_wait (&port[num].ioaddr, ATA_BUSY, 4000, 0);
  525. if ((status & (ATA_DRQ | ATA_BUSY | ATA_ERR)) != ATA_DRQ) {
  526. printf ("Error no DRQ dev %d blk %ld: sts 0x%02x\n",
  527. device, (ulong) blknr, status);
  528. return (n);
  529. }
  530. output_data (&port[num].ioaddr, buffer, ATA_SECTOR_WORDS);
  531. readb (port[num].ioaddr.altstatus_addr);
  532. udelay (50);
  533. ++n;
  534. ++blknr;
  535. buffer += ATA_SECTOR_WORDS;
  536. }
  537. return n;
  538. }
  539. /* Driver implementation */
  540. static u8 sil_get_device_cache_line (pci_dev_t pdev)
  541. {
  542. u8 cache_line = 0;
  543. pci_read_config_byte (pdev, PCI_CACHE_LINE_SIZE, &cache_line);
  544. return cache_line;
  545. }
  546. int init_sata (int dev)
  547. {
  548. static u8 init_done = 0;
  549. static int res = 1;
  550. pci_dev_t devno;
  551. u8 cls = 0;
  552. u16 cmd = 0;
  553. u32 sconf = 0;
  554. if (init_done) {
  555. return res;
  556. }
  557. init_done = 1;
  558. if ((devno = pci_find_device (SIL_VEND_ID, SIL3114_DEVICE_ID, 0)) == -1) {
  559. res = 1;
  560. return res;
  561. }
  562. /* Read out all BARs, even though we only use MMIO from BAR5 */
  563. pci_read_config_dword (devno, PCI_BASE_ADDRESS_0, &iobase[0]);
  564. pci_read_config_dword (devno, PCI_BASE_ADDRESS_1, &iobase[1]);
  565. pci_read_config_dword (devno, PCI_BASE_ADDRESS_2, &iobase[2]);
  566. pci_read_config_dword (devno, PCI_BASE_ADDRESS_3, &iobase[3]);
  567. pci_read_config_dword (devno, PCI_BASE_ADDRESS_4, &iobase[4]);
  568. pci_read_config_dword (devno, PCI_BASE_ADDRESS_5, &iobase[5]);
  569. if ((iobase[0] == 0xFFFFFFFF) || (iobase[1] == 0xFFFFFFFF) ||
  570. (iobase[2] == 0xFFFFFFFF) || (iobase[3] == 0xFFFFFFFF) ||
  571. (iobase[4] == 0xFFFFFFFF) || (iobase[5] == 0xFFFFFFFF)) {
  572. printf ("Error no base addr for SATA controller\n");
  573. res = 1;
  574. return res;
  575. }
  576. /* mask off unused bits */
  577. iobase[0] &= 0xfffffffc;
  578. iobase[1] &= 0xfffffff8;
  579. iobase[2] &= 0xfffffffc;
  580. iobase[3] &= 0xfffffff8;
  581. iobase[4] &= 0xfffffff0;
  582. iobase[5] &= 0xfffffc00;
  583. /* from sata_sil in Linux kernel */
  584. cls = sil_get_device_cache_line (devno);
  585. if (cls) {
  586. cls >>= 3;
  587. cls++; /* cls = (line_size/8)+1 */
  588. writel (cls << 8 | cls, iobase[5] + VND_FIFOCFG_CH0);
  589. writel (cls << 8 | cls, iobase[5] + VND_FIFOCFG_CH1);
  590. writel (cls << 8 | cls, iobase[5] + VND_FIFOCFG_CH2);
  591. writel (cls << 8 | cls, iobase[5] + VND_FIFOCFG_CH3);
  592. } else {
  593. printf ("Cache line not set. Driver may not function\n");
  594. }
  595. /* Enable operation */
  596. pci_read_config_word (devno, PCI_COMMAND, &cmd);
  597. cmd |= PCI_COMMAND_MASTER | PCI_COMMAND_IO | PCI_COMMAND_MEMORY;
  598. pci_write_config_word (devno, PCI_COMMAND, cmd);
  599. /* Disable interrupt usage */
  600. pci_read_config_dword (devno, VND_SYSCONFSTAT, &sconf);
  601. sconf |= (VND_SYSCONFSTAT_CHN_0_INTBLOCK | VND_SYSCONFSTAT_CHN_1_INTBLOCK);
  602. pci_write_config_dword (devno, VND_SYSCONFSTAT, sconf);
  603. res = 0;
  604. return res;
  605. }
  606. /* Check if device is connected to port */
  607. int sata_bus_probe (int portno)
  608. {
  609. u32 port = iobase[5];
  610. u32 val;
  611. switch (portno) {
  612. case 0:
  613. port += VND_SSTATUS_CH0;
  614. break;
  615. case 1:
  616. port += VND_SSTATUS_CH1;
  617. break;
  618. case 2:
  619. port += VND_SSTATUS_CH2;
  620. break;
  621. case 3:
  622. port += VND_SSTATUS_CH3;
  623. break;
  624. default:
  625. return 0;
  626. }
  627. val = readl (port);
  628. if ((val & SATA_DET_PRES) == SATA_DET_PRES) {
  629. return 1;
  630. } else {
  631. return 0;
  632. }
  633. }
  634. int sata_phy_reset (int portno)
  635. {
  636. u32 port = iobase[5];
  637. u32 val;
  638. switch (portno) {
  639. case 0:
  640. port += VND_SCONTROL_CH0;
  641. break;
  642. case 1:
  643. port += VND_SCONTROL_CH1;
  644. break;
  645. case 2:
  646. port += VND_SCONTROL_CH2;
  647. break;
  648. case 3:
  649. port += VND_SCONTROL_CH3;
  650. break;
  651. default:
  652. return 0;
  653. }
  654. val = readl (port);
  655. writel (val | SATA_SC_DET_RST, port);
  656. msleep (150);
  657. writel (val & ~SATA_SC_DET_RST, port);
  658. return 0;
  659. }
  660. int scan_sata (int dev)
  661. {
  662. /* A bit brain dead, but the code has a legacy */
  663. switch (dev) {
  664. case 0:
  665. port[0].port_no = 0;
  666. port[0].ioaddr.cmd_addr = iobase[5] + VND_TF0_CH0;
  667. port[0].ioaddr.altstatus_addr = port[0].ioaddr.ctl_addr =
  668. (iobase[5] + VND_TF2_CH0) | ATA_PCI_CTL_OFS;
  669. port[0].ioaddr.bmdma_addr = iobase[5] + VND_BMDMA_CH0;
  670. break;
  671. case 1:
  672. port[1].port_no = 0;
  673. port[1].ioaddr.cmd_addr = iobase[5] + VND_TF0_CH1;
  674. port[1].ioaddr.altstatus_addr = port[1].ioaddr.ctl_addr =
  675. (iobase[5] + VND_TF2_CH1) | ATA_PCI_CTL_OFS;
  676. port[1].ioaddr.bmdma_addr = iobase[5] + VND_BMDMA_CH1;
  677. break;
  678. case 2:
  679. port[2].port_no = 0;
  680. port[2].ioaddr.cmd_addr = iobase[5] + VND_TF0_CH2;
  681. port[2].ioaddr.altstatus_addr = port[2].ioaddr.ctl_addr =
  682. (iobase[5] + VND_TF2_CH2) | ATA_PCI_CTL_OFS;
  683. port[2].ioaddr.bmdma_addr = iobase[5] + VND_BMDMA_CH2;
  684. break;
  685. case 3:
  686. port[3].port_no = 0;
  687. port[3].ioaddr.cmd_addr = iobase[5] + VND_TF0_CH3;
  688. port[3].ioaddr.altstatus_addr = port[3].ioaddr.ctl_addr =
  689. (iobase[5] + VND_TF2_CH3) | ATA_PCI_CTL_OFS;
  690. port[3].ioaddr.bmdma_addr = iobase[5] + VND_BMDMA_CH3;
  691. break;
  692. default:
  693. printf ("Tried to scan unknown port: ata%d\n", dev);
  694. return 1;
  695. }
  696. /* Initialize other registers */
  697. sata_port (&port[dev].ioaddr);
  698. /* Check for attached device */
  699. if (!sata_bus_probe (dev)) {
  700. port[dev].port_state = 0;
  701. debug ("SATA#%d port is not present\n", dev);
  702. } else {
  703. debug ("SATA#%d port is present\n", dev);
  704. if (sata_bus_softreset (dev)) {
  705. /* soft reset failed, try a hard one */
  706. sata_phy_reset (dev);
  707. if (sata_bus_softreset (dev)) {
  708. port[dev].port_state = 0;
  709. } else {
  710. port[dev].port_state = 1;
  711. }
  712. } else {
  713. port[dev].port_state = 1;
  714. }
  715. }
  716. if (port[dev].port_state == 1) {
  717. /* Probe device and set xfer mode */
  718. sata_identify (dev, 0);
  719. set_Feature_cmd (dev, 0);
  720. }
  721. return 0;
  722. }