tx4938ide.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /*
  2. * TX4938 internal IDE driver
  3. * Based on tx4939ide.c.
  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/platform_device.h>
  16. #include <linux/io.h>
  17. #include <asm/ide.h>
  18. #include <asm/txx9/tx4938.h>
  19. static void tx4938ide_tune_ebusc(unsigned int ebus_ch,
  20. unsigned int gbus_clock,
  21. u8 pio)
  22. {
  23. struct ide_timing *t = ide_timing_find_mode(XFER_PIO_0 + pio);
  24. u64 cr = __raw_readq(&tx4938_ebuscptr->cr[ebus_ch]);
  25. unsigned int sp = (cr >> 4) & 3;
  26. unsigned int clock = gbus_clock / (4 - sp);
  27. unsigned int cycle = 1000000000 / clock;
  28. unsigned int shwt;
  29. int wt;
  30. /* Minimum DIOx- active time */
  31. wt = DIV_ROUND_UP(t->act8b, cycle) - 2;
  32. /* IORDY setup time: 35ns */
  33. wt = max_t(int, wt, DIV_ROUND_UP(35, cycle));
  34. /* actual wait-cycle is max(wt & ~1, 1) */
  35. if (wt > 2 && (wt & 1))
  36. wt++;
  37. wt &= ~1;
  38. /* Address-valid to DIOR/DIOW setup */
  39. shwt = DIV_ROUND_UP(t->setup, cycle);
  40. /* -DIOx recovery time (SHWT * 4) and cycle time requirement */
  41. while ((shwt * 4 + wt + (wt ? 2 : 3)) * cycle < t->cycle)
  42. shwt++;
  43. if (shwt > 7) {
  44. pr_warning("tx4938ide: SHWT violation (%d)\n", shwt);
  45. shwt = 7;
  46. }
  47. pr_debug("tx4938ide: ebus %d, bus cycle %dns, WT %d, SHWT %d\n",
  48. ebus_ch, cycle, wt, shwt);
  49. __raw_writeq((cr & ~0x3f007ull) | (wt << 12) | shwt,
  50. &tx4938_ebuscptr->cr[ebus_ch]);
  51. }
  52. static void tx4938ide_set_pio_mode(ide_drive_t *drive, const u8 pio)
  53. {
  54. ide_hwif_t *hwif = drive->hwif;
  55. struct tx4938ide_platform_info *pdata = hwif->dev->platform_data;
  56. u8 safe = pio;
  57. ide_drive_t *pair;
  58. pair = ide_get_pair_dev(drive);
  59. if (pair)
  60. safe = min(safe, ide_get_best_pio_mode(pair, 255, 5));
  61. tx4938ide_tune_ebusc(pdata->ebus_ch, pdata->gbus_clock, safe);
  62. }
  63. #ifdef __BIG_ENDIAN
  64. /* custom iops (independent from SWAP_IO_SPACE) */
  65. static void tx4938ide_input_data_swap(ide_drive_t *drive, struct ide_cmd *cmd,
  66. void *buf, unsigned int len)
  67. {
  68. unsigned long port = drive->hwif->io_ports.data_addr;
  69. unsigned short *ptr = buf;
  70. unsigned int count = (len + 1) / 2;
  71. while (count--)
  72. *ptr++ = cpu_to_le16(__raw_readw((void __iomem *)port));
  73. __ide_flush_dcache_range((unsigned long)buf, roundup(len, 2));
  74. }
  75. static void tx4938ide_output_data_swap(ide_drive_t *drive, struct ide_cmd *cmd,
  76. void *buf, unsigned int len)
  77. {
  78. unsigned long port = drive->hwif->io_ports.data_addr;
  79. unsigned short *ptr = buf;
  80. unsigned int count = (len + 1) / 2;
  81. while (count--) {
  82. __raw_writew(le16_to_cpu(*ptr), (void __iomem *)port);
  83. ptr++;
  84. }
  85. __ide_flush_dcache_range((unsigned long)buf, roundup(len, 2));
  86. }
  87. static const struct ide_tp_ops tx4938ide_tp_ops = {
  88. .exec_command = ide_exec_command,
  89. .read_status = ide_read_status,
  90. .read_altstatus = ide_read_altstatus,
  91. .write_devctl = ide_write_devctl,
  92. .dev_select = ide_dev_select,
  93. .tf_load = ide_tf_load,
  94. .tf_read = ide_tf_read,
  95. .input_data = tx4938ide_input_data_swap,
  96. .output_data = tx4938ide_output_data_swap,
  97. };
  98. #endif /* __BIG_ENDIAN */
  99. static const struct ide_port_ops tx4938ide_port_ops = {
  100. .set_pio_mode = tx4938ide_set_pio_mode,
  101. };
  102. static const struct ide_port_info tx4938ide_port_info __initdata = {
  103. .port_ops = &tx4938ide_port_ops,
  104. #ifdef __BIG_ENDIAN
  105. .tp_ops = &tx4938ide_tp_ops,
  106. #endif
  107. .host_flags = IDE_HFLAG_MMIO | IDE_HFLAG_NO_DMA,
  108. .pio_mask = ATA_PIO5,
  109. .chipset = ide_generic,
  110. };
  111. static int __init tx4938ide_probe(struct platform_device *pdev)
  112. {
  113. hw_regs_t hw;
  114. hw_regs_t *hws[] = { &hw, NULL, NULL, NULL };
  115. struct ide_host *host;
  116. struct resource *res;
  117. struct tx4938ide_platform_info *pdata = pdev->dev.platform_data;
  118. int irq, ret, i;
  119. unsigned long mapbase, mapctl;
  120. struct ide_port_info d = tx4938ide_port_info;
  121. irq = platform_get_irq(pdev, 0);
  122. if (irq < 0)
  123. return -ENODEV;
  124. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  125. if (!res)
  126. return -ENODEV;
  127. if (!devm_request_mem_region(&pdev->dev, res->start,
  128. res->end - res->start + 1, "tx4938ide"))
  129. return -EBUSY;
  130. mapbase = (unsigned long)devm_ioremap(&pdev->dev, res->start,
  131. 8 << pdata->ioport_shift);
  132. mapctl = (unsigned long)devm_ioremap(&pdev->dev,
  133. res->start + 0x10000 +
  134. (6 << pdata->ioport_shift),
  135. 1 << pdata->ioport_shift);
  136. if (!mapbase || !mapctl)
  137. return -EBUSY;
  138. memset(&hw, 0, sizeof(hw));
  139. if (pdata->ioport_shift) {
  140. unsigned long port = mapbase;
  141. unsigned long ctl = mapctl;
  142. hw.io_ports_array[0] = port;
  143. #ifdef __BIG_ENDIAN
  144. port++;
  145. ctl++;
  146. #endif
  147. for (i = 1; i <= 7; i++)
  148. hw.io_ports_array[i] =
  149. port + (i << pdata->ioport_shift);
  150. hw.io_ports.ctl_addr = ctl;
  151. } else
  152. ide_std_init_ports(&hw, mapbase, mapctl);
  153. hw.irq = irq;
  154. hw.dev = &pdev->dev;
  155. pr_info("TX4938 IDE interface (base %#lx, ctl %#lx, irq %d)\n",
  156. mapbase, mapctl, hw.irq);
  157. if (pdata->gbus_clock)
  158. tx4938ide_tune_ebusc(pdata->ebus_ch, pdata->gbus_clock, 0);
  159. else
  160. d.port_ops = NULL;
  161. ret = ide_host_add(&d, hws, &host);
  162. if (!ret)
  163. platform_set_drvdata(pdev, host);
  164. return ret;
  165. }
  166. static int __exit tx4938ide_remove(struct platform_device *pdev)
  167. {
  168. struct ide_host *host = platform_get_drvdata(pdev);
  169. ide_host_remove(host);
  170. return 0;
  171. }
  172. static struct platform_driver tx4938ide_driver = {
  173. .driver = {
  174. .name = "tx4938ide",
  175. .owner = THIS_MODULE,
  176. },
  177. .remove = __exit_p(tx4938ide_remove),
  178. };
  179. static int __init tx4938ide_init(void)
  180. {
  181. return platform_driver_probe(&tx4938ide_driver, tx4938ide_probe);
  182. }
  183. static void __exit tx4938ide_exit(void)
  184. {
  185. platform_driver_unregister(&tx4938ide_driver);
  186. }
  187. module_init(tx4938ide_init);
  188. module_exit(tx4938ide_exit);
  189. MODULE_DESCRIPTION("TX4938 internal IDE driver");
  190. MODULE_LICENSE("GPL");
  191. MODULE_ALIAS("platform:tx4938ide");