tx4938ide.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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 u8 tx4938ide_inb(unsigned long port)
  66. {
  67. return __raw_readb((void __iomem *)port);
  68. }
  69. static void tx4938ide_outb(u8 value, unsigned long port)
  70. {
  71. __raw_writeb(value, (void __iomem *)port);
  72. }
  73. static void tx4938ide_tf_load(ide_drive_t *drive, struct ide_cmd *cmd)
  74. {
  75. ide_hwif_t *hwif = drive->hwif;
  76. struct ide_io_ports *io_ports = &hwif->io_ports;
  77. struct ide_taskfile *tf = &cmd->tf;
  78. u8 HIHI = cmd->tf_flags & IDE_TFLAG_LBA48 ? 0xE0 : 0xEF;
  79. if (cmd->ftf_flags & IDE_FTFLAG_FLAGGED)
  80. HIHI = 0xFF;
  81. if (cmd->tf_flags & IDE_TFLAG_OUT_HOB_FEATURE)
  82. tx4938ide_outb(tf->hob_feature, io_ports->feature_addr);
  83. if (cmd->tf_flags & IDE_TFLAG_OUT_HOB_NSECT)
  84. tx4938ide_outb(tf->hob_nsect, io_ports->nsect_addr);
  85. if (cmd->tf_flags & IDE_TFLAG_OUT_HOB_LBAL)
  86. tx4938ide_outb(tf->hob_lbal, io_ports->lbal_addr);
  87. if (cmd->tf_flags & IDE_TFLAG_OUT_HOB_LBAM)
  88. tx4938ide_outb(tf->hob_lbam, io_ports->lbam_addr);
  89. if (cmd->tf_flags & IDE_TFLAG_OUT_HOB_LBAH)
  90. tx4938ide_outb(tf->hob_lbah, io_ports->lbah_addr);
  91. if (cmd->tf_flags & IDE_TFLAG_OUT_FEATURE)
  92. tx4938ide_outb(tf->feature, io_ports->feature_addr);
  93. if (cmd->tf_flags & IDE_TFLAG_OUT_NSECT)
  94. tx4938ide_outb(tf->nsect, io_ports->nsect_addr);
  95. if (cmd->tf_flags & IDE_TFLAG_OUT_LBAL)
  96. tx4938ide_outb(tf->lbal, io_ports->lbal_addr);
  97. if (cmd->tf_flags & IDE_TFLAG_OUT_LBAM)
  98. tx4938ide_outb(tf->lbam, io_ports->lbam_addr);
  99. if (cmd->tf_flags & IDE_TFLAG_OUT_LBAH)
  100. tx4938ide_outb(tf->lbah, io_ports->lbah_addr);
  101. if (cmd->tf_flags & IDE_TFLAG_OUT_DEVICE)
  102. tx4938ide_outb((tf->device & HIHI) | drive->select,
  103. io_ports->device_addr);
  104. }
  105. static void tx4938ide_tf_read(ide_drive_t *drive, struct ide_cmd *cmd)
  106. {
  107. ide_hwif_t *hwif = drive->hwif;
  108. struct ide_io_ports *io_ports = &hwif->io_ports;
  109. struct ide_taskfile *tf = &cmd->tf;
  110. /* be sure we're looking at the low order bits */
  111. tx4938ide_outb(ATA_DEVCTL_OBS, io_ports->ctl_addr);
  112. if (cmd->tf_flags & IDE_TFLAG_IN_ERROR)
  113. tf->error = tx4938ide_inb(io_ports->feature_addr);
  114. if (cmd->tf_flags & IDE_TFLAG_IN_NSECT)
  115. tf->nsect = tx4938ide_inb(io_ports->nsect_addr);
  116. if (cmd->tf_flags & IDE_TFLAG_IN_LBAL)
  117. tf->lbal = tx4938ide_inb(io_ports->lbal_addr);
  118. if (cmd->tf_flags & IDE_TFLAG_IN_LBAM)
  119. tf->lbam = tx4938ide_inb(io_ports->lbam_addr);
  120. if (cmd->tf_flags & IDE_TFLAG_IN_LBAH)
  121. tf->lbah = tx4938ide_inb(io_ports->lbah_addr);
  122. if (cmd->tf_flags & IDE_TFLAG_IN_DEVICE)
  123. tf->device = tx4938ide_inb(io_ports->device_addr);
  124. if (cmd->tf_flags & IDE_TFLAG_LBA48) {
  125. tx4938ide_outb(ATA_HOB | ATA_DEVCTL_OBS, io_ports->ctl_addr);
  126. if (cmd->tf_flags & IDE_TFLAG_IN_HOB_ERROR)
  127. tf->hob_error = tx4938ide_inb(io_ports->feature_addr);
  128. if (cmd->tf_flags & IDE_TFLAG_IN_HOB_NSECT)
  129. tf->hob_nsect = tx4938ide_inb(io_ports->nsect_addr);
  130. if (cmd->tf_flags & IDE_TFLAG_IN_HOB_LBAL)
  131. tf->hob_lbal = tx4938ide_inb(io_ports->lbal_addr);
  132. if (cmd->tf_flags & IDE_TFLAG_IN_HOB_LBAM)
  133. tf->hob_lbam = tx4938ide_inb(io_ports->lbam_addr);
  134. if (cmd->tf_flags & IDE_TFLAG_IN_HOB_LBAH)
  135. tf->hob_lbah = tx4938ide_inb(io_ports->lbah_addr);
  136. }
  137. }
  138. static void tx4938ide_input_data_swap(ide_drive_t *drive, struct ide_cmd *cmd,
  139. void *buf, unsigned int len)
  140. {
  141. unsigned long port = drive->hwif->io_ports.data_addr;
  142. unsigned short *ptr = buf;
  143. unsigned int count = (len + 1) / 2;
  144. while (count--)
  145. *ptr++ = cpu_to_le16(__raw_readw((void __iomem *)port));
  146. __ide_flush_dcache_range((unsigned long)buf, roundup(len, 2));
  147. }
  148. static void tx4938ide_output_data_swap(ide_drive_t *drive, struct ide_cmd *cmd,
  149. void *buf, unsigned int len)
  150. {
  151. unsigned long port = drive->hwif->io_ports.data_addr;
  152. unsigned short *ptr = buf;
  153. unsigned int count = (len + 1) / 2;
  154. while (count--) {
  155. __raw_writew(le16_to_cpu(*ptr), (void __iomem *)port);
  156. ptr++;
  157. }
  158. __ide_flush_dcache_range((unsigned long)buf, roundup(len, 2));
  159. }
  160. static const struct ide_tp_ops tx4938ide_tp_ops = {
  161. .exec_command = ide_exec_command,
  162. .read_status = ide_read_status,
  163. .read_altstatus = ide_read_altstatus,
  164. .write_devctl = ide_write_devctl,
  165. .dev_select = ide_dev_select,
  166. .tf_load = tx4938ide_tf_load,
  167. .tf_read = tx4938ide_tf_read,
  168. .input_data = tx4938ide_input_data_swap,
  169. .output_data = tx4938ide_output_data_swap,
  170. };
  171. #endif /* __BIG_ENDIAN */
  172. static const struct ide_port_ops tx4938ide_port_ops = {
  173. .set_pio_mode = tx4938ide_set_pio_mode,
  174. };
  175. static const struct ide_port_info tx4938ide_port_info __initdata = {
  176. .port_ops = &tx4938ide_port_ops,
  177. #ifdef __BIG_ENDIAN
  178. .tp_ops = &tx4938ide_tp_ops,
  179. #endif
  180. .host_flags = IDE_HFLAG_MMIO | IDE_HFLAG_NO_DMA,
  181. .pio_mask = ATA_PIO5,
  182. .chipset = ide_generic,
  183. };
  184. static int __init tx4938ide_probe(struct platform_device *pdev)
  185. {
  186. hw_regs_t hw;
  187. hw_regs_t *hws[] = { &hw, NULL, NULL, NULL };
  188. struct ide_host *host;
  189. struct resource *res;
  190. struct tx4938ide_platform_info *pdata = pdev->dev.platform_data;
  191. int irq, ret, i;
  192. unsigned long mapbase, mapctl;
  193. struct ide_port_info d = tx4938ide_port_info;
  194. irq = platform_get_irq(pdev, 0);
  195. if (irq < 0)
  196. return -ENODEV;
  197. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  198. if (!res)
  199. return -ENODEV;
  200. if (!devm_request_mem_region(&pdev->dev, res->start,
  201. res->end - res->start + 1, "tx4938ide"))
  202. return -EBUSY;
  203. mapbase = (unsigned long)devm_ioremap(&pdev->dev, res->start,
  204. 8 << pdata->ioport_shift);
  205. mapctl = (unsigned long)devm_ioremap(&pdev->dev,
  206. res->start + 0x10000 +
  207. (6 << pdata->ioport_shift),
  208. 1 << pdata->ioport_shift);
  209. if (!mapbase || !mapctl)
  210. return -EBUSY;
  211. memset(&hw, 0, sizeof(hw));
  212. if (pdata->ioport_shift) {
  213. unsigned long port = mapbase;
  214. unsigned long ctl = mapctl;
  215. hw.io_ports_array[0] = port;
  216. #ifdef __BIG_ENDIAN
  217. port++;
  218. ctl++;
  219. #endif
  220. for (i = 1; i <= 7; i++)
  221. hw.io_ports_array[i] =
  222. port + (i << pdata->ioport_shift);
  223. hw.io_ports.ctl_addr = ctl;
  224. } else
  225. ide_std_init_ports(&hw, mapbase, mapctl);
  226. hw.irq = irq;
  227. hw.dev = &pdev->dev;
  228. pr_info("TX4938 IDE interface (base %#lx, ctl %#lx, irq %d)\n",
  229. mapbase, mapctl, hw.irq);
  230. if (pdata->gbus_clock)
  231. tx4938ide_tune_ebusc(pdata->ebus_ch, pdata->gbus_clock, 0);
  232. else
  233. d.port_ops = NULL;
  234. ret = ide_host_add(&d, hws, &host);
  235. if (!ret)
  236. platform_set_drvdata(pdev, host);
  237. return ret;
  238. }
  239. static int __exit tx4938ide_remove(struct platform_device *pdev)
  240. {
  241. struct ide_host *host = platform_get_drvdata(pdev);
  242. ide_host_remove(host);
  243. return 0;
  244. }
  245. static struct platform_driver tx4938ide_driver = {
  246. .driver = {
  247. .name = "tx4938ide",
  248. .owner = THIS_MODULE,
  249. },
  250. .remove = __exit_p(tx4938ide_remove),
  251. };
  252. static int __init tx4938ide_init(void)
  253. {
  254. return platform_driver_probe(&tx4938ide_driver, tx4938ide_probe);
  255. }
  256. static void __exit tx4938ide_exit(void)
  257. {
  258. platform_driver_unregister(&tx4938ide_driver);
  259. }
  260. module_init(tx4938ide_init);
  261. module_exit(tx4938ide_exit);
  262. MODULE_DESCRIPTION("TX4938 internal IDE driver");
  263. MODULE_LICENSE("GPL");
  264. MODULE_ALIAS("platform:tx4938ide");