sata_sil.c 18 KB

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