sata_oxnas.c 67 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477
  1. /*
  2. * sata_oxnas
  3. * A driver to interface the 934 based sata core present in the ox820
  4. * with libata and scsi
  5. * based on sata_oxnas driver by Ma Haijun <mahaijuns@gmail.com>
  6. * based on ox820 sata code by:
  7. * Copyright (c) 2007 Oxford Semiconductor Ltd.
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2, or (at your option)
  12. * any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. */
  19. #include <linux/ata.h>
  20. #include <linux/libata.h>
  21. #include <linux/of_platform.h>
  22. #include <linux/delay.h>
  23. #include <linux/module.h>
  24. #include <linux/slab.h>
  25. #include <linux/spinlock.h>
  26. #include <linux/of_address.h>
  27. #include <linux/of_irq.h>
  28. #include <linux/clk.h>
  29. #include <linux/reset.h>
  30. #include <mach/utils.h>
  31. /* sgdma request structure */
  32. struct sgdma_request {
  33. volatile u32 qualifier;
  34. volatile u32 control;
  35. dma_addr_t src_pa;
  36. dma_addr_t dst_pa;
  37. } __packed __aligned(4);
  38. /* Controller information */
  39. enum {
  40. SATA_OXNAS_MAX_PRD = 254,
  41. SATA_OXNAS_DMA_SIZE = SATA_OXNAS_MAX_PRD *
  42. sizeof(struct ata_bmdma_prd) +
  43. sizeof(struct sgdma_request),
  44. SATA_OXNAS_MAX_PORTS = 2,
  45. /** The different Oxsemi SATA core version numbers */
  46. SATA_OXNAS_CORE_VERSION = 0x1f3,
  47. SATA_OXNAS_IRQ_FLAG = IRQF_SHARED,
  48. SATA_OXNAS_HOST_FLAGS = (ATA_FLAG_SATA | ATA_FLAG_PIO_DMA |
  49. ATA_FLAG_NO_ATAPI /*| ATA_FLAG_NCQ*/),
  50. SATA_OXNAS_QUEUE_DEPTH = 32,
  51. SATA_OXNAS_DMA_BOUNDARY = 0xFFFFFFFF,
  52. };
  53. /*
  54. * SATA Port Registers
  55. */
  56. enum {
  57. /** sata host port register offsets */
  58. ORB1 = 0x00,
  59. ORB2 = 0x04,
  60. ORB3 = 0x08,
  61. ORB4 = 0x0C,
  62. ORB5 = 0x10,
  63. MASTER_STATUS = 0x10,
  64. FIS_CTRL = 0x18,
  65. FIS_DATA = 0x1C,
  66. INT_STATUS = 0x30,
  67. INT_CLEAR = 0x30,
  68. INT_ENABLE = 0x34,
  69. INT_DISABLE = 0x38,
  70. VERSION = 0x3C,
  71. SATA_CONTROL = 0x5C,
  72. SATA_COMMAND = 0x60,
  73. HID_FEATURES = 0x64,
  74. PORT_CONTROL = 0x68,
  75. DRIVE_CONTROL = 0x6C,
  76. /** These registers allow access to the link layer registers
  77. that reside in a different clock domain to the processor bus */
  78. LINK_DATA = 0x70,
  79. LINK_RD_ADDR = 0x74,
  80. LINK_WR_ADDR = 0x78,
  81. LINK_CONTROL = 0x7C,
  82. /* window control */
  83. WIN1LO = 0x80,
  84. WIN1HI = 0x84,
  85. WIN2LO = 0x88,
  86. WIN2HI = 0x8C,
  87. WIN0_CONTROL = 0x90,
  88. };
  89. /** sata port register bits */
  90. enum{
  91. /**
  92. * commands to issue in the master status to tell it to move shadow ,
  93. * registers to the actual device ,
  94. */
  95. SATA_OPCODE_MASK = 0x00000007,
  96. CMD_WRITE_TO_ORB_REGS_NO_COMMAND = 0x4,
  97. CMD_WRITE_TO_ORB_REGS = 0x2,
  98. CMD_SYNC_ESCAPE = 0x7,
  99. CMD_CORE_BUSY = (1 << 7),
  100. CMD_DRIVE_SELECT_SHIFT = 12,
  101. CMD_DRIVE_SELECT_MASK = (0xf << CMD_DRIVE_SELECT_SHIFT),
  102. /** interrupt bits */
  103. INT_END_OF_CMD = 1 << 0,
  104. INT_LINK_SERROR = 1 << 1,
  105. INT_ERROR = 1 << 2,
  106. INT_LINK_IRQ = 1 << 3,
  107. INT_REG_ACCESS_ERR = 1 << 7,
  108. INT_BIST_FIS = 1 << 11,
  109. INT_MASKABLE = INT_END_OF_CMD |
  110. INT_LINK_SERROR |
  111. INT_ERROR |
  112. INT_LINK_IRQ |
  113. INT_REG_ACCESS_ERR |
  114. INT_BIST_FIS,
  115. INT_WANT = INT_END_OF_CMD |
  116. INT_LINK_SERROR |
  117. INT_REG_ACCESS_ERR |
  118. INT_ERROR,
  119. INT_ERRORS = INT_LINK_SERROR |
  120. INT_REG_ACCESS_ERR |
  121. INT_ERROR,
  122. /** raw interrupt bits, unmaskable, but do not generate interrupts */
  123. RAW_END_OF_CMD = INT_END_OF_CMD << 16,
  124. RAW_LINK_SERROR = INT_LINK_SERROR << 16,
  125. RAW_ERROR = INT_ERROR << 16,
  126. RAW_LINK_IRQ = INT_LINK_IRQ << 16,
  127. RAW_REG_ACCESS_ERR = INT_REG_ACCESS_ERR << 16,
  128. RAW_BIST_FIS = INT_BIST_FIS << 16,
  129. RAW_WANT = INT_WANT << 16,
  130. RAW_ERRORS = INT_ERRORS << 16,
  131. /**
  132. * variables to write to the device control register to set the current
  133. * device, ie. master or slave.
  134. */
  135. DR_CON_48 = 2,
  136. DR_CON_28 = 0,
  137. SATA_CTL_ERR_MASK = 0x00000016,
  138. };
  139. /* ATA SGDMA register offsets */
  140. enum {
  141. SGDMA_CONTROL = 0x0,
  142. SGDMA_STATUS = 0x4,
  143. SGDMA_REQUESTPTR = 0x8,
  144. SGDMA_RESETS = 0xC,
  145. SGDMA_CORESIZE = 0x10,
  146. };
  147. /* DMA controller register offsets */
  148. enum {
  149. DMA_CONTROL = 0x0,
  150. DMA_CORESIZE = 0x20,
  151. DMA_CONTROL_RESET = (1 << 12),
  152. };
  153. enum {
  154. /* see DMA core docs for the values. Out means from memory (bus A) out
  155. * to disk (bus B) */
  156. SGDMA_REQCTL0OUT = 0x0497c03d,
  157. /* burst mode disabled when no micro code used */
  158. SGDMA_REQCTL0IN = 0x0493a3c1,
  159. SGDMA_REQCTL1OUT = 0x0497c07d,
  160. SGDMA_REQCTL1IN = 0x0497a3c5,
  161. SGDMA_CONTROL_NOGO = 0x3e,
  162. SGDMA_CONTROL_GO = SGDMA_CONTROL_NOGO | 1,
  163. SGDMA_ERRORMASK = 0x3f,
  164. SGDMA_BUSY = 0x80,
  165. SGDMA_RESETS_CTRL = 1 << 0,
  166. SGDMA_RESETS_ARBT = 1 << 1,
  167. SGDMA_RESETS_AHB = 1 << 2,
  168. SGDMA_RESETS_ALL = SGDMA_RESETS_CTRL |
  169. SGDMA_RESETS_ARBT |
  170. SGDMA_RESETS_AHB,
  171. /* Final EOTs */
  172. SGDMA_REQQUAL = 0x00220001,
  173. };
  174. /** SATA core register offsets */
  175. enum {
  176. DM_DBG1 = 0x000,
  177. RAID_SET = 0x004,
  178. DM_DBG2 = 0x008,
  179. DATACOUNT_PORT0 = 0x010,
  180. DATACOUNT_PORT1 = 0x014,
  181. CORE_INT_STATUS = 0x030,
  182. CORE_INT_CLEAR = 0x030,
  183. CORE_INT_ENABLE = 0x034,
  184. CORE_INT_DISABLE = 0x038,
  185. CORE_REBUILD_ENABLE = 0x050,
  186. CORE_FAILED_PORT_R = 0x054,
  187. DEVICE_CONTROL = 0x068,
  188. EXCESS = 0x06C,
  189. RAID_SIZE_LOW = 0x070,
  190. RAID_SIZE_HIGH = 0x074,
  191. PORT_ERROR_MASK = 0x078,
  192. IDLE_STATUS = 0x07C,
  193. RAID_CONTROL = 0x090,
  194. DATA_PLANE_CTRL = 0x0AC,
  195. CORE_DATAPLANE_STAT = 0x0b8,
  196. PROC_PC = 0x100,
  197. CONFIG_IN = 0x3d8,
  198. PROC_START = 0x3f0,
  199. PROC_RESET = 0x3f4,
  200. UCODE_STORE = 0x1000,
  201. RAID_WP_BOT_LOW = 0x1FF0,
  202. RAID_WP_BOT_HIGH = 0x1FF4,
  203. RAID_WP_TOP_LOW = 0x1FF8,
  204. RAID_WP_TOP_HIGH = 0x1FFC,
  205. DATA_MUX_RAM0 = 0x8000,
  206. DATA_MUX_RAM1 = 0xA000,
  207. PORT_SIZE = 0x10000,
  208. };
  209. enum {
  210. /* Sata core debug1 register bits */
  211. CORE_PORT0_DATA_DIR_BIT = 20,
  212. CORE_PORT1_DATA_DIR_BIT = 21,
  213. CORE_PORT0_DATA_DIR = 1 << CORE_PORT0_DATA_DIR_BIT,
  214. CORE_PORT1_DATA_DIR = 1 << CORE_PORT1_DATA_DIR_BIT,
  215. /** sata core control register bits */
  216. SCTL_CLR_ERR = 0x00003016,
  217. RAID_CLR_ERR = 0x0000011e,
  218. /* Interrupts direct from the ports */
  219. NORMAL_INTS_WANTED = 0x00000303,
  220. /* shift these left by port number */
  221. COREINT_HOST = 0x00000001,
  222. COREINT_END = 0x00000100,
  223. CORERAW_HOST = COREINT_HOST << 16,
  224. CORERAW_END = COREINT_END << 16,
  225. /* Interrupts from the RAID controller only */
  226. RAID_INTS_WANTED = 0x00008300,
  227. /* The bits in the IDLE_STATUS that, when set indicate an idle core */
  228. IDLE_CORES = (1 << 18) | (1 << 19),
  229. /* Data plane control error-mask mask and bit, these bit in the data
  230. * plane control mask out errors from the ports that prevent the SGDMA
  231. * care from sending an interrupt */
  232. DPC_ERROR_MASK = 0x00000300,
  233. DPC_ERROR_MASK_BIT = 0x00000100,
  234. /* enable jbod micro-code */
  235. DPC_JBOD_UCODE = 1 << 0,
  236. DPC_FIS_SWCH = 1 << 1,
  237. /** Device Control register bits */
  238. DEVICE_CONTROL_DMABT = 1 << 4,
  239. DEVICE_CONTROL_ABORT = 1 << 2,
  240. DEVICE_CONTROL_PAD = 1 << 3,
  241. DEVICE_CONTROL_PADPAT = 1 << 16,
  242. DEVICE_CONTROL_PRTRST = 1 << 8,
  243. DEVICE_CONTROL_RAMRST = 1 << 12,
  244. DEVICE_CONTROL_ATA_ERR_OVERRIDE = 1 << 28,
  245. /** oxsemi HW raid modes */
  246. OXNASSATA_NOTRAID = 0,
  247. OXNASSATA_RAID0 = 1,
  248. OXNASSATA_RAID1 = 2,
  249. /** OX820 specific HW-RAID register values */
  250. RAID_TWODISKS = 3,
  251. UNKNOWN_MODE = ~0,
  252. CONFIG_IN_RESUME = 2,
  253. };
  254. /* SATA PHY Registers */
  255. enum {
  256. PHY_STAT = 0x00,
  257. PHY_DATA = 0x04,
  258. };
  259. enum {
  260. STAT_READ_VALID = (1 << 21),
  261. STAT_CR_ACK = (1 << 20),
  262. STAT_CR_READ = (1 << 19),
  263. STAT_CR_WRITE = (1 << 18),
  264. STAT_CAP_DATA = (1 << 17),
  265. STAT_CAP_ADDR = (1 << 16),
  266. STAT_ACK_ANY = STAT_CR_ACK |
  267. STAT_CR_READ |
  268. STAT_CR_WRITE |
  269. STAT_CAP_DATA |
  270. STAT_CAP_ADDR,
  271. CR_READ_ENABLE = (1 << 16),
  272. CR_WRITE_ENABLE = (1 << 17),
  273. CR_CAP_DATA = (1 << 18),
  274. };
  275. enum {
  276. /* Link layer registers */
  277. SERROR_IRQ_MASK = 5,
  278. };
  279. enum {
  280. OXNAS_SATA_SOFTRESET = 1,
  281. OXNAS_SATA_REINIT = 2,
  282. };
  283. enum {
  284. OXNAS_SATA_UCODE_RAID0,
  285. OXNAS_SATA_UCODE_RAID1,
  286. OXNAS_SATA_UCODE_JBOD,
  287. OXNAS_SATA_UCODE_NONE,
  288. };
  289. enum {
  290. SATA_UNLOCKED,
  291. SATA_WRITER,
  292. SATA_READER,
  293. SATA_REBUILD,
  294. SATA_HWRAID,
  295. SATA_SCSI_STACK
  296. };
  297. typedef irqreturn_t (*oxnas_sata_isr_callback_t)(int, unsigned long, int);
  298. struct sata_oxnas_host_priv {
  299. void __iomem *port_base;
  300. void __iomem *dmactl_base;
  301. void __iomem *sgdma_base;
  302. void __iomem *core_base;
  303. void __iomem *phy_base;
  304. dma_addr_t dma_base;
  305. void __iomem *dma_base_va;
  306. size_t dma_size;
  307. int irq;
  308. int n_ports;
  309. int current_ucode;
  310. u32 port_frozen;
  311. u32 port_in_eh;
  312. struct clk *clk;
  313. struct reset_control *rst_sata;
  314. struct reset_control *rst_link;
  315. struct reset_control *rst_phy;
  316. spinlock_t phy_lock;
  317. spinlock_t core_lock;
  318. int core_locked;
  319. int reentrant_port_no;
  320. int hw_lock_count;
  321. int direct_lock_count;
  322. void *locker_uid;
  323. int current_locker_type;
  324. int scsi_nonblocking_attempts;
  325. oxnas_sata_isr_callback_t isr_callback;
  326. void *isr_arg;
  327. wait_queue_head_t fast_wait_queue;
  328. wait_queue_head_t scsi_wait_queue;
  329. };
  330. struct sata_oxnas_port_priv {
  331. void __iomem *port_base;
  332. void __iomem *dmactl_base;
  333. void __iomem *sgdma_base;
  334. void __iomem *core_base;
  335. struct sgdma_request *sgdma_request;
  336. dma_addr_t sgdma_request_pa;
  337. };
  338. static u8 sata_oxnas_check_status(struct ata_port *ap);
  339. static int sata_oxnas_cleanup(struct ata_host *ah);
  340. static void sata_oxnas_tf_load(struct ata_port *ap,
  341. const struct ata_taskfile *tf);
  342. static void sata_oxnas_irq_on(struct ata_port *ap);
  343. static void sata_oxnas_post_reset_init(struct ata_port *ap);
  344. static int sata_oxnas_acquire_hw(struct ata_port *ap, int may_sleep,
  345. int timeout_jiffies);
  346. static void sata_oxnas_release_hw(struct ata_port *ap);
  347. static const void *HW_LOCKER_UID = (void *)0xdeadbeef;
  348. /***************************************************************************
  349. * ASIC access
  350. ***************************************************************************/
  351. static void wait_cr_ack(void __iomem *phy_base)
  352. {
  353. while ((ioread32(phy_base + PHY_STAT) >> 16) & 0x1f)
  354. ; /* wait for an ack bit to be set */
  355. }
  356. static u16 read_cr(void __iomem *phy_base, u16 address)
  357. {
  358. iowrite32((u32)address, phy_base + PHY_STAT);
  359. wait_cr_ack(phy_base);
  360. iowrite32(CR_READ_ENABLE, phy_base + PHY_DATA);
  361. wait_cr_ack(phy_base);
  362. return (u16)ioread32(phy_base + PHY_STAT);
  363. }
  364. static void write_cr(void __iomem *phy_base, u16 data, u16 address)
  365. {
  366. iowrite32((u32)address, phy_base + PHY_STAT);
  367. wait_cr_ack(phy_base);
  368. iowrite32((data | CR_CAP_DATA), phy_base + PHY_DATA);
  369. wait_cr_ack(phy_base);
  370. iowrite32(CR_WRITE_ENABLE, phy_base + PHY_DATA);
  371. wait_cr_ack(phy_base);
  372. }
  373. #define PH_GAIN 2
  374. #define FR_GAIN 3
  375. #define PH_GAIN_OFFSET 6
  376. #define FR_GAIN_OFFSET 8
  377. #define PH_GAIN_MASK (0x3 << PH_GAIN_OFFSET)
  378. #define FR_GAIN_MASK (0x3 << FR_GAIN_OFFSET)
  379. #define USE_INT_SETTING (1<<5)
  380. void workaround5458(struct ata_host *ah)
  381. {
  382. struct sata_oxnas_host_priv *hd = ah->private_data;
  383. void __iomem *phy_base = hd->phy_base;
  384. u16 rx_control;
  385. unsigned i;
  386. for (i = 0; i < 2; i++) {
  387. rx_control = read_cr(phy_base, 0x201d + (i << 8));
  388. rx_control &= ~(PH_GAIN_MASK | FR_GAIN_MASK);
  389. rx_control |= PH_GAIN << PH_GAIN_OFFSET;
  390. rx_control |= (FR_GAIN << FR_GAIN_OFFSET) | USE_INT_SETTING;
  391. write_cr(phy_base, rx_control, 0x201d+(i<<8));
  392. }
  393. }
  394. /**
  395. * allows access to the link layer registers
  396. * @param link_reg the link layer register to access (oxsemi indexing ie
  397. * 00 = static config, 04 = phy ctrl)
  398. */
  399. void sata_oxnas_link_write(struct ata_port *ap, unsigned int link_reg, u32 val)
  400. {
  401. struct sata_oxnas_port_priv *pd = ap->private_data;
  402. struct sata_oxnas_host_priv *hd = ap->host->private_data;
  403. void __iomem *port_base = pd->port_base;
  404. u32 patience;
  405. unsigned long flags;
  406. DPRINTK("P%d [0x%02x]->0x%08x\n", ap->port_no, link_reg, val);
  407. spin_lock_irqsave(&hd->phy_lock, flags);
  408. iowrite32(val, port_base + LINK_DATA);
  409. /* accessed twice as a work around for a bug in the SATA abp bridge
  410. * hardware (bug 6828) */
  411. iowrite32(link_reg , port_base + LINK_WR_ADDR);
  412. ioread32(port_base + LINK_WR_ADDR);
  413. for (patience = 0x100000; patience > 0; --patience) {
  414. if (ioread32(port_base + LINK_CONTROL) & 0x00000001)
  415. break;
  416. }
  417. spin_unlock_irqrestore(&hd->phy_lock, flags);
  418. }
  419. static int sata_oxnas_scr_write_port(struct ata_port *ap, unsigned int sc_reg,
  420. u32 val)
  421. {
  422. sata_oxnas_link_write(ap, 0x20 + (sc_reg * 4), val);
  423. return 0;
  424. }
  425. static int sata_oxnas_scr_write(struct ata_link *link, unsigned int sc_reg,
  426. u32 val)
  427. {
  428. return sata_oxnas_scr_write_port(link->ap, sc_reg, val);
  429. }
  430. u32 sata_oxnas_link_read(struct ata_port *ap, unsigned int link_reg)
  431. {
  432. struct sata_oxnas_port_priv *pd = ap->private_data;
  433. struct sata_oxnas_host_priv *hd = ap->host->private_data;
  434. void __iomem *port_base = pd->port_base;
  435. u32 result;
  436. u32 patience;
  437. unsigned long flags;
  438. spin_lock_irqsave(&hd->phy_lock, flags);
  439. /* accessed twice as a work around for a bug in the SATA abp bridge
  440. * hardware (bug 6828) */
  441. iowrite32(link_reg, port_base + LINK_RD_ADDR);
  442. ioread32(port_base + LINK_RD_ADDR);
  443. for (patience = 0x100000; patience > 0; --patience) {
  444. if (ioread32(port_base + LINK_CONTROL) & 0x00000001)
  445. break;
  446. }
  447. if (patience == 0)
  448. DPRINTK("link read timed out for port %d\n", ap->port_no);
  449. result = ioread32(port_base + LINK_DATA);
  450. spin_unlock_irqrestore(&hd->phy_lock, flags);
  451. return result;
  452. }
  453. static int sata_oxnas_scr_read_port(struct ata_port *ap, unsigned int sc_reg,
  454. u32 *val)
  455. {
  456. *val = sata_oxnas_link_read(ap, 0x20 + (sc_reg*4));
  457. return 0;
  458. }
  459. static int sata_oxnas_scr_read(struct ata_link *link,
  460. unsigned int sc_reg, u32 *val)
  461. {
  462. return sata_oxnas_scr_read_port(link->ap, sc_reg, val);
  463. }
  464. /**
  465. * sata_oxnas_irq_clear is called during probe just before the interrupt handler is
  466. * registered, to be sure hardware is quiet. It clears and masks interrupt bits
  467. * in the SATA core.
  468. *
  469. * @param ap hardware with the registers in
  470. */
  471. static void sata_oxnas_irq_clear(struct ata_port *ap)
  472. {
  473. struct sata_oxnas_port_priv *port_priv = ap->private_data;
  474. /* clear pending interrupts */
  475. iowrite32(~0, port_priv->port_base + INT_CLEAR);
  476. iowrite32(COREINT_END, port_priv->core_base + CORE_INT_CLEAR);
  477. }
  478. /**
  479. * qc_issue is used to make a command active, once the hardware and S/G tables
  480. * have been prepared. IDE BMDMA drivers use the helper function
  481. * ata_qc_issue_prot() for taskfile protocol-based dispatch. More advanced
  482. * drivers roll their own ->qc_issue implementation, using this as the
  483. * "issue new ATA command to hardware" hook.
  484. * @param qc the queued command to issue
  485. */
  486. static unsigned int sata_oxnas_qc_issue(struct ata_queued_cmd *qc)
  487. {
  488. struct sata_oxnas_port_priv *pd = qc->ap->private_data;
  489. struct sata_oxnas_host_priv *hd = qc->ap->host->private_data;
  490. void __iomem *port_base = pd->port_base;
  491. void __iomem *core_base = pd->core_base;
  492. int port_no = qc->ap->port_no;
  493. int no_microcode = (hd->current_ucode == UNKNOWN_MODE);
  494. u32 reg;
  495. /* check the core is idle */
  496. if (ioread32(port_base + SATA_COMMAND) & CMD_CORE_BUSY) {
  497. int count = 0;
  498. DPRINTK("core busy for a command on port %d\n",
  499. qc->ap->port_no);
  500. do {
  501. mdelay(1);
  502. if (++count > 100) {
  503. DPRINTK("core busy for a command on port %d\n",
  504. qc->ap->port_no);
  505. /* CrazyDumpDebug(); */
  506. sata_oxnas_cleanup(qc->ap->host);
  507. }
  508. } while (ioread32(port_base + SATA_COMMAND) & CMD_CORE_BUSY);
  509. }
  510. /* enable passing of error signals to DMA sub-core by clearing the
  511. * appropriate bit */
  512. reg = ioread32(core_base + DATA_PLANE_CTRL);
  513. if (no_microcode)
  514. reg |= (DPC_ERROR_MASK_BIT | (DPC_ERROR_MASK_BIT << 1));
  515. reg &= ~(DPC_ERROR_MASK_BIT << port_no);
  516. iowrite32(reg, core_base + DATA_PLANE_CTRL);
  517. /* Disable all interrupts for ports and RAID controller */
  518. iowrite32(~0, port_base + INT_DISABLE);
  519. /* Disable all interrupts for core */
  520. iowrite32(~0, core_base + CORE_INT_DISABLE);
  521. wmb();
  522. /* Load the command settings into the orb registers */
  523. sata_oxnas_tf_load(qc->ap, &qc->tf);
  524. /* both pio and dma commands use dma */
  525. if (ata_is_dma(qc->tf.protocol) || ata_is_pio(qc->tf.protocol)) {
  526. /* Start the DMA */
  527. iowrite32(SGDMA_CONTROL_GO, pd->sgdma_base + SGDMA_CONTROL);
  528. wmb();
  529. }
  530. /* enable End of command interrupt */
  531. iowrite32(INT_WANT, port_base + INT_ENABLE);
  532. iowrite32(COREINT_END, core_base + CORE_INT_ENABLE);
  533. wmb();
  534. /* Start the command */
  535. reg = ioread32(port_base + SATA_COMMAND);
  536. reg &= ~SATA_OPCODE_MASK;
  537. reg |= CMD_WRITE_TO_ORB_REGS;
  538. iowrite32(reg , port_base + SATA_COMMAND);
  539. wmb();
  540. return 0;
  541. }
  542. /**
  543. * Will schedule the libATA error handler on the premise that there has
  544. * been a hotplug event on the port specified
  545. */
  546. void sata_oxnas_checkforhotplug(struct ata_port *ap)
  547. {
  548. DPRINTK("ENTER\n");
  549. ata_ehi_hotplugged(&ap->link.eh_info);
  550. ata_port_freeze(ap);
  551. }
  552. /**************************************************************************/
  553. /* Locking */
  554. /**************************************************************************/
  555. /**
  556. * The underlying function that controls access to the sata core
  557. *
  558. * @return non-zero indicates that you have acquired exclusive access to the
  559. * sata core.
  560. */
  561. static int __acquire_sata_core(
  562. struct ata_host *ah,
  563. int port_no,
  564. oxnas_sata_isr_callback_t callback,
  565. void *arg,
  566. int may_sleep,
  567. int timeout_jiffies,
  568. int hw_access,
  569. void *uid,
  570. int locker_type)
  571. {
  572. unsigned long end = jiffies + timeout_jiffies;
  573. int acquired = 0;
  574. unsigned long flags;
  575. int timed_out = 0;
  576. struct sata_oxnas_host_priv *hd;
  577. DEFINE_WAIT(wait);
  578. if (!ah)
  579. return acquired;
  580. hd = ah->private_data;
  581. spin_lock_irqsave(&hd->core_lock, flags);
  582. DPRINTK("Entered uid %p, port %d, h/w count %d, d count %d, "
  583. "callback %p, hw_access %d, core_locked %d, "
  584. "reentrant_port_no %d, isr_callback %p\n",
  585. uid, port_no, hd->hw_lock_count, hd->direct_lock_count,
  586. callback, hw_access, hd->core_locked, hd->reentrant_port_no,
  587. hd->isr_callback);
  588. while (!timed_out) {
  589. if (hd->core_locked ||
  590. (!hw_access && hd->scsi_nonblocking_attempts)) {
  591. /* Can only allow access if from SCSI/SATA stack and if
  592. * reentrant access is allowed and this access is to the
  593. * same port for which the lock is current held
  594. */
  595. if (hw_access && (port_no == hd->reentrant_port_no)) {
  596. BUG_ON(!hd->hw_lock_count);
  597. ++(hd->hw_lock_count);
  598. DPRINTK("Allow SCSI/SATA re-entrant access to "
  599. "uid %p port %d\n", uid, port_no);
  600. acquired = 1;
  601. break;
  602. } else if (!hw_access) {
  603. if ((locker_type == SATA_READER) &&
  604. (hd->current_locker_type == SATA_READER)) {
  605. WARN(1,
  606. "Already locked by reader, "
  607. "uid %p, locker_uid %p, "
  608. "port %d, h/w count %d, "
  609. "d count %d, hw_access %d\n",
  610. uid, hd->locker_uid, port_no,
  611. hd->hw_lock_count,
  612. hd->direct_lock_count,
  613. hw_access);
  614. goto check_uid;
  615. }
  616. if ((locker_type != SATA_READER) &&
  617. (locker_type != SATA_WRITER)) {
  618. goto wait_for_lock;
  619. }
  620. check_uid:
  621. WARN(uid == hd->locker_uid, "Attempt to lock "
  622. "by locker type %d uid %p, already "
  623. "locked by locker type %d with "
  624. "locker_uid %p, port %d, "
  625. "h/w count %d, d count %d, "
  626. "hw_access %d\n", locker_type, uid,
  627. hd->current_locker_type,
  628. hd->locker_uid, port_no,
  629. hd->hw_lock_count,
  630. hd->direct_lock_count, hw_access);
  631. }
  632. } else {
  633. WARN(hd->hw_lock_count || hd->direct_lock_count,
  634. "Core unlocked but counts non-zero: uid %p, "
  635. "locker_uid %p, port %d, h/w count %d, "
  636. "d count %d, hw_access %d\n", uid,
  637. hd->locker_uid, port_no, hd->hw_lock_count,
  638. hd->direct_lock_count, hw_access);
  639. BUG_ON(hd->current_locker_type != SATA_UNLOCKED);
  640. WARN(hd->locker_uid, "Attempt to lock uid %p when "
  641. "locker_uid %p is non-zero, port %d, "
  642. "h/w count %d, d count %d, hw_access %d\n",
  643. uid, hd->locker_uid, port_no, hd->hw_lock_count,
  644. hd->direct_lock_count, hw_access);
  645. if (!hw_access) {
  646. /* Direct access attempting to acquire
  647. * non-contented lock
  648. */
  649. /* Must have callback for direct access */
  650. BUG_ON(!callback);
  651. /* Sanity check lock state */
  652. BUG_ON(hd->reentrant_port_no != -1);
  653. hd->isr_callback = callback;
  654. hd->isr_arg = arg;
  655. ++(hd->direct_lock_count);
  656. hd->current_locker_type = locker_type;
  657. } else {
  658. /* SCSI/SATA attempting to acquire
  659. * non-contented lock
  660. */
  661. /* No callbacks for SCSI/SATA access */
  662. BUG_ON(callback);
  663. /* No callback args for SCSI/SATA access */
  664. BUG_ON(arg);
  665. /* Sanity check lock state */
  666. BUG_ON(hd->isr_callback);
  667. BUG_ON(hd->isr_arg);
  668. ++(hd->hw_lock_count);
  669. hd->reentrant_port_no = port_no;
  670. hd->current_locker_type = SATA_SCSI_STACK;
  671. }
  672. hd->core_locked = 1;
  673. hd->locker_uid = uid;
  674. acquired = 1;
  675. break;
  676. }
  677. wait_for_lock:
  678. if (!may_sleep) {
  679. DPRINTK("Denying for uid %p locker_type %d, "
  680. "hw_access %d, port %d, current_locker_type %d as "
  681. "cannot sleep\n", uid, locker_type, hw_access, port_no,
  682. hd->current_locker_type);
  683. if (hw_access)
  684. ++(hd->scsi_nonblocking_attempts);
  685. break;
  686. }
  687. /* Core is locked and we're allowed to sleep, so wait to be
  688. * awoken when the core is unlocked
  689. */
  690. for (;;) {
  691. prepare_to_wait(hw_access ? &hd->scsi_wait_queue :
  692. &hd->fast_wait_queue,
  693. &wait, TASK_UNINTERRUPTIBLE);
  694. if (!hd->core_locked &&
  695. !(!hw_access && hd->scsi_nonblocking_attempts)) {
  696. /* We're going to use variables that will have
  697. * been changed by the waker prior to clearing
  698. * core_locked so we need to ensure we see
  699. * changes to all those variables
  700. */
  701. smp_rmb();
  702. break;
  703. }
  704. if (time_after(jiffies, end)) {
  705. printk(KERN_WARNING "__acquire_sata_core() "
  706. "uid %p failing for port %d timed out, "
  707. "locker_uid %p, h/w count %d, "
  708. "d count %d, callback %p, hw_access %d, "
  709. "core_locked %d, reentrant_port_no %d, "
  710. "isr_callback %p, isr_arg %p\n", uid,
  711. port_no, hd->locker_uid,
  712. hd->hw_lock_count,
  713. hd->direct_lock_count, callback,
  714. hw_access, hd->core_locked,
  715. hd->reentrant_port_no, hd->isr_callback,
  716. hd->isr_arg);
  717. timed_out = 1;
  718. break;
  719. }
  720. spin_unlock_irqrestore(&hd->core_lock, flags);
  721. if (!schedule_timeout(4*HZ)) {
  722. printk(KERN_INFO "__acquire_sata_core() uid %p, "
  723. "locker_uid %p, timed-out of "
  724. "schedule(), checking overall timeout\n",
  725. uid, hd->locker_uid);
  726. }
  727. spin_lock_irqsave(&hd->core_lock, flags);
  728. }
  729. finish_wait(hw_access ? &hd->scsi_wait_queue :
  730. &hd->fast_wait_queue, &wait);
  731. }
  732. if (hw_access && acquired) {
  733. if (hd->scsi_nonblocking_attempts)
  734. hd->scsi_nonblocking_attempts = 0;
  735. /* Wake any other SCSI/SATA waiters so they can get reentrant
  736. * access to the same port if appropriate. This is because if
  737. * the SATA core is locked by fast access, or SCSI/SATA access
  738. * to other port, then can have >1 SCSI/SATA waiters on the wait
  739. * list so want to give reentrant accessors a chance to get
  740. * access ASAP
  741. */
  742. if (!list_empty(&hd->scsi_wait_queue.task_list))
  743. wake_up(&hd->scsi_wait_queue);
  744. }
  745. DPRINTK("Leaving uid %p with acquired = %d, port %d, callback %p\n",
  746. uid, acquired, port_no, callback);
  747. spin_unlock_irqrestore(&hd->core_lock, flags);
  748. return acquired;
  749. }
  750. int sata_core_has_fast_waiters(struct ata_host *ah)
  751. {
  752. int has_waiters;
  753. unsigned long flags;
  754. struct sata_oxnas_host_priv *hd = ah->private_data;
  755. spin_lock_irqsave(&hd->core_lock, flags);
  756. has_waiters = !list_empty(&hd->fast_wait_queue.task_list);
  757. spin_unlock_irqrestore(&hd->core_lock, flags);
  758. return has_waiters;
  759. }
  760. EXPORT_SYMBOL(sata_core_has_fast_waiters);
  761. int sata_core_has_scsi_waiters(struct ata_host *ah)
  762. {
  763. int has_waiters;
  764. unsigned long flags;
  765. struct sata_oxnas_host_priv *hd = ah->private_data;
  766. spin_lock_irqsave(&hd->core_lock, flags);
  767. has_waiters = hd->scsi_nonblocking_attempts ||
  768. !list_empty(&hd->scsi_wait_queue.task_list);
  769. spin_unlock_irqrestore(&hd->core_lock, flags);
  770. return has_waiters;
  771. }
  772. EXPORT_SYMBOL(sata_core_has_scsi_waiters);
  773. /*
  774. * ata_port operation to gain ownership of the SATA hardware prior to issuing
  775. * a command against a SATA host. Allows any number of users of the port against
  776. * which the lock was first acquired, thus enforcing that only one SATA core
  777. * port may be operated on at once.
  778. */
  779. static int sata_oxnas_acquire_hw(
  780. struct ata_port *ap,
  781. int may_sleep,
  782. int timeout_jiffies)
  783. {
  784. return __acquire_sata_core(ap->host, ap->port_no, NULL, 0, may_sleep,
  785. timeout_jiffies, 1, (void *)HW_LOCKER_UID,
  786. SATA_SCSI_STACK);
  787. }
  788. /*
  789. * operation to release ownership of the SATA hardware
  790. */
  791. static void sata_oxnas_release_hw(struct ata_port *ap)
  792. {
  793. unsigned long flags;
  794. int released = 0;
  795. struct sata_oxnas_host_priv *hd = ap->host->private_data;
  796. spin_lock_irqsave(&hd->core_lock, flags);
  797. DPRINTK("Entered port_no = %d, h/w count %d, d count %d, "
  798. "core locked = %d, reentrant_port_no = %d, isr_callback %p\n",
  799. ap->port_no, hd->hw_lock_count, hd->direct_lock_count,
  800. hd->core_locked, hd->reentrant_port_no, hd->isr_callback);
  801. if (!hd->core_locked) {
  802. /* Nobody holds the SATA lock */
  803. printk(KERN_WARNING "Nobody holds SATA lock, port_no %d\n",
  804. ap->port_no);
  805. released = 1;
  806. } else if (!hd->hw_lock_count) {
  807. /* SCSI/SATA has released without holding the lock */
  808. printk(KERN_WARNING "SCSI/SATA does not hold SATA lock, "
  809. "port_no %d\n", ap->port_no);
  810. } else {
  811. /* Trap incorrect usage */
  812. BUG_ON(hd->reentrant_port_no == -1);
  813. BUG_ON(ap->port_no != hd->reentrant_port_no);
  814. BUG_ON(hd->direct_lock_count);
  815. BUG_ON(hd->current_locker_type != SATA_SCSI_STACK);
  816. WARN(!hd->locker_uid || (hd->locker_uid != HW_LOCKER_UID),
  817. "Invalid locker uid %p, h/w count %d, d count %d, "
  818. "reentrant_port_no %d, core_locked %d, "
  819. "isr_callback %p\n", hd->locker_uid, hd->hw_lock_count,
  820. hd->direct_lock_count, hd->reentrant_port_no,
  821. hd->core_locked, hd->isr_callback);
  822. if (--(hd->hw_lock_count)) {
  823. DPRINTK("Still nested port_no %d\n", ap->port_no);
  824. } else {
  825. DPRINTK("Release port_no %d\n", ap->port_no);
  826. hd->reentrant_port_no = -1;
  827. hd->isr_callback = NULL;
  828. hd->current_locker_type = SATA_UNLOCKED;
  829. hd->locker_uid = 0;
  830. hd->core_locked = 0;
  831. released = 1;
  832. wake_up(!list_empty(&hd->scsi_wait_queue.task_list) ?
  833. &hd->scsi_wait_queue :
  834. &hd->fast_wait_queue);
  835. }
  836. }
  837. DPRINTK("Leaving, port_no %d, count %d\n", ap->port_no,
  838. hd->hw_lock_count);
  839. spin_unlock_irqrestore(&hd->core_lock, flags);
  840. /* CONFIG_SATA_OX820_DIRECT_HWRAID */
  841. /* if (released)
  842. ox820hwraid_restart_queue();
  843. } */
  844. }
  845. static inline int sata_oxnas_is_host_frozen(struct ata_host *ah)
  846. {
  847. struct sata_oxnas_host_priv *hd = ah->private_data;
  848. smp_rmb();
  849. return hd->port_in_eh || hd->port_frozen;
  850. }
  851. static inline u32 sata_oxnas_hostportbusy(struct ata_port *ap)
  852. {
  853. struct sata_oxnas_host_priv *hd = ap->host->private_data;
  854. return (ioread32(hd->port_base + SATA_COMMAND) & CMD_CORE_BUSY) ||
  855. (hd->n_ports > 1 &&
  856. (ioread32(hd->port_base + PORT_SIZE + SATA_COMMAND) &
  857. CMD_CORE_BUSY));
  858. }
  859. static inline u32 sata_oxnas_hostdmabusy(struct ata_port *ap)
  860. {
  861. struct sata_oxnas_port_priv *pd = ap->private_data;
  862. return ioread32(pd->sgdma_base + SGDMA_STATUS) & SGDMA_BUSY;
  863. }
  864. /**
  865. * Turns on the cores clock and resets it
  866. */
  867. static void sata_oxnas_reset_core(struct ata_host *ah)
  868. {
  869. struct sata_oxnas_host_priv *host_priv = ah->private_data;
  870. int n;
  871. DPRINTK("ENTER\n");
  872. clk_prepare_enable(host_priv->clk);
  873. reset_control_assert(host_priv->rst_sata);
  874. reset_control_assert(host_priv->rst_link);
  875. reset_control_assert(host_priv->rst_phy);
  876. udelay(50);
  877. /* un-reset the PHY, then Link and Controller */
  878. reset_control_deassert(host_priv->rst_phy);
  879. udelay(50);
  880. reset_control_deassert(host_priv->rst_sata);
  881. reset_control_deassert(host_priv->rst_link);
  882. udelay(50);
  883. workaround5458(ah);
  884. /* tune for sata compatibility */
  885. sata_oxnas_link_write(ah->ports[0], 0x60, 0x2988);
  886. for (n = 0; n < host_priv->n_ports; n++) {
  887. /* each port in turn */
  888. sata_oxnas_link_write(ah->ports[n], 0x70, 0x55629);
  889. }
  890. udelay(50);
  891. }
  892. /**
  893. * Called after an identify device command has worked out what kind of device
  894. * is on the port
  895. *
  896. * @param port The port to configure
  897. * @param pdev The hardware associated with controlling the port
  898. */
  899. static void sata_oxnas_dev_config(struct ata_device *pdev)
  900. {
  901. struct sata_oxnas_port_priv *pd = pdev->link->ap->private_data;
  902. void __iomem *port_base = pd->port_base;
  903. u32 reg;
  904. DPRINTK("ENTER\n");
  905. /* Set the bits to put the port into 28 or 48-bit node */
  906. reg = ioread32(port_base + DRIVE_CONTROL);
  907. reg &= ~3;
  908. reg |= (pdev->flags & ATA_DFLAG_LBA48) ? DR_CON_48 : DR_CON_28;
  909. iowrite32(reg, port_base + DRIVE_CONTROL);
  910. /* if this is an ATA-6 disk, put port into ATA-5 auto translate mode */
  911. if (pdev->flags & ATA_DFLAG_LBA48) {
  912. reg = ioread32(port_base + PORT_CONTROL);
  913. reg |= 2;
  914. iowrite32(reg, port_base + PORT_CONTROL);
  915. }
  916. }
  917. /**
  918. * called to write a taskfile into the ORB registers
  919. * @param ap hardware with the registers in
  920. * @param tf taskfile to write to the registers
  921. */
  922. static void sata_oxnas_tf_load(struct ata_port *ap,
  923. const struct ata_taskfile *tf)
  924. {
  925. u32 count = 0;
  926. u32 Orb1 = 0;
  927. u32 Orb2 = 0;
  928. u32 Orb3 = 0;
  929. u32 Orb4 = 0;
  930. u32 Command_Reg;
  931. struct sata_oxnas_port_priv *port_priv = ap->private_data;
  932. void __iomem *port_base = port_priv->port_base;
  933. unsigned int is_addr = tf->flags & ATA_TFLAG_ISADDR;
  934. /* wait a maximum of 10ms for the core to be idle */
  935. do {
  936. Command_Reg = ioread32(port_base + SATA_COMMAND);
  937. if (!(Command_Reg & CMD_CORE_BUSY))
  938. break;
  939. count++;
  940. udelay(50);
  941. } while (count < 200);
  942. /* check if the ctl register has interrupts disabled or enabled and
  943. * modify the interrupt enable registers on the ata core as required */
  944. if (tf->ctl & ATA_NIEN) {
  945. /* interrupts disabled */
  946. u32 mask = (COREINT_END << ap->port_no);
  947. iowrite32(mask, port_priv->core_base + CORE_INT_DISABLE);
  948. sata_oxnas_irq_clear(ap);
  949. } else {
  950. sata_oxnas_irq_on(ap);
  951. }
  952. Orb2 |= (tf->command) << 24;
  953. /* write 48 or 28 bit tf parameters */
  954. if (is_addr) {
  955. /* set LBA bit as it's an address */
  956. Orb1 |= (tf->device & ATA_LBA) << 24;
  957. if (tf->flags & ATA_TFLAG_LBA48) {
  958. Orb1 |= ATA_LBA << 24;
  959. Orb2 |= (tf->hob_nsect) << 8;
  960. Orb3 |= (tf->hob_lbal) << 24;
  961. Orb4 |= (tf->hob_lbam) << 0;
  962. Orb4 |= (tf->hob_lbah) << 8;
  963. Orb4 |= (tf->hob_feature) << 16;
  964. } else {
  965. Orb3 |= (tf->device & 0xf) << 24;
  966. }
  967. /* write 28-bit lba */
  968. Orb2 |= (tf->nsect) << 0;
  969. Orb2 |= (tf->feature) << 16;
  970. Orb3 |= (tf->lbal) << 0;
  971. Orb3 |= (tf->lbam) << 8;
  972. Orb3 |= (tf->lbah) << 16;
  973. Orb4 |= (tf->ctl) << 24;
  974. }
  975. if (tf->flags & ATA_TFLAG_DEVICE)
  976. Orb1 |= (tf->device) << 24;
  977. ap->last_ctl = tf->ctl;
  978. /* write values to registers */
  979. iowrite32(Orb1, port_base + ORB1);
  980. iowrite32(Orb2, port_base + ORB2);
  981. iowrite32(Orb3, port_base + ORB3);
  982. iowrite32(Orb4, port_base + ORB4);
  983. }
  984. void sata_oxnas_set_mode(struct ata_host *ah, u32 mode, u32 force)
  985. {
  986. struct sata_oxnas_host_priv *host_priv = ah->private_data;
  987. void __iomem *core_base = host_priv->core_base;
  988. unsigned int *src;
  989. void __iomem *dst;
  990. unsigned int progmicrocode = 0;
  991. unsigned int changeparameters = 0;
  992. u32 previous_mode;
  993. /* these micro-code programs _should_ include the version word */
  994. /* JBOD */
  995. static const unsigned int jbod[] = {
  996. 0x07B400AC, 0x0228A280, 0x00200001, 0x00204002, 0x00224001,
  997. 0x00EE0009, 0x00724901, 0x01A24903, 0x00E40009, 0x00224001,
  998. 0x00621120, 0x0183C908, 0x00E20005, 0x00718908, 0x0198A206,
  999. 0x00621124, 0x0183C908, 0x00E20046, 0x00621104, 0x0183C908,
  1000. 0x00E20015, 0x00EE009D, 0x01A3E301, 0x00E2001B, 0x0183C900,
  1001. 0x00E2001B, 0x00210001, 0x00EE0020, 0x01A3E302, 0x00E2009D,
  1002. 0x0183C901, 0x00E2009D, 0x00210002, 0x0235D700, 0x0208A204,
  1003. 0x0071C908, 0x000F8207, 0x000FC207, 0x0071C920, 0x000F8507,
  1004. 0x000FC507, 0x0228A240, 0x02269A40, 0x00094004, 0x00621104,
  1005. 0x0180C908, 0x00E40031, 0x00621112, 0x01A3C801, 0x00E2002B,
  1006. 0x00294000, 0x0228A220, 0x01A69ABF, 0x002F8000, 0x002FC000,
  1007. 0x0198A204, 0x0001C022, 0x01B1A220, 0x0001C106, 0x00088007,
  1008. 0x0183C903, 0x00E2009D, 0x0228A220, 0x0071890C, 0x0208A206,
  1009. 0x0198A206, 0x0001C022, 0x01B1A220, 0x0001C106, 0x00088007,
  1010. 0x00EE009D, 0x00621104, 0x0183C908, 0x00E2004A, 0x00EE009D,
  1011. 0x01A3C901, 0x00E20050, 0x0021E7FF, 0x0183E007, 0x00E2009D,
  1012. 0x00EE0054, 0x0061600B, 0x0021E7FF, 0x0183C507, 0x00E2009D,
  1013. 0x01A3E301, 0x00E2005A, 0x0183C900, 0x00E2005A, 0x00210001,
  1014. 0x00EE005F, 0x01A3E302, 0x00E20005, 0x0183C901, 0x00E20005,
  1015. 0x00210002, 0x0235D700, 0x0208A204, 0x000F8109, 0x000FC109,
  1016. 0x0071C918, 0x000F8407, 0x000FC407, 0x0001C022, 0x01A1A2BF,
  1017. 0x0001C106, 0x00088007, 0x02269A40, 0x00094004, 0x00621112,
  1018. 0x01A3C801, 0x00E4007F, 0x00621104, 0x0180C908, 0x00E4008D,
  1019. 0x00621128, 0x0183C908, 0x00E2006C, 0x01A3C901, 0x00E2007B,
  1020. 0x0021E7FF, 0x0183E007, 0x00E2007F, 0x00EE006C, 0x0061600B,
  1021. 0x0021E7FF, 0x0183C507, 0x00E4006C, 0x00621111, 0x01A3C801,
  1022. 0x00E2007F, 0x00621110, 0x01A3C801, 0x00E20082, 0x0228A220,
  1023. 0x00621119, 0x01A3C801, 0x00E20086, 0x0001C022, 0x01B1A220,
  1024. 0x0001C106, 0x00088007, 0x0198A204, 0x00294000, 0x01A69ABF,
  1025. 0x002F8000, 0x002FC000, 0x0183C903, 0x00E20005, 0x0228A220,
  1026. 0x0071890C, 0x0208A206, 0x0198A206, 0x0001C022, 0x01B1A220,
  1027. 0x0001C106, 0x00088007, 0x00EE009D, 0x00621128, 0x0183C908,
  1028. 0x00E20005, 0x00621104, 0x0183C908, 0x00E200A6, 0x0062111C,
  1029. 0x0183C908, 0x00E20005, 0x0071890C, 0x0208A206, 0x0198A206,
  1030. 0x00718908, 0x0208A206, 0x00EE0005, ~0
  1031. };
  1032. /* Bi-Modal RAID-0/1 */
  1033. static const unsigned int raid[] = {
  1034. 0x00F20145, 0x00EE20FA, 0x00EE20A7, 0x0001C009, 0x00EE0004,
  1035. 0x00220000, 0x0001000B, 0x037003FF, 0x00700018, 0x037003FE,
  1036. 0x037043FD, 0x00704118, 0x037043FC, 0x01A3D240, 0x00E20017,
  1037. 0x00B3C235, 0x00E40018, 0x0093C104, 0x00E80014, 0x0093C004,
  1038. 0x00E80017, 0x01020000, 0x00274020, 0x00EE0083, 0x0080C904,
  1039. 0x0093C104, 0x00EA0020, 0x0093C103, 0x00EC001F, 0x00220002,
  1040. 0x00924104, 0x0005C009, 0x00EE0058, 0x0093CF04, 0x00E80026,
  1041. 0x00900F01, 0x00600001, 0x00910400, 0x00EE0058, 0x00601604,
  1042. 0x01A00003, 0x00E2002C, 0x01018000, 0x00274040, 0x00EE0083,
  1043. 0x0093CF03, 0x00EC0031, 0x00220003, 0x00924F04, 0x0005C009,
  1044. 0x00810104, 0x00B3C235, 0x00E20037, 0x0022C000, 0x00218210,
  1045. 0x00EE0039, 0x0022C001, 0x00218200, 0x00600401, 0x00A04901,
  1046. 0x00604101, 0x01A0C401, 0x00E20040, 0x00216202, 0x00EE0041,
  1047. 0x00216101, 0x02018506, 0x00EE2141, 0x00904901, 0x00E20049,
  1048. 0x00A00401, 0x00600001, 0x02E0C301, 0x00EE2141, 0x00216303,
  1049. 0x037003EE, 0x01A3C001, 0x00E40105, 0x00250080, 0x00204000,
  1050. 0x002042F1, 0x0004C001, 0x00230001, 0x00100006, 0x02C18605,
  1051. 0x00100006, 0x01A3D502, 0x00E20055, 0x00EE0053, 0x00004009,
  1052. 0x00000004, 0x00B3C235, 0x00E40062, 0x0022C001, 0x0020C000,
  1053. 0x00EE2141, 0x0020C001, 0x00EE2141, 0x00EE006B, 0x0022C000,
  1054. 0x0060D207, 0x00EE2141, 0x00B3C242, 0x00E20069, 0x01A3D601,
  1055. 0x00E2006E, 0x02E0C301, 0x00EE2141, 0x00230001, 0x00301303,
  1056. 0x00EE007B, 0x00218210, 0x01A3C301, 0x00E20073, 0x00216202,
  1057. 0x00EE0074, 0x00216101, 0x02018506, 0x00214000, 0x037003EE,
  1058. 0x01A3C001, 0x00E40108, 0x00230001, 0x00100006, 0x00250080,
  1059. 0x00204000, 0x002042F1, 0x0004C001, 0x00EE007F, 0x0024C000,
  1060. 0x01A3D1F0, 0x00E20088, 0x00230001, 0x00300000, 0x01A3D202,
  1061. 0x00E20085, 0x00EE00A5, 0x00B3C800, 0x00E20096, 0x00218000,
  1062. 0x00924709, 0x0005C009, 0x00B20802, 0x00E40093, 0x037103FD,
  1063. 0x00710418, 0x037103FC, 0x00EE0006, 0x00220000, 0x0001000F,
  1064. 0x00EE0006, 0x00800B0C, 0x00B00001, 0x00204000, 0x00208550,
  1065. 0x00208440, 0x002083E0, 0x00208200, 0x00208100, 0x01008000,
  1066. 0x037083EE, 0x02008212, 0x02008216, 0x01A3C201, 0x00E400A5,
  1067. 0x0100C000, 0x00EE20FA, 0x02800000, 0x00208000, 0x00B24C00,
  1068. 0x00E400AD, 0x00224001, 0x00724910, 0x0005C009, 0x00B3CDC4,
  1069. 0x00E200D5, 0x00B3CD29, 0x00E200D5, 0x00B3CD20, 0x00E200D5,
  1070. 0x00B3CD24, 0x00E200D5, 0x00B3CDC5, 0x00E200D2, 0x00B3CD39,
  1071. 0x00E200D2, 0x00B3CD30, 0x00E200D2, 0x00B3CD34, 0x00E200D2,
  1072. 0x00B3CDCA, 0x00E200CF, 0x00B3CD35, 0x00E200CF, 0x00B3CDC8,
  1073. 0x00E200CC, 0x00B3CD25, 0x00E200CC, 0x00B3CD40, 0x00E200CB,
  1074. 0x00B3CD42, 0x00E200CB, 0x01018000, 0x00EE0083, 0x0025C000,
  1075. 0x036083EE, 0x0000800D, 0x00EE00D8, 0x036083EE, 0x00208035,
  1076. 0x00EE00DA, 0x036083EE, 0x00208035, 0x00EE00DA, 0x00208007,
  1077. 0x036083EE, 0x00208025, 0x036083EF, 0x02400000, 0x01A3D208,
  1078. 0x00E200D8, 0x0067120A, 0x0021C000, 0x0021C224, 0x00220000,
  1079. 0x00404B1C, 0x00600105, 0x00800007, 0x0020C00E, 0x00214000,
  1080. 0x01004000, 0x01A0411F, 0x00404E01, 0x01A3C101, 0x00E200F1,
  1081. 0x00B20800, 0x00E400D8, 0x00220001, 0x0080490B, 0x00B04101,
  1082. 0x0040411C, 0x00EE00E1, 0x02269A01, 0x01020000, 0x02275D80,
  1083. 0x01A3D202, 0x00E200F4, 0x01B75D80, 0x01030000, 0x01B69A01,
  1084. 0x00EE00D8, 0x01A3D204, 0x00E40104, 0x00224000, 0x0020C00E,
  1085. 0x0020001E, 0x00214000, 0x01004000, 0x0212490E, 0x00214001,
  1086. 0x01004000, 0x02400000, 0x00B3D702, 0x00E80112, 0x00EE010E,
  1087. 0x00B3D702, 0x00E80112, 0x00B3D702, 0x00E4010E, 0x00230001,
  1088. 0x00EE0140, 0x00200005, 0x036003EE, 0x00204001, 0x00EE0116,
  1089. 0x00230001, 0x00100006, 0x02C18605, 0x00100006, 0x01A3D1F0,
  1090. 0x00E40083, 0x037003EE, 0x01A3C002, 0x00E20121, 0x0020A300,
  1091. 0x0183D102, 0x00E20124, 0x037003EE, 0x01A00005, 0x036003EE,
  1092. 0x01A0910F, 0x00B3C20F, 0x00E2012F, 0x01A3D502, 0x00E20116,
  1093. 0x01A3C002, 0x00E20116, 0x00B3D702, 0x00E4012C, 0x00300000,
  1094. 0x00EE011F, 0x02C18605, 0x00100006, 0x00EE0116, 0x01A3D1F0,
  1095. 0x00E40083, 0x037003EE, 0x01A3C004, 0x00E20088, 0x00200003,
  1096. 0x036003EE, 0x01A3D502, 0x00E20136, 0x00230001, 0x00B3C101,
  1097. 0x00E4012C, 0x00100006, 0x02C18605, 0x00100006, 0x00204000,
  1098. 0x00EE0116, 0x00100006, 0x01A3D1F0, 0x00E40083, 0x01000000,
  1099. 0x02400000, ~0
  1100. };
  1101. DPRINTK("ENTER: mode:%d, force:%d\n", mode, force);
  1102. if (force)
  1103. previous_mode = UNKNOWN_MODE;
  1104. else
  1105. previous_mode = host_priv->current_ucode;
  1106. if (mode == previous_mode)
  1107. return;
  1108. host_priv->current_ucode = mode;
  1109. /* decide what needs to be done using the STD in my logbook */
  1110. switch (previous_mode) {
  1111. case OXNASSATA_RAID1:
  1112. switch (mode) {
  1113. case OXNASSATA_RAID0:
  1114. changeparameters = 1;
  1115. break;
  1116. case OXNASSATA_NOTRAID:
  1117. changeparameters = 1;
  1118. progmicrocode = 1;
  1119. break;
  1120. }
  1121. break;
  1122. case OXNASSATA_RAID0:
  1123. switch (mode) {
  1124. case OXNASSATA_RAID1:
  1125. changeparameters = 1;
  1126. break;
  1127. case OXNASSATA_NOTRAID:
  1128. changeparameters = 1;
  1129. progmicrocode = 1;
  1130. break;
  1131. }
  1132. break;
  1133. case OXNASSATA_NOTRAID:
  1134. switch (mode) {
  1135. case OXNASSATA_RAID0:
  1136. case OXNASSATA_RAID1:
  1137. changeparameters = 1;
  1138. progmicrocode = 1;
  1139. break;
  1140. }
  1141. break;
  1142. case UNKNOWN_MODE:
  1143. changeparameters = 1;
  1144. progmicrocode = 1;
  1145. break;
  1146. }
  1147. /* no need to reprogram everything if already in the right mode */
  1148. if (progmicrocode) {
  1149. /* reset micro-code processor */
  1150. iowrite32(1, core_base + PROC_RESET);
  1151. wmb();
  1152. /* select micro-code */
  1153. switch (mode) {
  1154. case OXNASSATA_RAID1:
  1155. case OXNASSATA_RAID0:
  1156. VPRINTK("Loading RAID micro-code\n");
  1157. src = (unsigned int *)&raid[1];
  1158. break;
  1159. case OXNASSATA_NOTRAID:
  1160. VPRINTK("Loading JBOD micro-code\n");
  1161. src = (unsigned int *)&jbod[1];
  1162. break;
  1163. default:
  1164. BUG();
  1165. break;
  1166. }
  1167. /* load micro code */
  1168. dst = core_base + UCODE_STORE;
  1169. while (*src != ~0) {
  1170. iowrite32(*src, dst);
  1171. src++;
  1172. dst += sizeof(*src);
  1173. }
  1174. wmb();
  1175. }
  1176. if (changeparameters) {
  1177. u32 reg;
  1178. /* set other mode dependent flags */
  1179. switch (mode) {
  1180. case OXNASSATA_RAID1:
  1181. /* clear JBOD mode */
  1182. reg = ioread32(core_base + DATA_PLANE_CTRL);
  1183. reg |= DPC_JBOD_UCODE;
  1184. reg &= ~DPC_FIS_SWCH;
  1185. iowrite32(reg, core_base + DATA_PLANE_CTRL);
  1186. wmb();
  1187. /* set the hardware up for RAID-1 */
  1188. iowrite32(0, core_base + RAID_WP_BOT_LOW);
  1189. iowrite32(0, core_base + RAID_WP_BOT_HIGH);
  1190. iowrite32(0xffffffff, core_base + RAID_WP_TOP_LOW);
  1191. iowrite32(0x7fffffff, core_base + RAID_WP_TOP_HIGH);
  1192. iowrite32(0, core_base + RAID_SIZE_LOW);
  1193. iowrite32(0, core_base + RAID_SIZE_HIGH);
  1194. wmb();
  1195. break;
  1196. case OXNASSATA_RAID0:
  1197. /* clear JBOD mode */
  1198. reg = ioread32(core_base + DATA_PLANE_CTRL);
  1199. reg |= DPC_JBOD_UCODE;
  1200. reg &= ~DPC_FIS_SWCH;
  1201. iowrite32(reg, core_base + DATA_PLANE_CTRL);
  1202. wmb();
  1203. /* set the hardware up for RAID-1 */
  1204. iowrite32(0, core_base + RAID_WP_BOT_LOW);
  1205. iowrite32(0, core_base + RAID_WP_BOT_HIGH);
  1206. iowrite32(0xffffffff, core_base + RAID_WP_TOP_LOW);
  1207. iowrite32(0x7fffffff, core_base + RAID_WP_TOP_HIGH);
  1208. iowrite32(0xffffffff, core_base + RAID_SIZE_LOW);
  1209. iowrite32(0x7fffffff, core_base + RAID_SIZE_HIGH);
  1210. wmb();
  1211. break;
  1212. case OXNASSATA_NOTRAID:
  1213. /* enable jbod mode */
  1214. reg = ioread32(core_base + DATA_PLANE_CTRL);
  1215. reg &= ~DPC_JBOD_UCODE;
  1216. reg &= ~DPC_FIS_SWCH;
  1217. iowrite32(reg, core_base + DATA_PLANE_CTRL);
  1218. wmb();
  1219. /* start micro-code processor*/
  1220. iowrite32(1, core_base + PROC_START);
  1221. break;
  1222. default:
  1223. reg = ioread32(core_base + DATA_PLANE_CTRL);
  1224. reg |= DPC_JBOD_UCODE;
  1225. reg &= ~DPC_FIS_SWCH;
  1226. iowrite32(reg, core_base + DATA_PLANE_CTRL);
  1227. wmb();
  1228. break;
  1229. }
  1230. }
  1231. }
  1232. /**
  1233. * sends a sync-escape if there is a link present
  1234. */
  1235. static inline void sata_oxnas_send_sync_escape(struct ata_port *ap)
  1236. {
  1237. struct sata_oxnas_port_priv *pd = ap->private_data;
  1238. u32 reg;
  1239. /* read the SSTATUS register and only send a sync escape if there is a
  1240. * link active */
  1241. if ((sata_oxnas_link_read(ap, 0x20) & 3) == 3) {
  1242. reg = ioread32(pd->port_base + SATA_COMMAND);
  1243. reg &= ~SATA_OPCODE_MASK;
  1244. reg |= CMD_SYNC_ESCAPE;
  1245. iowrite32(reg, pd->port_base + SATA_COMMAND);
  1246. }
  1247. }
  1248. /* clears errors */
  1249. static inline void sata_oxnas_clear_CS_error(struct ata_port *ap)
  1250. {
  1251. struct sata_oxnas_port_priv *pd = ap->private_data;
  1252. u32 *base = pd->port_base;
  1253. u32 reg;
  1254. reg = ioread32(base + SATA_CONTROL);
  1255. reg &= SATA_CTL_ERR_MASK;
  1256. iowrite32(reg, base + SATA_CONTROL);
  1257. }
  1258. static inline void sata_oxnas_reset_sgdma(struct ata_port *ap)
  1259. {
  1260. struct sata_oxnas_port_priv *pd = ap->private_data;
  1261. iowrite32(SGDMA_RESETS_CTRL, pd->sgdma_base + SGDMA_RESETS);
  1262. }
  1263. static inline void sata_oxnas_reset_dma(struct ata_port *ap, int assert)
  1264. {
  1265. struct sata_oxnas_port_priv *pd = ap->private_data;
  1266. u32 reg;
  1267. reg = ioread32(pd->dmactl_base + DMA_CONTROL);
  1268. if (assert)
  1269. reg |= DMA_CONTROL_RESET;
  1270. else
  1271. reg &= ~DMA_CONTROL_RESET;
  1272. iowrite32(reg, pd->dmactl_base + DMA_CONTROL);
  1273. };
  1274. /**
  1275. * Clears the error caused by the core's registers being accessed when the
  1276. * core is busy.
  1277. */
  1278. static inline void sata_oxnas_clear_reg_access_error(struct ata_port *ap)
  1279. {
  1280. struct sata_oxnas_port_priv *pd = ap->private_data;
  1281. u32 *base = pd->port_base;
  1282. u32 reg;
  1283. reg = ioread32(base + INT_STATUS);
  1284. DPRINTK("ENTER\n");
  1285. if (reg & INT_REG_ACCESS_ERR) {
  1286. DPRINTK("clearing register access error on port %d\n",
  1287. ap->port_no);
  1288. iowrite32(INT_REG_ACCESS_ERR, base + INT_STATUS);
  1289. }
  1290. reg = ioread32(base + INT_STATUS);
  1291. if (reg & INT_REG_ACCESS_ERR)
  1292. DPRINTK("register access error didn't clear\n");
  1293. }
  1294. static inline void sata_oxnas_clear_sctl_error(struct ata_port *ap)
  1295. {
  1296. struct sata_oxnas_port_priv *pd = ap->private_data;
  1297. u32 *base = pd->port_base;
  1298. u32 reg;
  1299. reg = ioread32(base + SATA_CONTROL);
  1300. reg |= SCTL_CLR_ERR;
  1301. iowrite32(reg, base + SATA_CONTROL);
  1302. }
  1303. static inline void sata_oxnas_clear_raid_error(struct ata_host *ah)
  1304. {
  1305. return;
  1306. };
  1307. /**
  1308. * Clean up all the state machines in the sata core.
  1309. * @return post cleanup action required
  1310. */
  1311. static int sata_oxnas_cleanup(struct ata_host *ah)
  1312. {
  1313. struct sata_oxnas_host_priv *hd = ah->private_data;
  1314. int actions_required = 0;
  1315. int n;
  1316. printk(KERN_INFO "sata_oxnas: resetting SATA core\n");
  1317. /* core not recovering, reset it */
  1318. mdelay(5);
  1319. sata_oxnas_reset_core(ah);
  1320. mdelay(5);
  1321. actions_required |= OXNAS_SATA_REINIT;
  1322. /* Perform any SATA core re-initialisation after reset post reset init
  1323. * needs to be called for both ports as there's one reset for both
  1324. * ports */
  1325. for (n = 0; n < hd->n_ports; n++)
  1326. sata_oxnas_post_reset_init(ah->ports[n]);
  1327. return actions_required;
  1328. }
  1329. /**
  1330. * ata_qc_new - Request an available ATA command, for queueing
  1331. * @ap: Port associated with device @dev
  1332. * @return non zero will refuse a new command, zero will may grant on subject
  1333. * to conditions elsewhere.
  1334. *
  1335. */
  1336. static int sata_oxnas_qc_new(struct ata_port *ap)
  1337. {
  1338. struct sata_oxnas_host_priv *hd = ap->host->private_data;
  1339. DPRINTK("port %d\n", ap->port_no);
  1340. smp_rmb();
  1341. if (hd->port_frozen || hd->port_in_eh)
  1342. return 1;
  1343. else
  1344. return !sata_oxnas_acquire_hw(ap, 0, 0);
  1345. }
  1346. /**
  1347. * releases the lock on the port the command used
  1348. */
  1349. static void sata_oxnas_qc_free(struct ata_queued_cmd *qc)
  1350. {
  1351. DPRINTK("\n");
  1352. sata_oxnas_release_hw(qc->ap);
  1353. }
  1354. static void sata_oxnas_freeze(struct ata_port *ap)
  1355. {
  1356. struct sata_oxnas_host_priv *hd = ap->host->private_data;
  1357. DPRINTK("\n");
  1358. hd->port_frozen |= BIT(ap->port_no);
  1359. smp_wmb();
  1360. }
  1361. static void sata_oxnas_thaw(struct ata_port *ap)
  1362. {
  1363. struct sata_oxnas_host_priv *hd = ap->host->private_data;
  1364. DPRINTK("\n");
  1365. hd->port_frozen &= ~BIT(ap->port_no);
  1366. smp_wmb();
  1367. }
  1368. void sata_oxnas_freeze_host(struct ata_port *ap)
  1369. {
  1370. struct sata_oxnas_host_priv *hd = ap->host->private_data;
  1371. DPRINTK("ENTER\n");
  1372. hd->port_in_eh |= BIT(ap->port_no);
  1373. smp_wmb();
  1374. }
  1375. void sata_oxnas_thaw_host(struct ata_port *ap)
  1376. {
  1377. struct sata_oxnas_host_priv *hd = ap->host->private_data;
  1378. DPRINTK("ENTER\n");
  1379. hd->port_in_eh &= ~BIT(ap->port_no);
  1380. smp_wmb();
  1381. }
  1382. static void sata_oxnas_post_internal_cmd(struct ata_queued_cmd *qc)
  1383. {
  1384. DPRINTK("ENTER\n");
  1385. /* If the core is busy here, make it idle */
  1386. if (qc->flags & ATA_QCFLAG_FAILED)
  1387. sata_oxnas_cleanup(qc->ap->host);
  1388. }
  1389. /**
  1390. * turn on the interrupts
  1391. *
  1392. * @param ap Hardware with the registers in
  1393. */
  1394. static void sata_oxnas_irq_on(struct ata_port *ap)
  1395. {
  1396. struct sata_oxnas_port_priv *pd = ap->private_data;
  1397. u32 mask = (COREINT_END << ap->port_no);
  1398. /* Clear pending interrupts */
  1399. iowrite32(~0, pd->port_base + INT_CLEAR);
  1400. iowrite32(mask, pd->core_base + CORE_INT_STATUS);
  1401. wmb();
  1402. /* enable End of command interrupt */
  1403. iowrite32(INT_WANT, pd->port_base + INT_ENABLE);
  1404. iowrite32(mask, pd->core_base + CORE_INT_ENABLE);
  1405. }
  1406. /** @return true if the port has a cable connected */
  1407. int sata_oxnas_check_link(struct ata_port *ap)
  1408. {
  1409. int reg;
  1410. sata_oxnas_scr_read_port(ap, SCR_STATUS, &reg);
  1411. /* Check for the cable present indicated by SCR status bit-0 set */
  1412. return reg & 0x1;
  1413. }
  1414. /**
  1415. * ata_std_postreset - standard postreset callback
  1416. * @link: the target ata_link
  1417. * @classes: classes of attached devices
  1418. *
  1419. * This function is invoked after a successful reset. Note that
  1420. * the device might have been reset more than once using
  1421. * different reset methods before postreset is invoked.
  1422. *
  1423. * LOCKING:
  1424. * Kernel thread context (may sleep)
  1425. */
  1426. static void sata_oxnas_postreset(struct ata_link *link, unsigned int *classes)
  1427. {
  1428. struct ata_port *ap = link->ap;
  1429. struct sata_oxnas_host_priv *hd = ap->host->private_data;
  1430. unsigned int dev;
  1431. DPRINTK("ENTER\n");
  1432. ata_std_postreset(link, classes);
  1433. /* turn on phy error detection by removing the masks */
  1434. sata_oxnas_link_write(ap->host->ports[0], 0x0c, 0x30003);
  1435. if (hd->n_ports > 1)
  1436. sata_oxnas_link_write(ap->host->ports[1], 0x0c, 0x30003);
  1437. /* bail out if no device is present */
  1438. if (classes[0] == ATA_DEV_NONE && classes[1] == ATA_DEV_NONE) {
  1439. DPRINTK("EXIT, no device\n");
  1440. return;
  1441. }
  1442. /* go through all the devices and configure them */
  1443. for (dev = 0; dev < ATA_MAX_DEVICES; ++dev) {
  1444. if (ap->link.device[dev].class == ATA_DEV_ATA)
  1445. sata_oxnas_dev_config(&(ap->link.device[dev]));
  1446. }
  1447. DPRINTK("EXIT\n");
  1448. }
  1449. /**
  1450. * Called to read the hardware registers / DMA buffers, to
  1451. * obtain the current set of taskfile register values.
  1452. * @param ap hardware with the registers in
  1453. * @param tf taskfile to read the registers into
  1454. */
  1455. static void sata_oxnas_tf_read(struct ata_port *ap, struct ata_taskfile *tf)
  1456. {
  1457. struct sata_oxnas_port_priv *port_priv = ap->private_data;
  1458. void __iomem *port_base = port_priv->port_base;
  1459. /* read the orb registers */
  1460. u32 Orb1 = ioread32(port_base + ORB1);
  1461. u32 Orb2 = ioread32(port_base + ORB2);
  1462. u32 Orb3 = ioread32(port_base + ORB3);
  1463. u32 Orb4 = ioread32(port_base + ORB4);
  1464. /* read common 28/48 bit tf parameters */
  1465. tf->device = (Orb1 >> 24);
  1466. tf->nsect = (Orb2 >> 0);
  1467. tf->feature = (Orb2 >> 16);
  1468. tf->command = sata_oxnas_check_status(ap);
  1469. /* read 48 or 28 bit tf parameters */
  1470. if (tf->flags & ATA_TFLAG_LBA48) {
  1471. tf->hob_nsect = (Orb2 >> 8);
  1472. tf->lbal = (Orb3 >> 0);
  1473. tf->lbam = (Orb3 >> 8);
  1474. tf->lbah = (Orb3 >> 16);
  1475. tf->hob_lbal = (Orb3 >> 24);
  1476. tf->hob_lbam = (Orb4 >> 0);
  1477. tf->hob_lbah = (Orb4 >> 8);
  1478. /* feature ext and control are write only */
  1479. } else {
  1480. /* read 28-bit lba */
  1481. tf->lbal = (Orb3 >> 0);
  1482. tf->lbam = (Orb3 >> 8);
  1483. tf->lbah = (Orb3 >> 16);
  1484. }
  1485. }
  1486. /**
  1487. * Read a result task-file from the sata core registers.
  1488. */
  1489. static bool sata_oxnas_qc_fill_rtf(struct ata_queued_cmd *qc)
  1490. {
  1491. /* Read the most recently received FIS from the SATA core ORB registers
  1492. and convert to an ATA taskfile */
  1493. sata_oxnas_tf_read(qc->ap, &qc->result_tf);
  1494. return true;
  1495. }
  1496. /**
  1497. * Reads the Status ATA shadow register from hardware.
  1498. *
  1499. * @return The status register
  1500. */
  1501. static u8 sata_oxnas_check_status(struct ata_port *ap)
  1502. {
  1503. u32 Reg;
  1504. u8 status;
  1505. struct sata_oxnas_port_priv *port_priv = ap->private_data;
  1506. void __iomem *port_base = port_priv->port_base;
  1507. /* read byte 3 of Orb2 register */
  1508. status = ioread32(port_base + ORB2) >> 24;
  1509. /* check for the drive going missing indicated by SCR status bits
  1510. * 0-3 = 0 */
  1511. sata_oxnas_scr_read_port(ap, SCR_STATUS, &Reg);
  1512. if (!(Reg & 0x1)) {
  1513. status |= ATA_DF;
  1514. status |= ATA_ERR;
  1515. }
  1516. return status;
  1517. }
  1518. static inline void sata_oxnas_reset_ucode(struct ata_host *ah, int force,
  1519. int no_microcode)
  1520. {
  1521. struct sata_oxnas_host_priv *hd = ah->private_data;
  1522. DPRINTK("ENTER\n");
  1523. if (no_microcode) {
  1524. u32 reg;
  1525. sata_oxnas_set_mode(ah, UNKNOWN_MODE, force);
  1526. reg = ioread32(hd->core_base + DEVICE_CONTROL);
  1527. reg |= DEVICE_CONTROL_ATA_ERR_OVERRIDE;
  1528. iowrite32(reg, hd->core_base + DEVICE_CONTROL);
  1529. } else {
  1530. /* JBOD uCode */
  1531. sata_oxnas_set_mode(ah, OXNASSATA_NOTRAID, force);
  1532. /* Turn the work around off as it may have been left on by any
  1533. * HW-RAID code that we've been working with */
  1534. iowrite32(0x0, hd->core_base + PORT_ERROR_MASK);
  1535. }
  1536. }
  1537. /**
  1538. * Prepare as much as possible for a command without involving anything that is
  1539. * shared between ports.
  1540. */
  1541. static void sata_oxnas_qc_prep(struct ata_queued_cmd *qc)
  1542. {
  1543. struct sata_oxnas_port_priv *pd;
  1544. int port_no = qc->ap->port_no;
  1545. /* if the port's not connected, complete now with an error */
  1546. if (!sata_oxnas_check_link(qc->ap)) {
  1547. ata_port_err(qc->ap,
  1548. "port %d not connected completing with error\n",
  1549. port_no);
  1550. qc->err_mask |= AC_ERR_ATA_BUS;
  1551. ata_qc_complete(qc);
  1552. }
  1553. sata_oxnas_reset_ucode(qc->ap->host, 0, 0);
  1554. /* both pio and dma commands use dma */
  1555. if (ata_is_dma(qc->tf.protocol) || ata_is_pio(qc->tf.protocol)) {
  1556. /* program the scatterlist into the prd table */
  1557. ata_bmdma_qc_prep(qc);
  1558. /* point the sgdma controller at the dma request structure */
  1559. pd = qc->ap->private_data;
  1560. iowrite32(pd->sgdma_request_pa,
  1561. pd->sgdma_base + SGDMA_REQUESTPTR);
  1562. /* setup the request table */
  1563. if (port_no == 0) {
  1564. pd->sgdma_request->control =
  1565. (qc->dma_dir == DMA_FROM_DEVICE) ?
  1566. SGDMA_REQCTL0IN : SGDMA_REQCTL0OUT;
  1567. } else {
  1568. pd->sgdma_request->control =
  1569. (qc->dma_dir == DMA_FROM_DEVICE) ?
  1570. SGDMA_REQCTL1IN : SGDMA_REQCTL1OUT;
  1571. }
  1572. pd->sgdma_request->qualifier = SGDMA_REQQUAL;
  1573. pd->sgdma_request->src_pa = qc->ap->bmdma_prd_dma;
  1574. pd->sgdma_request->dst_pa = qc->ap->bmdma_prd_dma;
  1575. smp_wmb();
  1576. /* tell it to wait */
  1577. iowrite32(SGDMA_CONTROL_NOGO, pd->sgdma_base + SGDMA_CONTROL);
  1578. }
  1579. }
  1580. static int sata_oxnas_port_start(struct ata_port *ap)
  1581. {
  1582. struct sata_oxnas_host_priv *host_priv = ap->host->private_data;
  1583. struct device *dev = ap->host->dev;
  1584. struct sata_oxnas_port_priv *pp;
  1585. void *mem;
  1586. dma_addr_t mem_dma;
  1587. DPRINTK("ENTER\n");
  1588. pp = kzalloc(sizeof(*pp), GFP_KERNEL);
  1589. if (!pp)
  1590. return -ENOMEM;
  1591. pp->port_base = host_priv->port_base +
  1592. (ap->port_no ? PORT_SIZE : 0);
  1593. pp->dmactl_base = host_priv->dmactl_base +
  1594. (ap->port_no ? DMA_CORESIZE : 0);
  1595. pp->sgdma_base = host_priv->sgdma_base +
  1596. (ap->port_no ? SGDMA_CORESIZE : 0);
  1597. pp->core_base = host_priv->core_base;
  1598. /* preallocated */
  1599. if (host_priv->dma_size >= SATA_OXNAS_DMA_SIZE * host_priv->n_ports) {
  1600. DPRINTK("using preallocated DMA\n");
  1601. mem_dma = host_priv->dma_base +
  1602. (ap->port_no ? SATA_OXNAS_DMA_SIZE : 0);
  1603. mem = ioremap(mem_dma, SATA_OXNAS_DMA_SIZE);
  1604. } else {
  1605. mem = dma_alloc_coherent(dev, SATA_OXNAS_DMA_SIZE, &mem_dma,
  1606. GFP_KERNEL);
  1607. }
  1608. if (!mem)
  1609. goto err_ret;
  1610. pp->sgdma_request_pa = mem_dma;
  1611. pp->sgdma_request = mem;
  1612. ap->bmdma_prd_dma = mem_dma + sizeof(struct sgdma_request);
  1613. ap->bmdma_prd = mem + sizeof(struct sgdma_request);
  1614. ap->private_data = pp;
  1615. sata_oxnas_post_reset_init(ap);
  1616. return 0;
  1617. err_ret:
  1618. kfree(pp);
  1619. return -ENOMEM;
  1620. }
  1621. static void sata_oxnas_port_stop(struct ata_port *ap)
  1622. {
  1623. struct device *dev = ap->host->dev;
  1624. struct sata_oxnas_port_priv *pp = ap->private_data;
  1625. struct sata_oxnas_host_priv *host_priv = ap->host->private_data;
  1626. DPRINTK("ENTER\n");
  1627. ap->private_data = NULL;
  1628. if (host_priv->dma_size) {
  1629. iounmap(pp->sgdma_request);
  1630. } else {
  1631. dma_free_coherent(dev, SATA_OXNAS_DMA_SIZE,
  1632. pp->sgdma_request, pp->sgdma_request_pa);
  1633. }
  1634. kfree(pp);
  1635. }
  1636. static void sata_oxnas_post_reset_init(struct ata_port *ap)
  1637. {
  1638. uint dev;
  1639. /* force to load u-code only once after reset */
  1640. sata_oxnas_reset_ucode(ap->host, !ap->port_no, 0);
  1641. /* turn on phy error detection by removing the masks */
  1642. sata_oxnas_link_write(ap, 0x0C, 0x30003);
  1643. /* enable hotplug event detection */
  1644. sata_oxnas_scr_write_port(ap, SCR_ERROR, ~0);
  1645. sata_oxnas_scr_write_port(ap, SERROR_IRQ_MASK, 0x03feffff);
  1646. sata_oxnas_scr_write_port(ap, SCR_ACTIVE, ~0 & ~(1 << 26) & ~(1 << 16));
  1647. /* enable interrupts for ports */
  1648. sata_oxnas_irq_on(ap);
  1649. /* go through all the devices and configure them */
  1650. for (dev = 0; dev < ATA_MAX_DEVICES; ++dev) {
  1651. if (ap->link.device[dev].class == ATA_DEV_ATA) {
  1652. sata_std_hardreset(&ap->link, NULL, jiffies + HZ);
  1653. sata_oxnas_dev_config(&(ap->link.device[dev]));
  1654. }
  1655. }
  1656. /* clean up any remaining errors */
  1657. sata_oxnas_scr_write_port(ap, SCR_ERROR, ~0);
  1658. VPRINTK("done\n");
  1659. }
  1660. /**
  1661. * host_stop() is called when the rmmod or hot unplug process begins. The
  1662. * hook must stop all hardware interrupts, DMA engines, etc.
  1663. *
  1664. * @param ap hardware with the registers in
  1665. */
  1666. static void sata_oxnas_host_stop(struct ata_host *host_set)
  1667. {
  1668. DPRINTK("\n");
  1669. }
  1670. #define ERROR_HW_ACQUIRE_TIMEOUT_JIFFIES (10 * HZ)
  1671. static void sata_oxnas_error_handler(struct ata_port *ap)
  1672. {
  1673. DPRINTK("Enter port_no %d\n", ap->port_no);
  1674. sata_oxnas_freeze_host(ap);
  1675. /* If the core is busy here, make it idle */
  1676. sata_oxnas_cleanup(ap->host);
  1677. ata_std_error_handler(ap);
  1678. sata_oxnas_thaw_host(ap);
  1679. }
  1680. static int sata_oxnas_softreset(struct ata_link *link, unsigned int *class,
  1681. unsigned long deadline)
  1682. {
  1683. struct ata_port *ap = link->ap;
  1684. struct sata_oxnas_port_priv *pd = ap->private_data;
  1685. void __iomem *port_base = pd->port_base;
  1686. int rc;
  1687. struct ata_taskfile tf;
  1688. u32 Command_Reg;
  1689. DPRINTK("ENTER\n");
  1690. port_base = pd->port_base;
  1691. if (ata_link_offline(link)) {
  1692. DPRINTK("PHY reports no device\n");
  1693. *class = ATA_DEV_NONE;
  1694. goto out;
  1695. }
  1696. /* write value to register */
  1697. iowrite32(0, port_base + ORB1);
  1698. iowrite32(0, port_base + ORB2);
  1699. iowrite32(0, port_base + ORB3);
  1700. iowrite32((ap->ctl) << 24, port_base + ORB4);
  1701. /* command the core to send a control FIS */
  1702. Command_Reg = ioread32(port_base + SATA_COMMAND);
  1703. Command_Reg &= ~SATA_OPCODE_MASK;
  1704. Command_Reg |= CMD_WRITE_TO_ORB_REGS_NO_COMMAND;
  1705. iowrite32(Command_Reg, port_base + SATA_COMMAND);
  1706. udelay(20); /* FIXME: flush */
  1707. /* write value to register */
  1708. iowrite32((ap->ctl | ATA_SRST) << 24, port_base + ORB4);
  1709. /* command the core to send a control FIS */
  1710. Command_Reg &= ~SATA_OPCODE_MASK;
  1711. Command_Reg |= CMD_WRITE_TO_ORB_REGS_NO_COMMAND;
  1712. iowrite32(Command_Reg, port_base + SATA_COMMAND);
  1713. udelay(20); /* FIXME: flush */
  1714. /* write value to register */
  1715. iowrite32((ap->ctl) << 24, port_base + ORB4);
  1716. /* command the core to send a control FIS */
  1717. Command_Reg &= ~SATA_OPCODE_MASK;
  1718. Command_Reg |= CMD_WRITE_TO_ORB_REGS_NO_COMMAND;
  1719. iowrite32(Command_Reg, port_base + SATA_COMMAND);
  1720. msleep(150);
  1721. rc = ata_sff_wait_ready(link, deadline);
  1722. /* if link is occupied, -ENODEV too is an error */
  1723. if (rc && (rc != -ENODEV || sata_scr_valid(link))) {
  1724. ata_link_printk(link, KERN_ERR, "SRST failed (errno=%d)\n", rc);
  1725. return rc;
  1726. }
  1727. /* determine by signature whether we have ATA or ATAPI devices */
  1728. sata_oxnas_tf_read(ap, &tf);
  1729. *class = ata_dev_classify(&tf);
  1730. if (*class == ATA_DEV_UNKNOWN)
  1731. *class = ATA_DEV_NONE;
  1732. out:
  1733. DPRINTK("EXIT, class=%u\n", *class);
  1734. return 0;
  1735. }
  1736. int sata_oxnas_init_controller(struct ata_host *host)
  1737. {
  1738. return 0;
  1739. }
  1740. /**
  1741. * Ref bug-6320
  1742. *
  1743. * This code is a work around for a DMA hardware bug that will repeat the
  1744. * penultimate 8-bytes on some reads. This code will check that the amount
  1745. * of data transferred is a multiple of 512 bytes, if not the in it will
  1746. * fetch the correct data from a buffer in the SATA core and copy it into
  1747. * memory.
  1748. *
  1749. * @param port SATA port to check and if necessary, correct.
  1750. */
  1751. static int sata_oxnas_bug_6320_detect(struct ata_port *ap)
  1752. {
  1753. struct sata_oxnas_port_priv *pd = ap->private_data;
  1754. void __iomem *core_base = pd->core_base;
  1755. int is_read;
  1756. int quads_transferred;
  1757. int remainder;
  1758. int sector_quads_remaining;
  1759. int bug_present = 0;
  1760. /* Only want to apply fix to reads */
  1761. is_read = !(ioread32(core_base + DM_DBG1) & (ap->port_no ?
  1762. BIT(CORE_PORT1_DATA_DIR_BIT) :
  1763. BIT(CORE_PORT0_DATA_DIR_BIT)));
  1764. /* Check for an incomplete transfer, i.e. not a multiple of 512 bytes
  1765. transferred (datacount_port register counts quads transferred) */
  1766. quads_transferred =
  1767. ioread32(core_base + (ap->port_no ?
  1768. DATACOUNT_PORT1 : DATACOUNT_PORT0));
  1769. remainder = quads_transferred & 0x7f;
  1770. sector_quads_remaining = remainder ? (0x80 - remainder) : 0;
  1771. if (is_read && (sector_quads_remaining == 2)) {
  1772. bug_present = 1;
  1773. } else if (sector_quads_remaining) {
  1774. if (is_read) {
  1775. ata_port_warn(ap, "SATA read fixup cannot deal with "
  1776. "%d quads remaining\n",
  1777. sector_quads_remaining);
  1778. } else {
  1779. ata_port_warn(ap, "SATA write fixup of %d quads "
  1780. "remaining not supported\n",
  1781. sector_quads_remaining);
  1782. }
  1783. }
  1784. return bug_present;
  1785. }
  1786. /* This port done an interrupt */
  1787. static void sata_oxnas_port_irq(struct ata_port *ap, int force_error)
  1788. {
  1789. struct ata_queued_cmd *qc;
  1790. struct sata_oxnas_port_priv *pd = ap->private_data;
  1791. void __iomem *port_base = pd->port_base;
  1792. u32 int_status;
  1793. unsigned long flags = 0;
  1794. DPRINTK("ENTER port %d irqstatus %x\n", ap->port_no,
  1795. ioread32(port_base + INT_STATUS));
  1796. if (ap->qc_active & (1 << ATA_TAG_INTERNAL)) {
  1797. qc = ata_qc_from_tag(ap, ATA_TAG_INTERNAL);
  1798. DPRINTK("completing non-ncq cmd\n");
  1799. if (qc)
  1800. ata_qc_complete(qc);
  1801. return;
  1802. }
  1803. qc = ata_qc_from_tag(ap, ap->link.active_tag);
  1804. /* record the port's interrupt */
  1805. int_status = ioread32(port_base + INT_STATUS);
  1806. /* If there's no command associated with this IRQ, ignore it. We may get
  1807. * spurious interrupts when cleaning-up after a failed command, ignore
  1808. * these too. */
  1809. if (likely(qc)) {
  1810. /* get the status before any error cleanup */
  1811. qc->err_mask = ac_err_mask(sata_oxnas_check_status(ap));
  1812. if (force_error) {
  1813. /* Pretend there has been a link error */
  1814. qc->err_mask |= AC_ERR_ATA_BUS;
  1815. DPRINTK(" ####force error####\n");
  1816. }
  1817. /* tell libata we're done */
  1818. local_irq_save(flags);
  1819. sata_oxnas_irq_clear(ap);
  1820. local_irq_restore(flags);
  1821. ata_qc_complete(qc);
  1822. } else {
  1823. VPRINTK("Ignoring interrupt, can't find the command tag="
  1824. "%d %08x\n", ap->link.active_tag, ap->qc_active);
  1825. }
  1826. /* maybe a hotplug event */
  1827. if (unlikely(int_status & INT_LINK_SERROR)) {
  1828. u32 serror;
  1829. sata_oxnas_scr_read_port(ap, SCR_ERROR, &serror);
  1830. if (serror & (SERR_DEV_XCHG | SERR_PHYRDY_CHG)) {
  1831. ata_ehi_hotplugged(&ap->link.eh_info);
  1832. ata_port_freeze(ap);
  1833. }
  1834. }
  1835. }
  1836. /**
  1837. * irq_handler is the interrupt handling routine registered with the system,
  1838. * by libata.
  1839. */
  1840. static irqreturn_t sata_oxnas_interrupt(int irq, void *dev_instance)
  1841. {
  1842. struct ata_host *ah = dev_instance;
  1843. struct sata_oxnas_host_priv *hd = ah->private_data;
  1844. void __iomem *core_base = hd->core_base;
  1845. u32 int_status;
  1846. irqreturn_t ret = IRQ_NONE;
  1847. u32 port_no;
  1848. u32 mask;
  1849. int bug_present;
  1850. /* loop until there are no more interrupts */
  1851. while ((int_status = (ioread32(core_base + CORE_INT_STATUS)) &
  1852. (COREINT_END | (COREINT_END << 1)))) {
  1853. /* clear any interrupt */
  1854. iowrite32(int_status, core_base + CORE_INT_CLEAR);
  1855. /* Only need workaround_bug_6320 for single disk systems as dual
  1856. * disk will use uCode which prevents this read underrun problem
  1857. * from occurring.
  1858. * All single disk systems will use port 0 */
  1859. for (port_no = 0; port_no < hd->n_ports; ++port_no) {
  1860. /* check the raw end of command interrupt to see if the
  1861. * port is done */
  1862. mask = (COREINT_END << port_no);
  1863. if (!(int_status & mask))
  1864. continue;
  1865. /* this port had an interrupt, clear it */
  1866. iowrite32(mask, core_base + CORE_INT_CLEAR);
  1867. /* check for bug 6320 only if no microcode was loaded */
  1868. bug_present = (hd->current_ucode == UNKNOWN_MODE) &&
  1869. sata_oxnas_bug_6320_detect(ah->ports[port_no]);
  1870. sata_oxnas_port_irq(ah->ports[port_no],
  1871. bug_present);
  1872. ret = IRQ_HANDLED;
  1873. }
  1874. }
  1875. return ret;
  1876. }
  1877. /*
  1878. * scsi mid-layer and libata interface structures
  1879. */
  1880. static struct scsi_host_template sata_oxnas_sht = {
  1881. ATA_NCQ_SHT("sata_oxnas"),
  1882. .can_queue = SATA_OXNAS_QUEUE_DEPTH,
  1883. .sg_tablesize = SATA_OXNAS_MAX_PRD,
  1884. .dma_boundary = ATA_DMA_BOUNDARY,
  1885. .unchecked_isa_dma = 0,
  1886. };
  1887. static struct ata_port_operations sata_oxnas_ops = {
  1888. .inherits = &sata_port_ops,
  1889. .qc_prep = sata_oxnas_qc_prep,
  1890. .qc_issue = sata_oxnas_qc_issue,
  1891. .qc_fill_rtf = sata_oxnas_qc_fill_rtf,
  1892. .qc_new = sata_oxnas_qc_new,
  1893. .qc_free = sata_oxnas_qc_free,
  1894. .scr_read = sata_oxnas_scr_read,
  1895. .scr_write = sata_oxnas_scr_write,
  1896. .freeze = sata_oxnas_freeze,
  1897. .thaw = sata_oxnas_thaw,
  1898. .softreset = sata_oxnas_softreset,
  1899. /* .hardreset = sata_oxnas_hardreset, */
  1900. .postreset = sata_oxnas_postreset,
  1901. .error_handler = sata_oxnas_error_handler,
  1902. .post_internal_cmd = sata_oxnas_post_internal_cmd,
  1903. .port_start = sata_oxnas_port_start,
  1904. .port_stop = sata_oxnas_port_stop,
  1905. .host_stop = sata_oxnas_host_stop,
  1906. /* .pmp_attach = sata_oxnas_pmp_attach, */
  1907. /* .pmp_detach = sata_oxnas_pmp_detach, */
  1908. .sff_check_status = sata_oxnas_check_status,
  1909. .acquire_hw = sata_oxnas_acquire_hw,
  1910. };
  1911. static const struct ata_port_info sata_oxnas_port_info = {
  1912. .flags = SATA_OXNAS_HOST_FLAGS,
  1913. .pio_mask = ATA_PIO4,
  1914. .udma_mask = ATA_UDMA6,
  1915. .port_ops = &sata_oxnas_ops,
  1916. };
  1917. static int sata_oxnas_probe(struct platform_device *ofdev)
  1918. {
  1919. int retval = -ENXIO;
  1920. int n_ports = 0;
  1921. void __iomem *port_base = NULL;
  1922. void __iomem *dmactl_base = NULL;
  1923. void __iomem *sgdma_base = NULL;
  1924. void __iomem *core_base = NULL;
  1925. void __iomem *phy_base = NULL;
  1926. struct reset_control *rstc;
  1927. struct resource res = {};
  1928. struct sata_oxnas_host_priv *host_priv = NULL;
  1929. int irq = 0;
  1930. struct ata_host *host = NULL;
  1931. struct clk *clk = NULL;
  1932. const struct ata_port_info *ppi[] = { &sata_oxnas_port_info, NULL };
  1933. of_property_read_u32(ofdev->dev.of_node, "nr-ports", &n_ports);
  1934. if (n_ports < 1 || n_ports > SATA_OXNAS_MAX_PORTS)
  1935. goto error_exit_with_cleanup;
  1936. port_base = of_iomap(ofdev->dev.of_node, 0);
  1937. if (!port_base)
  1938. goto error_exit_with_cleanup;
  1939. dmactl_base = of_iomap(ofdev->dev.of_node, 1);
  1940. if (!dmactl_base)
  1941. goto error_exit_with_cleanup;
  1942. sgdma_base = of_iomap(ofdev->dev.of_node, 2);
  1943. if (!sgdma_base)
  1944. goto error_exit_with_cleanup;
  1945. core_base = of_iomap(ofdev->dev.of_node, 3);
  1946. if (!core_base)
  1947. goto error_exit_with_cleanup;
  1948. phy_base = of_iomap(ofdev->dev.of_node, 4);
  1949. if (!phy_base)
  1950. goto error_exit_with_cleanup;
  1951. host_priv = devm_kzalloc(&ofdev->dev,
  1952. sizeof(struct sata_oxnas_host_priv),
  1953. GFP_KERNEL);
  1954. if (!host_priv)
  1955. goto error_exit_with_cleanup;
  1956. host_priv->port_base = port_base;
  1957. host_priv->dmactl_base = dmactl_base;
  1958. host_priv->sgdma_base = sgdma_base;
  1959. host_priv->core_base = core_base;
  1960. host_priv->phy_base = phy_base;
  1961. host_priv->n_ports = n_ports;
  1962. host_priv->current_ucode = UNKNOWN_MODE;
  1963. if (!of_address_to_resource(ofdev->dev.of_node, 5, &res)) {
  1964. host_priv->dma_base = res.start;
  1965. host_priv->dma_size = resource_size(&res);
  1966. }
  1967. irq = irq_of_parse_and_map(ofdev->dev.of_node, 0);
  1968. if (!irq) {
  1969. dev_err(&ofdev->dev, "invalid irq from platform\n");
  1970. goto error_exit_with_cleanup;
  1971. }
  1972. host_priv->irq = irq;
  1973. clk = of_clk_get(ofdev->dev.of_node, 0);
  1974. if (IS_ERR(clk)) {
  1975. retval = PTR_ERR(clk);
  1976. clk = NULL;
  1977. goto error_exit_with_cleanup;
  1978. }
  1979. host_priv->clk = clk;
  1980. rstc = devm_reset_control_get(&ofdev->dev, "sata");
  1981. if (IS_ERR(rstc)) {
  1982. retval = PTR_ERR(rstc);
  1983. goto error_exit_with_cleanup;
  1984. }
  1985. host_priv->rst_sata = rstc;
  1986. rstc = devm_reset_control_get(&ofdev->dev, "link");
  1987. if (IS_ERR(rstc)) {
  1988. retval = PTR_ERR(rstc);
  1989. goto error_exit_with_cleanup;
  1990. }
  1991. host_priv->rst_link = rstc;
  1992. rstc = devm_reset_control_get(&ofdev->dev, "phy");
  1993. if (IS_ERR(rstc)) {
  1994. retval = PTR_ERR(rstc);
  1995. goto error_exit_with_cleanup;
  1996. }
  1997. host_priv->rst_phy = rstc;
  1998. /* allocate host structure */
  1999. host = ata_host_alloc_pinfo(&ofdev->dev, ppi, n_ports);
  2000. if (!host) {
  2001. retval = -ENOMEM;
  2002. goto error_exit_with_cleanup;
  2003. }
  2004. host->private_data = host_priv;
  2005. host->iomap = port_base;
  2006. /* initialize core locking and queues */
  2007. init_waitqueue_head(&host_priv->fast_wait_queue);
  2008. init_waitqueue_head(&host_priv->scsi_wait_queue);
  2009. spin_lock_init(&host_priv->phy_lock);
  2010. spin_lock_init(&host_priv->core_lock);
  2011. host_priv->core_locked = 0;
  2012. host_priv->reentrant_port_no = -1;
  2013. host_priv->hw_lock_count = 0;
  2014. host_priv->direct_lock_count = 0;
  2015. host_priv->locker_uid = 0;
  2016. host_priv->current_locker_type = SATA_UNLOCKED;
  2017. host_priv->isr_arg = NULL;
  2018. host_priv->isr_callback = NULL;
  2019. /* initialize host controller */
  2020. retval = sata_oxnas_init_controller(host);
  2021. if (retval)
  2022. goto error_exit_with_cleanup;
  2023. /*
  2024. * Now, register with libATA core, this will also initiate the
  2025. * device discovery process, invoking our port_start() handler &
  2026. * error_handler() to execute a dummy softreset EH session
  2027. */
  2028. ata_host_activate(host, irq, sata_oxnas_interrupt, SATA_OXNAS_IRQ_FLAG,
  2029. &sata_oxnas_sht);
  2030. return 0;
  2031. error_exit_with_cleanup:
  2032. if (irq)
  2033. irq_dispose_mapping(host_priv->irq);
  2034. if (clk)
  2035. clk_put(clk);
  2036. if (host)
  2037. ata_host_detach(host);
  2038. if (port_base)
  2039. iounmap(port_base);
  2040. if (sgdma_base)
  2041. iounmap(sgdma_base);
  2042. if (core_base)
  2043. iounmap(core_base);
  2044. if (phy_base)
  2045. iounmap(phy_base);
  2046. return retval;
  2047. }
  2048. static int sata_oxnas_remove(struct platform_device *ofdev)
  2049. {
  2050. struct ata_host *host = dev_get_drvdata(&ofdev->dev);
  2051. struct sata_oxnas_host_priv *host_priv = host->private_data;
  2052. ata_host_detach(host);
  2053. irq_dispose_mapping(host_priv->irq);
  2054. iounmap(host_priv->port_base);
  2055. iounmap(host_priv->sgdma_base);
  2056. iounmap(host_priv->core_base);
  2057. /* reset Controller, Link and PHY */
  2058. reset_control_assert(host_priv->rst_sata);
  2059. reset_control_assert(host_priv->rst_link);
  2060. reset_control_assert(host_priv->rst_phy);
  2061. /* Disable the clock to the SATA block */
  2062. clk_disable_unprepare(host_priv->clk);
  2063. clk_put(host_priv->clk);
  2064. return 0;
  2065. }
  2066. #ifdef CONFIG_PM
  2067. static int sata_oxnas_suspend(struct platform_device *op, pm_message_t state)
  2068. {
  2069. struct ata_host *host = dev_get_drvdata(&op->dev);
  2070. return ata_host_suspend(host, state);
  2071. }
  2072. static int sata_oxnas_resume(struct platform_device *op)
  2073. {
  2074. struct ata_host *host = dev_get_drvdata(&op->dev);
  2075. int ret;
  2076. ret = sata_oxnas_init_controller(host);
  2077. if (ret) {
  2078. dev_err(&op->dev, "Error initializing hardware\n");
  2079. return ret;
  2080. }
  2081. ata_host_resume(host);
  2082. return 0;
  2083. }
  2084. #endif
  2085. static struct of_device_id oxnas_sata_match[] = {
  2086. {
  2087. .compatible = "plxtech,nas782x-sata",
  2088. },
  2089. {},
  2090. };
  2091. MODULE_DEVICE_TABLE(of, oxnas_sata_match);
  2092. static struct platform_driver oxnas_sata_driver = {
  2093. .driver = {
  2094. .name = "oxnas-sata",
  2095. .owner = THIS_MODULE,
  2096. .of_match_table = oxnas_sata_match,
  2097. },
  2098. .probe = sata_oxnas_probe,
  2099. .remove = sata_oxnas_remove,
  2100. #ifdef CONFIG_PM
  2101. .suspend = sata_oxnas_suspend,
  2102. .resume = sata_oxnas_resume,
  2103. #endif
  2104. };
  2105. module_platform_driver(oxnas_sata_driver);
  2106. MODULE_LICENSE("GPL");
  2107. MODULE_VERSION("1.0");
  2108. MODULE_AUTHOR("Oxford Semiconductor Ltd.");
  2109. MODULE_DESCRIPTION("low-level driver for Oxford 934 SATA core");