ahci.c 24 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010
  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. * SPDX-License-Identifier: GPL-2.0+
  7. *
  8. * with the reference on libata and ahci drvier in kernel
  9. */
  10. #include <common.h>
  11. #include <command.h>
  12. #include <pci.h>
  13. #include <asm/processor.h>
  14. #include <asm/errno.h>
  15. #include <asm/io.h>
  16. #include <malloc.h>
  17. #include <scsi.h>
  18. #include <libata.h>
  19. #include <linux/ctype.h>
  20. #include <ahci.h>
  21. static int ata_io_flush(u8 port);
  22. struct ahci_probe_ent *probe_ent = NULL;
  23. u16 *ataid[AHCI_MAX_PORTS];
  24. #define writel_with_flush(a,b) do { writel(a,b); readl(b); } while (0)
  25. /*
  26. * Some controllers limit number of blocks they can read/write at once.
  27. * Contemporary SSD devices work much faster if the read/write size is aligned
  28. * to a power of 2. Let's set default to 128 and allowing to be overwritten if
  29. * needed.
  30. */
  31. #ifndef MAX_SATA_BLOCKS_READ_WRITE
  32. #define MAX_SATA_BLOCKS_READ_WRITE 0x80
  33. #endif
  34. /* Maximum timeouts for each event */
  35. #define WAIT_MS_SPINUP 20000
  36. #define WAIT_MS_DATAIO 5000
  37. #define WAIT_MS_FLUSH 5000
  38. #define WAIT_MS_LINKUP 200
  39. static inline u32 ahci_port_base(u32 base, u32 port)
  40. {
  41. return base + 0x100 + (port * 0x80);
  42. }
  43. static void ahci_setup_port(struct ahci_ioports *port, unsigned long base,
  44. unsigned int port_idx)
  45. {
  46. base = ahci_port_base(base, port_idx);
  47. port->cmd_addr = base;
  48. port->scr_addr = base + PORT_SCR;
  49. }
  50. #define msleep(a) udelay(a * 1000)
  51. static void ahci_dcache_flush_range(unsigned begin, unsigned len)
  52. {
  53. const unsigned long start = begin;
  54. const unsigned long end = start + len;
  55. debug("%s: flush dcache: [%#lx, %#lx)\n", __func__, start, end);
  56. flush_dcache_range(start, end);
  57. }
  58. /*
  59. * SATA controller DMAs to physical RAM. Ensure data from the
  60. * controller is invalidated from dcache; next access comes from
  61. * physical RAM.
  62. */
  63. static void ahci_dcache_invalidate_range(unsigned begin, unsigned len)
  64. {
  65. const unsigned long start = begin;
  66. const unsigned long end = start + len;
  67. debug("%s: invalidate dcache: [%#lx, %#lx)\n", __func__, start, end);
  68. invalidate_dcache_range(start, end);
  69. }
  70. /*
  71. * Ensure data for SATA controller is flushed out of dcache and
  72. * written to physical memory.
  73. */
  74. static void ahci_dcache_flush_sata_cmd(struct ahci_ioports *pp)
  75. {
  76. ahci_dcache_flush_range((unsigned long)pp->cmd_slot,
  77. AHCI_PORT_PRIV_DMA_SZ);
  78. }
  79. static int waiting_for_cmd_completed(volatile u8 *offset,
  80. int timeout_msec,
  81. u32 sign)
  82. {
  83. int i;
  84. u32 status;
  85. for (i = 0; ((status = readl(offset)) & sign) && i < timeout_msec; i++)
  86. msleep(1);
  87. return (i < timeout_msec) ? 0 : -1;
  88. }
  89. int __weak ahci_link_up(struct ahci_probe_ent *probe_ent, u8 port)
  90. {
  91. u32 tmp;
  92. int j = 0;
  93. u8 *port_mmio = (u8 *)probe_ent->port[port].port_mmio;
  94. /*
  95. * Bring up SATA link.
  96. * SATA link bringup time is usually less than 1 ms; only very
  97. * rarely has it taken between 1-2 ms. Never seen it above 2 ms.
  98. */
  99. while (j < WAIT_MS_LINKUP) {
  100. tmp = readl(port_mmio + PORT_SCR_STAT);
  101. tmp &= PORT_SCR_STAT_DET_MASK;
  102. if (tmp == PORT_SCR_STAT_DET_PHYRDY)
  103. return 0;
  104. udelay(1000);
  105. j++;
  106. }
  107. return 1;
  108. }
  109. #ifdef CONFIG_SUNXI_AHCI
  110. /* The sunxi AHCI controller requires this undocumented setup */
  111. static void sunxi_dma_init(volatile u8 *port_mmio)
  112. {
  113. clrsetbits_le32(port_mmio + PORT_P0DMACR, 0x0000ff00, 0x00004400);
  114. }
  115. #endif
  116. static int ahci_host_init(struct ahci_probe_ent *probe_ent)
  117. {
  118. #ifndef CONFIG_SCSI_AHCI_PLAT
  119. pci_dev_t pdev = probe_ent->dev;
  120. u16 tmp16;
  121. unsigned short vendor;
  122. #endif
  123. volatile u8 *mmio = (volatile u8 *)probe_ent->mmio_base;
  124. u32 tmp, cap_save, cmd;
  125. int i, j, ret;
  126. volatile u8 *port_mmio;
  127. u32 port_map;
  128. debug("ahci_host_init: start\n");
  129. cap_save = readl(mmio + HOST_CAP);
  130. cap_save &= ((1 << 28) | (1 << 17));
  131. cap_save |= (1 << 27); /* Staggered Spin-up. Not needed. */
  132. /* global controller reset */
  133. tmp = readl(mmio + HOST_CTL);
  134. if ((tmp & HOST_RESET) == 0)
  135. writel_with_flush(tmp | HOST_RESET, mmio + HOST_CTL);
  136. /* reset must complete within 1 second, or
  137. * the hardware should be considered fried.
  138. */
  139. i = 1000;
  140. do {
  141. udelay(1000);
  142. tmp = readl(mmio + HOST_CTL);
  143. if (!i--) {
  144. debug("controller reset failed (0x%x)\n", tmp);
  145. return -1;
  146. }
  147. } while (tmp & HOST_RESET);
  148. writel_with_flush(HOST_AHCI_EN, mmio + HOST_CTL);
  149. writel(cap_save, mmio + HOST_CAP);
  150. writel_with_flush(0xf, mmio + HOST_PORTS_IMPL);
  151. #ifndef CONFIG_SCSI_AHCI_PLAT
  152. pci_read_config_word(pdev, PCI_VENDOR_ID, &vendor);
  153. if (vendor == PCI_VENDOR_ID_INTEL) {
  154. u16 tmp16;
  155. pci_read_config_word(pdev, 0x92, &tmp16);
  156. tmp16 |= 0xf;
  157. pci_write_config_word(pdev, 0x92, tmp16);
  158. }
  159. #endif
  160. probe_ent->cap = readl(mmio + HOST_CAP);
  161. probe_ent->port_map = readl(mmio + HOST_PORTS_IMPL);
  162. port_map = probe_ent->port_map;
  163. probe_ent->n_ports = (probe_ent->cap & 0x1f) + 1;
  164. debug("cap 0x%x port_map 0x%x n_ports %d\n",
  165. probe_ent->cap, probe_ent->port_map, probe_ent->n_ports);
  166. if (probe_ent->n_ports > CONFIG_SYS_SCSI_MAX_SCSI_ID)
  167. probe_ent->n_ports = CONFIG_SYS_SCSI_MAX_SCSI_ID;
  168. for (i = 0; i < probe_ent->n_ports; i++) {
  169. if (!(port_map & (1 << i)))
  170. continue;
  171. probe_ent->port[i].port_mmio = ahci_port_base((u32) mmio, i);
  172. port_mmio = (u8 *) probe_ent->port[i].port_mmio;
  173. ahci_setup_port(&probe_ent->port[i], (unsigned long)mmio, i);
  174. /* make sure port is not active */
  175. tmp = readl(port_mmio + PORT_CMD);
  176. if (tmp & (PORT_CMD_LIST_ON | PORT_CMD_FIS_ON |
  177. PORT_CMD_FIS_RX | PORT_CMD_START)) {
  178. debug("Port %d is active. Deactivating.\n", i);
  179. tmp &= ~(PORT_CMD_LIST_ON | PORT_CMD_FIS_ON |
  180. PORT_CMD_FIS_RX | PORT_CMD_START);
  181. writel_with_flush(tmp, port_mmio + PORT_CMD);
  182. /* spec says 500 msecs for each bit, so
  183. * this is slightly incorrect.
  184. */
  185. msleep(500);
  186. }
  187. #ifdef CONFIG_SUNXI_AHCI
  188. sunxi_dma_init(port_mmio);
  189. #endif
  190. /* Add the spinup command to whatever mode bits may
  191. * already be on in the command register.
  192. */
  193. cmd = readl(port_mmio + PORT_CMD);
  194. cmd |= PORT_CMD_FIS_RX;
  195. cmd |= PORT_CMD_SPIN_UP;
  196. writel_with_flush(cmd, port_mmio + PORT_CMD);
  197. /* Bring up SATA link. */
  198. ret = ahci_link_up(probe_ent, i);
  199. if (ret) {
  200. printf("SATA link %d timeout.\n", i);
  201. continue;
  202. } else {
  203. debug("SATA link ok.\n");
  204. }
  205. /* Clear error status */
  206. tmp = readl(port_mmio + PORT_SCR_ERR);
  207. if (tmp)
  208. writel(tmp, port_mmio + PORT_SCR_ERR);
  209. debug("Spinning up device on SATA port %d... ", i);
  210. j = 0;
  211. while (j < WAIT_MS_SPINUP) {
  212. tmp = readl(port_mmio + PORT_TFDATA);
  213. if (!(tmp & (ATA_BUSY | ATA_DRQ)))
  214. break;
  215. udelay(1000);
  216. tmp = readl(port_mmio + PORT_SCR_STAT);
  217. tmp &= PORT_SCR_STAT_DET_MASK;
  218. if (tmp == PORT_SCR_STAT_DET_PHYRDY)
  219. break;
  220. j++;
  221. }
  222. tmp = readl(port_mmio + PORT_SCR_STAT) & PORT_SCR_STAT_DET_MASK;
  223. if (tmp == PORT_SCR_STAT_DET_COMINIT) {
  224. debug("SATA link %d down (COMINIT received), retrying...\n", i);
  225. i--;
  226. continue;
  227. }
  228. printf("Target spinup took %d ms.\n", j);
  229. if (j == WAIT_MS_SPINUP)
  230. debug("timeout.\n");
  231. else
  232. debug("ok.\n");
  233. tmp = readl(port_mmio + PORT_SCR_ERR);
  234. debug("PORT_SCR_ERR 0x%x\n", tmp);
  235. writel(tmp, port_mmio + PORT_SCR_ERR);
  236. /* ack any pending irq events for this port */
  237. tmp = readl(port_mmio + PORT_IRQ_STAT);
  238. debug("PORT_IRQ_STAT 0x%x\n", tmp);
  239. if (tmp)
  240. writel(tmp, port_mmio + PORT_IRQ_STAT);
  241. writel(1 << i, mmio + HOST_IRQ_STAT);
  242. /* set irq mask (enables interrupts) */
  243. writel(DEF_PORT_IRQ, port_mmio + PORT_IRQ_MASK);
  244. /* register linkup ports */
  245. tmp = readl(port_mmio + PORT_SCR_STAT);
  246. debug("SATA port %d status: 0x%x\n", i, tmp);
  247. if ((tmp & PORT_SCR_STAT_DET_MASK) == PORT_SCR_STAT_DET_PHYRDY)
  248. probe_ent->link_port_map |= (0x01 << i);
  249. }
  250. tmp = readl(mmio + HOST_CTL);
  251. debug("HOST_CTL 0x%x\n", tmp);
  252. writel(tmp | HOST_IRQ_EN, mmio + HOST_CTL);
  253. tmp = readl(mmio + HOST_CTL);
  254. debug("HOST_CTL 0x%x\n", tmp);
  255. #ifndef CONFIG_SCSI_AHCI_PLAT
  256. pci_read_config_word(pdev, PCI_COMMAND, &tmp16);
  257. tmp |= PCI_COMMAND_MASTER;
  258. pci_write_config_word(pdev, PCI_COMMAND, tmp16);
  259. #endif
  260. return 0;
  261. }
  262. static void ahci_print_info(struct ahci_probe_ent *probe_ent)
  263. {
  264. #ifndef CONFIG_SCSI_AHCI_PLAT
  265. pci_dev_t pdev = probe_ent->dev;
  266. u16 cc;
  267. #endif
  268. volatile u8 *mmio = (volatile u8 *)probe_ent->mmio_base;
  269. u32 vers, cap, cap2, impl, speed;
  270. const char *speed_s;
  271. const char *scc_s;
  272. vers = readl(mmio + HOST_VERSION);
  273. cap = probe_ent->cap;
  274. cap2 = readl(mmio + HOST_CAP2);
  275. impl = probe_ent->port_map;
  276. speed = (cap >> 20) & 0xf;
  277. if (speed == 1)
  278. speed_s = "1.5";
  279. else if (speed == 2)
  280. speed_s = "3";
  281. else if (speed == 3)
  282. speed_s = "6";
  283. else
  284. speed_s = "?";
  285. #ifdef CONFIG_SCSI_AHCI_PLAT
  286. scc_s = "SATA";
  287. #else
  288. pci_read_config_word(pdev, 0x0a, &cc);
  289. if (cc == 0x0101)
  290. scc_s = "IDE";
  291. else if (cc == 0x0106)
  292. scc_s = "SATA";
  293. else if (cc == 0x0104)
  294. scc_s = "RAID";
  295. else
  296. scc_s = "unknown";
  297. #endif
  298. printf("AHCI %02x%02x.%02x%02x "
  299. "%u slots %u ports %s Gbps 0x%x impl %s mode\n",
  300. (vers >> 24) & 0xff,
  301. (vers >> 16) & 0xff,
  302. (vers >> 8) & 0xff,
  303. vers & 0xff,
  304. ((cap >> 8) & 0x1f) + 1, (cap & 0x1f) + 1, speed_s, impl, scc_s);
  305. printf("flags: "
  306. "%s%s%s%s%s%s%s"
  307. "%s%s%s%s%s%s%s"
  308. "%s%s%s%s%s%s\n",
  309. cap & (1 << 31) ? "64bit " : "",
  310. cap & (1 << 30) ? "ncq " : "",
  311. cap & (1 << 28) ? "ilck " : "",
  312. cap & (1 << 27) ? "stag " : "",
  313. cap & (1 << 26) ? "pm " : "",
  314. cap & (1 << 25) ? "led " : "",
  315. cap & (1 << 24) ? "clo " : "",
  316. cap & (1 << 19) ? "nz " : "",
  317. cap & (1 << 18) ? "only " : "",
  318. cap & (1 << 17) ? "pmp " : "",
  319. cap & (1 << 16) ? "fbss " : "",
  320. cap & (1 << 15) ? "pio " : "",
  321. cap & (1 << 14) ? "slum " : "",
  322. cap & (1 << 13) ? "part " : "",
  323. cap & (1 << 7) ? "ccc " : "",
  324. cap & (1 << 6) ? "ems " : "",
  325. cap & (1 << 5) ? "sxs " : "",
  326. cap2 & (1 << 2) ? "apst " : "",
  327. cap2 & (1 << 1) ? "nvmp " : "",
  328. cap2 & (1 << 0) ? "boh " : "");
  329. }
  330. #ifndef CONFIG_SCSI_AHCI_PLAT
  331. static int ahci_init_one(pci_dev_t pdev)
  332. {
  333. u16 vendor;
  334. int rc;
  335. probe_ent = malloc(sizeof(struct ahci_probe_ent));
  336. if (!probe_ent) {
  337. printf("%s: No memory for probe_ent\n", __func__);
  338. return -ENOMEM;
  339. }
  340. memset(probe_ent, 0, sizeof(struct ahci_probe_ent));
  341. probe_ent->dev = pdev;
  342. probe_ent->host_flags = ATA_FLAG_SATA
  343. | ATA_FLAG_NO_LEGACY
  344. | ATA_FLAG_MMIO
  345. | ATA_FLAG_PIO_DMA
  346. | ATA_FLAG_NO_ATAPI;
  347. probe_ent->pio_mask = 0x1f;
  348. probe_ent->udma_mask = 0x7f; /*Fixme,assume to support UDMA6 */
  349. pci_read_config_dword(pdev, PCI_BASE_ADDRESS_5, &probe_ent->mmio_base);
  350. debug("ahci mmio_base=0x%08x\n", probe_ent->mmio_base);
  351. /* Take from kernel:
  352. * JMicron-specific fixup:
  353. * make sure we're in AHCI mode
  354. */
  355. pci_read_config_word(pdev, PCI_VENDOR_ID, &vendor);
  356. if (vendor == 0x197b)
  357. pci_write_config_byte(pdev, 0x41, 0xa1);
  358. /* initialize adapter */
  359. rc = ahci_host_init(probe_ent);
  360. if (rc)
  361. goto err_out;
  362. ahci_print_info(probe_ent);
  363. return 0;
  364. err_out:
  365. return rc;
  366. }
  367. #endif
  368. #define MAX_DATA_BYTE_COUNT (4*1024*1024)
  369. static int ahci_fill_sg(u8 port, unsigned char *buf, int buf_len)
  370. {
  371. struct ahci_ioports *pp = &(probe_ent->port[port]);
  372. struct ahci_sg *ahci_sg = pp->cmd_tbl_sg;
  373. u32 sg_count;
  374. int i;
  375. sg_count = ((buf_len - 1) / MAX_DATA_BYTE_COUNT) + 1;
  376. if (sg_count > AHCI_MAX_SG) {
  377. printf("Error:Too much sg!\n");
  378. return -1;
  379. }
  380. for (i = 0; i < sg_count; i++) {
  381. ahci_sg->addr =
  382. cpu_to_le32((u32) buf + i * MAX_DATA_BYTE_COUNT);
  383. ahci_sg->addr_hi = 0;
  384. ahci_sg->flags_size = cpu_to_le32(0x3fffff &
  385. (buf_len < MAX_DATA_BYTE_COUNT
  386. ? (buf_len - 1)
  387. : (MAX_DATA_BYTE_COUNT - 1)));
  388. ahci_sg++;
  389. buf_len -= MAX_DATA_BYTE_COUNT;
  390. }
  391. return sg_count;
  392. }
  393. static void ahci_fill_cmd_slot(struct ahci_ioports *pp, u32 opts)
  394. {
  395. pp->cmd_slot->opts = cpu_to_le32(opts);
  396. pp->cmd_slot->status = 0;
  397. pp->cmd_slot->tbl_addr = cpu_to_le32(pp->cmd_tbl & 0xffffffff);
  398. pp->cmd_slot->tbl_addr_hi = 0;
  399. }
  400. #ifdef CONFIG_AHCI_SETFEATURES_XFER
  401. static void ahci_set_feature(u8 port)
  402. {
  403. struct ahci_ioports *pp = &(probe_ent->port[port]);
  404. volatile u8 *port_mmio = (volatile u8 *)pp->port_mmio;
  405. u32 cmd_fis_len = 5; /* five dwords */
  406. u8 fis[20];
  407. /* set feature */
  408. memset(fis, 0, sizeof(fis));
  409. fis[0] = 0x27;
  410. fis[1] = 1 << 7;
  411. fis[2] = ATA_CMD_SET_FEATURES;
  412. fis[3] = SETFEATURES_XFER;
  413. fis[12] = __ilog2(probe_ent->udma_mask + 1) + 0x40 - 0x01;
  414. memcpy((unsigned char *)pp->cmd_tbl, fis, sizeof(fis));
  415. ahci_fill_cmd_slot(pp, cmd_fis_len);
  416. ahci_dcache_flush_sata_cmd(pp);
  417. writel(1, port_mmio + PORT_CMD_ISSUE);
  418. readl(port_mmio + PORT_CMD_ISSUE);
  419. if (waiting_for_cmd_completed(port_mmio + PORT_CMD_ISSUE,
  420. WAIT_MS_DATAIO, 0x1)) {
  421. printf("set feature error on port %d!\n", port);
  422. }
  423. }
  424. #endif
  425. static int ahci_port_start(u8 port)
  426. {
  427. struct ahci_ioports *pp = &(probe_ent->port[port]);
  428. volatile u8 *port_mmio = (volatile u8 *)pp->port_mmio;
  429. u32 port_status;
  430. u32 mem;
  431. debug("Enter start port: %d\n", port);
  432. port_status = readl(port_mmio + PORT_SCR_STAT);
  433. debug("Port %d status: %x\n", port, port_status);
  434. if ((port_status & 0xf) != 0x03) {
  435. printf("No Link on this port!\n");
  436. return -1;
  437. }
  438. mem = (u32) malloc(AHCI_PORT_PRIV_DMA_SZ + 2048);
  439. if (!mem) {
  440. free(pp);
  441. printf("%s: No mem for table!\n", __func__);
  442. return -ENOMEM;
  443. }
  444. mem = (mem + 0x800) & (~0x7ff); /* Aligned to 2048-bytes */
  445. memset((u8 *) mem, 0, AHCI_PORT_PRIV_DMA_SZ);
  446. /*
  447. * First item in chunk of DMA memory: 32-slot command table,
  448. * 32 bytes each in size
  449. */
  450. pp->cmd_slot =
  451. (struct ahci_cmd_hdr *)(uintptr_t)virt_to_phys((void *)mem);
  452. debug("cmd_slot = 0x%x\n", (unsigned)pp->cmd_slot);
  453. mem += (AHCI_CMD_SLOT_SZ + 224);
  454. /*
  455. * Second item: Received-FIS area
  456. */
  457. pp->rx_fis = virt_to_phys((void *)mem);
  458. mem += AHCI_RX_FIS_SZ;
  459. /*
  460. * Third item: data area for storing a single command
  461. * and its scatter-gather table
  462. */
  463. pp->cmd_tbl = virt_to_phys((void *)mem);
  464. debug("cmd_tbl_dma = 0x%x\n", pp->cmd_tbl);
  465. mem += AHCI_CMD_TBL_HDR;
  466. pp->cmd_tbl_sg =
  467. (struct ahci_sg *)(uintptr_t)virt_to_phys((void *)mem);
  468. writel_with_flush((u32) pp->cmd_slot, port_mmio + PORT_LST_ADDR);
  469. writel_with_flush(pp->rx_fis, port_mmio + PORT_FIS_ADDR);
  470. #ifdef CONFIG_SUNXI_AHCI
  471. sunxi_dma_init(port_mmio);
  472. #endif
  473. writel_with_flush(PORT_CMD_ICC_ACTIVE | PORT_CMD_FIS_RX |
  474. PORT_CMD_POWER_ON | PORT_CMD_SPIN_UP |
  475. PORT_CMD_START, port_mmio + PORT_CMD);
  476. debug("Exit start port %d\n", port);
  477. return 0;
  478. }
  479. static int ahci_device_data_io(u8 port, u8 *fis, int fis_len, u8 *buf,
  480. int buf_len, u8 is_write)
  481. {
  482. struct ahci_ioports *pp = &(probe_ent->port[port]);
  483. volatile u8 *port_mmio = (volatile u8 *)pp->port_mmio;
  484. u32 opts;
  485. u32 port_status;
  486. int sg_count;
  487. debug("Enter %s: for port %d\n", __func__, port);
  488. if (port > probe_ent->n_ports) {
  489. printf("Invalid port number %d\n", port);
  490. return -1;
  491. }
  492. port_status = readl(port_mmio + PORT_SCR_STAT);
  493. if ((port_status & 0xf) != 0x03) {
  494. debug("No Link on port %d!\n", port);
  495. return -1;
  496. }
  497. memcpy((unsigned char *)pp->cmd_tbl, fis, fis_len);
  498. sg_count = ahci_fill_sg(port, buf, buf_len);
  499. opts = (fis_len >> 2) | (sg_count << 16) | (is_write << 6);
  500. ahci_fill_cmd_slot(pp, opts);
  501. ahci_dcache_flush_sata_cmd(pp);
  502. ahci_dcache_flush_range((unsigned)buf, (unsigned)buf_len);
  503. writel_with_flush(1, port_mmio + PORT_CMD_ISSUE);
  504. if (waiting_for_cmd_completed(port_mmio + PORT_CMD_ISSUE,
  505. WAIT_MS_DATAIO, 0x1)) {
  506. printf("timeout exit!\n");
  507. return -1;
  508. }
  509. ahci_dcache_invalidate_range((unsigned)buf, (unsigned)buf_len);
  510. debug("%s: %d byte transferred.\n", __func__, pp->cmd_slot->status);
  511. return 0;
  512. }
  513. static char *ata_id_strcpy(u16 *target, u16 *src, int len)
  514. {
  515. int i;
  516. for (i = 0; i < len / 2; i++)
  517. target[i] = swab16(src[i]);
  518. return (char *)target;
  519. }
  520. /*
  521. * SCSI INQUIRY command operation.
  522. */
  523. static int ata_scsiop_inquiry(ccb *pccb)
  524. {
  525. static const u8 hdr[] = {
  526. 0,
  527. 0,
  528. 0x5, /* claim SPC-3 version compatibility */
  529. 2,
  530. 95 - 4,
  531. };
  532. u8 fis[20];
  533. u16 *idbuf;
  534. ALLOC_CACHE_ALIGN_BUFFER(u16, tmpid, ATA_ID_WORDS);
  535. u8 port;
  536. /* Clean ccb data buffer */
  537. memset(pccb->pdata, 0, pccb->datalen);
  538. memcpy(pccb->pdata, hdr, sizeof(hdr));
  539. if (pccb->datalen <= 35)
  540. return 0;
  541. memset(fis, 0, sizeof(fis));
  542. /* Construct the FIS */
  543. fis[0] = 0x27; /* Host to device FIS. */
  544. fis[1] = 1 << 7; /* Command FIS. */
  545. fis[2] = ATA_CMD_ID_ATA; /* Command byte. */
  546. /* Read id from sata */
  547. port = pccb->target;
  548. if (ahci_device_data_io(port, (u8 *) &fis, sizeof(fis), (u8 *)tmpid,
  549. ATA_ID_WORDS * 2, 0)) {
  550. debug("scsi_ahci: SCSI inquiry command failure.\n");
  551. return -EIO;
  552. }
  553. if (!ataid[port]) {
  554. ataid[port] = malloc(ATA_ID_WORDS * 2);
  555. if (!ataid[port]) {
  556. printf("%s: No memory for ataid[port]\n", __func__);
  557. return -ENOMEM;
  558. }
  559. }
  560. idbuf = ataid[port];
  561. memcpy(idbuf, tmpid, ATA_ID_WORDS * 2);
  562. ata_swap_buf_le16(idbuf, ATA_ID_WORDS);
  563. memcpy(&pccb->pdata[8], "ATA ", 8);
  564. ata_id_strcpy((u16 *)&pccb->pdata[16], &idbuf[ATA_ID_PROD], 16);
  565. ata_id_strcpy((u16 *)&pccb->pdata[32], &idbuf[ATA_ID_FW_REV], 4);
  566. #ifdef DEBUG
  567. ata_dump_id(idbuf);
  568. #endif
  569. return 0;
  570. }
  571. /*
  572. * SCSI READ10/WRITE10 command operation.
  573. */
  574. static int ata_scsiop_read_write(ccb *pccb, u8 is_write)
  575. {
  576. u32 lba = 0;
  577. u16 blocks = 0;
  578. u8 fis[20];
  579. u8 *user_buffer = pccb->pdata;
  580. u32 user_buffer_size = pccb->datalen;
  581. /* Retrieve the base LBA number from the ccb structure. */
  582. memcpy(&lba, pccb->cmd + 2, sizeof(lba));
  583. lba = be32_to_cpu(lba);
  584. /*
  585. * And the number of blocks.
  586. *
  587. * For 10-byte and 16-byte SCSI R/W commands, transfer
  588. * length 0 means transfer 0 block of data.
  589. * However, for ATA R/W commands, sector count 0 means
  590. * 256 or 65536 sectors, not 0 sectors as in SCSI.
  591. *
  592. * WARNING: one or two older ATA drives treat 0 as 0...
  593. */
  594. blocks = (((u16)pccb->cmd[7]) << 8) | ((u16) pccb->cmd[8]);
  595. debug("scsi_ahci: %s %d blocks starting from lba 0x%x\n",
  596. is_write ? "write" : "read", (unsigned)lba, blocks);
  597. /* Preset the FIS */
  598. memset(fis, 0, sizeof(fis));
  599. fis[0] = 0x27; /* Host to device FIS. */
  600. fis[1] = 1 << 7; /* Command FIS. */
  601. /* Command byte (read/write). */
  602. fis[2] = is_write ? ATA_CMD_WRITE_EXT : ATA_CMD_READ_EXT;
  603. while (blocks) {
  604. u16 now_blocks; /* number of blocks per iteration */
  605. u32 transfer_size; /* number of bytes per iteration */
  606. now_blocks = min(MAX_SATA_BLOCKS_READ_WRITE, blocks);
  607. transfer_size = ATA_SECT_SIZE * now_blocks;
  608. if (transfer_size > user_buffer_size) {
  609. printf("scsi_ahci: Error: buffer too small.\n");
  610. return -EIO;
  611. }
  612. /* LBA48 SATA command but only use 32bit address range within
  613. * that. The next smaller command range (28bit) is too small.
  614. */
  615. fis[4] = (lba >> 0) & 0xff;
  616. fis[5] = (lba >> 8) & 0xff;
  617. fis[6] = (lba >> 16) & 0xff;
  618. fis[7] = 1 << 6; /* device reg: set LBA mode */
  619. fis[8] = ((lba >> 24) & 0xff);
  620. fis[3] = 0xe0; /* features */
  621. /* Block (sector) count */
  622. fis[12] = (now_blocks >> 0) & 0xff;
  623. fis[13] = (now_blocks >> 8) & 0xff;
  624. /* Read/Write from ahci */
  625. if (ahci_device_data_io(pccb->target, (u8 *) &fis, sizeof(fis),
  626. user_buffer, user_buffer_size,
  627. is_write)) {
  628. debug("scsi_ahci: SCSI %s10 command failure.\n",
  629. is_write ? "WRITE" : "READ");
  630. return -EIO;
  631. }
  632. /* If this transaction is a write, do a following flush.
  633. * Writes in u-boot are so rare, and the logic to know when is
  634. * the last write and do a flush only there is sufficiently
  635. * difficult. Just do a flush after every write. This incurs,
  636. * usually, one extra flush when the rare writes do happen.
  637. */
  638. if (is_write) {
  639. if (-EIO == ata_io_flush(pccb->target))
  640. return -EIO;
  641. }
  642. user_buffer += transfer_size;
  643. user_buffer_size -= transfer_size;
  644. blocks -= now_blocks;
  645. lba += now_blocks;
  646. }
  647. return 0;
  648. }
  649. /*
  650. * SCSI READ CAPACITY10 command operation.
  651. */
  652. static int ata_scsiop_read_capacity10(ccb *pccb)
  653. {
  654. u32 cap;
  655. u64 cap64;
  656. u32 block_size;
  657. if (!ataid[pccb->target]) {
  658. printf("scsi_ahci: SCSI READ CAPACITY10 command failure. "
  659. "\tNo ATA info!\n"
  660. "\tPlease run SCSI commmand INQUIRY firstly!\n");
  661. return -EPERM;
  662. }
  663. cap64 = ata_id_n_sectors(ataid[pccb->target]);
  664. if (cap64 > 0x100000000ULL)
  665. cap64 = 0xffffffff;
  666. cap = cpu_to_be32(cap64);
  667. memcpy(pccb->pdata, &cap, sizeof(cap));
  668. block_size = cpu_to_be32((u32)512);
  669. memcpy(&pccb->pdata[4], &block_size, 4);
  670. return 0;
  671. }
  672. /*
  673. * SCSI READ CAPACITY16 command operation.
  674. */
  675. static int ata_scsiop_read_capacity16(ccb *pccb)
  676. {
  677. u64 cap;
  678. u64 block_size;
  679. if (!ataid[pccb->target]) {
  680. printf("scsi_ahci: SCSI READ CAPACITY16 command failure. "
  681. "\tNo ATA info!\n"
  682. "\tPlease run SCSI commmand INQUIRY firstly!\n");
  683. return -EPERM;
  684. }
  685. cap = ata_id_n_sectors(ataid[pccb->target]);
  686. cap = cpu_to_be64(cap);
  687. memcpy(pccb->pdata, &cap, sizeof(cap));
  688. block_size = cpu_to_be64((u64)512);
  689. memcpy(&pccb->pdata[8], &block_size, 8);
  690. return 0;
  691. }
  692. /*
  693. * SCSI TEST UNIT READY command operation.
  694. */
  695. static int ata_scsiop_test_unit_ready(ccb *pccb)
  696. {
  697. return (ataid[pccb->target]) ? 0 : -EPERM;
  698. }
  699. int scsi_exec(ccb *pccb)
  700. {
  701. int ret;
  702. switch (pccb->cmd[0]) {
  703. case SCSI_READ10:
  704. ret = ata_scsiop_read_write(pccb, 0);
  705. break;
  706. case SCSI_WRITE10:
  707. ret = ata_scsiop_read_write(pccb, 1);
  708. break;
  709. case SCSI_RD_CAPAC10:
  710. ret = ata_scsiop_read_capacity10(pccb);
  711. break;
  712. case SCSI_RD_CAPAC16:
  713. ret = ata_scsiop_read_capacity16(pccb);
  714. break;
  715. case SCSI_TST_U_RDY:
  716. ret = ata_scsiop_test_unit_ready(pccb);
  717. break;
  718. case SCSI_INQUIRY:
  719. ret = ata_scsiop_inquiry(pccb);
  720. break;
  721. default:
  722. printf("Unsupport SCSI command 0x%02x\n", pccb->cmd[0]);
  723. return false;
  724. }
  725. if (ret) {
  726. debug("SCSI command 0x%02x ret errno %d\n", pccb->cmd[0], ret);
  727. return false;
  728. }
  729. return true;
  730. }
  731. void scsi_low_level_init(int busdevfunc)
  732. {
  733. int i;
  734. u32 linkmap;
  735. #ifndef CONFIG_SCSI_AHCI_PLAT
  736. ahci_init_one(busdevfunc);
  737. #endif
  738. linkmap = probe_ent->link_port_map;
  739. for (i = 0; i < CONFIG_SYS_SCSI_MAX_SCSI_ID; i++) {
  740. if (((linkmap >> i) & 0x01)) {
  741. if (ahci_port_start((u8) i)) {
  742. printf("Can not start port %d\n", i);
  743. continue;
  744. }
  745. #ifdef CONFIG_AHCI_SETFEATURES_XFER
  746. ahci_set_feature((u8) i);
  747. #endif
  748. }
  749. }
  750. }
  751. #ifdef CONFIG_SCSI_AHCI_PLAT
  752. int ahci_init(u32 base)
  753. {
  754. int i, rc = 0;
  755. u32 linkmap;
  756. probe_ent = malloc(sizeof(struct ahci_probe_ent));
  757. if (!probe_ent) {
  758. printf("%s: No memory for probe_ent\n", __func__);
  759. return -ENOMEM;
  760. }
  761. memset(probe_ent, 0, sizeof(struct ahci_probe_ent));
  762. probe_ent->host_flags = ATA_FLAG_SATA
  763. | ATA_FLAG_NO_LEGACY
  764. | ATA_FLAG_MMIO
  765. | ATA_FLAG_PIO_DMA
  766. | ATA_FLAG_NO_ATAPI;
  767. probe_ent->pio_mask = 0x1f;
  768. probe_ent->udma_mask = 0x7f; /*Fixme,assume to support UDMA6 */
  769. probe_ent->mmio_base = base;
  770. /* initialize adapter */
  771. rc = ahci_host_init(probe_ent);
  772. if (rc)
  773. goto err_out;
  774. ahci_print_info(probe_ent);
  775. linkmap = probe_ent->link_port_map;
  776. for (i = 0; i < CONFIG_SYS_SCSI_MAX_SCSI_ID; i++) {
  777. if (((linkmap >> i) & 0x01)) {
  778. if (ahci_port_start((u8) i)) {
  779. printf("Can not start port %d\n", i);
  780. continue;
  781. }
  782. #ifdef CONFIG_AHCI_SETFEATURES_XFER
  783. ahci_set_feature((u8) i);
  784. #endif
  785. }
  786. }
  787. err_out:
  788. return rc;
  789. }
  790. void __weak scsi_init(void)
  791. {
  792. }
  793. #endif
  794. /*
  795. * In the general case of generic rotating media it makes sense to have a
  796. * flush capability. It probably even makes sense in the case of SSDs because
  797. * one cannot always know for sure what kind of internal cache/flush mechanism
  798. * is embodied therein. At first it was planned to invoke this after the last
  799. * write to disk and before rebooting. In practice, knowing, a priori, which
  800. * is the last write is difficult. Because writing to the disk in u-boot is
  801. * very rare, this flush command will be invoked after every block write.
  802. */
  803. static int ata_io_flush(u8 port)
  804. {
  805. u8 fis[20];
  806. struct ahci_ioports *pp = &(probe_ent->port[port]);
  807. volatile u8 *port_mmio = (volatile u8 *)pp->port_mmio;
  808. u32 cmd_fis_len = 5; /* five dwords */
  809. /* Preset the FIS */
  810. memset(fis, 0, 20);
  811. fis[0] = 0x27; /* Host to device FIS. */
  812. fis[1] = 1 << 7; /* Command FIS. */
  813. fis[2] = ATA_CMD_FLUSH_EXT;
  814. memcpy((unsigned char *)pp->cmd_tbl, fis, 20);
  815. ahci_fill_cmd_slot(pp, cmd_fis_len);
  816. writel_with_flush(1, port_mmio + PORT_CMD_ISSUE);
  817. if (waiting_for_cmd_completed(port_mmio + PORT_CMD_ISSUE,
  818. WAIT_MS_FLUSH, 0x1)) {
  819. debug("scsi_ahci: flush command timeout on port %d.\n", port);
  820. return -EIO;
  821. }
  822. return 0;
  823. }
  824. void scsi_bus_reset(void)
  825. {
  826. /*Not implement*/
  827. }
  828. void scsi_print_error(ccb * pccb)
  829. {
  830. /*The ahci error info can be read in the ahci driver*/
  831. }