sata_fsl.c 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399
  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. VPRINTK("SATA FSL : cd = 0x%x, prd = 0x%x\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. ata_for_each_sg(sg, qc) {
  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. (qc->n_iter + 1 != qc->n_elem)) {
  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 (is_atapi_taskfile(&qc->tf)) {
  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), ioread32(COMMANDSTAT + csr_base));
  387. return 0;
  388. }
  389. static int sata_fsl_scr_write(struct ata_port *ap, unsigned int sc_reg_in,
  390. u32 val)
  391. {
  392. struct sata_fsl_host_priv *host_priv = ap->host->private_data;
  393. void __iomem *ssr_base = host_priv->ssr_base;
  394. unsigned int sc_reg;
  395. switch (sc_reg_in) {
  396. case SCR_STATUS:
  397. case SCR_ERROR:
  398. case SCR_CONTROL:
  399. case SCR_ACTIVE:
  400. sc_reg = sc_reg_in;
  401. break;
  402. default:
  403. return -EINVAL;
  404. }
  405. VPRINTK("xx_scr_write, reg_in = %d\n", sc_reg);
  406. iowrite32(val, ssr_base + (sc_reg * 4));
  407. return 0;
  408. }
  409. static int sata_fsl_scr_read(struct ata_port *ap, unsigned int sc_reg_in,
  410. u32 *val)
  411. {
  412. struct sata_fsl_host_priv *host_priv = ap->host->private_data;
  413. void __iomem *ssr_base = host_priv->ssr_base;
  414. unsigned int sc_reg;
  415. switch (sc_reg_in) {
  416. case SCR_STATUS:
  417. case SCR_ERROR:
  418. case SCR_CONTROL:
  419. case SCR_ACTIVE:
  420. sc_reg = sc_reg_in;
  421. break;
  422. default:
  423. return -EINVAL;
  424. }
  425. VPRINTK("xx_scr_read, reg_in = %d\n", sc_reg);
  426. *val = ioread32(ssr_base + (sc_reg * 4));
  427. return 0;
  428. }
  429. static void sata_fsl_freeze(struct ata_port *ap)
  430. {
  431. struct sata_fsl_host_priv *host_priv = ap->host->private_data;
  432. void __iomem *hcr_base = host_priv->hcr_base;
  433. u32 temp;
  434. VPRINTK("xx_freeze, CQ=0x%x, CA=0x%x, CE=0x%x, DE=0x%x\n",
  435. ioread32(CQ + hcr_base),
  436. ioread32(CA + hcr_base),
  437. ioread32(CE + hcr_base), ioread32(DE + hcr_base));
  438. VPRINTK("CmdStat = 0x%x\n", ioread32(csr_base + COMMANDSTAT));
  439. /* disable interrupts on the controller/port */
  440. temp = ioread32(hcr_base + HCONTROL);
  441. iowrite32((temp & ~0x3F), hcr_base + HCONTROL);
  442. VPRINTK("in xx_freeze : HControl = 0x%x, HStatus = 0x%x\n",
  443. ioread32(hcr_base + HCONTROL), ioread32(hcr_base + HSTATUS));
  444. }
  445. static void sata_fsl_thaw(struct ata_port *ap)
  446. {
  447. struct sata_fsl_host_priv *host_priv = ap->host->private_data;
  448. void __iomem *hcr_base = host_priv->hcr_base;
  449. u32 temp;
  450. /* ack. any pending IRQs for this controller/port */
  451. temp = ioread32(hcr_base + HSTATUS);
  452. VPRINTK("xx_thaw, pending IRQs = 0x%x\n", (temp & 0x3F));
  453. if (temp & 0x3F)
  454. iowrite32((temp & 0x3F), hcr_base + HSTATUS);
  455. /* enable interrupts on the controller/port */
  456. temp = ioread32(hcr_base + HCONTROL);
  457. iowrite32((temp | DEFAULT_PORT_IRQ_ENABLE_MASK), hcr_base + HCONTROL);
  458. VPRINTK("xx_thaw : HControl = 0x%x, HStatus = 0x%x\n",
  459. ioread32(hcr_base + HCONTROL), ioread32(hcr_base + HSTATUS));
  460. }
  461. /*
  462. * NOTE : 1st D2H FIS from device does not update sfis in command descriptor.
  463. */
  464. static inline void sata_fsl_cache_taskfile_from_d2h_fis(struct ata_queued_cmd
  465. *qc,
  466. struct ata_port *ap)
  467. {
  468. struct sata_fsl_port_priv *pp = ap->private_data;
  469. struct sata_fsl_host_priv *host_priv = ap->host->private_data;
  470. void __iomem *hcr_base = host_priv->hcr_base;
  471. unsigned int tag = sata_fsl_tag(qc->tag, hcr_base);
  472. struct command_desc *cd;
  473. cd = pp->cmdentry + tag;
  474. ata_tf_from_fis(cd->sfis, &pp->tf);
  475. }
  476. static u8 sata_fsl_check_status(struct ata_port *ap)
  477. {
  478. struct sata_fsl_port_priv *pp = ap->private_data;
  479. return pp->tf.command;
  480. }
  481. static void sata_fsl_tf_read(struct ata_port *ap, struct ata_taskfile *tf)
  482. {
  483. struct sata_fsl_port_priv *pp = ap->private_data;
  484. *tf = pp->tf;
  485. }
  486. static int sata_fsl_port_start(struct ata_port *ap)
  487. {
  488. struct device *dev = ap->host->dev;
  489. struct sata_fsl_port_priv *pp;
  490. int retval;
  491. void *mem;
  492. dma_addr_t mem_dma;
  493. struct sata_fsl_host_priv *host_priv = ap->host->private_data;
  494. void __iomem *hcr_base = host_priv->hcr_base;
  495. u32 temp;
  496. pp = kzalloc(sizeof(*pp), GFP_KERNEL);
  497. if (!pp)
  498. return -ENOMEM;
  499. /*
  500. * allocate per command dma alignment pad buffer, which is used
  501. * internally by libATA to ensure that all transfers ending on
  502. * unaligned boundaries are padded, to align on Dword boundaries
  503. */
  504. retval = ata_pad_alloc(ap, dev);
  505. if (retval) {
  506. kfree(pp);
  507. return retval;
  508. }
  509. mem = dma_alloc_coherent(dev, SATA_FSL_PORT_PRIV_DMA_SZ, &mem_dma,
  510. GFP_KERNEL);
  511. if (!mem) {
  512. ata_pad_free(ap, dev);
  513. kfree(pp);
  514. return -ENOMEM;
  515. }
  516. memset(mem, 0, SATA_FSL_PORT_PRIV_DMA_SZ);
  517. pp->cmdslot = mem;
  518. pp->cmdslot_paddr = mem_dma;
  519. mem += SATA_FSL_CMD_SLOT_SIZE;
  520. mem_dma += SATA_FSL_CMD_SLOT_SIZE;
  521. pp->cmdentry = mem;
  522. pp->cmdentry_paddr = mem_dma;
  523. ap->private_data = pp;
  524. VPRINTK("CHBA = 0x%x, cmdentry_phys = 0x%x\n",
  525. pp->cmdslot_paddr, pp->cmdentry_paddr);
  526. /* Now, update the CHBA register in host controller cmd register set */
  527. iowrite32(pp->cmdslot_paddr & 0xffffffff, hcr_base + CHBA);
  528. /*
  529. * Now, we can bring the controller on-line & also initiate
  530. * the COMINIT sequence, we simply return here and the boot-probing
  531. * & device discovery process is re-initiated by libATA using a
  532. * Softreset EH (dummy) session. Hence, boot probing and device
  533. * discovey will be part of sata_fsl_softreset() callback.
  534. */
  535. temp = ioread32(hcr_base + HCONTROL);
  536. iowrite32((temp | HCONTROL_ONLINE_PHY_RST), hcr_base + HCONTROL);
  537. VPRINTK("HStatus = 0x%x\n", ioread32(hcr_base + HSTATUS));
  538. VPRINTK("HControl = 0x%x\n", ioread32(hcr_base + HCONTROL));
  539. VPRINTK("CHBA = 0x%x\n", ioread32(hcr_base + CHBA));
  540. #ifdef CONFIG_MPC8315_DS
  541. /*
  542. * Workaround for 8315DS board 3gbps link-up issue,
  543. * currently limit SATA port to GEN1 speed
  544. */
  545. sata_fsl_scr_read(ap, SCR_CONTROL, &temp);
  546. temp &= ~(0xF << 4);
  547. temp |= (0x1 << 4);
  548. sata_fsl_scr_write(ap, SCR_CONTROL, temp);
  549. sata_fsl_scr_read(ap, SCR_CONTROL, &temp);
  550. dev_printk(KERN_WARNING, dev, "scr_control, speed limited to %x\n",
  551. temp);
  552. #endif
  553. return 0;
  554. }
  555. static void sata_fsl_port_stop(struct ata_port *ap)
  556. {
  557. struct device *dev = ap->host->dev;
  558. struct sata_fsl_port_priv *pp = ap->private_data;
  559. struct sata_fsl_host_priv *host_priv = ap->host->private_data;
  560. void __iomem *hcr_base = host_priv->hcr_base;
  561. u32 temp;
  562. /*
  563. * Force host controller to go off-line, aborting current operations
  564. */
  565. temp = ioread32(hcr_base + HCONTROL);
  566. temp &= ~HCONTROL_ONLINE_PHY_RST;
  567. temp |= HCONTROL_FORCE_OFFLINE;
  568. iowrite32(temp, hcr_base + HCONTROL);
  569. /* Poll for controller to go offline - should happen immediately */
  570. ata_wait_register(hcr_base + HSTATUS, ONLINE, ONLINE, 1, 1);
  571. ap->private_data = NULL;
  572. dma_free_coherent(dev, SATA_FSL_PORT_PRIV_DMA_SZ,
  573. pp->cmdslot, pp->cmdslot_paddr);
  574. ata_pad_free(ap, dev);
  575. kfree(pp);
  576. }
  577. static unsigned int sata_fsl_dev_classify(struct ata_port *ap)
  578. {
  579. struct sata_fsl_host_priv *host_priv = ap->host->private_data;
  580. void __iomem *hcr_base = host_priv->hcr_base;
  581. struct ata_taskfile tf;
  582. u32 temp;
  583. temp = ioread32(hcr_base + SIGNATURE);
  584. VPRINTK("raw sig = 0x%x\n", temp);
  585. VPRINTK("HStatus = 0x%x\n", ioread32(hcr_base + HSTATUS));
  586. VPRINTK("HControl = 0x%x\n", ioread32(hcr_base + HCONTROL));
  587. tf.lbah = (temp >> 24) & 0xff;
  588. tf.lbam = (temp >> 16) & 0xff;
  589. tf.lbal = (temp >> 8) & 0xff;
  590. tf.nsect = temp & 0xff;
  591. return ata_dev_classify(&tf);
  592. }
  593. static int sata_fsl_softreset(struct ata_link *link, unsigned int *class,
  594. unsigned long deadline)
  595. {
  596. struct ata_port *ap = link->ap;
  597. struct sata_fsl_port_priv *pp = ap->private_data;
  598. struct sata_fsl_host_priv *host_priv = ap->host->private_data;
  599. void __iomem *hcr_base = host_priv->hcr_base;
  600. u32 temp;
  601. struct ata_taskfile tf;
  602. u8 *cfis;
  603. u32 Serror;
  604. int i = 0;
  605. unsigned long start_jiffies;
  606. DPRINTK("in xx_softreset\n");
  607. try_offline_again:
  608. /*
  609. * Force host controller to go off-line, aborting current operations
  610. */
  611. temp = ioread32(hcr_base + HCONTROL);
  612. temp &= ~HCONTROL_ONLINE_PHY_RST;
  613. iowrite32(temp, hcr_base + HCONTROL);
  614. /* Poll for controller to go offline */
  615. temp = ata_wait_register(hcr_base + HSTATUS, ONLINE, ONLINE, 1, 500);
  616. if (temp & ONLINE) {
  617. ata_port_printk(ap, KERN_ERR,
  618. "Softreset failed, not off-lined %d\n", i);
  619. /*
  620. * Try to offline controller atleast twice
  621. */
  622. i++;
  623. if (i == 2)
  624. goto err;
  625. else
  626. goto try_offline_again;
  627. }
  628. DPRINTK("softreset, controller off-lined\n");
  629. VPRINTK("HStatus = 0x%x\n", ioread32(hcr_base + HSTATUS));
  630. VPRINTK("HControl = 0x%x\n", ioread32(hcr_base + HCONTROL));
  631. /*
  632. * PHY reset should remain asserted for atleast 1ms
  633. */
  634. msleep(1);
  635. /*
  636. * Now, bring the host controller online again, this can take time
  637. * as PHY reset and communication establishment, 1st D2H FIS and
  638. * device signature update is done, on safe side assume 500ms
  639. * NOTE : Host online status may be indicated immediately!!
  640. */
  641. temp = ioread32(hcr_base + HCONTROL);
  642. temp |= (HCONTROL_ONLINE_PHY_RST | HCONTROL_SNOOP_ENABLE);
  643. iowrite32(temp, hcr_base + HCONTROL);
  644. temp = ata_wait_register(hcr_base + HSTATUS, ONLINE, 0, 1, 500);
  645. if (!(temp & ONLINE)) {
  646. ata_port_printk(ap, KERN_ERR,
  647. "Softreset failed, not on-lined\n");
  648. goto err;
  649. }
  650. DPRINTK("softreset, controller off-lined & on-lined\n");
  651. VPRINTK("HStatus = 0x%x\n", ioread32(hcr_base + HSTATUS));
  652. VPRINTK("HControl = 0x%x\n", ioread32(hcr_base + HCONTROL));
  653. /*
  654. * First, wait for the PHYRDY change to occur before waiting for
  655. * the signature, and also verify if SStatus indicates device
  656. * presence
  657. */
  658. temp = ata_wait_register(hcr_base + HSTATUS, 0xFF, 0, 1, 500);
  659. if ((!(temp & 0x10)) || ata_link_offline(link)) {
  660. ata_port_printk(ap, KERN_WARNING,
  661. "No Device OR PHYRDY change,Hstatus = 0x%x\n",
  662. ioread32(hcr_base + HSTATUS));
  663. goto err;
  664. }
  665. /*
  666. * Wait for the first D2H from device,i.e,signature update notification
  667. */
  668. start_jiffies = jiffies;
  669. temp = ata_wait_register(hcr_base + HSTATUS, 0xFF, 0x10,
  670. 500, jiffies_to_msecs(deadline - start_jiffies));
  671. if ((temp & 0xFF) != 0x18) {
  672. ata_port_printk(ap, KERN_WARNING, "No Signature Update\n");
  673. goto err;
  674. } else {
  675. ata_port_printk(ap, KERN_INFO,
  676. "Signature Update detected @ %d msecs\n",
  677. jiffies_to_msecs(jiffies - start_jiffies));
  678. }
  679. /*
  680. * Send a device reset (SRST) explicitly on command slot #0
  681. * Check : will the command queue (reg) be cleared during offlining ??
  682. * Also we will be online only if Phy commn. has been established
  683. * and device presence has been detected, therefore if we have
  684. * reached here, we can send a command to the target device
  685. */
  686. DPRINTK("Sending SRST/device reset\n");
  687. ata_tf_init(link->device, &tf);
  688. cfis = (u8 *) &pp->cmdentry->cfis;
  689. /* device reset/SRST is a control register update FIS, uses tag0 */
  690. sata_fsl_setup_cmd_hdr_entry(pp, 0,
  691. SRST_CMD | CMD_DESC_SNOOP_ENABLE, 0, 0, 5);
  692. tf.ctl |= ATA_SRST; /* setup SRST bit in taskfile control reg */
  693. ata_tf_to_fis(&tf, 0, 0, cfis);
  694. DPRINTK("Dumping cfis : 0x%x, 0x%x, 0x%x, 0x%x\n",
  695. cfis[0], cfis[1], cfis[2], cfis[3]);
  696. /*
  697. * Queue SRST command to the controller/device, ensure that no
  698. * other commands are active on the controller/device
  699. */
  700. DPRINTK("@Softreset, CQ = 0x%x, CA = 0x%x, CC = 0x%x\n",
  701. ioread32(CQ + hcr_base),
  702. ioread32(CA + hcr_base), ioread32(CC + hcr_base));
  703. iowrite32(0xFFFF, CC + hcr_base);
  704. iowrite32(1, CQ + hcr_base);
  705. temp = ata_wait_register(CQ + hcr_base, 0x1, 0x1, 1, 5000);
  706. if (temp & 0x1) {
  707. ata_port_printk(ap, KERN_WARNING, "ATA_SRST issue failed\n");
  708. DPRINTK("Softreset@5000,CQ=0x%x,CA=0x%x,CC=0x%x\n",
  709. ioread32(CQ + hcr_base),
  710. ioread32(CA + hcr_base), ioread32(CC + hcr_base));
  711. sata_fsl_scr_read(ap, SCR_ERROR, &Serror);
  712. DPRINTK("HStatus = 0x%x\n", ioread32(hcr_base + HSTATUS));
  713. DPRINTK("HControl = 0x%x\n", ioread32(hcr_base + HCONTROL));
  714. DPRINTK("Serror = 0x%x\n", Serror);
  715. goto err;
  716. }
  717. msleep(1);
  718. /*
  719. * SATA device enters reset state after receving a Control register
  720. * FIS with SRST bit asserted and it awaits another H2D Control reg.
  721. * FIS with SRST bit cleared, then the device does internal diags &
  722. * initialization, followed by indicating it's initialization status
  723. * using ATA signature D2H register FIS to the host controller.
  724. */
  725. sata_fsl_setup_cmd_hdr_entry(pp, 0, CMD_DESC_SNOOP_ENABLE, 0, 0, 5);
  726. tf.ctl &= ~ATA_SRST; /* 2nd H2D Ctl. register FIS */
  727. ata_tf_to_fis(&tf, 0, 0, cfis);
  728. iowrite32(1, CQ + hcr_base);
  729. msleep(150); /* ?? */
  730. /*
  731. * The above command would have signalled an interrupt on command
  732. * complete, which needs special handling, by clearing the Nth
  733. * command bit of the CCreg
  734. */
  735. iowrite32(0x01, CC + hcr_base); /* We know it will be cmd#0 always */
  736. DPRINTK("SATA FSL : Now checking device signature\n");
  737. *class = ATA_DEV_NONE;
  738. /* Verify if SStatus indicates device presence */
  739. if (ata_link_online(link)) {
  740. /*
  741. * if we are here, device presence has been detected,
  742. * 1st D2H FIS would have been received, but sfis in
  743. * command desc. is not updated, but signature register
  744. * would have been updated
  745. */
  746. *class = sata_fsl_dev_classify(ap);
  747. DPRINTK("class = %d\n", *class);
  748. VPRINTK("ccreg = 0x%x\n", ioread32(hcr_base + CC));
  749. VPRINTK("cereg = 0x%x\n", ioread32(hcr_base + CE));
  750. }
  751. return 0;
  752. err:
  753. return -EIO;
  754. }
  755. static void sata_fsl_error_handler(struct ata_port *ap)
  756. {
  757. DPRINTK("in xx_error_handler\n");
  758. /* perform recovery */
  759. ata_do_eh(ap, ata_std_prereset, sata_fsl_softreset, sata_std_hardreset,
  760. ata_std_postreset);
  761. }
  762. static void sata_fsl_post_internal_cmd(struct ata_queued_cmd *qc)
  763. {
  764. if (qc->flags & ATA_QCFLAG_FAILED)
  765. qc->err_mask |= AC_ERR_OTHER;
  766. if (qc->err_mask) {
  767. /* make DMA engine forget about the failed command */
  768. }
  769. }
  770. static void sata_fsl_irq_clear(struct ata_port *ap)
  771. {
  772. /* unused */
  773. }
  774. static void sata_fsl_error_intr(struct ata_port *ap)
  775. {
  776. struct ata_link *link = &ap->link;
  777. struct ata_eh_info *ehi = &link->eh_info;
  778. struct sata_fsl_host_priv *host_priv = ap->host->private_data;
  779. void __iomem *hcr_base = host_priv->hcr_base;
  780. u32 hstatus, dereg, cereg = 0, SError = 0;
  781. unsigned int err_mask = 0, action = 0;
  782. struct ata_queued_cmd *qc;
  783. int freeze = 0;
  784. hstatus = ioread32(hcr_base + HSTATUS);
  785. cereg = ioread32(hcr_base + CE);
  786. ata_ehi_clear_desc(ehi);
  787. /*
  788. * Handle & Clear SError
  789. */
  790. sata_fsl_scr_read(ap, SCR_ERROR, &SError);
  791. if (unlikely(SError & 0xFFFF0000)) {
  792. sata_fsl_scr_write(ap, SCR_ERROR, SError);
  793. err_mask |= AC_ERR_ATA_BUS;
  794. }
  795. DPRINTK("error_intr,hStat=0x%x,CE=0x%x,DE =0x%x,SErr=0x%x\n",
  796. hstatus, cereg, ioread32(hcr_base + DE), SError);
  797. /* handle single device errors */
  798. if (cereg) {
  799. /*
  800. * clear the command error, also clears queue to the device
  801. * in error, and we can (re)issue commands to this device.
  802. * When a device is in error all commands queued into the
  803. * host controller and at the device are considered aborted
  804. * and the queue for that device is stopped. Now, after
  805. * clearing the device error, we can issue commands to the
  806. * device to interrogate it to find the source of the error.
  807. */
  808. dereg = ioread32(hcr_base + DE);
  809. iowrite32(dereg, hcr_base + DE);
  810. iowrite32(cereg, hcr_base + CE);
  811. DPRINTK("single device error, CE=0x%x, DE=0x%x\n",
  812. ioread32(hcr_base + CE), ioread32(hcr_base + DE));
  813. /*
  814. * We should consider this as non fatal error, and TF must
  815. * be updated as done below.
  816. */
  817. err_mask |= AC_ERR_DEV;
  818. }
  819. /* handle fatal errors */
  820. if (hstatus & FATAL_ERROR_DECODE) {
  821. err_mask |= AC_ERR_ATA_BUS;
  822. action |= ATA_EH_SOFTRESET;
  823. /* how will fatal error interrupts be completed ?? */
  824. freeze = 1;
  825. }
  826. /* Handle PHYRDY change notification */
  827. if (hstatus & INT_ON_PHYRDY_CHG) {
  828. DPRINTK("SATA FSL: PHYRDY change indication\n");
  829. /* Setup a soft-reset EH action */
  830. ata_ehi_hotplugged(ehi);
  831. freeze = 1;
  832. }
  833. /* record error info */
  834. qc = ata_qc_from_tag(ap, link->active_tag);
  835. if (qc) {
  836. sata_fsl_cache_taskfile_from_d2h_fis(qc, qc->ap);
  837. qc->err_mask |= err_mask;
  838. } else
  839. ehi->err_mask |= err_mask;
  840. ehi->action |= action;
  841. ehi->serror |= SError;
  842. /* freeze or abort */
  843. if (freeze)
  844. ata_port_freeze(ap);
  845. else
  846. ata_port_abort(ap);
  847. }
  848. static void sata_fsl_qc_complete(struct ata_queued_cmd *qc)
  849. {
  850. if (qc->flags & ATA_QCFLAG_RESULT_TF) {
  851. DPRINTK("xx_qc_complete called\n");
  852. sata_fsl_cache_taskfile_from_d2h_fis(qc, qc->ap);
  853. }
  854. }
  855. static void sata_fsl_host_intr(struct ata_port *ap)
  856. {
  857. struct ata_link *link = &ap->link;
  858. struct sata_fsl_host_priv *host_priv = ap->host->private_data;
  859. void __iomem *hcr_base = host_priv->hcr_base;
  860. u32 hstatus, qc_active = 0;
  861. struct ata_queued_cmd *qc;
  862. u32 SError;
  863. hstatus = ioread32(hcr_base + HSTATUS);
  864. sata_fsl_scr_read(ap, SCR_ERROR, &SError);
  865. if (unlikely(SError & 0xFFFF0000)) {
  866. DPRINTK("serror @host_intr : 0x%x\n", SError);
  867. sata_fsl_error_intr(ap);
  868. }
  869. if (unlikely(hstatus & INT_ON_ERROR)) {
  870. DPRINTK("error interrupt!!\n");
  871. sata_fsl_error_intr(ap);
  872. return;
  873. }
  874. if (link->sactive) { /* only true for NCQ commands */
  875. int i;
  876. /* Read command completed register */
  877. qc_active = ioread32(hcr_base + CC);
  878. /* clear CC bit, this will also complete the interrupt */
  879. iowrite32(qc_active, hcr_base + CC);
  880. DPRINTK("Status of all queues :\n");
  881. DPRINTK("qc_active/CC = 0x%x, CA = 0x%x, CE=0x%x\n",
  882. qc_active, ioread32(hcr_base + CA),
  883. ioread32(hcr_base + CE));
  884. for (i = 0; i < SATA_FSL_QUEUE_DEPTH; i++) {
  885. if (qc_active & (1 << i)) {
  886. qc = ata_qc_from_tag(ap, i);
  887. if (qc) {
  888. sata_fsl_qc_complete(qc);
  889. ata_qc_complete(qc);
  890. }
  891. DPRINTK
  892. ("completing ncq cmd,tag=%d,CC=0x%x,CA=0x%x\n",
  893. i, ioread32(hcr_base + CC),
  894. ioread32(hcr_base + CA));
  895. }
  896. }
  897. return;
  898. } else if (ap->qc_active) {
  899. iowrite32(1, hcr_base + CC);
  900. qc = ata_qc_from_tag(ap, link->active_tag);
  901. DPRINTK("completing non-ncq cmd, tag=%d,CC=0x%x\n",
  902. link->active_tag, ioread32(hcr_base + CC));
  903. if (qc) {
  904. sata_fsl_qc_complete(qc);
  905. ata_qc_complete(qc);
  906. }
  907. } else {
  908. /* Spurious Interrupt!! */
  909. DPRINTK("spurious interrupt!!, CC = 0x%x\n",
  910. ioread32(hcr_base + CC));
  911. return;
  912. }
  913. }
  914. static irqreturn_t sata_fsl_interrupt(int irq, void *dev_instance)
  915. {
  916. struct ata_host *host = dev_instance;
  917. struct sata_fsl_host_priv *host_priv = host->private_data;
  918. void __iomem *hcr_base = host_priv->hcr_base;
  919. u32 interrupt_enables;
  920. unsigned handled = 0;
  921. struct ata_port *ap;
  922. /* ack. any pending IRQs for this controller/port */
  923. interrupt_enables = ioread32(hcr_base + HSTATUS);
  924. interrupt_enables &= 0x3F;
  925. DPRINTK("interrupt status 0x%x\n", interrupt_enables);
  926. if (!interrupt_enables)
  927. return IRQ_NONE;
  928. spin_lock(&host->lock);
  929. /* Assuming one port per host controller */
  930. ap = host->ports[0];
  931. if (ap) {
  932. sata_fsl_host_intr(ap);
  933. } else {
  934. dev_printk(KERN_WARNING, host->dev,
  935. "interrupt on disabled port 0\n");
  936. }
  937. iowrite32(interrupt_enables, hcr_base + HSTATUS);
  938. handled = 1;
  939. spin_unlock(&host->lock);
  940. return IRQ_RETVAL(handled);
  941. }
  942. /*
  943. * Multiple ports are represented by multiple SATA controllers with
  944. * one port per controller
  945. */
  946. static int sata_fsl_init_controller(struct ata_host *host)
  947. {
  948. struct sata_fsl_host_priv *host_priv = host->private_data;
  949. void __iomem *hcr_base = host_priv->hcr_base;
  950. u32 temp;
  951. /*
  952. * NOTE : We cannot bring the controller online before setting
  953. * the CHBA, hence main controller initialization is done as
  954. * part of the port_start() callback
  955. */
  956. /* ack. any pending IRQs for this controller/port */
  957. temp = ioread32(hcr_base + HSTATUS);
  958. if (temp & 0x3F)
  959. iowrite32((temp & 0x3F), hcr_base + HSTATUS);
  960. /* Keep interrupts disabled on the controller */
  961. temp = ioread32(hcr_base + HCONTROL);
  962. iowrite32((temp & ~0x3F), hcr_base + HCONTROL);
  963. /* Disable interrupt coalescing control(icc), for the moment */
  964. DPRINTK("icc = 0x%x\n", ioread32(hcr_base + ICC));
  965. iowrite32(0x01000000, hcr_base + ICC);
  966. /* clear error registers, SError is cleared by libATA */
  967. iowrite32(0x00000FFFF, hcr_base + CE);
  968. iowrite32(0x00000FFFF, hcr_base + DE);
  969. /* initially assuming no Port multiplier, set CQPMP to 0 */
  970. iowrite32(0x0, hcr_base + CQPMP);
  971. /*
  972. * host controller will be brought on-line, during xx_port_start()
  973. * callback, that should also initiate the OOB, COMINIT sequence
  974. */
  975. DPRINTK("HStatus = 0x%x\n", ioread32(hcr_base + HSTATUS));
  976. DPRINTK("HControl = 0x%x\n", ioread32(hcr_base + HCONTROL));
  977. return 0;
  978. }
  979. /*
  980. * scsi mid-layer and libata interface structures
  981. */
  982. static struct scsi_host_template sata_fsl_sht = {
  983. .module = THIS_MODULE,
  984. .name = "sata_fsl",
  985. .ioctl = ata_scsi_ioctl,
  986. .queuecommand = ata_scsi_queuecmd,
  987. .change_queue_depth = ata_scsi_change_queue_depth,
  988. .can_queue = SATA_FSL_QUEUE_DEPTH,
  989. .this_id = ATA_SHT_THIS_ID,
  990. .sg_tablesize = SATA_FSL_MAX_PRD_USABLE,
  991. .cmd_per_lun = ATA_SHT_CMD_PER_LUN,
  992. .emulated = ATA_SHT_EMULATED,
  993. .use_clustering = ATA_SHT_USE_CLUSTERING,
  994. .proc_name = "sata_fsl",
  995. .dma_boundary = ATA_DMA_BOUNDARY,
  996. .slave_configure = ata_scsi_slave_config,
  997. .slave_destroy = ata_scsi_slave_destroy,
  998. .bios_param = ata_std_bios_param,
  999. #ifdef CONFIG_PM
  1000. .suspend = ata_scsi_device_suspend,
  1001. .resume = ata_scsi_device_resume,
  1002. #endif
  1003. };
  1004. static const struct ata_port_operations sata_fsl_ops = {
  1005. .check_status = sata_fsl_check_status,
  1006. .check_altstatus = sata_fsl_check_status,
  1007. .dev_select = ata_noop_dev_select,
  1008. .tf_read = sata_fsl_tf_read,
  1009. .qc_prep = sata_fsl_qc_prep,
  1010. .qc_issue = sata_fsl_qc_issue,
  1011. .irq_clear = sata_fsl_irq_clear,
  1012. .scr_read = sata_fsl_scr_read,
  1013. .scr_write = sata_fsl_scr_write,
  1014. .freeze = sata_fsl_freeze,
  1015. .thaw = sata_fsl_thaw,
  1016. .error_handler = sata_fsl_error_handler,
  1017. .post_internal_cmd = sata_fsl_post_internal_cmd,
  1018. .port_start = sata_fsl_port_start,
  1019. .port_stop = sata_fsl_port_stop,
  1020. };
  1021. static const struct ata_port_info sata_fsl_port_info[] = {
  1022. {
  1023. .flags = SATA_FSL_HOST_FLAGS,
  1024. .link_flags = SATA_FSL_HOST_LFLAGS,
  1025. .pio_mask = 0x1f, /* pio 0-4 */
  1026. .udma_mask = 0x7f, /* udma 0-6 */
  1027. .port_ops = &sata_fsl_ops,
  1028. },
  1029. };
  1030. static int sata_fsl_probe(struct of_device *ofdev,
  1031. const struct of_device_id *match)
  1032. {
  1033. int retval = 0;
  1034. void __iomem *hcr_base = NULL;
  1035. void __iomem *ssr_base = NULL;
  1036. void __iomem *csr_base = NULL;
  1037. struct sata_fsl_host_priv *host_priv = NULL;
  1038. struct resource *r;
  1039. int irq;
  1040. struct ata_host *host;
  1041. struct ata_port_info pi = sata_fsl_port_info[0];
  1042. const struct ata_port_info *ppi[] = { &pi, NULL };
  1043. dev_printk(KERN_INFO, &ofdev->dev,
  1044. "Sata FSL Platform/CSB Driver init\n");
  1045. r = kmalloc(sizeof(struct resource), GFP_KERNEL);
  1046. hcr_base = of_iomap(ofdev->node, 0);
  1047. if (!hcr_base)
  1048. goto error_exit_with_cleanup;
  1049. ssr_base = hcr_base + 0x100;
  1050. csr_base = hcr_base + 0x140;
  1051. DPRINTK("@reset i/o = 0x%x\n", ioread32(csr_base + TRANSCFG));
  1052. DPRINTK("sizeof(cmd_desc) = %d\n", sizeof(struct command_desc));
  1053. DPRINTK("sizeof(#define cmd_desc) = %d\n", SATA_FSL_CMD_DESC_SIZE);
  1054. host_priv = kzalloc(sizeof(struct sata_fsl_host_priv), GFP_KERNEL);
  1055. if (!host_priv)
  1056. goto error_exit_with_cleanup;
  1057. host_priv->hcr_base = hcr_base;
  1058. host_priv->ssr_base = ssr_base;
  1059. host_priv->csr_base = csr_base;
  1060. irq = irq_of_parse_and_map(ofdev->node, 0);
  1061. if (irq < 0) {
  1062. dev_printk(KERN_ERR, &ofdev->dev, "invalid irq from platform\n");
  1063. goto error_exit_with_cleanup;
  1064. }
  1065. host_priv->irq = irq;
  1066. /* allocate host structure */
  1067. host = ata_host_alloc_pinfo(&ofdev->dev, ppi, SATA_FSL_MAX_PORTS);
  1068. /* host->iomap is not used currently */
  1069. host->private_data = host_priv;
  1070. /* setup port(s) */
  1071. host->ports[0]->ioaddr.cmd_addr = host_priv->hcr_base;
  1072. host->ports[0]->ioaddr.scr_addr = host_priv->ssr_base;
  1073. /* initialize host controller */
  1074. sata_fsl_init_controller(host);
  1075. /*
  1076. * Now, register with libATA core, this will also initiate the
  1077. * device discovery process, invoking our port_start() handler &
  1078. * error_handler() to execute a dummy Softreset EH session
  1079. */
  1080. ata_host_activate(host, irq, sata_fsl_interrupt, SATA_FSL_IRQ_FLAG,
  1081. &sata_fsl_sht);
  1082. dev_set_drvdata(&ofdev->dev, host);
  1083. return 0;
  1084. error_exit_with_cleanup:
  1085. if (hcr_base)
  1086. iounmap(hcr_base);
  1087. if (host_priv)
  1088. kfree(host_priv);
  1089. return retval;
  1090. }
  1091. static int sata_fsl_remove(struct of_device *ofdev)
  1092. {
  1093. struct ata_host *host = dev_get_drvdata(&ofdev->dev);
  1094. struct sata_fsl_host_priv *host_priv = host->private_data;
  1095. ata_host_detach(host);
  1096. dev_set_drvdata(&ofdev->dev, NULL);
  1097. irq_dispose_mapping(host_priv->irq);
  1098. iounmap(host_priv->hcr_base);
  1099. kfree(host_priv);
  1100. return 0;
  1101. }
  1102. static struct of_device_id fsl_sata_match[] = {
  1103. {
  1104. .compatible = "fsl,mpc8315-sata",
  1105. },
  1106. {
  1107. .compatible = "fsl,mpc8379-sata",
  1108. },
  1109. {},
  1110. };
  1111. MODULE_DEVICE_TABLE(of, fsl_sata_match);
  1112. static struct of_platform_driver fsl_sata_driver = {
  1113. .name = "fsl-sata",
  1114. .match_table = fsl_sata_match,
  1115. .probe = sata_fsl_probe,
  1116. .remove = sata_fsl_remove,
  1117. };
  1118. static int __init sata_fsl_init(void)
  1119. {
  1120. of_register_platform_driver(&fsl_sata_driver);
  1121. return 0;
  1122. }
  1123. static void __exit sata_fsl_exit(void)
  1124. {
  1125. of_unregister_platform_driver(&fsl_sata_driver);
  1126. }
  1127. MODULE_LICENSE("GPL");
  1128. MODULE_AUTHOR("Ashish Kalra, Freescale Semiconductor");
  1129. MODULE_DESCRIPTION("Freescale 3.0Gbps SATA controller low level driver");
  1130. MODULE_VERSION("1.10");
  1131. module_init(sata_fsl_init);
  1132. module_exit(sata_fsl_exit);