sata_fsl.c 38 KB

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