sata_sil.c 18 KB

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