sata_fsl.c 36 KB

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