sata_fsl.c 38 KB

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