tx4938ide.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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_hwif_t *hwif, ide_drive_t *drive)
  53. {
  54. struct tx4938ide_platform_info *pdata = dev_get_platdata(hwif->dev);
  55. u8 safe = drive->pio_mode - XFER_PIO_0;
  56. ide_drive_t *pair;
  57. pair = ide_get_pair_dev(drive);
  58. if (pair)
  59. safe = min_t(u8, safe, pair->pio_mode - XFER_PIO_0);
  60. tx4938ide_tune_ebusc(pdata->ebus_ch, pdata->gbus_clock, safe);
  61. }
  62. #ifdef __BIG_ENDIAN
  63. /* custom iops (independent from SWAP_IO_SPACE) */
  64. static void tx4938ide_input_data_swap(ide_drive_t *drive, struct ide_cmd *cmd,
  65. void *buf, unsigned int len)
  66. {
  67. unsigned long port = drive->hwif->io_ports.data_addr;
  68. unsigned short *ptr = buf;
  69. unsigned int count = (len + 1) / 2;
  70. while (count--)
  71. *ptr++ = cpu_to_le16(__raw_readw((void __iomem *)port));
  72. __ide_flush_dcache_range((unsigned long)buf, roundup(len, 2));
  73. }
  74. static void tx4938ide_output_data_swap(ide_drive_t *drive, struct ide_cmd *cmd,
  75. void *buf, unsigned int len)
  76. {
  77. unsigned long port = drive->hwif->io_ports.data_addr;
  78. unsigned short *ptr = buf;
  79. unsigned int count = (len + 1) / 2;
  80. while (count--) {
  81. __raw_writew(le16_to_cpu(*ptr), (void __iomem *)port);
  82. ptr++;
  83. }
  84. __ide_flush_dcache_range((unsigned long)buf, roundup(len, 2));
  85. }
  86. static const struct ide_tp_ops tx4938ide_tp_ops = {
  87. .exec_command = ide_exec_command,
  88. .read_status = ide_read_status,
  89. .read_altstatus = ide_read_altstatus,
  90. .write_devctl = ide_write_devctl,
  91. .dev_select = ide_dev_select,
  92. .tf_load = ide_tf_load,
  93. .tf_read = ide_tf_read,
  94. .input_data = tx4938ide_input_data_swap,
  95. .output_data = tx4938ide_output_data_swap,
  96. };
  97. #endif /* __BIG_ENDIAN */
  98. static const struct ide_port_ops tx4938ide_port_ops = {
  99. .set_pio_mode = tx4938ide_set_pio_mode,
  100. };
  101. static const struct ide_port_info tx4938ide_port_info __initconst = {
  102. .port_ops = &tx4938ide_port_ops,
  103. #ifdef __BIG_ENDIAN
  104. .tp_ops = &tx4938ide_tp_ops,
  105. #endif
  106. .host_flags = IDE_HFLAG_MMIO | IDE_HFLAG_NO_DMA,
  107. .pio_mask = ATA_PIO5,
  108. .chipset = ide_generic,
  109. };
  110. static int __init tx4938ide_probe(struct platform_device *pdev)
  111. {
  112. struct ide_hw hw, *hws[] = { &hw };
  113. struct ide_host *host;
  114. struct resource *res;
  115. struct tx4938ide_platform_info *pdata = dev_get_platdata(&pdev->dev);
  116. int irq, ret, i;
  117. unsigned long mapbase, mapctl;
  118. struct ide_port_info d = tx4938ide_port_info;
  119. irq = platform_get_irq(pdev, 0);
  120. if (irq < 0)
  121. return -ENODEV;
  122. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  123. if (!res)
  124. return -ENODEV;
  125. if (!devm_request_mem_region(&pdev->dev, res->start,
  126. resource_size(res), "tx4938ide"))
  127. return -EBUSY;
  128. mapbase = (unsigned long)devm_ioremap(&pdev->dev, res->start,
  129. 8 << pdata->ioport_shift);
  130. mapctl = (unsigned long)devm_ioremap(&pdev->dev,
  131. res->start + 0x10000 +
  132. (6 << pdata->ioport_shift),
  133. 1 << pdata->ioport_shift);
  134. if (!mapbase || !mapctl)
  135. return -EBUSY;
  136. memset(&hw, 0, sizeof(hw));
  137. if (pdata->ioport_shift) {
  138. unsigned long port = mapbase;
  139. unsigned long ctl = mapctl;
  140. hw.io_ports_array[0] = port;
  141. #ifdef __BIG_ENDIAN
  142. port++;
  143. ctl++;
  144. #endif
  145. for (i = 1; i <= 7; i++)
  146. hw.io_ports_array[i] =
  147. port + (i << pdata->ioport_shift);
  148. hw.io_ports.ctl_addr = ctl;
  149. } else
  150. ide_std_init_ports(&hw, mapbase, mapctl);
  151. hw.irq = irq;
  152. hw.dev = &pdev->dev;
  153. pr_info("TX4938 IDE interface (base %#lx, ctl %#lx, irq %d)\n",
  154. mapbase, mapctl, hw.irq);
  155. if (pdata->gbus_clock)
  156. tx4938ide_tune_ebusc(pdata->ebus_ch, pdata->gbus_clock, 0);
  157. else
  158. d.port_ops = NULL;
  159. ret = ide_host_add(&d, hws, 1, &host);
  160. if (!ret)
  161. platform_set_drvdata(pdev, host);
  162. return ret;
  163. }
  164. static int __exit tx4938ide_remove(struct platform_device *pdev)
  165. {
  166. struct ide_host *host = platform_get_drvdata(pdev);
  167. ide_host_remove(host);
  168. return 0;
  169. }
  170. static struct platform_driver tx4938ide_driver = {
  171. .driver = {
  172. .name = "tx4938ide",
  173. },
  174. .remove = __exit_p(tx4938ide_remove),
  175. };
  176. module_platform_driver_probe(tx4938ide_driver, tx4938ide_probe);
  177. MODULE_DESCRIPTION("TX4938 internal IDE driver");
  178. MODULE_LICENSE("GPL");
  179. MODULE_ALIAS("platform:tx4938ide");