sata_dwc.c 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071
  1. /*
  2. * sata_dwc.c
  3. *
  4. * Synopsys DesignWare Cores (DWC) SATA host driver
  5. *
  6. * Author: Mark Miesfeld <mmiesfeld@amcc.com>
  7. *
  8. * Ported from 2.6.19.2 to 2.6.25/26 by Stefan Roese <sr@denx.de>
  9. * Copyright 2008 DENX Software Engineering
  10. *
  11. * Based on versions provided by AMCC and Synopsys which are:
  12. * Copyright 2006 Applied Micro Circuits Corporation
  13. * COPYRIGHT (C) 2005 SYNOPSYS, INC. ALL RIGHTS RESERVED
  14. *
  15. * SPDX-License-Identifier: GPL-2.0+
  16. */
  17. /*
  18. * SATA support based on the chip canyonlands.
  19. *
  20. * 04-17-2009
  21. * The local version of this driver for the canyonlands board
  22. * does not use interrupts but polls the chip instead.
  23. */
  24. #include <common.h>
  25. #include <command.h>
  26. #include <pci.h>
  27. #include <asm/processor.h>
  28. #include <asm/errno.h>
  29. #include <asm/io.h>
  30. #include <malloc.h>
  31. #include <ata.h>
  32. #include <sata.h>
  33. #include <linux/ctype.h>
  34. #include "sata_dwc.h"
  35. #define DMA_NUM_CHANS 1
  36. #define DMA_NUM_CHAN_REGS 8
  37. #define AHB_DMA_BRST_DFLT 16
  38. struct dmareg {
  39. u32 low;
  40. u32 high;
  41. };
  42. struct dma_chan_regs {
  43. struct dmareg sar;
  44. struct dmareg dar;
  45. struct dmareg llp;
  46. struct dmareg ctl;
  47. struct dmareg sstat;
  48. struct dmareg dstat;
  49. struct dmareg sstatar;
  50. struct dmareg dstatar;
  51. struct dmareg cfg;
  52. struct dmareg sgr;
  53. struct dmareg dsr;
  54. };
  55. struct dma_interrupt_regs {
  56. struct dmareg tfr;
  57. struct dmareg block;
  58. struct dmareg srctran;
  59. struct dmareg dsttran;
  60. struct dmareg error;
  61. };
  62. struct ahb_dma_regs {
  63. struct dma_chan_regs chan_regs[DMA_NUM_CHAN_REGS];
  64. struct dma_interrupt_regs interrupt_raw;
  65. struct dma_interrupt_regs interrupt_status;
  66. struct dma_interrupt_regs interrupt_mask;
  67. struct dma_interrupt_regs interrupt_clear;
  68. struct dmareg statusInt;
  69. struct dmareg rq_srcreg;
  70. struct dmareg rq_dstreg;
  71. struct dmareg rq_sgl_srcreg;
  72. struct dmareg rq_sgl_dstreg;
  73. struct dmareg rq_lst_srcreg;
  74. struct dmareg rq_lst_dstreg;
  75. struct dmareg dma_cfg;
  76. struct dmareg dma_chan_en;
  77. struct dmareg dma_id;
  78. struct dmareg dma_test;
  79. struct dmareg res1;
  80. struct dmareg res2;
  81. /* DMA Comp Params
  82. * Param 6 = dma_param[0], Param 5 = dma_param[1],
  83. * Param 4 = dma_param[2] ...
  84. */
  85. struct dmareg dma_params[6];
  86. };
  87. #define DMA_EN 0x00000001
  88. #define DMA_DI 0x00000000
  89. #define DMA_CHANNEL(ch) (0x00000001 << (ch))
  90. #define DMA_ENABLE_CHAN(ch) ((0x00000001 << (ch)) | \
  91. ((0x000000001 << (ch)) << 8))
  92. #define DMA_DISABLE_CHAN(ch) (0x00000000 | \
  93. ((0x000000001 << (ch)) << 8))
  94. #define SATA_DWC_MAX_PORTS 1
  95. #define SATA_DWC_SCR_OFFSET 0x24
  96. #define SATA_DWC_REG_OFFSET 0x64
  97. struct sata_dwc_regs {
  98. u32 fptagr;
  99. u32 fpbor;
  100. u32 fptcr;
  101. u32 dmacr;
  102. u32 dbtsr;
  103. u32 intpr;
  104. u32 intmr;
  105. u32 errmr;
  106. u32 llcr;
  107. u32 phycr;
  108. u32 physr;
  109. u32 rxbistpd;
  110. u32 rxbistpd1;
  111. u32 rxbistpd2;
  112. u32 txbistpd;
  113. u32 txbistpd1;
  114. u32 txbistpd2;
  115. u32 bistcr;
  116. u32 bistfctr;
  117. u32 bistsr;
  118. u32 bistdecr;
  119. u32 res[15];
  120. u32 testr;
  121. u32 versionr;
  122. u32 idr;
  123. u32 unimpl[192];
  124. u32 dmadr[256];
  125. };
  126. #define SATA_DWC_TXFIFO_DEPTH 0x01FF
  127. #define SATA_DWC_RXFIFO_DEPTH 0x01FF
  128. #define SATA_DWC_DBTSR_MWR(size) ((size / 4) & SATA_DWC_TXFIFO_DEPTH)
  129. #define SATA_DWC_DBTSR_MRD(size) (((size / 4) & \
  130. SATA_DWC_RXFIFO_DEPTH) << 16)
  131. #define SATA_DWC_INTPR_DMAT 0x00000001
  132. #define SATA_DWC_INTPR_NEWFP 0x00000002
  133. #define SATA_DWC_INTPR_PMABRT 0x00000004
  134. #define SATA_DWC_INTPR_ERR 0x00000008
  135. #define SATA_DWC_INTPR_NEWBIST 0x00000010
  136. #define SATA_DWC_INTPR_IPF 0x10000000
  137. #define SATA_DWC_INTMR_DMATM 0x00000001
  138. #define SATA_DWC_INTMR_NEWFPM 0x00000002
  139. #define SATA_DWC_INTMR_PMABRTM 0x00000004
  140. #define SATA_DWC_INTMR_ERRM 0x00000008
  141. #define SATA_DWC_INTMR_NEWBISTM 0x00000010
  142. #define SATA_DWC_DMACR_TMOD_TXCHEN 0x00000004
  143. #define SATA_DWC_DMACR_TXRXCH_CLEAR SATA_DWC_DMACR_TMOD_TXCHEN
  144. #define SATA_DWC_QCMD_MAX 32
  145. #define SATA_DWC_SERROR_ERR_BITS 0x0FFF0F03
  146. #define HSDEVP_FROM_AP(ap) (struct sata_dwc_device_port*) \
  147. (ap)->private_data
  148. struct sata_dwc_device {
  149. struct device *dev;
  150. struct ata_probe_ent *pe;
  151. struct ata_host *host;
  152. u8 *reg_base;
  153. struct sata_dwc_regs *sata_dwc_regs;
  154. int irq_dma;
  155. };
  156. struct sata_dwc_device_port {
  157. struct sata_dwc_device *hsdev;
  158. int cmd_issued[SATA_DWC_QCMD_MAX];
  159. u32 dma_chan[SATA_DWC_QCMD_MAX];
  160. int dma_pending[SATA_DWC_QCMD_MAX];
  161. };
  162. enum {
  163. SATA_DWC_CMD_ISSUED_NOT = 0,
  164. SATA_DWC_CMD_ISSUED_PEND = 1,
  165. SATA_DWC_CMD_ISSUED_EXEC = 2,
  166. SATA_DWC_CMD_ISSUED_NODATA = 3,
  167. SATA_DWC_DMA_PENDING_NONE = 0,
  168. SATA_DWC_DMA_PENDING_TX = 1,
  169. SATA_DWC_DMA_PENDING_RX = 2,
  170. };
  171. #define msleep(a) udelay(a * 1000)
  172. #define ssleep(a) msleep(a * 1000)
  173. static int ata_probe_timeout = (ATA_TMOUT_INTERNAL / 100);
  174. enum sata_dev_state {
  175. SATA_INIT = 0,
  176. SATA_READY = 1,
  177. SATA_NODEVICE = 2,
  178. SATA_ERROR = 3,
  179. };
  180. enum sata_dev_state dev_state = SATA_INIT;
  181. static struct ahb_dma_regs *sata_dma_regs = 0;
  182. static struct ata_host *phost;
  183. static struct ata_port ap;
  184. static struct ata_port *pap = &ap;
  185. static struct ata_device ata_device;
  186. static struct sata_dwc_device_port dwc_devp;
  187. static void *scr_addr_sstatus;
  188. static u32 temp_n_block = 0;
  189. static unsigned ata_exec_internal(struct ata_device *dev,
  190. struct ata_taskfile *tf, const u8 *cdb,
  191. int dma_dir, unsigned int buflen,
  192. unsigned long timeout);
  193. static unsigned int ata_dev_set_feature(struct ata_device *dev,
  194. u8 enable,u8 feature);
  195. static unsigned int ata_dev_init_params(struct ata_device *dev,
  196. u16 heads, u16 sectors);
  197. static u8 ata_irq_on(struct ata_port *ap);
  198. static struct ata_queued_cmd *__ata_qc_from_tag(struct ata_port *ap,
  199. unsigned int tag);
  200. static int ata_hsm_move(struct ata_port *ap, struct ata_queued_cmd *qc,
  201. u8 status, int in_wq);
  202. static void ata_tf_to_host(struct ata_port *ap,
  203. const struct ata_taskfile *tf);
  204. static void ata_exec_command(struct ata_port *ap,
  205. const struct ata_taskfile *tf);
  206. static unsigned int ata_qc_issue_prot(struct ata_queued_cmd *qc);
  207. static u8 ata_check_altstatus(struct ata_port *ap);
  208. static u8 ata_check_status(struct ata_port *ap);
  209. static void ata_dev_select(struct ata_port *ap, unsigned int device,
  210. unsigned int wait, unsigned int can_sleep);
  211. static void ata_qc_issue(struct ata_queued_cmd *qc);
  212. static void ata_tf_load(struct ata_port *ap,
  213. const struct ata_taskfile *tf);
  214. static int ata_dev_read_sectors(unsigned char* pdata,
  215. unsigned long datalen, u32 block, u32 n_block);
  216. static int ata_dev_write_sectors(unsigned char* pdata,
  217. unsigned long datalen , u32 block, u32 n_block);
  218. static void ata_std_dev_select(struct ata_port *ap, unsigned int device);
  219. static void ata_qc_complete(struct ata_queued_cmd *qc);
  220. static void __ata_qc_complete(struct ata_queued_cmd *qc);
  221. static void fill_result_tf(struct ata_queued_cmd *qc);
  222. static void ata_tf_read(struct ata_port *ap, struct ata_taskfile *tf);
  223. static void ata_mmio_data_xfer(struct ata_device *dev,
  224. unsigned char *buf,
  225. unsigned int buflen,int do_write);
  226. static void ata_pio_task(struct ata_port *arg_ap);
  227. static void __ata_port_freeze(struct ata_port *ap);
  228. static int ata_port_freeze(struct ata_port *ap);
  229. static void ata_qc_free(struct ata_queued_cmd *qc);
  230. static void ata_pio_sectors(struct ata_queued_cmd *qc);
  231. static void ata_pio_sector(struct ata_queued_cmd *qc);
  232. static void ata_pio_queue_task(struct ata_port *ap,
  233. void *data,unsigned long delay);
  234. static void ata_hsm_qc_complete(struct ata_queued_cmd *qc, int in_wq);
  235. static int sata_dwc_softreset(struct ata_port *ap);
  236. static int ata_dev_read_id(struct ata_device *dev, unsigned int *p_class,
  237. unsigned int flags, u16 *id);
  238. static int check_sata_dev_state(void);
  239. static const struct ata_port_info sata_dwc_port_info[] = {
  240. {
  241. .flags = ATA_FLAG_SATA | ATA_FLAG_NO_LEGACY |
  242. ATA_FLAG_MMIO | ATA_FLAG_PIO_POLLING |
  243. ATA_FLAG_SRST | ATA_FLAG_NCQ,
  244. .pio_mask = 0x1f,
  245. .mwdma_mask = 0x07,
  246. .udma_mask = 0x7f,
  247. },
  248. };
  249. int init_sata(int dev)
  250. {
  251. struct sata_dwc_device hsdev;
  252. struct ata_host host;
  253. struct ata_port_info pi = sata_dwc_port_info[0];
  254. struct ata_link *link;
  255. struct sata_dwc_device_port hsdevp = dwc_devp;
  256. u8 *base = 0;
  257. u8 *sata_dma_regs_addr = 0;
  258. u8 status;
  259. unsigned long base_addr = 0;
  260. int chan = 0;
  261. int rc;
  262. int i;
  263. phost = &host;
  264. base = (u8*)SATA_BASE_ADDR;
  265. hsdev.sata_dwc_regs = (void *__iomem)(base + SATA_DWC_REG_OFFSET);
  266. host.n_ports = SATA_DWC_MAX_PORTS;
  267. for (i = 0; i < SATA_DWC_MAX_PORTS; i++) {
  268. ap.pflags |= ATA_PFLAG_INITIALIZING;
  269. ap.flags = ATA_FLAG_DISABLED;
  270. ap.print_id = -1;
  271. ap.ctl = ATA_DEVCTL_OBS;
  272. ap.host = &host;
  273. ap.last_ctl = 0xFF;
  274. link = &ap.link;
  275. link->ap = &ap;
  276. link->pmp = 0;
  277. link->active_tag = ATA_TAG_POISON;
  278. link->hw_sata_spd_limit = 0;
  279. ap.port_no = i;
  280. host.ports[i] = &ap;
  281. }
  282. ap.pio_mask = pi.pio_mask;
  283. ap.mwdma_mask = pi.mwdma_mask;
  284. ap.udma_mask = pi.udma_mask;
  285. ap.flags |= pi.flags;
  286. ap.link.flags |= pi.link_flags;
  287. host.ports[0]->ioaddr.cmd_addr = base;
  288. host.ports[0]->ioaddr.scr_addr = base + SATA_DWC_SCR_OFFSET;
  289. scr_addr_sstatus = base + SATA_DWC_SCR_OFFSET;
  290. base_addr = (unsigned long)base;
  291. host.ports[0]->ioaddr.cmd_addr = (void *)base_addr + 0x00;
  292. host.ports[0]->ioaddr.data_addr = (void *)base_addr + 0x00;
  293. host.ports[0]->ioaddr.error_addr = (void *)base_addr + 0x04;
  294. host.ports[0]->ioaddr.feature_addr = (void *)base_addr + 0x04;
  295. host.ports[0]->ioaddr.nsect_addr = (void *)base_addr + 0x08;
  296. host.ports[0]->ioaddr.lbal_addr = (void *)base_addr + 0x0c;
  297. host.ports[0]->ioaddr.lbam_addr = (void *)base_addr + 0x10;
  298. host.ports[0]->ioaddr.lbah_addr = (void *)base_addr + 0x14;
  299. host.ports[0]->ioaddr.device_addr = (void *)base_addr + 0x18;
  300. host.ports[0]->ioaddr.command_addr = (void *)base_addr + 0x1c;
  301. host.ports[0]->ioaddr.status_addr = (void *)base_addr + 0x1c;
  302. host.ports[0]->ioaddr.altstatus_addr = (void *)base_addr + 0x20;
  303. host.ports[0]->ioaddr.ctl_addr = (void *)base_addr + 0x20;
  304. sata_dma_regs_addr = (u8*)SATA_DMA_REG_ADDR;
  305. sata_dma_regs = (void *__iomem)sata_dma_regs_addr;
  306. status = ata_check_altstatus(&ap);
  307. if (status == 0x7f) {
  308. printf("Hard Disk not found.\n");
  309. dev_state = SATA_NODEVICE;
  310. rc = false;
  311. return rc;
  312. }
  313. printf("Waiting for device...");
  314. i = 0;
  315. while (1) {
  316. udelay(10000);
  317. status = ata_check_altstatus(&ap);
  318. if ((status & ATA_BUSY) == 0) {
  319. printf("\n");
  320. break;
  321. }
  322. i++;
  323. if (i > (ATA_RESET_TIME * 100)) {
  324. printf("** TimeOUT **\n");
  325. dev_state = SATA_NODEVICE;
  326. rc = false;
  327. return rc;
  328. }
  329. if ((i >= 100) && ((i % 100) == 0))
  330. printf(".");
  331. }
  332. rc = sata_dwc_softreset(&ap);
  333. if (rc) {
  334. printf("sata_dwc : error. soft reset failed\n");
  335. return rc;
  336. }
  337. for (chan = 0; chan < DMA_NUM_CHANS; chan++) {
  338. out_le32(&(sata_dma_regs->interrupt_mask.error.low),
  339. DMA_DISABLE_CHAN(chan));
  340. out_le32(&(sata_dma_regs->interrupt_mask.tfr.low),
  341. DMA_DISABLE_CHAN(chan));
  342. }
  343. out_le32(&(sata_dma_regs->dma_cfg.low), DMA_DI);
  344. out_le32(&hsdev.sata_dwc_regs->intmr,
  345. SATA_DWC_INTMR_ERRM |
  346. SATA_DWC_INTMR_PMABRTM);
  347. /* Unmask the error bits that should trigger
  348. * an error interrupt by setting the error mask register.
  349. */
  350. out_le32(&hsdev.sata_dwc_regs->errmr, SATA_DWC_SERROR_ERR_BITS);
  351. hsdev.host = ap.host;
  352. memset(&hsdevp, 0, sizeof(hsdevp));
  353. hsdevp.hsdev = &hsdev;
  354. for (i = 0; i < SATA_DWC_QCMD_MAX; i++)
  355. hsdevp.cmd_issued[i] = SATA_DWC_CMD_ISSUED_NOT;
  356. out_le32((void __iomem *)scr_addr_sstatus + 4,
  357. in_le32((void __iomem *)scr_addr_sstatus + 4));
  358. rc = 0;
  359. return rc;
  360. }
  361. static u8 ata_check_altstatus(struct ata_port *ap)
  362. {
  363. u8 val = 0;
  364. val = readb(ap->ioaddr.altstatus_addr);
  365. return val;
  366. }
  367. static int sata_dwc_softreset(struct ata_port *ap)
  368. {
  369. u8 nsect,lbal = 0;
  370. u8 tmp = 0;
  371. struct ata_ioports *ioaddr = &ap->ioaddr;
  372. in_le32((void *)ap->ioaddr.scr_addr + (SCR_ERROR * 4));
  373. writeb(0x55, ioaddr->nsect_addr);
  374. writeb(0xaa, ioaddr->lbal_addr);
  375. writeb(0xaa, ioaddr->nsect_addr);
  376. writeb(0x55, ioaddr->lbal_addr);
  377. writeb(0x55, ioaddr->nsect_addr);
  378. writeb(0xaa, ioaddr->lbal_addr);
  379. nsect = readb(ioaddr->nsect_addr);
  380. lbal = readb(ioaddr->lbal_addr);
  381. if ((nsect == 0x55) && (lbal == 0xaa)) {
  382. printf("Device found\n");
  383. } else {
  384. printf("No device found\n");
  385. dev_state = SATA_NODEVICE;
  386. return false;
  387. }
  388. tmp = ATA_DEVICE_OBS;
  389. writeb(tmp, ioaddr->device_addr);
  390. writeb(ap->ctl, ioaddr->ctl_addr);
  391. udelay(200);
  392. writeb(ap->ctl | ATA_SRST, ioaddr->ctl_addr);
  393. udelay(200);
  394. writeb(ap->ctl, ioaddr->ctl_addr);
  395. msleep(150);
  396. ata_check_status(ap);
  397. msleep(50);
  398. ata_check_status(ap);
  399. while (1) {
  400. u8 status = ata_check_status(ap);
  401. if (!(status & ATA_BUSY))
  402. break;
  403. printf("Hard Disk status is BUSY.\n");
  404. msleep(50);
  405. }
  406. tmp = ATA_DEVICE_OBS;
  407. writeb(tmp, ioaddr->device_addr);
  408. nsect = readb(ioaddr->nsect_addr);
  409. lbal = readb(ioaddr->lbal_addr);
  410. return 0;
  411. }
  412. static u8 ata_check_status(struct ata_port *ap)
  413. {
  414. u8 val = 0;
  415. val = readb(ap->ioaddr.status_addr);
  416. return val;
  417. }
  418. static int ata_id_has_hipm(const u16 *id)
  419. {
  420. u16 val = id[76];
  421. if (val == 0 || val == 0xffff)
  422. return -1;
  423. return val & (1 << 9);
  424. }
  425. static int ata_id_has_dipm(const u16 *id)
  426. {
  427. u16 val = id[78];
  428. if (val == 0 || val == 0xffff)
  429. return -1;
  430. return val & (1 << 3);
  431. }
  432. int scan_sata(int dev)
  433. {
  434. int i;
  435. int rc;
  436. u8 status;
  437. const u16 *id;
  438. struct ata_device *ata_dev = &ata_device;
  439. unsigned long pio_mask, mwdma_mask;
  440. char revbuf[7];
  441. u16 iobuf[ATA_SECTOR_WORDS];
  442. memset(iobuf, 0, sizeof(iobuf));
  443. if (dev_state == SATA_NODEVICE)
  444. return 1;
  445. printf("Waiting for device...");
  446. i = 0;
  447. while (1) {
  448. udelay(10000);
  449. status = ata_check_altstatus(&ap);
  450. if ((status & ATA_BUSY) == 0) {
  451. printf("\n");
  452. break;
  453. }
  454. i++;
  455. if (i > (ATA_RESET_TIME * 100)) {
  456. printf("** TimeOUT **\n");
  457. dev_state = SATA_NODEVICE;
  458. return 1;
  459. }
  460. if ((i >= 100) && ((i % 100) == 0))
  461. printf(".");
  462. }
  463. udelay(1000);
  464. rc = ata_dev_read_id(ata_dev, &ata_dev->class,
  465. ATA_READID_POSTRESET,ata_dev->id);
  466. if (rc) {
  467. printf("sata_dwc : error. failed sata scan\n");
  468. return 1;
  469. }
  470. /* SATA drives indicate we have a bridge. We don't know which
  471. * end of the link the bridge is which is a problem
  472. */
  473. if (ata_id_is_sata(ata_dev->id))
  474. ap.cbl = ATA_CBL_SATA;
  475. id = ata_dev->id;
  476. ata_dev->flags &= ~ATA_DFLAG_CFG_MASK;
  477. ata_dev->max_sectors = 0;
  478. ata_dev->cdb_len = 0;
  479. ata_dev->n_sectors = 0;
  480. ata_dev->cylinders = 0;
  481. ata_dev->heads = 0;
  482. ata_dev->sectors = 0;
  483. if (id[ATA_ID_FIELD_VALID] & (1 << 1)) {
  484. pio_mask = id[ATA_ID_PIO_MODES] & 0x03;
  485. pio_mask <<= 3;
  486. pio_mask |= 0x7;
  487. } else {
  488. /* If word 64 isn't valid then Word 51 high byte holds
  489. * the PIO timing number for the maximum. Turn it into
  490. * a mask.
  491. */
  492. u8 mode = (id[ATA_ID_OLD_PIO_MODES] >> 8) & 0xFF;
  493. if (mode < 5) {
  494. pio_mask = (2 << mode) - 1;
  495. } else {
  496. pio_mask = 1;
  497. }
  498. }
  499. mwdma_mask = id[ATA_ID_MWDMA_MODES] & 0x07;
  500. if (ata_id_is_cfa(id)) {
  501. int pio = id[163] & 0x7;
  502. int dma = (id[163] >> 3) & 7;
  503. if (pio)
  504. pio_mask |= (1 << 5);
  505. if (pio > 1)
  506. pio_mask |= (1 << 6);
  507. if (dma)
  508. mwdma_mask |= (1 << 3);
  509. if (dma > 1)
  510. mwdma_mask |= (1 << 4);
  511. }
  512. if (ata_dev->class == ATA_DEV_ATA) {
  513. if (ata_id_is_cfa(id)) {
  514. if (id[162] & 1)
  515. printf("supports DRM functions and may "
  516. "not be fully accessable.\n");
  517. sprintf(revbuf, "%s", "CFA");
  518. } else {
  519. if (ata_id_has_tpm(id))
  520. printf("supports DRM functions and may "
  521. "not be fully accessable.\n");
  522. }
  523. ata_dev->n_sectors = ata_id_n_sectors((u16*)id);
  524. if (ata_dev->id[59] & 0x100)
  525. ata_dev->multi_count = ata_dev->id[59] & 0xff;
  526. if (ata_id_has_lba(id)) {
  527. char ncq_desc[20];
  528. ata_dev->flags |= ATA_DFLAG_LBA;
  529. if (ata_id_has_lba48(id)) {
  530. ata_dev->flags |= ATA_DFLAG_LBA48;
  531. if (ata_dev->n_sectors >= (1UL << 28) &&
  532. ata_id_has_flush_ext(id))
  533. ata_dev->flags |= ATA_DFLAG_FLUSH_EXT;
  534. }
  535. if (!ata_id_has_ncq(ata_dev->id))
  536. ncq_desc[0] = '\0';
  537. if (ata_dev->horkage & ATA_HORKAGE_NONCQ)
  538. sprintf(ncq_desc, "%s", "NCQ (not used)");
  539. if (ap.flags & ATA_FLAG_NCQ)
  540. ata_dev->flags |= ATA_DFLAG_NCQ;
  541. }
  542. ata_dev->cdb_len = 16;
  543. }
  544. ata_dev->max_sectors = ATA_MAX_SECTORS;
  545. if (ata_dev->flags & ATA_DFLAG_LBA48)
  546. ata_dev->max_sectors = ATA_MAX_SECTORS_LBA48;
  547. if (!(ata_dev->horkage & ATA_HORKAGE_IPM)) {
  548. if (ata_id_has_hipm(ata_dev->id))
  549. ata_dev->flags |= ATA_DFLAG_HIPM;
  550. if (ata_id_has_dipm(ata_dev->id))
  551. ata_dev->flags |= ATA_DFLAG_DIPM;
  552. }
  553. if ((ap.cbl == ATA_CBL_SATA) && (!ata_id_is_sata(ata_dev->id))) {
  554. ata_dev->udma_mask &= ATA_UDMA5;
  555. ata_dev->max_sectors = ATA_MAX_SECTORS;
  556. }
  557. if (ata_dev->horkage & ATA_HORKAGE_DIAGNOSTIC) {
  558. printf("Drive reports diagnostics failure."
  559. "This may indicate a drive\n");
  560. printf("fault or invalid emulation."
  561. "Contact drive vendor for information.\n");
  562. }
  563. rc = check_sata_dev_state();
  564. ata_id_c_string(ata_dev->id,
  565. (unsigned char *)sata_dev_desc[dev].revision,
  566. ATA_ID_FW_REV, sizeof(sata_dev_desc[dev].revision));
  567. ata_id_c_string(ata_dev->id,
  568. (unsigned char *)sata_dev_desc[dev].vendor,
  569. ATA_ID_PROD, sizeof(sata_dev_desc[dev].vendor));
  570. ata_id_c_string(ata_dev->id,
  571. (unsigned char *)sata_dev_desc[dev].product,
  572. ATA_ID_SERNO, sizeof(sata_dev_desc[dev].product));
  573. sata_dev_desc[dev].lba = (u32) ata_dev->n_sectors;
  574. #ifdef CONFIG_LBA48
  575. if (ata_dev->id[83] & (1 << 10)) {
  576. sata_dev_desc[dev].lba48 = 1;
  577. } else {
  578. sata_dev_desc[dev].lba48 = 0;
  579. }
  580. #endif
  581. return 0;
  582. }
  583. static u8 ata_busy_wait(struct ata_port *ap,
  584. unsigned int bits,unsigned int max)
  585. {
  586. u8 status;
  587. do {
  588. udelay(10);
  589. status = ata_check_status(ap);
  590. max--;
  591. } while (status != 0xff && (status & bits) && (max > 0));
  592. return status;
  593. }
  594. static int ata_dev_read_id(struct ata_device *dev, unsigned int *p_class,
  595. unsigned int flags, u16 *id)
  596. {
  597. struct ata_port *ap = pap;
  598. unsigned int class = *p_class;
  599. struct ata_taskfile tf;
  600. unsigned int err_mask = 0;
  601. const char *reason;
  602. int may_fallback = 1, tried_spinup = 0;
  603. u8 status;
  604. int rc;
  605. status = ata_busy_wait(ap, ATA_BUSY, 30000);
  606. if (status & ATA_BUSY) {
  607. printf("BSY = 0 check. timeout.\n");
  608. rc = false;
  609. return rc;
  610. }
  611. ata_dev_select(ap, dev->devno, 1, 1);
  612. retry:
  613. memset(&tf, 0, sizeof(tf));
  614. ap->print_id = 1;
  615. ap->flags &= ~ATA_FLAG_DISABLED;
  616. tf.ctl = ap->ctl;
  617. tf.device = ATA_DEVICE_OBS;
  618. tf.command = ATA_CMD_ID_ATA;
  619. tf.protocol = ATA_PROT_PIO;
  620. /* Some devices choke if TF registers contain garbage. Make
  621. * sure those are properly initialized.
  622. */
  623. tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
  624. /* Device presence detection is unreliable on some
  625. * controllers. Always poll IDENTIFY if available.
  626. */
  627. tf.flags |= ATA_TFLAG_POLLING;
  628. temp_n_block = 1;
  629. err_mask = ata_exec_internal(dev, &tf, NULL, DMA_FROM_DEVICE,
  630. sizeof(id[0]) * ATA_ID_WORDS, 0);
  631. if (err_mask) {
  632. if (err_mask & AC_ERR_NODEV_HINT) {
  633. printf("NODEV after polling detection\n");
  634. return -ENOENT;
  635. }
  636. if ((err_mask == AC_ERR_DEV) && (tf.feature & ATA_ABORTED)) {
  637. /* Device or controller might have reported
  638. * the wrong device class. Give a shot at the
  639. * other IDENTIFY if the current one is
  640. * aborted by the device.
  641. */
  642. if (may_fallback) {
  643. may_fallback = 0;
  644. if (class == ATA_DEV_ATA) {
  645. class = ATA_DEV_ATAPI;
  646. } else {
  647. class = ATA_DEV_ATA;
  648. }
  649. goto retry;
  650. }
  651. /* Control reaches here iff the device aborted
  652. * both flavors of IDENTIFYs which happens
  653. * sometimes with phantom devices.
  654. */
  655. printf("both IDENTIFYs aborted, assuming NODEV\n");
  656. return -ENOENT;
  657. }
  658. rc = -EIO;
  659. reason = "I/O error";
  660. goto err_out;
  661. }
  662. /* Falling back doesn't make sense if ID data was read
  663. * successfully at least once.
  664. */
  665. may_fallback = 0;
  666. unsigned int id_cnt;
  667. for (id_cnt = 0; id_cnt < ATA_ID_WORDS; id_cnt++)
  668. id[id_cnt] = le16_to_cpu(id[id_cnt]);
  669. rc = -EINVAL;
  670. reason = "device reports invalid type";
  671. if (class == ATA_DEV_ATA) {
  672. if (!ata_id_is_ata(id) && !ata_id_is_cfa(id))
  673. goto err_out;
  674. } else {
  675. if (ata_id_is_ata(id))
  676. goto err_out;
  677. }
  678. if (!tried_spinup && (id[2] == 0x37c8 || id[2] == 0x738c)) {
  679. tried_spinup = 1;
  680. /*
  681. * Drive powered-up in standby mode, and requires a specific
  682. * SET_FEATURES spin-up subcommand before it will accept
  683. * anything other than the original IDENTIFY command.
  684. */
  685. err_mask = ata_dev_set_feature(dev, SETFEATURES_SPINUP, 0);
  686. if (err_mask && id[2] != 0x738c) {
  687. rc = -EIO;
  688. reason = "SPINUP failed";
  689. goto err_out;
  690. }
  691. /*
  692. * If the drive initially returned incomplete IDENTIFY info,
  693. * we now must reissue the IDENTIFY command.
  694. */
  695. if (id[2] == 0x37c8)
  696. goto retry;
  697. }
  698. if ((flags & ATA_READID_POSTRESET) && class == ATA_DEV_ATA) {
  699. /*
  700. * The exact sequence expected by certain pre-ATA4 drives is:
  701. * SRST RESET
  702. * IDENTIFY (optional in early ATA)
  703. * INITIALIZE DEVICE PARAMETERS (later IDE and ATA)
  704. * anything else..
  705. * Some drives were very specific about that exact sequence.
  706. *
  707. * Note that ATA4 says lba is mandatory so the second check
  708. * shoud never trigger.
  709. */
  710. if (ata_id_major_version(id) < 4 || !ata_id_has_lba(id)) {
  711. err_mask = ata_dev_init_params(dev, id[3], id[6]);
  712. if (err_mask) {
  713. rc = -EIO;
  714. reason = "INIT_DEV_PARAMS failed";
  715. goto err_out;
  716. }
  717. /* current CHS translation info (id[53-58]) might be
  718. * changed. reread the identify device info.
  719. */
  720. flags &= ~ATA_READID_POSTRESET;
  721. goto retry;
  722. }
  723. }
  724. *p_class = class;
  725. return 0;
  726. err_out:
  727. printf("failed to READ ID (%s, err_mask=0x%x)\n", reason, err_mask);
  728. return rc;
  729. }
  730. static u8 ata_wait_idle(struct ata_port *ap)
  731. {
  732. u8 status = ata_busy_wait(ap, ATA_BUSY | ATA_DRQ, 1000);
  733. return status;
  734. }
  735. static void ata_dev_select(struct ata_port *ap, unsigned int device,
  736. unsigned int wait, unsigned int can_sleep)
  737. {
  738. if (wait)
  739. ata_wait_idle(ap);
  740. ata_std_dev_select(ap, device);
  741. if (wait)
  742. ata_wait_idle(ap);
  743. }
  744. static void ata_std_dev_select(struct ata_port *ap, unsigned int device)
  745. {
  746. u8 tmp;
  747. if (device == 0) {
  748. tmp = ATA_DEVICE_OBS;
  749. } else {
  750. tmp = ATA_DEVICE_OBS | ATA_DEV1;
  751. }
  752. writeb(tmp, ap->ioaddr.device_addr);
  753. readb(ap->ioaddr.altstatus_addr);
  754. udelay(1);
  755. }
  756. static int waiting_for_reg_state(volatile u8 *offset,
  757. int timeout_msec,
  758. u32 sign)
  759. {
  760. int i;
  761. u32 status;
  762. for (i = 0; i < timeout_msec; i++) {
  763. status = readl(offset);
  764. if ((status & sign) != 0)
  765. break;
  766. msleep(1);
  767. }
  768. return (i < timeout_msec) ? 0 : -1;
  769. }
  770. static void ata_qc_reinit(struct ata_queued_cmd *qc)
  771. {
  772. qc->dma_dir = DMA_NONE;
  773. qc->flags = 0;
  774. qc->nbytes = qc->extrabytes = qc->curbytes = 0;
  775. qc->n_elem = 0;
  776. qc->err_mask = 0;
  777. qc->sect_size = ATA_SECT_SIZE;
  778. qc->nbytes = ATA_SECT_SIZE * temp_n_block;
  779. memset(&qc->tf, 0, sizeof(qc->tf));
  780. qc->tf.ctl = 0;
  781. qc->tf.device = ATA_DEVICE_OBS;
  782. qc->result_tf.command = ATA_DRDY;
  783. qc->result_tf.feature = 0;
  784. }
  785. struct ata_queued_cmd *__ata_qc_from_tag(struct ata_port *ap,
  786. unsigned int tag)
  787. {
  788. if (tag < ATA_MAX_QUEUE)
  789. return &ap->qcmd[tag];
  790. return NULL;
  791. }
  792. static void __ata_port_freeze(struct ata_port *ap)
  793. {
  794. printf("set port freeze.\n");
  795. ap->pflags |= ATA_PFLAG_FROZEN;
  796. }
  797. static int ata_port_freeze(struct ata_port *ap)
  798. {
  799. __ata_port_freeze(ap);
  800. return 0;
  801. }
  802. unsigned ata_exec_internal(struct ata_device *dev,
  803. struct ata_taskfile *tf, const u8 *cdb,
  804. int dma_dir, unsigned int buflen,
  805. unsigned long timeout)
  806. {
  807. struct ata_link *link = dev->link;
  808. struct ata_port *ap = pap;
  809. struct ata_queued_cmd *qc;
  810. unsigned int tag, preempted_tag;
  811. u32 preempted_sactive, preempted_qc_active;
  812. int preempted_nr_active_links;
  813. unsigned int err_mask;
  814. int rc = 0;
  815. u8 status;
  816. status = ata_busy_wait(ap, ATA_BUSY, 300000);
  817. if (status & ATA_BUSY) {
  818. printf("BSY = 0 check. timeout.\n");
  819. rc = false;
  820. return rc;
  821. }
  822. if (ap->pflags & ATA_PFLAG_FROZEN)
  823. return AC_ERR_SYSTEM;
  824. tag = ATA_TAG_INTERNAL;
  825. if (test_and_set_bit(tag, &ap->qc_allocated)) {
  826. rc = false;
  827. return rc;
  828. }
  829. qc = __ata_qc_from_tag(ap, tag);
  830. qc->tag = tag;
  831. qc->ap = ap;
  832. qc->dev = dev;
  833. ata_qc_reinit(qc);
  834. preempted_tag = link->active_tag;
  835. preempted_sactive = link->sactive;
  836. preempted_qc_active = ap->qc_active;
  837. preempted_nr_active_links = ap->nr_active_links;
  838. link->active_tag = ATA_TAG_POISON;
  839. link->sactive = 0;
  840. ap->qc_active = 0;
  841. ap->nr_active_links = 0;
  842. qc->tf = *tf;
  843. if (cdb)
  844. memcpy(qc->cdb, cdb, ATAPI_CDB_LEN);
  845. qc->flags |= ATA_QCFLAG_RESULT_TF;
  846. qc->dma_dir = dma_dir;
  847. qc->private_data = 0;
  848. ata_qc_issue(qc);
  849. if (!timeout)
  850. timeout = ata_probe_timeout * 1000 / HZ;
  851. status = ata_busy_wait(ap, ATA_BUSY, 30000);
  852. if (status & ATA_BUSY) {
  853. printf("BSY = 0 check. timeout.\n");
  854. printf("altstatus = 0x%x.\n", status);
  855. qc->err_mask |= AC_ERR_OTHER;
  856. return qc->err_mask;
  857. }
  858. if (waiting_for_reg_state(ap->ioaddr.altstatus_addr, 1000, 0x8)) {
  859. u8 status = 0;
  860. u8 errorStatus = 0;
  861. status = readb(ap->ioaddr.altstatus_addr);
  862. if ((status & 0x01) != 0) {
  863. errorStatus = readb(ap->ioaddr.feature_addr);
  864. if (errorStatus == 0x04 &&
  865. qc->tf.command == ATA_CMD_PIO_READ_EXT){
  866. printf("Hard Disk doesn't support LBA48\n");
  867. dev_state = SATA_ERROR;
  868. qc->err_mask |= AC_ERR_OTHER;
  869. return qc->err_mask;
  870. }
  871. }
  872. qc->err_mask |= AC_ERR_OTHER;
  873. return qc->err_mask;
  874. }
  875. status = ata_busy_wait(ap, ATA_BUSY, 10);
  876. if (status & ATA_BUSY) {
  877. printf("BSY = 0 check. timeout.\n");
  878. qc->err_mask |= AC_ERR_OTHER;
  879. return qc->err_mask;
  880. }
  881. ata_pio_task(ap);
  882. if (!rc) {
  883. if (qc->flags & ATA_QCFLAG_ACTIVE) {
  884. qc->err_mask |= AC_ERR_TIMEOUT;
  885. ata_port_freeze(ap);
  886. }
  887. }
  888. if (qc->flags & ATA_QCFLAG_FAILED) {
  889. if (qc->result_tf.command & (ATA_ERR | ATA_DF))
  890. qc->err_mask |= AC_ERR_DEV;
  891. if (!qc->err_mask)
  892. qc->err_mask |= AC_ERR_OTHER;
  893. if (qc->err_mask & ~AC_ERR_OTHER)
  894. qc->err_mask &= ~AC_ERR_OTHER;
  895. }
  896. *tf = qc->result_tf;
  897. err_mask = qc->err_mask;
  898. ata_qc_free(qc);
  899. link->active_tag = preempted_tag;
  900. link->sactive = preempted_sactive;
  901. ap->qc_active = preempted_qc_active;
  902. ap->nr_active_links = preempted_nr_active_links;
  903. if (ap->flags & ATA_FLAG_DISABLED) {
  904. err_mask |= AC_ERR_SYSTEM;
  905. ap->flags &= ~ATA_FLAG_DISABLED;
  906. }
  907. return err_mask;
  908. }
  909. static void ata_qc_issue(struct ata_queued_cmd *qc)
  910. {
  911. struct ata_port *ap = qc->ap;
  912. struct ata_link *link = qc->dev->link;
  913. u8 prot = qc->tf.protocol;
  914. if (ata_is_ncq(prot)) {
  915. if (!link->sactive)
  916. ap->nr_active_links++;
  917. link->sactive |= 1 << qc->tag;
  918. } else {
  919. ap->nr_active_links++;
  920. link->active_tag = qc->tag;
  921. }
  922. qc->flags |= ATA_QCFLAG_ACTIVE;
  923. ap->qc_active |= 1 << qc->tag;
  924. if (qc->dev->flags & ATA_DFLAG_SLEEPING) {
  925. msleep(1);
  926. return;
  927. }
  928. qc->err_mask |= ata_qc_issue_prot(qc);
  929. if (qc->err_mask)
  930. goto err;
  931. return;
  932. err:
  933. ata_qc_complete(qc);
  934. }
  935. static unsigned int ata_qc_issue_prot(struct ata_queued_cmd *qc)
  936. {
  937. struct ata_port *ap = qc->ap;
  938. if (ap->flags & ATA_FLAG_PIO_POLLING) {
  939. switch (qc->tf.protocol) {
  940. case ATA_PROT_PIO:
  941. case ATA_PROT_NODATA:
  942. case ATAPI_PROT_PIO:
  943. case ATAPI_PROT_NODATA:
  944. qc->tf.flags |= ATA_TFLAG_POLLING;
  945. break;
  946. default:
  947. break;
  948. }
  949. }
  950. ata_dev_select(ap, qc->dev->devno, 1, 0);
  951. switch (qc->tf.protocol) {
  952. case ATA_PROT_PIO:
  953. if (qc->tf.flags & ATA_TFLAG_POLLING)
  954. qc->tf.ctl |= ATA_NIEN;
  955. ata_tf_to_host(ap, &qc->tf);
  956. ap->hsm_task_state = HSM_ST;
  957. if (qc->tf.flags & ATA_TFLAG_POLLING)
  958. ata_pio_queue_task(ap, qc, 0);
  959. break;
  960. default:
  961. return AC_ERR_SYSTEM;
  962. }
  963. return 0;
  964. }
  965. static void ata_tf_to_host(struct ata_port *ap,
  966. const struct ata_taskfile *tf)
  967. {
  968. ata_tf_load(ap, tf);
  969. ata_exec_command(ap, tf);
  970. }
  971. static void ata_tf_load(struct ata_port *ap,
  972. const struct ata_taskfile *tf)
  973. {
  974. struct ata_ioports *ioaddr = &ap->ioaddr;
  975. unsigned int is_addr = tf->flags & ATA_TFLAG_ISADDR;
  976. if (tf->ctl != ap->last_ctl) {
  977. if (ioaddr->ctl_addr)
  978. writeb(tf->ctl, ioaddr->ctl_addr);
  979. ap->last_ctl = tf->ctl;
  980. ata_wait_idle(ap);
  981. }
  982. if (is_addr && (tf->flags & ATA_TFLAG_LBA48)) {
  983. writeb(tf->hob_feature, ioaddr->feature_addr);
  984. writeb(tf->hob_nsect, ioaddr->nsect_addr);
  985. writeb(tf->hob_lbal, ioaddr->lbal_addr);
  986. writeb(tf->hob_lbam, ioaddr->lbam_addr);
  987. writeb(tf->hob_lbah, ioaddr->lbah_addr);
  988. }
  989. if (is_addr) {
  990. writeb(tf->feature, ioaddr->feature_addr);
  991. writeb(tf->nsect, ioaddr->nsect_addr);
  992. writeb(tf->lbal, ioaddr->lbal_addr);
  993. writeb(tf->lbam, ioaddr->lbam_addr);
  994. writeb(tf->lbah, ioaddr->lbah_addr);
  995. }
  996. if (tf->flags & ATA_TFLAG_DEVICE)
  997. writeb(tf->device, ioaddr->device_addr);
  998. ata_wait_idle(ap);
  999. }
  1000. static void ata_exec_command(struct ata_port *ap,
  1001. const struct ata_taskfile *tf)
  1002. {
  1003. writeb(tf->command, ap->ioaddr.command_addr);
  1004. readb(ap->ioaddr.altstatus_addr);
  1005. udelay(1);
  1006. }
  1007. static void ata_pio_queue_task(struct ata_port *ap,
  1008. void *data,unsigned long delay)
  1009. {
  1010. ap->port_task_data = data;
  1011. }
  1012. static unsigned int ac_err_mask(u8 status)
  1013. {
  1014. if (status & (ATA_BUSY | ATA_DRQ))
  1015. return AC_ERR_HSM;
  1016. if (status & (ATA_ERR | ATA_DF))
  1017. return AC_ERR_DEV;
  1018. return 0;
  1019. }
  1020. static unsigned int __ac_err_mask(u8 status)
  1021. {
  1022. unsigned int mask = ac_err_mask(status);
  1023. if (mask == 0)
  1024. return AC_ERR_OTHER;
  1025. return mask;
  1026. }
  1027. static void ata_pio_task(struct ata_port *arg_ap)
  1028. {
  1029. struct ata_port *ap = arg_ap;
  1030. struct ata_queued_cmd *qc = ap->port_task_data;
  1031. u8 status;
  1032. int poll_next;
  1033. fsm_start:
  1034. /*
  1035. * This is purely heuristic. This is a fast path.
  1036. * Sometimes when we enter, BSY will be cleared in
  1037. * a chk-status or two. If not, the drive is probably seeking
  1038. * or something. Snooze for a couple msecs, then
  1039. * chk-status again. If still busy, queue delayed work.
  1040. */
  1041. status = ata_busy_wait(ap, ATA_BUSY, 5);
  1042. if (status & ATA_BUSY) {
  1043. msleep(2);
  1044. status = ata_busy_wait(ap, ATA_BUSY, 10);
  1045. if (status & ATA_BUSY) {
  1046. ata_pio_queue_task(ap, qc, ATA_SHORT_PAUSE);
  1047. return;
  1048. }
  1049. }
  1050. poll_next = ata_hsm_move(ap, qc, status, 1);
  1051. /* another command or interrupt handler
  1052. * may be running at this point.
  1053. */
  1054. if (poll_next)
  1055. goto fsm_start;
  1056. }
  1057. static int ata_hsm_move(struct ata_port *ap, struct ata_queued_cmd *qc,
  1058. u8 status, int in_wq)
  1059. {
  1060. int poll_next;
  1061. fsm_start:
  1062. switch (ap->hsm_task_state) {
  1063. case HSM_ST_FIRST:
  1064. poll_next = (qc->tf.flags & ATA_TFLAG_POLLING);
  1065. if ((status & ATA_DRQ) == 0) {
  1066. if (status & (ATA_ERR | ATA_DF)) {
  1067. qc->err_mask |= AC_ERR_DEV;
  1068. } else {
  1069. qc->err_mask |= AC_ERR_HSM;
  1070. }
  1071. ap->hsm_task_state = HSM_ST_ERR;
  1072. goto fsm_start;
  1073. }
  1074. /* Device should not ask for data transfer (DRQ=1)
  1075. * when it finds something wrong.
  1076. * We ignore DRQ here and stop the HSM by
  1077. * changing hsm_task_state to HSM_ST_ERR and
  1078. * let the EH abort the command or reset the device.
  1079. */
  1080. if (status & (ATA_ERR | ATA_DF)) {
  1081. if (!(qc->dev->horkage & ATA_HORKAGE_STUCK_ERR)) {
  1082. printf("DRQ=1 with device error, "
  1083. "dev_stat 0x%X\n", status);
  1084. qc->err_mask |= AC_ERR_HSM;
  1085. ap->hsm_task_state = HSM_ST_ERR;
  1086. goto fsm_start;
  1087. }
  1088. }
  1089. if (qc->tf.protocol == ATA_PROT_PIO) {
  1090. /* PIO data out protocol.
  1091. * send first data block.
  1092. */
  1093. /* ata_pio_sectors() might change the state
  1094. * to HSM_ST_LAST. so, the state is changed here
  1095. * before ata_pio_sectors().
  1096. */
  1097. ap->hsm_task_state = HSM_ST;
  1098. ata_pio_sectors(qc);
  1099. } else {
  1100. printf("protocol is not ATA_PROT_PIO \n");
  1101. }
  1102. break;
  1103. case HSM_ST:
  1104. if ((status & ATA_DRQ) == 0) {
  1105. if (status & (ATA_ERR | ATA_DF)) {
  1106. qc->err_mask |= AC_ERR_DEV;
  1107. } else {
  1108. /* HSM violation. Let EH handle this.
  1109. * Phantom devices also trigger this
  1110. * condition. Mark hint.
  1111. */
  1112. qc->err_mask |= AC_ERR_HSM | AC_ERR_NODEV_HINT;
  1113. }
  1114. ap->hsm_task_state = HSM_ST_ERR;
  1115. goto fsm_start;
  1116. }
  1117. /* For PIO reads, some devices may ask for
  1118. * data transfer (DRQ=1) alone with ERR=1.
  1119. * We respect DRQ here and transfer one
  1120. * block of junk data before changing the
  1121. * hsm_task_state to HSM_ST_ERR.
  1122. *
  1123. * For PIO writes, ERR=1 DRQ=1 doesn't make
  1124. * sense since the data block has been
  1125. * transferred to the device.
  1126. */
  1127. if (status & (ATA_ERR | ATA_DF)) {
  1128. qc->err_mask |= AC_ERR_DEV;
  1129. if (!(qc->tf.flags & ATA_TFLAG_WRITE)) {
  1130. ata_pio_sectors(qc);
  1131. status = ata_wait_idle(ap);
  1132. }
  1133. if (status & (ATA_BUSY | ATA_DRQ))
  1134. qc->err_mask |= AC_ERR_HSM;
  1135. /* ata_pio_sectors() might change the
  1136. * state to HSM_ST_LAST. so, the state
  1137. * is changed after ata_pio_sectors().
  1138. */
  1139. ap->hsm_task_state = HSM_ST_ERR;
  1140. goto fsm_start;
  1141. }
  1142. ata_pio_sectors(qc);
  1143. if (ap->hsm_task_state == HSM_ST_LAST &&
  1144. (!(qc->tf.flags & ATA_TFLAG_WRITE))) {
  1145. status = ata_wait_idle(ap);
  1146. goto fsm_start;
  1147. }
  1148. poll_next = 1;
  1149. break;
  1150. case HSM_ST_LAST:
  1151. if (!ata_ok(status)) {
  1152. qc->err_mask |= __ac_err_mask(status);
  1153. ap->hsm_task_state = HSM_ST_ERR;
  1154. goto fsm_start;
  1155. }
  1156. ap->hsm_task_state = HSM_ST_IDLE;
  1157. ata_hsm_qc_complete(qc, in_wq);
  1158. poll_next = 0;
  1159. break;
  1160. case HSM_ST_ERR:
  1161. /* make sure qc->err_mask is available to
  1162. * know what's wrong and recover
  1163. */
  1164. ap->hsm_task_state = HSM_ST_IDLE;
  1165. ata_hsm_qc_complete(qc, in_wq);
  1166. poll_next = 0;
  1167. break;
  1168. default:
  1169. poll_next = 0;
  1170. }
  1171. return poll_next;
  1172. }
  1173. static void ata_pio_sectors(struct ata_queued_cmd *qc)
  1174. {
  1175. struct ata_port *ap;
  1176. ap = pap;
  1177. qc->pdata = ap->pdata;
  1178. ata_pio_sector(qc);
  1179. readb(qc->ap->ioaddr.altstatus_addr);
  1180. udelay(1);
  1181. }
  1182. static void ata_pio_sector(struct ata_queued_cmd *qc)
  1183. {
  1184. int do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
  1185. struct ata_port *ap = qc->ap;
  1186. unsigned int offset;
  1187. unsigned char *buf;
  1188. char temp_data_buf[512];
  1189. if (qc->curbytes == qc->nbytes - qc->sect_size)
  1190. ap->hsm_task_state = HSM_ST_LAST;
  1191. offset = qc->curbytes;
  1192. switch (qc->tf.command) {
  1193. case ATA_CMD_ID_ATA:
  1194. buf = (unsigned char *)&ata_device.id[0];
  1195. break;
  1196. case ATA_CMD_PIO_READ_EXT:
  1197. case ATA_CMD_PIO_READ:
  1198. case ATA_CMD_PIO_WRITE_EXT:
  1199. case ATA_CMD_PIO_WRITE:
  1200. buf = qc->pdata + offset;
  1201. break;
  1202. default:
  1203. buf = (unsigned char *)&temp_data_buf[0];
  1204. }
  1205. ata_mmio_data_xfer(qc->dev, buf, qc->sect_size, do_write);
  1206. qc->curbytes += qc->sect_size;
  1207. }
  1208. static void ata_mmio_data_xfer(struct ata_device *dev, unsigned char *buf,
  1209. unsigned int buflen, int do_write)
  1210. {
  1211. struct ata_port *ap = pap;
  1212. void __iomem *data_addr = ap->ioaddr.data_addr;
  1213. unsigned int words = buflen >> 1;
  1214. u16 *buf16 = (u16 *)buf;
  1215. unsigned int i = 0;
  1216. udelay(100);
  1217. if (do_write) {
  1218. for (i = 0; i < words; i++)
  1219. writew(le16_to_cpu(buf16[i]), data_addr);
  1220. } else {
  1221. for (i = 0; i < words; i++)
  1222. buf16[i] = cpu_to_le16(readw(data_addr));
  1223. }
  1224. if (buflen & 0x01) {
  1225. __le16 align_buf[1] = { 0 };
  1226. unsigned char *trailing_buf = buf + buflen - 1;
  1227. if (do_write) {
  1228. memcpy(align_buf, trailing_buf, 1);
  1229. writew(le16_to_cpu(align_buf[0]), data_addr);
  1230. } else {
  1231. align_buf[0] = cpu_to_le16(readw(data_addr));
  1232. memcpy(trailing_buf, align_buf, 1);
  1233. }
  1234. }
  1235. }
  1236. static void ata_hsm_qc_complete(struct ata_queued_cmd *qc, int in_wq)
  1237. {
  1238. struct ata_port *ap = qc->ap;
  1239. if (in_wq) {
  1240. /* EH might have kicked in while host lock is
  1241. * released.
  1242. */
  1243. qc = &ap->qcmd[qc->tag];
  1244. if (qc) {
  1245. if (!(qc->err_mask & AC_ERR_HSM)) {
  1246. ata_irq_on(ap);
  1247. ata_qc_complete(qc);
  1248. } else {
  1249. ata_port_freeze(ap);
  1250. }
  1251. }
  1252. } else {
  1253. if (!(qc->err_mask & AC_ERR_HSM)) {
  1254. ata_qc_complete(qc);
  1255. } else {
  1256. ata_port_freeze(ap);
  1257. }
  1258. }
  1259. }
  1260. static u8 ata_irq_on(struct ata_port *ap)
  1261. {
  1262. struct ata_ioports *ioaddr = &ap->ioaddr;
  1263. u8 tmp;
  1264. ap->ctl &= ~ATA_NIEN;
  1265. ap->last_ctl = ap->ctl;
  1266. if (ioaddr->ctl_addr)
  1267. writeb(ap->ctl, ioaddr->ctl_addr);
  1268. tmp = ata_wait_idle(ap);
  1269. return tmp;
  1270. }
  1271. static unsigned int ata_tag_internal(unsigned int tag)
  1272. {
  1273. return tag == ATA_MAX_QUEUE - 1;
  1274. }
  1275. static void ata_qc_complete(struct ata_queued_cmd *qc)
  1276. {
  1277. struct ata_device *dev = qc->dev;
  1278. if (qc->err_mask)
  1279. qc->flags |= ATA_QCFLAG_FAILED;
  1280. if (qc->flags & ATA_QCFLAG_FAILED) {
  1281. if (!ata_tag_internal(qc->tag)) {
  1282. fill_result_tf(qc);
  1283. return;
  1284. }
  1285. }
  1286. if (qc->flags & ATA_QCFLAG_RESULT_TF)
  1287. fill_result_tf(qc);
  1288. /* Some commands need post-processing after successful
  1289. * completion.
  1290. */
  1291. switch (qc->tf.command) {
  1292. case ATA_CMD_SET_FEATURES:
  1293. if (qc->tf.feature != SETFEATURES_WC_ON &&
  1294. qc->tf.feature != SETFEATURES_WC_OFF)
  1295. break;
  1296. case ATA_CMD_INIT_DEV_PARAMS:
  1297. case ATA_CMD_SET_MULTI:
  1298. break;
  1299. case ATA_CMD_SLEEP:
  1300. dev->flags |= ATA_DFLAG_SLEEPING;
  1301. break;
  1302. }
  1303. __ata_qc_complete(qc);
  1304. }
  1305. static void fill_result_tf(struct ata_queued_cmd *qc)
  1306. {
  1307. struct ata_port *ap = qc->ap;
  1308. qc->result_tf.flags = qc->tf.flags;
  1309. ata_tf_read(ap, &qc->result_tf);
  1310. }
  1311. static void ata_tf_read(struct ata_port *ap, struct ata_taskfile *tf)
  1312. {
  1313. struct ata_ioports *ioaddr = &ap->ioaddr;
  1314. tf->command = ata_check_status(ap);
  1315. tf->feature = readb(ioaddr->error_addr);
  1316. tf->nsect = readb(ioaddr->nsect_addr);
  1317. tf->lbal = readb(ioaddr->lbal_addr);
  1318. tf->lbam = readb(ioaddr->lbam_addr);
  1319. tf->lbah = readb(ioaddr->lbah_addr);
  1320. tf->device = readb(ioaddr->device_addr);
  1321. if (tf->flags & ATA_TFLAG_LBA48) {
  1322. if (ioaddr->ctl_addr) {
  1323. writeb(tf->ctl | ATA_HOB, ioaddr->ctl_addr);
  1324. tf->hob_feature = readb(ioaddr->error_addr);
  1325. tf->hob_nsect = readb(ioaddr->nsect_addr);
  1326. tf->hob_lbal = readb(ioaddr->lbal_addr);
  1327. tf->hob_lbam = readb(ioaddr->lbam_addr);
  1328. tf->hob_lbah = readb(ioaddr->lbah_addr);
  1329. writeb(tf->ctl, ioaddr->ctl_addr);
  1330. ap->last_ctl = tf->ctl;
  1331. } else {
  1332. printf("sata_dwc warnning register read.\n");
  1333. }
  1334. }
  1335. }
  1336. static void __ata_qc_complete(struct ata_queued_cmd *qc)
  1337. {
  1338. struct ata_port *ap = qc->ap;
  1339. struct ata_link *link = qc->dev->link;
  1340. link->active_tag = ATA_TAG_POISON;
  1341. ap->nr_active_links--;
  1342. if (qc->flags & ATA_QCFLAG_CLEAR_EXCL && ap->excl_link == link)
  1343. ap->excl_link = NULL;
  1344. qc->flags &= ~ATA_QCFLAG_ACTIVE;
  1345. ap->qc_active &= ~(1 << qc->tag);
  1346. }
  1347. static void ata_qc_free(struct ata_queued_cmd *qc)
  1348. {
  1349. struct ata_port *ap = qc->ap;
  1350. unsigned int tag;
  1351. qc->flags = 0;
  1352. tag = qc->tag;
  1353. if (tag < ATA_MAX_QUEUE) {
  1354. qc->tag = ATA_TAG_POISON;
  1355. clear_bit(tag, &ap->qc_allocated);
  1356. }
  1357. }
  1358. static int check_sata_dev_state(void)
  1359. {
  1360. unsigned long datalen;
  1361. unsigned char *pdata;
  1362. int ret = 0;
  1363. int i = 0;
  1364. char temp_data_buf[512];
  1365. while (1) {
  1366. udelay(10000);
  1367. pdata = (unsigned char*)&temp_data_buf[0];
  1368. datalen = 512;
  1369. ret = ata_dev_read_sectors(pdata, datalen, 0, 1);
  1370. if (ret == true)
  1371. break;
  1372. i++;
  1373. if (i > (ATA_RESET_TIME * 100)) {
  1374. printf("** TimeOUT **\n");
  1375. dev_state = SATA_NODEVICE;
  1376. return false;
  1377. }
  1378. if ((i >= 100) && ((i % 100) == 0))
  1379. printf(".");
  1380. }
  1381. dev_state = SATA_READY;
  1382. return true;
  1383. }
  1384. static unsigned int ata_dev_set_feature(struct ata_device *dev,
  1385. u8 enable, u8 feature)
  1386. {
  1387. struct ata_taskfile tf;
  1388. struct ata_port *ap;
  1389. ap = pap;
  1390. unsigned int err_mask;
  1391. memset(&tf, 0, sizeof(tf));
  1392. tf.ctl = ap->ctl;
  1393. tf.device = ATA_DEVICE_OBS;
  1394. tf.command = ATA_CMD_SET_FEATURES;
  1395. tf.feature = enable;
  1396. tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
  1397. tf.protocol = ATA_PROT_NODATA;
  1398. tf.nsect = feature;
  1399. err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, 0, 0);
  1400. return err_mask;
  1401. }
  1402. static unsigned int ata_dev_init_params(struct ata_device *dev,
  1403. u16 heads, u16 sectors)
  1404. {
  1405. struct ata_taskfile tf;
  1406. struct ata_port *ap;
  1407. ap = pap;
  1408. unsigned int err_mask;
  1409. if (sectors < 1 || sectors > 255 || heads < 1 || heads > 16)
  1410. return AC_ERR_INVALID;
  1411. memset(&tf, 0, sizeof(tf));
  1412. tf.ctl = ap->ctl;
  1413. tf.device = ATA_DEVICE_OBS;
  1414. tf.command = ATA_CMD_INIT_DEV_PARAMS;
  1415. tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
  1416. tf.protocol = ATA_PROT_NODATA;
  1417. tf.nsect = sectors;
  1418. tf.device |= (heads - 1) & 0x0f;
  1419. err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, 0, 0);
  1420. if (err_mask == AC_ERR_DEV && (tf.feature & ATA_ABORTED))
  1421. err_mask = 0;
  1422. return err_mask;
  1423. }
  1424. #if defined(CONFIG_SATA_DWC) && !defined(CONFIG_LBA48)
  1425. #define SATA_MAX_READ_BLK 0xFF
  1426. #else
  1427. #define SATA_MAX_READ_BLK 0xFFFF
  1428. #endif
  1429. ulong sata_read(int device, ulong blknr, lbaint_t blkcnt, void *buffer)
  1430. {
  1431. ulong start,blks, buf_addr;
  1432. unsigned short smallblks;
  1433. unsigned long datalen;
  1434. unsigned char *pdata;
  1435. device &= 0xff;
  1436. u32 block = 0;
  1437. u32 n_block = 0;
  1438. if (dev_state != SATA_READY)
  1439. return 0;
  1440. buf_addr = (unsigned long)buffer;
  1441. start = blknr;
  1442. blks = blkcnt;
  1443. do {
  1444. pdata = (unsigned char *)buf_addr;
  1445. if (blks > SATA_MAX_READ_BLK) {
  1446. datalen = sata_dev_desc[device].blksz * SATA_MAX_READ_BLK;
  1447. smallblks = SATA_MAX_READ_BLK;
  1448. block = (u32)start;
  1449. n_block = (u32)smallblks;
  1450. start += SATA_MAX_READ_BLK;
  1451. blks -= SATA_MAX_READ_BLK;
  1452. } else {
  1453. datalen = sata_dev_desc[device].blksz * SATA_MAX_READ_BLK;
  1454. datalen = sata_dev_desc[device].blksz * blks;
  1455. smallblks = (unsigned short)blks;
  1456. block = (u32)start;
  1457. n_block = (u32)smallblks;
  1458. start += blks;
  1459. blks = 0;
  1460. }
  1461. if (ata_dev_read_sectors(pdata, datalen, block, n_block) != true) {
  1462. printf("sata_dwc : Hard disk read error.\n");
  1463. blkcnt -= blks;
  1464. break;
  1465. }
  1466. buf_addr += datalen;
  1467. } while (blks != 0);
  1468. return (blkcnt);
  1469. }
  1470. static int ata_dev_read_sectors(unsigned char *pdata, unsigned long datalen,
  1471. u32 block, u32 n_block)
  1472. {
  1473. struct ata_port *ap = pap;
  1474. struct ata_device *dev = &ata_device;
  1475. struct ata_taskfile tf;
  1476. unsigned int class = ATA_DEV_ATA;
  1477. unsigned int err_mask = 0;
  1478. const char *reason;
  1479. int may_fallback = 1;
  1480. if (dev_state == SATA_ERROR)
  1481. return false;
  1482. ata_dev_select(ap, dev->devno, 1, 1);
  1483. retry:
  1484. memset(&tf, 0, sizeof(tf));
  1485. tf.ctl = ap->ctl;
  1486. ap->print_id = 1;
  1487. ap->flags &= ~ATA_FLAG_DISABLED;
  1488. ap->pdata = pdata;
  1489. tf.device = ATA_DEVICE_OBS;
  1490. temp_n_block = n_block;
  1491. #ifdef CONFIG_LBA48
  1492. tf.command = ATA_CMD_PIO_READ_EXT;
  1493. tf.flags |= ATA_TFLAG_LBA | ATA_TFLAG_LBA48;
  1494. tf.hob_feature = 31;
  1495. tf.feature = 31;
  1496. tf.hob_nsect = (n_block >> 8) & 0xff;
  1497. tf.nsect = n_block & 0xff;
  1498. tf.hob_lbah = 0x0;
  1499. tf.hob_lbam = 0x0;
  1500. tf.hob_lbal = (block >> 24) & 0xff;
  1501. tf.lbah = (block >> 16) & 0xff;
  1502. tf.lbam = (block >> 8) & 0xff;
  1503. tf.lbal = block & 0xff;
  1504. tf.device = 1 << 6;
  1505. if (tf.flags & ATA_TFLAG_FUA)
  1506. tf.device |= 1 << 7;
  1507. #else
  1508. tf.command = ATA_CMD_PIO_READ;
  1509. tf.flags |= ATA_TFLAG_LBA ;
  1510. tf.feature = 31;
  1511. tf.nsect = n_block & 0xff;
  1512. tf.lbah = (block >> 16) & 0xff;
  1513. tf.lbam = (block >> 8) & 0xff;
  1514. tf.lbal = block & 0xff;
  1515. tf.device = (block >> 24) & 0xf;
  1516. tf.device |= 1 << 6;
  1517. if (tf.flags & ATA_TFLAG_FUA)
  1518. tf.device |= 1 << 7;
  1519. #endif
  1520. tf.protocol = ATA_PROT_PIO;
  1521. /* Some devices choke if TF registers contain garbage. Make
  1522. * sure those are properly initialized.
  1523. */
  1524. tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
  1525. tf.flags |= ATA_TFLAG_POLLING;
  1526. err_mask = ata_exec_internal(dev, &tf, NULL, DMA_FROM_DEVICE, 0, 0);
  1527. if (err_mask) {
  1528. if (err_mask & AC_ERR_NODEV_HINT) {
  1529. printf("READ_SECTORS NODEV after polling detection\n");
  1530. return -ENOENT;
  1531. }
  1532. if ((err_mask == AC_ERR_DEV) && (tf.feature & ATA_ABORTED)) {
  1533. /* Device or controller might have reported
  1534. * the wrong device class. Give a shot at the
  1535. * other IDENTIFY if the current one is
  1536. * aborted by the device.
  1537. */
  1538. if (may_fallback) {
  1539. may_fallback = 0;
  1540. if (class == ATA_DEV_ATA) {
  1541. class = ATA_DEV_ATAPI;
  1542. } else {
  1543. class = ATA_DEV_ATA;
  1544. }
  1545. goto retry;
  1546. }
  1547. /* Control reaches here iff the device aborted
  1548. * both flavors of IDENTIFYs which happens
  1549. * sometimes with phantom devices.
  1550. */
  1551. printf("both IDENTIFYs aborted, assuming NODEV\n");
  1552. return -ENOENT;
  1553. }
  1554. reason = "I/O error";
  1555. goto err_out;
  1556. }
  1557. return true;
  1558. err_out:
  1559. printf("failed to READ SECTORS (%s, err_mask=0x%x)\n", reason, err_mask);
  1560. return false;
  1561. }
  1562. #if defined(CONFIG_SATA_DWC) && !defined(CONFIG_LBA48)
  1563. #define SATA_MAX_WRITE_BLK 0xFF
  1564. #else
  1565. #define SATA_MAX_WRITE_BLK 0xFFFF
  1566. #endif
  1567. ulong sata_write(int device, ulong blknr, lbaint_t blkcnt, const void *buffer)
  1568. {
  1569. ulong start,blks, buf_addr;
  1570. unsigned short smallblks;
  1571. unsigned long datalen;
  1572. unsigned char *pdata;
  1573. device &= 0xff;
  1574. u32 block = 0;
  1575. u32 n_block = 0;
  1576. if (dev_state != SATA_READY)
  1577. return 0;
  1578. buf_addr = (unsigned long)buffer;
  1579. start = blknr;
  1580. blks = blkcnt;
  1581. do {
  1582. pdata = (unsigned char *)buf_addr;
  1583. if (blks > SATA_MAX_WRITE_BLK) {
  1584. datalen = sata_dev_desc[device].blksz * SATA_MAX_WRITE_BLK;
  1585. smallblks = SATA_MAX_WRITE_BLK;
  1586. block = (u32)start;
  1587. n_block = (u32)smallblks;
  1588. start += SATA_MAX_WRITE_BLK;
  1589. blks -= SATA_MAX_WRITE_BLK;
  1590. } else {
  1591. datalen = sata_dev_desc[device].blksz * blks;
  1592. smallblks = (unsigned short)blks;
  1593. block = (u32)start;
  1594. n_block = (u32)smallblks;
  1595. start += blks;
  1596. blks = 0;
  1597. }
  1598. if (ata_dev_write_sectors(pdata, datalen, block, n_block) != true) {
  1599. printf("sata_dwc : Hard disk read error.\n");
  1600. blkcnt -= blks;
  1601. break;
  1602. }
  1603. buf_addr += datalen;
  1604. } while (blks != 0);
  1605. return (blkcnt);
  1606. }
  1607. static int ata_dev_write_sectors(unsigned char* pdata, unsigned long datalen,
  1608. u32 block, u32 n_block)
  1609. {
  1610. struct ata_port *ap = pap;
  1611. struct ata_device *dev = &ata_device;
  1612. struct ata_taskfile tf;
  1613. unsigned int class = ATA_DEV_ATA;
  1614. unsigned int err_mask = 0;
  1615. const char *reason;
  1616. int may_fallback = 1;
  1617. if (dev_state == SATA_ERROR)
  1618. return false;
  1619. ata_dev_select(ap, dev->devno, 1, 1);
  1620. retry:
  1621. memset(&tf, 0, sizeof(tf));
  1622. tf.ctl = ap->ctl;
  1623. ap->print_id = 1;
  1624. ap->flags &= ~ATA_FLAG_DISABLED;
  1625. ap->pdata = pdata;
  1626. tf.device = ATA_DEVICE_OBS;
  1627. temp_n_block = n_block;
  1628. #ifdef CONFIG_LBA48
  1629. tf.command = ATA_CMD_PIO_WRITE_EXT;
  1630. tf.flags |= ATA_TFLAG_LBA | ATA_TFLAG_LBA48 | ATA_TFLAG_WRITE;
  1631. tf.hob_feature = 31;
  1632. tf.feature = 31;
  1633. tf.hob_nsect = (n_block >> 8) & 0xff;
  1634. tf.nsect = n_block & 0xff;
  1635. tf.hob_lbah = 0x0;
  1636. tf.hob_lbam = 0x0;
  1637. tf.hob_lbal = (block >> 24) & 0xff;
  1638. tf.lbah = (block >> 16) & 0xff;
  1639. tf.lbam = (block >> 8) & 0xff;
  1640. tf.lbal = block & 0xff;
  1641. tf.device = 1 << 6;
  1642. if (tf.flags & ATA_TFLAG_FUA)
  1643. tf.device |= 1 << 7;
  1644. #else
  1645. tf.command = ATA_CMD_PIO_WRITE;
  1646. tf.flags |= ATA_TFLAG_LBA | ATA_TFLAG_WRITE;
  1647. tf.feature = 31;
  1648. tf.nsect = n_block & 0xff;
  1649. tf.lbah = (block >> 16) & 0xff;
  1650. tf.lbam = (block >> 8) & 0xff;
  1651. tf.lbal = block & 0xff;
  1652. tf.device = (block >> 24) & 0xf;
  1653. tf.device |= 1 << 6;
  1654. if (tf.flags & ATA_TFLAG_FUA)
  1655. tf.device |= 1 << 7;
  1656. #endif
  1657. tf.protocol = ATA_PROT_PIO;
  1658. /* Some devices choke if TF registers contain garbage. Make
  1659. * sure those are properly initialized.
  1660. */
  1661. tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
  1662. tf.flags |= ATA_TFLAG_POLLING;
  1663. err_mask = ata_exec_internal(dev, &tf, NULL, DMA_FROM_DEVICE, 0, 0);
  1664. if (err_mask) {
  1665. if (err_mask & AC_ERR_NODEV_HINT) {
  1666. printf("READ_SECTORS NODEV after polling detection\n");
  1667. return -ENOENT;
  1668. }
  1669. if ((err_mask == AC_ERR_DEV) && (tf.feature & ATA_ABORTED)) {
  1670. /* Device or controller might have reported
  1671. * the wrong device class. Give a shot at the
  1672. * other IDENTIFY if the current one is
  1673. * aborted by the device.
  1674. */
  1675. if (may_fallback) {
  1676. may_fallback = 0;
  1677. if (class == ATA_DEV_ATA) {
  1678. class = ATA_DEV_ATAPI;
  1679. } else {
  1680. class = ATA_DEV_ATA;
  1681. }
  1682. goto retry;
  1683. }
  1684. /* Control reaches here iff the device aborted
  1685. * both flavors of IDENTIFYs which happens
  1686. * sometimes with phantom devices.
  1687. */
  1688. printf("both IDENTIFYs aborted, assuming NODEV\n");
  1689. return -ENOENT;
  1690. }
  1691. reason = "I/O error";
  1692. goto err_out;
  1693. }
  1694. return true;
  1695. err_out:
  1696. printf("failed to WRITE SECTORS (%s, err_mask=0x%x)\n", reason, err_mask);
  1697. return false;
  1698. }