sata_mv.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051
  1. /*
  2. * Copyright (C) Excito Elektronik i Skåne AB, 2010.
  3. * Author: Tor Krill <tor@excito.com>
  4. *
  5. * Copyright (C) 2015 Stefan Roese <sr@denx.de>
  6. *
  7. * SPDX-License-Identifier: GPL-2.0+
  8. */
  9. /*
  10. * This driver supports the SATA controller of some Mavell SoC's.
  11. * Here a (most likely incomplete) list of the supported SoC's:
  12. * - Kirkwood
  13. * - Armada 370
  14. * - Armada XP
  15. *
  16. * This driver implementation is an alternative to the already available
  17. * driver via the "ide" commands interface (drivers/block/mvsata_ide.c).
  18. * But this driver only supports PIO mode and as this new driver also
  19. * supports transfer via DMA, its much faster.
  20. *
  21. * Please note, that the newer SoC's (e.g. Armada 38x) are not supported
  22. * by this driver. As they have an AHCI compatible SATA controller
  23. * integrated.
  24. */
  25. /*
  26. * TODO:
  27. * Better error recovery
  28. * No support for using PRDs (Thus max 64KB transfers)
  29. * No NCQ support
  30. * No port multiplier support
  31. */
  32. #include <common.h>
  33. #include <fis.h>
  34. #include <libata.h>
  35. #include <malloc.h>
  36. #include <sata.h>
  37. #include <linux/errno.h>
  38. #include <asm/io.h>
  39. #include <linux/mbus.h>
  40. #if defined(CONFIG_KIRKWOOD)
  41. #include <asm/arch/kirkwood.h>
  42. #define SATAHC_BASE KW_SATA_BASE
  43. #else
  44. #include <asm/arch/soc.h>
  45. #define SATAHC_BASE MVEBU_AXP_SATA_BASE
  46. #endif
  47. #define SATA0_BASE (SATAHC_BASE + 0x2000)
  48. #define SATA1_BASE (SATAHC_BASE + 0x4000)
  49. /* EDMA registers */
  50. #define EDMA_CFG 0x000
  51. #define EDMA_CFG_NCQ (1 << 5)
  52. #define EDMA_CFG_EQUE (1 << 9)
  53. #define EDMA_TIMER 0x004
  54. #define EDMA_IECR 0x008
  55. #define EDMA_IEMR 0x00c
  56. #define EDMA_RQBA_HI 0x010
  57. #define EDMA_RQIPR 0x014
  58. #define EDMA_RQIPR_IPMASK (0x1f << 5)
  59. #define EDMA_RQIPR_IPSHIFT 5
  60. #define EDMA_RQOPR 0x018
  61. #define EDMA_RQOPR_OPMASK (0x1f << 5)
  62. #define EDMA_RQOPR_OPSHIFT 5
  63. #define EDMA_RSBA_HI 0x01c
  64. #define EDMA_RSIPR 0x020
  65. #define EDMA_RSIPR_IPMASK (0x1f << 3)
  66. #define EDMA_RSIPR_IPSHIFT 3
  67. #define EDMA_RSOPR 0x024
  68. #define EDMA_RSOPR_OPMASK (0x1f << 3)
  69. #define EDMA_RSOPR_OPSHIFT 3
  70. #define EDMA_CMD 0x028
  71. #define EDMA_CMD_ENEDMA (0x01 << 0)
  72. #define EDMA_CMD_DISEDMA (0x01 << 1)
  73. #define EDMA_CMD_ATARST (0x01 << 2)
  74. #define EDMA_CMD_FREEZE (0x01 << 4)
  75. #define EDMA_TEST_CTL 0x02c
  76. #define EDMA_STATUS 0x030
  77. #define EDMA_IORTO 0x034
  78. #define EDMA_CDTR 0x040
  79. #define EDMA_HLTCND 0x060
  80. #define EDMA_NTSR 0x094
  81. /* Basic DMA registers */
  82. #define BDMA_CMD 0x224
  83. #define BDMA_STATUS 0x228
  84. #define BDMA_DTLB 0x22c
  85. #define BDMA_DTHB 0x230
  86. #define BDMA_DRL 0x234
  87. #define BDMA_DRH 0x238
  88. /* SATA Interface registers */
  89. #define SIR_ICFG 0x050
  90. #define SIR_CFG_GEN2EN (0x1 << 7)
  91. #define SIR_PLL_CFG 0x054
  92. #define SIR_SSTATUS 0x300
  93. #define SSTATUS_DET_MASK (0x0f << 0)
  94. #define SIR_SERROR 0x304
  95. #define SIR_SCONTROL 0x308
  96. #define SIR_SCONTROL_DETEN (0x01 << 0)
  97. #define SIR_LTMODE 0x30c
  98. #define SIR_LTMODE_NELBE (0x01 << 7)
  99. #define SIR_PHYMODE3 0x310
  100. #define SIR_PHYMODE4 0x314
  101. #define SIR_PHYMODE1 0x32c
  102. #define SIR_PHYMODE2 0x330
  103. #define SIR_BIST_CTRL 0x334
  104. #define SIR_BIST_DW1 0x338
  105. #define SIR_BIST_DW2 0x33c
  106. #define SIR_SERR_IRQ_MASK 0x340
  107. #define SIR_SATA_IFCTRL 0x344
  108. #define SIR_SATA_TESTCTRL 0x348
  109. #define SIR_SATA_IFSTATUS 0x34c
  110. #define SIR_VEND_UNIQ 0x35c
  111. #define SIR_FIS_CFG 0x360
  112. #define SIR_FIS_IRQ_CAUSE 0x364
  113. #define SIR_FIS_IRQ_MASK 0x368
  114. #define SIR_FIS_DWORD0 0x370
  115. #define SIR_FIS_DWORD1 0x374
  116. #define SIR_FIS_DWORD2 0x378
  117. #define SIR_FIS_DWORD3 0x37c
  118. #define SIR_FIS_DWORD4 0x380
  119. #define SIR_FIS_DWORD5 0x384
  120. #define SIR_FIS_DWORD6 0x388
  121. #define SIR_PHYM9_GEN2 0x398
  122. #define SIR_PHYM9_GEN1 0x39c
  123. #define SIR_PHY_CFG 0x3a0
  124. #define SIR_PHYCTL 0x3a4
  125. #define SIR_PHYM10 0x3a8
  126. #define SIR_PHYM12 0x3b0
  127. /* Shadow registers */
  128. #define PIO_DATA 0x100
  129. #define PIO_ERR_FEATURES 0x104
  130. #define PIO_SECTOR_COUNT 0x108
  131. #define PIO_LBA_LOW 0x10c
  132. #define PIO_LBA_MID 0x110
  133. #define PIO_LBA_HI 0x114
  134. #define PIO_DEVICE 0x118
  135. #define PIO_CMD_STATUS 0x11c
  136. #define PIO_STATUS_ERR (0x01 << 0)
  137. #define PIO_STATUS_DRQ (0x01 << 3)
  138. #define PIO_STATUS_DF (0x01 << 5)
  139. #define PIO_STATUS_DRDY (0x01 << 6)
  140. #define PIO_STATUS_BSY (0x01 << 7)
  141. #define PIO_CTRL_ALTSTAT 0x120
  142. /* SATAHC arbiter registers */
  143. #define SATAHC_CFG 0x000
  144. #define SATAHC_RQOP 0x004
  145. #define SATAHC_RQIP 0x008
  146. #define SATAHC_ICT 0x00c
  147. #define SATAHC_ITT 0x010
  148. #define SATAHC_ICR 0x014
  149. #define SATAHC_ICR_PORT0 (0x01 << 0)
  150. #define SATAHC_ICR_PORT1 (0x01 << 1)
  151. #define SATAHC_MIC 0x020
  152. #define SATAHC_MIM 0x024
  153. #define SATAHC_LED_CFG 0x02c
  154. #define REQUEST_QUEUE_SIZE 32
  155. #define RESPONSE_QUEUE_SIZE REQUEST_QUEUE_SIZE
  156. struct crqb {
  157. u32 dtb_low; /* DW0 */
  158. u32 dtb_high; /* DW1 */
  159. u32 control_flags; /* DW2 */
  160. u32 drb_count; /* DW3 */
  161. u32 ata_cmd_feat; /* DW4 */
  162. u32 ata_addr; /* DW5 */
  163. u32 ata_addr_exp; /* DW6 */
  164. u32 ata_sect_count; /* DW7 */
  165. };
  166. #define CRQB_ALIGN 0x400
  167. #define CRQB_CNTRLFLAGS_DIR (0x01 << 0)
  168. #define CRQB_CNTRLFLAGS_DQTAGMASK (0x1f << 1)
  169. #define CRQB_CNTRLFLAGS_DQTAGSHIFT 1
  170. #define CRQB_CNTRLFLAGS_PMPORTMASK (0x0f << 12)
  171. #define CRQB_CNTRLFLAGS_PMPORTSHIFT 12
  172. #define CRQB_CNTRLFLAGS_PRDMODE (0x01 << 16)
  173. #define CRQB_CNTRLFLAGS_HQTAGMASK (0x1f << 17)
  174. #define CRQB_CNTRLFLAGS_HQTAGSHIFT 17
  175. #define CRQB_CMDFEAT_CMDMASK (0xff << 16)
  176. #define CRQB_CMDFEAT_CMDSHIFT 16
  177. #define CRQB_CMDFEAT_FEATMASK (0xff << 16)
  178. #define CRQB_CMDFEAT_FEATSHIFT 24
  179. #define CRQB_ADDR_LBA_LOWMASK (0xff << 0)
  180. #define CRQB_ADDR_LBA_LOWSHIFT 0
  181. #define CRQB_ADDR_LBA_MIDMASK (0xff << 8)
  182. #define CRQB_ADDR_LBA_MIDSHIFT 8
  183. #define CRQB_ADDR_LBA_HIGHMASK (0xff << 16)
  184. #define CRQB_ADDR_LBA_HIGHSHIFT 16
  185. #define CRQB_ADDR_DEVICE_MASK (0xff << 24)
  186. #define CRQB_ADDR_DEVICE_SHIFT 24
  187. #define CRQB_ADDR_LBA_LOW_EXP_MASK (0xff << 0)
  188. #define CRQB_ADDR_LBA_LOW_EXP_SHIFT 0
  189. #define CRQB_ADDR_LBA_MID_EXP_MASK (0xff << 8)
  190. #define CRQB_ADDR_LBA_MID_EXP_SHIFT 8
  191. #define CRQB_ADDR_LBA_HIGH_EXP_MASK (0xff << 16)
  192. #define CRQB_ADDR_LBA_HIGH_EXP_SHIFT 16
  193. #define CRQB_ADDR_FEATURE_EXP_MASK (0xff << 24)
  194. #define CRQB_ADDR_FEATURE_EXP_SHIFT 24
  195. #define CRQB_SECTCOUNT_COUNT_MASK (0xff << 0)
  196. #define CRQB_SECTCOUNT_COUNT_SHIFT 0
  197. #define CRQB_SECTCOUNT_COUNT_EXP_MASK (0xff << 8)
  198. #define CRQB_SECTCOUNT_COUNT_EXP_SHIFT 8
  199. #define MVSATA_WIN_CONTROL(w) (MVEBU_AXP_SATA_BASE + 0x30 + ((w) << 4))
  200. #define MVSATA_WIN_BASE(w) (MVEBU_AXP_SATA_BASE + 0x34 + ((w) << 4))
  201. struct eprd {
  202. u32 phyaddr_low;
  203. u32 bytecount_eot;
  204. u32 phyaddr_hi;
  205. u32 reserved;
  206. };
  207. #define EPRD_PHYADDR_MASK 0xfffffffe
  208. #define EPRD_BYTECOUNT_MASK 0x0000ffff
  209. #define EPRD_EOT (0x01 << 31)
  210. struct crpb {
  211. u32 id;
  212. u32 flags;
  213. u32 timestamp;
  214. };
  215. #define CRPB_ALIGN 0x100
  216. #define READ_CMD 0
  217. #define WRITE_CMD 1
  218. /*
  219. * Since we don't use PRDs yet max transfer size
  220. * is 64KB
  221. */
  222. #define MV_ATA_MAX_SECTORS (65535 / ATA_SECT_SIZE)
  223. /* Keep track if hw is initialized or not */
  224. static u32 hw_init;
  225. struct mv_priv {
  226. char name[12];
  227. u32 link;
  228. u32 regbase;
  229. u32 queue_depth;
  230. u16 pio;
  231. u16 mwdma;
  232. u16 udma;
  233. void *crqb_alloc;
  234. struct crqb *request;
  235. void *crpb_alloc;
  236. struct crpb *response;
  237. };
  238. static int ata_wait_register(u32 *addr, u32 mask, u32 val, u32 timeout_msec)
  239. {
  240. ulong start;
  241. start = get_timer(0);
  242. do {
  243. if ((in_le32(addr) & mask) == val)
  244. return 0;
  245. } while (get_timer(start) < timeout_msec);
  246. return -ETIMEDOUT;
  247. }
  248. /* Cut from sata_mv in linux kernel */
  249. static int mv_stop_edma_engine(int port)
  250. {
  251. struct mv_priv *priv = (struct mv_priv *)sata_dev_desc[port].priv;
  252. int i;
  253. /* Disable eDMA. The disable bit auto clears. */
  254. out_le32(priv->regbase + EDMA_CMD, EDMA_CMD_DISEDMA);
  255. /* Wait for the chip to confirm eDMA is off. */
  256. for (i = 10000; i > 0; i--) {
  257. u32 reg = in_le32(priv->regbase + EDMA_CMD);
  258. if (!(reg & EDMA_CMD_ENEDMA)) {
  259. debug("EDMA stop on port %d succesful\n", port);
  260. return 0;
  261. }
  262. udelay(10);
  263. }
  264. debug("EDMA stop on port %d failed\n", port);
  265. return -1;
  266. }
  267. static int mv_start_edma_engine(int port)
  268. {
  269. struct mv_priv *priv = (struct mv_priv *)sata_dev_desc[port].priv;
  270. u32 tmp;
  271. /* Check preconditions */
  272. tmp = in_le32(priv->regbase + SIR_SSTATUS);
  273. if ((tmp & SSTATUS_DET_MASK) != 0x03) {
  274. printf("Device error on port: %d\n", port);
  275. return -1;
  276. }
  277. tmp = in_le32(priv->regbase + PIO_CMD_STATUS);
  278. if (tmp & (ATA_BUSY | ATA_DRQ)) {
  279. printf("Device not ready on port: %d\n", port);
  280. return -1;
  281. }
  282. /* Clear interrupt cause */
  283. out_le32(priv->regbase + EDMA_IECR, 0x0);
  284. tmp = in_le32(SATAHC_BASE + SATAHC_ICR);
  285. tmp &= ~(port == 0 ? SATAHC_ICR_PORT0 : SATAHC_ICR_PORT1);
  286. out_le32(SATAHC_BASE + SATAHC_ICR, tmp);
  287. /* Configure edma operation */
  288. tmp = in_le32(priv->regbase + EDMA_CFG);
  289. tmp &= ~EDMA_CFG_NCQ; /* No NCQ */
  290. tmp &= ~EDMA_CFG_EQUE; /* Dont queue operations */
  291. out_le32(priv->regbase + EDMA_CFG, tmp);
  292. out_le32(priv->regbase + SIR_FIS_IRQ_CAUSE, 0x0);
  293. /* Configure fis, set all to no-wait for now */
  294. out_le32(priv->regbase + SIR_FIS_CFG, 0x0);
  295. /* Setup request queue */
  296. out_le32(priv->regbase + EDMA_RQBA_HI, 0x0);
  297. out_le32(priv->regbase + EDMA_RQIPR, priv->request);
  298. out_le32(priv->regbase + EDMA_RQOPR, 0x0);
  299. /* Setup response queue */
  300. out_le32(priv->regbase + EDMA_RSBA_HI, 0x0);
  301. out_le32(priv->regbase + EDMA_RSOPR, priv->response);
  302. out_le32(priv->regbase + EDMA_RSIPR, 0x0);
  303. /* Start edma */
  304. out_le32(priv->regbase + EDMA_CMD, EDMA_CMD_ENEDMA);
  305. return 0;
  306. }
  307. static int mv_reset_channel(int port)
  308. {
  309. struct mv_priv *priv = (struct mv_priv *)sata_dev_desc[port].priv;
  310. /* Make sure edma is stopped */
  311. mv_stop_edma_engine(port);
  312. out_le32(priv->regbase + EDMA_CMD, EDMA_CMD_ATARST);
  313. udelay(25); /* allow reset propagation */
  314. out_le32(priv->regbase + EDMA_CMD, 0);
  315. mdelay(10);
  316. return 0;
  317. }
  318. static void mv_reset_port(int port)
  319. {
  320. struct mv_priv *priv = (struct mv_priv *)sata_dev_desc[port].priv;
  321. mv_reset_channel(port);
  322. out_le32(priv->regbase + EDMA_CMD, 0x0);
  323. out_le32(priv->regbase + EDMA_CFG, 0x101f);
  324. out_le32(priv->regbase + EDMA_IECR, 0x0);
  325. out_le32(priv->regbase + EDMA_IEMR, 0x0);
  326. out_le32(priv->regbase + EDMA_RQBA_HI, 0x0);
  327. out_le32(priv->regbase + EDMA_RQIPR, 0x0);
  328. out_le32(priv->regbase + EDMA_RQOPR, 0x0);
  329. out_le32(priv->regbase + EDMA_RSBA_HI, 0x0);
  330. out_le32(priv->regbase + EDMA_RSIPR, 0x0);
  331. out_le32(priv->regbase + EDMA_RSOPR, 0x0);
  332. out_le32(priv->regbase + EDMA_IORTO, 0xfa);
  333. }
  334. static void mv_reset_one_hc(void)
  335. {
  336. out_le32(SATAHC_BASE + SATAHC_ICT, 0x00);
  337. out_le32(SATAHC_BASE + SATAHC_ITT, 0x00);
  338. out_le32(SATAHC_BASE + SATAHC_ICR, 0x00);
  339. }
  340. static int probe_port(int port)
  341. {
  342. struct mv_priv *priv = (struct mv_priv *)sata_dev_desc[port].priv;
  343. int tries, tries2, set15 = 0;
  344. u32 tmp;
  345. debug("Probe port: %d\n", port);
  346. for (tries = 0; tries < 2; tries++) {
  347. /* Clear SError */
  348. out_le32(priv->regbase + SIR_SERROR, 0x0);
  349. /* trigger com-init */
  350. tmp = in_le32(priv->regbase + SIR_SCONTROL);
  351. tmp = (tmp & 0x0f0) | 0x300 | SIR_SCONTROL_DETEN;
  352. out_le32(priv->regbase + SIR_SCONTROL, tmp);
  353. mdelay(1);
  354. tmp = in_le32(priv->regbase + SIR_SCONTROL);
  355. tries2 = 5;
  356. do {
  357. tmp = (tmp & 0x0f0) | 0x300;
  358. out_le32(priv->regbase + SIR_SCONTROL, tmp);
  359. mdelay(10);
  360. tmp = in_le32(priv->regbase + SIR_SCONTROL);
  361. } while ((tmp & 0xf0f) != 0x300 && tries2--);
  362. mdelay(10);
  363. for (tries2 = 0; tries2 < 200; tries2++) {
  364. tmp = in_le32(priv->regbase + SIR_SSTATUS);
  365. if ((tmp & SSTATUS_DET_MASK) == 0x03) {
  366. debug("Found device on port\n");
  367. return 0;
  368. }
  369. mdelay(1);
  370. }
  371. if ((tmp & SSTATUS_DET_MASK) == 0) {
  372. debug("No device attached on port %d\n", port);
  373. return -ENODEV;
  374. }
  375. if (!set15) {
  376. /* Try on 1.5Gb/S */
  377. debug("Try 1.5Gb link\n");
  378. set15 = 1;
  379. out_le32(priv->regbase + SIR_SCONTROL, 0x304);
  380. tmp = in_le32(priv->regbase + SIR_ICFG);
  381. tmp &= ~SIR_CFG_GEN2EN;
  382. out_le32(priv->regbase + SIR_ICFG, tmp);
  383. mv_reset_channel(port);
  384. }
  385. }
  386. debug("Failed to probe port\n");
  387. return -1;
  388. }
  389. /* Get request queue in pointer */
  390. static int get_reqip(int port)
  391. {
  392. struct mv_priv *priv = (struct mv_priv *)sata_dev_desc[port].priv;
  393. u32 tmp;
  394. tmp = in_le32(priv->regbase + EDMA_RQIPR) & EDMA_RQIPR_IPMASK;
  395. tmp = tmp >> EDMA_RQIPR_IPSHIFT;
  396. return tmp;
  397. }
  398. static void set_reqip(int port, int reqin)
  399. {
  400. struct mv_priv *priv = (struct mv_priv *)sata_dev_desc[port].priv;
  401. u32 tmp;
  402. tmp = in_le32(priv->regbase + EDMA_RQIPR) & ~EDMA_RQIPR_IPMASK;
  403. tmp |= ((reqin << EDMA_RQIPR_IPSHIFT) & EDMA_RQIPR_IPMASK);
  404. out_le32(priv->regbase + EDMA_RQIPR, tmp);
  405. }
  406. /* Get next available slot, ignoring possible overwrite */
  407. static int get_next_reqip(int port)
  408. {
  409. int slot = get_reqip(port);
  410. slot = (slot + 1) % REQUEST_QUEUE_SIZE;
  411. return slot;
  412. }
  413. /* Get response queue in pointer */
  414. static int get_rspip(int port)
  415. {
  416. struct mv_priv *priv = (struct mv_priv *)sata_dev_desc[port].priv;
  417. u32 tmp;
  418. tmp = in_le32(priv->regbase + EDMA_RSIPR) & EDMA_RSIPR_IPMASK;
  419. tmp = tmp >> EDMA_RSIPR_IPSHIFT;
  420. return tmp;
  421. }
  422. /* Get response queue out pointer */
  423. static int get_rspop(int port)
  424. {
  425. struct mv_priv *priv = (struct mv_priv *)sata_dev_desc[port].priv;
  426. u32 tmp;
  427. tmp = in_le32(priv->regbase + EDMA_RSOPR) & EDMA_RSOPR_OPMASK;
  428. tmp = tmp >> EDMA_RSOPR_OPSHIFT;
  429. return tmp;
  430. }
  431. /* Get next response queue pointer */
  432. static int get_next_rspop(int port)
  433. {
  434. return (get_rspop(port) + 1) % RESPONSE_QUEUE_SIZE;
  435. }
  436. /* Set response queue pointer */
  437. static void set_rspop(int port, int reqin)
  438. {
  439. struct mv_priv *priv = (struct mv_priv *)sata_dev_desc[port].priv;
  440. u32 tmp;
  441. tmp = in_le32(priv->regbase + EDMA_RSOPR) & ~EDMA_RSOPR_OPMASK;
  442. tmp |= ((reqin << EDMA_RSOPR_OPSHIFT) & EDMA_RSOPR_OPMASK);
  443. out_le32(priv->regbase + EDMA_RSOPR, tmp);
  444. }
  445. static int wait_dma_completion(int port, int index, u32 timeout_msec)
  446. {
  447. u32 tmp, res;
  448. tmp = port == 0 ? SATAHC_ICR_PORT0 : SATAHC_ICR_PORT1;
  449. res = ata_wait_register((u32 *)(SATAHC_BASE + SATAHC_ICR), tmp,
  450. tmp, timeout_msec);
  451. if (res)
  452. printf("Failed to wait for completion on port %d\n", port);
  453. return res;
  454. }
  455. static void process_responses(int port)
  456. {
  457. #ifdef DEBUG
  458. struct mv_priv *priv = (struct mv_priv *)sata_dev_desc[port].priv;
  459. #endif
  460. u32 tmp;
  461. u32 outind = get_rspop(port);
  462. /* Ack interrupts */
  463. tmp = in_le32(SATAHC_BASE + SATAHC_ICR);
  464. if (port == 0)
  465. tmp &= ~(BIT(0) | BIT(8));
  466. else
  467. tmp &= ~(BIT(1) | BIT(9));
  468. tmp &= ~(BIT(4));
  469. out_le32(SATAHC_BASE + SATAHC_ICR, tmp);
  470. while (get_rspip(port) != outind) {
  471. #ifdef DEBUG
  472. debug("Response index %d flags %08x on port %d\n", outind,
  473. priv->response[outind].flags, port);
  474. #endif
  475. outind = get_next_rspop(port);
  476. set_rspop(port, outind);
  477. }
  478. }
  479. static int mv_ata_exec_ata_cmd(int port, struct sata_fis_h2d *cfis,
  480. u8 *buffer, u32 len, u32 iswrite)
  481. {
  482. struct mv_priv *priv = (struct mv_priv *)sata_dev_desc[port].priv;
  483. struct crqb *req;
  484. int slot;
  485. u32 start;
  486. if (len >= 64 * 1024) {
  487. printf("We only support <64K transfers for now\n");
  488. return -1;
  489. }
  490. /* Initialize request */
  491. slot = get_reqip(port);
  492. memset(&priv->request[slot], 0, sizeof(struct crqb));
  493. req = &priv->request[slot];
  494. req->dtb_low = (u32)buffer;
  495. /* Dont use PRDs */
  496. req->control_flags = CRQB_CNTRLFLAGS_PRDMODE;
  497. req->control_flags |= iswrite ? 0 : CRQB_CNTRLFLAGS_DIR;
  498. req->control_flags |=
  499. ((cfis->pm_port_c << CRQB_CNTRLFLAGS_PMPORTSHIFT)
  500. & CRQB_CNTRLFLAGS_PMPORTMASK);
  501. req->drb_count = len;
  502. req->ata_cmd_feat = (cfis->command << CRQB_CMDFEAT_CMDSHIFT) &
  503. CRQB_CMDFEAT_CMDMASK;
  504. req->ata_cmd_feat |= (cfis->features << CRQB_CMDFEAT_FEATSHIFT) &
  505. CRQB_CMDFEAT_FEATMASK;
  506. req->ata_addr = (cfis->lba_low << CRQB_ADDR_LBA_LOWSHIFT) &
  507. CRQB_ADDR_LBA_LOWMASK;
  508. req->ata_addr |= (cfis->lba_mid << CRQB_ADDR_LBA_MIDSHIFT) &
  509. CRQB_ADDR_LBA_MIDMASK;
  510. req->ata_addr |= (cfis->lba_high << CRQB_ADDR_LBA_HIGHSHIFT) &
  511. CRQB_ADDR_LBA_HIGHMASK;
  512. req->ata_addr |= (cfis->device << CRQB_ADDR_DEVICE_SHIFT) &
  513. CRQB_ADDR_DEVICE_MASK;
  514. req->ata_addr_exp = (cfis->lba_low_exp << CRQB_ADDR_LBA_LOW_EXP_SHIFT) &
  515. CRQB_ADDR_LBA_LOW_EXP_MASK;
  516. req->ata_addr_exp |=
  517. (cfis->lba_mid_exp << CRQB_ADDR_LBA_MID_EXP_SHIFT) &
  518. CRQB_ADDR_LBA_MID_EXP_MASK;
  519. req->ata_addr_exp |=
  520. (cfis->lba_high_exp << CRQB_ADDR_LBA_HIGH_EXP_SHIFT) &
  521. CRQB_ADDR_LBA_HIGH_EXP_MASK;
  522. req->ata_addr_exp |=
  523. (cfis->features_exp << CRQB_ADDR_FEATURE_EXP_SHIFT) &
  524. CRQB_ADDR_FEATURE_EXP_MASK;
  525. req->ata_sect_count =
  526. (cfis->sector_count << CRQB_SECTCOUNT_COUNT_SHIFT) &
  527. CRQB_SECTCOUNT_COUNT_MASK;
  528. req->ata_sect_count |=
  529. (cfis->sector_count_exp << CRQB_SECTCOUNT_COUNT_EXP_SHIFT) &
  530. CRQB_SECTCOUNT_COUNT_EXP_MASK;
  531. /* Flush data */
  532. start = (u32)req & ~(ARCH_DMA_MINALIGN - 1);
  533. flush_dcache_range(start,
  534. start + ALIGN(sizeof(*req), ARCH_DMA_MINALIGN));
  535. /* Trigger operation */
  536. slot = get_next_reqip(port);
  537. set_reqip(port, slot);
  538. /* Wait for completion */
  539. if (wait_dma_completion(port, slot, 10000)) {
  540. printf("ATA operation timed out\n");
  541. return -1;
  542. }
  543. process_responses(port);
  544. /* Invalidate data on read */
  545. if (buffer && len) {
  546. start = (u32)buffer & ~(ARCH_DMA_MINALIGN - 1);
  547. invalidate_dcache_range(start,
  548. start + ALIGN(len, ARCH_DMA_MINALIGN));
  549. }
  550. return len;
  551. }
  552. static u32 mv_sata_rw_cmd_ext(int port, lbaint_t start, u32 blkcnt,
  553. u8 *buffer, int is_write)
  554. {
  555. struct sata_fis_h2d cfis;
  556. u32 res;
  557. u64 block;
  558. block = (u64)start;
  559. memset(&cfis, 0, sizeof(struct sata_fis_h2d));
  560. cfis.fis_type = SATA_FIS_TYPE_REGISTER_H2D;
  561. cfis.command = (is_write) ? ATA_CMD_WRITE_EXT : ATA_CMD_READ_EXT;
  562. cfis.lba_high_exp = (block >> 40) & 0xff;
  563. cfis.lba_mid_exp = (block >> 32) & 0xff;
  564. cfis.lba_low_exp = (block >> 24) & 0xff;
  565. cfis.lba_high = (block >> 16) & 0xff;
  566. cfis.lba_mid = (block >> 8) & 0xff;
  567. cfis.lba_low = block & 0xff;
  568. cfis.device = ATA_LBA;
  569. cfis.sector_count_exp = (blkcnt >> 8) & 0xff;
  570. cfis.sector_count = blkcnt & 0xff;
  571. res = mv_ata_exec_ata_cmd(port, &cfis, buffer, ATA_SECT_SIZE * blkcnt,
  572. is_write);
  573. return res >= 0 ? blkcnt : res;
  574. }
  575. static u32 mv_sata_rw_cmd(int port, lbaint_t start, u32 blkcnt, u8 *buffer,
  576. int is_write)
  577. {
  578. struct sata_fis_h2d cfis;
  579. lbaint_t block;
  580. u32 res;
  581. block = start;
  582. memset(&cfis, 0, sizeof(struct sata_fis_h2d));
  583. cfis.fis_type = SATA_FIS_TYPE_REGISTER_H2D;
  584. cfis.command = (is_write) ? ATA_CMD_WRITE : ATA_CMD_READ;
  585. cfis.device = ATA_LBA;
  586. cfis.device |= (block >> 24) & 0xf;
  587. cfis.lba_high = (block >> 16) & 0xff;
  588. cfis.lba_mid = (block >> 8) & 0xff;
  589. cfis.lba_low = block & 0xff;
  590. cfis.sector_count = (u8)(blkcnt & 0xff);
  591. res = mv_ata_exec_ata_cmd(port, &cfis, buffer, ATA_SECT_SIZE * blkcnt,
  592. is_write);
  593. return res >= 0 ? blkcnt : res;
  594. }
  595. static u32 ata_low_level_rw(int dev, lbaint_t blknr, lbaint_t blkcnt,
  596. void *buffer, int is_write)
  597. {
  598. lbaint_t start, blks;
  599. u8 *addr;
  600. int max_blks;
  601. debug("%s: %ld %ld\n", __func__, blknr, blkcnt);
  602. start = blknr;
  603. blks = blkcnt;
  604. addr = (u8 *)buffer;
  605. max_blks = MV_ATA_MAX_SECTORS;
  606. do {
  607. if (blks > max_blks) {
  608. if (sata_dev_desc[dev].lba48) {
  609. mv_sata_rw_cmd_ext(dev, start, max_blks, addr,
  610. is_write);
  611. } else {
  612. mv_sata_rw_cmd(dev, start, max_blks, addr,
  613. is_write);
  614. }
  615. start += max_blks;
  616. blks -= max_blks;
  617. addr += ATA_SECT_SIZE * max_blks;
  618. } else {
  619. if (sata_dev_desc[dev].lba48) {
  620. mv_sata_rw_cmd_ext(dev, start, blks, addr,
  621. is_write);
  622. } else {
  623. mv_sata_rw_cmd(dev, start, blks, addr,
  624. is_write);
  625. }
  626. start += blks;
  627. blks = 0;
  628. addr += ATA_SECT_SIZE * blks;
  629. }
  630. } while (blks != 0);
  631. return blkcnt;
  632. }
  633. static int mv_ata_exec_ata_cmd_nondma(int port,
  634. struct sata_fis_h2d *cfis, u8 *buffer,
  635. u32 len, u32 iswrite)
  636. {
  637. struct mv_priv *priv = (struct mv_priv *)sata_dev_desc[port].priv;
  638. int i;
  639. u16 *tp;
  640. debug("%s\n", __func__);
  641. out_le32(priv->regbase + PIO_SECTOR_COUNT, cfis->sector_count);
  642. out_le32(priv->regbase + PIO_LBA_HI, cfis->lba_high);
  643. out_le32(priv->regbase + PIO_LBA_MID, cfis->lba_mid);
  644. out_le32(priv->regbase + PIO_LBA_LOW, cfis->lba_low);
  645. out_le32(priv->regbase + PIO_ERR_FEATURES, cfis->features);
  646. out_le32(priv->regbase + PIO_DEVICE, cfis->device);
  647. out_le32(priv->regbase + PIO_CMD_STATUS, cfis->command);
  648. if (ata_wait_register((u32 *)(priv->regbase + PIO_CMD_STATUS),
  649. ATA_BUSY, 0x0, 10000)) {
  650. debug("Failed to wait for completion\n");
  651. return -1;
  652. }
  653. if (len > 0) {
  654. tp = (u16 *)buffer;
  655. for (i = 0; i < len / 2; i++) {
  656. if (iswrite)
  657. out_le16(priv->regbase + PIO_DATA, *tp++);
  658. else
  659. *tp++ = in_le16(priv->regbase + PIO_DATA);
  660. }
  661. }
  662. return len;
  663. }
  664. static int mv_sata_identify(int port, u16 *id)
  665. {
  666. struct sata_fis_h2d h2d;
  667. memset(&h2d, 0, sizeof(struct sata_fis_h2d));
  668. h2d.fis_type = SATA_FIS_TYPE_REGISTER_H2D;
  669. h2d.command = ATA_CMD_ID_ATA;
  670. /* Give device time to get operational */
  671. mdelay(10);
  672. return mv_ata_exec_ata_cmd_nondma(port, &h2d, (u8 *)id,
  673. ATA_ID_WORDS * 2, READ_CMD);
  674. }
  675. static void mv_sata_xfer_mode(int port, u16 *id)
  676. {
  677. struct mv_priv *priv = (struct mv_priv *)sata_dev_desc[port].priv;
  678. priv->pio = id[ATA_ID_PIO_MODES];
  679. priv->mwdma = id[ATA_ID_MWDMA_MODES];
  680. priv->udma = id[ATA_ID_UDMA_MODES];
  681. debug("pio %04x, mwdma %04x, udma %04x\n", priv->pio, priv->mwdma,
  682. priv->udma);
  683. }
  684. static void mv_sata_set_features(int port)
  685. {
  686. struct mv_priv *priv = (struct mv_priv *)sata_dev_desc[port].priv;
  687. struct sata_fis_h2d cfis;
  688. u8 udma_cap;
  689. memset(&cfis, 0, sizeof(struct sata_fis_h2d));
  690. cfis.fis_type = SATA_FIS_TYPE_REGISTER_H2D;
  691. cfis.command = ATA_CMD_SET_FEATURES;
  692. cfis.features = SETFEATURES_XFER;
  693. /* First check the device capablity */
  694. udma_cap = (u8) (priv->udma & 0xff);
  695. if (udma_cap == ATA_UDMA6)
  696. cfis.sector_count = XFER_UDMA_6;
  697. if (udma_cap == ATA_UDMA5)
  698. cfis.sector_count = XFER_UDMA_5;
  699. if (udma_cap == ATA_UDMA4)
  700. cfis.sector_count = XFER_UDMA_4;
  701. if (udma_cap == ATA_UDMA3)
  702. cfis.sector_count = XFER_UDMA_3;
  703. mv_ata_exec_ata_cmd_nondma(port, &cfis, NULL, 0, READ_CMD);
  704. }
  705. int mv_sata_spin_down(int dev)
  706. {
  707. struct sata_fis_h2d cfis;
  708. struct mv_priv *priv = (struct mv_priv *)sata_dev_desc[dev].priv;
  709. if (priv->link == 0) {
  710. debug("No device on port: %d\n", dev);
  711. return 1;
  712. }
  713. memset(&cfis, 0, sizeof(struct sata_fis_h2d));
  714. cfis.fis_type = SATA_FIS_TYPE_REGISTER_H2D;
  715. cfis.command = ATA_CMD_STANDBY;
  716. return mv_ata_exec_ata_cmd_nondma(dev, &cfis, NULL, 0, READ_CMD);
  717. }
  718. int mv_sata_spin_up(int dev)
  719. {
  720. struct sata_fis_h2d cfis;
  721. struct mv_priv *priv = (struct mv_priv *)sata_dev_desc[dev].priv;
  722. if (priv->link == 0) {
  723. debug("No device on port: %d\n", dev);
  724. return 1;
  725. }
  726. memset(&cfis, 0, sizeof(struct sata_fis_h2d));
  727. cfis.fis_type = SATA_FIS_TYPE_REGISTER_H2D;
  728. cfis.command = ATA_CMD_IDLE;
  729. return mv_ata_exec_ata_cmd_nondma(dev, &cfis, NULL, 0, READ_CMD);
  730. }
  731. ulong sata_read(int dev, ulong blknr, lbaint_t blkcnt, void *buffer)
  732. {
  733. return ata_low_level_rw(dev, blknr, blkcnt, buffer, READ_CMD);
  734. }
  735. ulong sata_write(int dev, ulong blknr, lbaint_t blkcnt, const void *buffer)
  736. {
  737. return ata_low_level_rw(dev, blknr, blkcnt, (void *)buffer, WRITE_CMD);
  738. }
  739. /*
  740. * Initialize SATA memory windows
  741. */
  742. static void mvsata_ide_conf_mbus_windows(void)
  743. {
  744. const struct mbus_dram_target_info *dram;
  745. int i;
  746. dram = mvebu_mbus_dram_info();
  747. /* Disable windows, Set Size/Base to 0 */
  748. for (i = 0; i < 4; i++) {
  749. writel(0, MVSATA_WIN_CONTROL(i));
  750. writel(0, MVSATA_WIN_BASE(i));
  751. }
  752. for (i = 0; i < dram->num_cs; i++) {
  753. const struct mbus_dram_window *cs = dram->cs + i;
  754. writel(((cs->size - 1) & 0xffff0000) | (cs->mbus_attr << 8) |
  755. (dram->mbus_dram_target_id << 4) | 1,
  756. MVSATA_WIN_CONTROL(i));
  757. writel(cs->base & 0xffff0000, MVSATA_WIN_BASE(i));
  758. }
  759. }
  760. int init_sata(int dev)
  761. {
  762. struct mv_priv *priv;
  763. debug("Initialize sata dev: %d\n", dev);
  764. if (dev < 0 || dev >= CONFIG_SYS_SATA_MAX_DEVICE) {
  765. printf("Invalid sata device %d\n", dev);
  766. return -1;
  767. }
  768. priv = (struct mv_priv *)malloc(sizeof(struct mv_priv));
  769. if (!priv) {
  770. printf("Failed to allocate memory for private sata data\n");
  771. return -ENOMEM;
  772. }
  773. memset((void *)priv, 0, sizeof(struct mv_priv));
  774. /* Allocate and align request buffer */
  775. priv->crqb_alloc = malloc(sizeof(struct crqb) * REQUEST_QUEUE_SIZE +
  776. CRQB_ALIGN);
  777. if (!priv->crqb_alloc) {
  778. printf("Unable to allocate memory for request queue\n");
  779. return -ENOMEM;
  780. }
  781. memset(priv->crqb_alloc, 0,
  782. sizeof(struct crqb) * REQUEST_QUEUE_SIZE + CRQB_ALIGN);
  783. priv->request = (struct crqb *)(((u32) priv->crqb_alloc + CRQB_ALIGN) &
  784. ~(CRQB_ALIGN - 1));
  785. /* Allocate and align response buffer */
  786. priv->crpb_alloc = malloc(sizeof(struct crpb) * REQUEST_QUEUE_SIZE +
  787. CRPB_ALIGN);
  788. if (!priv->crpb_alloc) {
  789. printf("Unable to allocate memory for response queue\n");
  790. return -ENOMEM;
  791. }
  792. memset(priv->crpb_alloc, 0,
  793. sizeof(struct crpb) * REQUEST_QUEUE_SIZE + CRPB_ALIGN);
  794. priv->response = (struct crpb *)(((u32) priv->crpb_alloc + CRPB_ALIGN) &
  795. ~(CRPB_ALIGN - 1));
  796. sata_dev_desc[dev].priv = (void *)priv;
  797. sprintf(priv->name, "SATA%d", dev);
  798. priv->regbase = dev == 0 ? SATA0_BASE : SATA1_BASE;
  799. if (!hw_init) {
  800. debug("Initialize sata hw\n");
  801. hw_init = 1;
  802. mv_reset_one_hc();
  803. mvsata_ide_conf_mbus_windows();
  804. }
  805. mv_reset_port(dev);
  806. if (probe_port(dev)) {
  807. priv->link = 0;
  808. return -ENODEV;
  809. }
  810. priv->link = 1;
  811. return 0;
  812. }
  813. int reset_sata(int dev)
  814. {
  815. return 0;
  816. }
  817. int scan_sata(int port)
  818. {
  819. unsigned char serial[ATA_ID_SERNO_LEN + 1];
  820. unsigned char firmware[ATA_ID_FW_REV_LEN + 1];
  821. unsigned char product[ATA_ID_PROD_LEN + 1];
  822. u64 n_sectors;
  823. u16 *id;
  824. struct mv_priv *priv = (struct mv_priv *)sata_dev_desc[port].priv;
  825. if (!priv->link)
  826. return -ENODEV;
  827. id = (u16 *)malloc(ATA_ID_WORDS * 2);
  828. if (!id) {
  829. printf("Failed to malloc id data\n");
  830. return -ENOMEM;
  831. }
  832. mv_sata_identify(port, id);
  833. ata_swap_buf_le16(id, ATA_ID_WORDS);
  834. #ifdef DEBUG
  835. ata_dump_id(id);
  836. #endif
  837. /* Serial number */
  838. ata_id_c_string(id, serial, ATA_ID_SERNO, sizeof(serial));
  839. memcpy(sata_dev_desc[port].product, serial, sizeof(serial));
  840. /* Firmware version */
  841. ata_id_c_string(id, firmware, ATA_ID_FW_REV, sizeof(firmware));
  842. memcpy(sata_dev_desc[port].revision, firmware, sizeof(firmware));
  843. /* Product model */
  844. ata_id_c_string(id, product, ATA_ID_PROD, sizeof(product));
  845. memcpy(sata_dev_desc[port].vendor, product, sizeof(product));
  846. /* Total sectors */
  847. n_sectors = ata_id_n_sectors(id);
  848. sata_dev_desc[port].lba = n_sectors;
  849. /* Check if support LBA48 */
  850. if (ata_id_has_lba48(id)) {
  851. sata_dev_desc[port].lba48 = 1;
  852. debug("Device support LBA48\n");
  853. }
  854. /* Get the NCQ queue depth from device */
  855. priv->queue_depth = ata_id_queue_depth(id);
  856. /* Get the xfer mode from device */
  857. mv_sata_xfer_mode(port, id);
  858. /* Set the xfer mode to highest speed */
  859. mv_sata_set_features(port);
  860. /* Start up */
  861. mv_start_edma_engine(port);
  862. return 0;
  863. }