ahci.c 27 KB

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