sata_fsl.c 35 KB

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