sata_fsl.c 43 KB

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