sata_sil.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (C) 2011 Freescale Semiconductor, Inc.
  4. * Author: Tang Yuantian <b29983@freescale.com>
  5. */
  6. #include <common.h>
  7. #include <pci.h>
  8. #include <command.h>
  9. #include <asm/byteorder.h>
  10. #include <malloc.h>
  11. #include <asm/io.h>
  12. #include <fis.h>
  13. #include <sata.h>
  14. #include <libata.h>
  15. #include <sata.h>
  16. #include "sata_sil.h"
  17. /* Convert sectorsize to wordsize */
  18. #define ATA_SECTOR_WORDS (ATA_SECT_SIZE/2)
  19. #define virt_to_bus(devno, v) pci_virt_to_mem(devno, (void *) (v))
  20. static struct sata_info sata_info;
  21. static struct pci_device_id supported[] = {
  22. {PCI_VENDOR_ID_SILICONIMAGE, PCI_DEVICE_ID_SIL3131},
  23. {PCI_VENDOR_ID_SILICONIMAGE, PCI_DEVICE_ID_SIL3132},
  24. {PCI_VENDOR_ID_SILICONIMAGE, PCI_DEVICE_ID_SIL3124},
  25. {}
  26. };
  27. static void sil_sata_dump_fis(struct sata_fis_d2h *s)
  28. {
  29. printf("Status FIS dump:\n");
  30. printf("fis_type: %02x\n", s->fis_type);
  31. printf("pm_port_i: %02x\n", s->pm_port_i);
  32. printf("status: %02x\n", s->status);
  33. printf("error: %02x\n", s->error);
  34. printf("lba_low: %02x\n", s->lba_low);
  35. printf("lba_mid: %02x\n", s->lba_mid);
  36. printf("lba_high: %02x\n", s->lba_high);
  37. printf("device: %02x\n", s->device);
  38. printf("lba_low_exp: %02x\n", s->lba_low_exp);
  39. printf("lba_mid_exp: %02x\n", s->lba_mid_exp);
  40. printf("lba_high_exp: %02x\n", s->lba_high_exp);
  41. printf("res1: %02x\n", s->res1);
  42. printf("sector_count: %02x\n", s->sector_count);
  43. printf("sector_count_exp: %02x\n", s->sector_count_exp);
  44. }
  45. static const char *sata_spd_string(unsigned int speed)
  46. {
  47. static const char * const spd_str[] = {
  48. "1.5 Gbps",
  49. "3.0 Gbps",
  50. "6.0 Gbps",
  51. };
  52. if ((speed - 1) > 2)
  53. return "<unknown>";
  54. return spd_str[speed - 1];
  55. }
  56. static u32 ata_wait_register(void *reg, u32 mask,
  57. u32 val, int timeout_msec)
  58. {
  59. u32 tmp;
  60. tmp = readl(reg);
  61. while ((tmp & mask) == val && timeout_msec > 0) {
  62. mdelay(1);
  63. timeout_msec--;
  64. tmp = readl(reg);
  65. }
  66. return tmp;
  67. }
  68. static void sil_config_port(void *port)
  69. {
  70. /* configure IRQ WoC */
  71. writel(PORT_CS_IRQ_WOC, port + PORT_CTRL_CLR);
  72. /* zero error counters. */
  73. writew(0x8000, port + PORT_DECODE_ERR_THRESH);
  74. writew(0x8000, port + PORT_CRC_ERR_THRESH);
  75. writew(0x8000, port + PORT_HSHK_ERR_THRESH);
  76. writew(0x0000, port + PORT_DECODE_ERR_CNT);
  77. writew(0x0000, port + PORT_CRC_ERR_CNT);
  78. writew(0x0000, port + PORT_HSHK_ERR_CNT);
  79. /* always use 64bit activation */
  80. writel(PORT_CS_32BIT_ACTV, port + PORT_CTRL_CLR);
  81. /* clear port multiplier enable and resume bits */
  82. writel(PORT_CS_PMP_EN | PORT_CS_PMP_RESUME, port + PORT_CTRL_CLR);
  83. }
  84. static int sil_init_port(void *port)
  85. {
  86. u32 tmp;
  87. writel(PORT_CS_INIT, port + PORT_CTRL_STAT);
  88. ata_wait_register(port + PORT_CTRL_STAT,
  89. PORT_CS_INIT, PORT_CS_INIT, 100);
  90. tmp = ata_wait_register(port + PORT_CTRL_STAT,
  91. PORT_CS_RDY, 0, 100);
  92. if ((tmp & (PORT_CS_INIT | PORT_CS_RDY)) != PORT_CS_RDY)
  93. return 1;
  94. return 0;
  95. }
  96. static void sil_read_fis(int dev, int tag, struct sata_fis_d2h *fis)
  97. {
  98. struct sil_sata *sata = sata_dev_desc[dev].priv;
  99. void *port = sata->port;
  100. struct sil_prb *prb;
  101. int i;
  102. u32 *src, *dst;
  103. prb = port + PORT_LRAM + tag * PORT_LRAM_SLOT_SZ;
  104. src = (u32 *)&prb->fis;
  105. dst = (u32 *)fis;
  106. for (i = 0; i < sizeof(struct sata_fis_h2d); i += 4)
  107. *dst++ = readl(src++);
  108. }
  109. static int sil_exec_cmd(int dev, struct sil_cmd_block *pcmd, int tag)
  110. {
  111. struct sil_sata *sata = sata_dev_desc[dev].priv;
  112. void *port = sata->port;
  113. u64 paddr = virt_to_bus(sata->devno, pcmd);
  114. u32 irq_mask, irq_stat;
  115. int rc;
  116. writel(PORT_IRQ_COMPLETE | PORT_IRQ_ERROR, port + PORT_IRQ_ENABLE_CLR);
  117. /* better to add momery barrior here */
  118. writel((u32)paddr, port + PORT_CMD_ACTIVATE + tag * 8);
  119. writel((u64)paddr >> 32, port + PORT_CMD_ACTIVATE + tag * 8 + 4);
  120. irq_mask = (PORT_IRQ_COMPLETE | PORT_IRQ_ERROR) << PORT_IRQ_RAW_SHIFT;
  121. irq_stat = ata_wait_register(port + PORT_IRQ_STAT, irq_mask,
  122. 0, 10000);
  123. /* clear IRQs */
  124. writel(irq_mask, port + PORT_IRQ_STAT);
  125. irq_stat >>= PORT_IRQ_RAW_SHIFT;
  126. if (irq_stat & PORT_IRQ_COMPLETE)
  127. rc = 0;
  128. else {
  129. /* force port into known state */
  130. sil_init_port(port);
  131. if (irq_stat & PORT_IRQ_ERROR)
  132. rc = 1; /* error */
  133. else
  134. rc = 2; /* busy */
  135. }
  136. return rc;
  137. }
  138. static int sil_cmd_set_feature(int dev)
  139. {
  140. struct sil_sata *sata = sata_dev_desc[dev].priv;
  141. struct sil_cmd_block cmdb, *pcmd = &cmdb;
  142. struct sata_fis_d2h fis;
  143. u8 udma_cap;
  144. int ret;
  145. memset((void *)&cmdb, 0, sizeof(struct sil_cmd_block));
  146. pcmd->prb.fis.fis_type = SATA_FIS_TYPE_REGISTER_H2D;
  147. pcmd->prb.fis.pm_port_c = (1 << 7);
  148. pcmd->prb.fis.command = ATA_CMD_SET_FEATURES;
  149. pcmd->prb.fis.features = SETFEATURES_XFER;
  150. /* First check the device capablity */
  151. udma_cap = (u8)(sata->udma & 0xff);
  152. debug("udma_cap %02x\n", udma_cap);
  153. if (udma_cap == ATA_UDMA6)
  154. pcmd->prb.fis.sector_count = XFER_UDMA_6;
  155. if (udma_cap == ATA_UDMA5)
  156. pcmd->prb.fis.sector_count = XFER_UDMA_5;
  157. if (udma_cap == ATA_UDMA4)
  158. pcmd->prb.fis.sector_count = XFER_UDMA_4;
  159. if (udma_cap == ATA_UDMA3)
  160. pcmd->prb.fis.sector_count = XFER_UDMA_3;
  161. ret = sil_exec_cmd(dev, pcmd, 0);
  162. if (ret) {
  163. sil_read_fis(dev, 0, &fis);
  164. printf("Err: exe cmd(0x%x).\n",
  165. readl(sata->port + PORT_SERROR));
  166. sil_sata_dump_fis(&fis);
  167. return 1;
  168. }
  169. return 0;
  170. }
  171. static int sil_cmd_identify_device(int dev, u16 *id)
  172. {
  173. struct sil_sata *sata = sata_dev_desc[dev].priv;
  174. struct sil_cmd_block cmdb, *pcmd = &cmdb;
  175. struct sata_fis_d2h fis;
  176. int ret;
  177. memset((void *)&cmdb, 0, sizeof(struct sil_cmd_block));
  178. pcmd->prb.ctrl = cpu_to_le16(PRB_CTRL_PROTOCOL);
  179. pcmd->prb.prot = cpu_to_le16(PRB_PROT_READ);
  180. pcmd->prb.fis.fis_type = SATA_FIS_TYPE_REGISTER_H2D;
  181. pcmd->prb.fis.pm_port_c = (1 << 7);
  182. pcmd->prb.fis.command = ATA_CMD_ID_ATA;
  183. pcmd->sge.addr = cpu_to_le64(virt_to_bus(sata->devno, id));
  184. pcmd->sge.cnt = cpu_to_le32(sizeof(id[0]) * ATA_ID_WORDS);
  185. pcmd->sge.flags = cpu_to_le32(SGE_TRM);
  186. ret = sil_exec_cmd(dev, pcmd, 0);
  187. if (ret) {
  188. sil_read_fis(dev, 0, &fis);
  189. printf("Err: id cmd(0x%x).\n", readl(sata->port + PORT_SERROR));
  190. sil_sata_dump_fis(&fis);
  191. return 1;
  192. }
  193. ata_swap_buf_le16(id, ATA_ID_WORDS);
  194. return 0;
  195. }
  196. static int sil_cmd_soft_reset(int dev)
  197. {
  198. struct sil_cmd_block cmdb, *pcmd = &cmdb;
  199. struct sil_sata *sata = sata_dev_desc[dev].priv;
  200. struct sata_fis_d2h fis;
  201. void *port = sata->port;
  202. int ret;
  203. /* put the port into known state */
  204. if (sil_init_port(port)) {
  205. printf("SRST: port %d not ready\n", dev);
  206. return 1;
  207. }
  208. memset((void *)&cmdb, 0, sizeof(struct sil_cmd_block));
  209. pcmd->prb.ctrl = cpu_to_le16(PRB_CTRL_SRST);
  210. pcmd->prb.fis.fis_type = SATA_FIS_TYPE_REGISTER_H2D;
  211. pcmd->prb.fis.pm_port_c = 0xf;
  212. ret = sil_exec_cmd(dev, &cmdb, 0);
  213. if (ret) {
  214. sil_read_fis(dev, 0, &fis);
  215. printf("SRST cmd error.\n");
  216. sil_sata_dump_fis(&fis);
  217. return 1;
  218. }
  219. return 0;
  220. }
  221. static ulong sil_sata_rw_cmd(int dev, ulong start, ulong blkcnt,
  222. u8 *buffer, int is_write)
  223. {
  224. struct sil_sata *sata = sata_dev_desc[dev].priv;
  225. struct sil_cmd_block cmdb, *pcmd = &cmdb;
  226. struct sata_fis_d2h fis;
  227. u64 block;
  228. int ret;
  229. block = (u64)start;
  230. memset(pcmd, 0, sizeof(struct sil_cmd_block));
  231. pcmd->prb.ctrl = cpu_to_le16(PRB_CTRL_PROTOCOL);
  232. pcmd->prb.fis.fis_type = SATA_FIS_TYPE_REGISTER_H2D;
  233. pcmd->prb.fis.pm_port_c = (1 << 7);
  234. if (is_write) {
  235. pcmd->prb.fis.command = ATA_CMD_WRITE;
  236. pcmd->prb.prot = cpu_to_le16(PRB_PROT_WRITE);
  237. } else {
  238. pcmd->prb.fis.command = ATA_CMD_READ;
  239. pcmd->prb.prot = cpu_to_le16(PRB_PROT_READ);
  240. }
  241. pcmd->prb.fis.device = ATA_LBA;
  242. pcmd->prb.fis.device |= (block >> 24) & 0xf;
  243. pcmd->prb.fis.lba_high = (block >> 16) & 0xff;
  244. pcmd->prb.fis.lba_mid = (block >> 8) & 0xff;
  245. pcmd->prb.fis.lba_low = block & 0xff;
  246. pcmd->prb.fis.sector_count = (u8)blkcnt & 0xff;
  247. pcmd->sge.addr = cpu_to_le64(virt_to_bus(sata->devno, buffer));
  248. pcmd->sge.cnt = cpu_to_le32(blkcnt * ATA_SECT_SIZE);
  249. pcmd->sge.flags = cpu_to_le32(SGE_TRM);
  250. ret = sil_exec_cmd(dev, pcmd, 0);
  251. if (ret) {
  252. sil_read_fis(dev, 0, &fis);
  253. printf("Err: rw cmd(0x%08x).\n",
  254. readl(sata->port + PORT_SERROR));
  255. sil_sata_dump_fis(&fis);
  256. return 1;
  257. }
  258. return blkcnt;
  259. }
  260. static ulong sil_sata_rw_cmd_ext(int dev, ulong start, ulong blkcnt,
  261. u8 *buffer, int is_write)
  262. {
  263. struct sil_sata *sata = sata_dev_desc[dev].priv;
  264. struct sil_cmd_block cmdb, *pcmd = &cmdb;
  265. struct sata_fis_d2h fis;
  266. u64 block;
  267. int ret;
  268. block = (u64)start;
  269. memset(pcmd, 0, sizeof(struct sil_cmd_block));
  270. pcmd->prb.ctrl = cpu_to_le16(PRB_CTRL_PROTOCOL);
  271. pcmd->prb.fis.fis_type = SATA_FIS_TYPE_REGISTER_H2D;
  272. pcmd->prb.fis.pm_port_c = (1 << 7);
  273. if (is_write) {
  274. pcmd->prb.fis.command = ATA_CMD_WRITE_EXT;
  275. pcmd->prb.prot = cpu_to_le16(PRB_PROT_WRITE);
  276. } else {
  277. pcmd->prb.fis.command = ATA_CMD_READ_EXT;
  278. pcmd->prb.prot = cpu_to_le16(PRB_PROT_READ);
  279. }
  280. pcmd->prb.fis.lba_high_exp = (block >> 40) & 0xff;
  281. pcmd->prb.fis.lba_mid_exp = (block >> 32) & 0xff;
  282. pcmd->prb.fis.lba_low_exp = (block >> 24) & 0xff;
  283. pcmd->prb.fis.lba_high = (block >> 16) & 0xff;
  284. pcmd->prb.fis.lba_mid = (block >> 8) & 0xff;
  285. pcmd->prb.fis.lba_low = block & 0xff;
  286. pcmd->prb.fis.device = ATA_LBA;
  287. pcmd->prb.fis.sector_count_exp = (blkcnt >> 8) & 0xff;
  288. pcmd->prb.fis.sector_count = blkcnt & 0xff;
  289. pcmd->sge.addr = cpu_to_le64(virt_to_bus(sata->devno, buffer));
  290. pcmd->sge.cnt = cpu_to_le32(blkcnt * ATA_SECT_SIZE);
  291. pcmd->sge.flags = cpu_to_le32(SGE_TRM);
  292. ret = sil_exec_cmd(dev, pcmd, 0);
  293. if (ret) {
  294. sil_read_fis(dev, 0, &fis);
  295. printf("Err: rw ext cmd(0x%08x).\n",
  296. readl(sata->port + PORT_SERROR));
  297. sil_sata_dump_fis(&fis);
  298. return 1;
  299. }
  300. return blkcnt;
  301. }
  302. static ulong sil_sata_rw_lba28(int dev, ulong blknr, lbaint_t blkcnt,
  303. const void *buffer, int is_write)
  304. {
  305. ulong start, blks, max_blks;
  306. u8 *addr;
  307. start = blknr;
  308. blks = blkcnt;
  309. addr = (u8 *)buffer;
  310. max_blks = ATA_MAX_SECTORS;
  311. do {
  312. if (blks > max_blks) {
  313. sil_sata_rw_cmd(dev, start, max_blks, addr, is_write);
  314. start += max_blks;
  315. blks -= max_blks;
  316. addr += ATA_SECT_SIZE * max_blks;
  317. } else {
  318. sil_sata_rw_cmd(dev, start, blks, addr, is_write);
  319. start += blks;
  320. blks = 0;
  321. addr += ATA_SECT_SIZE * blks;
  322. }
  323. } while (blks != 0);
  324. return blkcnt;
  325. }
  326. static ulong sil_sata_rw_lba48(int dev, ulong blknr, lbaint_t blkcnt,
  327. const void *buffer, int is_write)
  328. {
  329. ulong start, blks, max_blks;
  330. u8 *addr;
  331. start = blknr;
  332. blks = blkcnt;
  333. addr = (u8 *)buffer;
  334. max_blks = ATA_MAX_SECTORS_LBA48;
  335. do {
  336. if (blks > max_blks) {
  337. sil_sata_rw_cmd_ext(dev, start, max_blks,
  338. addr, is_write);
  339. start += max_blks;
  340. blks -= max_blks;
  341. addr += ATA_SECT_SIZE * max_blks;
  342. } else {
  343. sil_sata_rw_cmd_ext(dev, start, blks,
  344. addr, is_write);
  345. start += blks;
  346. blks = 0;
  347. addr += ATA_SECT_SIZE * blks;
  348. }
  349. } while (blks != 0);
  350. return blkcnt;
  351. }
  352. static void sil_sata_cmd_flush_cache(int dev)
  353. {
  354. struct sil_cmd_block cmdb, *pcmd = &cmdb;
  355. memset((void *)pcmd, 0, sizeof(struct sil_cmd_block));
  356. pcmd->prb.fis.fis_type = SATA_FIS_TYPE_REGISTER_H2D;
  357. pcmd->prb.fis.pm_port_c = (1 << 7);
  358. pcmd->prb.fis.command = ATA_CMD_FLUSH;
  359. sil_exec_cmd(dev, pcmd, 0);
  360. }
  361. static void sil_sata_cmd_flush_cache_ext(int dev)
  362. {
  363. struct sil_cmd_block cmdb, *pcmd = &cmdb;
  364. memset((void *)pcmd, 0, sizeof(struct sil_cmd_block));
  365. pcmd->prb.fis.fis_type = SATA_FIS_TYPE_REGISTER_H2D;
  366. pcmd->prb.fis.pm_port_c = (1 << 7);
  367. pcmd->prb.fis.command = ATA_CMD_FLUSH_EXT;
  368. sil_exec_cmd(dev, pcmd, 0);
  369. }
  370. static void sil_sata_init_wcache(int dev, u16 *id)
  371. {
  372. struct sil_sata *sata = sata_dev_desc[dev].priv;
  373. if (ata_id_has_wcache(id) && ata_id_wcache_enabled(id))
  374. sata->wcache = 1;
  375. if (ata_id_has_flush(id))
  376. sata->flush = 1;
  377. if (ata_id_has_flush_ext(id))
  378. sata->flush_ext = 1;
  379. }
  380. static int sil_sata_get_wcache(int dev)
  381. {
  382. struct sil_sata *sata = sata_dev_desc[dev].priv;
  383. return sata->wcache;
  384. }
  385. static int sil_sata_get_flush(int dev)
  386. {
  387. struct sil_sata *sata = sata_dev_desc[dev].priv;
  388. return sata->flush;
  389. }
  390. static int sil_sata_get_flush_ext(int dev)
  391. {
  392. struct sil_sata *sata = sata_dev_desc[dev].priv;
  393. return sata->flush_ext;
  394. }
  395. /*
  396. * SATA interface between low level driver and command layer
  397. */
  398. ulong sata_read(int dev, ulong blknr, lbaint_t blkcnt, void *buffer)
  399. {
  400. struct sil_sata *sata = sata_dev_desc[dev].priv;
  401. ulong rc;
  402. if (sata->lba48)
  403. rc = sil_sata_rw_lba48(dev, blknr, blkcnt, buffer, READ_CMD);
  404. else
  405. rc = sil_sata_rw_lba28(dev, blknr, blkcnt, buffer, READ_CMD);
  406. return rc;
  407. }
  408. /*
  409. * SATA interface between low level driver and command layer
  410. */
  411. ulong sata_write(int dev, ulong blknr, lbaint_t blkcnt, const void *buffer)
  412. {
  413. struct sil_sata *sata = sata_dev_desc[dev].priv;
  414. ulong rc;
  415. if (sata->lba48) {
  416. rc = sil_sata_rw_lba48(dev, blknr, blkcnt, buffer, WRITE_CMD);
  417. if (sil_sata_get_wcache(dev) && sil_sata_get_flush_ext(dev))
  418. sil_sata_cmd_flush_cache_ext(dev);
  419. } else {
  420. rc = sil_sata_rw_lba28(dev, blknr, blkcnt, buffer, WRITE_CMD);
  421. if (sil_sata_get_wcache(dev) && sil_sata_get_flush(dev))
  422. sil_sata_cmd_flush_cache(dev);
  423. }
  424. return rc;
  425. }
  426. /*
  427. * SATA interface between low level driver and command layer
  428. */
  429. int init_sata(int dev)
  430. {
  431. static int init_done, idx;
  432. pci_dev_t devno;
  433. u16 word;
  434. if (init_done == 1 && dev < sata_info.maxport)
  435. return 0;
  436. init_done = 1;
  437. /* Find PCI device(s) */
  438. devno = pci_find_devices(supported, idx++);
  439. if (devno == -1)
  440. return 1;
  441. pci_read_config_word(devno, PCI_DEVICE_ID, &word);
  442. /* get the port count */
  443. word &= 0xf;
  444. sata_info.portbase = sata_info.maxport;
  445. sata_info.maxport = sata_info.portbase + word;
  446. sata_info.devno = devno;
  447. /* Read out all BARs */
  448. sata_info.iobase[0] = (ulong)pci_map_bar(devno,
  449. PCI_BASE_ADDRESS_0, PCI_REGION_MEM);
  450. sata_info.iobase[1] = (ulong)pci_map_bar(devno,
  451. PCI_BASE_ADDRESS_2, PCI_REGION_MEM);
  452. sata_info.iobase[2] = (ulong)pci_map_bar(devno,
  453. PCI_BASE_ADDRESS_4, PCI_REGION_MEM);
  454. /* mask out the unused bits */
  455. sata_info.iobase[0] &= 0xffffff80;
  456. sata_info.iobase[1] &= 0xfffffc00;
  457. sata_info.iobase[2] &= 0xffffff80;
  458. /* Enable Bus Mastering and memory region */
  459. pci_write_config_word(devno, PCI_COMMAND,
  460. PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER);
  461. /* Check if mem accesses and Bus Mastering are enabled. */
  462. pci_read_config_word(devno, PCI_COMMAND, &word);
  463. if (!(word & PCI_COMMAND_MEMORY) ||
  464. (!(word & PCI_COMMAND_MASTER))) {
  465. printf("Error: Can not enable MEM access or Bus Mastering.\n");
  466. debug("PCI command: %04x\n", word);
  467. return 1;
  468. }
  469. /* GPIO off */
  470. writel(0, (void *)(sata_info.iobase[0] + HOST_FLASH_CMD));
  471. /* clear global reset & mask interrupts during initialization */
  472. writel(0, (void *)(sata_info.iobase[0] + HOST_CTRL));
  473. return 0;
  474. }
  475. int reset_sata(int dev)
  476. {
  477. return 0;
  478. }
  479. /*
  480. * SATA interface between low level driver and command layer
  481. */
  482. int scan_sata(int dev)
  483. {
  484. unsigned char serial[ATA_ID_SERNO_LEN + 1];
  485. unsigned char firmware[ATA_ID_FW_REV_LEN + 1];
  486. unsigned char product[ATA_ID_PROD_LEN + 1];
  487. struct sil_sata *sata;
  488. void *port;
  489. int cnt;
  490. u16 *id;
  491. u32 tmp;
  492. if (dev >= sata_info.maxport) {
  493. printf("SATA#%d is not present\n", dev);
  494. return 1;
  495. }
  496. printf("SATA#%d\n", dev);
  497. port = (void *)sata_info.iobase[1] +
  498. PORT_REGS_SIZE * (dev - sata_info.portbase);
  499. /* Initial PHY setting */
  500. writel(0x20c, port + PORT_PHY_CFG);
  501. /* clear port RST */
  502. tmp = readl(port + PORT_CTRL_STAT);
  503. if (tmp & PORT_CS_PORT_RST) {
  504. writel(PORT_CS_PORT_RST, port + PORT_CTRL_CLR);
  505. tmp = ata_wait_register(port + PORT_CTRL_STAT,
  506. PORT_CS_PORT_RST, PORT_CS_PORT_RST, 100);
  507. if (tmp & PORT_CS_PORT_RST)
  508. printf("Err: Failed to clear port RST\n");
  509. }
  510. /* Check if device is present */
  511. for (cnt = 0; cnt < 100; cnt++) {
  512. tmp = readl(port + PORT_SSTATUS);
  513. if ((tmp & 0xF) == 0x3)
  514. break;
  515. mdelay(1);
  516. }
  517. tmp = readl(port + PORT_SSTATUS);
  518. if ((tmp & 0xf) != 0x3) {
  519. printf(" (No RDY)\n");
  520. return 1;
  521. }
  522. /* Wait for port ready */
  523. tmp = ata_wait_register(port + PORT_CTRL_STAT,
  524. PORT_CS_RDY, PORT_CS_RDY, 100);
  525. if ((tmp & PORT_CS_RDY) != PORT_CS_RDY) {
  526. printf("%d port not ready.\n", dev);
  527. return 1;
  528. }
  529. /* configure port */
  530. sil_config_port(port);
  531. /* Reset port */
  532. writel(PORT_CS_DEV_RST, port + PORT_CTRL_STAT);
  533. readl(port + PORT_CTRL_STAT);
  534. tmp = ata_wait_register(port + PORT_CTRL_STAT, PORT_CS_DEV_RST,
  535. PORT_CS_DEV_RST, 100);
  536. if (tmp & PORT_CS_DEV_RST) {
  537. printf("%d port reset failed.\n", dev);
  538. return 1;
  539. }
  540. sata = (struct sil_sata *)malloc(sizeof(struct sil_sata));
  541. if (!sata) {
  542. printf("%d no memory.\n", dev);
  543. return 1;
  544. }
  545. memset((void *)sata, 0, sizeof(struct sil_sata));
  546. /* turn on port interrupt */
  547. tmp = readl((void *)(sata_info.iobase[0] + HOST_CTRL));
  548. tmp |= (1 << (dev - sata_info.portbase));
  549. writel(tmp, (void *)(sata_info.iobase[0] + HOST_CTRL));
  550. /* Save the private struct to block device struct */
  551. sata_dev_desc[dev].priv = (void *)sata;
  552. sata->port = port;
  553. sata->devno = sata_info.devno;
  554. sprintf(sata->name, "SATA#%d", dev);
  555. sil_cmd_soft_reset(dev);
  556. tmp = readl(port + PORT_SSTATUS);
  557. tmp = (tmp >> 4) & 0xf;
  558. printf(" (%s)\n", sata_spd_string(tmp));
  559. id = (u16 *)malloc(ATA_ID_WORDS * 2);
  560. if (!id) {
  561. printf("Id malloc failed\n");
  562. free((void *)sata);
  563. return 1;
  564. }
  565. sil_cmd_identify_device(dev, id);
  566. #ifdef CONFIG_LBA48
  567. /* Check if support LBA48 */
  568. if (ata_id_has_lba48(id)) {
  569. sata_dev_desc[dev].lba48 = 1;
  570. sata->lba48 = 1;
  571. debug("Device supports LBA48\n");
  572. } else
  573. debug("Device supports LBA28\n");
  574. #endif
  575. /* Serial number */
  576. ata_id_c_string(id, serial, ATA_ID_SERNO, sizeof(serial));
  577. memcpy(sata_dev_desc[dev].product, serial, sizeof(serial));
  578. /* Firmware version */
  579. ata_id_c_string(id, firmware, ATA_ID_FW_REV, sizeof(firmware));
  580. memcpy(sata_dev_desc[dev].revision, firmware, sizeof(firmware));
  581. /* Product model */
  582. ata_id_c_string(id, product, ATA_ID_PROD, sizeof(product));
  583. memcpy(sata_dev_desc[dev].vendor, product, sizeof(product));
  584. /* Totoal sectors */
  585. sata_dev_desc[dev].lba = ata_id_n_sectors(id);
  586. sil_sata_init_wcache(dev, id);
  587. sil_cmd_set_feature(dev);
  588. #ifdef DEBUG
  589. sil_cmd_identify_device(dev, id);
  590. ata_dump_id(id);
  591. #endif
  592. free((void *)id);
  593. return 0;
  594. }