sata_fsl.c 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396
  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_NCQ),
  34. SATA_FSL_HOST_LFLAGS = ATA_LFLAG_SKIP_D2H_BSY,
  35. SATA_FSL_MAX_CMDS = SATA_FSL_QUEUE_DEPTH,
  36. SATA_FSL_CMD_HDR_SIZE = 16, /* 4 DWORDS */
  37. SATA_FSL_CMD_SLOT_SIZE = (SATA_FSL_MAX_CMDS * SATA_FSL_CMD_HDR_SIZE),
  38. /*
  39. * SATA-FSL host controller supports a max. of (15+1) direct PRDEs, and
  40. * chained indirect PRDEs upto a max count of 63.
  41. * We are allocating an array of 63 PRDEs contigiously, but PRDE#15 will
  42. * be setup as an indirect descriptor, pointing to it's next
  43. * (contigious) PRDE. Though chained indirect PRDE arrays are
  44. * supported,it will be more efficient to use a direct PRDT and
  45. * a single chain/link to indirect PRDE array/PRDT.
  46. */
  47. SATA_FSL_CMD_DESC_CFIS_SZ = 32,
  48. SATA_FSL_CMD_DESC_SFIS_SZ = 32,
  49. SATA_FSL_CMD_DESC_ACMD_SZ = 16,
  50. SATA_FSL_CMD_DESC_RSRVD = 16,
  51. SATA_FSL_CMD_DESC_SIZE = (SATA_FSL_CMD_DESC_CFIS_SZ +
  52. SATA_FSL_CMD_DESC_SFIS_SZ +
  53. SATA_FSL_CMD_DESC_ACMD_SZ +
  54. SATA_FSL_CMD_DESC_RSRVD +
  55. SATA_FSL_MAX_PRD * 16),
  56. SATA_FSL_CMD_DESC_OFFSET_TO_PRDT =
  57. (SATA_FSL_CMD_DESC_CFIS_SZ +
  58. SATA_FSL_CMD_DESC_SFIS_SZ +
  59. SATA_FSL_CMD_DESC_ACMD_SZ +
  60. SATA_FSL_CMD_DESC_RSRVD),
  61. SATA_FSL_CMD_DESC_AR_SZ = (SATA_FSL_CMD_DESC_SIZE * SATA_FSL_MAX_CMDS),
  62. SATA_FSL_PORT_PRIV_DMA_SZ = (SATA_FSL_CMD_SLOT_SIZE +
  63. SATA_FSL_CMD_DESC_AR_SZ),
  64. /*
  65. * MPC8315 has two SATA controllers, SATA1 & SATA2
  66. * (one port per controller)
  67. * MPC837x has 2/4 controllers, one port per controller
  68. */
  69. SATA_FSL_MAX_PORTS = 1,
  70. SATA_FSL_IRQ_FLAG = IRQF_SHARED,
  71. };
  72. /*
  73. * Host Controller command register set - per port
  74. */
  75. enum {
  76. CQ = 0,
  77. CA = 8,
  78. CC = 0x10,
  79. CE = 0x18,
  80. DE = 0x20,
  81. CHBA = 0x24,
  82. HSTATUS = 0x28,
  83. HCONTROL = 0x2C,
  84. CQPMP = 0x30,
  85. SIGNATURE = 0x34,
  86. ICC = 0x38,
  87. /*
  88. * Host Status Register (HStatus) bitdefs
  89. */
  90. ONLINE = (1 << 31),
  91. GOING_OFFLINE = (1 << 30),
  92. BIST_ERR = (1 << 29),
  93. FATAL_ERR_HC_MASTER_ERR = (1 << 18),
  94. FATAL_ERR_PARITY_ERR_TX = (1 << 17),
  95. FATAL_ERR_PARITY_ERR_RX = (1 << 16),
  96. FATAL_ERR_DATA_UNDERRUN = (1 << 13),
  97. FATAL_ERR_DATA_OVERRUN = (1 << 12),
  98. FATAL_ERR_CRC_ERR_TX = (1 << 11),
  99. FATAL_ERR_CRC_ERR_RX = (1 << 10),
  100. FATAL_ERR_FIFO_OVRFL_TX = (1 << 9),
  101. FATAL_ERR_FIFO_OVRFL_RX = (1 << 8),
  102. FATAL_ERROR_DECODE = FATAL_ERR_HC_MASTER_ERR |
  103. FATAL_ERR_PARITY_ERR_TX |
  104. FATAL_ERR_PARITY_ERR_RX |
  105. FATAL_ERR_DATA_UNDERRUN |
  106. FATAL_ERR_DATA_OVERRUN |
  107. FATAL_ERR_CRC_ERR_TX |
  108. FATAL_ERR_CRC_ERR_RX |
  109. FATAL_ERR_FIFO_OVRFL_TX | FATAL_ERR_FIFO_OVRFL_RX,
  110. INT_ON_FATAL_ERR = (1 << 5),
  111. INT_ON_PHYRDY_CHG = (1 << 4),
  112. INT_ON_SIGNATURE_UPDATE = (1 << 3),
  113. INT_ON_SNOTIFY_UPDATE = (1 << 2),
  114. INT_ON_SINGL_DEVICE_ERR = (1 << 1),
  115. INT_ON_CMD_COMPLETE = 1,
  116. INT_ON_ERROR = INT_ON_FATAL_ERR |
  117. INT_ON_PHYRDY_CHG | INT_ON_SINGL_DEVICE_ERR,
  118. /*
  119. * Host Control Register (HControl) bitdefs
  120. */
  121. HCONTROL_ONLINE_PHY_RST = (1 << 31),
  122. HCONTROL_FORCE_OFFLINE = (1 << 30),
  123. HCONTROL_PARITY_PROT_MOD = (1 << 14),
  124. HCONTROL_DPATH_PARITY = (1 << 12),
  125. HCONTROL_SNOOP_ENABLE = (1 << 10),
  126. HCONTROL_PMP_ATTACHED = (1 << 9),
  127. HCONTROL_COPYOUT_STATFIS = (1 << 8),
  128. IE_ON_FATAL_ERR = (1 << 5),
  129. IE_ON_PHYRDY_CHG = (1 << 4),
  130. IE_ON_SIGNATURE_UPDATE = (1 << 3),
  131. IE_ON_SNOTIFY_UPDATE = (1 << 2),
  132. IE_ON_SINGL_DEVICE_ERR = (1 << 1),
  133. IE_ON_CMD_COMPLETE = 1,
  134. DEFAULT_PORT_IRQ_ENABLE_MASK = IE_ON_FATAL_ERR | IE_ON_PHYRDY_CHG |
  135. IE_ON_SIGNATURE_UPDATE |
  136. IE_ON_SINGL_DEVICE_ERR | IE_ON_CMD_COMPLETE,
  137. EXT_INDIRECT_SEG_PRD_FLAG = (1 << 31),
  138. DATA_SNOOP_ENABLE = (1 << 22),
  139. };
  140. /*
  141. * SATA Superset Registers
  142. */
  143. enum {
  144. SSTATUS = 0,
  145. SERROR = 4,
  146. SCONTROL = 8,
  147. SNOTIFY = 0xC,
  148. };
  149. /*
  150. * Control Status Register Set
  151. */
  152. enum {
  153. TRANSCFG = 0,
  154. TRANSSTATUS = 4,
  155. LINKCFG = 8,
  156. LINKCFG1 = 0xC,
  157. LINKCFG2 = 0x10,
  158. LINKSTATUS = 0x14,
  159. LINKSTATUS1 = 0x18,
  160. PHYCTRLCFG = 0x1C,
  161. COMMANDSTAT = 0x20,
  162. };
  163. /* PHY (link-layer) configuration control */
  164. enum {
  165. PHY_BIST_ENABLE = 0x01,
  166. };
  167. /*
  168. * Command Header Table entry, i.e, command slot
  169. * 4 Dwords per command slot, command header size == 64 Dwords.
  170. */
  171. struct cmdhdr_tbl_entry {
  172. u32 cda;
  173. u32 prde_fis_len;
  174. u32 ttl;
  175. u32 desc_info;
  176. };
  177. /*
  178. * Description information bitdefs
  179. */
  180. enum {
  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. * SATA FSL controller has a Status FIS which should contain the
  218. * received D2H FIS & taskfile registers. This SFIS is present in
  219. * the command descriptor, and to have a ready reference to it,
  220. * we are caching it here, quite similar to what is done in H/W on
  221. * AHCI compliant devices by copying taskfile fields to a 32-bit
  222. * register.
  223. */
  224. struct ata_taskfile tf;
  225. };
  226. /*
  227. * ata_port->host_set private data
  228. */
  229. struct sata_fsl_host_priv {
  230. void __iomem *hcr_base;
  231. void __iomem *ssr_base;
  232. void __iomem *csr_base;
  233. int irq;
  234. };
  235. static inline unsigned int sata_fsl_tag(unsigned int tag,
  236. void __iomem *hcr_base)
  237. {
  238. /* We let libATA core do actual (queue) tag allocation */
  239. /* all non NCQ/queued commands should have tag#0 */
  240. if (ata_tag_internal(tag)) {
  241. DPRINTK("mapping internal cmds to tag#0\n");
  242. return 0;
  243. }
  244. if (unlikely(tag >= SATA_FSL_QUEUE_DEPTH)) {
  245. DPRINTK("tag %d invalid : out of range\n", tag);
  246. return 0;
  247. }
  248. if (unlikely((ioread32(hcr_base + CQ)) & (1 << tag))) {
  249. DPRINTK("tag %d invalid : in use!!\n", tag);
  250. return 0;
  251. }
  252. return tag;
  253. }
  254. static void sata_fsl_setup_cmd_hdr_entry(struct sata_fsl_port_priv *pp,
  255. unsigned int tag, u32 desc_info,
  256. u32 data_xfer_len, u8 num_prde,
  257. u8 fis_len)
  258. {
  259. dma_addr_t cmd_descriptor_address;
  260. cmd_descriptor_address = pp->cmdentry_paddr +
  261. tag * SATA_FSL_CMD_DESC_SIZE;
  262. /* NOTE: both data_xfer_len & fis_len are Dword counts */
  263. pp->cmdslot[tag].cda = cpu_to_le32(cmd_descriptor_address);
  264. pp->cmdslot[tag].prde_fis_len =
  265. cpu_to_le32((num_prde << 16) | (fis_len << 2));
  266. pp->cmdslot[tag].ttl = cpu_to_le32(data_xfer_len & ~0x03);
  267. pp->cmdslot[tag].desc_info = cpu_to_le32(desc_info | (tag & 0x1F));
  268. VPRINTK("cda=0x%x, prde_fis_len=0x%x, ttl=0x%x, di=0x%x\n",
  269. pp->cmdslot[tag].cda,
  270. pp->cmdslot[tag].prde_fis_len,
  271. pp->cmdslot[tag].ttl, pp->cmdslot[tag].desc_info);
  272. }
  273. static unsigned int sata_fsl_fill_sg(struct ata_queued_cmd *qc, void *cmd_desc,
  274. u32 *ttl, dma_addr_t cmd_desc_paddr)
  275. {
  276. struct scatterlist *sg;
  277. unsigned int num_prde = 0;
  278. u32 ttl_dwords = 0;
  279. /*
  280. * NOTE : direct & indirect prdt's are contigiously allocated
  281. */
  282. struct prde *prd = (struct prde *)&((struct command_desc *)
  283. cmd_desc)->prdt;
  284. struct prde *prd_ptr_to_indirect_ext = NULL;
  285. unsigned indirect_ext_segment_sz = 0;
  286. dma_addr_t indirect_ext_segment_paddr;
  287. unsigned int si;
  288. VPRINTK("SATA FSL : cd = 0x%x, prd = 0x%x\n", cmd_desc, prd);
  289. indirect_ext_segment_paddr = cmd_desc_paddr +
  290. SATA_FSL_CMD_DESC_OFFSET_TO_PRDT + SATA_FSL_MAX_PRD_DIRECT * 16;
  291. for_each_sg(qc->sg, sg, qc->n_elem, si) {
  292. dma_addr_t sg_addr = sg_dma_address(sg);
  293. u32 sg_len = sg_dma_len(sg);
  294. VPRINTK("SATA FSL : fill_sg, sg_addr = 0x%x, sg_len = %d\n",
  295. sg_addr, sg_len);
  296. /* warn if each s/g element is not dword aligned */
  297. if (sg_addr & 0x03)
  298. ata_port_printk(qc->ap, KERN_ERR,
  299. "s/g addr unaligned : 0x%x\n", sg_addr);
  300. if (sg_len & 0x03)
  301. ata_port_printk(qc->ap, KERN_ERR,
  302. "s/g len unaligned : 0x%x\n", sg_len);
  303. if ((num_prde == (SATA_FSL_MAX_PRD_DIRECT - 1)) &&
  304. (qc->n_iter + 1 != qc->n_elem)) {
  305. VPRINTK("setting indirect prde\n");
  306. prd_ptr_to_indirect_ext = prd;
  307. prd->dba = cpu_to_le32(indirect_ext_segment_paddr);
  308. indirect_ext_segment_sz = 0;
  309. ++prd;
  310. ++num_prde;
  311. }
  312. ttl_dwords += sg_len;
  313. prd->dba = cpu_to_le32(sg_addr);
  314. prd->ddc_and_ext =
  315. cpu_to_le32(DATA_SNOOP_ENABLE | (sg_len & ~0x03));
  316. VPRINTK("sg_fill, ttl=%d, dba=0x%x, ddc=0x%x\n",
  317. ttl_dwords, prd->dba, prd->ddc_and_ext);
  318. ++num_prde;
  319. ++prd;
  320. if (prd_ptr_to_indirect_ext)
  321. indirect_ext_segment_sz += sg_len;
  322. }
  323. if (prd_ptr_to_indirect_ext) {
  324. /* set indirect extension flag along with indirect ext. size */
  325. prd_ptr_to_indirect_ext->ddc_and_ext =
  326. cpu_to_le32((EXT_INDIRECT_SEG_PRD_FLAG |
  327. DATA_SNOOP_ENABLE |
  328. (indirect_ext_segment_sz & ~0x03)));
  329. }
  330. *ttl = ttl_dwords;
  331. return num_prde;
  332. }
  333. static void sata_fsl_qc_prep(struct ata_queued_cmd *qc)
  334. {
  335. struct ata_port *ap = qc->ap;
  336. struct sata_fsl_port_priv *pp = ap->private_data;
  337. struct sata_fsl_host_priv *host_priv = ap->host->private_data;
  338. void __iomem *hcr_base = host_priv->hcr_base;
  339. unsigned int tag = sata_fsl_tag(qc->tag, hcr_base);
  340. struct command_desc *cd;
  341. u32 desc_info = CMD_DESC_SNOOP_ENABLE;
  342. u32 num_prde = 0;
  343. u32 ttl_dwords = 0;
  344. dma_addr_t cd_paddr;
  345. cd = (struct command_desc *)pp->cmdentry + tag;
  346. cd_paddr = pp->cmdentry_paddr + tag * SATA_FSL_CMD_DESC_SIZE;
  347. ata_tf_to_fis(&qc->tf, 0, 1, (u8 *) &cd->cfis);
  348. VPRINTK("Dumping cfis : 0x%x, 0x%x, 0x%x\n",
  349. cd->cfis[0], cd->cfis[1], cd->cfis[2]);
  350. if (qc->tf.protocol == ATA_PROT_NCQ) {
  351. VPRINTK("FPDMA xfer,Sctor cnt[0:7],[8:15] = %d,%d\n",
  352. cd->cfis[3], cd->cfis[11]);
  353. }
  354. /* setup "ACMD - atapi command" in cmd. desc. if this is ATAPI cmd */
  355. if (ata_is_atapi(qc->tf.protocol)) {
  356. desc_info |= ATAPI_CMD;
  357. memset((void *)&cd->acmd, 0, 32);
  358. memcpy((void *)&cd->acmd, qc->cdb, qc->dev->cdb_len);
  359. }
  360. if (qc->flags & ATA_QCFLAG_DMAMAP)
  361. num_prde = sata_fsl_fill_sg(qc, (void *)cd,
  362. &ttl_dwords, cd_paddr);
  363. if (qc->tf.protocol == ATA_PROT_NCQ)
  364. desc_info |= FPDMA_QUEUED_CMD;
  365. sata_fsl_setup_cmd_hdr_entry(pp, tag, desc_info, ttl_dwords,
  366. num_prde, 5);
  367. VPRINTK("SATA FSL : xx_qc_prep, di = 0x%x, ttl = %d, num_prde = %d\n",
  368. desc_info, ttl_dwords, num_prde);
  369. }
  370. static unsigned int sata_fsl_qc_issue(struct ata_queued_cmd *qc)
  371. {
  372. struct ata_port *ap = qc->ap;
  373. struct sata_fsl_host_priv *host_priv = ap->host->private_data;
  374. void __iomem *hcr_base = host_priv->hcr_base;
  375. unsigned int tag = sata_fsl_tag(qc->tag, hcr_base);
  376. VPRINTK("xx_qc_issue called,CQ=0x%x,CA=0x%x,CE=0x%x,CC=0x%x\n",
  377. ioread32(CQ + hcr_base),
  378. ioread32(CA + hcr_base),
  379. ioread32(CE + hcr_base), ioread32(CC + hcr_base));
  380. /* Simply queue command to the controller/device */
  381. iowrite32(1 << tag, CQ + hcr_base);
  382. VPRINTK("xx_qc_issue called, tag=%d, CQ=0x%x, CA=0x%x\n",
  383. tag, ioread32(CQ + hcr_base), ioread32(CA + hcr_base));
  384. VPRINTK("CE=0x%x, DE=0x%x, CC=0x%x, CmdStat = 0x%x\n",
  385. ioread32(CE + hcr_base),
  386. ioread32(DE + hcr_base),
  387. ioread32(CC + hcr_base), ioread32(COMMANDSTAT + csr_base));
  388. return 0;
  389. }
  390. static int sata_fsl_scr_write(struct ata_port *ap, unsigned int sc_reg_in,
  391. u32 val)
  392. {
  393. struct sata_fsl_host_priv *host_priv = ap->host->private_data;
  394. void __iomem *ssr_base = host_priv->ssr_base;
  395. unsigned int sc_reg;
  396. switch (sc_reg_in) {
  397. case SCR_STATUS:
  398. case SCR_ERROR:
  399. case SCR_CONTROL:
  400. case SCR_ACTIVE:
  401. sc_reg = sc_reg_in;
  402. break;
  403. default:
  404. return -EINVAL;
  405. }
  406. VPRINTK("xx_scr_write, reg_in = %d\n", sc_reg);
  407. iowrite32(val, ssr_base + (sc_reg * 4));
  408. return 0;
  409. }
  410. static int sata_fsl_scr_read(struct ata_port *ap, unsigned int sc_reg_in,
  411. u32 *val)
  412. {
  413. struct sata_fsl_host_priv *host_priv = ap->host->private_data;
  414. void __iomem *ssr_base = host_priv->ssr_base;
  415. unsigned int sc_reg;
  416. switch (sc_reg_in) {
  417. case SCR_STATUS:
  418. case SCR_ERROR:
  419. case SCR_CONTROL:
  420. case SCR_ACTIVE:
  421. sc_reg = sc_reg_in;
  422. break;
  423. default:
  424. return -EINVAL;
  425. }
  426. VPRINTK("xx_scr_read, reg_in = %d\n", sc_reg);
  427. *val = ioread32(ssr_base + (sc_reg * 4));
  428. return 0;
  429. }
  430. static void sata_fsl_freeze(struct ata_port *ap)
  431. {
  432. struct sata_fsl_host_priv *host_priv = ap->host->private_data;
  433. void __iomem *hcr_base = host_priv->hcr_base;
  434. u32 temp;
  435. VPRINTK("xx_freeze, CQ=0x%x, CA=0x%x, CE=0x%x, DE=0x%x\n",
  436. ioread32(CQ + hcr_base),
  437. ioread32(CA + hcr_base),
  438. ioread32(CE + hcr_base), ioread32(DE + hcr_base));
  439. VPRINTK("CmdStat = 0x%x\n", ioread32(csr_base + COMMANDSTAT));
  440. /* disable interrupts on the controller/port */
  441. temp = ioread32(hcr_base + HCONTROL);
  442. iowrite32((temp & ~0x3F), hcr_base + HCONTROL);
  443. VPRINTK("in xx_freeze : HControl = 0x%x, HStatus = 0x%x\n",
  444. ioread32(hcr_base + HCONTROL), ioread32(hcr_base + HSTATUS));
  445. }
  446. static void sata_fsl_thaw(struct ata_port *ap)
  447. {
  448. struct sata_fsl_host_priv *host_priv = ap->host->private_data;
  449. void __iomem *hcr_base = host_priv->hcr_base;
  450. u32 temp;
  451. /* ack. any pending IRQs for this controller/port */
  452. temp = ioread32(hcr_base + HSTATUS);
  453. VPRINTK("xx_thaw, pending IRQs = 0x%x\n", (temp & 0x3F));
  454. if (temp & 0x3F)
  455. iowrite32((temp & 0x3F), hcr_base + HSTATUS);
  456. /* enable interrupts on the controller/port */
  457. temp = ioread32(hcr_base + HCONTROL);
  458. iowrite32((temp | DEFAULT_PORT_IRQ_ENABLE_MASK), hcr_base + HCONTROL);
  459. VPRINTK("xx_thaw : HControl = 0x%x, HStatus = 0x%x\n",
  460. ioread32(hcr_base + HCONTROL), ioread32(hcr_base + HSTATUS));
  461. }
  462. /*
  463. * NOTE : 1st D2H FIS from device does not update sfis in command descriptor.
  464. */
  465. static inline void sata_fsl_cache_taskfile_from_d2h_fis(struct ata_queued_cmd
  466. *qc,
  467. struct ata_port *ap)
  468. {
  469. struct sata_fsl_port_priv *pp = ap->private_data;
  470. struct sata_fsl_host_priv *host_priv = ap->host->private_data;
  471. void __iomem *hcr_base = host_priv->hcr_base;
  472. unsigned int tag = sata_fsl_tag(qc->tag, hcr_base);
  473. struct command_desc *cd;
  474. cd = pp->cmdentry + tag;
  475. ata_tf_from_fis(cd->sfis, &pp->tf);
  476. }
  477. static u8 sata_fsl_check_status(struct ata_port *ap)
  478. {
  479. struct sata_fsl_port_priv *pp = ap->private_data;
  480. return pp->tf.command;
  481. }
  482. static void sata_fsl_tf_read(struct ata_port *ap, struct ata_taskfile *tf)
  483. {
  484. struct sata_fsl_port_priv *pp = ap->private_data;
  485. *tf = pp->tf;
  486. }
  487. static int sata_fsl_port_start(struct ata_port *ap)
  488. {
  489. struct device *dev = ap->host->dev;
  490. struct sata_fsl_port_priv *pp;
  491. int retval;
  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. /*
  501. * allocate per command dma alignment pad buffer, which is used
  502. * internally by libATA to ensure that all transfers ending on
  503. * unaligned boundaries are padded, to align on Dword boundaries
  504. */
  505. retval = ata_pad_alloc(ap, dev);
  506. if (retval) {
  507. kfree(pp);
  508. return retval;
  509. }
  510. mem = dma_alloc_coherent(dev, SATA_FSL_PORT_PRIV_DMA_SZ, &mem_dma,
  511. GFP_KERNEL);
  512. if (!mem) {
  513. ata_pad_free(ap, dev);
  514. kfree(pp);
  515. return -ENOMEM;
  516. }
  517. memset(mem, 0, SATA_FSL_PORT_PRIV_DMA_SZ);
  518. pp->cmdslot = mem;
  519. pp->cmdslot_paddr = mem_dma;
  520. mem += SATA_FSL_CMD_SLOT_SIZE;
  521. mem_dma += SATA_FSL_CMD_SLOT_SIZE;
  522. pp->cmdentry = mem;
  523. pp->cmdentry_paddr = mem_dma;
  524. ap->private_data = pp;
  525. VPRINTK("CHBA = 0x%x, cmdentry_phys = 0x%x\n",
  526. pp->cmdslot_paddr, pp->cmdentry_paddr);
  527. /* Now, update the CHBA register in host controller cmd register set */
  528. iowrite32(pp->cmdslot_paddr & 0xffffffff, hcr_base + CHBA);
  529. /*
  530. * Now, we can bring the controller on-line & also initiate
  531. * the COMINIT sequence, we simply return here and the boot-probing
  532. * & device discovery process is re-initiated by libATA using a
  533. * Softreset EH (dummy) session. Hence, boot probing and device
  534. * discovey will be part of sata_fsl_softreset() callback.
  535. */
  536. temp = ioread32(hcr_base + HCONTROL);
  537. iowrite32((temp | HCONTROL_ONLINE_PHY_RST), hcr_base + HCONTROL);
  538. VPRINTK("HStatus = 0x%x\n", ioread32(hcr_base + HSTATUS));
  539. VPRINTK("HControl = 0x%x\n", ioread32(hcr_base + HCONTROL));
  540. VPRINTK("CHBA = 0x%x\n", ioread32(hcr_base + CHBA));
  541. #ifdef CONFIG_MPC8315_DS
  542. /*
  543. * Workaround for 8315DS board 3gbps link-up issue,
  544. * currently limit SATA port to GEN1 speed
  545. */
  546. sata_fsl_scr_read(ap, SCR_CONTROL, &temp);
  547. temp &= ~(0xF << 4);
  548. temp |= (0x1 << 4);
  549. sata_fsl_scr_write(ap, SCR_CONTROL, temp);
  550. sata_fsl_scr_read(ap, SCR_CONTROL, &temp);
  551. dev_printk(KERN_WARNING, dev, "scr_control, speed limited to %x\n",
  552. temp);
  553. #endif
  554. return 0;
  555. }
  556. static void sata_fsl_port_stop(struct ata_port *ap)
  557. {
  558. struct device *dev = ap->host->dev;
  559. struct sata_fsl_port_priv *pp = ap->private_data;
  560. struct sata_fsl_host_priv *host_priv = ap->host->private_data;
  561. void __iomem *hcr_base = host_priv->hcr_base;
  562. u32 temp;
  563. /*
  564. * Force host controller to go off-line, aborting current operations
  565. */
  566. temp = ioread32(hcr_base + HCONTROL);
  567. temp &= ~HCONTROL_ONLINE_PHY_RST;
  568. temp |= HCONTROL_FORCE_OFFLINE;
  569. iowrite32(temp, hcr_base + HCONTROL);
  570. /* Poll for controller to go offline - should happen immediately */
  571. ata_wait_register(hcr_base + HSTATUS, ONLINE, ONLINE, 1, 1);
  572. ap->private_data = NULL;
  573. dma_free_coherent(dev, SATA_FSL_PORT_PRIV_DMA_SZ,
  574. pp->cmdslot, pp->cmdslot_paddr);
  575. ata_pad_free(ap, dev);
  576. kfree(pp);
  577. }
  578. static unsigned int sata_fsl_dev_classify(struct ata_port *ap)
  579. {
  580. struct sata_fsl_host_priv *host_priv = ap->host->private_data;
  581. void __iomem *hcr_base = host_priv->hcr_base;
  582. struct ata_taskfile tf;
  583. u32 temp;
  584. temp = ioread32(hcr_base + SIGNATURE);
  585. VPRINTK("raw sig = 0x%x\n", temp);
  586. VPRINTK("HStatus = 0x%x\n", ioread32(hcr_base + HSTATUS));
  587. VPRINTK("HControl = 0x%x\n", ioread32(hcr_base + HCONTROL));
  588. tf.lbah = (temp >> 24) & 0xff;
  589. tf.lbam = (temp >> 16) & 0xff;
  590. tf.lbal = (temp >> 8) & 0xff;
  591. tf.nsect = temp & 0xff;
  592. return ata_dev_classify(&tf);
  593. }
  594. static int sata_fsl_softreset(struct ata_link *link, unsigned int *class,
  595. unsigned long deadline)
  596. {
  597. struct ata_port *ap = link->ap;
  598. struct sata_fsl_port_priv *pp = ap->private_data;
  599. struct sata_fsl_host_priv *host_priv = ap->host->private_data;
  600. void __iomem *hcr_base = host_priv->hcr_base;
  601. u32 temp;
  602. struct ata_taskfile tf;
  603. u8 *cfis;
  604. u32 Serror;
  605. int i = 0;
  606. unsigned long start_jiffies;
  607. DPRINTK("in xx_softreset\n");
  608. try_offline_again:
  609. /*
  610. * Force host controller to go off-line, aborting current operations
  611. */
  612. temp = ioread32(hcr_base + HCONTROL);
  613. temp &= ~HCONTROL_ONLINE_PHY_RST;
  614. iowrite32(temp, hcr_base + HCONTROL);
  615. /* Poll for controller to go offline */
  616. temp = ata_wait_register(hcr_base + HSTATUS, ONLINE, ONLINE, 1, 500);
  617. if (temp & ONLINE) {
  618. ata_port_printk(ap, KERN_ERR,
  619. "Softreset failed, not off-lined %d\n", i);
  620. /*
  621. * Try to offline controller atleast twice
  622. */
  623. i++;
  624. if (i == 2)
  625. goto err;
  626. else
  627. goto try_offline_again;
  628. }
  629. DPRINTK("softreset, controller off-lined\n");
  630. VPRINTK("HStatus = 0x%x\n", ioread32(hcr_base + HSTATUS));
  631. VPRINTK("HControl = 0x%x\n", ioread32(hcr_base + HCONTROL));
  632. /*
  633. * PHY reset should remain asserted for atleast 1ms
  634. */
  635. msleep(1);
  636. /*
  637. * Now, bring the host controller online again, this can take time
  638. * as PHY reset and communication establishment, 1st D2H FIS and
  639. * device signature update is done, on safe side assume 500ms
  640. * NOTE : Host online status may be indicated immediately!!
  641. */
  642. temp = ioread32(hcr_base + HCONTROL);
  643. temp |= (HCONTROL_ONLINE_PHY_RST | HCONTROL_SNOOP_ENABLE);
  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. goto err;
  665. }
  666. /*
  667. * Wait for the first D2H from device,i.e,signature update notification
  668. */
  669. start_jiffies = jiffies;
  670. temp = ata_wait_register(hcr_base + HSTATUS, 0xFF, 0x10,
  671. 500, jiffies_to_msecs(deadline - start_jiffies));
  672. if ((temp & 0xFF) != 0x18) {
  673. ata_port_printk(ap, KERN_WARNING, "No Signature Update\n");
  674. goto err;
  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. DPRINTK("Sending SRST/device reset\n");
  688. ata_tf_init(link->device, &tf);
  689. cfis = (u8 *) &pp->cmdentry->cfis;
  690. /* device reset/SRST is a control register update FIS, uses tag0 */
  691. sata_fsl_setup_cmd_hdr_entry(pp, 0,
  692. SRST_CMD | CMD_DESC_SNOOP_ENABLE, 0, 0, 5);
  693. tf.ctl |= ATA_SRST; /* setup SRST bit in taskfile control reg */
  694. ata_tf_to_fis(&tf, 0, 0, cfis);
  695. DPRINTK("Dumping cfis : 0x%x, 0x%x, 0x%x, 0x%x\n",
  696. cfis[0], cfis[1], cfis[2], cfis[3]);
  697. /*
  698. * Queue SRST command to the controller/device, ensure that no
  699. * other commands are active on the controller/device
  700. */
  701. DPRINTK("@Softreset, CQ = 0x%x, CA = 0x%x, CC = 0x%x\n",
  702. ioread32(CQ + hcr_base),
  703. ioread32(CA + hcr_base), ioread32(CC + hcr_base));
  704. iowrite32(0xFFFF, CC + hcr_base);
  705. iowrite32(1, CQ + hcr_base);
  706. temp = ata_wait_register(CQ + hcr_base, 0x1, 0x1, 1, 5000);
  707. if (temp & 0x1) {
  708. ata_port_printk(ap, KERN_WARNING, "ATA_SRST issue failed\n");
  709. DPRINTK("Softreset@5000,CQ=0x%x,CA=0x%x,CC=0x%x\n",
  710. ioread32(CQ + hcr_base),
  711. ioread32(CA + hcr_base), ioread32(CC + hcr_base));
  712. sata_fsl_scr_read(ap, SCR_ERROR, &Serror);
  713. DPRINTK("HStatus = 0x%x\n", ioread32(hcr_base + HSTATUS));
  714. DPRINTK("HControl = 0x%x\n", ioread32(hcr_base + HCONTROL));
  715. DPRINTK("Serror = 0x%x\n", Serror);
  716. goto err;
  717. }
  718. msleep(1);
  719. /*
  720. * SATA device enters reset state after receving a Control register
  721. * FIS with SRST bit asserted and it awaits another H2D Control reg.
  722. * FIS with SRST bit cleared, then the device does internal diags &
  723. * initialization, followed by indicating it's initialization status
  724. * using ATA signature D2H register FIS to the host controller.
  725. */
  726. sata_fsl_setup_cmd_hdr_entry(pp, 0, CMD_DESC_SNOOP_ENABLE, 0, 0, 5);
  727. tf.ctl &= ~ATA_SRST; /* 2nd H2D Ctl. register FIS */
  728. ata_tf_to_fis(&tf, 0, 0, cfis);
  729. iowrite32(1, CQ + hcr_base);
  730. msleep(150); /* ?? */
  731. /*
  732. * The above command would have signalled an interrupt on command
  733. * complete, which needs special handling, by clearing the Nth
  734. * command bit of the CCreg
  735. */
  736. iowrite32(0x01, CC + hcr_base); /* We know it will be cmd#0 always */
  737. DPRINTK("SATA FSL : Now checking device signature\n");
  738. *class = ATA_DEV_NONE;
  739. /* Verify if SStatus indicates device presence */
  740. if (ata_link_online(link)) {
  741. /*
  742. * if we are here, device presence has been detected,
  743. * 1st D2H FIS would have been received, but sfis in
  744. * command desc. is not updated, but signature register
  745. * would have been updated
  746. */
  747. *class = sata_fsl_dev_classify(ap);
  748. DPRINTK("class = %d\n", *class);
  749. VPRINTK("ccreg = 0x%x\n", ioread32(hcr_base + CC));
  750. VPRINTK("cereg = 0x%x\n", ioread32(hcr_base + CE));
  751. }
  752. return 0;
  753. err:
  754. return -EIO;
  755. }
  756. static void sata_fsl_error_handler(struct ata_port *ap)
  757. {
  758. DPRINTK("in xx_error_handler\n");
  759. /* perform recovery */
  760. ata_do_eh(ap, ata_std_prereset, sata_fsl_softreset, sata_std_hardreset,
  761. ata_std_postreset);
  762. }
  763. static void sata_fsl_post_internal_cmd(struct ata_queued_cmd *qc)
  764. {
  765. if (qc->flags & ATA_QCFLAG_FAILED)
  766. qc->err_mask |= AC_ERR_OTHER;
  767. if (qc->err_mask) {
  768. /* make DMA engine forget about the failed command */
  769. }
  770. }
  771. static void sata_fsl_irq_clear(struct ata_port *ap)
  772. {
  773. /* unused */
  774. }
  775. static void sata_fsl_error_intr(struct ata_port *ap)
  776. {
  777. struct ata_link *link = &ap->link;
  778. struct ata_eh_info *ehi = &link->eh_info;
  779. struct sata_fsl_host_priv *host_priv = ap->host->private_data;
  780. void __iomem *hcr_base = host_priv->hcr_base;
  781. u32 hstatus, dereg, cereg = 0, SError = 0;
  782. unsigned int err_mask = 0, action = 0;
  783. struct ata_queued_cmd *qc;
  784. int freeze = 0;
  785. hstatus = ioread32(hcr_base + HSTATUS);
  786. cereg = ioread32(hcr_base + CE);
  787. ata_ehi_clear_desc(ehi);
  788. /*
  789. * Handle & Clear SError
  790. */
  791. sata_fsl_scr_read(ap, SCR_ERROR, &SError);
  792. if (unlikely(SError & 0xFFFF0000)) {
  793. sata_fsl_scr_write(ap, SCR_ERROR, SError);
  794. err_mask |= AC_ERR_ATA_BUS;
  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 single device errors */
  799. if (cereg) {
  800. /*
  801. * clear the command error, also clears queue to the device
  802. * in error, and we can (re)issue commands to this device.
  803. * When a device is in error all commands queued into the
  804. * host controller and at the device are considered aborted
  805. * and the queue for that device is stopped. Now, after
  806. * clearing the device error, we can issue commands to the
  807. * device to interrogate it to find the source of the error.
  808. */
  809. dereg = ioread32(hcr_base + DE);
  810. iowrite32(dereg, hcr_base + DE);
  811. iowrite32(cereg, hcr_base + CE);
  812. DPRINTK("single device error, CE=0x%x, DE=0x%x\n",
  813. ioread32(hcr_base + CE), ioread32(hcr_base + DE));
  814. /*
  815. * We should consider this as non fatal error, and TF must
  816. * be updated as done below.
  817. */
  818. err_mask |= AC_ERR_DEV;
  819. }
  820. /* handle fatal errors */
  821. if (hstatus & FATAL_ERROR_DECODE) {
  822. err_mask |= AC_ERR_ATA_BUS;
  823. action |= ATA_EH_SOFTRESET;
  824. /* how will fatal error interrupts be completed ?? */
  825. freeze = 1;
  826. }
  827. /* Handle PHYRDY change notification */
  828. if (hstatus & INT_ON_PHYRDY_CHG) {
  829. DPRINTK("SATA FSL: PHYRDY change indication\n");
  830. /* Setup a soft-reset EH action */
  831. ata_ehi_hotplugged(ehi);
  832. freeze = 1;
  833. }
  834. /* record error info */
  835. qc = ata_qc_from_tag(ap, link->active_tag);
  836. if (qc) {
  837. sata_fsl_cache_taskfile_from_d2h_fis(qc, qc->ap);
  838. qc->err_mask |= err_mask;
  839. } else
  840. ehi->err_mask |= err_mask;
  841. ehi->action |= action;
  842. ehi->serror |= SError;
  843. /* freeze or abort */
  844. if (freeze)
  845. ata_port_freeze(ap);
  846. else
  847. ata_port_abort(ap);
  848. }
  849. static void sata_fsl_qc_complete(struct ata_queued_cmd *qc)
  850. {
  851. if (qc->flags & ATA_QCFLAG_RESULT_TF) {
  852. DPRINTK("xx_qc_complete called\n");
  853. sata_fsl_cache_taskfile_from_d2h_fis(qc, qc->ap);
  854. }
  855. }
  856. static void sata_fsl_host_intr(struct ata_port *ap)
  857. {
  858. struct ata_link *link = &ap->link;
  859. struct sata_fsl_host_priv *host_priv = ap->host->private_data;
  860. void __iomem *hcr_base = host_priv->hcr_base;
  861. u32 hstatus, qc_active = 0;
  862. struct ata_queued_cmd *qc;
  863. u32 SError;
  864. hstatus = ioread32(hcr_base + HSTATUS);
  865. sata_fsl_scr_read(ap, SCR_ERROR, &SError);
  866. if (unlikely(SError & 0xFFFF0000)) {
  867. DPRINTK("serror @host_intr : 0x%x\n", SError);
  868. sata_fsl_error_intr(ap);
  869. }
  870. if (unlikely(hstatus & INT_ON_ERROR)) {
  871. DPRINTK("error interrupt!!\n");
  872. sata_fsl_error_intr(ap);
  873. return;
  874. }
  875. if (link->sactive) { /* only true for NCQ commands */
  876. int i;
  877. /* Read command completed register */
  878. qc_active = ioread32(hcr_base + CC);
  879. /* clear CC bit, this will also complete the interrupt */
  880. iowrite32(qc_active, hcr_base + CC);
  881. DPRINTK("Status of all queues :\n");
  882. DPRINTK("qc_active/CC = 0x%x, CA = 0x%x, CE=0x%x\n",
  883. qc_active, ioread32(hcr_base + CA),
  884. ioread32(hcr_base + CE));
  885. for (i = 0; i < SATA_FSL_QUEUE_DEPTH; i++) {
  886. if (qc_active & (1 << i)) {
  887. qc = ata_qc_from_tag(ap, i);
  888. if (qc) {
  889. sata_fsl_qc_complete(qc);
  890. ata_qc_complete(qc);
  891. }
  892. DPRINTK
  893. ("completing ncq cmd,tag=%d,CC=0x%x,CA=0x%x\n",
  894. i, ioread32(hcr_base + CC),
  895. ioread32(hcr_base + CA));
  896. }
  897. }
  898. return;
  899. } else if (ap->qc_active) {
  900. iowrite32(1, hcr_base + CC);
  901. qc = ata_qc_from_tag(ap, link->active_tag);
  902. DPRINTK("completing non-ncq cmd, tag=%d,CC=0x%x\n",
  903. link->active_tag, ioread32(hcr_base + CC));
  904. if (qc) {
  905. sata_fsl_qc_complete(qc);
  906. ata_qc_complete(qc);
  907. }
  908. } else {
  909. /* Spurious Interrupt!! */
  910. DPRINTK("spurious interrupt!!, CC = 0x%x\n",
  911. ioread32(hcr_base + CC));
  912. return;
  913. }
  914. }
  915. static irqreturn_t sata_fsl_interrupt(int irq, void *dev_instance)
  916. {
  917. struct ata_host *host = dev_instance;
  918. struct sata_fsl_host_priv *host_priv = host->private_data;
  919. void __iomem *hcr_base = host_priv->hcr_base;
  920. u32 interrupt_enables;
  921. unsigned handled = 0;
  922. struct ata_port *ap;
  923. /* ack. any pending IRQs for this controller/port */
  924. interrupt_enables = ioread32(hcr_base + HSTATUS);
  925. interrupt_enables &= 0x3F;
  926. DPRINTK("interrupt status 0x%x\n", interrupt_enables);
  927. if (!interrupt_enables)
  928. return IRQ_NONE;
  929. spin_lock(&host->lock);
  930. /* Assuming one port per host controller */
  931. ap = host->ports[0];
  932. if (ap) {
  933. sata_fsl_host_intr(ap);
  934. } else {
  935. dev_printk(KERN_WARNING, host->dev,
  936. "interrupt on disabled port 0\n");
  937. }
  938. iowrite32(interrupt_enables, hcr_base + HSTATUS);
  939. handled = 1;
  940. spin_unlock(&host->lock);
  941. return IRQ_RETVAL(handled);
  942. }
  943. /*
  944. * Multiple ports are represented by multiple SATA controllers with
  945. * one port per controller
  946. */
  947. static int sata_fsl_init_controller(struct ata_host *host)
  948. {
  949. struct sata_fsl_host_priv *host_priv = host->private_data;
  950. void __iomem *hcr_base = host_priv->hcr_base;
  951. u32 temp;
  952. /*
  953. * NOTE : We cannot bring the controller online before setting
  954. * the CHBA, hence main controller initialization is done as
  955. * part of the port_start() callback
  956. */
  957. /* ack. any pending IRQs for this controller/port */
  958. temp = ioread32(hcr_base + HSTATUS);
  959. if (temp & 0x3F)
  960. iowrite32((temp & 0x3F), hcr_base + HSTATUS);
  961. /* Keep interrupts disabled on the controller */
  962. temp = ioread32(hcr_base + HCONTROL);
  963. iowrite32((temp & ~0x3F), hcr_base + HCONTROL);
  964. /* Disable interrupt coalescing control(icc), for the moment */
  965. DPRINTK("icc = 0x%x\n", ioread32(hcr_base + ICC));
  966. iowrite32(0x01000000, hcr_base + ICC);
  967. /* clear error registers, SError is cleared by libATA */
  968. iowrite32(0x00000FFFF, hcr_base + CE);
  969. iowrite32(0x00000FFFF, hcr_base + DE);
  970. /* initially assuming no Port multiplier, set CQPMP to 0 */
  971. iowrite32(0x0, hcr_base + CQPMP);
  972. /*
  973. * host controller will be brought on-line, during xx_port_start()
  974. * callback, that should also initiate the OOB, COMINIT sequence
  975. */
  976. DPRINTK("HStatus = 0x%x\n", ioread32(hcr_base + HSTATUS));
  977. DPRINTK("HControl = 0x%x\n", ioread32(hcr_base + HCONTROL));
  978. return 0;
  979. }
  980. /*
  981. * scsi mid-layer and libata interface structures
  982. */
  983. static struct scsi_host_template sata_fsl_sht = {
  984. .module = THIS_MODULE,
  985. .name = "sata_fsl",
  986. .ioctl = ata_scsi_ioctl,
  987. .queuecommand = ata_scsi_queuecmd,
  988. .change_queue_depth = ata_scsi_change_queue_depth,
  989. .can_queue = SATA_FSL_QUEUE_DEPTH,
  990. .this_id = ATA_SHT_THIS_ID,
  991. .sg_tablesize = SATA_FSL_MAX_PRD_USABLE,
  992. .cmd_per_lun = ATA_SHT_CMD_PER_LUN,
  993. .emulated = ATA_SHT_EMULATED,
  994. .use_clustering = ATA_SHT_USE_CLUSTERING,
  995. .proc_name = "sata_fsl",
  996. .dma_boundary = ATA_DMA_BOUNDARY,
  997. .slave_configure = ata_scsi_slave_config,
  998. .slave_destroy = ata_scsi_slave_destroy,
  999. .bios_param = ata_std_bios_param,
  1000. };
  1001. static const struct ata_port_operations sata_fsl_ops = {
  1002. .check_status = sata_fsl_check_status,
  1003. .check_altstatus = sata_fsl_check_status,
  1004. .dev_select = ata_noop_dev_select,
  1005. .tf_read = sata_fsl_tf_read,
  1006. .qc_prep = sata_fsl_qc_prep,
  1007. .qc_issue = sata_fsl_qc_issue,
  1008. .irq_clear = sata_fsl_irq_clear,
  1009. .scr_read = sata_fsl_scr_read,
  1010. .scr_write = sata_fsl_scr_write,
  1011. .freeze = sata_fsl_freeze,
  1012. .thaw = sata_fsl_thaw,
  1013. .error_handler = sata_fsl_error_handler,
  1014. .post_internal_cmd = sata_fsl_post_internal_cmd,
  1015. .port_start = sata_fsl_port_start,
  1016. .port_stop = sata_fsl_port_stop,
  1017. };
  1018. static const struct ata_port_info sata_fsl_port_info[] = {
  1019. {
  1020. .flags = SATA_FSL_HOST_FLAGS,
  1021. .link_flags = SATA_FSL_HOST_LFLAGS,
  1022. .pio_mask = 0x1f, /* pio 0-4 */
  1023. .udma_mask = 0x7f, /* udma 0-6 */
  1024. .port_ops = &sata_fsl_ops,
  1025. },
  1026. };
  1027. static int sata_fsl_probe(struct of_device *ofdev,
  1028. const struct of_device_id *match)
  1029. {
  1030. int retval = 0;
  1031. void __iomem *hcr_base = NULL;
  1032. void __iomem *ssr_base = NULL;
  1033. void __iomem *csr_base = NULL;
  1034. struct sata_fsl_host_priv *host_priv = NULL;
  1035. struct resource *r;
  1036. int irq;
  1037. struct ata_host *host;
  1038. struct ata_port_info pi = sata_fsl_port_info[0];
  1039. const struct ata_port_info *ppi[] = { &pi, NULL };
  1040. dev_printk(KERN_INFO, &ofdev->dev,
  1041. "Sata FSL Platform/CSB Driver init\n");
  1042. r = kmalloc(sizeof(struct resource), GFP_KERNEL);
  1043. hcr_base = of_iomap(ofdev->node, 0);
  1044. if (!hcr_base)
  1045. goto error_exit_with_cleanup;
  1046. ssr_base = hcr_base + 0x100;
  1047. csr_base = hcr_base + 0x140;
  1048. DPRINTK("@reset i/o = 0x%x\n", ioread32(csr_base + TRANSCFG));
  1049. DPRINTK("sizeof(cmd_desc) = %d\n", sizeof(struct command_desc));
  1050. DPRINTK("sizeof(#define cmd_desc) = %d\n", SATA_FSL_CMD_DESC_SIZE);
  1051. host_priv = kzalloc(sizeof(struct sata_fsl_host_priv), GFP_KERNEL);
  1052. if (!host_priv)
  1053. goto error_exit_with_cleanup;
  1054. host_priv->hcr_base = hcr_base;
  1055. host_priv->ssr_base = ssr_base;
  1056. host_priv->csr_base = csr_base;
  1057. irq = irq_of_parse_and_map(ofdev->node, 0);
  1058. if (irq < 0) {
  1059. dev_printk(KERN_ERR, &ofdev->dev, "invalid irq from platform\n");
  1060. goto error_exit_with_cleanup;
  1061. }
  1062. host_priv->irq = irq;
  1063. /* allocate host structure */
  1064. host = ata_host_alloc_pinfo(&ofdev->dev, ppi, SATA_FSL_MAX_PORTS);
  1065. /* host->iomap is not used currently */
  1066. host->private_data = host_priv;
  1067. /* setup port(s) */
  1068. host->ports[0]->ioaddr.cmd_addr = host_priv->hcr_base;
  1069. host->ports[0]->ioaddr.scr_addr = host_priv->ssr_base;
  1070. /* initialize host controller */
  1071. sata_fsl_init_controller(host);
  1072. /*
  1073. * Now, register with libATA core, this will also initiate the
  1074. * device discovery process, invoking our port_start() handler &
  1075. * error_handler() to execute a dummy Softreset EH session
  1076. */
  1077. ata_host_activate(host, irq, sata_fsl_interrupt, SATA_FSL_IRQ_FLAG,
  1078. &sata_fsl_sht);
  1079. dev_set_drvdata(&ofdev->dev, host);
  1080. return 0;
  1081. error_exit_with_cleanup:
  1082. if (hcr_base)
  1083. iounmap(hcr_base);
  1084. if (host_priv)
  1085. kfree(host_priv);
  1086. return retval;
  1087. }
  1088. static int sata_fsl_remove(struct of_device *ofdev)
  1089. {
  1090. struct ata_host *host = dev_get_drvdata(&ofdev->dev);
  1091. struct sata_fsl_host_priv *host_priv = host->private_data;
  1092. ata_host_detach(host);
  1093. dev_set_drvdata(&ofdev->dev, NULL);
  1094. irq_dispose_mapping(host_priv->irq);
  1095. iounmap(host_priv->hcr_base);
  1096. kfree(host_priv);
  1097. return 0;
  1098. }
  1099. static struct of_device_id fsl_sata_match[] = {
  1100. {
  1101. .compatible = "fsl,mpc8315-sata",
  1102. },
  1103. {
  1104. .compatible = "fsl,mpc8379-sata",
  1105. },
  1106. {},
  1107. };
  1108. MODULE_DEVICE_TABLE(of, fsl_sata_match);
  1109. static struct of_platform_driver fsl_sata_driver = {
  1110. .name = "fsl-sata",
  1111. .match_table = fsl_sata_match,
  1112. .probe = sata_fsl_probe,
  1113. .remove = sata_fsl_remove,
  1114. };
  1115. static int __init sata_fsl_init(void)
  1116. {
  1117. of_register_platform_driver(&fsl_sata_driver);
  1118. return 0;
  1119. }
  1120. static void __exit sata_fsl_exit(void)
  1121. {
  1122. of_unregister_platform_driver(&fsl_sata_driver);
  1123. }
  1124. MODULE_LICENSE("GPL");
  1125. MODULE_AUTHOR("Ashish Kalra, Freescale Semiconductor");
  1126. MODULE_DESCRIPTION("Freescale 3.0Gbps SATA controller low level driver");
  1127. MODULE_VERSION("1.10");
  1128. module_init(sata_fsl_init);
  1129. module_exit(sata_fsl_exit);