sata_sil.c 18 KB

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