sata_fsl.c 37 KB

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