tx4939ide.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754
  1. /*
  2. * TX4939 internal IDE driver
  3. * Based on RBTX49xx patch from CELF patch archive.
  4. *
  5. * This file is subject to the terms and conditions of the GNU General Public
  6. * License. See the file "COPYING" in the main directory of this archive
  7. * for more details.
  8. *
  9. * (C) Copyright TOSHIBA CORPORATION 2005-2007
  10. */
  11. #include <linux/module.h>
  12. #include <linux/types.h>
  13. #include <linux/ide.h>
  14. #include <linux/init.h>
  15. #include <linux/delay.h>
  16. #include <linux/platform_device.h>
  17. #include <linux/io.h>
  18. #include <linux/scatterlist.h>
  19. #define MODNAME "tx4939ide"
  20. /* ATA Shadow Registers (8-bit except for Data which is 16-bit) */
  21. #define TX4939IDE_Data 0x000
  22. #define TX4939IDE_Error_Feature 0x001
  23. #define TX4939IDE_Sec 0x002
  24. #define TX4939IDE_LBA0 0x003
  25. #define TX4939IDE_LBA1 0x004
  26. #define TX4939IDE_LBA2 0x005
  27. #define TX4939IDE_DevHead 0x006
  28. #define TX4939IDE_Stat_Cmd 0x007
  29. #define TX4939IDE_AltStat_DevCtl 0x402
  30. /* H/W DMA Registers */
  31. #define TX4939IDE_DMA_Cmd 0x800 /* 8-bit */
  32. #define TX4939IDE_DMA_Stat 0x802 /* 8-bit */
  33. #define TX4939IDE_PRD_Ptr 0x804 /* 32-bit */
  34. /* ATA100 CORE Registers (16-bit) */
  35. #define TX4939IDE_Sys_Ctl 0xc00
  36. #define TX4939IDE_Xfer_Cnt_1 0xc08
  37. #define TX4939IDE_Xfer_Cnt_2 0xc0a
  38. #define TX4939IDE_Sec_Cnt 0xc10
  39. #define TX4939IDE_Start_Lo_Addr 0xc18
  40. #define TX4939IDE_Start_Up_Addr 0xc20
  41. #define TX4939IDE_Add_Ctl 0xc28
  42. #define TX4939IDE_Lo_Burst_Cnt 0xc30
  43. #define TX4939IDE_Up_Burst_Cnt 0xc38
  44. #define TX4939IDE_PIO_Addr 0xc88
  45. #define TX4939IDE_H_Rst_Tim 0xc90
  46. #define TX4939IDE_Int_Ctl 0xc98
  47. #define TX4939IDE_Pkt_Cmd 0xcb8
  48. #define TX4939IDE_Bxfer_Cnt_Hi 0xcc0
  49. #define TX4939IDE_Bxfer_Cnt_Lo 0xcc8
  50. #define TX4939IDE_Dev_TErr 0xcd0
  51. #define TX4939IDE_Pkt_Xfer_Ctl 0xcd8
  52. #define TX4939IDE_Start_TAddr 0xce0
  53. /* bits for Int_Ctl */
  54. #define TX4939IDE_INT_ADDRERR 0x80
  55. #define TX4939IDE_INT_REACHMUL 0x40
  56. #define TX4939IDE_INT_DEVTIMING 0x20
  57. #define TX4939IDE_INT_UDMATERM 0x10
  58. #define TX4939IDE_INT_TIMER 0x08
  59. #define TX4939IDE_INT_BUSERR 0x04
  60. #define TX4939IDE_INT_XFEREND 0x02
  61. #define TX4939IDE_INT_HOST 0x01
  62. #define TX4939IDE_IGNORE_INTS \
  63. (TX4939IDE_INT_ADDRERR | TX4939IDE_INT_REACHMUL | \
  64. TX4939IDE_INT_DEVTIMING | TX4939IDE_INT_UDMATERM | \
  65. TX4939IDE_INT_TIMER | TX4939IDE_INT_XFEREND)
  66. #ifdef __BIG_ENDIAN
  67. #define tx4939ide_swizzlel(a) ((a) ^ 4)
  68. #define tx4939ide_swizzlew(a) ((a) ^ 6)
  69. #define tx4939ide_swizzleb(a) ((a) ^ 7)
  70. #else
  71. #define tx4939ide_swizzlel(a) (a)
  72. #define tx4939ide_swizzlew(a) (a)
  73. #define tx4939ide_swizzleb(a) (a)
  74. #endif
  75. static u16 tx4939ide_readw(void __iomem *base, u32 reg)
  76. {
  77. return __raw_readw(base + tx4939ide_swizzlew(reg));
  78. }
  79. static u8 tx4939ide_readb(void __iomem *base, u32 reg)
  80. {
  81. return __raw_readb(base + tx4939ide_swizzleb(reg));
  82. }
  83. static void tx4939ide_writel(u32 val, void __iomem *base, u32 reg)
  84. {
  85. __raw_writel(val, base + tx4939ide_swizzlel(reg));
  86. }
  87. static void tx4939ide_writew(u16 val, void __iomem *base, u32 reg)
  88. {
  89. __raw_writew(val, base + tx4939ide_swizzlew(reg));
  90. }
  91. static void tx4939ide_writeb(u8 val, void __iomem *base, u32 reg)
  92. {
  93. __raw_writeb(val, base + tx4939ide_swizzleb(reg));
  94. }
  95. #define TX4939IDE_BASE(hwif) ((void __iomem *)(hwif)->extra_base)
  96. static void tx4939ide_set_pio_mode(ide_drive_t *drive, const u8 pio)
  97. {
  98. ide_hwif_t *hwif = drive->hwif;
  99. int is_slave = drive->dn;
  100. u32 mask, val;
  101. u8 safe = pio;
  102. ide_drive_t *pair;
  103. pair = ide_get_pair_dev(drive);
  104. if (pair)
  105. safe = min(safe, ide_get_best_pio_mode(pair, 255, 4));
  106. /*
  107. * Update Command Transfer Mode for master/slave and Data
  108. * Transfer Mode for this drive.
  109. */
  110. mask = is_slave ? 0x07f00000 : 0x000007f0;
  111. val = ((safe << 8) | (pio << 4)) << (is_slave ? 16 : 0);
  112. hwif->select_data = (hwif->select_data & ~mask) | val;
  113. /* tx4939ide_tf_load_fixup() will set the Sys_Ctl register */
  114. }
  115. static void tx4939ide_set_dma_mode(ide_drive_t *drive, const u8 mode)
  116. {
  117. ide_hwif_t *hwif = drive->hwif;
  118. u32 mask, val;
  119. /* Update Data Transfer Mode for this drive. */
  120. if (mode >= XFER_UDMA_0)
  121. val = mode - XFER_UDMA_0 + 8;
  122. else
  123. val = mode - XFER_MW_DMA_0 + 5;
  124. if (drive->dn) {
  125. mask = 0x00f00000;
  126. val <<= 20;
  127. } else {
  128. mask = 0x000000f0;
  129. val <<= 4;
  130. }
  131. hwif->select_data = (hwif->select_data & ~mask) | val;
  132. /* tx4939ide_tf_load_fixup() will set the Sys_Ctl register */
  133. }
  134. static u16 tx4939ide_check_error_ints(ide_hwif_t *hwif)
  135. {
  136. void __iomem *base = TX4939IDE_BASE(hwif);
  137. u16 ctl = tx4939ide_readw(base, TX4939IDE_Int_Ctl);
  138. if (ctl & TX4939IDE_INT_BUSERR) {
  139. /* reset FIFO */
  140. u16 sysctl = tx4939ide_readw(base, TX4939IDE_Sys_Ctl);
  141. tx4939ide_writew(sysctl | 0x4000, base, TX4939IDE_Sys_Ctl);
  142. mmiowb();
  143. /* wait 12GBUSCLK (typ. 60ns @ GBUS200MHz, max 270ns) */
  144. ndelay(270);
  145. tx4939ide_writew(sysctl, base, TX4939IDE_Sys_Ctl);
  146. }
  147. if (ctl & (TX4939IDE_INT_ADDRERR |
  148. TX4939IDE_INT_DEVTIMING | TX4939IDE_INT_BUSERR))
  149. pr_err("%s: Error interrupt %#x (%s%s%s )\n",
  150. hwif->name, ctl,
  151. ctl & TX4939IDE_INT_ADDRERR ? " Address-Error" : "",
  152. ctl & TX4939IDE_INT_DEVTIMING ? " DEV-Timing" : "",
  153. ctl & TX4939IDE_INT_BUSERR ? " Bus-Error" : "");
  154. return ctl;
  155. }
  156. static void tx4939ide_clear_irq(ide_drive_t *drive)
  157. {
  158. ide_hwif_t *hwif;
  159. void __iomem *base;
  160. u16 ctl;
  161. /*
  162. * tx4939ide_dma_test_irq() and tx4939ide_dma_end() do all job
  163. * for DMA case.
  164. */
  165. if (drive->waiting_for_dma)
  166. return;
  167. hwif = drive->hwif;
  168. base = TX4939IDE_BASE(hwif);
  169. ctl = tx4939ide_check_error_ints(hwif);
  170. tx4939ide_writew(ctl, base, TX4939IDE_Int_Ctl);
  171. }
  172. static u8 tx4939ide_cable_detect(ide_hwif_t *hwif)
  173. {
  174. void __iomem *base = TX4939IDE_BASE(hwif);
  175. return tx4939ide_readw(base, TX4939IDE_Sys_Ctl) & 0x2000 ?
  176. ATA_CBL_PATA40 : ATA_CBL_PATA80;
  177. }
  178. #ifdef __BIG_ENDIAN
  179. static void tx4939ide_dma_host_set(ide_drive_t *drive, int on)
  180. {
  181. ide_hwif_t *hwif = drive->hwif;
  182. u8 unit = drive->dn;
  183. void __iomem *base = TX4939IDE_BASE(hwif);
  184. u8 dma_stat = tx4939ide_readb(base, TX4939IDE_DMA_Stat);
  185. if (on)
  186. dma_stat |= (1 << (5 + unit));
  187. else
  188. dma_stat &= ~(1 << (5 + unit));
  189. tx4939ide_writeb(dma_stat, base, TX4939IDE_DMA_Stat);
  190. }
  191. #else
  192. #define tx4939ide_dma_host_set ide_dma_host_set
  193. #endif
  194. static u8 tx4939ide_clear_dma_status(void __iomem *base)
  195. {
  196. u8 dma_stat;
  197. /* read DMA status for INTR & ERROR flags */
  198. dma_stat = tx4939ide_readb(base, TX4939IDE_DMA_Stat);
  199. /* clear INTR & ERROR flags */
  200. tx4939ide_writeb(dma_stat | ATA_DMA_INTR | ATA_DMA_ERR, base,
  201. TX4939IDE_DMA_Stat);
  202. /* recover intmask cleared by writing to bit2 of DMA_Stat */
  203. tx4939ide_writew(TX4939IDE_IGNORE_INTS << 8, base, TX4939IDE_Int_Ctl);
  204. return dma_stat;
  205. }
  206. #ifdef __BIG_ENDIAN
  207. /* custom ide_build_dmatable to handle swapped layout */
  208. static int tx4939ide_build_dmatable(ide_drive_t *drive, struct request *rq)
  209. {
  210. ide_hwif_t *hwif = drive->hwif;
  211. u32 *table = (u32 *)hwif->dmatable_cpu;
  212. unsigned int count = 0;
  213. int i;
  214. struct scatterlist *sg;
  215. hwif->sg_nents = ide_build_sglist(drive, rq);
  216. if (hwif->sg_nents == 0)
  217. return 0;
  218. for_each_sg(hwif->sg_table, sg, hwif->sg_nents, i) {
  219. u32 cur_addr, cur_len, bcount;
  220. cur_addr = sg_dma_address(sg);
  221. cur_len = sg_dma_len(sg);
  222. /*
  223. * Fill in the DMA table, without crossing any 64kB boundaries.
  224. */
  225. while (cur_len) {
  226. if (count++ >= PRD_ENTRIES)
  227. goto use_pio_instead;
  228. bcount = 0x10000 - (cur_addr & 0xffff);
  229. if (bcount > cur_len)
  230. bcount = cur_len;
  231. *table++ = bcount & 0xffff;
  232. *table++ = cur_addr;
  233. cur_addr += bcount;
  234. cur_len -= bcount;
  235. }
  236. }
  237. if (count) {
  238. *(table - 2) |= 0x80000000;
  239. return count;
  240. }
  241. use_pio_instead:
  242. printk(KERN_ERR "%s: %s\n", drive->name,
  243. count ? "DMA table too small" : "empty DMA table?");
  244. ide_destroy_dmatable(drive);
  245. return 0; /* revert to PIO for this request */
  246. }
  247. #else
  248. #define tx4939ide_build_dmatable ide_build_dmatable
  249. #endif
  250. static int tx4939ide_dma_setup(ide_drive_t *drive)
  251. {
  252. ide_hwif_t *hwif = drive->hwif;
  253. void __iomem *base = TX4939IDE_BASE(hwif);
  254. struct request *rq = hwif->hwgroup->rq;
  255. u8 reading;
  256. int nent;
  257. if (rq_data_dir(rq))
  258. reading = 0;
  259. else
  260. reading = ATA_DMA_WR;
  261. /* fall back to PIO! */
  262. nent = tx4939ide_build_dmatable(drive, rq);
  263. if (!nent) {
  264. ide_map_sg(drive, rq);
  265. return 1;
  266. }
  267. /* PRD table */
  268. tx4939ide_writel(hwif->dmatable_dma, base, TX4939IDE_PRD_Ptr);
  269. /* specify r/w */
  270. tx4939ide_writeb(reading, base, TX4939IDE_DMA_Cmd);
  271. /* clear INTR & ERROR flags */
  272. tx4939ide_clear_dma_status(base);
  273. drive->waiting_for_dma = 1;
  274. tx4939ide_writew(SECTOR_SIZE / 2, base, drive->dn ?
  275. TX4939IDE_Xfer_Cnt_2 : TX4939IDE_Xfer_Cnt_1);
  276. tx4939ide_writew(rq->nr_sectors, base, TX4939IDE_Sec_Cnt);
  277. return 0;
  278. }
  279. static int tx4939ide_dma_end(ide_drive_t *drive)
  280. {
  281. ide_hwif_t *hwif = drive->hwif;
  282. u8 dma_stat, dma_cmd;
  283. void __iomem *base = TX4939IDE_BASE(hwif);
  284. u16 ctl = tx4939ide_readw(base, TX4939IDE_Int_Ctl);
  285. drive->waiting_for_dma = 0;
  286. /* get DMA command mode */
  287. dma_cmd = tx4939ide_readb(base, TX4939IDE_DMA_Cmd);
  288. /* stop DMA */
  289. tx4939ide_writeb(dma_cmd & ~ATA_DMA_START, base, TX4939IDE_DMA_Cmd);
  290. /* read and clear the INTR & ERROR bits */
  291. dma_stat = tx4939ide_clear_dma_status(base);
  292. /* purge DMA mappings */
  293. ide_destroy_dmatable(drive);
  294. /* verify good DMA status */
  295. wmb();
  296. if ((dma_stat & (ATA_DMA_INTR | ATA_DMA_ERR | ATA_DMA_ACTIVE)) == 0 &&
  297. (ctl & (TX4939IDE_INT_XFEREND | TX4939IDE_INT_HOST)) ==
  298. (TX4939IDE_INT_XFEREND | TX4939IDE_INT_HOST))
  299. /* INT_IDE lost... bug? */
  300. return 0;
  301. return ((dma_stat & (ATA_DMA_INTR | ATA_DMA_ERR | ATA_DMA_ACTIVE)) !=
  302. ATA_DMA_INTR) ? 0x10 | dma_stat : 0;
  303. }
  304. /* returns 1 if DMA IRQ issued, 0 otherwise */
  305. static int tx4939ide_dma_test_irq(ide_drive_t *drive)
  306. {
  307. ide_hwif_t *hwif = drive->hwif;
  308. void __iomem *base = TX4939IDE_BASE(hwif);
  309. u16 ctl, ide_int;
  310. u8 dma_stat, stat;
  311. int found = 0;
  312. ctl = tx4939ide_check_error_ints(hwif);
  313. ide_int = ctl & (TX4939IDE_INT_XFEREND | TX4939IDE_INT_HOST);
  314. switch (ide_int) {
  315. case TX4939IDE_INT_HOST:
  316. /* On error, XFEREND might not be asserted. */
  317. stat = tx4939ide_readb(base, TX4939IDE_AltStat_DevCtl);
  318. if ((stat & (ATA_BUSY | ATA_DRQ | ATA_ERR)) == ATA_ERR)
  319. found = 1;
  320. else
  321. /* Wait for XFEREND (Mask HOST and unmask XFEREND) */
  322. ctl &= ~TX4939IDE_INT_XFEREND << 8;
  323. ctl |= ide_int << 8;
  324. break;
  325. case TX4939IDE_INT_HOST | TX4939IDE_INT_XFEREND:
  326. dma_stat = tx4939ide_readb(base, TX4939IDE_DMA_Stat);
  327. if (!(dma_stat & ATA_DMA_INTR))
  328. pr_warning("%s: weird interrupt status. "
  329. "DMA_Stat %#02x int_ctl %#04x\n",
  330. hwif->name, dma_stat, ctl);
  331. found = 1;
  332. break;
  333. }
  334. /*
  335. * Do not clear XFEREND, HOST now. They will be cleared by
  336. * clearing bit2 of DMA_Stat.
  337. */
  338. ctl &= ~ide_int;
  339. tx4939ide_writew(ctl, base, TX4939IDE_Int_Ctl);
  340. return found;
  341. }
  342. static void tx4939ide_init_hwif(ide_hwif_t *hwif)
  343. {
  344. void __iomem *base = TX4939IDE_BASE(hwif);
  345. /* Soft Reset */
  346. tx4939ide_writew(0x8000, base, TX4939IDE_Sys_Ctl);
  347. mmiowb();
  348. /* at least 20 GBUSCLK (typ. 100ns @ GBUS200MHz, max 450ns) */
  349. ndelay(450);
  350. tx4939ide_writew(0x0000, base, TX4939IDE_Sys_Ctl);
  351. /* mask some interrupts and clear all interrupts */
  352. tx4939ide_writew((TX4939IDE_IGNORE_INTS << 8) | 0xff, base,
  353. TX4939IDE_Int_Ctl);
  354. tx4939ide_writew(0x0008, base, TX4939IDE_Lo_Burst_Cnt);
  355. tx4939ide_writew(0, base, TX4939IDE_Up_Burst_Cnt);
  356. }
  357. static int tx4939ide_init_dma(ide_hwif_t *hwif, const struct ide_port_info *d)
  358. {
  359. hwif->dma_base =
  360. hwif->extra_base + tx4939ide_swizzleb(TX4939IDE_DMA_Cmd);
  361. /*
  362. * Note that we cannot use ATA_DMA_TABLE_OFS, ATA_DMA_STATUS
  363. * for big endian.
  364. */
  365. return ide_allocate_dma_engine(hwif);
  366. }
  367. static void tx4939ide_tf_load_fixup(ide_drive_t *drive, ide_task_t *task)
  368. {
  369. ide_hwif_t *hwif = drive->hwif;
  370. void __iomem *base = TX4939IDE_BASE(hwif);
  371. u16 sysctl = hwif->select_data >> (drive->dn ? 16 : 0);
  372. /*
  373. * Fix ATA100 CORE System Control Register. (The write to the
  374. * Device/Head register may write wrong data to the System
  375. * Control Register)
  376. * While Sys_Ctl is written here, selectproc is not needed.
  377. */
  378. tx4939ide_writew(sysctl, base, TX4939IDE_Sys_Ctl);
  379. }
  380. #ifdef __BIG_ENDIAN
  381. static u8 tx4939ide_read_sff_dma_status(ide_hwif_t *hwif)
  382. {
  383. void __iomem *base = TX4939IDE_BASE(hwif);
  384. return tx4939ide_readb(base, TX4939IDE_DMA_Stat);
  385. }
  386. /* custom iops (independent from SWAP_IO_SPACE) */
  387. static u8 tx4939ide_inb(unsigned long port)
  388. {
  389. return __raw_readb((void __iomem *)port);
  390. }
  391. static void tx4939ide_outb(u8 value, unsigned long port)
  392. {
  393. __raw_writeb(value, (void __iomem *)port);
  394. }
  395. static void tx4939ide_tf_load(ide_drive_t *drive, ide_task_t *task)
  396. {
  397. ide_hwif_t *hwif = drive->hwif;
  398. struct ide_io_ports *io_ports = &hwif->io_ports;
  399. struct ide_taskfile *tf = &task->tf;
  400. u8 HIHI = task->tf_flags & IDE_TFLAG_LBA48 ? 0xE0 : 0xEF;
  401. if (task->tf_flags & IDE_TFLAG_FLAGGED)
  402. HIHI = 0xFF;
  403. if (task->tf_flags & IDE_TFLAG_OUT_DATA) {
  404. u16 data = (tf->hob_data << 8) | tf->data;
  405. /* no endian swap */
  406. __raw_writew(data, (void __iomem *)io_ports->data_addr);
  407. }
  408. if (task->tf_flags & IDE_TFLAG_OUT_HOB_FEATURE)
  409. tx4939ide_outb(tf->hob_feature, io_ports->feature_addr);
  410. if (task->tf_flags & IDE_TFLAG_OUT_HOB_NSECT)
  411. tx4939ide_outb(tf->hob_nsect, io_ports->nsect_addr);
  412. if (task->tf_flags & IDE_TFLAG_OUT_HOB_LBAL)
  413. tx4939ide_outb(tf->hob_lbal, io_ports->lbal_addr);
  414. if (task->tf_flags & IDE_TFLAG_OUT_HOB_LBAM)
  415. tx4939ide_outb(tf->hob_lbam, io_ports->lbam_addr);
  416. if (task->tf_flags & IDE_TFLAG_OUT_HOB_LBAH)
  417. tx4939ide_outb(tf->hob_lbah, io_ports->lbah_addr);
  418. if (task->tf_flags & IDE_TFLAG_OUT_FEATURE)
  419. tx4939ide_outb(tf->feature, io_ports->feature_addr);
  420. if (task->tf_flags & IDE_TFLAG_OUT_NSECT)
  421. tx4939ide_outb(tf->nsect, io_ports->nsect_addr);
  422. if (task->tf_flags & IDE_TFLAG_OUT_LBAL)
  423. tx4939ide_outb(tf->lbal, io_ports->lbal_addr);
  424. if (task->tf_flags & IDE_TFLAG_OUT_LBAM)
  425. tx4939ide_outb(tf->lbam, io_ports->lbam_addr);
  426. if (task->tf_flags & IDE_TFLAG_OUT_LBAH)
  427. tx4939ide_outb(tf->lbah, io_ports->lbah_addr);
  428. if (task->tf_flags & IDE_TFLAG_OUT_DEVICE) {
  429. tx4939ide_outb((tf->device & HIHI) | drive->select,
  430. io_ports->device_addr);
  431. tx4939ide_tf_load_fixup(drive, task);
  432. }
  433. }
  434. static void tx4939ide_tf_read(ide_drive_t *drive, ide_task_t *task)
  435. {
  436. ide_hwif_t *hwif = drive->hwif;
  437. struct ide_io_ports *io_ports = &hwif->io_ports;
  438. struct ide_taskfile *tf = &task->tf;
  439. if (task->tf_flags & IDE_TFLAG_IN_DATA) {
  440. u16 data;
  441. /* no endian swap */
  442. data = __raw_readw((void __iomem *)io_ports->data_addr);
  443. tf->data = data & 0xff;
  444. tf->hob_data = (data >> 8) & 0xff;
  445. }
  446. /* be sure we're looking at the low order bits */
  447. tx4939ide_outb(ATA_DEVCTL_OBS & ~0x80, io_ports->ctl_addr);
  448. if (task->tf_flags & IDE_TFLAG_IN_FEATURE)
  449. tf->feature = tx4939ide_inb(io_ports->feature_addr);
  450. if (task->tf_flags & IDE_TFLAG_IN_NSECT)
  451. tf->nsect = tx4939ide_inb(io_ports->nsect_addr);
  452. if (task->tf_flags & IDE_TFLAG_IN_LBAL)
  453. tf->lbal = tx4939ide_inb(io_ports->lbal_addr);
  454. if (task->tf_flags & IDE_TFLAG_IN_LBAM)
  455. tf->lbam = tx4939ide_inb(io_ports->lbam_addr);
  456. if (task->tf_flags & IDE_TFLAG_IN_LBAH)
  457. tf->lbah = tx4939ide_inb(io_ports->lbah_addr);
  458. if (task->tf_flags & IDE_TFLAG_IN_DEVICE)
  459. tf->device = tx4939ide_inb(io_ports->device_addr);
  460. if (task->tf_flags & IDE_TFLAG_LBA48) {
  461. tx4939ide_outb(ATA_DEVCTL_OBS | 0x80, io_ports->ctl_addr);
  462. if (task->tf_flags & IDE_TFLAG_IN_HOB_FEATURE)
  463. tf->hob_feature =
  464. tx4939ide_inb(io_ports->feature_addr);
  465. if (task->tf_flags & IDE_TFLAG_IN_HOB_NSECT)
  466. tf->hob_nsect = tx4939ide_inb(io_ports->nsect_addr);
  467. if (task->tf_flags & IDE_TFLAG_IN_HOB_LBAL)
  468. tf->hob_lbal = tx4939ide_inb(io_ports->lbal_addr);
  469. if (task->tf_flags & IDE_TFLAG_IN_HOB_LBAM)
  470. tf->hob_lbam = tx4939ide_inb(io_ports->lbam_addr);
  471. if (task->tf_flags & IDE_TFLAG_IN_HOB_LBAH)
  472. tf->hob_lbah = tx4939ide_inb(io_ports->lbah_addr);
  473. }
  474. }
  475. static void tx4939ide_input_data_swap(ide_drive_t *drive, struct request *rq,
  476. void *buf, unsigned int len)
  477. {
  478. unsigned long port = drive->hwif->io_ports.data_addr;
  479. unsigned short *ptr = buf;
  480. unsigned int count = (len + 1) / 2;
  481. while (count--)
  482. *ptr++ = cpu_to_le16(__raw_readw((void __iomem *)port));
  483. __ide_flush_dcache_range((unsigned long)buf, count * 2);
  484. }
  485. static void tx4939ide_output_data_swap(ide_drive_t *drive, struct request *rq,
  486. void *buf, unsigned int len)
  487. {
  488. unsigned long port = drive->hwif->io_ports.data_addr;
  489. unsigned short *ptr = buf;
  490. unsigned int count = (len + 1) / 2;
  491. while (count--) {
  492. __raw_writew(le16_to_cpu(*ptr), (void __iomem *)port);
  493. ptr++;
  494. }
  495. __ide_flush_dcache_range((unsigned long)buf, count * 2);
  496. }
  497. static const struct ide_tp_ops tx4939ide_tp_ops = {
  498. .exec_command = ide_exec_command,
  499. .read_status = ide_read_status,
  500. .read_altstatus = ide_read_altstatus,
  501. .read_sff_dma_status = tx4939ide_read_sff_dma_status,
  502. .set_irq = ide_set_irq,
  503. .tf_load = tx4939ide_tf_load,
  504. .tf_read = tx4939ide_tf_read,
  505. .input_data = tx4939ide_input_data_swap,
  506. .output_data = tx4939ide_output_data_swap,
  507. };
  508. #else /* __LITTLE_ENDIAN */
  509. static void tx4939ide_tf_load(ide_drive_t *drive, ide_task_t *task)
  510. {
  511. ide_tf_load(drive, task);
  512. if (task->tf_flags & IDE_TFLAG_OUT_DEVICE)
  513. tx4939ide_tf_load_fixup(drive, task);
  514. }
  515. static const struct ide_tp_ops tx4939ide_tp_ops = {
  516. .exec_command = ide_exec_command,
  517. .read_status = ide_read_status,
  518. .read_altstatus = ide_read_altstatus,
  519. .read_sff_dma_status = ide_read_sff_dma_status,
  520. .set_irq = ide_set_irq,
  521. .tf_load = tx4939ide_tf_load,
  522. .tf_read = ide_tf_read,
  523. .input_data = ide_input_data,
  524. .output_data = ide_output_data,
  525. };
  526. #endif /* __LITTLE_ENDIAN */
  527. static const struct ide_port_ops tx4939ide_port_ops = {
  528. .set_pio_mode = tx4939ide_set_pio_mode,
  529. .set_dma_mode = tx4939ide_set_dma_mode,
  530. .clear_irq = tx4939ide_clear_irq,
  531. .cable_detect = tx4939ide_cable_detect,
  532. };
  533. static const struct ide_dma_ops tx4939ide_dma_ops = {
  534. .dma_host_set = tx4939ide_dma_host_set,
  535. .dma_setup = tx4939ide_dma_setup,
  536. .dma_exec_cmd = ide_dma_exec_cmd,
  537. .dma_start = ide_dma_start,
  538. .dma_end = tx4939ide_dma_end,
  539. .dma_test_irq = tx4939ide_dma_test_irq,
  540. .dma_lost_irq = ide_dma_lost_irq,
  541. .dma_timeout = ide_dma_timeout,
  542. };
  543. static const struct ide_port_info tx4939ide_port_info __initdata = {
  544. .init_hwif = tx4939ide_init_hwif,
  545. .init_dma = tx4939ide_init_dma,
  546. .port_ops = &tx4939ide_port_ops,
  547. .dma_ops = &tx4939ide_dma_ops,
  548. .tp_ops = &tx4939ide_tp_ops,
  549. .host_flags = IDE_HFLAG_MMIO,
  550. .pio_mask = ATA_PIO4,
  551. .mwdma_mask = ATA_MWDMA2,
  552. .udma_mask = ATA_UDMA5,
  553. };
  554. static int __init tx4939ide_probe(struct platform_device *pdev)
  555. {
  556. hw_regs_t hw;
  557. hw_regs_t *hws[] = { &hw, NULL, NULL, NULL };
  558. struct ide_host *host;
  559. struct resource *res;
  560. int irq, ret;
  561. unsigned long mapbase;
  562. irq = platform_get_irq(pdev, 0);
  563. if (irq < 0)
  564. return -ENODEV;
  565. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  566. if (!res)
  567. return -ENODEV;
  568. if (!devm_request_mem_region(&pdev->dev, res->start,
  569. res->end - res->start + 1, "tx4938ide"))
  570. return -EBUSY;
  571. mapbase = (unsigned long)devm_ioremap(&pdev->dev, res->start,
  572. res->end - res->start + 1);
  573. if (!mapbase)
  574. return -EBUSY;
  575. memset(&hw, 0, sizeof(hw));
  576. hw.io_ports.data_addr =
  577. mapbase + tx4939ide_swizzlew(TX4939IDE_Data);
  578. hw.io_ports.error_addr =
  579. mapbase + tx4939ide_swizzleb(TX4939IDE_Error_Feature);
  580. hw.io_ports.nsect_addr =
  581. mapbase + tx4939ide_swizzleb(TX4939IDE_Sec);
  582. hw.io_ports.lbal_addr =
  583. mapbase + tx4939ide_swizzleb(TX4939IDE_LBA0);
  584. hw.io_ports.lbam_addr =
  585. mapbase + tx4939ide_swizzleb(TX4939IDE_LBA1);
  586. hw.io_ports.lbah_addr =
  587. mapbase + tx4939ide_swizzleb(TX4939IDE_LBA2);
  588. hw.io_ports.device_addr =
  589. mapbase + tx4939ide_swizzleb(TX4939IDE_DevHead);
  590. hw.io_ports.command_addr =
  591. mapbase + tx4939ide_swizzleb(TX4939IDE_Stat_Cmd);
  592. hw.io_ports.ctl_addr =
  593. mapbase + tx4939ide_swizzleb(TX4939IDE_AltStat_DevCtl);
  594. hw.irq = irq;
  595. hw.dev = &pdev->dev;
  596. pr_info("TX4939 IDE interface (base %#lx, irq %d)\n", mapbase, irq);
  597. host = ide_host_alloc(&tx4939ide_port_info, hws);
  598. if (!host)
  599. return -ENOMEM;
  600. /* use extra_base for base address of the all registers */
  601. host->ports[0]->extra_base = mapbase;
  602. ret = ide_host_register(host, &tx4939ide_port_info, hws);
  603. if (ret) {
  604. ide_host_free(host);
  605. return ret;
  606. }
  607. platform_set_drvdata(pdev, host);
  608. return 0;
  609. }
  610. static int __exit tx4939ide_remove(struct platform_device *pdev)
  611. {
  612. struct ide_host *host = platform_get_drvdata(pdev);
  613. ide_host_remove(host);
  614. return 0;
  615. }
  616. #ifdef CONFIG_PM
  617. static int tx4939ide_resume(struct platform_device *dev)
  618. {
  619. struct ide_host *host = platform_get_drvdata(dev);
  620. ide_hwif_t *hwif = host->ports[0];
  621. tx4939ide_init_hwif(hwif);
  622. return 0;
  623. }
  624. #else
  625. #define tx4939ide_resume NULL
  626. #endif
  627. static struct platform_driver tx4939ide_driver = {
  628. .driver = {
  629. .name = MODNAME,
  630. .owner = THIS_MODULE,
  631. },
  632. .remove = __exit_p(tx4939ide_remove),
  633. .resume = tx4939ide_resume,
  634. };
  635. static int __init tx4939ide_init(void)
  636. {
  637. return platform_driver_probe(&tx4939ide_driver, tx4939ide_probe);
  638. }
  639. static void __exit tx4939ide_exit(void)
  640. {
  641. platform_driver_unregister(&tx4939ide_driver);
  642. }
  643. module_init(tx4939ide_init);
  644. module_exit(tx4939ide_exit);
  645. MODULE_DESCRIPTION("TX4939 internal IDE driver");
  646. MODULE_LICENSE("GPL");
  647. MODULE_ALIAS("platform:tx4939ide");