tx4939ide.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631
  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. #include <asm/ide.h>
  20. #define MODNAME "tx4939ide"
  21. /* ATA Shadow Registers (8-bit except for Data which is 16-bit) */
  22. #define TX4939IDE_Data 0x000
  23. #define TX4939IDE_Error_Feature 0x001
  24. #define TX4939IDE_Sec 0x002
  25. #define TX4939IDE_LBA0 0x003
  26. #define TX4939IDE_LBA1 0x004
  27. #define TX4939IDE_LBA2 0x005
  28. #define TX4939IDE_DevHead 0x006
  29. #define TX4939IDE_Stat_Cmd 0x007
  30. #define TX4939IDE_AltStat_DevCtl 0x402
  31. /* H/W DMA Registers */
  32. #define TX4939IDE_DMA_Cmd 0x800 /* 8-bit */
  33. #define TX4939IDE_DMA_Stat 0x802 /* 8-bit */
  34. #define TX4939IDE_PRD_Ptr 0x804 /* 32-bit */
  35. /* ATA100 CORE Registers (16-bit) */
  36. #define TX4939IDE_Sys_Ctl 0xc00
  37. #define TX4939IDE_Xfer_Cnt_1 0xc08
  38. #define TX4939IDE_Xfer_Cnt_2 0xc0a
  39. #define TX4939IDE_Sec_Cnt 0xc10
  40. #define TX4939IDE_Start_Lo_Addr 0xc18
  41. #define TX4939IDE_Start_Up_Addr 0xc20
  42. #define TX4939IDE_Add_Ctl 0xc28
  43. #define TX4939IDE_Lo_Burst_Cnt 0xc30
  44. #define TX4939IDE_Up_Burst_Cnt 0xc38
  45. #define TX4939IDE_PIO_Addr 0xc88
  46. #define TX4939IDE_H_Rst_Tim 0xc90
  47. #define TX4939IDE_Int_Ctl 0xc98
  48. #define TX4939IDE_Pkt_Cmd 0xcb8
  49. #define TX4939IDE_Bxfer_Cnt_Hi 0xcc0
  50. #define TX4939IDE_Bxfer_Cnt_Lo 0xcc8
  51. #define TX4939IDE_Dev_TErr 0xcd0
  52. #define TX4939IDE_Pkt_Xfer_Ctl 0xcd8
  53. #define TX4939IDE_Start_TAddr 0xce0
  54. /* bits for Int_Ctl */
  55. #define TX4939IDE_INT_ADDRERR 0x80
  56. #define TX4939IDE_INT_REACHMUL 0x40
  57. #define TX4939IDE_INT_DEVTIMING 0x20
  58. #define TX4939IDE_INT_UDMATERM 0x10
  59. #define TX4939IDE_INT_TIMER 0x08
  60. #define TX4939IDE_INT_BUSERR 0x04
  61. #define TX4939IDE_INT_XFEREND 0x02
  62. #define TX4939IDE_INT_HOST 0x01
  63. #define TX4939IDE_IGNORE_INTS \
  64. (TX4939IDE_INT_ADDRERR | TX4939IDE_INT_REACHMUL | \
  65. TX4939IDE_INT_DEVTIMING | TX4939IDE_INT_UDMATERM | \
  66. TX4939IDE_INT_TIMER | TX4939IDE_INT_XFEREND)
  67. #ifdef __BIG_ENDIAN
  68. #define tx4939ide_swizzlel(a) ((a) ^ 4)
  69. #define tx4939ide_swizzlew(a) ((a) ^ 6)
  70. #define tx4939ide_swizzleb(a) ((a) ^ 7)
  71. #else
  72. #define tx4939ide_swizzlel(a) (a)
  73. #define tx4939ide_swizzlew(a) (a)
  74. #define tx4939ide_swizzleb(a) (a)
  75. #endif
  76. static u16 tx4939ide_readw(void __iomem *base, u32 reg)
  77. {
  78. return __raw_readw(base + tx4939ide_swizzlew(reg));
  79. }
  80. static u8 tx4939ide_readb(void __iomem *base, u32 reg)
  81. {
  82. return __raw_readb(base + tx4939ide_swizzleb(reg));
  83. }
  84. static void tx4939ide_writel(u32 val, void __iomem *base, u32 reg)
  85. {
  86. __raw_writel(val, base + tx4939ide_swizzlel(reg));
  87. }
  88. static void tx4939ide_writew(u16 val, void __iomem *base, u32 reg)
  89. {
  90. __raw_writew(val, base + tx4939ide_swizzlew(reg));
  91. }
  92. static void tx4939ide_writeb(u8 val, void __iomem *base, u32 reg)
  93. {
  94. __raw_writeb(val, base + tx4939ide_swizzleb(reg));
  95. }
  96. #define TX4939IDE_BASE(hwif) ((void __iomem *)(hwif)->extra_base)
  97. static void tx4939ide_set_pio_mode(ide_hwif_t *hwif, ide_drive_t *drive)
  98. {
  99. int is_slave = drive->dn;
  100. u32 mask, val;
  101. const u8 pio = drive->pio_mode - XFER_PIO_0;
  102. u8 safe = pio;
  103. ide_drive_t *pair;
  104. pair = ide_get_pair_dev(drive);
  105. if (pair)
  106. safe = min_t(u8, safe, pair->pio_mode - XFER_PIO_0);
  107. /*
  108. * Update Command Transfer Mode for master/slave and Data
  109. * Transfer Mode for this drive.
  110. */
  111. mask = is_slave ? 0x07f00000 : 0x000007f0;
  112. val = ((safe << 8) | (pio << 4)) << (is_slave ? 16 : 0);
  113. hwif->select_data = (hwif->select_data & ~mask) | val;
  114. /* tx4939ide_tf_load_fixup() will set the Sys_Ctl register */
  115. }
  116. static void tx4939ide_set_dma_mode(ide_hwif_t *hwif, ide_drive_t *drive)
  117. {
  118. u32 mask, val;
  119. const u8 mode = drive->dma_mode;
  120. /* Update Data Transfer Mode for this drive. */
  121. if (mode >= XFER_UDMA_0)
  122. val = mode - XFER_UDMA_0 + 8;
  123. else
  124. val = mode - XFER_MW_DMA_0 + 5;
  125. if (drive->dn) {
  126. mask = 0x00f00000;
  127. val <<= 20;
  128. } else {
  129. mask = 0x000000f0;
  130. val <<= 4;
  131. }
  132. hwif->select_data = (hwif->select_data & ~mask) | val;
  133. /* tx4939ide_tf_load_fixup() will set the Sys_Ctl register */
  134. }
  135. static u16 tx4939ide_check_error_ints(ide_hwif_t *hwif)
  136. {
  137. void __iomem *base = TX4939IDE_BASE(hwif);
  138. u16 ctl = tx4939ide_readw(base, TX4939IDE_Int_Ctl);
  139. if (ctl & TX4939IDE_INT_BUSERR) {
  140. /* reset FIFO */
  141. u16 sysctl = tx4939ide_readw(base, TX4939IDE_Sys_Ctl);
  142. tx4939ide_writew(sysctl | 0x4000, base, TX4939IDE_Sys_Ctl);
  143. mmiowb();
  144. /* wait 12GBUSCLK (typ. 60ns @ GBUS200MHz, max 270ns) */
  145. ndelay(270);
  146. tx4939ide_writew(sysctl, base, TX4939IDE_Sys_Ctl);
  147. }
  148. if (ctl & (TX4939IDE_INT_ADDRERR |
  149. TX4939IDE_INT_DEVTIMING | TX4939IDE_INT_BUSERR))
  150. pr_err("%s: Error interrupt %#x (%s%s%s )\n",
  151. hwif->name, ctl,
  152. ctl & TX4939IDE_INT_ADDRERR ? " Address-Error" : "",
  153. ctl & TX4939IDE_INT_DEVTIMING ? " DEV-Timing" : "",
  154. ctl & TX4939IDE_INT_BUSERR ? " Bus-Error" : "");
  155. return ctl;
  156. }
  157. static void tx4939ide_clear_irq(ide_drive_t *drive)
  158. {
  159. ide_hwif_t *hwif;
  160. void __iomem *base;
  161. u16 ctl;
  162. /*
  163. * tx4939ide_dma_test_irq() and tx4939ide_dma_end() do all job
  164. * for DMA case.
  165. */
  166. if (drive->waiting_for_dma)
  167. return;
  168. hwif = drive->hwif;
  169. base = TX4939IDE_BASE(hwif);
  170. ctl = tx4939ide_check_error_ints(hwif);
  171. tx4939ide_writew(ctl, base, TX4939IDE_Int_Ctl);
  172. }
  173. static u8 tx4939ide_cable_detect(ide_hwif_t *hwif)
  174. {
  175. void __iomem *base = TX4939IDE_BASE(hwif);
  176. return tx4939ide_readw(base, TX4939IDE_Sys_Ctl) & 0x2000 ?
  177. ATA_CBL_PATA40 : ATA_CBL_PATA80;
  178. }
  179. #ifdef __BIG_ENDIAN
  180. static void tx4939ide_dma_host_set(ide_drive_t *drive, int on)
  181. {
  182. ide_hwif_t *hwif = drive->hwif;
  183. u8 unit = drive->dn;
  184. void __iomem *base = TX4939IDE_BASE(hwif);
  185. u8 dma_stat = tx4939ide_readb(base, TX4939IDE_DMA_Stat);
  186. if (on)
  187. dma_stat |= (1 << (5 + unit));
  188. else
  189. dma_stat &= ~(1 << (5 + unit));
  190. tx4939ide_writeb(dma_stat, base, TX4939IDE_DMA_Stat);
  191. }
  192. #else
  193. #define tx4939ide_dma_host_set ide_dma_host_set
  194. #endif
  195. static u8 tx4939ide_clear_dma_status(void __iomem *base)
  196. {
  197. u8 dma_stat;
  198. /* read DMA status for INTR & ERROR flags */
  199. dma_stat = tx4939ide_readb(base, TX4939IDE_DMA_Stat);
  200. /* clear INTR & ERROR flags */
  201. tx4939ide_writeb(dma_stat | ATA_DMA_INTR | ATA_DMA_ERR, base,
  202. TX4939IDE_DMA_Stat);
  203. /* recover intmask cleared by writing to bit2 of DMA_Stat */
  204. tx4939ide_writew(TX4939IDE_IGNORE_INTS << 8, base, TX4939IDE_Int_Ctl);
  205. return dma_stat;
  206. }
  207. #ifdef __BIG_ENDIAN
  208. /* custom ide_build_dmatable to handle swapped layout */
  209. static int tx4939ide_build_dmatable(ide_drive_t *drive, struct ide_cmd *cmd)
  210. {
  211. ide_hwif_t *hwif = drive->hwif;
  212. u32 *table = (u32 *)hwif->dmatable_cpu;
  213. unsigned int count = 0;
  214. int i;
  215. struct scatterlist *sg;
  216. for_each_sg(hwif->sg_table, sg, cmd->sg_nents, i) {
  217. u32 cur_addr, cur_len, bcount;
  218. cur_addr = sg_dma_address(sg);
  219. cur_len = sg_dma_len(sg);
  220. /*
  221. * Fill in the DMA table, without crossing any 64kB boundaries.
  222. */
  223. while (cur_len) {
  224. if (count++ >= PRD_ENTRIES)
  225. goto use_pio_instead;
  226. bcount = 0x10000 - (cur_addr & 0xffff);
  227. if (bcount > cur_len)
  228. bcount = cur_len;
  229. /*
  230. * This workaround for zero count seems required.
  231. * (standard ide_build_dmatable does it too)
  232. */
  233. if (bcount == 0x10000)
  234. bcount = 0x8000;
  235. *table++ = bcount & 0xffff;
  236. *table++ = cur_addr;
  237. cur_addr += bcount;
  238. cur_len -= bcount;
  239. }
  240. }
  241. if (count) {
  242. *(table - 2) |= 0x80000000;
  243. return count;
  244. }
  245. use_pio_instead:
  246. printk(KERN_ERR "%s: %s\n", drive->name,
  247. count ? "DMA table too small" : "empty DMA table?");
  248. return 0; /* revert to PIO for this request */
  249. }
  250. #else
  251. #define tx4939ide_build_dmatable ide_build_dmatable
  252. #endif
  253. static int tx4939ide_dma_setup(ide_drive_t *drive, struct ide_cmd *cmd)
  254. {
  255. ide_hwif_t *hwif = drive->hwif;
  256. void __iomem *base = TX4939IDE_BASE(hwif);
  257. u8 rw = (cmd->tf_flags & IDE_TFLAG_WRITE) ? 0 : ATA_DMA_WR;
  258. /* fall back to PIO! */
  259. if (tx4939ide_build_dmatable(drive, cmd) == 0)
  260. return 1;
  261. /* PRD table */
  262. tx4939ide_writel(hwif->dmatable_dma, base, TX4939IDE_PRD_Ptr);
  263. /* specify r/w */
  264. tx4939ide_writeb(rw, base, TX4939IDE_DMA_Cmd);
  265. /* clear INTR & ERROR flags */
  266. tx4939ide_clear_dma_status(base);
  267. tx4939ide_writew(SECTOR_SIZE / 2, base, drive->dn ?
  268. TX4939IDE_Xfer_Cnt_2 : TX4939IDE_Xfer_Cnt_1);
  269. tx4939ide_writew(blk_rq_sectors(cmd->rq), base, TX4939IDE_Sec_Cnt);
  270. return 0;
  271. }
  272. static int tx4939ide_dma_end(ide_drive_t *drive)
  273. {
  274. ide_hwif_t *hwif = drive->hwif;
  275. u8 dma_stat, dma_cmd;
  276. void __iomem *base = TX4939IDE_BASE(hwif);
  277. u16 ctl = tx4939ide_readw(base, TX4939IDE_Int_Ctl);
  278. /* get DMA command mode */
  279. dma_cmd = tx4939ide_readb(base, TX4939IDE_DMA_Cmd);
  280. /* stop DMA */
  281. tx4939ide_writeb(dma_cmd & ~ATA_DMA_START, base, TX4939IDE_DMA_Cmd);
  282. /* read and clear the INTR & ERROR bits */
  283. dma_stat = tx4939ide_clear_dma_status(base);
  284. #define CHECK_DMA_MASK (ATA_DMA_ACTIVE | ATA_DMA_ERR | ATA_DMA_INTR)
  285. /* verify good DMA status */
  286. if ((dma_stat & CHECK_DMA_MASK) == 0 &&
  287. (ctl & (TX4939IDE_INT_XFEREND | TX4939IDE_INT_HOST)) ==
  288. (TX4939IDE_INT_XFEREND | TX4939IDE_INT_HOST))
  289. /* INT_IDE lost... bug? */
  290. return 0;
  291. return ((dma_stat & CHECK_DMA_MASK) !=
  292. ATA_DMA_INTR) ? 0x10 | dma_stat : 0;
  293. }
  294. /* returns 1 if DMA IRQ issued, 0 otherwise */
  295. static int tx4939ide_dma_test_irq(ide_drive_t *drive)
  296. {
  297. ide_hwif_t *hwif = drive->hwif;
  298. void __iomem *base = TX4939IDE_BASE(hwif);
  299. u16 ctl, ide_int;
  300. u8 dma_stat, stat;
  301. int found = 0;
  302. ctl = tx4939ide_check_error_ints(hwif);
  303. ide_int = ctl & (TX4939IDE_INT_XFEREND | TX4939IDE_INT_HOST);
  304. switch (ide_int) {
  305. case TX4939IDE_INT_HOST:
  306. /* On error, XFEREND might not be asserted. */
  307. stat = tx4939ide_readb(base, TX4939IDE_AltStat_DevCtl);
  308. if ((stat & (ATA_BUSY | ATA_DRQ | ATA_ERR)) == ATA_ERR)
  309. found = 1;
  310. else
  311. /* Wait for XFEREND (Mask HOST and unmask XFEREND) */
  312. ctl &= ~TX4939IDE_INT_XFEREND << 8;
  313. ctl |= ide_int << 8;
  314. break;
  315. case TX4939IDE_INT_HOST | TX4939IDE_INT_XFEREND:
  316. dma_stat = tx4939ide_readb(base, TX4939IDE_DMA_Stat);
  317. if (!(dma_stat & ATA_DMA_INTR))
  318. pr_warning("%s: weird interrupt status. "
  319. "DMA_Stat %#02x int_ctl %#04x\n",
  320. hwif->name, dma_stat, ctl);
  321. found = 1;
  322. break;
  323. }
  324. /*
  325. * Do not clear XFEREND, HOST now. They will be cleared by
  326. * clearing bit2 of DMA_Stat.
  327. */
  328. ctl &= ~ide_int;
  329. tx4939ide_writew(ctl, base, TX4939IDE_Int_Ctl);
  330. return found;
  331. }
  332. #ifdef __BIG_ENDIAN
  333. static u8 tx4939ide_dma_sff_read_status(ide_hwif_t *hwif)
  334. {
  335. void __iomem *base = TX4939IDE_BASE(hwif);
  336. return tx4939ide_readb(base, TX4939IDE_DMA_Stat);
  337. }
  338. #else
  339. #define tx4939ide_dma_sff_read_status ide_dma_sff_read_status
  340. #endif
  341. static void tx4939ide_init_hwif(ide_hwif_t *hwif)
  342. {
  343. void __iomem *base = TX4939IDE_BASE(hwif);
  344. /* Soft Reset */
  345. tx4939ide_writew(0x8000, base, TX4939IDE_Sys_Ctl);
  346. mmiowb();
  347. /* at least 20 GBUSCLK (typ. 100ns @ GBUS200MHz, max 450ns) */
  348. ndelay(450);
  349. tx4939ide_writew(0x0000, base, TX4939IDE_Sys_Ctl);
  350. /* mask some interrupts and clear all interrupts */
  351. tx4939ide_writew((TX4939IDE_IGNORE_INTS << 8) | 0xff, base,
  352. TX4939IDE_Int_Ctl);
  353. tx4939ide_writew(0x0008, base, TX4939IDE_Lo_Burst_Cnt);
  354. tx4939ide_writew(0, base, TX4939IDE_Up_Burst_Cnt);
  355. }
  356. static int tx4939ide_init_dma(ide_hwif_t *hwif, const struct ide_port_info *d)
  357. {
  358. hwif->dma_base =
  359. hwif->extra_base + tx4939ide_swizzleb(TX4939IDE_DMA_Cmd);
  360. /*
  361. * Note that we cannot use ATA_DMA_TABLE_OFS, ATA_DMA_STATUS
  362. * for big endian.
  363. */
  364. return ide_allocate_dma_engine(hwif);
  365. }
  366. static void tx4939ide_tf_load_fixup(ide_drive_t *drive)
  367. {
  368. ide_hwif_t *hwif = drive->hwif;
  369. void __iomem *base = TX4939IDE_BASE(hwif);
  370. u16 sysctl = hwif->select_data >> (drive->dn ? 16 : 0);
  371. /*
  372. * Fix ATA100 CORE System Control Register. (The write to the
  373. * Device/Head register may write wrong data to the System
  374. * Control Register)
  375. * While Sys_Ctl is written here, dev_select() is not needed.
  376. */
  377. tx4939ide_writew(sysctl, base, TX4939IDE_Sys_Ctl);
  378. }
  379. static void tx4939ide_tf_load(ide_drive_t *drive, struct ide_taskfile *tf,
  380. u8 valid)
  381. {
  382. ide_tf_load(drive, tf, valid);
  383. if (valid & IDE_VALID_DEVICE)
  384. tx4939ide_tf_load_fixup(drive);
  385. }
  386. #ifdef __BIG_ENDIAN
  387. /* custom iops (independent from SWAP_IO_SPACE) */
  388. static void tx4939ide_input_data_swap(ide_drive_t *drive, struct ide_cmd *cmd,
  389. void *buf, unsigned int len)
  390. {
  391. unsigned long port = drive->hwif->io_ports.data_addr;
  392. unsigned short *ptr = buf;
  393. unsigned int count = (len + 1) / 2;
  394. while (count--)
  395. *ptr++ = cpu_to_le16(__raw_readw((void __iomem *)port));
  396. __ide_flush_dcache_range((unsigned long)buf, roundup(len, 2));
  397. }
  398. static void tx4939ide_output_data_swap(ide_drive_t *drive, struct ide_cmd *cmd,
  399. void *buf, unsigned int len)
  400. {
  401. unsigned long port = drive->hwif->io_ports.data_addr;
  402. unsigned short *ptr = buf;
  403. unsigned int count = (len + 1) / 2;
  404. while (count--) {
  405. __raw_writew(le16_to_cpu(*ptr), (void __iomem *)port);
  406. ptr++;
  407. }
  408. __ide_flush_dcache_range((unsigned long)buf, roundup(len, 2));
  409. }
  410. static const struct ide_tp_ops tx4939ide_tp_ops = {
  411. .exec_command = ide_exec_command,
  412. .read_status = ide_read_status,
  413. .read_altstatus = ide_read_altstatus,
  414. .write_devctl = ide_write_devctl,
  415. .dev_select = ide_dev_select,
  416. .tf_load = tx4939ide_tf_load,
  417. .tf_read = ide_tf_read,
  418. .input_data = tx4939ide_input_data_swap,
  419. .output_data = tx4939ide_output_data_swap,
  420. };
  421. #else /* __LITTLE_ENDIAN */
  422. static const struct ide_tp_ops tx4939ide_tp_ops = {
  423. .exec_command = ide_exec_command,
  424. .read_status = ide_read_status,
  425. .read_altstatus = ide_read_altstatus,
  426. .write_devctl = ide_write_devctl,
  427. .dev_select = ide_dev_select,
  428. .tf_load = tx4939ide_tf_load,
  429. .tf_read = ide_tf_read,
  430. .input_data = ide_input_data,
  431. .output_data = ide_output_data,
  432. };
  433. #endif /* __LITTLE_ENDIAN */
  434. static const struct ide_port_ops tx4939ide_port_ops = {
  435. .set_pio_mode = tx4939ide_set_pio_mode,
  436. .set_dma_mode = tx4939ide_set_dma_mode,
  437. .clear_irq = tx4939ide_clear_irq,
  438. .cable_detect = tx4939ide_cable_detect,
  439. };
  440. static const struct ide_dma_ops tx4939ide_dma_ops = {
  441. .dma_host_set = tx4939ide_dma_host_set,
  442. .dma_setup = tx4939ide_dma_setup,
  443. .dma_start = ide_dma_start,
  444. .dma_end = tx4939ide_dma_end,
  445. .dma_test_irq = tx4939ide_dma_test_irq,
  446. .dma_lost_irq = ide_dma_lost_irq,
  447. .dma_timer_expiry = ide_dma_sff_timer_expiry,
  448. .dma_sff_read_status = tx4939ide_dma_sff_read_status,
  449. };
  450. static const struct ide_port_info tx4939ide_port_info __initconst = {
  451. .init_hwif = tx4939ide_init_hwif,
  452. .init_dma = tx4939ide_init_dma,
  453. .port_ops = &tx4939ide_port_ops,
  454. .dma_ops = &tx4939ide_dma_ops,
  455. .tp_ops = &tx4939ide_tp_ops,
  456. .host_flags = IDE_HFLAG_MMIO,
  457. .pio_mask = ATA_PIO4,
  458. .mwdma_mask = ATA_MWDMA2,
  459. .udma_mask = ATA_UDMA5,
  460. .chipset = ide_generic,
  461. };
  462. static int __init tx4939ide_probe(struct platform_device *pdev)
  463. {
  464. struct ide_hw hw, *hws[] = { &hw };
  465. struct ide_host *host;
  466. struct resource *res;
  467. int irq, ret;
  468. unsigned long mapbase;
  469. irq = platform_get_irq(pdev, 0);
  470. if (irq < 0)
  471. return -ENODEV;
  472. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  473. if (!res)
  474. return -ENODEV;
  475. if (!devm_request_mem_region(&pdev->dev, res->start,
  476. resource_size(res), "tx4938ide"))
  477. return -EBUSY;
  478. mapbase = (unsigned long)devm_ioremap(&pdev->dev, res->start,
  479. resource_size(res));
  480. if (!mapbase)
  481. return -EBUSY;
  482. memset(&hw, 0, sizeof(hw));
  483. hw.io_ports.data_addr =
  484. mapbase + tx4939ide_swizzlew(TX4939IDE_Data);
  485. hw.io_ports.error_addr =
  486. mapbase + tx4939ide_swizzleb(TX4939IDE_Error_Feature);
  487. hw.io_ports.nsect_addr =
  488. mapbase + tx4939ide_swizzleb(TX4939IDE_Sec);
  489. hw.io_ports.lbal_addr =
  490. mapbase + tx4939ide_swizzleb(TX4939IDE_LBA0);
  491. hw.io_ports.lbam_addr =
  492. mapbase + tx4939ide_swizzleb(TX4939IDE_LBA1);
  493. hw.io_ports.lbah_addr =
  494. mapbase + tx4939ide_swizzleb(TX4939IDE_LBA2);
  495. hw.io_ports.device_addr =
  496. mapbase + tx4939ide_swizzleb(TX4939IDE_DevHead);
  497. hw.io_ports.command_addr =
  498. mapbase + tx4939ide_swizzleb(TX4939IDE_Stat_Cmd);
  499. hw.io_ports.ctl_addr =
  500. mapbase + tx4939ide_swizzleb(TX4939IDE_AltStat_DevCtl);
  501. hw.irq = irq;
  502. hw.dev = &pdev->dev;
  503. pr_info("TX4939 IDE interface (base %#lx, irq %d)\n", mapbase, irq);
  504. host = ide_host_alloc(&tx4939ide_port_info, hws, 1);
  505. if (!host)
  506. return -ENOMEM;
  507. /* use extra_base for base address of the all registers */
  508. host->ports[0]->extra_base = mapbase;
  509. ret = ide_host_register(host, &tx4939ide_port_info, hws);
  510. if (ret) {
  511. ide_host_free(host);
  512. return ret;
  513. }
  514. platform_set_drvdata(pdev, host);
  515. return 0;
  516. }
  517. static int __exit tx4939ide_remove(struct platform_device *pdev)
  518. {
  519. struct ide_host *host = platform_get_drvdata(pdev);
  520. ide_host_remove(host);
  521. return 0;
  522. }
  523. #ifdef CONFIG_PM
  524. static int tx4939ide_resume(struct platform_device *dev)
  525. {
  526. struct ide_host *host = platform_get_drvdata(dev);
  527. ide_hwif_t *hwif = host->ports[0];
  528. tx4939ide_init_hwif(hwif);
  529. return 0;
  530. }
  531. #else
  532. #define tx4939ide_resume NULL
  533. #endif
  534. static struct platform_driver tx4939ide_driver = {
  535. .driver = {
  536. .name = MODNAME,
  537. },
  538. .remove = __exit_p(tx4939ide_remove),
  539. .resume = tx4939ide_resume,
  540. };
  541. module_platform_driver_probe(tx4939ide_driver, tx4939ide_probe);
  542. MODULE_DESCRIPTION("TX4939 internal IDE driver");
  543. MODULE_LICENSE("GPL");
  544. MODULE_ALIAS("platform:tx4939ide");