ahci.c 22 KB

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