sata_fsl.c 44 KB

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