ahci.c 19 KB

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