sata_fsl.c 36 KB

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