sata_dwc.c 46 KB

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