sata_fsl.c 35 KB

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