tx4938ide.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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/txx9/tx4938.h>
  18. static void tx4938ide_tune_ebusc(unsigned int ebus_ch,
  19. unsigned int gbus_clock,
  20. u8 pio)
  21. {
  22. struct ide_timing *t = ide_timing_find_mode(XFER_PIO_0 + pio);
  23. u64 cr = __raw_readq(&tx4938_ebuscptr->cr[ebus_ch]);
  24. unsigned int sp = (cr >> 4) & 3;
  25. unsigned int clock = gbus_clock / (4 - sp);
  26. unsigned int cycle = 1000000000 / clock;
  27. unsigned int wt, shwt;
  28. /* Minimum DIOx- active time */
  29. wt = DIV_ROUND_UP(t->act8b, cycle) - 2;
  30. /* IORDY setup time: 35ns */
  31. wt = max(wt, DIV_ROUND_UP(35, cycle));
  32. /* actual wait-cycle is max(wt & ~1, 1) */
  33. if (wt > 2 && (wt & 1))
  34. wt++;
  35. wt &= ~1;
  36. /* Address-valid to DIOR/DIOW setup */
  37. shwt = DIV_ROUND_UP(t->setup, cycle);
  38. /* -DIOx recovery time (SHWT * 4) and cycle time requirement */
  39. while ((shwt * 4 + wt + (wt ? 2 : 3)) * cycle < t->cycle)
  40. shwt++;
  41. if (shwt > 7) {
  42. pr_warning("tx4938ide: SHWT violation (%d)\n", shwt);
  43. shwt = 7;
  44. }
  45. pr_debug("tx4938ide: ebus %d, bus cycle %dns, WT %d, SHWT %d\n",
  46. ebus_ch, cycle, wt, shwt);
  47. __raw_writeq((cr & ~0x3f007ull) | (wt << 12) | shwt,
  48. &tx4938_ebuscptr->cr[ebus_ch]);
  49. }
  50. static void tx4938ide_set_pio_mode(ide_drive_t *drive, const u8 pio)
  51. {
  52. ide_hwif_t *hwif = drive->hwif;
  53. struct tx4938ide_platform_info *pdata = hwif->dev->platform_data;
  54. u8 safe = pio;
  55. ide_drive_t *pair;
  56. pair = ide_get_pair_dev(drive);
  57. if (pair)
  58. safe = min(safe, ide_get_best_pio_mode(pair, 255, 5));
  59. tx4938ide_tune_ebusc(pdata->ebus_ch, pdata->gbus_clock, safe);
  60. }
  61. #ifdef __BIG_ENDIAN
  62. /* custom iops (independent from SWAP_IO_SPACE) */
  63. static u8 tx4938ide_inb(unsigned long port)
  64. {
  65. return __raw_readb((void __iomem *)port);
  66. }
  67. static void tx4938ide_outb(u8 value, unsigned long port)
  68. {
  69. __raw_writeb(value, (void __iomem *)port);
  70. }
  71. static void tx4938ide_tf_load(ide_drive_t *drive, ide_task_t *task)
  72. {
  73. ide_hwif_t *hwif = drive->hwif;
  74. struct ide_io_ports *io_ports = &hwif->io_ports;
  75. struct ide_taskfile *tf = &task->tf;
  76. u8 HIHI = task->tf_flags & IDE_TFLAG_LBA48 ? 0xE0 : 0xEF;
  77. if (task->tf_flags & IDE_TFLAG_FLAGGED)
  78. HIHI = 0xFF;
  79. if (task->tf_flags & IDE_TFLAG_OUT_DATA) {
  80. u16 data = (tf->hob_data << 8) | tf->data;
  81. /* no endian swap */
  82. __raw_writew(data, (void __iomem *)io_ports->data_addr);
  83. }
  84. if (task->tf_flags & IDE_TFLAG_OUT_HOB_FEATURE)
  85. tx4938ide_outb(tf->hob_feature, io_ports->feature_addr);
  86. if (task->tf_flags & IDE_TFLAG_OUT_HOB_NSECT)
  87. tx4938ide_outb(tf->hob_nsect, io_ports->nsect_addr);
  88. if (task->tf_flags & IDE_TFLAG_OUT_HOB_LBAL)
  89. tx4938ide_outb(tf->hob_lbal, io_ports->lbal_addr);
  90. if (task->tf_flags & IDE_TFLAG_OUT_HOB_LBAM)
  91. tx4938ide_outb(tf->hob_lbam, io_ports->lbam_addr);
  92. if (task->tf_flags & IDE_TFLAG_OUT_HOB_LBAH)
  93. tx4938ide_outb(tf->hob_lbah, io_ports->lbah_addr);
  94. if (task->tf_flags & IDE_TFLAG_OUT_FEATURE)
  95. tx4938ide_outb(tf->feature, io_ports->feature_addr);
  96. if (task->tf_flags & IDE_TFLAG_OUT_NSECT)
  97. tx4938ide_outb(tf->nsect, io_ports->nsect_addr);
  98. if (task->tf_flags & IDE_TFLAG_OUT_LBAL)
  99. tx4938ide_outb(tf->lbal, io_ports->lbal_addr);
  100. if (task->tf_flags & IDE_TFLAG_OUT_LBAM)
  101. tx4938ide_outb(tf->lbam, io_ports->lbam_addr);
  102. if (task->tf_flags & IDE_TFLAG_OUT_LBAH)
  103. tx4938ide_outb(tf->lbah, io_ports->lbah_addr);
  104. if (task->tf_flags & IDE_TFLAG_OUT_DEVICE)
  105. tx4938ide_outb((tf->device & HIHI) | drive->select,
  106. io_ports->device_addr);
  107. }
  108. static void tx4938ide_tf_read(ide_drive_t *drive, ide_task_t *task)
  109. {
  110. ide_hwif_t *hwif = drive->hwif;
  111. struct ide_io_ports *io_ports = &hwif->io_ports;
  112. struct ide_taskfile *tf = &task->tf;
  113. if (task->tf_flags & IDE_TFLAG_IN_DATA) {
  114. u16 data;
  115. /* no endian swap */
  116. data = __raw_readw((void __iomem *)io_ports->data_addr);
  117. tf->data = data & 0xff;
  118. tf->hob_data = (data >> 8) & 0xff;
  119. }
  120. /* be sure we're looking at the low order bits */
  121. tx4938ide_outb(ATA_DEVCTL_OBS & ~0x80, io_ports->ctl_addr);
  122. if (task->tf_flags & IDE_TFLAG_IN_FEATURE)
  123. tf->feature = tx4938ide_inb(io_ports->feature_addr);
  124. if (task->tf_flags & IDE_TFLAG_IN_NSECT)
  125. tf->nsect = tx4938ide_inb(io_ports->nsect_addr);
  126. if (task->tf_flags & IDE_TFLAG_IN_LBAL)
  127. tf->lbal = tx4938ide_inb(io_ports->lbal_addr);
  128. if (task->tf_flags & IDE_TFLAG_IN_LBAM)
  129. tf->lbam = tx4938ide_inb(io_ports->lbam_addr);
  130. if (task->tf_flags & IDE_TFLAG_IN_LBAH)
  131. tf->lbah = tx4938ide_inb(io_ports->lbah_addr);
  132. if (task->tf_flags & IDE_TFLAG_IN_DEVICE)
  133. tf->device = tx4938ide_inb(io_ports->device_addr);
  134. if (task->tf_flags & IDE_TFLAG_LBA48) {
  135. tx4938ide_outb(ATA_DEVCTL_OBS | 0x80, io_ports->ctl_addr);
  136. if (task->tf_flags & IDE_TFLAG_IN_HOB_FEATURE)
  137. tf->hob_feature =
  138. tx4938ide_inb(io_ports->feature_addr);
  139. if (task->tf_flags & IDE_TFLAG_IN_HOB_NSECT)
  140. tf->hob_nsect = tx4938ide_inb(io_ports->nsect_addr);
  141. if (task->tf_flags & IDE_TFLAG_IN_HOB_LBAL)
  142. tf->hob_lbal = tx4938ide_inb(io_ports->lbal_addr);
  143. if (task->tf_flags & IDE_TFLAG_IN_HOB_LBAM)
  144. tf->hob_lbam = tx4938ide_inb(io_ports->lbam_addr);
  145. if (task->tf_flags & IDE_TFLAG_IN_HOB_LBAH)
  146. tf->hob_lbah = tx4938ide_inb(io_ports->lbah_addr);
  147. }
  148. }
  149. static void tx4938ide_input_data_swap(ide_drive_t *drive, struct request *rq,
  150. void *buf, unsigned int len)
  151. {
  152. unsigned long port = drive->hwif->io_ports.data_addr;
  153. unsigned short *ptr = buf;
  154. unsigned int count = (len + 1) / 2;
  155. while (count--)
  156. *ptr++ = cpu_to_le16(__raw_readw((void __iomem *)port));
  157. __ide_flush_dcache_range((unsigned long)buf, count * 2);
  158. }
  159. static void tx4938ide_output_data_swap(ide_drive_t *drive, struct request *rq,
  160. void *buf, unsigned int len)
  161. {
  162. unsigned long port = drive->hwif->io_ports.data_addr;
  163. unsigned short *ptr = buf;
  164. unsigned int count = (len + 1) / 2;
  165. while (count--) {
  166. __raw_writew(le16_to_cpu(*ptr), (void __iomem *)port);
  167. ptr++;
  168. }
  169. __ide_flush_dcache_range((unsigned long)buf, count * 2);
  170. }
  171. static const struct ide_tp_ops tx4938ide_tp_ops = {
  172. .exec_command = ide_exec_command,
  173. .read_status = ide_read_status,
  174. .read_altstatus = ide_read_altstatus,
  175. .read_sff_dma_status = ide_read_sff_dma_status,
  176. .set_irq = ide_set_irq,
  177. .tf_load = tx4938ide_tf_load,
  178. .tf_read = tx4938ide_tf_read,
  179. .input_data = tx4938ide_input_data_swap,
  180. .output_data = tx4938ide_output_data_swap,
  181. };
  182. #endif /* __BIG_ENDIAN */
  183. static const struct ide_port_ops tx4938ide_port_ops = {
  184. .set_pio_mode = tx4938ide_set_pio_mode,
  185. };
  186. static const struct ide_port_info tx4938ide_port_info __initdata = {
  187. .port_ops = &tx4938ide_port_ops,
  188. #ifdef __BIG_ENDIAN
  189. .tp_ops = &tx4938ide_tp_ops,
  190. #endif
  191. .host_flags = IDE_HFLAG_MMIO | IDE_HFLAG_NO_DMA,
  192. .pio_mask = ATA_PIO5,
  193. };
  194. static int __init tx4938ide_probe(struct platform_device *pdev)
  195. {
  196. hw_regs_t hw;
  197. hw_regs_t *hws[] = { &hw, NULL, NULL, NULL };
  198. struct ide_host *host;
  199. struct resource *res;
  200. struct tx4938ide_platform_info *pdata = pdev->dev.platform_data;
  201. int irq, ret, i;
  202. unsigned long mapbase;
  203. struct ide_port_info d = tx4938ide_port_info;
  204. irq = platform_get_irq(pdev, 0);
  205. if (irq < 0)
  206. return -ENODEV;
  207. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  208. if (!res)
  209. return -ENODEV;
  210. if (!devm_request_mem_region(&pdev->dev, res->start,
  211. res->end - res->start + 1, "tx4938ide"))
  212. return -EBUSY;
  213. mapbase = (unsigned long)devm_ioremap(&pdev->dev, res->start,
  214. res->end - res->start + 1);
  215. if (!mapbase)
  216. return -EBUSY;
  217. memset(&hw, 0, sizeof(hw));
  218. if (pdata->ioport_shift) {
  219. unsigned long port = mapbase;
  220. hw.io_ports_array[0] = port;
  221. #ifdef __BIG_ENDIAN
  222. port++;
  223. #endif
  224. for (i = 1; i <= 7; i++)
  225. hw.io_ports_array[i] =
  226. port + (i << pdata->ioport_shift);
  227. hw.io_ports.ctl_addr =
  228. port + 0x10000 + (6 << pdata->ioport_shift);
  229. } else
  230. ide_std_init_ports(&hw, mapbase, mapbase + 0x10006);
  231. hw.irq = irq;
  232. hw.dev = &pdev->dev;
  233. pr_info("TX4938 IDE interface (base %#lx, irq %d)\n", mapbase, hw.irq);
  234. if (pdata->gbus_clock)
  235. tx4938ide_tune_ebusc(pdata->ebus_ch, pdata->gbus_clock, 0);
  236. else
  237. d.port_ops = NULL;
  238. ret = ide_host_add(&d, hws, &host);
  239. if (ret)
  240. return ret;
  241. platform_set_drvdata(pdev, host);
  242. return 0;
  243. }
  244. static int __exit tx4938ide_remove(struct platform_device *pdev)
  245. {
  246. struct ide_host *host = platform_get_drvdata(pdev);
  247. ide_host_remove(host);
  248. return 0;
  249. }
  250. static struct platform_driver tx4938ide_driver = {
  251. .driver = {
  252. .name = "tx4938ide",
  253. .owner = THIS_MODULE,
  254. },
  255. .remove = __exit_p(tx4938ide_remove),
  256. };
  257. static int __init tx4938ide_init(void)
  258. {
  259. return platform_driver_probe(&tx4938ide_driver, tx4938ide_probe);
  260. }
  261. static void __exit tx4938ide_exit(void)
  262. {
  263. platform_driver_unregister(&tx4938ide_driver);
  264. }
  265. module_init(tx4938ide_init);
  266. module_exit(tx4938ide_exit);
  267. MODULE_DESCRIPTION("TX4938 internal IDE driver");
  268. MODULE_LICENSE("GPL");
  269. MODULE_ALIAS("platform:tx4938ide");