ahci.c 20 KB

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