ahci.c 18 KB

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