sata_sil.c 18 KB

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