tx4938ide.c 9.2 KB

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