sata_fsl.c 38 KB

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