sata_dwc.c 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077
  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 <linux/ctype.h>
  38. #include "sata_dwc.h"
  39. #define DMA_NUM_CHANS 1
  40. #define DMA_NUM_CHAN_REGS 8
  41. #define AHB_DMA_BRST_DFLT 16
  42. struct dmareg {
  43. u32 low;
  44. u32 high;
  45. };
  46. struct dma_chan_regs {
  47. struct dmareg sar;
  48. struct dmareg dar;
  49. struct dmareg llp;
  50. struct dmareg ctl;
  51. struct dmareg sstat;
  52. struct dmareg dstat;
  53. struct dmareg sstatar;
  54. struct dmareg dstatar;
  55. struct dmareg cfg;
  56. struct dmareg sgr;
  57. struct dmareg dsr;
  58. };
  59. struct dma_interrupt_regs {
  60. struct dmareg tfr;
  61. struct dmareg block;
  62. struct dmareg srctran;
  63. struct dmareg dsttran;
  64. struct dmareg error;
  65. };
  66. struct ahb_dma_regs {
  67. struct dma_chan_regs chan_regs[DMA_NUM_CHAN_REGS];
  68. struct dma_interrupt_regs interrupt_raw;
  69. struct dma_interrupt_regs interrupt_status;
  70. struct dma_interrupt_regs interrupt_mask;
  71. struct dma_interrupt_regs interrupt_clear;
  72. struct dmareg statusInt;
  73. struct dmareg rq_srcreg;
  74. struct dmareg rq_dstreg;
  75. struct dmareg rq_sgl_srcreg;
  76. struct dmareg rq_sgl_dstreg;
  77. struct dmareg rq_lst_srcreg;
  78. struct dmareg rq_lst_dstreg;
  79. struct dmareg dma_cfg;
  80. struct dmareg dma_chan_en;
  81. struct dmareg dma_id;
  82. struct dmareg dma_test;
  83. struct dmareg res1;
  84. struct dmareg res2;
  85. /* DMA Comp Params
  86. * Param 6 = dma_param[0], Param 5 = dma_param[1],
  87. * Param 4 = dma_param[2] ...
  88. */
  89. struct dmareg dma_params[6];
  90. };
  91. #define DMA_EN 0x00000001
  92. #define DMA_DI 0x00000000
  93. #define DMA_CHANNEL(ch) (0x00000001 << (ch))
  94. #define DMA_ENABLE_CHAN(ch) ((0x00000001 << (ch)) | \
  95. ((0x000000001 << (ch)) << 8))
  96. #define DMA_DISABLE_CHAN(ch) (0x00000000 | \
  97. ((0x000000001 << (ch)) << 8))
  98. #define SATA_DWC_MAX_PORTS 1
  99. #define SATA_DWC_SCR_OFFSET 0x24
  100. #define SATA_DWC_REG_OFFSET 0x64
  101. struct sata_dwc_regs {
  102. u32 fptagr;
  103. u32 fpbor;
  104. u32 fptcr;
  105. u32 dmacr;
  106. u32 dbtsr;
  107. u32 intpr;
  108. u32 intmr;
  109. u32 errmr;
  110. u32 llcr;
  111. u32 phycr;
  112. u32 physr;
  113. u32 rxbistpd;
  114. u32 rxbistpd1;
  115. u32 rxbistpd2;
  116. u32 txbistpd;
  117. u32 txbistpd1;
  118. u32 txbistpd2;
  119. u32 bistcr;
  120. u32 bistfctr;
  121. u32 bistsr;
  122. u32 bistdecr;
  123. u32 res[15];
  124. u32 testr;
  125. u32 versionr;
  126. u32 idr;
  127. u32 unimpl[192];
  128. u32 dmadr[256];
  129. };
  130. #define SATA_DWC_TXFIFO_DEPTH 0x01FF
  131. #define SATA_DWC_RXFIFO_DEPTH 0x01FF
  132. #define SATA_DWC_DBTSR_MWR(size) ((size / 4) & SATA_DWC_TXFIFO_DEPTH)
  133. #define SATA_DWC_DBTSR_MRD(size) (((size / 4) & \
  134. SATA_DWC_RXFIFO_DEPTH) << 16)
  135. #define SATA_DWC_INTPR_DMAT 0x00000001
  136. #define SATA_DWC_INTPR_NEWFP 0x00000002
  137. #define SATA_DWC_INTPR_PMABRT 0x00000004
  138. #define SATA_DWC_INTPR_ERR 0x00000008
  139. #define SATA_DWC_INTPR_NEWBIST 0x00000010
  140. #define SATA_DWC_INTPR_IPF 0x10000000
  141. #define SATA_DWC_INTMR_DMATM 0x00000001
  142. #define SATA_DWC_INTMR_NEWFPM 0x00000002
  143. #define SATA_DWC_INTMR_PMABRTM 0x00000004
  144. #define SATA_DWC_INTMR_ERRM 0x00000008
  145. #define SATA_DWC_INTMR_NEWBISTM 0x00000010
  146. #define SATA_DWC_DMACR_TMOD_TXCHEN 0x00000004
  147. #define SATA_DWC_DMACR_TXRXCH_CLEAR SATA_DWC_DMACR_TMOD_TXCHEN
  148. #define SATA_DWC_QCMD_MAX 32
  149. #define SATA_DWC_SERROR_ERR_BITS 0x0FFF0F03
  150. #define HSDEVP_FROM_AP(ap) (struct sata_dwc_device_port*) \
  151. (ap)->private_data
  152. struct sata_dwc_device {
  153. struct device *dev;
  154. struct ata_probe_ent *pe;
  155. struct ata_host *host;
  156. u8 *reg_base;
  157. struct sata_dwc_regs *sata_dwc_regs;
  158. int irq_dma;
  159. };
  160. struct sata_dwc_device_port {
  161. struct sata_dwc_device *hsdev;
  162. int cmd_issued[SATA_DWC_QCMD_MAX];
  163. u32 dma_chan[SATA_DWC_QCMD_MAX];
  164. int dma_pending[SATA_DWC_QCMD_MAX];
  165. };
  166. enum {
  167. SATA_DWC_CMD_ISSUED_NOT = 0,
  168. SATA_DWC_CMD_ISSUED_PEND = 1,
  169. SATA_DWC_CMD_ISSUED_EXEC = 2,
  170. SATA_DWC_CMD_ISSUED_NODATA = 3,
  171. SATA_DWC_DMA_PENDING_NONE = 0,
  172. SATA_DWC_DMA_PENDING_TX = 1,
  173. SATA_DWC_DMA_PENDING_RX = 2,
  174. };
  175. #define msleep(a) udelay(a * 1000)
  176. #define ssleep(a) msleep(a * 1000)
  177. static int ata_probe_timeout = (ATA_TMOUT_INTERNAL / 100);
  178. enum sata_dev_state {
  179. SATA_INIT = 0,
  180. SATA_READY = 1,
  181. SATA_NODEVICE = 2,
  182. SATA_ERROR = 3,
  183. };
  184. enum sata_dev_state dev_state = SATA_INIT;
  185. static struct ahb_dma_regs *sata_dma_regs = 0;
  186. static struct ata_host *phost;
  187. static struct ata_port ap;
  188. static struct ata_port *pap = &ap;
  189. static struct ata_device ata_device;
  190. static struct sata_dwc_device_port dwc_devp;
  191. static void *scr_addr_sstatus;
  192. static u32 temp_n_block = 0;
  193. static unsigned ata_exec_internal(struct ata_device *dev,
  194. struct ata_taskfile *tf, const u8 *cdb,
  195. int dma_dir, unsigned int buflen,
  196. unsigned long timeout);
  197. static unsigned int ata_dev_set_feature(struct ata_device *dev,
  198. u8 enable,u8 feature);
  199. static unsigned int ata_dev_init_params(struct ata_device *dev,
  200. u16 heads, u16 sectors);
  201. static u8 ata_irq_on(struct ata_port *ap);
  202. static struct ata_queued_cmd *__ata_qc_from_tag(struct ata_port *ap,
  203. unsigned int tag);
  204. static int ata_hsm_move(struct ata_port *ap, struct ata_queued_cmd *qc,
  205. u8 status, int in_wq);
  206. static void ata_tf_to_host(struct ata_port *ap,
  207. const struct ata_taskfile *tf);
  208. static void ata_exec_command(struct ata_port *ap,
  209. const struct ata_taskfile *tf);
  210. static unsigned int ata_qc_issue_prot(struct ata_queued_cmd *qc);
  211. static u8 ata_check_altstatus(struct ata_port *ap);
  212. static u8 ata_check_status(struct ata_port *ap);
  213. static void ata_dev_select(struct ata_port *ap, unsigned int device,
  214. unsigned int wait, unsigned int can_sleep);
  215. static void ata_qc_issue(struct ata_queued_cmd *qc);
  216. static void ata_tf_load(struct ata_port *ap,
  217. const struct ata_taskfile *tf);
  218. static int ata_dev_read_sectors(unsigned char* pdata,
  219. unsigned long datalen, u32 block, u32 n_block);
  220. static int ata_dev_write_sectors(unsigned char* pdata,
  221. unsigned long datalen , u32 block, u32 n_block);
  222. static void ata_std_dev_select(struct ata_port *ap, unsigned int device);
  223. static void ata_qc_complete(struct ata_queued_cmd *qc);
  224. static void __ata_qc_complete(struct ata_queued_cmd *qc);
  225. static void fill_result_tf(struct ata_queued_cmd *qc);
  226. static void ata_tf_read(struct ata_port *ap, struct ata_taskfile *tf);
  227. static void ata_mmio_data_xfer(struct ata_device *dev,
  228. unsigned char *buf,
  229. unsigned int buflen,int do_write);
  230. static void ata_pio_task(struct ata_port *arg_ap);
  231. static void __ata_port_freeze(struct ata_port *ap);
  232. static int ata_port_freeze(struct ata_port *ap);
  233. static void ata_qc_free(struct ata_queued_cmd *qc);
  234. static void ata_pio_sectors(struct ata_queued_cmd *qc);
  235. static void ata_pio_sector(struct ata_queued_cmd *qc);
  236. static void ata_pio_queue_task(struct ata_port *ap,
  237. void *data,unsigned long delay);
  238. static void ata_hsm_qc_complete(struct ata_queued_cmd *qc, int in_wq);
  239. static int sata_dwc_softreset(struct ata_port *ap);
  240. static int ata_dev_read_id(struct ata_device *dev, unsigned int *p_class,
  241. unsigned int flags, u16 *id);
  242. static int check_sata_dev_state(void);
  243. extern block_dev_desc_t sata_dev_desc[CONFIG_SYS_SATA_MAX_DEVICE];
  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. }