ahci.c 19 KB

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