ahci.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (C) Freescale Semiconductor, Inc. 2006.
  4. * Author: Jason Jin<Jason.jin@freescale.com>
  5. * Zhang Wei<wei.zhang@freescale.com>
  6. *
  7. * with the reference on libata and ahci drvier in kernel
  8. *
  9. * This driver provides a SCSI interface to SATA.
  10. */
  11. #include <common.h>
  12. #include <command.h>
  13. #include <dm.h>
  14. #include <pci.h>
  15. #include <asm/processor.h>
  16. #include <linux/errno.h>
  17. #include <asm/io.h>
  18. #include <malloc.h>
  19. #include <memalign.h>
  20. #include <pci.h>
  21. #include <scsi.h>
  22. #include <libata.h>
  23. #include <linux/ctype.h>
  24. #include <ahci.h>
  25. #include <dm/device-internal.h>
  26. #include <dm/lists.h>
  27. static int ata_io_flush(struct ahci_uc_priv *uc_priv, u8 port);
  28. #ifndef CONFIG_DM_SCSI
  29. struct ahci_uc_priv *probe_ent = NULL;
  30. #endif
  31. #define writel_with_flush(a,b) do { writel(a,b); readl(b); } while (0)
  32. /*
  33. * Some controllers limit number of blocks they can read/write at once.
  34. * Contemporary SSD devices work much faster if the read/write size is aligned
  35. * to a power of 2. Let's set default to 128 and allowing to be overwritten if
  36. * needed.
  37. */
  38. #ifndef MAX_SATA_BLOCKS_READ_WRITE
  39. #define MAX_SATA_BLOCKS_READ_WRITE 0x80
  40. #endif
  41. /* Maximum timeouts for each event */
  42. #define WAIT_MS_SPINUP 20000
  43. #define WAIT_MS_DATAIO 10000
  44. #define WAIT_MS_FLUSH 5000
  45. #define WAIT_MS_LINKUP 200
  46. __weak void __iomem *ahci_port_base(void __iomem *base, u32 port)
  47. {
  48. return base + 0x100 + (port * 0x80);
  49. }
  50. static void ahci_setup_port(struct ahci_ioports *port, void __iomem *base,
  51. unsigned int port_idx)
  52. {
  53. base = ahci_port_base(base, port_idx);
  54. port->cmd_addr = base;
  55. port->scr_addr = base + PORT_SCR;
  56. }
  57. #define msleep(a) udelay(a * 1000)
  58. static void ahci_dcache_flush_range(unsigned long begin, unsigned long len)
  59. {
  60. const unsigned long start = begin;
  61. const unsigned long end = start + len;
  62. debug("%s: flush dcache: [%#lx, %#lx)\n", __func__, start, end);
  63. flush_dcache_range(start, end);
  64. }
  65. /*
  66. * SATA controller DMAs to physical RAM. Ensure data from the
  67. * controller is invalidated from dcache; next access comes from
  68. * physical RAM.
  69. */
  70. static void ahci_dcache_invalidate_range(unsigned long begin, unsigned long len)
  71. {
  72. const unsigned long start = begin;
  73. const unsigned long end = start + len;
  74. debug("%s: invalidate dcache: [%#lx, %#lx)\n", __func__, start, end);
  75. invalidate_dcache_range(start, end);
  76. }
  77. /*
  78. * Ensure data for SATA controller is flushed out of dcache and
  79. * written to physical memory.
  80. */
  81. static void ahci_dcache_flush_sata_cmd(struct ahci_ioports *pp)
  82. {
  83. ahci_dcache_flush_range((unsigned long)pp->cmd_slot,
  84. AHCI_PORT_PRIV_DMA_SZ);
  85. }
  86. static int waiting_for_cmd_completed(void __iomem *offset,
  87. int timeout_msec,
  88. u32 sign)
  89. {
  90. int i;
  91. u32 status;
  92. for (i = 0; ((status = readl(offset)) & sign) && i < timeout_msec; i++)
  93. msleep(1);
  94. return (i < timeout_msec) ? 0 : -1;
  95. }
  96. int __weak ahci_link_up(struct ahci_uc_priv *uc_priv, u8 port)
  97. {
  98. u32 tmp;
  99. int j = 0;
  100. void __iomem *port_mmio = uc_priv->port[port].port_mmio;
  101. /*
  102. * Bring up SATA link.
  103. * SATA link bringup time is usually less than 1 ms; only very
  104. * rarely has it taken between 1-2 ms. Never seen it above 2 ms.
  105. */
  106. while (j < WAIT_MS_LINKUP) {
  107. tmp = readl(port_mmio + PORT_SCR_STAT);
  108. tmp &= PORT_SCR_STAT_DET_MASK;
  109. if (tmp == PORT_SCR_STAT_DET_PHYRDY)
  110. return 0;
  111. udelay(1000);
  112. j++;
  113. }
  114. return 1;
  115. }
  116. #ifdef CONFIG_SUNXI_AHCI
  117. /* The sunxi AHCI controller requires this undocumented setup */
  118. static void sunxi_dma_init(void __iomem *port_mmio)
  119. {
  120. clrsetbits_le32(port_mmio + PORT_P0DMACR, 0x0000ff00, 0x00004400);
  121. }
  122. #endif
  123. int ahci_reset(void __iomem *base)
  124. {
  125. int i = 1000;
  126. u32 __iomem *host_ctl_reg = base + HOST_CTL;
  127. u32 tmp = readl(host_ctl_reg); /* global controller reset */
  128. if ((tmp & HOST_RESET) == 0)
  129. writel_with_flush(tmp | HOST_RESET, host_ctl_reg);
  130. /*
  131. * reset must complete within 1 second, or
  132. * the hardware should be considered fried.
  133. */
  134. do {
  135. udelay(1000);
  136. tmp = readl(host_ctl_reg);
  137. i--;
  138. } while ((i > 0) && (tmp & HOST_RESET));
  139. if (i == 0) {
  140. printf("controller reset failed (0x%x)\n", tmp);
  141. return -1;
  142. }
  143. return 0;
  144. }
  145. static int ahci_host_init(struct ahci_uc_priv *uc_priv)
  146. {
  147. #if !defined(CONFIG_SCSI_AHCI_PLAT) && !defined(CONFIG_DM_SCSI)
  148. # ifdef CONFIG_DM_PCI
  149. struct udevice *dev = uc_priv->dev;
  150. struct pci_child_platdata *pplat = dev_get_parent_platdata(dev);
  151. # else
  152. pci_dev_t pdev = uc_priv->dev;
  153. unsigned short vendor;
  154. # endif
  155. u16 tmp16;
  156. #endif
  157. void __iomem *mmio = uc_priv->mmio_base;
  158. u32 tmp, cap_save, cmd;
  159. int i, j, ret;
  160. void __iomem *port_mmio;
  161. u32 port_map;
  162. debug("ahci_host_init: start\n");
  163. cap_save = readl(mmio + HOST_CAP);
  164. cap_save &= ((1 << 28) | (1 << 17));
  165. cap_save |= (1 << 27); /* Staggered Spin-up. Not needed. */
  166. ret = ahci_reset(uc_priv->mmio_base);
  167. if (ret)
  168. return ret;
  169. writel_with_flush(HOST_AHCI_EN, mmio + HOST_CTL);
  170. writel(cap_save, mmio + HOST_CAP);
  171. writel_with_flush(0xf, mmio + HOST_PORTS_IMPL);
  172. #if !defined(CONFIG_SCSI_AHCI_PLAT) && !defined(CONFIG_DM_SCSI)
  173. # ifdef CONFIG_DM_PCI
  174. if (pplat->vendor == PCI_VENDOR_ID_INTEL) {
  175. u16 tmp16;
  176. dm_pci_read_config16(dev, 0x92, &tmp16);
  177. dm_pci_write_config16(dev, 0x92, tmp16 | 0xf);
  178. }
  179. # else
  180. pci_read_config_word(pdev, PCI_VENDOR_ID, &vendor);
  181. if (vendor == PCI_VENDOR_ID_INTEL) {
  182. u16 tmp16;
  183. pci_read_config_word(pdev, 0x92, &tmp16);
  184. tmp16 |= 0xf;
  185. pci_write_config_word(pdev, 0x92, tmp16);
  186. }
  187. # endif
  188. #endif
  189. uc_priv->cap = readl(mmio + HOST_CAP);
  190. uc_priv->port_map = readl(mmio + HOST_PORTS_IMPL);
  191. port_map = uc_priv->port_map;
  192. uc_priv->n_ports = (uc_priv->cap & 0x1f) + 1;
  193. debug("cap 0x%x port_map 0x%x n_ports %d\n",
  194. uc_priv->cap, uc_priv->port_map, uc_priv->n_ports);
  195. if (uc_priv->n_ports > CONFIG_SYS_SCSI_MAX_SCSI_ID)
  196. uc_priv->n_ports = CONFIG_SYS_SCSI_MAX_SCSI_ID;
  197. for (i = 0; i < uc_priv->n_ports; i++) {
  198. if (!(port_map & (1 << i)))
  199. continue;
  200. uc_priv->port[i].port_mmio = ahci_port_base(mmio, i);
  201. port_mmio = (u8 *)uc_priv->port[i].port_mmio;
  202. ahci_setup_port(&uc_priv->port[i], mmio, i);
  203. /* make sure port is not active */
  204. tmp = readl(port_mmio + PORT_CMD);
  205. if (tmp & (PORT_CMD_LIST_ON | PORT_CMD_FIS_ON |
  206. PORT_CMD_FIS_RX | PORT_CMD_START)) {
  207. debug("Port %d is active. Deactivating.\n", i);
  208. tmp &= ~(PORT_CMD_LIST_ON | PORT_CMD_FIS_ON |
  209. PORT_CMD_FIS_RX | PORT_CMD_START);
  210. writel_with_flush(tmp, port_mmio + PORT_CMD);
  211. /* spec says 500 msecs for each bit, so
  212. * this is slightly incorrect.
  213. */
  214. msleep(500);
  215. }
  216. #ifdef CONFIG_SUNXI_AHCI
  217. sunxi_dma_init(port_mmio);
  218. #endif
  219. /* Add the spinup command to whatever mode bits may
  220. * already be on in the command register.
  221. */
  222. cmd = readl(port_mmio + PORT_CMD);
  223. cmd |= PORT_CMD_SPIN_UP;
  224. writel_with_flush(cmd, port_mmio + PORT_CMD);
  225. /* Bring up SATA link. */
  226. ret = ahci_link_up(uc_priv, i);
  227. if (ret) {
  228. printf("SATA link %d timeout.\n", i);
  229. continue;
  230. } else {
  231. debug("SATA link ok.\n");
  232. }
  233. /* Clear error status */
  234. tmp = readl(port_mmio + PORT_SCR_ERR);
  235. if (tmp)
  236. writel(tmp, port_mmio + PORT_SCR_ERR);
  237. debug("Spinning up device on SATA port %d... ", i);
  238. j = 0;
  239. while (j < WAIT_MS_SPINUP) {
  240. tmp = readl(port_mmio + PORT_TFDATA);
  241. if (!(tmp & (ATA_BUSY | ATA_DRQ)))
  242. break;
  243. udelay(1000);
  244. tmp = readl(port_mmio + PORT_SCR_STAT);
  245. tmp &= PORT_SCR_STAT_DET_MASK;
  246. if (tmp == PORT_SCR_STAT_DET_PHYRDY)
  247. break;
  248. j++;
  249. }
  250. tmp = readl(port_mmio + PORT_SCR_STAT) & PORT_SCR_STAT_DET_MASK;
  251. if (tmp == PORT_SCR_STAT_DET_COMINIT) {
  252. debug("SATA link %d down (COMINIT received), retrying...\n", i);
  253. i--;
  254. continue;
  255. }
  256. printf("Target spinup took %d ms.\n", j);
  257. if (j == WAIT_MS_SPINUP)
  258. debug("timeout.\n");
  259. else
  260. debug("ok.\n");
  261. tmp = readl(port_mmio + PORT_SCR_ERR);
  262. debug("PORT_SCR_ERR 0x%x\n", tmp);
  263. writel(tmp, port_mmio + PORT_SCR_ERR);
  264. /* ack any pending irq events for this port */
  265. tmp = readl(port_mmio + PORT_IRQ_STAT);
  266. debug("PORT_IRQ_STAT 0x%x\n", tmp);
  267. if (tmp)
  268. writel(tmp, port_mmio + PORT_IRQ_STAT);
  269. writel(1 << i, mmio + HOST_IRQ_STAT);
  270. /* register linkup ports */
  271. tmp = readl(port_mmio + PORT_SCR_STAT);
  272. debug("SATA port %d status: 0x%x\n", i, tmp);
  273. if ((tmp & PORT_SCR_STAT_DET_MASK) == PORT_SCR_STAT_DET_PHYRDY)
  274. uc_priv->link_port_map |= (0x01 << i);
  275. }
  276. tmp = readl(mmio + HOST_CTL);
  277. debug("HOST_CTL 0x%x\n", tmp);
  278. writel(tmp | HOST_IRQ_EN, mmio + HOST_CTL);
  279. tmp = readl(mmio + HOST_CTL);
  280. debug("HOST_CTL 0x%x\n", tmp);
  281. #if !defined(CONFIG_DM_SCSI)
  282. #ifndef CONFIG_SCSI_AHCI_PLAT
  283. # ifdef CONFIG_DM_PCI
  284. dm_pci_read_config16(dev, PCI_COMMAND, &tmp16);
  285. tmp |= PCI_COMMAND_MASTER;
  286. dm_pci_write_config16(dev, PCI_COMMAND, tmp16);
  287. # else
  288. pci_read_config_word(pdev, PCI_COMMAND, &tmp16);
  289. tmp |= PCI_COMMAND_MASTER;
  290. pci_write_config_word(pdev, PCI_COMMAND, tmp16);
  291. # endif
  292. #endif
  293. #endif
  294. return 0;
  295. }
  296. static void ahci_print_info(struct ahci_uc_priv *uc_priv)
  297. {
  298. #if !defined(CONFIG_SCSI_AHCI_PLAT) && !defined(CONFIG_DM_SCSI)
  299. # if defined(CONFIG_DM_PCI)
  300. struct udevice *dev = uc_priv->dev;
  301. # else
  302. pci_dev_t pdev = uc_priv->dev;
  303. # endif
  304. u16 cc;
  305. #endif
  306. void __iomem *mmio = uc_priv->mmio_base;
  307. u32 vers, cap, cap2, impl, speed;
  308. const char *speed_s;
  309. const char *scc_s;
  310. vers = readl(mmio + HOST_VERSION);
  311. cap = uc_priv->cap;
  312. cap2 = readl(mmio + HOST_CAP2);
  313. impl = uc_priv->port_map;
  314. speed = (cap >> 20) & 0xf;
  315. if (speed == 1)
  316. speed_s = "1.5";
  317. else if (speed == 2)
  318. speed_s = "3";
  319. else if (speed == 3)
  320. speed_s = "6";
  321. else
  322. speed_s = "?";
  323. #if defined(CONFIG_SCSI_AHCI_PLAT) || defined(CONFIG_DM_SCSI)
  324. scc_s = "SATA";
  325. #else
  326. # ifdef CONFIG_DM_PCI
  327. dm_pci_read_config16(dev, 0x0a, &cc);
  328. # else
  329. pci_read_config_word(pdev, 0x0a, &cc);
  330. # endif
  331. if (cc == 0x0101)
  332. scc_s = "IDE";
  333. else if (cc == 0x0106)
  334. scc_s = "SATA";
  335. else if (cc == 0x0104)
  336. scc_s = "RAID";
  337. else
  338. scc_s = "unknown";
  339. #endif
  340. printf("AHCI %02x%02x.%02x%02x "
  341. "%u slots %u ports %s Gbps 0x%x impl %s mode\n",
  342. (vers >> 24) & 0xff,
  343. (vers >> 16) & 0xff,
  344. (vers >> 8) & 0xff,
  345. vers & 0xff,
  346. ((cap >> 8) & 0x1f) + 1, (cap & 0x1f) + 1, speed_s, impl, scc_s);
  347. printf("flags: "
  348. "%s%s%s%s%s%s%s"
  349. "%s%s%s%s%s%s%s"
  350. "%s%s%s%s%s%s\n",
  351. cap & (1 << 31) ? "64bit " : "",
  352. cap & (1 << 30) ? "ncq " : "",
  353. cap & (1 << 28) ? "ilck " : "",
  354. cap & (1 << 27) ? "stag " : "",
  355. cap & (1 << 26) ? "pm " : "",
  356. cap & (1 << 25) ? "led " : "",
  357. cap & (1 << 24) ? "clo " : "",
  358. cap & (1 << 19) ? "nz " : "",
  359. cap & (1 << 18) ? "only " : "",
  360. cap & (1 << 17) ? "pmp " : "",
  361. cap & (1 << 16) ? "fbss " : "",
  362. cap & (1 << 15) ? "pio " : "",
  363. cap & (1 << 14) ? "slum " : "",
  364. cap & (1 << 13) ? "part " : "",
  365. cap & (1 << 7) ? "ccc " : "",
  366. cap & (1 << 6) ? "ems " : "",
  367. cap & (1 << 5) ? "sxs " : "",
  368. cap2 & (1 << 2) ? "apst " : "",
  369. cap2 & (1 << 1) ? "nvmp " : "",
  370. cap2 & (1 << 0) ? "boh " : "");
  371. }
  372. #if defined(CONFIG_DM_SCSI) || !defined(CONFIG_SCSI_AHCI_PLAT)
  373. # if defined(CONFIG_DM_PCI) || defined(CONFIG_DM_SCSI)
  374. static int ahci_init_one(struct ahci_uc_priv *uc_priv, struct udevice *dev)
  375. # else
  376. static int ahci_init_one(struct ahci_uc_priv *uc_priv, pci_dev_t dev)
  377. # endif
  378. {
  379. #if !defined(CONFIG_DM_SCSI)
  380. u16 vendor;
  381. #endif
  382. int rc;
  383. uc_priv->dev = dev;
  384. uc_priv->host_flags = ATA_FLAG_SATA
  385. | ATA_FLAG_NO_LEGACY
  386. | ATA_FLAG_MMIO
  387. | ATA_FLAG_PIO_DMA
  388. | ATA_FLAG_NO_ATAPI;
  389. uc_priv->pio_mask = 0x1f;
  390. uc_priv->udma_mask = 0x7f; /*Fixme,assume to support UDMA6 */
  391. #if !defined(CONFIG_DM_SCSI)
  392. #ifdef CONFIG_DM_PCI
  393. uc_priv->mmio_base = dm_pci_map_bar(dev, PCI_BASE_ADDRESS_5,
  394. PCI_REGION_MEM);
  395. /* Take from kernel:
  396. * JMicron-specific fixup:
  397. * make sure we're in AHCI mode
  398. */
  399. dm_pci_read_config16(dev, PCI_VENDOR_ID, &vendor);
  400. if (vendor == 0x197b)
  401. dm_pci_write_config8(dev, 0x41, 0xa1);
  402. #else
  403. uc_priv->mmio_base = pci_map_bar(dev, PCI_BASE_ADDRESS_5,
  404. PCI_REGION_MEM);
  405. /* Take from kernel:
  406. * JMicron-specific fixup:
  407. * make sure we're in AHCI mode
  408. */
  409. pci_read_config_word(dev, PCI_VENDOR_ID, &vendor);
  410. if (vendor == 0x197b)
  411. pci_write_config_byte(dev, 0x41, 0xa1);
  412. #endif
  413. #else
  414. struct scsi_platdata *plat = dev_get_uclass_platdata(dev);
  415. uc_priv->mmio_base = (void *)plat->base;
  416. #endif
  417. debug("ahci mmio_base=0x%p\n", uc_priv->mmio_base);
  418. /* initialize adapter */
  419. rc = ahci_host_init(uc_priv);
  420. if (rc)
  421. goto err_out;
  422. ahci_print_info(uc_priv);
  423. return 0;
  424. err_out:
  425. return rc;
  426. }
  427. #endif
  428. #define MAX_DATA_BYTE_COUNT (4*1024*1024)
  429. static int ahci_fill_sg(struct ahci_uc_priv *uc_priv, u8 port,
  430. unsigned char *buf, int buf_len)
  431. {
  432. struct ahci_ioports *pp = &(uc_priv->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(struct ahci_uc_priv *uc_priv, u8 port)
  477. {
  478. struct ahci_ioports *pp = &(uc_priv->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(struct ahci_uc_priv *uc_priv, u8 port, u8 *fis,
  537. int fis_len, u8 *buf, int buf_len, u8 is_write)
  538. {
  539. struct ahci_ioports *pp = &(uc_priv->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 > uc_priv->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(uc_priv, 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(struct ahci_uc_priv *uc_priv,
  582. struct scsi_cmd *pccb)
  583. {
  584. static const u8 hdr[] = {
  585. 0,
  586. 0,
  587. 0x5, /* claim SPC-3 version compatibility */
  588. 2,
  589. 95 - 4,
  590. };
  591. u8 fis[20];
  592. u16 *idbuf;
  593. ALLOC_CACHE_ALIGN_BUFFER(u16, tmpid, ATA_ID_WORDS);
  594. u8 port;
  595. /* Clean ccb data buffer */
  596. memset(pccb->pdata, 0, pccb->datalen);
  597. memcpy(pccb->pdata, hdr, sizeof(hdr));
  598. if (pccb->datalen <= 35)
  599. return 0;
  600. memset(fis, 0, sizeof(fis));
  601. /* Construct the FIS */
  602. fis[0] = 0x27; /* Host to device FIS. */
  603. fis[1] = 1 << 7; /* Command FIS. */
  604. fis[2] = ATA_CMD_ID_ATA; /* Command byte. */
  605. /* Read id from sata */
  606. port = pccb->target;
  607. if (ahci_device_data_io(uc_priv, port, (u8 *)&fis, sizeof(fis),
  608. (u8 *)tmpid, ATA_ID_WORDS * 2, 0)) {
  609. debug("scsi_ahci: SCSI inquiry command failure.\n");
  610. return -EIO;
  611. }
  612. if (!uc_priv->ataid[port]) {
  613. uc_priv->ataid[port] = malloc(ATA_ID_WORDS * 2);
  614. if (!uc_priv->ataid[port]) {
  615. printf("%s: No memory for ataid[port]\n", __func__);
  616. return -ENOMEM;
  617. }
  618. }
  619. idbuf = uc_priv->ataid[port];
  620. memcpy(idbuf, tmpid, ATA_ID_WORDS * 2);
  621. ata_swap_buf_le16(idbuf, ATA_ID_WORDS);
  622. memcpy(&pccb->pdata[8], "ATA ", 8);
  623. ata_id_strcpy((u16 *)&pccb->pdata[16], &idbuf[ATA_ID_PROD], 16);
  624. ata_id_strcpy((u16 *)&pccb->pdata[32], &idbuf[ATA_ID_FW_REV], 4);
  625. #ifdef DEBUG
  626. ata_dump_id(idbuf);
  627. #endif
  628. return 0;
  629. }
  630. /*
  631. * SCSI READ10/WRITE10 command operation.
  632. */
  633. static int ata_scsiop_read_write(struct ahci_uc_priv *uc_priv,
  634. struct scsi_cmd *pccb, u8 is_write)
  635. {
  636. lbaint_t lba = 0;
  637. u16 blocks = 0;
  638. u8 fis[20];
  639. u8 *user_buffer = pccb->pdata;
  640. u32 user_buffer_size = pccb->datalen;
  641. /* Retrieve the base LBA number from the ccb structure. */
  642. if (pccb->cmd[0] == SCSI_READ16) {
  643. memcpy(&lba, pccb->cmd + 2, 8);
  644. lba = be64_to_cpu(lba);
  645. } else {
  646. u32 temp;
  647. memcpy(&temp, pccb->cmd + 2, 4);
  648. lba = be32_to_cpu(temp);
  649. }
  650. /*
  651. * Retrieve the base LBA number and the block count from
  652. * the ccb structure.
  653. *
  654. * For 10-byte and 16-byte SCSI R/W commands, transfer
  655. * length 0 means transfer 0 block of data.
  656. * However, for ATA R/W commands, sector count 0 means
  657. * 256 or 65536 sectors, not 0 sectors as in SCSI.
  658. *
  659. * WARNING: one or two older ATA drives treat 0 as 0...
  660. */
  661. if (pccb->cmd[0] == SCSI_READ16)
  662. blocks = (((u16)pccb->cmd[13]) << 8) | ((u16) pccb->cmd[14]);
  663. else
  664. blocks = (((u16)pccb->cmd[7]) << 8) | ((u16) pccb->cmd[8]);
  665. debug("scsi_ahci: %s %u blocks starting from lba 0x" LBAFU "\n",
  666. is_write ? "write" : "read", blocks, lba);
  667. /* Preset the FIS */
  668. memset(fis, 0, sizeof(fis));
  669. fis[0] = 0x27; /* Host to device FIS. */
  670. fis[1] = 1 << 7; /* Command FIS. */
  671. /* Command byte (read/write). */
  672. fis[2] = is_write ? ATA_CMD_WRITE_EXT : ATA_CMD_READ_EXT;
  673. while (blocks) {
  674. u16 now_blocks; /* number of blocks per iteration */
  675. u32 transfer_size; /* number of bytes per iteration */
  676. now_blocks = min((u16)MAX_SATA_BLOCKS_READ_WRITE, blocks);
  677. transfer_size = ATA_SECT_SIZE * now_blocks;
  678. if (transfer_size > user_buffer_size) {
  679. printf("scsi_ahci: Error: buffer too small.\n");
  680. return -EIO;
  681. }
  682. /*
  683. * LBA48 SATA command but only use 32bit address range within
  684. * that (unless we've enabled 64bit LBA support). The next
  685. * smaller command range (28bit) is too small.
  686. */
  687. fis[4] = (lba >> 0) & 0xff;
  688. fis[5] = (lba >> 8) & 0xff;
  689. fis[6] = (lba >> 16) & 0xff;
  690. fis[7] = 1 << 6; /* device reg: set LBA mode */
  691. fis[8] = ((lba >> 24) & 0xff);
  692. #ifdef CONFIG_SYS_64BIT_LBA
  693. if (pccb->cmd[0] == SCSI_READ16) {
  694. fis[9] = ((lba >> 32) & 0xff);
  695. fis[10] = ((lba >> 40) & 0xff);
  696. }
  697. #endif
  698. fis[3] = 0xe0; /* features */
  699. /* Block (sector) count */
  700. fis[12] = (now_blocks >> 0) & 0xff;
  701. fis[13] = (now_blocks >> 8) & 0xff;
  702. /* Read/Write from ahci */
  703. if (ahci_device_data_io(uc_priv, pccb->target, (u8 *)&fis,
  704. sizeof(fis), user_buffer, transfer_size,
  705. is_write)) {
  706. debug("scsi_ahci: SCSI %s10 command failure.\n",
  707. is_write ? "WRITE" : "READ");
  708. return -EIO;
  709. }
  710. /* If this transaction is a write, do a following flush.
  711. * Writes in u-boot are so rare, and the logic to know when is
  712. * the last write and do a flush only there is sufficiently
  713. * difficult. Just do a flush after every write. This incurs,
  714. * usually, one extra flush when the rare writes do happen.
  715. */
  716. if (is_write) {
  717. if (-EIO == ata_io_flush(uc_priv, pccb->target))
  718. return -EIO;
  719. }
  720. user_buffer += transfer_size;
  721. user_buffer_size -= transfer_size;
  722. blocks -= now_blocks;
  723. lba += now_blocks;
  724. }
  725. return 0;
  726. }
  727. /*
  728. * SCSI READ CAPACITY10 command operation.
  729. */
  730. static int ata_scsiop_read_capacity10(struct ahci_uc_priv *uc_priv,
  731. struct scsi_cmd *pccb)
  732. {
  733. u32 cap;
  734. u64 cap64;
  735. u32 block_size;
  736. if (!uc_priv->ataid[pccb->target]) {
  737. printf("scsi_ahci: SCSI READ CAPACITY10 command failure. "
  738. "\tNo ATA info!\n"
  739. "\tPlease run SCSI command INQUIRY first!\n");
  740. return -EPERM;
  741. }
  742. cap64 = ata_id_n_sectors(uc_priv->ataid[pccb->target]);
  743. if (cap64 > 0x100000000ULL)
  744. cap64 = 0xffffffff;
  745. cap = cpu_to_be32(cap64);
  746. memcpy(pccb->pdata, &cap, sizeof(cap));
  747. block_size = cpu_to_be32((u32)512);
  748. memcpy(&pccb->pdata[4], &block_size, 4);
  749. return 0;
  750. }
  751. /*
  752. * SCSI READ CAPACITY16 command operation.
  753. */
  754. static int ata_scsiop_read_capacity16(struct ahci_uc_priv *uc_priv,
  755. struct scsi_cmd *pccb)
  756. {
  757. u64 cap;
  758. u64 block_size;
  759. if (!uc_priv->ataid[pccb->target]) {
  760. printf("scsi_ahci: SCSI READ CAPACITY16 command failure. "
  761. "\tNo ATA info!\n"
  762. "\tPlease run SCSI command INQUIRY first!\n");
  763. return -EPERM;
  764. }
  765. cap = ata_id_n_sectors(uc_priv->ataid[pccb->target]);
  766. cap = cpu_to_be64(cap);
  767. memcpy(pccb->pdata, &cap, sizeof(cap));
  768. block_size = cpu_to_be64((u64)512);
  769. memcpy(&pccb->pdata[8], &block_size, 8);
  770. return 0;
  771. }
  772. /*
  773. * SCSI TEST UNIT READY command operation.
  774. */
  775. static int ata_scsiop_test_unit_ready(struct ahci_uc_priv *uc_priv,
  776. struct scsi_cmd *pccb)
  777. {
  778. return (uc_priv->ataid[pccb->target]) ? 0 : -EPERM;
  779. }
  780. static int ahci_scsi_exec(struct udevice *dev, struct scsi_cmd *pccb)
  781. {
  782. struct ahci_uc_priv *uc_priv;
  783. #ifdef CONFIG_DM_SCSI
  784. uc_priv = dev_get_uclass_priv(dev->parent);
  785. #else
  786. uc_priv = probe_ent;
  787. #endif
  788. int ret;
  789. switch (pccb->cmd[0]) {
  790. case SCSI_READ16:
  791. case SCSI_READ10:
  792. ret = ata_scsiop_read_write(uc_priv, pccb, 0);
  793. break;
  794. case SCSI_WRITE10:
  795. ret = ata_scsiop_read_write(uc_priv, pccb, 1);
  796. break;
  797. case SCSI_RD_CAPAC10:
  798. ret = ata_scsiop_read_capacity10(uc_priv, pccb);
  799. break;
  800. case SCSI_RD_CAPAC16:
  801. ret = ata_scsiop_read_capacity16(uc_priv, pccb);
  802. break;
  803. case SCSI_TST_U_RDY:
  804. ret = ata_scsiop_test_unit_ready(uc_priv, pccb);
  805. break;
  806. case SCSI_INQUIRY:
  807. ret = ata_scsiop_inquiry(uc_priv, pccb);
  808. break;
  809. default:
  810. printf("Unsupport SCSI command 0x%02x\n", pccb->cmd[0]);
  811. return -ENOTSUPP;
  812. }
  813. if (ret) {
  814. debug("SCSI command 0x%02x ret errno %d\n", pccb->cmd[0], ret);
  815. return ret;
  816. }
  817. return 0;
  818. }
  819. static int ahci_start_ports(struct ahci_uc_priv *uc_priv)
  820. {
  821. u32 linkmap;
  822. int i;
  823. linkmap = uc_priv->link_port_map;
  824. for (i = 0; i < CONFIG_SYS_SCSI_MAX_SCSI_ID; i++) {
  825. if (((linkmap >> i) & 0x01)) {
  826. if (ahci_port_start(uc_priv, (u8) i)) {
  827. printf("Can not start port %d\n", i);
  828. continue;
  829. }
  830. }
  831. }
  832. return 0;
  833. }
  834. #ifndef CONFIG_DM_SCSI
  835. void scsi_low_level_init(int busdevfunc)
  836. {
  837. struct ahci_uc_priv *uc_priv;
  838. #ifndef CONFIG_SCSI_AHCI_PLAT
  839. probe_ent = calloc(1, sizeof(struct ahci_uc_priv));
  840. if (!probe_ent) {
  841. printf("%s: No memory for uc_priv\n", __func__);
  842. return;
  843. }
  844. uc_priv = probe_ent;
  845. # if defined(CONFIG_DM_PCI)
  846. struct udevice *dev;
  847. int ret;
  848. ret = dm_pci_bus_find_bdf(busdevfunc, &dev);
  849. if (ret)
  850. return;
  851. ahci_init_one(uc_priv, dev);
  852. # else
  853. ahci_init_one(uc_priv, busdevfunc);
  854. # endif
  855. #else
  856. uc_priv = probe_ent;
  857. #endif
  858. ahci_start_ports(uc_priv);
  859. }
  860. #endif
  861. #ifndef CONFIG_SCSI_AHCI_PLAT
  862. # if defined(CONFIG_DM_PCI) || defined(CONFIG_DM_SCSI)
  863. int ahci_init_one_dm(struct udevice *dev)
  864. {
  865. struct ahci_uc_priv *uc_priv = dev_get_uclass_priv(dev);
  866. return ahci_init_one(uc_priv, dev);
  867. }
  868. #endif
  869. #endif
  870. int ahci_start_ports_dm(struct udevice *dev)
  871. {
  872. struct ahci_uc_priv *uc_priv = dev_get_uclass_priv(dev);
  873. return ahci_start_ports(uc_priv);
  874. }
  875. #ifdef CONFIG_SCSI_AHCI_PLAT
  876. static int ahci_init_common(struct ahci_uc_priv *uc_priv, void __iomem *base)
  877. {
  878. int rc;
  879. uc_priv->host_flags = ATA_FLAG_SATA
  880. | ATA_FLAG_NO_LEGACY
  881. | ATA_FLAG_MMIO
  882. | ATA_FLAG_PIO_DMA
  883. | ATA_FLAG_NO_ATAPI;
  884. uc_priv->pio_mask = 0x1f;
  885. uc_priv->udma_mask = 0x7f; /*Fixme,assume to support UDMA6 */
  886. uc_priv->mmio_base = base;
  887. /* initialize adapter */
  888. rc = ahci_host_init(uc_priv);
  889. if (rc)
  890. goto err_out;
  891. ahci_print_info(uc_priv);
  892. rc = ahci_start_ports(uc_priv);
  893. err_out:
  894. return rc;
  895. }
  896. #ifndef CONFIG_DM_SCSI
  897. int ahci_init(void __iomem *base)
  898. {
  899. struct ahci_uc_priv *uc_priv;
  900. probe_ent = malloc(sizeof(struct ahci_uc_priv));
  901. if (!probe_ent) {
  902. printf("%s: No memory for uc_priv\n", __func__);
  903. return -ENOMEM;
  904. }
  905. uc_priv = probe_ent;
  906. memset(uc_priv, 0, sizeof(struct ahci_uc_priv));
  907. return ahci_init_common(uc_priv, base);
  908. }
  909. #endif
  910. int ahci_init_dm(struct udevice *dev, void __iomem *base)
  911. {
  912. struct ahci_uc_priv *uc_priv = dev_get_uclass_priv(dev);
  913. return ahci_init_common(uc_priv, base);
  914. }
  915. void __weak scsi_init(void)
  916. {
  917. }
  918. #endif /* CONFIG_SCSI_AHCI_PLAT */
  919. /*
  920. * In the general case of generic rotating media it makes sense to have a
  921. * flush capability. It probably even makes sense in the case of SSDs because
  922. * one cannot always know for sure what kind of internal cache/flush mechanism
  923. * is embodied therein. At first it was planned to invoke this after the last
  924. * write to disk and before rebooting. In practice, knowing, a priori, which
  925. * is the last write is difficult. Because writing to the disk in u-boot is
  926. * very rare, this flush command will be invoked after every block write.
  927. */
  928. static int ata_io_flush(struct ahci_uc_priv *uc_priv, u8 port)
  929. {
  930. u8 fis[20];
  931. struct ahci_ioports *pp = &(uc_priv->port[port]);
  932. void __iomem *port_mmio = pp->port_mmio;
  933. u32 cmd_fis_len = 5; /* five dwords */
  934. /* Preset the FIS */
  935. memset(fis, 0, 20);
  936. fis[0] = 0x27; /* Host to device FIS. */
  937. fis[1] = 1 << 7; /* Command FIS. */
  938. fis[2] = ATA_CMD_FLUSH_EXT;
  939. memcpy((unsigned char *)pp->cmd_tbl, fis, 20);
  940. ahci_fill_cmd_slot(pp, cmd_fis_len);
  941. ahci_dcache_flush_sata_cmd(pp);
  942. writel_with_flush(1, port_mmio + PORT_CMD_ISSUE);
  943. if (waiting_for_cmd_completed(port_mmio + PORT_CMD_ISSUE,
  944. WAIT_MS_FLUSH, 0x1)) {
  945. debug("scsi_ahci: flush command timeout on port %d.\n", port);
  946. return -EIO;
  947. }
  948. return 0;
  949. }
  950. static int ahci_scsi_bus_reset(struct udevice *dev)
  951. {
  952. /* Not implemented */
  953. return 0;
  954. }
  955. #ifdef CONFIG_DM_SCSI
  956. int ahci_bind_scsi(struct udevice *ahci_dev, struct udevice **devp)
  957. {
  958. struct udevice *dev;
  959. int ret;
  960. ret = device_bind_driver(ahci_dev, "ahci_scsi", "ahci_scsi", &dev);
  961. if (ret)
  962. return ret;
  963. *devp = dev;
  964. return 0;
  965. }
  966. int ahci_probe_scsi(struct udevice *ahci_dev, ulong base)
  967. {
  968. struct ahci_uc_priv *uc_priv;
  969. struct scsi_platdata *uc_plat;
  970. struct udevice *dev;
  971. int ret;
  972. device_find_first_child(ahci_dev, &dev);
  973. if (!dev)
  974. return -ENODEV;
  975. uc_plat = dev_get_uclass_platdata(dev);
  976. uc_plat->base = base;
  977. uc_plat->max_lun = 1;
  978. uc_plat->max_id = 2;
  979. uc_priv = dev_get_uclass_priv(ahci_dev);
  980. ret = ahci_init_one(uc_priv, dev);
  981. if (ret)
  982. return ret;
  983. ret = ahci_start_ports(uc_priv);
  984. if (ret)
  985. return ret;
  986. return 0;
  987. }
  988. #ifdef CONFIG_DM_PCI
  989. int ahci_probe_scsi_pci(struct udevice *ahci_dev)
  990. {
  991. ulong base;
  992. base = (ulong)dm_pci_map_bar(ahci_dev, PCI_BASE_ADDRESS_5,
  993. PCI_REGION_MEM);
  994. return ahci_probe_scsi(ahci_dev, base);
  995. }
  996. #endif
  997. struct scsi_ops scsi_ops = {
  998. .exec = ahci_scsi_exec,
  999. .bus_reset = ahci_scsi_bus_reset,
  1000. };
  1001. U_BOOT_DRIVER(ahci_scsi) = {
  1002. .name = "ahci_scsi",
  1003. .id = UCLASS_SCSI,
  1004. .ops = &scsi_ops,
  1005. };
  1006. #else
  1007. int scsi_exec(struct udevice *dev, struct scsi_cmd *pccb)
  1008. {
  1009. return ahci_scsi_exec(dev, pccb);
  1010. }
  1011. __weak int scsi_bus_reset(struct udevice *dev)
  1012. {
  1013. return ahci_scsi_bus_reset(dev);
  1014. return 0;
  1015. }
  1016. #endif