sata_fsl.c 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471
  1. /*
  2. * drivers/ata/sata_fsl.c
  3. *
  4. * Freescale 3.0Gbps SATA device driver
  5. *
  6. * Author: Ashish Kalra <ashish.kalra@freescale.com>
  7. * Li Yang <leoli@freescale.com>
  8. *
  9. * Copyright (c) 2006-2007, 2011 Freescale Semiconductor, Inc.
  10. *
  11. * This program is free software; you can redistribute it and/or modify it
  12. * under the terms of the GNU General Public License as published by the
  13. * Free Software Foundation; either version 2 of the License, or (at your
  14. * option) any later version.
  15. *
  16. */
  17. #include <linux/kernel.h>
  18. #include <linux/module.h>
  19. #include <linux/platform_device.h>
  20. #include <linux/slab.h>
  21. #include <scsi/scsi_host.h>
  22. #include <scsi/scsi_cmnd.h>
  23. #include <linux/libata.h>
  24. #include <asm/io.h>
  25. #include <linux/of_platform.h>
  26. /* Controller information */
  27. enum {
  28. SATA_FSL_QUEUE_DEPTH = 16,
  29. SATA_FSL_MAX_PRD = 63,
  30. SATA_FSL_MAX_PRD_USABLE = SATA_FSL_MAX_PRD - 1,
  31. SATA_FSL_MAX_PRD_DIRECT = 16, /* Direct PRDT entries */
  32. SATA_FSL_HOST_FLAGS = (ATA_FLAG_SATA | ATA_FLAG_PIO_DMA |
  33. ATA_FLAG_PMP | ATA_FLAG_NCQ | ATA_FLAG_AN),
  34. SATA_FSL_MAX_CMDS = SATA_FSL_QUEUE_DEPTH,
  35. SATA_FSL_CMD_HDR_SIZE = 16, /* 4 DWORDS */
  36. SATA_FSL_CMD_SLOT_SIZE = (SATA_FSL_MAX_CMDS * SATA_FSL_CMD_HDR_SIZE),
  37. /*
  38. * SATA-FSL host controller supports a max. of (15+1) direct PRDEs, and
  39. * chained indirect PRDEs up to a max count of 63.
  40. * We are allocating an array of 63 PRDEs contiguously, but PRDE#15 will
  41. * be setup as an indirect descriptor, pointing to it's next
  42. * (contiguous) PRDE. Though chained indirect PRDE arrays are
  43. * supported,it will be more efficient to use a direct PRDT and
  44. * a single chain/link to indirect PRDE array/PRDT.
  45. */
  46. SATA_FSL_CMD_DESC_CFIS_SZ = 32,
  47. SATA_FSL_CMD_DESC_SFIS_SZ = 32,
  48. SATA_FSL_CMD_DESC_ACMD_SZ = 16,
  49. SATA_FSL_CMD_DESC_RSRVD = 16,
  50. SATA_FSL_CMD_DESC_SIZE = (SATA_FSL_CMD_DESC_CFIS_SZ +
  51. SATA_FSL_CMD_DESC_SFIS_SZ +
  52. SATA_FSL_CMD_DESC_ACMD_SZ +
  53. SATA_FSL_CMD_DESC_RSRVD +
  54. SATA_FSL_MAX_PRD * 16),
  55. SATA_FSL_CMD_DESC_OFFSET_TO_PRDT =
  56. (SATA_FSL_CMD_DESC_CFIS_SZ +
  57. SATA_FSL_CMD_DESC_SFIS_SZ +
  58. SATA_FSL_CMD_DESC_ACMD_SZ +
  59. SATA_FSL_CMD_DESC_RSRVD),
  60. SATA_FSL_CMD_DESC_AR_SZ = (SATA_FSL_CMD_DESC_SIZE * SATA_FSL_MAX_CMDS),
  61. SATA_FSL_PORT_PRIV_DMA_SZ = (SATA_FSL_CMD_SLOT_SIZE +
  62. SATA_FSL_CMD_DESC_AR_SZ),
  63. /*
  64. * MPC8315 has two SATA controllers, SATA1 & SATA2
  65. * (one port per controller)
  66. * MPC837x has 2/4 controllers, one port per controller
  67. */
  68. SATA_FSL_MAX_PORTS = 1,
  69. SATA_FSL_IRQ_FLAG = IRQF_SHARED,
  70. };
  71. /*
  72. * Host Controller command register set - per port
  73. */
  74. enum {
  75. CQ = 0,
  76. CA = 8,
  77. CC = 0x10,
  78. CE = 0x18,
  79. DE = 0x20,
  80. CHBA = 0x24,
  81. HSTATUS = 0x28,
  82. HCONTROL = 0x2C,
  83. CQPMP = 0x30,
  84. SIGNATURE = 0x34,
  85. ICC = 0x38,
  86. /*
  87. * Host Status Register (HStatus) bitdefs
  88. */
  89. ONLINE = (1 << 31),
  90. GOING_OFFLINE = (1 << 30),
  91. BIST_ERR = (1 << 29),
  92. FATAL_ERR_HC_MASTER_ERR = (1 << 18),
  93. FATAL_ERR_PARITY_ERR_TX = (1 << 17),
  94. FATAL_ERR_PARITY_ERR_RX = (1 << 16),
  95. FATAL_ERR_DATA_UNDERRUN = (1 << 13),
  96. FATAL_ERR_DATA_OVERRUN = (1 << 12),
  97. FATAL_ERR_CRC_ERR_TX = (1 << 11),
  98. FATAL_ERR_CRC_ERR_RX = (1 << 10),
  99. FATAL_ERR_FIFO_OVRFL_TX = (1 << 9),
  100. FATAL_ERR_FIFO_OVRFL_RX = (1 << 8),
  101. FATAL_ERROR_DECODE = FATAL_ERR_HC_MASTER_ERR |
  102. FATAL_ERR_PARITY_ERR_TX |
  103. FATAL_ERR_PARITY_ERR_RX |
  104. FATAL_ERR_DATA_UNDERRUN |
  105. FATAL_ERR_DATA_OVERRUN |
  106. FATAL_ERR_CRC_ERR_TX |
  107. FATAL_ERR_CRC_ERR_RX |
  108. FATAL_ERR_FIFO_OVRFL_TX | FATAL_ERR_FIFO_OVRFL_RX,
  109. INT_ON_FATAL_ERR = (1 << 5),
  110. INT_ON_PHYRDY_CHG = (1 << 4),
  111. INT_ON_SIGNATURE_UPDATE = (1 << 3),
  112. INT_ON_SNOTIFY_UPDATE = (1 << 2),
  113. INT_ON_SINGL_DEVICE_ERR = (1 << 1),
  114. INT_ON_CMD_COMPLETE = 1,
  115. INT_ON_ERROR = INT_ON_FATAL_ERR | INT_ON_SNOTIFY_UPDATE |
  116. INT_ON_PHYRDY_CHG | INT_ON_SINGL_DEVICE_ERR,
  117. /*
  118. * Host Control Register (HControl) bitdefs
  119. */
  120. HCONTROL_ONLINE_PHY_RST = (1 << 31),
  121. HCONTROL_FORCE_OFFLINE = (1 << 30),
  122. HCONTROL_LEGACY = (1 << 28),
  123. HCONTROL_PARITY_PROT_MOD = (1 << 14),
  124. HCONTROL_DPATH_PARITY = (1 << 12),
  125. HCONTROL_SNOOP_ENABLE = (1 << 10),
  126. HCONTROL_PMP_ATTACHED = (1 << 9),
  127. HCONTROL_COPYOUT_STATFIS = (1 << 8),
  128. IE_ON_FATAL_ERR = (1 << 5),
  129. IE_ON_PHYRDY_CHG = (1 << 4),
  130. IE_ON_SIGNATURE_UPDATE = (1 << 3),
  131. IE_ON_SNOTIFY_UPDATE = (1 << 2),
  132. IE_ON_SINGL_DEVICE_ERR = (1 << 1),
  133. IE_ON_CMD_COMPLETE = 1,
  134. DEFAULT_PORT_IRQ_ENABLE_MASK = IE_ON_FATAL_ERR | IE_ON_PHYRDY_CHG |
  135. IE_ON_SIGNATURE_UPDATE | IE_ON_SNOTIFY_UPDATE |
  136. IE_ON_SINGL_DEVICE_ERR | IE_ON_CMD_COMPLETE,
  137. EXT_INDIRECT_SEG_PRD_FLAG = (1 << 31),
  138. DATA_SNOOP_ENABLE_V1 = (1 << 22),
  139. DATA_SNOOP_ENABLE_V2 = (1 << 28),
  140. };
  141. /*
  142. * SATA Superset Registers
  143. */
  144. enum {
  145. SSTATUS = 0,
  146. SERROR = 4,
  147. SCONTROL = 8,
  148. SNOTIFY = 0xC,
  149. };
  150. /*
  151. * Control Status Register Set
  152. */
  153. enum {
  154. TRANSCFG = 0,
  155. TRANSSTATUS = 4,
  156. LINKCFG = 8,
  157. LINKCFG1 = 0xC,
  158. LINKCFG2 = 0x10,
  159. LINKSTATUS = 0x14,
  160. LINKSTATUS1 = 0x18,
  161. PHYCTRLCFG = 0x1C,
  162. COMMANDSTAT = 0x20,
  163. };
  164. /* TRANSCFG (transport-layer) configuration control */
  165. enum {
  166. TRANSCFG_RX_WATER_MARK = (1 << 4),
  167. };
  168. /* PHY (link-layer) configuration control */
  169. enum {
  170. PHY_BIST_ENABLE = 0x01,
  171. };
  172. /*
  173. * Command Header Table entry, i.e, command slot
  174. * 4 Dwords per command slot, command header size == 64 Dwords.
  175. */
  176. struct cmdhdr_tbl_entry {
  177. u32 cda;
  178. u32 prde_fis_len;
  179. u32 ttl;
  180. u32 desc_info;
  181. };
  182. /*
  183. * Description information bitdefs
  184. */
  185. enum {
  186. CMD_DESC_RES = (1 << 11),
  187. VENDOR_SPECIFIC_BIST = (1 << 10),
  188. CMD_DESC_SNOOP_ENABLE = (1 << 9),
  189. FPDMA_QUEUED_CMD = (1 << 8),
  190. SRST_CMD = (1 << 7),
  191. BIST = (1 << 6),
  192. ATAPI_CMD = (1 << 5),
  193. };
  194. /*
  195. * Command Descriptor
  196. */
  197. struct command_desc {
  198. u8 cfis[8 * 4];
  199. u8 sfis[8 * 4];
  200. u8 acmd[4 * 4];
  201. u8 fill[4 * 4];
  202. u32 prdt[SATA_FSL_MAX_PRD_DIRECT * 4];
  203. u32 prdt_indirect[(SATA_FSL_MAX_PRD - SATA_FSL_MAX_PRD_DIRECT) * 4];
  204. };
  205. /*
  206. * Physical region table descriptor(PRD)
  207. */
  208. struct prde {
  209. u32 dba;
  210. u8 fill[2 * 4];
  211. u32 ddc_and_ext;
  212. };
  213. /*
  214. * ata_port private data
  215. * This is our per-port instance data.
  216. */
  217. struct sata_fsl_port_priv {
  218. struct cmdhdr_tbl_entry *cmdslot;
  219. dma_addr_t cmdslot_paddr;
  220. struct command_desc *cmdentry;
  221. dma_addr_t cmdentry_paddr;
  222. };
  223. /*
  224. * ata_port->host_set private data
  225. */
  226. struct sata_fsl_host_priv {
  227. void __iomem *hcr_base;
  228. void __iomem *ssr_base;
  229. void __iomem *csr_base;
  230. int irq;
  231. int data_snoop;
  232. };
  233. static inline unsigned int sata_fsl_tag(unsigned int tag,
  234. void __iomem *hcr_base)
  235. {
  236. /* We let libATA core do actual (queue) tag allocation */
  237. /* all non NCQ/queued commands should have tag#0 */
  238. if (ata_tag_internal(tag)) {
  239. DPRINTK("mapping internal cmds to tag#0\n");
  240. return 0;
  241. }
  242. if (unlikely(tag >= SATA_FSL_QUEUE_DEPTH)) {
  243. DPRINTK("tag %d invalid : out of range\n", tag);
  244. return 0;
  245. }
  246. if (unlikely((ioread32(hcr_base + CQ)) & (1 << tag))) {
  247. DPRINTK("tag %d invalid : in use!!\n", tag);
  248. return 0;
  249. }
  250. return tag;
  251. }
  252. static void sata_fsl_setup_cmd_hdr_entry(struct sata_fsl_port_priv *pp,
  253. unsigned int tag, u32 desc_info,
  254. u32 data_xfer_len, u8 num_prde,
  255. u8 fis_len)
  256. {
  257. dma_addr_t cmd_descriptor_address;
  258. cmd_descriptor_address = pp->cmdentry_paddr +
  259. tag * SATA_FSL_CMD_DESC_SIZE;
  260. /* NOTE: both data_xfer_len & fis_len are Dword counts */
  261. pp->cmdslot[tag].cda = cpu_to_le32(cmd_descriptor_address);
  262. pp->cmdslot[tag].prde_fis_len =
  263. cpu_to_le32((num_prde << 16) | (fis_len << 2));
  264. pp->cmdslot[tag].ttl = cpu_to_le32(data_xfer_len & ~0x03);
  265. pp->cmdslot[tag].desc_info = cpu_to_le32(desc_info | (tag & 0x1F));
  266. VPRINTK("cda=0x%x, prde_fis_len=0x%x, ttl=0x%x, di=0x%x\n",
  267. pp->cmdslot[tag].cda,
  268. pp->cmdslot[tag].prde_fis_len,
  269. pp->cmdslot[tag].ttl, pp->cmdslot[tag].desc_info);
  270. }
  271. static unsigned int sata_fsl_fill_sg(struct ata_queued_cmd *qc, void *cmd_desc,
  272. u32 *ttl, dma_addr_t cmd_desc_paddr,
  273. int data_snoop)
  274. {
  275. struct scatterlist *sg;
  276. unsigned int num_prde = 0;
  277. u32 ttl_dwords = 0;
  278. /*
  279. * NOTE : direct & indirect prdt's are contiguously allocated
  280. */
  281. struct prde *prd = (struct prde *)&((struct command_desc *)
  282. cmd_desc)->prdt;
  283. struct prde *prd_ptr_to_indirect_ext = NULL;
  284. unsigned indirect_ext_segment_sz = 0;
  285. dma_addr_t indirect_ext_segment_paddr;
  286. unsigned int si;
  287. VPRINTK("SATA FSL : cd = 0x%p, prd = 0x%p\n", cmd_desc, prd);
  288. indirect_ext_segment_paddr = cmd_desc_paddr +
  289. SATA_FSL_CMD_DESC_OFFSET_TO_PRDT + SATA_FSL_MAX_PRD_DIRECT * 16;
  290. for_each_sg(qc->sg, sg, qc->n_elem, si) {
  291. dma_addr_t sg_addr = sg_dma_address(sg);
  292. u32 sg_len = sg_dma_len(sg);
  293. VPRINTK("SATA FSL : fill_sg, sg_addr = 0x%llx, sg_len = %d\n",
  294. (unsigned long long)sg_addr, sg_len);
  295. /* warn if each s/g element is not dword aligned */
  296. if (sg_addr & 0x03)
  297. ata_port_err(qc->ap, "s/g addr unaligned : 0x%llx\n",
  298. (unsigned long long)sg_addr);
  299. if (sg_len & 0x03)
  300. ata_port_err(qc->ap, "s/g len unaligned : 0x%x\n",
  301. sg_len);
  302. if (num_prde == (SATA_FSL_MAX_PRD_DIRECT - 1) &&
  303. sg_next(sg) != NULL) {
  304. VPRINTK("setting indirect prde\n");
  305. prd_ptr_to_indirect_ext = prd;
  306. prd->dba = cpu_to_le32(indirect_ext_segment_paddr);
  307. indirect_ext_segment_sz = 0;
  308. ++prd;
  309. ++num_prde;
  310. }
  311. ttl_dwords += sg_len;
  312. prd->dba = cpu_to_le32(sg_addr);
  313. prd->ddc_and_ext = cpu_to_le32(data_snoop | (sg_len & ~0x03));
  314. VPRINTK("sg_fill, ttl=%d, dba=0x%x, ddc=0x%x\n",
  315. ttl_dwords, prd->dba, prd->ddc_and_ext);
  316. ++num_prde;
  317. ++prd;
  318. if (prd_ptr_to_indirect_ext)
  319. indirect_ext_segment_sz += sg_len;
  320. }
  321. if (prd_ptr_to_indirect_ext) {
  322. /* set indirect extension flag along with indirect ext. size */
  323. prd_ptr_to_indirect_ext->ddc_and_ext =
  324. cpu_to_le32((EXT_INDIRECT_SEG_PRD_FLAG |
  325. data_snoop |
  326. (indirect_ext_segment_sz & ~0x03)));
  327. }
  328. *ttl = ttl_dwords;
  329. return num_prde;
  330. }
  331. static void sata_fsl_qc_prep(struct ata_queued_cmd *qc)
  332. {
  333. struct ata_port *ap = qc->ap;
  334. struct sata_fsl_port_priv *pp = ap->private_data;
  335. struct sata_fsl_host_priv *host_priv = ap->host->private_data;
  336. void __iomem *hcr_base = host_priv->hcr_base;
  337. unsigned int tag = sata_fsl_tag(qc->tag, hcr_base);
  338. struct command_desc *cd;
  339. u32 desc_info = CMD_DESC_RES | CMD_DESC_SNOOP_ENABLE;
  340. u32 num_prde = 0;
  341. u32 ttl_dwords = 0;
  342. dma_addr_t cd_paddr;
  343. cd = (struct command_desc *)pp->cmdentry + tag;
  344. cd_paddr = pp->cmdentry_paddr + tag * SATA_FSL_CMD_DESC_SIZE;
  345. ata_tf_to_fis(&qc->tf, qc->dev->link->pmp, 1, (u8 *) &cd->cfis);
  346. VPRINTK("Dumping cfis : 0x%x, 0x%x, 0x%x\n",
  347. cd->cfis[0], cd->cfis[1], cd->cfis[2]);
  348. if (qc->tf.protocol == ATA_PROT_NCQ) {
  349. VPRINTK("FPDMA xfer,Sctor cnt[0:7],[8:15] = %d,%d\n",
  350. cd->cfis[3], cd->cfis[11]);
  351. }
  352. /* setup "ACMD - atapi command" in cmd. desc. if this is ATAPI cmd */
  353. if (ata_is_atapi(qc->tf.protocol)) {
  354. desc_info |= ATAPI_CMD;
  355. memset((void *)&cd->acmd, 0, 32);
  356. memcpy((void *)&cd->acmd, qc->cdb, qc->dev->cdb_len);
  357. }
  358. if (qc->flags & ATA_QCFLAG_DMAMAP)
  359. num_prde = sata_fsl_fill_sg(qc, (void *)cd,
  360. &ttl_dwords, cd_paddr,
  361. host_priv->data_snoop);
  362. if (qc->tf.protocol == ATA_PROT_NCQ)
  363. desc_info |= FPDMA_QUEUED_CMD;
  364. sata_fsl_setup_cmd_hdr_entry(pp, tag, desc_info, ttl_dwords,
  365. num_prde, 5);
  366. VPRINTK("SATA FSL : xx_qc_prep, di = 0x%x, ttl = %d, num_prde = %d\n",
  367. desc_info, ttl_dwords, num_prde);
  368. }
  369. static unsigned int sata_fsl_qc_issue(struct ata_queued_cmd *qc)
  370. {
  371. struct ata_port *ap = qc->ap;
  372. struct sata_fsl_host_priv *host_priv = ap->host->private_data;
  373. void __iomem *hcr_base = host_priv->hcr_base;
  374. unsigned int tag = sata_fsl_tag(qc->tag, hcr_base);
  375. VPRINTK("xx_qc_issue called,CQ=0x%x,CA=0x%x,CE=0x%x,CC=0x%x\n",
  376. ioread32(CQ + hcr_base),
  377. ioread32(CA + hcr_base),
  378. ioread32(CE + hcr_base), ioread32(CC + hcr_base));
  379. iowrite32(qc->dev->link->pmp, CQPMP + hcr_base);
  380. /* Simply queue command to the controller/device */
  381. iowrite32(1 << tag, CQ + hcr_base);
  382. VPRINTK("xx_qc_issue called, tag=%d, CQ=0x%x, CA=0x%x\n",
  383. tag, ioread32(CQ + hcr_base), ioread32(CA + hcr_base));
  384. VPRINTK("CE=0x%x, DE=0x%x, CC=0x%x, CmdStat = 0x%x\n",
  385. ioread32(CE + hcr_base),
  386. ioread32(DE + hcr_base),
  387. ioread32(CC + hcr_base),
  388. ioread32(COMMANDSTAT + host_priv->csr_base));
  389. return 0;
  390. }
  391. static bool sata_fsl_qc_fill_rtf(struct ata_queued_cmd *qc)
  392. {
  393. struct sata_fsl_port_priv *pp = qc->ap->private_data;
  394. struct sata_fsl_host_priv *host_priv = qc->ap->host->private_data;
  395. void __iomem *hcr_base = host_priv->hcr_base;
  396. unsigned int tag = sata_fsl_tag(qc->tag, hcr_base);
  397. struct command_desc *cd;
  398. cd = pp->cmdentry + tag;
  399. ata_tf_from_fis(cd->sfis, &qc->result_tf);
  400. return true;
  401. }
  402. static int sata_fsl_scr_write(struct ata_link *link,
  403. unsigned int sc_reg_in, u32 val)
  404. {
  405. struct sata_fsl_host_priv *host_priv = link->ap->host->private_data;
  406. void __iomem *ssr_base = host_priv->ssr_base;
  407. unsigned int sc_reg;
  408. switch (sc_reg_in) {
  409. case SCR_STATUS:
  410. case SCR_ERROR:
  411. case SCR_CONTROL:
  412. case SCR_ACTIVE:
  413. sc_reg = sc_reg_in;
  414. break;
  415. default:
  416. return -EINVAL;
  417. }
  418. VPRINTK("xx_scr_write, reg_in = %d\n", sc_reg);
  419. iowrite32(val, ssr_base + (sc_reg * 4));
  420. return 0;
  421. }
  422. static int sata_fsl_scr_read(struct ata_link *link,
  423. unsigned int sc_reg_in, u32 *val)
  424. {
  425. struct sata_fsl_host_priv *host_priv = link->ap->host->private_data;
  426. void __iomem *ssr_base = host_priv->ssr_base;
  427. unsigned int sc_reg;
  428. switch (sc_reg_in) {
  429. case SCR_STATUS:
  430. case SCR_ERROR:
  431. case SCR_CONTROL:
  432. case SCR_ACTIVE:
  433. sc_reg = sc_reg_in;
  434. break;
  435. default:
  436. return -EINVAL;
  437. }
  438. VPRINTK("xx_scr_read, reg_in = %d\n", sc_reg);
  439. *val = ioread32(ssr_base + (sc_reg * 4));
  440. return 0;
  441. }
  442. static void sata_fsl_freeze(struct ata_port *ap)
  443. {
  444. struct sata_fsl_host_priv *host_priv = ap->host->private_data;
  445. void __iomem *hcr_base = host_priv->hcr_base;
  446. u32 temp;
  447. VPRINTK("xx_freeze, CQ=0x%x, CA=0x%x, CE=0x%x, DE=0x%x\n",
  448. ioread32(CQ + hcr_base),
  449. ioread32(CA + hcr_base),
  450. ioread32(CE + hcr_base), ioread32(DE + hcr_base));
  451. VPRINTK("CmdStat = 0x%x\n",
  452. ioread32(host_priv->csr_base + COMMANDSTAT));
  453. /* disable interrupts on the controller/port */
  454. temp = ioread32(hcr_base + HCONTROL);
  455. iowrite32((temp & ~0x3F), hcr_base + HCONTROL);
  456. VPRINTK("in xx_freeze : HControl = 0x%x, HStatus = 0x%x\n",
  457. ioread32(hcr_base + HCONTROL), ioread32(hcr_base + HSTATUS));
  458. }
  459. static void sata_fsl_thaw(struct ata_port *ap)
  460. {
  461. struct sata_fsl_host_priv *host_priv = ap->host->private_data;
  462. void __iomem *hcr_base = host_priv->hcr_base;
  463. u32 temp;
  464. /* ack. any pending IRQs for this controller/port */
  465. temp = ioread32(hcr_base + HSTATUS);
  466. VPRINTK("xx_thaw, pending IRQs = 0x%x\n", (temp & 0x3F));
  467. if (temp & 0x3F)
  468. iowrite32((temp & 0x3F), hcr_base + HSTATUS);
  469. /* enable interrupts on the controller/port */
  470. temp = ioread32(hcr_base + HCONTROL);
  471. iowrite32((temp | DEFAULT_PORT_IRQ_ENABLE_MASK), hcr_base + HCONTROL);
  472. VPRINTK("xx_thaw : HControl = 0x%x, HStatus = 0x%x\n",
  473. ioread32(hcr_base + HCONTROL), ioread32(hcr_base + HSTATUS));
  474. }
  475. static void sata_fsl_pmp_attach(struct ata_port *ap)
  476. {
  477. struct sata_fsl_host_priv *host_priv = ap->host->private_data;
  478. void __iomem *hcr_base = host_priv->hcr_base;
  479. u32 temp;
  480. temp = ioread32(hcr_base + HCONTROL);
  481. iowrite32((temp | HCONTROL_PMP_ATTACHED), hcr_base + HCONTROL);
  482. }
  483. static void sata_fsl_pmp_detach(struct ata_port *ap)
  484. {
  485. struct sata_fsl_host_priv *host_priv = ap->host->private_data;
  486. void __iomem *hcr_base = host_priv->hcr_base;
  487. u32 temp;
  488. temp = ioread32(hcr_base + HCONTROL);
  489. temp &= ~HCONTROL_PMP_ATTACHED;
  490. iowrite32(temp, hcr_base + HCONTROL);
  491. /* enable interrupts on the controller/port */
  492. temp = ioread32(hcr_base + HCONTROL);
  493. iowrite32((temp | DEFAULT_PORT_IRQ_ENABLE_MASK), hcr_base + HCONTROL);
  494. }
  495. static int sata_fsl_port_start(struct ata_port *ap)
  496. {
  497. struct device *dev = ap->host->dev;
  498. struct sata_fsl_port_priv *pp;
  499. void *mem;
  500. dma_addr_t mem_dma;
  501. struct sata_fsl_host_priv *host_priv = ap->host->private_data;
  502. void __iomem *hcr_base = host_priv->hcr_base;
  503. u32 temp;
  504. pp = kzalloc(sizeof(*pp), GFP_KERNEL);
  505. if (!pp)
  506. return -ENOMEM;
  507. mem = dma_alloc_coherent(dev, SATA_FSL_PORT_PRIV_DMA_SZ, &mem_dma,
  508. GFP_KERNEL);
  509. if (!mem) {
  510. kfree(pp);
  511. return -ENOMEM;
  512. }
  513. memset(mem, 0, SATA_FSL_PORT_PRIV_DMA_SZ);
  514. pp->cmdslot = mem;
  515. pp->cmdslot_paddr = mem_dma;
  516. mem += SATA_FSL_CMD_SLOT_SIZE;
  517. mem_dma += SATA_FSL_CMD_SLOT_SIZE;
  518. pp->cmdentry = mem;
  519. pp->cmdentry_paddr = mem_dma;
  520. ap->private_data = pp;
  521. VPRINTK("CHBA = 0x%x, cmdentry_phys = 0x%x\n",
  522. pp->cmdslot_paddr, pp->cmdentry_paddr);
  523. /* Now, update the CHBA register in host controller cmd register set */
  524. iowrite32(pp->cmdslot_paddr & 0xffffffff, hcr_base + CHBA);
  525. /*
  526. * Now, we can bring the controller on-line & also initiate
  527. * the COMINIT sequence, we simply return here and the boot-probing
  528. * & device discovery process is re-initiated by libATA using a
  529. * Softreset EH (dummy) session. Hence, boot probing and device
  530. * discovey will be part of sata_fsl_softreset() callback.
  531. */
  532. temp = ioread32(hcr_base + HCONTROL);
  533. iowrite32((temp | HCONTROL_ONLINE_PHY_RST), hcr_base + HCONTROL);
  534. VPRINTK("HStatus = 0x%x\n", ioread32(hcr_base + HSTATUS));
  535. VPRINTK("HControl = 0x%x\n", ioread32(hcr_base + HCONTROL));
  536. VPRINTK("CHBA = 0x%x\n", ioread32(hcr_base + CHBA));
  537. #ifdef CONFIG_MPC8315_DS
  538. /*
  539. * Workaround for 8315DS board 3gbps link-up issue,
  540. * currently limit SATA port to GEN1 speed
  541. */
  542. sata_fsl_scr_read(&ap->link, SCR_CONTROL, &temp);
  543. temp &= ~(0xF << 4);
  544. temp |= (0x1 << 4);
  545. sata_fsl_scr_write(&ap->link, SCR_CONTROL, temp);
  546. sata_fsl_scr_read(&ap->link, SCR_CONTROL, &temp);
  547. dev_warn(dev, "scr_control, speed limited to %x\n", temp);
  548. #endif
  549. return 0;
  550. }
  551. static void sata_fsl_port_stop(struct ata_port *ap)
  552. {
  553. struct device *dev = ap->host->dev;
  554. struct sata_fsl_port_priv *pp = ap->private_data;
  555. struct sata_fsl_host_priv *host_priv = ap->host->private_data;
  556. void __iomem *hcr_base = host_priv->hcr_base;
  557. u32 temp;
  558. /*
  559. * Force host controller to go off-line, aborting current operations
  560. */
  561. temp = ioread32(hcr_base + HCONTROL);
  562. temp &= ~HCONTROL_ONLINE_PHY_RST;
  563. temp |= HCONTROL_FORCE_OFFLINE;
  564. iowrite32(temp, hcr_base + HCONTROL);
  565. /* Poll for controller to go offline - should happen immediately */
  566. ata_wait_register(ap, hcr_base + HSTATUS, ONLINE, ONLINE, 1, 1);
  567. ap->private_data = NULL;
  568. dma_free_coherent(dev, SATA_FSL_PORT_PRIV_DMA_SZ,
  569. pp->cmdslot, pp->cmdslot_paddr);
  570. kfree(pp);
  571. }
  572. static unsigned int sata_fsl_dev_classify(struct ata_port *ap)
  573. {
  574. struct sata_fsl_host_priv *host_priv = ap->host->private_data;
  575. void __iomem *hcr_base = host_priv->hcr_base;
  576. struct ata_taskfile tf;
  577. u32 temp;
  578. temp = ioread32(hcr_base + SIGNATURE);
  579. VPRINTK("raw sig = 0x%x\n", temp);
  580. VPRINTK("HStatus = 0x%x\n", ioread32(hcr_base + HSTATUS));
  581. VPRINTK("HControl = 0x%x\n", ioread32(hcr_base + HCONTROL));
  582. tf.lbah = (temp >> 24) & 0xff;
  583. tf.lbam = (temp >> 16) & 0xff;
  584. tf.lbal = (temp >> 8) & 0xff;
  585. tf.nsect = temp & 0xff;
  586. return ata_dev_classify(&tf);
  587. }
  588. static int sata_fsl_hardreset(struct ata_link *link, unsigned int *class,
  589. unsigned long deadline)
  590. {
  591. struct ata_port *ap = link->ap;
  592. struct sata_fsl_host_priv *host_priv = ap->host->private_data;
  593. void __iomem *hcr_base = host_priv->hcr_base;
  594. u32 temp;
  595. int i = 0;
  596. unsigned long start_jiffies;
  597. DPRINTK("in xx_hardreset\n");
  598. try_offline_again:
  599. /*
  600. * Force host controller to go off-line, aborting current operations
  601. */
  602. temp = ioread32(hcr_base + HCONTROL);
  603. temp &= ~HCONTROL_ONLINE_PHY_RST;
  604. iowrite32(temp, hcr_base + HCONTROL);
  605. /* Poll for controller to go offline */
  606. temp = ata_wait_register(ap, hcr_base + HSTATUS, ONLINE, ONLINE,
  607. 1, 500);
  608. if (temp & ONLINE) {
  609. ata_port_err(ap, "Hardreset failed, not off-lined %d\n", i);
  610. /*
  611. * Try to offline controller atleast twice
  612. */
  613. i++;
  614. if (i == 2)
  615. goto err;
  616. else
  617. goto try_offline_again;
  618. }
  619. DPRINTK("hardreset, controller off-lined\n");
  620. VPRINTK("HStatus = 0x%x\n", ioread32(hcr_base + HSTATUS));
  621. VPRINTK("HControl = 0x%x\n", ioread32(hcr_base + HCONTROL));
  622. /*
  623. * PHY reset should remain asserted for atleast 1ms
  624. */
  625. ata_msleep(ap, 1);
  626. /*
  627. * Now, bring the host controller online again, this can take time
  628. * as PHY reset and communication establishment, 1st D2H FIS and
  629. * device signature update is done, on safe side assume 500ms
  630. * NOTE : Host online status may be indicated immediately!!
  631. */
  632. temp = ioread32(hcr_base + HCONTROL);
  633. temp |= (HCONTROL_ONLINE_PHY_RST | HCONTROL_SNOOP_ENABLE);
  634. temp |= HCONTROL_PMP_ATTACHED;
  635. iowrite32(temp, hcr_base + HCONTROL);
  636. temp = ata_wait_register(ap, hcr_base + HSTATUS, ONLINE, 0, 1, 500);
  637. if (!(temp & ONLINE)) {
  638. ata_port_err(ap, "Hardreset failed, not on-lined\n");
  639. goto err;
  640. }
  641. DPRINTK("hardreset, controller off-lined & on-lined\n");
  642. VPRINTK("HStatus = 0x%x\n", ioread32(hcr_base + HSTATUS));
  643. VPRINTK("HControl = 0x%x\n", ioread32(hcr_base + HCONTROL));
  644. /*
  645. * First, wait for the PHYRDY change to occur before waiting for
  646. * the signature, and also verify if SStatus indicates device
  647. * presence
  648. */
  649. temp = ata_wait_register(ap, hcr_base + HSTATUS, 0xFF, 0, 1, 500);
  650. if ((!(temp & 0x10)) || ata_link_offline(link)) {
  651. ata_port_warn(ap, "No Device OR PHYRDY change,Hstatus = 0x%x\n",
  652. ioread32(hcr_base + HSTATUS));
  653. *class = ATA_DEV_NONE;
  654. return 0;
  655. }
  656. /*
  657. * Wait for the first D2H from device,i.e,signature update notification
  658. */
  659. start_jiffies = jiffies;
  660. temp = ata_wait_register(ap, hcr_base + HSTATUS, 0xFF, 0x10,
  661. 500, jiffies_to_msecs(deadline - start_jiffies));
  662. if ((temp & 0xFF) != 0x18) {
  663. ata_port_warn(ap, "No Signature Update\n");
  664. *class = ATA_DEV_NONE;
  665. goto do_followup_srst;
  666. } else {
  667. ata_port_info(ap, "Signature Update detected @ %d msecs\n",
  668. jiffies_to_msecs(jiffies - start_jiffies));
  669. *class = sata_fsl_dev_classify(ap);
  670. return 0;
  671. }
  672. do_followup_srst:
  673. /*
  674. * request libATA to perform follow-up softreset
  675. */
  676. return -EAGAIN;
  677. err:
  678. return -EIO;
  679. }
  680. static int sata_fsl_softreset(struct ata_link *link, unsigned int *class,
  681. unsigned long deadline)
  682. {
  683. struct ata_port *ap = link->ap;
  684. struct sata_fsl_port_priv *pp = ap->private_data;
  685. struct sata_fsl_host_priv *host_priv = ap->host->private_data;
  686. void __iomem *hcr_base = host_priv->hcr_base;
  687. int pmp = sata_srst_pmp(link);
  688. u32 temp;
  689. struct ata_taskfile tf;
  690. u8 *cfis;
  691. u32 Serror;
  692. DPRINTK("in xx_softreset\n");
  693. if (ata_link_offline(link)) {
  694. DPRINTK("PHY reports no device\n");
  695. *class = ATA_DEV_NONE;
  696. return 0;
  697. }
  698. /*
  699. * Send a device reset (SRST) explicitly on command slot #0
  700. * Check : will the command queue (reg) be cleared during offlining ??
  701. * Also we will be online only if Phy commn. has been established
  702. * and device presence has been detected, therefore if we have
  703. * reached here, we can send a command to the target device
  704. */
  705. DPRINTK("Sending SRST/device reset\n");
  706. ata_tf_init(link->device, &tf);
  707. cfis = (u8 *) &pp->cmdentry->cfis;
  708. /* device reset/SRST is a control register update FIS, uses tag0 */
  709. sata_fsl_setup_cmd_hdr_entry(pp, 0,
  710. SRST_CMD | CMD_DESC_RES | CMD_DESC_SNOOP_ENABLE, 0, 0, 5);
  711. tf.ctl |= ATA_SRST; /* setup SRST bit in taskfile control reg */
  712. ata_tf_to_fis(&tf, pmp, 0, cfis);
  713. DPRINTK("Dumping cfis : 0x%x, 0x%x, 0x%x, 0x%x\n",
  714. cfis[0], cfis[1], cfis[2], cfis[3]);
  715. /*
  716. * Queue SRST command to the controller/device, ensure that no
  717. * other commands are active on the controller/device
  718. */
  719. DPRINTK("@Softreset, CQ = 0x%x, CA = 0x%x, CC = 0x%x\n",
  720. ioread32(CQ + hcr_base),
  721. ioread32(CA + hcr_base), ioread32(CC + hcr_base));
  722. iowrite32(0xFFFF, CC + hcr_base);
  723. if (pmp != SATA_PMP_CTRL_PORT)
  724. iowrite32(pmp, CQPMP + hcr_base);
  725. iowrite32(1, CQ + hcr_base);
  726. temp = ata_wait_register(ap, CQ + hcr_base, 0x1, 0x1, 1, 5000);
  727. if (temp & 0x1) {
  728. ata_port_warn(ap, "ATA_SRST issue failed\n");
  729. DPRINTK("Softreset@5000,CQ=0x%x,CA=0x%x,CC=0x%x\n",
  730. ioread32(CQ + hcr_base),
  731. ioread32(CA + hcr_base), ioread32(CC + hcr_base));
  732. sata_fsl_scr_read(&ap->link, SCR_ERROR, &Serror);
  733. DPRINTK("HStatus = 0x%x\n", ioread32(hcr_base + HSTATUS));
  734. DPRINTK("HControl = 0x%x\n", ioread32(hcr_base + HCONTROL));
  735. DPRINTK("Serror = 0x%x\n", Serror);
  736. goto err;
  737. }
  738. ata_msleep(ap, 1);
  739. /*
  740. * SATA device enters reset state after receiving a Control register
  741. * FIS with SRST bit asserted and it awaits another H2D Control reg.
  742. * FIS with SRST bit cleared, then the device does internal diags &
  743. * initialization, followed by indicating it's initialization status
  744. * using ATA signature D2H register FIS to the host controller.
  745. */
  746. sata_fsl_setup_cmd_hdr_entry(pp, 0, CMD_DESC_RES | CMD_DESC_SNOOP_ENABLE,
  747. 0, 0, 5);
  748. tf.ctl &= ~ATA_SRST; /* 2nd H2D Ctl. register FIS */
  749. ata_tf_to_fis(&tf, pmp, 0, cfis);
  750. if (pmp != SATA_PMP_CTRL_PORT)
  751. iowrite32(pmp, CQPMP + hcr_base);
  752. iowrite32(1, CQ + hcr_base);
  753. ata_msleep(ap, 150); /* ?? */
  754. /*
  755. * The above command would have signalled an interrupt on command
  756. * complete, which needs special handling, by clearing the Nth
  757. * command bit of the CCreg
  758. */
  759. iowrite32(0x01, CC + hcr_base); /* We know it will be cmd#0 always */
  760. DPRINTK("SATA FSL : Now checking device signature\n");
  761. *class = ATA_DEV_NONE;
  762. /* Verify if SStatus indicates device presence */
  763. if (ata_link_online(link)) {
  764. /*
  765. * if we are here, device presence has been detected,
  766. * 1st D2H FIS would have been received, but sfis in
  767. * command desc. is not updated, but signature register
  768. * would have been updated
  769. */
  770. *class = sata_fsl_dev_classify(ap);
  771. DPRINTK("class = %d\n", *class);
  772. VPRINTK("ccreg = 0x%x\n", ioread32(hcr_base + CC));
  773. VPRINTK("cereg = 0x%x\n", ioread32(hcr_base + CE));
  774. }
  775. return 0;
  776. err:
  777. return -EIO;
  778. }
  779. static void sata_fsl_error_handler(struct ata_port *ap)
  780. {
  781. DPRINTK("in xx_error_handler\n");
  782. sata_pmp_error_handler(ap);
  783. }
  784. static void sata_fsl_post_internal_cmd(struct ata_queued_cmd *qc)
  785. {
  786. if (qc->flags & ATA_QCFLAG_FAILED)
  787. qc->err_mask |= AC_ERR_OTHER;
  788. if (qc->err_mask) {
  789. /* make DMA engine forget about the failed command */
  790. }
  791. }
  792. static void sata_fsl_error_intr(struct ata_port *ap)
  793. {
  794. struct sata_fsl_host_priv *host_priv = ap->host->private_data;
  795. void __iomem *hcr_base = host_priv->hcr_base;
  796. u32 hstatus, dereg=0, cereg = 0, SError = 0;
  797. unsigned int err_mask = 0, action = 0;
  798. int freeze = 0, abort=0;
  799. struct ata_link *link = NULL;
  800. struct ata_queued_cmd *qc = NULL;
  801. struct ata_eh_info *ehi;
  802. hstatus = ioread32(hcr_base + HSTATUS);
  803. cereg = ioread32(hcr_base + CE);
  804. /* first, analyze and record host port events */
  805. link = &ap->link;
  806. ehi = &link->eh_info;
  807. ata_ehi_clear_desc(ehi);
  808. /*
  809. * Handle & Clear SError
  810. */
  811. sata_fsl_scr_read(&ap->link, SCR_ERROR, &SError);
  812. if (unlikely(SError & 0xFFFF0000))
  813. sata_fsl_scr_write(&ap->link, SCR_ERROR, SError);
  814. DPRINTK("error_intr,hStat=0x%x,CE=0x%x,DE =0x%x,SErr=0x%x\n",
  815. hstatus, cereg, ioread32(hcr_base + DE), SError);
  816. /* handle fatal errors */
  817. if (hstatus & FATAL_ERROR_DECODE) {
  818. ehi->err_mask |= AC_ERR_ATA_BUS;
  819. ehi->action |= ATA_EH_SOFTRESET;
  820. freeze = 1;
  821. }
  822. /* Handle SDB FIS receive & notify update */
  823. if (hstatus & INT_ON_SNOTIFY_UPDATE)
  824. sata_async_notification(ap);
  825. /* Handle PHYRDY change notification */
  826. if (hstatus & INT_ON_PHYRDY_CHG) {
  827. DPRINTK("SATA FSL: PHYRDY change indication\n");
  828. /* Setup a soft-reset EH action */
  829. ata_ehi_hotplugged(ehi);
  830. ata_ehi_push_desc(ehi, "%s", "PHY RDY changed");
  831. freeze = 1;
  832. }
  833. /* handle single device errors */
  834. if (cereg) {
  835. /*
  836. * clear the command error, also clears queue to the device
  837. * in error, and we can (re)issue commands to this device.
  838. * When a device is in error all commands queued into the
  839. * host controller and at the device are considered aborted
  840. * and the queue for that device is stopped. Now, after
  841. * clearing the device error, we can issue commands to the
  842. * device to interrogate it to find the source of the error.
  843. */
  844. abort = 1;
  845. DPRINTK("single device error, CE=0x%x, DE=0x%x\n",
  846. ioread32(hcr_base + CE), ioread32(hcr_base + DE));
  847. /* find out the offending link and qc */
  848. if (ap->nr_pmp_links) {
  849. unsigned int dev_num;
  850. dereg = ioread32(hcr_base + DE);
  851. iowrite32(dereg, hcr_base + DE);
  852. iowrite32(cereg, hcr_base + CE);
  853. dev_num = ffs(dereg) - 1;
  854. if (dev_num < ap->nr_pmp_links && dereg != 0) {
  855. link = &ap->pmp_link[dev_num];
  856. ehi = &link->eh_info;
  857. qc = ata_qc_from_tag(ap, link->active_tag);
  858. /*
  859. * We should consider this as non fatal error,
  860. * and TF must be updated as done below.
  861. */
  862. err_mask |= AC_ERR_DEV;
  863. } else {
  864. err_mask |= AC_ERR_HSM;
  865. action |= ATA_EH_HARDRESET;
  866. freeze = 1;
  867. }
  868. } else {
  869. dereg = ioread32(hcr_base + DE);
  870. iowrite32(dereg, hcr_base + DE);
  871. iowrite32(cereg, hcr_base + CE);
  872. qc = ata_qc_from_tag(ap, link->active_tag);
  873. /*
  874. * We should consider this as non fatal error,
  875. * and TF must be updated as done below.
  876. */
  877. err_mask |= AC_ERR_DEV;
  878. }
  879. }
  880. /* record error info */
  881. if (qc)
  882. qc->err_mask |= err_mask;
  883. else
  884. ehi->err_mask |= err_mask;
  885. ehi->action |= action;
  886. /* freeze or abort */
  887. if (freeze)
  888. ata_port_freeze(ap);
  889. else if (abort) {
  890. if (qc)
  891. ata_link_abort(qc->dev->link);
  892. else
  893. ata_port_abort(ap);
  894. }
  895. }
  896. static void sata_fsl_host_intr(struct ata_port *ap)
  897. {
  898. struct sata_fsl_host_priv *host_priv = ap->host->private_data;
  899. void __iomem *hcr_base = host_priv->hcr_base;
  900. u32 hstatus, done_mask = 0;
  901. struct ata_queued_cmd *qc;
  902. u32 SError;
  903. hstatus = ioread32(hcr_base + HSTATUS);
  904. sata_fsl_scr_read(&ap->link, SCR_ERROR, &SError);
  905. if (unlikely(SError & 0xFFFF0000)) {
  906. DPRINTK("serror @host_intr : 0x%x\n", SError);
  907. sata_fsl_error_intr(ap);
  908. }
  909. if (unlikely(hstatus & INT_ON_ERROR)) {
  910. DPRINTK("error interrupt!!\n");
  911. sata_fsl_error_intr(ap);
  912. return;
  913. }
  914. /* Read command completed register */
  915. done_mask = ioread32(hcr_base + CC);
  916. VPRINTK("Status of all queues :\n");
  917. VPRINTK("done_mask/CC = 0x%x, CA = 0x%x, CE=0x%x,CQ=0x%x,apqa=0x%x\n",
  918. done_mask,
  919. ioread32(hcr_base + CA),
  920. ioread32(hcr_base + CE),
  921. ioread32(hcr_base + CQ),
  922. ap->qc_active);
  923. if (done_mask & ap->qc_active) {
  924. int i;
  925. /* clear CC bit, this will also complete the interrupt */
  926. iowrite32(done_mask, hcr_base + CC);
  927. DPRINTK("Status of all queues :\n");
  928. DPRINTK("done_mask/CC = 0x%x, CA = 0x%x, CE=0x%x\n",
  929. done_mask, ioread32(hcr_base + CA),
  930. ioread32(hcr_base + CE));
  931. for (i = 0; i < SATA_FSL_QUEUE_DEPTH; i++) {
  932. if (done_mask & (1 << i))
  933. DPRINTK
  934. ("completing ncq cmd,tag=%d,CC=0x%x,CA=0x%x\n",
  935. i, ioread32(hcr_base + CC),
  936. ioread32(hcr_base + CA));
  937. }
  938. ata_qc_complete_multiple(ap, ap->qc_active ^ done_mask);
  939. return;
  940. } else if ((ap->qc_active & (1 << ATA_TAG_INTERNAL))) {
  941. iowrite32(1, hcr_base + CC);
  942. qc = ata_qc_from_tag(ap, ATA_TAG_INTERNAL);
  943. DPRINTK("completing non-ncq cmd, CC=0x%x\n",
  944. ioread32(hcr_base + CC));
  945. if (qc) {
  946. ata_qc_complete(qc);
  947. }
  948. } else {
  949. /* Spurious Interrupt!! */
  950. DPRINTK("spurious interrupt!!, CC = 0x%x\n",
  951. ioread32(hcr_base + CC));
  952. iowrite32(done_mask, hcr_base + CC);
  953. return;
  954. }
  955. }
  956. static irqreturn_t sata_fsl_interrupt(int irq, void *dev_instance)
  957. {
  958. struct ata_host *host = dev_instance;
  959. struct sata_fsl_host_priv *host_priv = host->private_data;
  960. void __iomem *hcr_base = host_priv->hcr_base;
  961. u32 interrupt_enables;
  962. unsigned handled = 0;
  963. struct ata_port *ap;
  964. /* ack. any pending IRQs for this controller/port */
  965. interrupt_enables = ioread32(hcr_base + HSTATUS);
  966. interrupt_enables &= 0x3F;
  967. DPRINTK("interrupt status 0x%x\n", interrupt_enables);
  968. if (!interrupt_enables)
  969. return IRQ_NONE;
  970. spin_lock(&host->lock);
  971. /* Assuming one port per host controller */
  972. ap = host->ports[0];
  973. if (ap) {
  974. sata_fsl_host_intr(ap);
  975. } else {
  976. dev_warn(host->dev, "interrupt on disabled port 0\n");
  977. }
  978. iowrite32(interrupt_enables, hcr_base + HSTATUS);
  979. handled = 1;
  980. spin_unlock(&host->lock);
  981. return IRQ_RETVAL(handled);
  982. }
  983. /*
  984. * Multiple ports are represented by multiple SATA controllers with
  985. * one port per controller
  986. */
  987. static int sata_fsl_init_controller(struct ata_host *host)
  988. {
  989. struct sata_fsl_host_priv *host_priv = host->private_data;
  990. void __iomem *hcr_base = host_priv->hcr_base;
  991. u32 temp;
  992. /*
  993. * NOTE : We cannot bring the controller online before setting
  994. * the CHBA, hence main controller initialization is done as
  995. * part of the port_start() callback
  996. */
  997. /* sata controller to operate in enterprise mode */
  998. temp = ioread32(hcr_base + HCONTROL);
  999. iowrite32(temp & ~HCONTROL_LEGACY, hcr_base + HCONTROL);
  1000. /* ack. any pending IRQs for this controller/port */
  1001. temp = ioread32(hcr_base + HSTATUS);
  1002. if (temp & 0x3F)
  1003. iowrite32((temp & 0x3F), hcr_base + HSTATUS);
  1004. /* Keep interrupts disabled on the controller */
  1005. temp = ioread32(hcr_base + HCONTROL);
  1006. iowrite32((temp & ~0x3F), hcr_base + HCONTROL);
  1007. /* Disable interrupt coalescing control(icc), for the moment */
  1008. DPRINTK("icc = 0x%x\n", ioread32(hcr_base + ICC));
  1009. iowrite32(0x01000000, hcr_base + ICC);
  1010. /* clear error registers, SError is cleared by libATA */
  1011. iowrite32(0x00000FFFF, hcr_base + CE);
  1012. iowrite32(0x00000FFFF, hcr_base + DE);
  1013. /*
  1014. * host controller will be brought on-line, during xx_port_start()
  1015. * callback, that should also initiate the OOB, COMINIT sequence
  1016. */
  1017. DPRINTK("HStatus = 0x%x\n", ioread32(hcr_base + HSTATUS));
  1018. DPRINTK("HControl = 0x%x\n", ioread32(hcr_base + HCONTROL));
  1019. return 0;
  1020. }
  1021. /*
  1022. * scsi mid-layer and libata interface structures
  1023. */
  1024. static struct scsi_host_template sata_fsl_sht = {
  1025. ATA_NCQ_SHT("sata_fsl"),
  1026. .can_queue = SATA_FSL_QUEUE_DEPTH,
  1027. .sg_tablesize = SATA_FSL_MAX_PRD_USABLE,
  1028. .dma_boundary = ATA_DMA_BOUNDARY,
  1029. };
  1030. static struct ata_port_operations sata_fsl_ops = {
  1031. .inherits = &sata_pmp_port_ops,
  1032. .qc_defer = ata_std_qc_defer,
  1033. .qc_prep = sata_fsl_qc_prep,
  1034. .qc_issue = sata_fsl_qc_issue,
  1035. .qc_fill_rtf = sata_fsl_qc_fill_rtf,
  1036. .scr_read = sata_fsl_scr_read,
  1037. .scr_write = sata_fsl_scr_write,
  1038. .freeze = sata_fsl_freeze,
  1039. .thaw = sata_fsl_thaw,
  1040. .softreset = sata_fsl_softreset,
  1041. .hardreset = sata_fsl_hardreset,
  1042. .pmp_softreset = sata_fsl_softreset,
  1043. .error_handler = sata_fsl_error_handler,
  1044. .post_internal_cmd = sata_fsl_post_internal_cmd,
  1045. .port_start = sata_fsl_port_start,
  1046. .port_stop = sata_fsl_port_stop,
  1047. .pmp_attach = sata_fsl_pmp_attach,
  1048. .pmp_detach = sata_fsl_pmp_detach,
  1049. };
  1050. static const struct ata_port_info sata_fsl_port_info[] = {
  1051. {
  1052. .flags = SATA_FSL_HOST_FLAGS,
  1053. .pio_mask = ATA_PIO4,
  1054. .udma_mask = ATA_UDMA6,
  1055. .port_ops = &sata_fsl_ops,
  1056. },
  1057. };
  1058. static int sata_fsl_probe(struct platform_device *ofdev)
  1059. {
  1060. int retval = -ENXIO;
  1061. void __iomem *hcr_base = NULL;
  1062. void __iomem *ssr_base = NULL;
  1063. void __iomem *csr_base = NULL;
  1064. struct sata_fsl_host_priv *host_priv = NULL;
  1065. int irq;
  1066. struct ata_host *host;
  1067. u32 temp;
  1068. struct ata_port_info pi = sata_fsl_port_info[0];
  1069. const struct ata_port_info *ppi[] = { &pi, NULL };
  1070. dev_info(&ofdev->dev, "Sata FSL Platform/CSB Driver init\n");
  1071. hcr_base = of_iomap(ofdev->dev.of_node, 0);
  1072. if (!hcr_base)
  1073. goto error_exit_with_cleanup;
  1074. ssr_base = hcr_base + 0x100;
  1075. csr_base = hcr_base + 0x140;
  1076. if (!of_device_is_compatible(ofdev->dev.of_node, "fsl,mpc8315-sata")) {
  1077. temp = ioread32(csr_base + TRANSCFG);
  1078. temp = temp & 0xffffffe0;
  1079. iowrite32(temp | TRANSCFG_RX_WATER_MARK, csr_base + TRANSCFG);
  1080. }
  1081. DPRINTK("@reset i/o = 0x%x\n", ioread32(csr_base + TRANSCFG));
  1082. DPRINTK("sizeof(cmd_desc) = %d\n", sizeof(struct command_desc));
  1083. DPRINTK("sizeof(#define cmd_desc) = %d\n", SATA_FSL_CMD_DESC_SIZE);
  1084. host_priv = kzalloc(sizeof(struct sata_fsl_host_priv), GFP_KERNEL);
  1085. if (!host_priv)
  1086. goto error_exit_with_cleanup;
  1087. host_priv->hcr_base = hcr_base;
  1088. host_priv->ssr_base = ssr_base;
  1089. host_priv->csr_base = csr_base;
  1090. irq = irq_of_parse_and_map(ofdev->dev.of_node, 0);
  1091. if (irq < 0) {
  1092. dev_err(&ofdev->dev, "invalid irq from platform\n");
  1093. goto error_exit_with_cleanup;
  1094. }
  1095. host_priv->irq = irq;
  1096. if (of_device_is_compatible(ofdev->dev.of_node, "fsl,pq-sata-v2"))
  1097. host_priv->data_snoop = DATA_SNOOP_ENABLE_V2;
  1098. else
  1099. host_priv->data_snoop = DATA_SNOOP_ENABLE_V1;
  1100. /* allocate host structure */
  1101. host = ata_host_alloc_pinfo(&ofdev->dev, ppi, SATA_FSL_MAX_PORTS);
  1102. /* host->iomap is not used currently */
  1103. host->private_data = host_priv;
  1104. /* initialize host controller */
  1105. sata_fsl_init_controller(host);
  1106. /*
  1107. * Now, register with libATA core, this will also initiate the
  1108. * device discovery process, invoking our port_start() handler &
  1109. * error_handler() to execute a dummy Softreset EH session
  1110. */
  1111. ata_host_activate(host, irq, sata_fsl_interrupt, SATA_FSL_IRQ_FLAG,
  1112. &sata_fsl_sht);
  1113. dev_set_drvdata(&ofdev->dev, host);
  1114. return 0;
  1115. error_exit_with_cleanup:
  1116. if (hcr_base)
  1117. iounmap(hcr_base);
  1118. if (host_priv)
  1119. kfree(host_priv);
  1120. return retval;
  1121. }
  1122. static int sata_fsl_remove(struct platform_device *ofdev)
  1123. {
  1124. struct ata_host *host = dev_get_drvdata(&ofdev->dev);
  1125. struct sata_fsl_host_priv *host_priv = host->private_data;
  1126. ata_host_detach(host);
  1127. dev_set_drvdata(&ofdev->dev, NULL);
  1128. irq_dispose_mapping(host_priv->irq);
  1129. iounmap(host_priv->hcr_base);
  1130. kfree(host_priv);
  1131. return 0;
  1132. }
  1133. #ifdef CONFIG_PM
  1134. static int sata_fsl_suspend(struct platform_device *op, pm_message_t state)
  1135. {
  1136. struct ata_host *host = dev_get_drvdata(&op->dev);
  1137. return ata_host_suspend(host, state);
  1138. }
  1139. static int sata_fsl_resume(struct platform_device *op)
  1140. {
  1141. struct ata_host *host = dev_get_drvdata(&op->dev);
  1142. struct sata_fsl_host_priv *host_priv = host->private_data;
  1143. int ret;
  1144. void __iomem *hcr_base = host_priv->hcr_base;
  1145. struct ata_port *ap = host->ports[0];
  1146. struct sata_fsl_port_priv *pp = ap->private_data;
  1147. ret = sata_fsl_init_controller(host);
  1148. if (ret) {
  1149. dev_err(&op->dev, "Error initializing hardware\n");
  1150. return ret;
  1151. }
  1152. /* Recovery the CHBA register in host controller cmd register set */
  1153. iowrite32(pp->cmdslot_paddr & 0xffffffff, hcr_base + CHBA);
  1154. iowrite32((ioread32(hcr_base + HCONTROL)
  1155. | HCONTROL_ONLINE_PHY_RST
  1156. | HCONTROL_SNOOP_ENABLE
  1157. | HCONTROL_PMP_ATTACHED),
  1158. hcr_base + HCONTROL);
  1159. ata_host_resume(host);
  1160. return 0;
  1161. }
  1162. #endif
  1163. static struct of_device_id fsl_sata_match[] = {
  1164. {
  1165. .compatible = "fsl,pq-sata",
  1166. },
  1167. {
  1168. .compatible = "fsl,pq-sata-v2",
  1169. },
  1170. {},
  1171. };
  1172. MODULE_DEVICE_TABLE(of, fsl_sata_match);
  1173. static struct platform_driver fsl_sata_driver = {
  1174. .driver = {
  1175. .name = "fsl-sata",
  1176. .owner = THIS_MODULE,
  1177. .of_match_table = fsl_sata_match,
  1178. },
  1179. .probe = sata_fsl_probe,
  1180. .remove = sata_fsl_remove,
  1181. #ifdef CONFIG_PM
  1182. .suspend = sata_fsl_suspend,
  1183. .resume = sata_fsl_resume,
  1184. #endif
  1185. };
  1186. module_platform_driver(fsl_sata_driver);
  1187. MODULE_LICENSE("GPL");
  1188. MODULE_AUTHOR("Ashish Kalra, Freescale Semiconductor");
  1189. MODULE_DESCRIPTION("Freescale 3.0Gbps SATA controller low level driver");
  1190. MODULE_VERSION("1.10");