ahci.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941
  1. /*
  2. * Copyright (C) Freescale Semiconductor, Inc. 2006.
  3. * Author: Jason Jin<Jason.jin@freescale.com>
  4. * Zhang Wei<wei.zhang@freescale.com>
  5. *
  6. * See file CREDITS for list of people who contributed to this
  7. * project.
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License as
  11. * published by the Free Software Foundation; either version 2 of
  12. * the License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  22. * MA 02111-1307 USA
  23. *
  24. * with the reference on libata and ahci drvier in kernel
  25. *
  26. */
  27. #include <common.h>
  28. #include <command.h>
  29. #include <pci.h>
  30. #include <asm/processor.h>
  31. #include <asm/errno.h>
  32. #include <asm/io.h>
  33. #include <malloc.h>
  34. #include <scsi.h>
  35. #include <ata.h>
  36. #include <linux/ctype.h>
  37. #include <ahci.h>
  38. struct ahci_probe_ent *probe_ent = NULL;
  39. hd_driveid_t *ataid[AHCI_MAX_PORTS];
  40. #define writel_with_flush(a,b) do { writel(a,b); readl(b); } while (0)
  41. /*
  42. * Some controllers limit number of blocks they can read/write at once.
  43. * Contemporary SSD devices work much faster if the read/write size is aligned
  44. * to a power of 2. Let's set default to 128 and allowing to be overwritten if
  45. * needed.
  46. */
  47. #ifndef MAX_SATA_BLOCKS_READ_WRITE
  48. #define MAX_SATA_BLOCKS_READ_WRITE 0x80
  49. #endif
  50. /* Maximum timeouts for each event */
  51. #define WAIT_MS_SPINUP 10000
  52. #define WAIT_MS_DATAIO 5000
  53. #define WAIT_MS_LINKUP 4
  54. static inline u32 ahci_port_base(u32 base, u32 port)
  55. {
  56. return base + 0x100 + (port * 0x80);
  57. }
  58. static void ahci_setup_port(struct ahci_ioports *port, unsigned long base,
  59. unsigned int port_idx)
  60. {
  61. base = ahci_port_base(base, port_idx);
  62. port->cmd_addr = base;
  63. port->scr_addr = base + PORT_SCR;
  64. }
  65. #define msleep(a) udelay(a * 1000)
  66. static void ahci_dcache_flush_range(unsigned begin, unsigned len)
  67. {
  68. const unsigned long start = begin;
  69. const unsigned long end = start + len;
  70. debug("%s: flush dcache: [%#lx, %#lx)\n", __func__, start, end);
  71. flush_dcache_range(start, end);
  72. }
  73. /*
  74. * SATA controller DMAs to physical RAM. Ensure data from the
  75. * controller is invalidated from dcache; next access comes from
  76. * physical RAM.
  77. */
  78. static void ahci_dcache_invalidate_range(unsigned begin, unsigned len)
  79. {
  80. const unsigned long start = begin;
  81. const unsigned long end = start + len;
  82. debug("%s: invalidate dcache: [%#lx, %#lx)\n", __func__, start, end);
  83. invalidate_dcache_range(start, end);
  84. }
  85. /*
  86. * Ensure data for SATA controller is flushed out of dcache and
  87. * written to physical memory.
  88. */
  89. static void ahci_dcache_flush_sata_cmd(struct ahci_ioports *pp)
  90. {
  91. ahci_dcache_flush_range((unsigned long)pp->cmd_slot,
  92. AHCI_PORT_PRIV_DMA_SZ);
  93. }
  94. static int waiting_for_cmd_completed(volatile u8 *offset,
  95. int timeout_msec,
  96. u32 sign)
  97. {
  98. int i;
  99. u32 status;
  100. for (i = 0; ((status = readl(offset)) & sign) && i < timeout_msec; i++)
  101. msleep(1);
  102. return (i < timeout_msec) ? 0 : -1;
  103. }
  104. static int ahci_host_init(struct ahci_probe_ent *probe_ent)
  105. {
  106. #ifndef CONFIG_SCSI_AHCI_PLAT
  107. pci_dev_t pdev = probe_ent->dev;
  108. u16 tmp16;
  109. unsigned short vendor;
  110. #endif
  111. volatile u8 *mmio = (volatile u8 *)probe_ent->mmio_base;
  112. u32 tmp, cap_save, cmd;
  113. int i, j;
  114. volatile u8 *port_mmio;
  115. debug("ahci_host_init: start\n");
  116. cap_save = readl(mmio + HOST_CAP);
  117. cap_save &= ((1 << 28) | (1 << 17));
  118. cap_save |= (1 << 27); /* Staggered Spin-up. Not needed. */
  119. /* global controller reset */
  120. tmp = readl(mmio + HOST_CTL);
  121. if ((tmp & HOST_RESET) == 0)
  122. writel_with_flush(tmp | HOST_RESET, mmio + HOST_CTL);
  123. /* reset must complete within 1 second, or
  124. * the hardware should be considered fried.
  125. */
  126. i = 1000;
  127. do {
  128. udelay(1000);
  129. tmp = readl(mmio + HOST_CTL);
  130. if (!i--) {
  131. debug("controller reset failed (0x%x)\n", tmp);
  132. return -1;
  133. }
  134. } while (tmp & HOST_RESET);
  135. writel_with_flush(HOST_AHCI_EN, mmio + HOST_CTL);
  136. writel(cap_save, mmio + HOST_CAP);
  137. writel_with_flush(0xf, mmio + HOST_PORTS_IMPL);
  138. #ifndef CONFIG_SCSI_AHCI_PLAT
  139. pci_read_config_word(pdev, PCI_VENDOR_ID, &vendor);
  140. if (vendor == PCI_VENDOR_ID_INTEL) {
  141. u16 tmp16;
  142. pci_read_config_word(pdev, 0x92, &tmp16);
  143. tmp16 |= 0xf;
  144. pci_write_config_word(pdev, 0x92, tmp16);
  145. }
  146. #endif
  147. probe_ent->cap = readl(mmio + HOST_CAP);
  148. probe_ent->port_map = readl(mmio + HOST_PORTS_IMPL);
  149. probe_ent->n_ports = (probe_ent->cap & 0x1f) + 1;
  150. debug("cap 0x%x port_map 0x%x n_ports %d\n",
  151. probe_ent->cap, probe_ent->port_map, probe_ent->n_ports);
  152. if (probe_ent->n_ports > CONFIG_SYS_SCSI_MAX_SCSI_ID)
  153. probe_ent->n_ports = CONFIG_SYS_SCSI_MAX_SCSI_ID;
  154. for (i = 0; i < probe_ent->n_ports; i++) {
  155. probe_ent->port[i].port_mmio = ahci_port_base((u32) mmio, i);
  156. port_mmio = (u8 *) probe_ent->port[i].port_mmio;
  157. ahci_setup_port(&probe_ent->port[i], (unsigned long)mmio, i);
  158. /* make sure port is not active */
  159. tmp = readl(port_mmio + PORT_CMD);
  160. if (tmp & (PORT_CMD_LIST_ON | PORT_CMD_FIS_ON |
  161. PORT_CMD_FIS_RX | PORT_CMD_START)) {
  162. debug("Port %d is active. Deactivating.\n", i);
  163. tmp &= ~(PORT_CMD_LIST_ON | PORT_CMD_FIS_ON |
  164. PORT_CMD_FIS_RX | PORT_CMD_START);
  165. writel_with_flush(tmp, port_mmio + PORT_CMD);
  166. /* spec says 500 msecs for each bit, so
  167. * this is slightly incorrect.
  168. */
  169. msleep(500);
  170. }
  171. /* Add the spinup command to whatever mode bits may
  172. * already be on in the command register.
  173. */
  174. cmd = readl(port_mmio + PORT_CMD);
  175. cmd |= PORT_CMD_FIS_RX;
  176. cmd |= PORT_CMD_SPIN_UP;
  177. writel_with_flush(cmd, port_mmio + PORT_CMD);
  178. /* Bring up SATA link.
  179. * SATA link bringup time is usually less than 1 ms; only very
  180. * rarely has it taken between 1-2 ms. Never seen it above 2 ms.
  181. */
  182. j = 0;
  183. while (j < WAIT_MS_LINKUP) {
  184. tmp = readl(port_mmio + PORT_SCR_STAT);
  185. if ((tmp & 0xf) == 0x3)
  186. break;
  187. udelay(1000);
  188. j++;
  189. }
  190. if (j == WAIT_MS_LINKUP) {
  191. printf("SATA link %d timeout.\n", i);
  192. continue;
  193. } else {
  194. debug("SATA link ok.\n");
  195. }
  196. /* Clear error status */
  197. tmp = readl(port_mmio + PORT_SCR_ERR);
  198. if (tmp)
  199. writel(tmp, port_mmio + PORT_SCR_ERR);
  200. debug("Spinning up device on SATA port %d... ", i);
  201. j = 0;
  202. while (j < WAIT_MS_SPINUP) {
  203. tmp = readl(port_mmio + PORT_TFDATA);
  204. if (!(tmp & (ATA_STAT_BUSY | ATA_STAT_DRQ)))
  205. break;
  206. udelay(1000);
  207. j++;
  208. }
  209. printf("Target spinup took %d ms.\n", j);
  210. if (j == WAIT_MS_SPINUP)
  211. debug("timeout.\n");
  212. else
  213. debug("ok.\n");
  214. tmp = readl(port_mmio + PORT_SCR_ERR);
  215. debug("PORT_SCR_ERR 0x%x\n", tmp);
  216. writel(tmp, port_mmio + PORT_SCR_ERR);
  217. /* ack any pending irq events for this port */
  218. tmp = readl(port_mmio + PORT_IRQ_STAT);
  219. debug("PORT_IRQ_STAT 0x%x\n", tmp);
  220. if (tmp)
  221. writel(tmp, port_mmio + PORT_IRQ_STAT);
  222. writel(1 << i, mmio + HOST_IRQ_STAT);
  223. /* set irq mask (enables interrupts) */
  224. writel(DEF_PORT_IRQ, port_mmio + PORT_IRQ_MASK);
  225. /* register linkup ports */
  226. tmp = readl(port_mmio + PORT_SCR_STAT);
  227. debug("Port %d status: 0x%x\n", i, tmp);
  228. if ((tmp & 0xf) == 0x03)
  229. probe_ent->link_port_map |= (0x01 << i);
  230. }
  231. tmp = readl(mmio + HOST_CTL);
  232. debug("HOST_CTL 0x%x\n", tmp);
  233. writel(tmp | HOST_IRQ_EN, mmio + HOST_CTL);
  234. tmp = readl(mmio + HOST_CTL);
  235. debug("HOST_CTL 0x%x\n", tmp);
  236. #ifndef CONFIG_SCSI_AHCI_PLAT
  237. pci_read_config_word(pdev, PCI_COMMAND, &tmp16);
  238. tmp |= PCI_COMMAND_MASTER;
  239. pci_write_config_word(pdev, PCI_COMMAND, tmp16);
  240. #endif
  241. return 0;
  242. }
  243. static void ahci_print_info(struct ahci_probe_ent *probe_ent)
  244. {
  245. #ifndef CONFIG_SCSI_AHCI_PLAT
  246. pci_dev_t pdev = probe_ent->dev;
  247. u16 cc;
  248. #endif
  249. volatile u8 *mmio = (volatile u8 *)probe_ent->mmio_base;
  250. u32 vers, cap, cap2, impl, speed;
  251. const char *speed_s;
  252. const char *scc_s;
  253. vers = readl(mmio + HOST_VERSION);
  254. cap = probe_ent->cap;
  255. cap2 = readl(mmio + HOST_CAP2);
  256. impl = probe_ent->port_map;
  257. speed = (cap >> 20) & 0xf;
  258. if (speed == 1)
  259. speed_s = "1.5";
  260. else if (speed == 2)
  261. speed_s = "3";
  262. else if (speed == 3)
  263. speed_s = "6";
  264. else
  265. speed_s = "?";
  266. #ifdef CONFIG_SCSI_AHCI_PLAT
  267. scc_s = "SATA";
  268. #else
  269. pci_read_config_word(pdev, 0x0a, &cc);
  270. if (cc == 0x0101)
  271. scc_s = "IDE";
  272. else if (cc == 0x0106)
  273. scc_s = "SATA";
  274. else if (cc == 0x0104)
  275. scc_s = "RAID";
  276. else
  277. scc_s = "unknown";
  278. #endif
  279. printf("AHCI %02x%02x.%02x%02x "
  280. "%u slots %u ports %s Gbps 0x%x impl %s mode\n",
  281. (vers >> 24) & 0xff,
  282. (vers >> 16) & 0xff,
  283. (vers >> 8) & 0xff,
  284. vers & 0xff,
  285. ((cap >> 8) & 0x1f) + 1, (cap & 0x1f) + 1, speed_s, impl, scc_s);
  286. printf("flags: "
  287. "%s%s%s%s%s%s%s"
  288. "%s%s%s%s%s%s%s"
  289. "%s%s%s%s%s%s\n",
  290. cap & (1 << 31) ? "64bit " : "",
  291. cap & (1 << 30) ? "ncq " : "",
  292. cap & (1 << 28) ? "ilck " : "",
  293. cap & (1 << 27) ? "stag " : "",
  294. cap & (1 << 26) ? "pm " : "",
  295. cap & (1 << 25) ? "led " : "",
  296. cap & (1 << 24) ? "clo " : "",
  297. cap & (1 << 19) ? "nz " : "",
  298. cap & (1 << 18) ? "only " : "",
  299. cap & (1 << 17) ? "pmp " : "",
  300. cap & (1 << 16) ? "fbss " : "",
  301. cap & (1 << 15) ? "pio " : "",
  302. cap & (1 << 14) ? "slum " : "",
  303. cap & (1 << 13) ? "part " : "",
  304. cap & (1 << 7) ? "ccc " : "",
  305. cap & (1 << 6) ? "ems " : "",
  306. cap & (1 << 5) ? "sxs " : "",
  307. cap2 & (1 << 2) ? "apst " : "",
  308. cap2 & (1 << 1) ? "nvmp " : "",
  309. cap2 & (1 << 0) ? "boh " : "");
  310. }
  311. #ifndef CONFIG_SCSI_AHCI_PLAT
  312. static int ahci_init_one(pci_dev_t pdev)
  313. {
  314. u16 vendor;
  315. int rc;
  316. memset((void *)ataid, 0, sizeof(hd_driveid_t *) * AHCI_MAX_PORTS);
  317. probe_ent = malloc(sizeof(struct ahci_probe_ent));
  318. memset(probe_ent, 0, sizeof(struct ahci_probe_ent));
  319. probe_ent->dev = pdev;
  320. probe_ent->host_flags = ATA_FLAG_SATA
  321. | ATA_FLAG_NO_LEGACY
  322. | ATA_FLAG_MMIO
  323. | ATA_FLAG_PIO_DMA
  324. | ATA_FLAG_NO_ATAPI;
  325. probe_ent->pio_mask = 0x1f;
  326. probe_ent->udma_mask = 0x7f; /*Fixme,assume to support UDMA6 */
  327. pci_read_config_dword(pdev, PCI_BASE_ADDRESS_5, &probe_ent->mmio_base);
  328. debug("ahci mmio_base=0x%08x\n", probe_ent->mmio_base);
  329. /* Take from kernel:
  330. * JMicron-specific fixup:
  331. * make sure we're in AHCI mode
  332. */
  333. pci_read_config_word(pdev, PCI_VENDOR_ID, &vendor);
  334. if (vendor == 0x197b)
  335. pci_write_config_byte(pdev, 0x41, 0xa1);
  336. /* initialize adapter */
  337. rc = ahci_host_init(probe_ent);
  338. if (rc)
  339. goto err_out;
  340. ahci_print_info(probe_ent);
  341. return 0;
  342. err_out:
  343. return rc;
  344. }
  345. #endif
  346. #define MAX_DATA_BYTE_COUNT (4*1024*1024)
  347. static int ahci_fill_sg(u8 port, unsigned char *buf, int buf_len)
  348. {
  349. struct ahci_ioports *pp = &(probe_ent->port[port]);
  350. struct ahci_sg *ahci_sg = pp->cmd_tbl_sg;
  351. u32 sg_count;
  352. int i;
  353. sg_count = ((buf_len - 1) / MAX_DATA_BYTE_COUNT) + 1;
  354. if (sg_count > AHCI_MAX_SG) {
  355. printf("Error:Too much sg!\n");
  356. return -1;
  357. }
  358. for (i = 0; i < sg_count; i++) {
  359. ahci_sg->addr =
  360. cpu_to_le32((u32) buf + i * MAX_DATA_BYTE_COUNT);
  361. ahci_sg->addr_hi = 0;
  362. ahci_sg->flags_size = cpu_to_le32(0x3fffff &
  363. (buf_len < MAX_DATA_BYTE_COUNT
  364. ? (buf_len - 1)
  365. : (MAX_DATA_BYTE_COUNT - 1)));
  366. ahci_sg++;
  367. buf_len -= MAX_DATA_BYTE_COUNT;
  368. }
  369. return sg_count;
  370. }
  371. static void ahci_fill_cmd_slot(struct ahci_ioports *pp, u32 opts)
  372. {
  373. pp->cmd_slot->opts = cpu_to_le32(opts);
  374. pp->cmd_slot->status = 0;
  375. pp->cmd_slot->tbl_addr = cpu_to_le32(pp->cmd_tbl & 0xffffffff);
  376. pp->cmd_slot->tbl_addr_hi = 0;
  377. }
  378. #ifdef CONFIG_AHCI_SETFEATURES_XFER
  379. static void ahci_set_feature(u8 port)
  380. {
  381. struct ahci_ioports *pp = &(probe_ent->port[port]);
  382. volatile u8 *port_mmio = (volatile u8 *)pp->port_mmio;
  383. u32 cmd_fis_len = 5; /* five dwords */
  384. u8 fis[20];
  385. /* set feature */
  386. memset(fis, 0, sizeof(fis));
  387. fis[0] = 0x27;
  388. fis[1] = 1 << 7;
  389. fis[2] = ATA_CMD_SETF;
  390. fis[3] = SETFEATURES_XFER;
  391. fis[12] = __ilog2(probe_ent->udma_mask + 1) + 0x40 - 0x01;
  392. memcpy((unsigned char *)pp->cmd_tbl, fis, sizeof(fis));
  393. ahci_fill_cmd_slot(pp, cmd_fis_len);
  394. ahci_dcache_flush_sata_cmd(pp);
  395. writel(1, port_mmio + PORT_CMD_ISSUE);
  396. readl(port_mmio + PORT_CMD_ISSUE);
  397. if (waiting_for_cmd_completed(port_mmio + PORT_CMD_ISSUE,
  398. WAIT_MS_DATAIO, 0x1)) {
  399. printf("set feature error on port %d!\n", port);
  400. }
  401. }
  402. #endif
  403. static int ahci_port_start(u8 port)
  404. {
  405. struct ahci_ioports *pp = &(probe_ent->port[port]);
  406. volatile u8 *port_mmio = (volatile u8 *)pp->port_mmio;
  407. u32 port_status;
  408. u32 mem;
  409. debug("Enter start port: %d\n", port);
  410. port_status = readl(port_mmio + PORT_SCR_STAT);
  411. debug("Port %d status: %x\n", port, port_status);
  412. if ((port_status & 0xf) != 0x03) {
  413. printf("No Link on this port!\n");
  414. return -1;
  415. }
  416. mem = (u32) malloc(AHCI_PORT_PRIV_DMA_SZ + 2048);
  417. if (!mem) {
  418. free(pp);
  419. printf("No mem for table!\n");
  420. return -ENOMEM;
  421. }
  422. mem = (mem + 0x800) & (~0x7ff); /* Aligned to 2048-bytes */
  423. memset((u8 *) mem, 0, AHCI_PORT_PRIV_DMA_SZ);
  424. /*
  425. * First item in chunk of DMA memory: 32-slot command table,
  426. * 32 bytes each in size
  427. */
  428. pp->cmd_slot =
  429. (struct ahci_cmd_hdr *)(uintptr_t)virt_to_phys((void *)mem);
  430. debug("cmd_slot = 0x%x\n", (unsigned)pp->cmd_slot);
  431. mem += (AHCI_CMD_SLOT_SZ + 224);
  432. /*
  433. * Second item: Received-FIS area
  434. */
  435. pp->rx_fis = virt_to_phys((void *)mem);
  436. mem += AHCI_RX_FIS_SZ;
  437. /*
  438. * Third item: data area for storing a single command
  439. * and its scatter-gather table
  440. */
  441. pp->cmd_tbl = virt_to_phys((void *)mem);
  442. debug("cmd_tbl_dma = 0x%x\n", pp->cmd_tbl);
  443. mem += AHCI_CMD_TBL_HDR;
  444. pp->cmd_tbl_sg =
  445. (struct ahci_sg *)(uintptr_t)virt_to_phys((void *)mem);
  446. writel_with_flush((u32) pp->cmd_slot, port_mmio + PORT_LST_ADDR);
  447. writel_with_flush(pp->rx_fis, port_mmio + PORT_FIS_ADDR);
  448. writel_with_flush(PORT_CMD_ICC_ACTIVE | PORT_CMD_FIS_RX |
  449. PORT_CMD_POWER_ON | PORT_CMD_SPIN_UP |
  450. PORT_CMD_START, port_mmio + PORT_CMD);
  451. debug("Exit start port %d\n", port);
  452. return 0;
  453. }
  454. static int ahci_device_data_io(u8 port, u8 *fis, int fis_len, u8 *buf,
  455. int buf_len, u8 is_write)
  456. {
  457. struct ahci_ioports *pp = &(probe_ent->port[port]);
  458. volatile u8 *port_mmio = (volatile u8 *)pp->port_mmio;
  459. u32 opts;
  460. u32 port_status;
  461. int sg_count;
  462. debug("Enter %s: for port %d\n", __func__, port);
  463. if (port > probe_ent->n_ports) {
  464. printf("Invalid port number %d\n", port);
  465. return -1;
  466. }
  467. port_status = readl(port_mmio + PORT_SCR_STAT);
  468. if ((port_status & 0xf) != 0x03) {
  469. debug("No Link on port %d!\n", port);
  470. return -1;
  471. }
  472. memcpy((unsigned char *)pp->cmd_tbl, fis, fis_len);
  473. sg_count = ahci_fill_sg(port, buf, buf_len);
  474. opts = (fis_len >> 2) | (sg_count << 16) | (is_write << 6);
  475. ahci_fill_cmd_slot(pp, opts);
  476. ahci_dcache_flush_sata_cmd(pp);
  477. ahci_dcache_flush_range((unsigned)buf, (unsigned)buf_len);
  478. writel_with_flush(1, port_mmio + PORT_CMD_ISSUE);
  479. if (waiting_for_cmd_completed(port_mmio + PORT_CMD_ISSUE,
  480. WAIT_MS_DATAIO, 0x1)) {
  481. printf("timeout exit!\n");
  482. return -1;
  483. }
  484. ahci_dcache_invalidate_range((unsigned)buf, (unsigned)buf_len);
  485. debug("%s: %d byte transferred.\n", __func__, pp->cmd_slot->status);
  486. return 0;
  487. }
  488. static char *ata_id_strcpy(u16 *target, u16 *src, int len)
  489. {
  490. int i;
  491. for (i = 0; i < len / 2; i++)
  492. target[i] = swab16(src[i]);
  493. return (char *)target;
  494. }
  495. static void dump_ataid(hd_driveid_t *ataid)
  496. {
  497. debug("(49)ataid->capability = 0x%x\n", ataid->capability);
  498. debug("(53)ataid->field_valid =0x%x\n", ataid->field_valid);
  499. debug("(63)ataid->dma_mword = 0x%x\n", ataid->dma_mword);
  500. debug("(64)ataid->eide_pio_modes = 0x%x\n", ataid->eide_pio_modes);
  501. debug("(75)ataid->queue_depth = 0x%x\n", ataid->queue_depth);
  502. debug("(80)ataid->major_rev_num = 0x%x\n", ataid->major_rev_num);
  503. debug("(81)ataid->minor_rev_num = 0x%x\n", ataid->minor_rev_num);
  504. debug("(82)ataid->command_set_1 = 0x%x\n", ataid->command_set_1);
  505. debug("(83)ataid->command_set_2 = 0x%x\n", ataid->command_set_2);
  506. debug("(84)ataid->cfsse = 0x%x\n", ataid->cfsse);
  507. debug("(85)ataid->cfs_enable_1 = 0x%x\n", ataid->cfs_enable_1);
  508. debug("(86)ataid->cfs_enable_2 = 0x%x\n", ataid->cfs_enable_2);
  509. debug("(87)ataid->csf_default = 0x%x\n", ataid->csf_default);
  510. debug("(88)ataid->dma_ultra = 0x%x\n", ataid->dma_ultra);
  511. debug("(93)ataid->hw_config = 0x%x\n", ataid->hw_config);
  512. }
  513. /*
  514. * SCSI INQUIRY command operation.
  515. */
  516. static int ata_scsiop_inquiry(ccb *pccb)
  517. {
  518. u8 hdr[] = {
  519. 0,
  520. 0,
  521. 0x5, /* claim SPC-3 version compatibility */
  522. 2,
  523. 95 - 4,
  524. };
  525. u8 fis[20];
  526. u8 *tmpid;
  527. u8 port;
  528. /* Clean ccb data buffer */
  529. memset(pccb->pdata, 0, pccb->datalen);
  530. memcpy(pccb->pdata, hdr, sizeof(hdr));
  531. if (pccb->datalen <= 35)
  532. return 0;
  533. memset(fis, 0, sizeof(fis));
  534. /* Construct the FIS */
  535. fis[0] = 0x27; /* Host to device FIS. */
  536. fis[1] = 1 << 7; /* Command FIS. */
  537. fis[2] = ATA_CMD_IDENT; /* Command byte. */
  538. /* Read id from sata */
  539. port = pccb->target;
  540. if (!(tmpid = malloc(sizeof(hd_driveid_t))))
  541. return -ENOMEM;
  542. if (ahci_device_data_io(port, (u8 *) &fis, sizeof(fis), tmpid,
  543. sizeof(hd_driveid_t), 0)) {
  544. debug("scsi_ahci: SCSI inquiry command failure.\n");
  545. return -EIO;
  546. }
  547. if (ataid[port])
  548. free(ataid[port]);
  549. ataid[port] = (hd_driveid_t *) tmpid;
  550. memcpy(&pccb->pdata[8], "ATA ", 8);
  551. ata_id_strcpy((u16 *) &pccb->pdata[16], (u16 *)ataid[port]->model, 16);
  552. ata_id_strcpy((u16 *) &pccb->pdata[32], (u16 *)ataid[port]->fw_rev, 4);
  553. dump_ataid(ataid[port]);
  554. return 0;
  555. }
  556. /*
  557. * SCSI READ10/WRITE10 command operation.
  558. */
  559. static int ata_scsiop_read_write(ccb *pccb, u8 is_write)
  560. {
  561. u32 lba = 0;
  562. u16 blocks = 0;
  563. u8 fis[20];
  564. u8 *user_buffer = pccb->pdata;
  565. u32 user_buffer_size = pccb->datalen;
  566. /* Retrieve the base LBA number from the ccb structure. */
  567. memcpy(&lba, pccb->cmd + 2, sizeof(lba));
  568. lba = be32_to_cpu(lba);
  569. /*
  570. * And the number of blocks.
  571. *
  572. * For 10-byte and 16-byte SCSI R/W commands, transfer
  573. * length 0 means transfer 0 block of data.
  574. * However, for ATA R/W commands, sector count 0 means
  575. * 256 or 65536 sectors, not 0 sectors as in SCSI.
  576. *
  577. * WARNING: one or two older ATA drives treat 0 as 0...
  578. */
  579. blocks = (((u16)pccb->cmd[7]) << 8) | ((u16) pccb->cmd[8]);
  580. debug("scsi_ahci: %s %d blocks starting from lba 0x%x\n",
  581. is_write ? "write" : "read", (unsigned)lba, blocks);
  582. /* Preset the FIS */
  583. memset(fis, 0, sizeof(fis));
  584. fis[0] = 0x27; /* Host to device FIS. */
  585. fis[1] = 1 << 7; /* Command FIS. */
  586. /* Command byte (read/write). */
  587. fis[2] = is_write ? ATA_CMD_WR_DMA : ATA_CMD_RD_DMA;
  588. while (blocks) {
  589. u16 now_blocks; /* number of blocks per iteration */
  590. u32 transfer_size; /* number of bytes per iteration */
  591. now_blocks = min(MAX_SATA_BLOCKS_READ_WRITE, blocks);
  592. transfer_size = ATA_BLOCKSIZE * now_blocks;
  593. if (transfer_size > user_buffer_size) {
  594. printf("scsi_ahci: Error: buffer too small.\n");
  595. return -EIO;
  596. }
  597. /* LBA address, only support LBA28 in this driver */
  598. fis[4] = (lba >> 0) & 0xff;
  599. fis[5] = (lba >> 8) & 0xff;
  600. fis[6] = (lba >> 16) & 0xff;
  601. fis[7] = ((lba >> 24) & 0xf) | 0xe0;
  602. /* Block (sector) count */
  603. fis[12] = (now_blocks >> 0) & 0xff;
  604. fis[13] = (now_blocks >> 8) & 0xff;
  605. /* Read/Write from ahci */
  606. if (ahci_device_data_io(pccb->target, (u8 *) &fis, sizeof(fis),
  607. user_buffer, user_buffer_size,
  608. is_write)) {
  609. debug("scsi_ahci: SCSI %s10 command failure.\n",
  610. is_write ? "WRITE" : "READ");
  611. return -EIO;
  612. }
  613. user_buffer += transfer_size;
  614. user_buffer_size -= transfer_size;
  615. blocks -= now_blocks;
  616. lba += now_blocks;
  617. }
  618. return 0;
  619. }
  620. /*
  621. * SCSI READ CAPACITY10 command operation.
  622. */
  623. static int ata_scsiop_read_capacity10(ccb *pccb)
  624. {
  625. u32 cap;
  626. u32 block_size;
  627. if (!ataid[pccb->target]) {
  628. printf("scsi_ahci: SCSI READ CAPACITY10 command failure. "
  629. "\tNo ATA info!\n"
  630. "\tPlease run SCSI commmand INQUIRY firstly!\n");
  631. return -EPERM;
  632. }
  633. cap = le32_to_cpu(ataid[pccb->target]->lba_capacity);
  634. if (cap == 0xfffffff) {
  635. unsigned short *cap48 = ataid[pccb->target]->lba48_capacity;
  636. if (cap48[2] || cap48[3]) {
  637. cap = 0xffffffff;
  638. } else {
  639. cap = (le16_to_cpu(cap48[1]) << 16) |
  640. (le16_to_cpu(cap48[0]));
  641. }
  642. }
  643. cap = cpu_to_be32(cap);
  644. memcpy(pccb->pdata, &cap, sizeof(cap));
  645. block_size = cpu_to_be32((u32)512);
  646. memcpy(&pccb->pdata[4], &block_size, 4);
  647. return 0;
  648. }
  649. /*
  650. * SCSI READ CAPACITY16 command operation.
  651. */
  652. static int ata_scsiop_read_capacity16(ccb *pccb)
  653. {
  654. u64 cap;
  655. u64 block_size;
  656. if (!ataid[pccb->target]) {
  657. printf("scsi_ahci: SCSI READ CAPACITY16 command failure. "
  658. "\tNo ATA info!\n"
  659. "\tPlease run SCSI commmand INQUIRY firstly!\n");
  660. return -EPERM;
  661. }
  662. cap = le32_to_cpu(ataid[pccb->target]->lba_capacity);
  663. if (cap == 0xfffffff) {
  664. memcpy(&cap, ataid[pccb->target]->lba48_capacity, sizeof(cap));
  665. cap = le64_to_cpu(cap);
  666. }
  667. cap = cpu_to_be64(cap);
  668. memcpy(pccb->pdata, &cap, sizeof(cap));
  669. block_size = cpu_to_be64((u64)512);
  670. memcpy(&pccb->pdata[8], &block_size, 8);
  671. return 0;
  672. }
  673. /*
  674. * SCSI TEST UNIT READY command operation.
  675. */
  676. static int ata_scsiop_test_unit_ready(ccb *pccb)
  677. {
  678. return (ataid[pccb->target]) ? 0 : -EPERM;
  679. }
  680. int scsi_exec(ccb *pccb)
  681. {
  682. int ret;
  683. switch (pccb->cmd[0]) {
  684. case SCSI_READ10:
  685. ret = ata_scsiop_read_write(pccb, 0);
  686. break;
  687. case SCSI_WRITE10:
  688. ret = ata_scsiop_read_write(pccb, 1);
  689. break;
  690. case SCSI_RD_CAPAC10:
  691. ret = ata_scsiop_read_capacity10(pccb);
  692. break;
  693. case SCSI_RD_CAPAC16:
  694. ret = ata_scsiop_read_capacity16(pccb);
  695. break;
  696. case SCSI_TST_U_RDY:
  697. ret = ata_scsiop_test_unit_ready(pccb);
  698. break;
  699. case SCSI_INQUIRY:
  700. ret = ata_scsiop_inquiry(pccb);
  701. break;
  702. default:
  703. printf("Unsupport SCSI command 0x%02x\n", pccb->cmd[0]);
  704. return FALSE;
  705. }
  706. if (ret) {
  707. debug("SCSI command 0x%02x ret errno %d\n", pccb->cmd[0], ret);
  708. return FALSE;
  709. }
  710. return TRUE;
  711. }
  712. void scsi_low_level_init(int busdevfunc)
  713. {
  714. int i;
  715. u32 linkmap;
  716. #ifndef CONFIG_SCSI_AHCI_PLAT
  717. ahci_init_one(busdevfunc);
  718. #endif
  719. linkmap = probe_ent->link_port_map;
  720. for (i = 0; i < CONFIG_SYS_SCSI_MAX_SCSI_ID; i++) {
  721. if (((linkmap >> i) & 0x01)) {
  722. if (ahci_port_start((u8) i)) {
  723. printf("Can not start port %d\n", i);
  724. continue;
  725. }
  726. #ifdef CONFIG_AHCI_SETFEATURES_XFER
  727. ahci_set_feature((u8) i);
  728. #endif
  729. }
  730. }
  731. }
  732. #ifdef CONFIG_SCSI_AHCI_PLAT
  733. int ahci_init(u32 base)
  734. {
  735. int i, rc = 0;
  736. u32 linkmap;
  737. memset(ataid, 0, sizeof(ataid));
  738. probe_ent = malloc(sizeof(struct ahci_probe_ent));
  739. memset(probe_ent, 0, sizeof(struct ahci_probe_ent));
  740. probe_ent->host_flags = ATA_FLAG_SATA
  741. | ATA_FLAG_NO_LEGACY
  742. | ATA_FLAG_MMIO
  743. | ATA_FLAG_PIO_DMA
  744. | ATA_FLAG_NO_ATAPI;
  745. probe_ent->pio_mask = 0x1f;
  746. probe_ent->udma_mask = 0x7f; /*Fixme,assume to support UDMA6 */
  747. probe_ent->mmio_base = base;
  748. /* initialize adapter */
  749. rc = ahci_host_init(probe_ent);
  750. if (rc)
  751. goto err_out;
  752. ahci_print_info(probe_ent);
  753. linkmap = probe_ent->link_port_map;
  754. for (i = 0; i < CONFIG_SYS_SCSI_MAX_SCSI_ID; i++) {
  755. if (((linkmap >> i) & 0x01)) {
  756. if (ahci_port_start((u8) i)) {
  757. printf("Can not start port %d\n", i);
  758. continue;
  759. }
  760. #ifdef CONFIG_AHCI_SETFEATURES_XFER
  761. ahci_set_feature((u8) i);
  762. #endif
  763. }
  764. }
  765. err_out:
  766. return rc;
  767. }
  768. #endif
  769. void scsi_bus_reset(void)
  770. {
  771. /*Not implement*/
  772. }
  773. void scsi_print_error(ccb * pccb)
  774. {
  775. /*The ahci error info can be read in the ahci driver*/
  776. }