ahci.c 22 KB

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