ahci.c 26 KB

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